datasource_external.inc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /**
  3. * @file
  4. * Contains the SearchApiExternalDataSourceController class.
  5. */
  6. /**
  7. * Base class for data source controllers for external data sources.
  8. *
  9. * This data source controller is a base implementation for item types that
  10. * represent external data, not directly accessible in Drupal. You can use this
  11. * controller as a base class when you don't want to index items of the type via
  12. * Drupal, but only want the search capabilities of the Search API. In addition
  13. * you most probably also have to create a fitting service class for executing
  14. * the actual searches.
  15. *
  16. * To use most of the functionality of the Search API and related modules, you
  17. * will only have to specify some property information in getPropertyInfo(). If
  18. * you have a custom service class which already returns the extracted fields
  19. * with the search results, you will only have to provide a label and a type for
  20. * each field.
  21. */
  22. class SearchApiExternalDataSourceController extends SearchApiAbstractDataSourceController {
  23. /**
  24. * Return information on the ID field for this controller's type.
  25. *
  26. * This implementation will return a field named "id" of type "string". This
  27. * can also be used if the item type in question has no IDs.
  28. *
  29. * @return array
  30. * An associative array containing the following keys:
  31. * - key: The property key for the ID field, as used in the item wrapper.
  32. * - type: The type of the ID field. Has to be one of the types from
  33. * search_api_field_types(). List types ("list<*>") are not allowed.
  34. */
  35. public function getIdFieldInfo() {
  36. return array(
  37. 'key' => 'id',
  38. 'type' => 'string',
  39. );
  40. }
  41. /**
  42. * Load items of the type of this data source controller.
  43. *
  44. * Always returns an empty array. If you want the items of your type to be
  45. * loadable, specify a function here.
  46. *
  47. * @param array $ids
  48. * The IDs of the items to load.
  49. *
  50. * @return array
  51. * The loaded items, keyed by ID.
  52. */
  53. public function loadItems(array $ids) {
  54. return array();
  55. }
  56. /**
  57. * Overrides SearchApiAbstractDataSourceController::getPropertyInfo().
  58. *
  59. * Only returns a single string ID field.
  60. */
  61. protected function getPropertyInfo() {
  62. $info['property info']['id'] = array(
  63. 'label' => t('ID'),
  64. 'type' => 'string',
  65. );
  66. return $info;
  67. }
  68. /**
  69. * Get the unique ID of an item.
  70. *
  71. * Always returns 1.
  72. *
  73. * @param $item
  74. * An item of this controller's type.
  75. *
  76. * @return
  77. * Either the unique ID of the item, or NULL if none is available.
  78. */
  79. public function getItemId($item) {
  80. return 1;
  81. }
  82. /**
  83. * Get a human-readable label for an item.
  84. *
  85. * Always returns NULL.
  86. *
  87. * @param $item
  88. * An item of this controller's type.
  89. *
  90. * @return
  91. * Either a human-readable label for the item, or NULL if none is available.
  92. */
  93. public function getItemLabel($item) {
  94. return NULL;
  95. }
  96. /**
  97. * Get a URL at which the item can be viewed on the web.
  98. *
  99. * Always returns NULL.
  100. *
  101. * @param $item
  102. * An item of this controller's type.
  103. *
  104. * @return
  105. * Either an array containing the 'path' and 'options' keys used to build
  106. * the URL of the item, and matching the signature of url(), or NULL if the
  107. * item has no URL of its own.
  108. */
  109. public function getItemUrl($item) {
  110. return NULL;
  111. }
  112. /**
  113. * Initialize tracking of the index status of items for the given indexes.
  114. *
  115. * All currently known items of this data source's type should be inserted
  116. * into the tracking table for the given indexes, with status "changed". If
  117. * items were already present, these should also be set to "changed" and not
  118. * be inserted again.
  119. *
  120. * @param array $indexes
  121. * The SearchApiIndex objects for which item tracking should be initialized.
  122. *
  123. * @throws SearchApiDataSourceException
  124. * If any of the indexes doesn't use the same item type as this controller.
  125. */
  126. public function startTracking(array $indexes) {
  127. return;
  128. }
  129. /**
  130. * Stop tracking of the index status of items for the given indexes.
  131. *
  132. * The tracking tables of the given indexes should be completely cleared.
  133. *
  134. * @param array $indexes
  135. * The SearchApiIndex objects for which item tracking should be stopped.
  136. *
  137. * @throws SearchApiDataSourceException
  138. * If any of the indexes doesn't use the same item type as this controller.
  139. */
  140. public function stopTracking(array $indexes) {
  141. return;
  142. }
  143. /**
  144. * Start tracking the index status for the given items on the given indexes.
  145. *
  146. * @param array $item_ids
  147. * The IDs of new items to track.
  148. * @param array $indexes
  149. * The indexes for which items should be tracked.
  150. *
  151. * @throws SearchApiDataSourceException
  152. * If any of the indexes doesn't use the same item type as this controller.
  153. */
  154. public function trackItemInsert(array $item_ids, array $indexes) {
  155. return;
  156. }
  157. /**
  158. * Set the tracking status of the given items to "changed"/"dirty".
  159. *
  160. * @param $item_ids
  161. * Either an array with the IDs of the changed items. Or FALSE to mark all
  162. * items as changed for the given indexes.
  163. * @param array $indexes
  164. * The indexes for which the change should be tracked.
  165. * @param $dequeue
  166. * If set to TRUE, also change the status of queued items.
  167. *
  168. * @throws SearchApiDataSourceException
  169. * If any of the indexes doesn't use the same item type as this controller.
  170. */
  171. public function trackItemChange($item_ids, array $indexes, $dequeue = FALSE) {
  172. return;
  173. }
  174. /**
  175. * Set the tracking status of the given items to "indexed".
  176. *
  177. * @param array $item_ids
  178. * The IDs of the indexed items.
  179. * @param SearchApiIndex $indexes
  180. * The index on which the items were indexed.
  181. *
  182. * @throws SearchApiDataSourceException
  183. * If the index doesn't use the same item type as this controller.
  184. */
  185. public function trackItemIndexed(array $item_ids, SearchApiIndex $index) {
  186. return;
  187. }
  188. /**
  189. * Stop tracking the index status for the given items on the given indexes.
  190. *
  191. * @param array $item_ids
  192. * The IDs of the removed items.
  193. * @param array $indexes
  194. * The indexes for which the deletions should be tracked.
  195. *
  196. * @throws SearchApiDataSourceException
  197. * If any of the indexes doesn't use the same item type as this controller.
  198. */
  199. public function trackItemDelete(array $item_ids, array $indexes) {
  200. return;
  201. }
  202. /**
  203. * Get a list of items that need to be indexed.
  204. *
  205. * If possible, completely unindexed items should be returned before items
  206. * that were indexed but later changed. Also, items that were changed longer
  207. * ago should be favored.
  208. *
  209. * @param SearchApiIndex $index
  210. * The index for which changed items should be returned.
  211. * @param $limit
  212. * The maximum number of items to return. Negative values mean "unlimited".
  213. *
  214. * @return array
  215. * The IDs of items that need to be indexed for the given index.
  216. */
  217. public function getChangedItems(SearchApiIndex $index, $limit = -1) {
  218. return array();
  219. }
  220. /**
  221. * Get information on how many items have been indexed for a certain index.
  222. *
  223. * @param SearchApiIndex $index
  224. * The index whose index status should be returned.
  225. *
  226. * @return array
  227. * An associative array containing two keys (in this order):
  228. * - indexed: The number of items already indexed in their latest version.
  229. * - total: The total number of items that have to be indexed for this
  230. * index.
  231. *
  232. * @throws SearchApiDataSourceException
  233. * If the index doesn't use the same item type as this controller.
  234. */
  235. public function getIndexStatus(SearchApiIndex $index) {
  236. return array(
  237. 'indexed' => 0,
  238. 'total' => 0,
  239. );
  240. }
  241. }