-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSearchlightDatasource.inc
More file actions
572 lines (521 loc) · 18.6 KB
/
Copy pathSearchlightDatasource.inc
File metadata and controls
572 lines (521 loc) · 18.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
<?php
class SearchlightDatasource {
var $id;
var $name;
var $base_table;
var $base_field;
var $fields;
var $filters;
var $options;
var $view;
function construct() {
$this->name = isset($this->name) ? $this->name : '';
$this->base_table = isset($this->base_table) ? $this->base_table : '';
$this->base_field = '';
if ($views_data = views_fetch_data($this->base_table)) {
$this->base_field = $views_data['table']['base']['field'];
}
$this->fields = !empty($this->fields) ? $this->fields : array();
$this->filters = !empty($this->filters) ? $this->filters : array();
$this->options = !empty($this->options) ? $this->options : array();
$this->options = $this->options + array(
'node_access' => TRUE,
);
$this->setId();
}
function setId() {
global $db_url;
$url = is_array($db_url) ? $db_url['default'] : $db_url;
$url = parse_url($url);
$this->id = trim(urldecode($url['path']), '/') .'_'. $this->name;
return $this;
}
function init() {
return empty($this->view) ? $this->viewInit()->viewSetHandlers() : $this;
}
function viewInit() {
views_include('view');
$this->view = new view;
$this->view->base_table = $this->base_table;
$this->view->api_version = '3.0-alpha1';
$this->view->new_display('default', 'Default', 'default');
$this->view->set_display('default');
return $this;
}
function viewSetHandlers() {
$handler = $this->view->display_handler;
$handler->override_option('arguments', array());
$handler->override_option('filters', array());
$handler->override_option('relationships', array());
$handler->override_option('sorts', array());
$fields = array();
// Force the base field onto the view if possible.
if ($this->base_field) {
$fields[$this->base_field] = array(
'id' => $this->base_field,
'table' => $this->base_table,
'field' => $this->base_field,
'relationship' => 'none'
);
}
foreach ($this->getFields() as $name => $field) {
$fields[$name] = array(
'id' => $field['name'],
'table' => $field['table'],
'field' => $field['field'],
'relationship' => 'none'
);
}
$handler->override_option('fields', $fields);
return $this;
}
/**
* Given a Views handler return one of the datasource datatypes based on the
* database column type and other metadata. May be one of the following
* datasource attribute types:
*
* `text`: varchar or text column.
* `int`: serial or int column.
* `float`: float column.
* `timestamp`: unix timestamp.
*/
function getDatatype($handler = NULL, $table, $field) {
$attribute = array();
// Ensure that pseudo table names are traced back to their literal tables
// that actually have a schema definition.
$data = views_fetch_data();
if (isset($data[$table]['table']['join'])) {
foreach ($data[$table]['table']['join'] as $join_info) {
if (isset($join_info['table']) && $join_info['table'] !== $table) {
$table = $join_info['table'];
break;
}
}
}
$schema = drupal_get_schema($table);
if ($schema && isset($field, $schema['fields'][$field])) {
$class = $handler ? get_class($handler) : NULL;
// Get the datasource attribute type.
// We use the handler class for special cases like timestamp where DB
// column type is not enough information to determine the usage of the
// field.
$map = array(
'serial' => 'int',
'int' => 'int',
'varchar' => 'text',
'text' => 'text',
'float' => 'float',
);
if (isset($map[$schema['fields'][$field]['type']])) {
$column_type = $map[$schema['fields'][$field]['type']];
if ($column_type === 'int' && strpos($class, 'date') !== FALSE) {
return 'timestamp';
}
return isset($map[$column_type]) ? $map[$column_type] : FALSE;
}
}
// Handle a list of known function/calculation fields.
$map = array(
'node_comment_statistics.last_updated' => 'timestamp',
);
return isset($map["{$table}.{$field}"]) ? $map["{$table}.{$field}"] : FALSE;
}
/**
* Retrieve fields currently enabled for this datasource.
*/
function getFields() {
$fields = array();
foreach ($this->fields as $name => $field) {
if ($field['usage'] != 'multivalue') {
$fields[$name] = $field;
}
}
return $fields;
}
/**
* Retrieve multivalue fields currently enabled for this datasource.
*/
function getMultivalues() {
$fields = array();
foreach ($this->fields as $name => $field) {
if ($field['usage'] == 'multivalue') {
$fields[$name] = $field;
}
}
return $fields;
}
/**
* Retrieve all possible multivalue fields for this base table.
*/
function buildMultivalues() {
views_invalidate_cache();
$multivalues = array();
// Gather all views that have the multivalue display plugin.
$views = views_get_all_views();
$usable = array();
foreach ($views as $view) {
if ($view->base_table === $this->base_table) {
foreach ($view->display as $display) {
if ($display->display_plugin === 'searchlight_multivalue') {
$usable[$view->name] = $view;
}
}
}
}
foreach ($usable as $view) {
if ($view->name !== 'searchlight_node_access') {
foreach ($view->display as $display) {
if ($display->display_plugin === 'searchlight_multivalue') {
$view = views_get_view($view->name);
$view->set_display($display->id);
$multivalue = $view->execute_display();
$multivalues[$multivalue['name']] = $multivalue;
$view->destroy();
}
}
}
}
return $multivalues;
}
/**
* Build and retrieve node_access multivalue information.
*/
function buildNodeaccess() {
views_invalidate_cache();
$nodeaccess = array();
if ($this->options['node_access'] && $this->base_table === 'node' && $view = views_get_view('searchlight_node_access')) {
foreach ($view->display as $display) {
if ($display->display_plugin === 'searchlight_multivalue') {
$view = views_get_view($view->name);
$view->set_display($display->id);
$multivalue = $view->execute_display();
$nodeaccess[$multivalue['name']] = $multivalue;
$view->destroy();
}
}
}
return $nodeaccess;
}
/**
* Provide the default form for setting options.
*/
function optionsForm(&$form, &$form_state) {
views_include('admin');
views_include('form');
// Theme function will handle formatting of datasource information.
$form['#theme'] = 'searchlight_admin_datasource';
$form['#datasource'] = $this;
// Calculations of indexing percentage.
$result = db_query("SELECT count(id) AS count, status FROM {searchlight_search} WHERE type = '%s' GROUP BY status", $this->base_table);
$items_indexed = 0;
$items_total = 0;
while ($row = db_fetch_object($result)) {
$items_indexed = $row->status == 1 ? $row->count : $items_indexed;
$items_total = $items_total + $row->count;
}
$form['index']['#tree'] = TRUE;
$form['index']['percent'] = array(
'#type' => 'markup',
'#value' => !empty($items_total) ? number_format(($items_indexed / $items_total * 100), 1) . '%' : '0%',
);
$form['index']['counts'] = array(
'#type' => 'markup',
'#value' => t('@indexed of @total total', array(
'@indexed' => $items_indexed,
'@total' => $items_total,
)),
);
$form['index']['invalidate'] = array(
'#type' => 'submit',
'#value' => t('Queue for reindexing'),
);
$form['help'] = array(
'#type' => 'item',
'#value' => t('Choose a usage type for each field in the search datasource. <strong>Content</strong> fields will be used to perform text searches. <strong>Attributes</strong> can be used to filter, sort or group the search results.')
);
$form['fields'] = array(
'#prefix' => "<div id='datasource-fields'>",
'#suffix' => "</div>",
'#tree' => TRUE,
'#theme' => 'searchlight_admin_datasource_fields',
'#title' => t('Fields'),
);
// Add fields
$field_options = array();
$fields = views_fetch_fields($this->base_table, 'field', TRUE);
foreach ($fields as $field_id => $info) {
$field_options[$info['group']][$field_id] = $info['title'];
}
$form['fields']['new']['#tree'] = TRUE;
$form['fields']['new']['field'] = array(
'#type' => 'select',
'#options' => $field_options,
);
$form['fields']['new']['add'] = array(
'#value' => t('Add field'),
'#type' => 'submit',
'#submit' => array('searchlight_admin_datasource_edit_submit'),
'#ahah' => array(
'path' => 'admin/settings/search/datasource/ahah/edit-fields-new-add/fields',
'wrapper' => 'datasource-fields',
'method' => 'replace',
),
);
// Remove fields
if (count($this->getFields())) {
$form['fields']['remove'] = array(
'#value' => t('Remove selected fields'),
'#type' => 'submit',
'#submit' => array('searchlight_admin_datasource_edit_submit'),
'#ahah' => array(
'path' => 'admin/settings/search/datasource/ahah/edit-fields-remove/fields',
'wrapper' => 'datasource-fields',
'method' => 'replace',
),
);
}
// Adjust existing fields
$form['fields']['fields'] = array('#tree' => TRUE);
foreach ($this->getFields() as $name => $info) {
$form['fields']['fields'][$name] = array();
// Remove field checkbox.
$form['fields']['fields'][$name]['remove'] = array(
'#type' => 'checkbox',
'#default_value' => FALSE,
);
// Field label.
$form['fields']['fields'][$name]['label'] = array(
'#type' => 'markup',
'#value' => $info['label'],
);
// Datatype
$form['fields']['fields'][$name]['datatype'] = array(
'#type' => 'markup',
'#value' => "<code>{$info['datatype']}</code>",
);
// Usage
$default_usage = ($info['datatype'] === 'text') ? 'content' : 'attribute';
$default_usage = isset($this->fields[$name]['usage']) ? $this->fields[$name]['usage'] : $default_usage;
$form['fields']['fields'][$name]['usage'] = array(
'#type' => 'select',
'#options' => array( 'content' => t('Content'), 'attribute' => t('Attribute')),
'#default_value' => $default_usage,
);
}
// @TODO: Handle multivalues.
$form['multivalues'] = array(
'#tree' => TRUE,
'#theme' => 'searchlight_admin_datasource_fields',
'#title' => t('Multivalues'),
'#prefix' => "<div id='datasource-multivalues'>",
'#suffix' => "</div>",
);
// Add fields
$multivalue_options = array();
foreach ($this->buildMultivalues() as $name => $info) {
if (!array_key_exists($name, $this->getMultivalues())) {
$multivalue_options[$name] = $info['label'];
}
}
$form['multivalues']['new']['#tree'] = TRUE;
$form['multivalues']['new']['field'] = array(
'#type' => 'select',
'#options' => $multivalue_options,
);
$form['multivalues']['new']['add'] = array(
'#value' => t('Add multivalue'),
'#type' => 'submit',
'#submit' => array('searchlight_admin_datasource_edit_submit'),
'#ahah' => array(
'path' => 'admin/settings/search/datasource/ahah/edit-multivalues-new-add/multivalues',
'wrapper' => 'datasource-multivalues',
'method' => 'replace',
),
);
// Remove fields
if (count($this->getMultivalues())) {
$form['multivalues']['remove'] = array(
'#value' => t('Remove selected multivalues'),
'#type' => 'submit',
'#submit' => array('searchlight_admin_datasource_edit_submit'),
'#ahah' => array(
'path' => 'admin/settings/search/datasource/ahah/edit-multivalues-remove/multivalues',
'wrapper' => 'datasource-multivalues',
'method' => 'replace',
),
);
}
foreach ($this->getMultivalues() as $name => $info) {
$form['multivalues']['fields'][$name] = array();
// Remove field checkbox.
$form['multivalues']['fields'][$name]['remove'] = array(
'#type' => 'checkbox',
'#default_value' => FALSE,
);
// Field label.
$form['multivalues']['fields'][$name]['label'] = array(
'#type' => 'markup',
'#value' => $info['label'],
);
// Datatype
$form['multivalues']['fields'][$name]['datatype'] = array(
'#type' => 'markup',
'#value' => "<code>{$info['datatype']}</code>",
);
$form['multivalues']['fields'][$name]['usage'] = array(
'#type' => 'markup',
'#value' => t('Multivalue'),
);
}
$form['options']['#tree'] = TRUE;
// In reality, these are multivalue fields that are generated
// programmatically.
if ($this->base_table === 'node') {
$form['options']['node_access'] = array(
'#title' => t('Node access'),
'#description' => t('Include node access information in datasource'),
'#type' => 'checkbox',
'#default_value' => $this->options['node_access'],
);
}
return $form;
}
/**
* Perform any necessary changes to the form values prior to storage.
* There is no need for this function to actually store the data.
*/
function optionsSubmit($form, &$form_state) {
switch ($form_state['clicked_button']['#id']) {
case 'edit-index-invalidate':
searchlight_datasource_save($this);
drupal_set_message(t('The index for this datasource needs to be rebuilt.'));
break;
case 'edit-fields-new-add':
list($table, $field) = explode('.', $form_state['values']['fields']['new']['field']);
$this->addField($table, $field);
searchlight_datasource_save($this, TRUE);
break;
case 'edit-fields-remove':
foreach ($form_state['values']['fields']['fields'] as $name => $values) {
if (!empty($values['remove'])) {
$this->removeField(NULL, NULL, $name);
}
}
searchlight_datasource_save($this, TRUE);
break;
case 'edit-multivalues-new-add':
$this->addMultivalue($form_state['values']['multivalues']['new']['field']);
searchlight_datasource_save($this, TRUE);
break;
case 'edit-multivalues-remove':
foreach ($form_state['values']['multivalues']['fields'] as $name => $values) {
if (!empty($values['remove'])) {
$this->removeMultivalue($name);
}
}
searchlight_datasource_save($this, TRUE);
break;
case 'edit-save':
// Save additional metadata from fields, multivalues.
foreach (array('fields', 'multivalues') as $key) {
if (!empty($form_state['values'][$key]['fields'])) {
foreach ($form_state['values'][$key]['fields'] as $name => $values) {
$values = array_diff_key($values, array('remove' => NULL));
if (isset($this->{$key}[$name])) {
$this->{$key}[$name] = array_merge($this->{$key}[$name], $values);
}
}
}
}
// Save options.
$this->options = $form_state['values']['options'];
// Save the datasource.
searchlight_datasource_save($this);
drupal_set_message(t('Datasource @datasource saved. The index for this datasource needs to be rebuilt.', array('@datasource' => $this->name)));
break;
}
}
function addField($table, $field) {
// Add the field to the view and build. This will give us an inited handler
// with full aliases.
$this->viewInit()->viewSetHandlers();
$fields = $this->view->display_handler->get_option('fields');
$fields[$field] = array(
'id' => $field,
'table' => $table,
'field' => $field,
'relationship' => 'none',
);
$this->view->display_handler->set_option('fields', $fields);
$this->view->build();
// Retrieve field information for storage with datasource.
$handler = isset($this->view->field[$field]) ? $this->view->field[$field] : NULL;
// Don't use alias for base field.
if ($field === $this->view->base_field) {
$this->fields[$field] = array(
'label' => $field,
'datatype' => $this->getDatatype(NULL, $table, $field),
'table' => $table,
'field' => $field,
'name' => $field,
);
}
// Use alias for all other fields.
if ($handler) {
$this->fields[$handler->field_alias] = array(
'label' => $handler->ui_name() . " ({$handler->real_field})",
'datatype' => $this->getDatatype($handler, $handler->table, $handler->real_field),
'table' => $handler->table,
'field' => $handler->real_field,
'name' => $handler->field_alias,
);
}
if (!empty($handler->additional_fields)) {
foreach ($handler->additional_fields as $identifier => $info) {
$table = $handler->table;
$field = $info;
// If the additional fields have specified table/field in an array,
// use these.
if (is_array($info) && isset($info['field'])) {
$table = isset($info['table']) ? $info['table'] : $table;
$field = $info['field'];
}
if ($field !== $handler->view->base_field || $table !== $handler->view->base_table) {
$this->fields[$handler->aliases[$field]] = array(
'label' => $handler->ui_name() . " ({$field})",
'datatype' => $this->getDatatype($handler, $table, $field),
'table' => $table,
'field' => $field,
'name' => $handler->aliases[$field],
);
}
}
}
return $this->viewInit()->viewSetHandlers();
}
function removeField($table = NULL, $field = NULL, $name = NULL) {
if (isset($name)) {
if (array_key_exists($name, $this->fields)) {
unset($this->fields[$name]);
}
}
else if (isset($table, $field)) {
foreach ($this->fields as $name => $field) {
if ($field['table'] === $table && $field['field'] === $field) {
unset($this->fields[$name]);
}
}
}
return $this->viewInit()->viewSetHandlers();
}
function addMultivalue($name) {
$multivalues = $this->buildMultivalues();
if (isset($multivalues[$name])) {
$this->fields[$name] = $multivalues[$name];
}
return $this;
}
function removeMultivalue($name) {
return $this->removeField(NULL, NULL, $name);
}
}