datasource_external.inc 8.1 KB

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