-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsearch_api.module
executable file
·3623 lines (3403 loc) · 117 KB
/
search_api.module
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Provides a flexible framework for implementing search services.
*/
/**
* Default number of items indexed per cron batch for each enabled index.
*/
define('SEARCH_API_DEFAULT_CRON_LIMIT', 50);
include_once BACKDROP_ROOT . '/core/includes/transliteration.inc';
/**
* Implements hook_menu().
*/
function search_api_menu() {
$pre = 'admin/config/search/search_api';
$items[$pre] = array(
'title' => 'Search API',
'description' => 'Create and configure search engines.',
'page callback' => 'search_api_admin_overview',
'access arguments' => array('administer search_api'),
'file' => 'search_api.admin.inc',
);
$items[$pre . '/overview'] = array(
'title' => 'Overview',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items[$pre . '/add_server'] = array(
'title' => 'Add server',
'description' => 'Create a new search server.',
'page callback' => 'backdrop_get_form',
'page arguments' => array('search_api_admin_add_server'),
'access arguments' => array('administer search_api'),
'file' => 'search_api.admin.inc',
'weight' => -1,
'type' => MENU_LOCAL_ACTION,
);
$items[$pre . '/add_index'] = array(
'title' => 'Add index',
'description' => 'Create a new search index.',
'page callback' => 'backdrop_get_form',
'page arguments' => array('search_api_admin_add_index'),
'access arguments' => array('administer search_api'),
'file' => 'search_api.admin.inc',
'type' => MENU_LOCAL_ACTION,
);
$items[$pre . '/server/%search_api_server'] = array(
'title' => 'View server',
'title callback' => 'search_api_admin_item_title',
'title arguments' => array(5),
'description' => 'View server details.',
'page callback' => 'search_api_admin_server_view',
'page arguments' => array(5),
'access arguments' => array('administer search_api'),
'file' => 'search_api.admin.inc',
);
$items[$pre . '/server/%search_api_server/view'] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items[$pre . '/server/%search_api_server/edit'] = array(
'title' => 'Edit',
'description' => 'Edit server details.',
'page callback' => 'backdrop_get_form',
'page arguments' => array('search_api_admin_server_edit', 5),
'access arguments' => array('administer search_api'),
'file' => 'search_api.admin.inc',
'weight' => -1,
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE | MENU_CONTEXT_PAGE,
);
$items[$pre . '/server/%search_api_server/execute-tasks'] = array(
'title' => 'Execute pending tasks',
'description' => 'Attempt to process pending tasks for a given server.',
'page callback' => 'search_api_execute_pending_tasks',
'page arguments' => array(5),
'access callback' => 'search_api_access_execute_tasks_batch',
'access arguments' => array(5),
'type' => MENU_CALLBACK,
);
$items[$pre . '/server/%search_api_server/disable'] = array(
'title' => 'Disable',
'description' => 'Disable index.',
'page callback' => 'search_api_admin_server_view',
'page arguments' => array(5, 6),
'access callback' => 'search_api_access_disable_page',
'access arguments' => array(5),
'file' => 'search_api.admin.inc',
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'weight' => 8,
);
$items[$pre . '/server/%search_api_server/delete'] = array(
'title' => 'Delete',
'title callback' => 'search_api_title_delete_page',
'title arguments' => array(5),
'description' => 'Delete server.',
'page callback' => 'backdrop_get_form',
'page arguments' => array('search_api_admin_confirm', 'server', 'delete', 5),
'access callback' => 'search_api_access_delete_page',
'access arguments' => array(5),
'file' => 'search_api.admin.inc',
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'weight' => 10,
);
$items[$pre . '/execute-tasks'] = array(
'title' => 'Execute pending tasks',
'description' => 'Attempt to process pending server tasks.',
'page callback' => 'search_api_execute_pending_tasks',
'access callback' => 'search_api_access_execute_tasks_batch',
'type' => MENU_LOCAL_ACTION,
);
$items[$pre . '/index/%search_api_index'] = array(
'title' => 'View index',
'title callback' => 'search_api_admin_item_title',
'title arguments' => array(5),
'description' => 'View index details.',
'page callback' => 'search_api_admin_index_view',
'page arguments' => array(5),
'access arguments' => array('administer search_api'),
'file' => 'search_api.admin.inc',
);
$items[$pre . '/index/%search_api_index/view'] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items[$pre . '/index/%search_api_index/edit'] = array(
'title' => 'Edit',
'description' => 'Edit index settings.',
'page callback' => 'backdrop_get_form',
'page arguments' => array('search_api_admin_index_edit', 5),
'access arguments' => array('administer search_api'),
'file' => 'search_api.admin.inc',
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE | MENU_CONTEXT_PAGE,
'weight' => -6,
);
$items[$pre . '/index/%search_api_index/fields'] = array(
'title' => 'Fields',
'description' => 'Select indexed fields.',
'page callback' => 'backdrop_get_form',
'page arguments' => array('search_api_admin_index_fields', 5),
'access arguments' => array('administer search_api'),
'file' => 'search_api.admin.inc',
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE | MENU_CONTEXT_PAGE,
'weight' => -4,
);
$items[$pre . '/index/%search_api_index/workflow'] = array(
'title' => 'Filters',
'description' => 'Edit indexing workflow.',
'page callback' => 'backdrop_get_form',
'page arguments' => array('search_api_admin_index_workflow', 5),
'access arguments' => array('administer search_api'),
'file' => 'search_api.admin.inc',
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE | MENU_CONTEXT_PAGE,
'weight' => -2,
);
$items[$pre . '/index/%search_api_index/disable'] = array(
'title' => 'Disable',
'description' => 'Disable index.',
'page callback' => 'search_api_admin_index_view',
'page arguments' => array(5, 6),
'access callback' => 'search_api_access_disable_page',
'access arguments' => array(5),
'file' => 'search_api.admin.inc',
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'weight' => 8,
);
$items[$pre . '/index/%search_api_index/delete'] = array(
'title' => 'Delete',
'title callback' => 'search_api_title_delete_page',
'title arguments' => array(5),
'description' => 'Delete index.',
'page callback' => 'backdrop_get_form',
'page arguments' => array('search_api_admin_confirm', 'index', 'delete', 5),
'access callback' => 'search_api_access_delete_page',
'access arguments' => array(5),
'file' => 'search_api.admin.inc',
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'weight' => 10,
);
return $items;
}
/**
* Implements hook_help().
*/
function search_api_help($path) {
switch ($path) {
case 'admin/help#search_api':
$classes = array();
foreach (search_api_get_service_info() as $id => $info) {
$id = backdrop_clean_css_identifier($id);
$name = check_plain($info['name']);
$description = isset($info['description']) ? $info['description'] : '';
$classes[] = "<h2 id=\"$id\">$name</h2>\n$description";
}
$output = '';
if ($classes) {
$output .= '<p>' . t('The following service classes are available for creating a search server.') . "</p>\n";
$output .= implode("\n\n", $classes);
}
return $output;
case 'admin/config/search/search_api':
return '<p>' . t('A search server and search index are used to execute searches. Several indexes can exist per server.<br />You need at least one server and one index to create searches on your site.') . '</p>';
}
}
/**
* Implements hook_hook_info().
*/
function search_api_hook_info() {
// We use the same group for all hooks, so save code lines.
$hook_info = array(
'group' => 'search_api',
);
return array(
'search_api_service_info' => $hook_info,
'search_api_service_info_alter' => $hook_info,
'search_api_item_type_info' => $hook_info,
'search_api_item_type_info_alter' => $hook_info,
'search_api_data_type_info' => $hook_info,
'search_api_data_type_info_alter' => $hook_info,
'search_api_alter_callback_info' => $hook_info,
'search_api_alter_callback_info_alter' => $hook_info,
'search_api_processor_info' => $hook_info,
'search_api_processor_info_alter' => $hook_info,
'search_api_index_items_alter' => $hook_info,
'search_api_items_indexed' => $hook_info,
'search_api_query_alter' => $hook_info,
'search_api_results_alter' => $hook_info,
'search_api_server_load' => $hook_info,
'search_api_server_insert' => $hook_info,
'search_api_server_update' => $hook_info,
'search_api_server_delete' => $hook_info,
'default_search_api_server' => $hook_info,
'default_search_api_server_alter' => $hook_info,
'search_api_index_load' => $hook_info,
'search_api_index_insert' => $hook_info,
'search_api_index_update' => $hook_info,
'search_api_index_reindex' => $hook_info,
'search_api_index_delete' => $hook_info,
'default_search_api_index' => $hook_info,
'default_search_api_index_alter' => $hook_info,
);
}
/**
* Implements hook_theme().
*/
function search_api_theme() {
$themes['search_api_server'] = array(
'variables' => array(
'id' => NULL,
'name' => '',
'machine_name' => '',
'description' => NULL,
'enabled' => NULL,
'class_id' => NULL,
'class_name' => NULL,
'class_description' => NULL,
'indexes' => array(),
'options' => array(),
'extra' => array(),
'status' => NULL,
),
'file' => 'search_api.admin.inc',
);
$themes['search_api_index'] = array(
'variables' => array(
'id' => NULL,
'name' => '',
'machine_name' => '',
'description' => NULL,
'item_type' => NULL,
'datasource_config' => NULL,
'enabled' => NULL,
'server' => NULL,
'options' => array(),
'fields' => array(),
'indexed_items' => 0,
'on_server' => NULL,
'total_items' => 0,
'read_only' => 0,
'status' => NULL,
),
'file' => 'search_api.admin.inc',
);
$themes['search_api_admin_item_order'] = array(
'render element' => 'element',
'file' => 'search_api.admin.inc',
);
$themes['search_api_admin_fields_table'] = array(
'render element' => 'element',
'file' => 'search_api.admin.inc',
);
return $themes;
}
/**
* Implements hook_permission().
*/
function search_api_permission() {
return array(
'administer search_api' => array(
'title' => t('Administer Search API'),
'description' => t('Create and configure Search API servers and indexes.'),
),
);
}
/**
* Implements hook_config_info().
*/
function search_api_config_info() {
$prefixes['search_api.settings'] = array(
'label' => t('Search API settings'),
'group' => t('Configuration'),
);
return $prefixes;
}
/**
* Implements hook_cron().
*
* This will first execute any pending server tasks. After that, items will
* be indexed on all enabled indexes with a non-zero cron limit. Indexing will
* run for the time set in the search_api_index_worker_callback_runtime variable
* (defaulting to 15 seconds), but will at least index one batch of items on
* each index.
*
* @see search_api_server_tasks_check()
*/
function search_api_cron() {
// Execute pending server tasks.
search_api_server_tasks_check();
// Load all enabled, not read-only indexes.
$conditions = array(
'enabled' => TRUE,
'read_only' => 0,
);
$indexes = search_api_index_load_multiple(FALSE, $conditions);
if (!$indexes) {
return;
}
// Remember servers which threw an exception.
$ignored_servers = array();
// Continue indexing, one batch from each index, until the time is up, but at
// least index one batch per index.
$end = time() + config_get('search_api.settings', 'search_api_index_worker_callback_runtime');
$first_pass = TRUE;
while (TRUE) {
if (!$indexes) {
break;
}
foreach ($indexes as $id => $index) {
if (!$first_pass && time() >= $end) {
break 2;
}
if (!empty($ignored_servers[$index->server])) {
continue;
}
$limit = isset($index->options['cron_limit'])
? $index->options['cron_limit']
: SEARCH_API_DEFAULT_CRON_LIMIT;
$num = 0;
if ($limit) {
try {
$num = search_api_index_items($index, $limit);
if ($num) {
$variables = array(
'@num' => $num,
'%name' => $index->name,
);
watchdog('search_api', 'Indexed @num items for index %name.', $variables, WATCHDOG_INFO);
}
}
catch (SearchApiException $e) {
// Exceptions will probably be caused by the server in most cases.
// Therefore, don't index for any index on this server.
$ignored_servers[$index->server] = TRUE;
$vars['%index'] = $index->name;
watchdog_exception('search_api', $e, '%type while trying to index items on %index: !message in %function (line %line of %file).', $vars);
}
}
if (!$num) {
// Couldn't index any items => stop indexing for this index in this
// cron run.
unset($indexes[$id]);
}
}
$first_pass = FALSE;
}
}
/**
* Implements hook_entity_info().
*/
function search_api_entity_info() {
$info['search_api_server'] = array(
'label' => t('Search server'),
'controller class' => 'EntityPlusControllerExportable',
'metadata controller class' => FALSE,
'entity class' => 'SearchApiServer',
'base table' => 'search_api_server',
'uri callback' => 'search_api_server_url',
'access callback' => 'search_api_entity_access',
'module' => 'search_api',
'exportable' => TRUE,
'entity keys' => array(
'id' => 'id',
'label' => 'name',
'name' => 'machine_name',
),
);
$info['search_api_index'] = array(
'label' => t('Search index'),
'controller class' => 'EntityPlusControllerExportable',
'metadata controller class' => FALSE,
'entity class' => 'SearchApiIndex',
'base table' => 'search_api_index',
'uri callback' => 'search_api_index_url',
'access callback' => 'search_api_entity_access',
'module' => 'search_api',
'exportable' => TRUE,
'entity keys' => array(
'id' => 'id',
'label' => 'name',
'name' => 'machine_name',
),
);
return $info;
}
/**
* Implements hook_entity_property_info().
*/
function search_api_entity_property_info() {
$info['search_api_server']['properties'] = array(
'id' => array(
'label' => t('ID'),
'type' => 'integer',
'description' => t('The primary identifier for a server.'),
'schema field' => 'id',
'validation callback' => 'entity_metadata_validate_integer_positive',
),
'name' => array(
'label' => t('Name'),
'type' => 'text',
'description' => t('The displayed name for a server.'),
'schema field' => 'name',
'required' => TRUE,
),
'machine_name' => array(
'label' => t('Machine name'),
'type' => 'token',
'description' => t('The internally used machine name for a server.'),
'schema field' => 'machine_name',
'required' => TRUE,
),
'description' => array(
'label' => t('Description'),
'type' => 'text',
'description' => t('The displayed description for a server.'),
'schema field' => 'description',
'sanitize' => 'filter_xss',
),
'class' => array(
'label' => t('Service class'),
'type' => 'text',
'description' => t('The ID of the service class to use for this server.'),
'schema field' => 'class',
'required' => TRUE,
),
'enabled' => array(
'label' => t('Enabled'),
'type' => 'boolean',
'description' => t('A flag indicating whether the server is enabled.'),
'schema field' => 'enabled',
),
'status' => array(
'label' => t('Status'),
'type' => 'integer',
'description' => t('Search API server status property'),
'schema field' => 'status',
'options list' => 'search_api_status_options_list',
),
'module' => array(
'label' => t('Module'),
'type' => 'text',
'description' => t('The name of the module from which this server originates.'),
'schema field' => 'module',
),
);
$info['search_api_index']['properties'] = array(
'id' => array(
'label' => t('ID'),
'type' => 'integer',
'description' => t('An integer identifying the index.'),
'schema field' => 'id',
'validation callback' => 'entity_metadata_validate_integer_positive',
),
'name' => array(
'label' => t('Name'),
'type' => 'text',
'description' => t('A name to be displayed for the index.'),
'schema field' => 'name',
'required' => TRUE,
),
'machine_name' => array(
'label' => t('Machine name'),
'type' => 'token',
'description' => t('The internally used machine name for an index.'),
'schema field' => 'machine_name',
'required' => TRUE,
),
'description' => array(
'label' => t('Description'),
'type' => 'text',
'description' => t("A string describing the index' use to users."),
'schema field' => 'description',
'sanitize' => 'filter_xss',
),
'server' => array(
'label' => t('Server ID'),
'type' => 'token',
'description' => t('The machine name of the search_api_server with which data should be indexed.'),
'schema field' => 'server',
),
'server_entity' => array(
'label' => t('Server'),
'type' => 'search_api_server',
'description' => t('The search_api_server with which data should be indexed.'),
'getter callback' => 'search_api_index_get_server',
),
'item_type' => array(
'label' => t('Item type'),
'type' => 'token',
'description' => t('The type of items stored in this index.'),
'schema field' => 'item_type',
'required' => TRUE,
),
'enabled' => array(
'label' => t('Enabled'),
'type' => 'boolean',
'description' => t('A flag indicating whether the index is enabled.'),
'schema field' => 'enabled',
),
'read_only' => array(
'label' => t('Read only'),
'type' => 'boolean',
'description' => t('A flag indicating whether the index is read-only.'),
'schema field' => 'read_only',
),
'status' => array(
'label' => t('Status'),
'type' => 'integer',
'description' => t('Search API index status property'),
'schema field' => 'status',
'options list' => 'search_api_status_options_list',
),
'module' => array(
'label' => t('Module'),
'type' => 'text',
'description' => t('The name of the module from which this index originates.'),
'schema field' => 'module',
),
);
return $info;
}
/**
* Implements hook_search_api_server_insert().
*
* Calls the postCreate() method for the server.
*/
function search_api_search_api_server_insert(SearchApiServer $server) {
// Check whether this is actually part of a revert.
$reverts = &backdrop_static('search_api_search_api_server_delete', array());
if (isset($reverts[$server->machine_name])) {
$server->original = $reverts[$server->machine_name];
unset($reverts[$server->machine_name]);
search_api_search_api_server_update($server);
unset($server->original);
return;
}
$server->postCreate();
}
/**
* Implements hook_search_api_server_update().
*
* Calls the server's postUpdate() method and marks all of this server's indexes
* for reindexing, if necessary.
*/
function search_api_search_api_server_update(SearchApiServer $server) {
if ($server->postUpdate()) {
foreach (search_api_index_load_multiple(FALSE, array('server' => $server->machine_name)) as $index) {
$index->reindex();
}
}
if (!empty($server->original) && $server->enabled != $server->original->enabled) {
if ($server->enabled) {
search_api_server_tasks_check($server);
}
else {
foreach (search_api_index_load_multiple(FALSE, array('server' => $server->machine_name)) as $index) {
$index->update(array('enabled' => 0, 'server' => NULL));
}
}
}
}
/**
* Implements hook_search_api_server_delete().
*
* Calls the preDelete() method for the server.
*/
function search_api_search_api_server_delete(SearchApiServer $server) {
// Only react on real delete, not revert.
if ($server->hasStatus(ENTITY_PLUS_IN_CODE)) {
$reverts = &backdrop_static(__FUNCTION__, array());
$reverts[$server->machine_name] = $server;
return;
}
$server->preDelete();
foreach (search_api_index_load_multiple(FALSE, array('server' => $server->machine_name)) as $index) {
$index->update(array('server' => NULL, 'enabled' => FALSE));
}
search_api_server_tasks_delete(NULL, $server);
}
/**
* Implements hook_search_api_index_insert().
*
* Adds the index to its server (if any) and starts tracking indexed items (if
* the index is enabled).
*/
function search_api_search_api_index_insert(SearchApiIndex $index) {
// Check whether this is actually part of a revert.
$reverts = &backdrop_static('search_api_search_api_index_delete', array());
if (isset($reverts[$index->machine_name])) {
$index->original = $reverts[$index->machine_name];
unset($reverts[$index->machine_name]);
search_api_search_api_index_update($index);
unset($index->original);
return;
}
$index->postCreate();
}
/**
* Implements hook_search_api_index_update().
*/
function search_api_search_api_index_update(SearchApiIndex $index) {
// Call the datasource update function with the tables this module provides.
search_api_index_update_datasource($index, 'search_api_item');
search_api_index_update_datasource($index, 'search_api_item_string_id');
// If the server was changed, we have to call the appropriate service class
// hook methods.
if ($index->server != $index->original->server) {
// Server changed - inform old and new ones.
if ($index->original->server) {
$old_server = search_api_server_load($index->original->server);
// The server might have changed because the old one was deleted:
if ($old_server) {
$old_server->removeIndex($index);
}
}
if ($index->server) {
try {
$new_server = $index->server(TRUE);
// If the server is enabled, we call addIndex(); otherwise, we save the task.
$new_server->addIndex($index);
}
catch (SearchApiException $e) {
watchdog_exception('search_api', $e);
// If the new server doesn't exist, we remove the index from all
// servers. Note that saving an entity in its own update hook is usually
// a recipe for disaster, but since we are only doing this if a server
// is set and remove the server here before saving, it should be safe
// enough.
$index->server = NULL;
$index->save();
}
}
// We also have to re-index all content.
_search_api_index_reindex($index);
}
// If the fields were changed, call the appropriate service class hook method
// and re-index the content, if necessary.
$old_fields = $index->original->options + array('fields' => array());
$old_fields = $old_fields['fields'];
$new_fields = $index->options + array('fields' => array());
$new_fields = $new_fields['fields'];
if ($old_fields != $new_fields) {
if ($index->server) {
$index->server()->fieldsUpdated($index);
}
}
// If the index's enabled or read-only status is being changed, queue or
// dequeue items for indexing.
if (!$index->read_only && $index->enabled != $index->original->enabled) {
if ($index->enabled) {
$index->queueItems();
}
else {
$index->dequeueItems();
}
}
elseif ($index->read_only != $index->original->read_only) {
if ($index->read_only) {
$index->dequeueItems();
}
else {
$index->queueItems();
}
}
}
/**
* Implements hook_search_api_index_delete().
*
* Removes all data for indexes not available any more.
*/
function search_api_search_api_index_delete(SearchApiIndex $index) {
// Only react on real delete, not revert.
if ($index->hasStatus(ENTITY_PLUS_IN_CODE)) {
$reverts = &backdrop_static(__FUNCTION__, array());
$reverts[$index->machine_name] = $index;
return;
}
cache_clear_all($index->getCacheId(''), 'cache', TRUE);
$index->postDelete();
}
/**
* Implements hook_features_export_alter().
*
* Adds dependency information for exported servers.
*/
function search_api_features_export_alter(&$export) {
if (isset($export['features']['search_api_server'])) {
// Get a list of the modules that provide storage engines.
$hook = 'search_api_service_info';
$classes = array();
foreach (module_implements('search_api_service_info') as $module) {
$function = $module . '_' . $hook;
$engines = $function();
foreach ($engines as $service => $specs) {
$classes[$service] = $module;
}
}
// Check all of the exported server specifications.
foreach ($export['features']['search_api_server'] as $server_name) {
// Load the server's object.
$server = search_api_server_load($server_name);
$module = $classes[$server->class];
// Ensure that the module responsible for the server object is listed as
// a dependency.
if (!isset($export['dependencies'][$module])) {
$export['dependencies'][$module] = $module;
}
}
// Ensure the dependencies list is still sorted alphabetically.
ksort($export['dependencies']);
}
}
/**
* Implements hook_system_info_alter().
*
* Checks if the module provides any search item types or service classes. If it
* does, and there are search indexes using those item types, respectively
* servers using those service classes, the module is set to "required".
*
* Heavily borrowed from field_system_info_alter().
*
* @see hook_search_api_item_type_info()
*/
function search_api_system_info_alter(&$info, $file, $type) {
if ($type != 'module' || $file->name == 'search_api' || !module_exists($file->name)) {
return;
}
// Check for defined item types.
if (module_hook($file->name, 'search_api_item_type_info')) {
$types = array();
foreach (search_api_get_item_type_info() as $type => $type_info) {
if ($type_info['module'] == $file->name) {
$types[] = $type;
}
}
if ($types) {
$sql = 'SELECT machine_name, name FROM {search_api_index} WHERE item_type IN (:types)';
$indexes = db_query($sql, array(':types' => $types))->fetchAllKeyed();
if ($indexes) {
$info['disabled'] = TRUE;
$links = array();
foreach ($indexes as $id => $name) {
$url = url("admin/config/search/search_api/index/$id");
$links[] = '<a href="' . check_plain($url) . '">' . check_plain($name) . '</a>';
}
$args = array('!indexes' => implode(', ', $links));
$info['explanation'] = format_plural(count($indexes), 'Item type in use by the following index: !indexes.', 'Item type(s) in use by the following indexes: !indexes.', $args);
}
}
}
// Check for defined service classes.
if (module_hook($file->name, 'search_api_service_info')) {
$classes = array();
foreach (search_api_get_service_info() as $class => $class_info) {
if ($class_info['module'] == $file->name) {
$classes[] = $class;
}
}
if ($classes) {
$sql = 'SELECT machine_name, name FROM {search_api_server} WHERE class IN (:classes)';
$servers = db_query($sql, array(':classes' => $classes))->fetchAllKeyed();
if ($servers) {
$info['disabled'] = TRUE;
$links = array();
foreach ($servers as $id => $name) {
$url = url("admin/config/search/search_api/server/$id");
$links[] = '<a href="' . check_plain($url) . '">' . check_plain($name) . '</a>';
}
$args = array('!servers' => implode(', ', $links));
$explanation = format_plural(count($servers), 'Service class in use by the following server: !servers.', 'Service class(es) in use by the following servers: !servers.', $args);
$info['explanation'] = (!empty($info['explanation']) ? $info['explanation'] . ' ' : '') . $explanation;
}
}
}
}
/**
* Implements hook_entity_insert().
*
* This is implemented on behalf of the SearchApiEntityDataSourceController
* datasource controller and calls search_api_track_item_insert() for the
* inserted items.
*
* @see search_api_search_api_item_type_info()
*/
function search_api_entity_insert($entity, $type) {
// When inserting a new search index, the new index was already inserted into
// the tracking table. This would lead to a duplicate-key issue, if we would
// continue.
// We also only react on entity operations for types with property
// information, as we don't provide search integration for the others.
if ($type == 'search_api_index' || !entity_plus_get_property_info($type)) {
return;
}
list($id) = entity_extract_ids($type, $entity);
if (isset($id)) {
search_api_track_item_insert($type, array($id));
$combined_id = $type . '/' . $id;
search_api_track_item_insert('multiple', array($combined_id));
}
}
/**
* Implements hook_entity_update().
*
* This is implemented on behalf of the SearchApiEntityDataSourceController
* datasource controller and calls search_api_track_item_change() for the
* updated items.
*
* It also checks whether the entity's bundle changed and acts accordingly.
*
* @see search_api_search_api_item_type_info()
*/
function search_api_entity_update($entity, $type) {
// We only react on entity operations for types with property information, as
// we don't provide search integration for the others.
if (!entity_plus_get_property_info($type)) {
return;
}
list($id, , $new_bundle) = entity_extract_ids($type, $entity);
// Check if the entity's bundle changed.
if (!empty($entity->original)) {
list(, , $old_bundle) = entity_extract_ids($type, $entity->original);
if ($new_bundle != $old_bundle) {
_search_api_entity_datasource_bundle_change($type, $id, $old_bundle, $new_bundle);
}
}
if (isset($id)) {
search_api_track_item_change($type, array($id));
$combined_id = $type . '/' . $id;
search_api_track_item_change('multiple', array($combined_id));
}
}
/**
* Implements hook_entity_delete().
*
* This is implemented on behalf of the SearchApiEntityDataSourceController
* datasource controller and calls search_api_track_item_delete() for the
* deleted items.
*
* @see search_api_search_api_item_type_info()
*/
function search_api_entity_delete($entity, $type) {
// We only react on entity operations for types with property information, as
// we don't provide search integration for the others.
if (!entity_plus_get_property_info($type)) {
return;
}
list($id) = entity_extract_ids($type, $entity);
if (isset($id)) {
search_api_track_item_delete($type, array($id));
$combined_id = $type . '/' . $id;
search_api_track_item_delete('multiple', array($combined_id));
}
}
/**
* Implements hook_node_access_records_alter().
*
* Marks the node as "changed" in indexes that use the "Node access" data
* alteration. Also marks the node's comments as changed in indexes that use the
* "Comment access" data alteration.
*/
function search_api_node_access_records_alter(&$grants, $node) {
$conditions = array(
'enabled' => 1,
'read_only' => 0,
);
$indexes = search_api_index_load_multiple(FALSE, $conditions);
foreach ($indexes as $index) {
$item_ids = array();
if (!empty($index->options['data_alter_callbacks']['search_api_alter_node_access']['status'])) {
$item_id = $index->datasource()->getItemId($node);
if ($item_id !== NULL) {
$item_ids = array($item_id);
}
}
elseif (!empty($index->options['data_alter_callbacks']['search_api_alter_comment_access']['status'])) {
if (!isset($comments)) {
$comments = comment_load_multiple(FALSE, array('nid' => $node->nid));
}
foreach ($comments as $comment) {
$item_ids[] = $index->datasource()->getItemId($comment);
}
}
if ($item_ids) {
search_api_track_item_change_for_indexes(
$index->item_type,
$item_ids,
array($index->machine_name => $index)
);
}
}
}
/**
* Implements hook_field_attach_rename_bundle().
*
* This is implemented on behalf of the SearchApiEntityDataSourceController
* datasource controller, to update any bundle settings that contain the changed
* bundle.
*/
function search_api_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
foreach (search_api_index_load_multiple(FALSE, array('item_type' => $entity_type)) as $index) {
$bundles = &$index->options['datasource']['bundles'];
if (isset($bundles) && ($pos = array_search($bundle_old, $bundles)) !== FALSE) {
$bundles[$pos] = $bundle_new;