Mercurial > hg > MPIWG-drupal-modules
comparison sites/all/modules/custom/solrconnect/apachesolr.api.php @ 0:015d06b10d37 default tip
initial
author | dwinter |
---|---|
date | Wed, 31 Jul 2013 13:49:13 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:015d06b10d37 |
---|---|
1 <?php | |
2 /** | |
3 * @file | |
4 * Exposed Hooks in 7.x: | |
5 */ | |
6 | |
7 /** | |
8 * Lets modules know when the default environment is changed. | |
9 * | |
10 * @param string $env_id | |
11 * The machine name of the environment. | |
12 * @param string $old_env_id | |
13 * The old machine name of the environment. | |
14 */ | |
15 function hook_apachesolr_default_environment($env_id, $old_env_id) { | |
16 $page = apachesolr_search_page_load('core_search'); | |
17 if ($page && $page['env_id'] != $env_id) { | |
18 $page['env_id'] = $env_id; | |
19 apachesolr_search_page_save($page); | |
20 } | |
21 } | |
22 | |
23 /** | |
24 * Add index mappings for Field API types. The default mappings array | |
25 * handles just list fields and taxonomy term reference fields, such as: | |
26 * | |
27 * $mappings['list_text'] = array( | |
28 * 'indexing_callback' => 'apachesolr_fields_list_indexing_callback', | |
29 * 'index_type' => 'string', | |
30 * 'map callback' => 'apachesolr_fields_list_display_callback', | |
31 * 'facets' => TRUE, | |
32 * ), | |
33 * | |
34 * In your implementation you can add additional field types such as: | |
35 * $mappings['number_integer']['number'] = array(...); | |
36 * | |
37 * You can also add mapping for a specific field. This will take precedence | |
38 * over any mapping for a general field type. A field-specific mapping would | |
39 * looks like: | |
40 * $mappings['per-field']['field_model_name'] = array(...); | |
41 * | |
42 * Much more information can be found below in the example implementation or in | |
43 * facetapi.api.php. If you feel restricted with the options as set below | |
44 * there is nothing that stops you from implementing facetapi directly. However | |
45 * it is recommended to not directly talk to solr fields since this could break | |
46 * in the future. | |
47 * | |
48 * @return array $mappings | |
49 * An associative array of mappings as defined by modules that implement | |
50 * hook_apachesolr_field_mappings(). | |
51 */ | |
52 function hook_apachesolr_field_mappings() { | |
53 $mappings = array( | |
54 // Example for a field API type. See extensive documentation below | |
55 'number_float' => array( | |
56 'indexing_callback' => 'apachesolr_fields_default_indexing_callback', | |
57 'index_type' => 'tfloat', | |
58 'facets' => TRUE, | |
59 'query types' => array('term', 'numeric_range'), | |
60 'query type' => 'term', | |
61 'facet mincount allowed' => TRUE, | |
62 ), | |
63 // Example for a field API field | |
64 'per-field' => array( | |
65 // machine name of the field in Field API | |
66 'field_price' => array( | |
67 // REQUIRED FIELDS // | |
68 // Function callback to return the value that will be put in to | |
69 // the solr index | |
70 'indexing_callback' => 'apachesolr_fields_default_indexing_callback', | |
71 | |
72 // NON REQUIRED FIELDS // | |
73 // See apachesolr_index_key() for the correct type. Defaults string | |
74 'index_type' => 'string', | |
75 // How to display the values when they return as a facet | |
76 'map callback' => 'apachesolr_fields_list_facet_map_callback', | |
77 // Does your facet have a dynamic name? Add function call here and will | |
78 // have the name of the return value | |
79 'name callback' => FALSE, | |
80 // If a custom field needs to be searchable but does not need to be faceted you | |
81 // can change the 'facets' parameter to FALSE. | |
82 'facets' => FALSE, | |
83 // Do you want to allow items without value | |
84 'facet missing allowed' => FALSE, | |
85 // (optional) Whether or not the facet supports the | |
86 // "minimum facet count" setting. Defaults to TRUE. | |
87 'facet mincount allowed' => FALSE, | |
88 // Field API allows any field to be multi-valued. | |
89 // If we set this to false we are able to sort | |
90 'dependency plugins' => array('bundle', 'role'), | |
91 // Does your solr index has a hierarchy? | |
92 // See facetapi_get_taxonomy_hierarchy for details or | |
93 // view the mapping of taxonomy_term_reference | |
94 'hierarchy callback' => FALSE, | |
95 // There are different query types to return information from Solr | |
96 // term : Regular strings | |
97 // date : Everything regarding dates | |
98 // numeric_range : Useful when you have widgets that depend | |
99 // on statistics coming from Solr | |
100 'query types' => array('term', 'numeric_range'), | |
101 // Backwards compatible with previous facetapi versions. | |
102 // Pick the main query type | |
103 'query type' => 'term', | |
104 // What dependencies do you have (see facetapi) | |
105 'multiple' => TRUE, | |
106 ), | |
107 ), | |
108 ); | |
109 return $mappings; | |
110 } | |
111 | |
112 /** | |
113 * Alter hook for apachesolr_field_mappings(). | |
114 * | |
115 * Add or alter index mappings for Field API types. The default mappings array | |
116 * handles just list fields and taxonomy term reference fields, in the same way | |
117 * as documented in hook_apachesolr_field_mappings. | |
118 * | |
119 * @param array $mappings | |
120 * An associative array of mappings as defined by modules that implement | |
121 * hook_apachesolr_field_mappings(). | |
122 * @param string $entity_type | |
123 * The entity type for which you want to alter the field mappings | |
124 */ | |
125 function hook_apachesolr_field_mappings_alter(array &$mappings, $entity_type) { | |
126 // Enable indexing for text fields | |
127 $mappings['text'] = array( | |
128 'indexing_callback' => 'apachesolr_fields_default_indexing_callback', | |
129 'map callback' => '', | |
130 'index_type' => 'string', | |
131 'facets' => TRUE, | |
132 'facet missing allowed' => TRUE, | |
133 'dependency plugins' => array('bundle', 'role'), | |
134 'hierarchy callback' => FALSE, | |
135 'name callback' => '', | |
136 'facet mincount allowed' => FALSE, | |
137 'multiple' => FALSE, | |
138 ); | |
139 | |
140 // Add our per field mapping here so we can sort on the | |
141 // price by making it single. Solr cannot sort on multivalued fields | |
142 // field_price is our identifier of a custom field, and it was decided to | |
143 // index in the same way as a number_float field. | |
144 $mappings['per-field']['field_price'] = $mappings['number_float']; | |
145 $mappings['per-field']['field_price']['multiple'] = FALSE; | |
146 } | |
147 | |
148 /** | |
149 * Prepare the query by adding parameters, sorts, etc. | |
150 * | |
151 * This hook is invoked before the query is cached. The cached query is used | |
152 * after the search such as for building facet and sort blocks, so parameters | |
153 * added during this hook may be visible to end users. | |
154 * | |
155 * This is otherwise the same as HOOK_apachesolr_query_alter(), but runs before | |
156 * it. | |
157 * | |
158 * @param DrupalSolrQueryInterface $query | |
159 * An object implementing DrupalSolrQueryInterface. No need for &. | |
160 */ | |
161 function hook_apachesolr_query_prepare(DrupalSolrQueryInterface $query) { | |
162 // Add a sort on the node ID. | |
163 $query->setAvailableSort('entity_id', array( | |
164 'title' => t('Node ID'), | |
165 'default' => 'asc', | |
166 )); | |
167 } | |
168 | |
169 /** | |
170 * Assigns a readable name to your custom solr field | |
171 * | |
172 * @param array $map | |
173 */ | |
174 function hook_apachesolr_field_name_map_alter(array &$map) { | |
175 $map['xs_node'] = t('The full node object'); | |
176 } | |
177 | |
178 /** | |
179 * Alter the query after it's prepared and cached. | |
180 * | |
181 * Any module performing a search should call | |
182 * drupal_alter('apachesolr_query', $query). That function then invokes this | |
183 * hook. It allows modules to modify the query object and its parameters. | |
184 * | |
185 * A module implementing HOOK_apachesolr_query_alter() may set | |
186 * $query->abort_search to TRUE to flag the query to be aborted. | |
187 * | |
188 * @param DrupalSolrQueryInterface $query | |
189 * An object implementing DrupalSolrQueryInterface. No need for &. | |
190 */ | |
191 function hook_apachesolr_query_alter(DrupalSolrQueryInterface $query) { | |
192 // I only want to see articles by the admin. | |
193 // | |
194 // NOTE: this "is_uid" filter does NOT refer to the English word "is" | |
195 // It is a combination of flags representing Integer-Single, which is | |
196 // abbreviated with the letters i and s. | |
197 // | |
198 // @see the <dynamicField> definitions in schema.xml or schema-solr3.xml | |
199 $query->addFilter("is_uid", 1); | |
200 | |
201 // Only search titles. | |
202 $query->replaceParam('qf', 'label'); | |
203 } | |
204 | |
205 /** | |
206 * Allows a module to modify the delete query. | |
207 * | |
208 * @param string $query | |
209 * Defaults to *:* | |
210 * This is not an instance of DrupalSolrQueryInterface, it is the raw query that is being sent to Solr | |
211 */ | |
212 function hook_apachesolr_delete_by_query_alter($query) { | |
213 // use the site hash so that you only delete this site's content | |
214 if ($query == '*:*') { | |
215 $query = 'hash:' . apachesolr_site_hash(); | |
216 } | |
217 else { | |
218 $query .= ' AND hash:' . apachesolr_site_hash(); | |
219 } | |
220 } | |
221 | |
222 /** | |
223 * This is the place to look for the replacement to hook_apachesolr_node_exclude | |
224 * You should define a replacement for the status callback and return | |
225 * FALSE for entities which you do not want to appear in the index and TRUE for | |
226 * those that you want to include | |
227 */ | |
228 | |
229 /** | |
230 * This is invoked for each entity that is being inspected to be added to the | |
231 * index. if any module returns TRUE, the entity is skipped for indexing. | |
232 * | |
233 * @param string $entity_id | |
234 * @param string $entity_type | |
235 * @param object $row | |
236 * A complete set of data from the indexing table. | |
237 * @param string $env_id | |
238 * The machine name of the environment. | |
239 * @return boolean | |
240 */ | |
241 function hook_apachesolr_exclude($entity_id, $entity_type, $row, $env_id) { | |
242 // Never index media entities to core_1 | |
243 if ($entity_type == 'media' && $env_id == 'core_1') { | |
244 return TRUE; | |
245 } | |
246 return FALSE; | |
247 } | |
248 | |
249 /** | |
250 * This is invoked for each entity from the type of ENTITY_TYPE that is being | |
251 * inspected to be added to the index. if any module returns TRUE, | |
252 * the entity is skipped for indexing. | |
253 * | |
254 * @param string $entity_id | |
255 * @param object $row | |
256 * A complete set of data from the indexing table. | |
257 * @param string $env_id | |
258 * The machine name of the environment. | |
259 * @return boolean | |
260 */ | |
261 function hook_apachesolr_ENTITY_TYPE_exclude($entity_id, $row, $env_id) { | |
262 // Never index ENTITY_TYPE to core_1 | |
263 if ($env_id == 'core_1') { | |
264 return TRUE; | |
265 } | |
266 return FALSE; | |
267 } | |
268 | |
269 /** | |
270 * Add information to index other entities. | |
271 * There are some modules in http://drupal.org that can give a good example of | |
272 * custom entity indexing such as apachesolr_user, apachesolr_term | |
273 * | |
274 * @param array $entity_info | |
275 */ | |
276 function hook_apachesolr_entity_info_alter(array &$entity_info) { | |
277 // REQUIRED VALUES | |
278 // myentity should be replaced with user/node/custom entity | |
279 $entity_info['node'] = array(); | |
280 // Set this entity as indexable | |
281 $entity_info['node']['indexable'] = TRUE; | |
282 // Validate each entity if it can be indexed or not. Multiple callbacks are | |
283 // allowed. If one of them returns false it won't be indexed | |
284 $entity_info['node']['status callback'][] = 'apachesolr_index_node_status_callback'; | |
285 // Build up a custom document. | |
286 $entity_info['node']['document callback'][] = 'apachesolr_index_node_solr_document'; | |
287 // What to do when a reindex is issued. Most probably this will reset all the | |
288 // items in the index_table | |
289 $entity_info['node']['reindex callback'] = 'apachesolr_index_node_solr_reindex'; | |
290 | |
291 // OPTIONAL VALUES | |
292 // Index in a separate table? Useful for huge datasets. | |
293 $entity_info['node']['index_table'] = 'apachesolr_index_entities_node'; | |
294 // Execute custom callback on each cron run. | |
295 // See apachesolr_index_node_check_table | |
296 $entity_info['node']['cron_check'] = 'apachesolr_index_node_check_table'; | |
297 // Specific output processing for the results | |
298 $entity_info['node']['apachesolr']['result callback'] = 'apachesolr_search_node_result'; | |
299 | |
300 // BUNDLE SPECIFIC OVERRIDES | |
301 // The following can be overridden on a per-bundle basis. | |
302 // The bundle-specific settings will take precedence over the entity settings. | |
303 $entity_info['node']['bundles']['page']['apachesolr']['result callback'] = 'apachesolr_search_node_result'; | |
304 $entity_info['node']['bundles']['page']['apachesolr']['status callback'][] = 'apachesolr_index_node_status_callback'; | |
305 $entity_info['node']['bundles']['page']['apachesolr']['document callback'][] = 'apachesolr_index_node_solr_document'; | |
306 } | |
307 | |
308 | |
309 /** | |
310 * The is invoked by apachesolr_search.module for each document returned in a | |
311 * search. This has been introduced in 6.x-beta7 as a replacement for the call | |
312 * to HOOK_nodeapi(). | |
313 * | |
314 * @param ApacheSolrDocument $document | |
315 * The ApacheSolrDocument instance. | |
316 * @param array $extra | |
317 * @param DrupalSolrQueryInterface $query | |
318 */ | |
319 function hook_apachesolr_search_result_alter(ApacheSolrDocument $document, array &$extra, DrupalSolrQueryInterface $query) { | |
320 } | |
321 | |
322 /** | |
323 * This is invoked by apachesolr_search.module for the whole resultset returned | |
324 * in a search. | |
325 * | |
326 * @param array $results | |
327 * The returned search results. | |
328 * @param DrupalSolrQueryInterface $query | |
329 * The query for which we want to process the results from | |
330 */ | |
331 function hook_apachesolr_process_results(array &$results, DrupalSolrQueryInterface $query) { | |
332 foreach ($results as $id => $result) { | |
333 $results[$id]['title'] = t('[Result] !title', array('!title' => $result['title'])); | |
334 } | |
335 } | |
336 | |
337 /** | |
338 * Respond to search environment deletion. | |
339 * | |
340 * This hook is invoked from apachesolr_environment_delete() after the | |
341 * environment is removed from the database. | |
342 * | |
343 * @param array $environment | |
344 * The environment object that is being deleted. | |
345 */ | |
346 function hook_apachesolr_environment_delete(array $environment) { | |
347 } | |
348 | |
349 /** | |
350 * | |
351 * Modify the build array for any search output build by Apache Solr | |
352 * This includes core and custom pages and makes it very easy to modify both | |
353 * of them at once | |
354 * | |
355 * @param array $build | |
356 * @param array $search_page | |
357 */ | |
358 function hook_apachesolr_search_page_alter(array &$build, array $search_page) { | |
359 // Adds a text to the top of the page | |
360 $info = array('#markup' => t('Add information to every search page')); | |
361 array_unshift($build, $info); | |
362 } | |
363 | |
364 /** | |
365 * Modify the search types as found in the search pages administration | |
366 * | |
367 * @param array $search_types | |
368 */ | |
369 function hook_apachesolr_search_types_alter(&$search_types) { | |
370 $search_types['ss_language'] = array( | |
371 'name' => apachesolr_field_name_map('ss_language'), | |
372 'default menu' => 'search/language/%', | |
373 'title callback' => 'custom_title_callback', | |
374 ); | |
375 } | |
376 | |
377 /** | |
378 * Build the documents before sending them to Solr. | |
379 * The function is the follow-up for apachesolr_update_index | |
380 * | |
381 * @param ApacheSolrDocument $document | |
382 * @param object $entity | |
383 * @param string $entity_type | |
384 * @param string $env_id | |
385 * The machine name of the environment. | |
386 */ | |
387 function hook_apachesolr_index_document_build(ApacheSolrDocument $document, $entity, $entity_type, $env_id) { | |
388 | |
389 } | |
390 | |
391 /** | |
392 * Build the documents before sending them to Solr. | |
393 * | |
394 * Supports all types of | |
395 * hook_apachesolr_index_document_build_' . $entity_type($documents[$id], $entity, $env_id); | |
396 * | |
397 * The function is the follow-up for apachesolr_update_index but then for | |
398 * specific entity types | |
399 * | |
400 * @param ApacheSolrDocument $document | |
401 * @param object $entity | |
402 * @param string $env_id | |
403 * The machine name of the environment. | |
404 */ | |
405 function hook_apachesolr_index_document_build_ENTITY_TYPE(ApacheSolrDocument $document, $entity, $env_id) { | |
406 // Index field_main_image as a separate field | |
407 if ($entity->type == 'profile') { | |
408 $user = user_load(array('uid' => $entity->uid)); | |
409 // Hard coded field, not recommended for inexperienced users. | |
410 $document->setMultiValue('sm_field_main_image', $user->picture); | |
411 } | |
412 } | |
413 | |
414 /** | |
415 * Alter the prepared documents from one entity before sending them to Solr. | |
416 * | |
417 * @param $documents | |
418 * Array of ApacheSolrDocument objects. | |
419 * @param object $entity | |
420 * @param string $entity_type | |
421 * @param string $env_id | |
422 * The machine name of the environment. | |
423 */ | |
424 function hook_apachesolr_index_documents_alter(array &$documents, $entity, $entity_type, $env_id) { | |
425 // Do whatever altering you need here | |
426 } |