search_api.api.php 16 KB

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