search_api.api.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the Search API module.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * Defines one or more search service classes a module offers.
  12. *
  13. * Note: The ids should be valid PHP identifiers.
  14. *
  15. * @return array
  16. * An associative array of search service classes, keyed by a unique
  17. * identifier and containing associative arrays with the following keys:
  18. * - name: The service class' translated name.
  19. * - description: A translated string to be shown to administrators when
  20. * selecting a service class. Should contain all peculiarities of the
  21. * service class, like field type support, supported features (like facets),
  22. * the "direct" parse mode and other specific things to keep in mind.
  23. * - class: The service class, which has to implement the
  24. * SearchApiServiceInterface interface.
  25. *
  26. * @see hook_search_api_service_info_alter()
  27. */
  28. function hook_search_api_service_info() {
  29. $services['example_some'] = array(
  30. 'name' => t('Some Service'),
  31. 'description' => t('Service for some search engine.'),
  32. 'class' => 'SomeServiceClass',
  33. // Unknown keys can later be read by the object for additional information.
  34. 'init args' => array('foo' => 'Foo', 'bar' => 42),
  35. );
  36. $services['example_other'] = array(
  37. 'name' => t('Other Service'),
  38. 'description' => t('Service for another search engine.'),
  39. 'class' => 'OtherServiceClass',
  40. );
  41. return $services;
  42. }
  43. /**
  44. * Alter the Search API service info.
  45. *
  46. * Modules may implement this hook to alter the information that defines Search
  47. * API services. All properties that are available in
  48. * hook_search_api_service_info() can be altered here, with the addition of the
  49. * "module" key specifying the module that originally defined the service class.
  50. *
  51. * @param array $service_info
  52. * The Search API service info array, keyed by service id.
  53. *
  54. * @see hook_search_api_service_info()
  55. */
  56. function hook_search_api_service_info_alter(array &$service_info) {
  57. foreach ($service_info as $id => $info) {
  58. $service_info[$id]['class'] = 'MyProxyServiceClass';
  59. $service_info[$id]['example_original_class'] = $info['class'];
  60. }
  61. }
  62. /**
  63. * Define new types of items that can be searched.
  64. *
  65. * This hook allows modules to define their own item types, for which indexes
  66. * can then be created. (Note that the Search API natively provides support for
  67. * all entity types that specify property information, so they should not be
  68. * added here. You should therefore also not use an existing entity type as the
  69. * identifier of a new item type.)
  70. *
  71. * The main part of defining a new item type is implementing its data source
  72. * controller class, which is responsible for loading items, providing metadata
  73. * and tracking existing items. The module defining a certain item type is also
  74. * responsible for observing creations, updates and deletions of items of that
  75. * type and notifying the Search API of them by calling
  76. * search_api_track_item_insert(), search_api_track_item_change() and
  77. * search_api_track_item_delete(), as appropriate.
  78. * The only other restriction for item types is that they have to have a single
  79. * item ID field, with a scalar value. This is, e.g., used to track indexed
  80. * items.
  81. *
  82. * Note, however, that you can also define item types where some of these
  83. * conditions are not met, as long as you are aware that some functionality of
  84. * the Search API and related modules might then not be available for that type.
  85. *
  86. * @return array
  87. * An associative array keyed by item type identifier, and containing type
  88. * information arrays with at least the following keys:
  89. * - name: A human-readable name for the type.
  90. * - datasource controller: A class implementing the
  91. * SearchApiDataSourceControllerInterface interface which will be used as
  92. * the data source controller for this type.
  93. * - entity_type: (optional) If the type represents entities, the entity type.
  94. * This is used by SearchApiAbstractDataSourceController for determining the
  95. * entity type of items. Other datasource controllers might ignore this.
  96. * Other, datasource-specific settings might also be placed here. These should
  97. * be specified with the data source controller in question.
  98. *
  99. * @see hook_search_api_item_type_info_alter()
  100. */
  101. function hook_search_api_item_type_info() {
  102. // Copied from search_api_search_api_item_type_info().
  103. $types = array();
  104. foreach (entity_get_property_info() as $type => $property_info) {
  105. if ($info = entity_get_info($type)) {
  106. $types[$type] = array(
  107. 'name' => $info['label'],
  108. 'datasource controller' => 'SearchApiEntityDataSourceController',
  109. 'entity_type' => $type,
  110. );
  111. }
  112. }
  113. return $types;
  114. }
  115. /**
  116. * Alter the item type info.
  117. *
  118. * Modules may implement this hook to alter the information that defines an
  119. * item type. All properties that are available in
  120. * hook_search_api_item_type_info() can be altered here, with the addition of
  121. * the "module" key specifying the module that originally defined the type.
  122. *
  123. * @param array $infos
  124. * The item type info array, keyed by type identifier.
  125. *
  126. * @see hook_search_api_item_type_info()
  127. */
  128. function hook_search_api_item_type_info_alter(array &$infos) {
  129. // Adds a boolean value is_entity to all type options telling whether the item
  130. // type represents an entity type.
  131. foreach ($infos as $type => $info) {
  132. $info['is_entity'] = (bool) entity_get_info($type);
  133. }
  134. }
  135. /**
  136. * Define new data types for indexed properties.
  137. *
  138. * New data types will appear as new option for the „Type“ field on indexes'
  139. * „Fields“ tabs. Whether choosing a custom data type will have any effect
  140. * depends on the server on which the data is indexed.
  141. *
  142. * @return array
  143. * An array containing custom data type definitions, keyed by their type
  144. * identifier and containing the following keys:
  145. * - name: The human-readable name of the type.
  146. * - fallback: (optional) One of the default data types (the keys from
  147. * search_api_default_field_types()) which should be used as a fallback if
  148. * the server doesn't support this data type. Defaults to "string".
  149. * - conversion callback: (optional) If specified, a callback function for
  150. * converting raw values to the given type, if possible. For the contract
  151. * of such a callback, see example_data_type_conversion().
  152. *
  153. * @see hook_search_api_data_type_info_alter()
  154. * @see search_api_get_data_type_info()
  155. * @see example_data_type_conversion()
  156. */
  157. function hook_search_api_data_type_info() {
  158. return array(
  159. 'example_type' => array(
  160. 'name' => t('Example type'),
  161. // Could be omitted, as "string" is the default.
  162. 'fallback' => 'string',
  163. 'conversion callback' => 'example_data_type_conversion',
  164. ),
  165. );
  166. }
  167. /**
  168. * Alter the data type info.
  169. *
  170. * Modules may implement this hook to alter the information that defines a data
  171. * type, or to add/remove some entirely. All properties that are available in
  172. * hook_search_api_data_type_info() can be altered here.
  173. *
  174. * @param array $infos
  175. * The data type info array, keyed by type identifier.
  176. *
  177. * @see hook_search_api_data_type_info()
  178. */
  179. function hook_search_api_data_type_info_alter(array &$infos) {
  180. $infos['example_type']['name'] .= ' 2';
  181. }
  182. /**
  183. * Registers one or more callbacks that can be called at index time to add
  184. * additional data to the indexed items (e.g. comments or attachments to nodes),
  185. * alter the data in other forms or remove items from the array.
  186. *
  187. * Data-alter callbacks (which are called "Data alterations" in the UI) are
  188. * classes implementing the SearchApiAlterCallbackInterface interface.
  189. *
  190. * @see SearchApiAlterCallbackInterface
  191. *
  192. * @return array
  193. * An associative array keyed by the callback IDs and containing arrays with
  194. * the following keys:
  195. * - name: The name to display for this callback.
  196. * - description: A short description of what the callback does.
  197. * - class: The callback class.
  198. * - weight: (optional) Defines the order in which callbacks are displayed
  199. * (and, therefore, invoked) by default. Defaults to 0.
  200. */
  201. function hook_search_api_alter_callback_info() {
  202. $callbacks['example_random_alter'] = array(
  203. 'name' => t('Random alteration'),
  204. 'description' => t('Alters all passed item data completely randomly.'),
  205. 'class' => 'ExampleRandomAlter',
  206. 'weight' => 100,
  207. );
  208. $callbacks['example_add_comments'] = array(
  209. 'name' => t('Add comments'),
  210. 'description' => t('For nodes and similar entities, adds comments.'),
  211. 'class' => 'ExampleAddComments',
  212. );
  213. return $callbacks;
  214. }
  215. /**
  216. * Registers one or more processors. These are classes implementing the
  217. * SearchApiProcessorInterface interface which can be used at index and search
  218. * time to pre-process item data or the search query, and at search time to
  219. * post-process the returned search results.
  220. *
  221. * @see SearchApiProcessorInterface
  222. *
  223. * @return array
  224. * An associative array keyed by the processor id and containing arrays
  225. * with the following keys:
  226. * - name: The name to display for this processor.
  227. * - description: A short description of what the processor does at each
  228. * phase.
  229. * - class: The processor class, which has to implement the
  230. * SearchApiProcessorInterface interface.
  231. * - weight: (optional) Defines the order in which processors are displayed
  232. * (and, therefore, invoked) by default. Defaults to 0.
  233. */
  234. function hook_search_api_processor_info() {
  235. $callbacks['example_processor'] = array(
  236. 'name' => t('Example processor'),
  237. 'description' => t('Pre- and post-processes data in really cool ways.'),
  238. 'class' => 'ExampleSearchApiProcessor',
  239. 'weight' => -1,
  240. );
  241. $callbacks['example_processor_minimal'] = array(
  242. 'name' => t('Example processor 2'),
  243. 'description' => t('Processor with minimal description.'),
  244. 'class' => 'ExampleSearchApiProcessor2',
  245. );
  246. return $callbacks;
  247. }
  248. /**
  249. * Allows you to log or alter the items that are indexed.
  250. *
  251. * Please be aware that generally preventing the indexing of certain items is
  252. * deprecated. This is better done with data alterations, which can easily be
  253. * configured and only added to indexes where this behaviour is wanted.
  254. * If your module will use this hook to reject certain items from indexing,
  255. * please document this clearly to avoid confusion.
  256. *
  257. * @param array $items
  258. * The entities that will be indexed (before calling any data alterations).
  259. * @param SearchApiIndex $index
  260. * The search index on which items will be indexed.
  261. */
  262. function hook_search_api_index_items_alter(array &$items, SearchApiIndex $index) {
  263. foreach ($items as $id => $item) {
  264. if ($id % 5 == 0) {
  265. unset($items[$id]);
  266. }
  267. }
  268. example_store_indexed_entity_ids($index->item_type, array_keys($items));
  269. }
  270. /**
  271. * Allows modules to react after items were indexed.
  272. *
  273. * @param SearchApiIndex $index
  274. * The used index.
  275. * @param array $item_ids
  276. * An array containing the indexed items' IDs.
  277. */
  278. function hook_search_api_items_indexed(SearchApiIndex $index, array $item_ids) {
  279. if ($index->getEntityType() == 'node') {
  280. // Flush page cache of the search page.
  281. cache_clear_all(url('search'), 'cache_page');
  282. }
  283. }
  284. /**
  285. * Lets modules alter a search query before executing it.
  286. *
  287. * @param SearchApiQueryInterface $query
  288. * The SearchApiQueryInterface object representing the search query.
  289. */
  290. function hook_search_api_query_alter(SearchApiQueryInterface $query) {
  291. // Exclude entities with ID 0. (Assume the ID field is always indexed.)
  292. if ($query->getIndex()->getEntityType()) {
  293. $info = entity_get_info($query->getIndex()->getEntityType());
  294. $query->condition($info['entity keys']['id'], 0, '!=');
  295. }
  296. }
  297. /**
  298. * Act on search servers when they are loaded.
  299. *
  300. * @param array $servers
  301. * An array of loaded SearchApiServer objects.
  302. */
  303. function hook_search_api_server_load(array $servers) {
  304. foreach ($servers as $server) {
  305. db_insert('example_search_server_access')
  306. ->fields(array(
  307. 'server' => $server->machine_name,
  308. 'access_time' => REQUEST_TIME,
  309. ))
  310. ->execute();
  311. }
  312. }
  313. /**
  314. * A new search server was created.
  315. *
  316. * @param SearchApiServer $server
  317. * The new server.
  318. */
  319. function hook_search_api_server_insert(SearchApiServer $server) {
  320. db_insert('example_search_server')
  321. ->fields(array(
  322. 'server' => $server->machine_name,
  323. 'insert_time' => REQUEST_TIME,
  324. ))
  325. ->execute();
  326. }
  327. /**
  328. * A search server was edited, enabled or disabled.
  329. *
  330. * @param SearchApiServer $server
  331. * The edited server.
  332. */
  333. function hook_search_api_server_update(SearchApiServer $server) {
  334. if ($server->name != $server->original->name) {
  335. db_insert('example_search_server_name_update')
  336. ->fields(array(
  337. 'server' => $server->machine_name,
  338. 'update_time' => REQUEST_TIME,
  339. ))
  340. ->execute();
  341. }
  342. }
  343. /**
  344. * A search server was deleted.
  345. *
  346. * @param SearchApiServer $server
  347. * The deleted server.
  348. */
  349. function hook_search_api_server_delete(SearchApiServer $server) {
  350. db_insert('example_search_server_update')
  351. ->fields(array(
  352. 'server' => $server->machine_name,
  353. 'update_time' => REQUEST_TIME,
  354. ))
  355. ->execute();
  356. db_delete('example_search_server')
  357. ->condition('server', $server->machine_name)
  358. ->execute();
  359. }
  360. /**
  361. * Define default search servers.
  362. *
  363. * @return array
  364. * An array of default search servers, keyed by machine names.
  365. *
  366. * @see hook_default_search_api_server_alter()
  367. */
  368. function hook_default_search_api_server() {
  369. $defaults['main'] = entity_create('search_api_server', array(
  370. 'name' => 'Main server',
  371. 'machine_name' => 'main',// Must be same as the used array key.
  372. // Other properties ...
  373. ));
  374. return $defaults;
  375. }
  376. /**
  377. * Alter default search servers.
  378. *
  379. * @param array $defaults
  380. * An array of default search servers, keyed by machine names.
  381. *
  382. * @see hook_default_search_api_server()
  383. */
  384. function hook_default_search_api_server_alter(array &$defaults) {
  385. $defaults['main']->name = 'Customized main server';
  386. }
  387. /**
  388. * Act on search indexes when they are loaded.
  389. *
  390. * @param array $indexes
  391. * An array of loaded SearchApiIndex objects.
  392. */
  393. function hook_search_api_index_load(array $indexes) {
  394. foreach ($indexes as $index) {
  395. db_insert('example_search_index_access')
  396. ->fields(array(
  397. 'index' => $index->machine_name,
  398. 'access_time' => REQUEST_TIME,
  399. ))
  400. ->execute();
  401. }
  402. }
  403. /**
  404. * A new search index was created.
  405. *
  406. * @param SearchApiIndex $index
  407. * The new index.
  408. */
  409. function hook_search_api_index_insert(SearchApiIndex $index) {
  410. db_insert('example_search_index')
  411. ->fields(array(
  412. 'index' => $index->machine_name,
  413. 'insert_time' => REQUEST_TIME,
  414. ))
  415. ->execute();
  416. }
  417. /**
  418. * A search index was edited in any way.
  419. *
  420. * @param SearchApiIndex $index
  421. * The edited index.
  422. */
  423. function hook_search_api_index_update(SearchApiIndex $index) {
  424. if ($index->name != $index->original->name) {
  425. db_insert('example_search_index_name_update')
  426. ->fields(array(
  427. 'index' => $index->machine_name,
  428. 'update_time' => REQUEST_TIME,
  429. ))
  430. ->execute();
  431. }
  432. }
  433. /**
  434. * A search index was scheduled for reindexing
  435. *
  436. * @param SearchApiIndex $index
  437. * The edited index.
  438. * @param $clear
  439. * Boolean indicating whether the index was also cleared.
  440. */
  441. function hook_search_api_index_reindex(SearchApiIndex $index, $clear = FALSE) {
  442. db_insert('example_search_index_reindexed')
  443. ->fields(array(
  444. 'index' => $index->id,
  445. 'update_time' => REQUEST_TIME,
  446. ))
  447. ->execute();
  448. }
  449. /**
  450. * A search index was deleted.
  451. *
  452. * @param SearchApiIndex $index
  453. * The deleted index.
  454. */
  455. function hook_search_api_index_delete(SearchApiIndex $index) {
  456. db_insert('example_search_index_update')
  457. ->fields(array(
  458. 'index' => $index->machine_name,
  459. 'update_time' => REQUEST_TIME,
  460. ))
  461. ->execute();
  462. db_delete('example_search_index')
  463. ->condition('index', $index->machine_name)
  464. ->execute();
  465. }
  466. /**
  467. * Define default search indexes.
  468. *
  469. * @return array
  470. * An array of default search indexes, keyed by machine names.
  471. *
  472. * @see hook_default_search_api_index_alter()
  473. */
  474. function hook_default_search_api_index() {
  475. $defaults['main'] = entity_create('search_api_index', array(
  476. 'name' => 'Main index',
  477. 'machine_name' => 'main',// Must be same as the used array key.
  478. // Other properties ...
  479. ));
  480. return $defaults;
  481. }
  482. /**
  483. * Alter default search indexes.
  484. *
  485. * @param array $defaults
  486. * An array of default search indexes, keyed by machine names.
  487. *
  488. * @see hook_default_search_api_index()
  489. */
  490. function hook_default_search_api_index_alter(array &$defaults) {
  491. $defaults['main']->name = 'Customized main index';
  492. }
  493. /**
  494. * @} End of "addtogroup hooks".
  495. */
  496. /**
  497. * Convert a raw value from an entity to a custom data type.
  498. *
  499. * This function will be called for fields of the specific data type to convert
  500. * all individual values of the field to the correct format.
  501. *
  502. * @param $value
  503. * The raw, single value, as extracted from an entity wrapper.
  504. * @param $original_type
  505. * The original Entity API type of the value.
  506. * @param $type
  507. * The custom data type to which the value should be converted. Can be ignored
  508. * if the callback is only used for a single data type.
  509. *
  510. * @return
  511. * The converted value, if a conversion could be executed. NULL otherwise.
  512. *
  513. * @see hook_search_api_data_type_info()
  514. */
  515. function example_data_type_conversion($value, $original_type, $type) {
  516. if ($type === 'example_type') {
  517. // The example_type type apparently requires a rather complex data format.
  518. return array(
  519. 'value' => $value,
  520. 'original' => $original_type,
  521. );
  522. }
  523. // Someone used this callback for another, unknown type. Return NULL.
  524. // (Normally, you can just assume that the/a correct type is given.)
  525. return NULL;
  526. }