annotate sites/all/modules/custom/solrconnect/apachesolr.index.inc @ 0:015d06b10d37 default tip

initial
author dwinter
date Wed, 31 Jul 2013 13:49:13 +0200
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
015d06b10d37 initial
dwinter
parents:
diff changeset
1 <?php
015d06b10d37 initial
dwinter
parents:
diff changeset
2
015d06b10d37 initial
dwinter
parents:
diff changeset
3 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
4 * @file
015d06b10d37 initial
dwinter
parents:
diff changeset
5 * Functions related to Apache Solr indexing operations.
015d06b10d37 initial
dwinter
parents:
diff changeset
6 */
015d06b10d37 initial
dwinter
parents:
diff changeset
7
015d06b10d37 initial
dwinter
parents:
diff changeset
8 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
9 * Processes all index queues associated with the passed environment.
015d06b10d37 initial
dwinter
parents:
diff changeset
10 *
015d06b10d37 initial
dwinter
parents:
diff changeset
11 * An environment usually indexes one or more entity types. Each entity type
015d06b10d37 initial
dwinter
parents:
diff changeset
12 * stores its queue in a database table that is defined in the entity type's
015d06b10d37 initial
dwinter
parents:
diff changeset
13 * info array. This function processes N number of items in each queue table,
015d06b10d37 initial
dwinter
parents:
diff changeset
14 * where N is the limit passed as the second argument.
015d06b10d37 initial
dwinter
parents:
diff changeset
15 *
015d06b10d37 initial
dwinter
parents:
diff changeset
16 * The indexing routine allows developers to selectively bypass indexing on a
015d06b10d37 initial
dwinter
parents:
diff changeset
17 * per-entity basis by implementing the following hooks:
015d06b10d37 initial
dwinter
parents:
diff changeset
18 * - hook_apachesolr_exclude()
015d06b10d37 initial
dwinter
parents:
diff changeset
19 * - hook_apachesolr_ENTITY_TYPE_exclude()
015d06b10d37 initial
dwinter
parents:
diff changeset
20 *
015d06b10d37 initial
dwinter
parents:
diff changeset
21 * @param string $env_id
015d06b10d37 initial
dwinter
parents:
diff changeset
22 * The machine name of the environment.
015d06b10d37 initial
dwinter
parents:
diff changeset
23 * @param int $limit
015d06b10d37 initial
dwinter
parents:
diff changeset
24 * The number of items to process per queue table. For example, if there are
015d06b10d37 initial
dwinter
parents:
diff changeset
25 * two entities that are being indexed in this environment and they each have
015d06b10d37 initial
dwinter
parents:
diff changeset
26 * their own queue table, setting a limit of 50 will send a maximum number of
015d06b10d37 initial
dwinter
parents:
diff changeset
27 * 100 documents to the Apache Solr server.
015d06b10d37 initial
dwinter
parents:
diff changeset
28 *
015d06b10d37 initial
dwinter
parents:
diff changeset
29 * @return int
015d06b10d37 initial
dwinter
parents:
diff changeset
30 * The total number of documents sent to the Apache Solr server for indexing.
015d06b10d37 initial
dwinter
parents:
diff changeset
31 *
015d06b10d37 initial
dwinter
parents:
diff changeset
32 * @see apachesolr_index_get_entities_to_index()
015d06b10d37 initial
dwinter
parents:
diff changeset
33 * @see apachesolr_index_entity_to_documents()
015d06b10d37 initial
dwinter
parents:
diff changeset
34 * @see apachesolr_index_send_to_solr()
015d06b10d37 initial
dwinter
parents:
diff changeset
35 */
015d06b10d37 initial
dwinter
parents:
diff changeset
36 function apachesolr_index_entities($env_id, $limit) {
015d06b10d37 initial
dwinter
parents:
diff changeset
37 $documents_submitted = 0;
015d06b10d37 initial
dwinter
parents:
diff changeset
38 foreach (entity_get_info() as $entity_type => $info) {
015d06b10d37 initial
dwinter
parents:
diff changeset
39 // With each pass through the callback, retrieve the next group of nids.
015d06b10d37 initial
dwinter
parents:
diff changeset
40 $rows = apachesolr_index_get_entities_to_index($env_id, $entity_type, $limit);
015d06b10d37 initial
dwinter
parents:
diff changeset
41 $documents = array();
015d06b10d37 initial
dwinter
parents:
diff changeset
42 foreach ($rows as $row) {
015d06b10d37 initial
dwinter
parents:
diff changeset
43 $row_documents = apachesolr_index_entities_document($row, $entity_type, $env_id);
015d06b10d37 initial
dwinter
parents:
diff changeset
44 $documents = array_merge($documents, $row_documents);
015d06b10d37 initial
dwinter
parents:
diff changeset
45 }
015d06b10d37 initial
dwinter
parents:
diff changeset
46
015d06b10d37 initial
dwinter
parents:
diff changeset
47 $indexed = apachesolr_index_send_to_solr($env_id, $documents);
015d06b10d37 initial
dwinter
parents:
diff changeset
48 if ($indexed !== FALSE) {
015d06b10d37 initial
dwinter
parents:
diff changeset
49 $documents_submitted += count($documents);
015d06b10d37 initial
dwinter
parents:
diff changeset
50 $index_position = apachesolr_get_last_index_position($env_id, $entity_type);
015d06b10d37 initial
dwinter
parents:
diff changeset
51 $max_changed = $index_position['last_changed'];
015d06b10d37 initial
dwinter
parents:
diff changeset
52 $max_entity_id = $index_position['last_entity_id'];
015d06b10d37 initial
dwinter
parents:
diff changeset
53 foreach ($rows as $row) {
015d06b10d37 initial
dwinter
parents:
diff changeset
54 if (!empty($row->status)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
55 if ($row->changed > $max_changed) {
015d06b10d37 initial
dwinter
parents:
diff changeset
56 $max_changed = $row->changed;
015d06b10d37 initial
dwinter
parents:
diff changeset
57 }
015d06b10d37 initial
dwinter
parents:
diff changeset
58 if ($row->entity_id > $max_entity_id) {
015d06b10d37 initial
dwinter
parents:
diff changeset
59 $max_entity_id = $row->entity_id;
015d06b10d37 initial
dwinter
parents:
diff changeset
60 }
015d06b10d37 initial
dwinter
parents:
diff changeset
61 }
015d06b10d37 initial
dwinter
parents:
diff changeset
62 }
015d06b10d37 initial
dwinter
parents:
diff changeset
63 apachesolr_set_last_index_position($env_id, $entity_type, $max_changed, $max_entity_id);
015d06b10d37 initial
dwinter
parents:
diff changeset
64 apachesolr_set_last_index_updated($env_id, REQUEST_TIME);
015d06b10d37 initial
dwinter
parents:
diff changeset
65 }
015d06b10d37 initial
dwinter
parents:
diff changeset
66 }
015d06b10d37 initial
dwinter
parents:
diff changeset
67 return $documents_submitted;
015d06b10d37 initial
dwinter
parents:
diff changeset
68 }
015d06b10d37 initial
dwinter
parents:
diff changeset
69
015d06b10d37 initial
dwinter
parents:
diff changeset
70 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
71 * Convert a certain entity from the apachesolr index table to a set of documents. 1 entity
015d06b10d37 initial
dwinter
parents:
diff changeset
72 * can be converted in multiple documents if the apachesolr_index_entity_to_documents decides to do so.
015d06b10d37 initial
dwinter
parents:
diff changeset
73 *
015d06b10d37 initial
dwinter
parents:
diff changeset
74 * @param array $row
015d06b10d37 initial
dwinter
parents:
diff changeset
75 * A row from the indexing table
015d06b10d37 initial
dwinter
parents:
diff changeset
76 * @param string $entity_type
015d06b10d37 initial
dwinter
parents:
diff changeset
77 * The type of the entity
015d06b10d37 initial
dwinter
parents:
diff changeset
78 * @param string $env_id
015d06b10d37 initial
dwinter
parents:
diff changeset
79 * The machine name of the environment.
015d06b10d37 initial
dwinter
parents:
diff changeset
80 *
015d06b10d37 initial
dwinter
parents:
diff changeset
81 * @return array of ApacheSolrDocument(s)
015d06b10d37 initial
dwinter
parents:
diff changeset
82 */
015d06b10d37 initial
dwinter
parents:
diff changeset
83 function apachesolr_index_entities_document($row, $entity_type, $env_id) {
015d06b10d37 initial
dwinter
parents:
diff changeset
84 $documents = array();
015d06b10d37 initial
dwinter
parents:
diff changeset
85 if (!empty($row->status)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
86 // Let any module exclude this entity from the index.
015d06b10d37 initial
dwinter
parents:
diff changeset
87 $build_document = TRUE;
015d06b10d37 initial
dwinter
parents:
diff changeset
88 foreach (module_implements('apachesolr_exclude') as $module) {
015d06b10d37 initial
dwinter
parents:
diff changeset
89 $exclude = module_invoke($module, 'apachesolr_exclude', $row->entity_id, $entity_type, $row, $env_id);
015d06b10d37 initial
dwinter
parents:
diff changeset
90 // If the hook returns TRUE we should exclude the entity
015d06b10d37 initial
dwinter
parents:
diff changeset
91 if (!empty($exclude)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
92 $build_document = FALSE;
015d06b10d37 initial
dwinter
parents:
diff changeset
93 }
015d06b10d37 initial
dwinter
parents:
diff changeset
94 }
015d06b10d37 initial
dwinter
parents:
diff changeset
95 foreach (module_implements('apachesolr_' . $entity_type . '_exclude') as $module) {
015d06b10d37 initial
dwinter
parents:
diff changeset
96 $exclude = module_invoke($module, 'apachesolr_' . $entity_type . '_exclude', $row->entity_id, $row, $env_id);
015d06b10d37 initial
dwinter
parents:
diff changeset
97 // If the hook returns TRUE we should exclude the entity
015d06b10d37 initial
dwinter
parents:
diff changeset
98 if (!empty($exclude)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
99 $build_document = FALSE;
015d06b10d37 initial
dwinter
parents:
diff changeset
100 }
015d06b10d37 initial
dwinter
parents:
diff changeset
101 }
015d06b10d37 initial
dwinter
parents:
diff changeset
102 if ($build_document) {
015d06b10d37 initial
dwinter
parents:
diff changeset
103 $documents = array_merge($documents, apachesolr_index_entity_to_documents($row, $env_id));
015d06b10d37 initial
dwinter
parents:
diff changeset
104 }
015d06b10d37 initial
dwinter
parents:
diff changeset
105 }
015d06b10d37 initial
dwinter
parents:
diff changeset
106 else {
015d06b10d37 initial
dwinter
parents:
diff changeset
107 // Delete the entity from our index if the status callback returned 0
015d06b10d37 initial
dwinter
parents:
diff changeset
108 apachesolr_remove_entity($env_id, $row->entity_type, $row->entity_id);
015d06b10d37 initial
dwinter
parents:
diff changeset
109 }
015d06b10d37 initial
dwinter
parents:
diff changeset
110 // Clear entity cache for this specific entity
015d06b10d37 initial
dwinter
parents:
diff changeset
111 entity_get_controller($row->entity_type)->resetCache(array($row->entity_id));
015d06b10d37 initial
dwinter
parents:
diff changeset
112 return $documents;
015d06b10d37 initial
dwinter
parents:
diff changeset
113 }
015d06b10d37 initial
dwinter
parents:
diff changeset
114 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
115 * Returns the total number of documents that are able to be indexed and the
015d06b10d37 initial
dwinter
parents:
diff changeset
116 * number of documents left to be indexed.
015d06b10d37 initial
dwinter
parents:
diff changeset
117 *
015d06b10d37 initial
dwinter
parents:
diff changeset
118 * This is a helper function for modules that implement hook_search_status().
015d06b10d37 initial
dwinter
parents:
diff changeset
119 *
015d06b10d37 initial
dwinter
parents:
diff changeset
120 * @param string $env_id
015d06b10d37 initial
dwinter
parents:
diff changeset
121 * The machine name of the environment.
015d06b10d37 initial
dwinter
parents:
diff changeset
122 *
015d06b10d37 initial
dwinter
parents:
diff changeset
123 * @return array
015d06b10d37 initial
dwinter
parents:
diff changeset
124 * An associative array with the key-value pairs:
015d06b10d37 initial
dwinter
parents:
diff changeset
125 * - remaining: The number of items left to index.
015d06b10d37 initial
dwinter
parents:
diff changeset
126 * - total: The total number of items to index.
015d06b10d37 initial
dwinter
parents:
diff changeset
127 *
015d06b10d37 initial
dwinter
parents:
diff changeset
128 * @see hook_search_status()
015d06b10d37 initial
dwinter
parents:
diff changeset
129 */
015d06b10d37 initial
dwinter
parents:
diff changeset
130 function apachesolr_index_status($env_id) {
015d06b10d37 initial
dwinter
parents:
diff changeset
131 $remaining = 0;
015d06b10d37 initial
dwinter
parents:
diff changeset
132 $total = 0;
015d06b10d37 initial
dwinter
parents:
diff changeset
133
015d06b10d37 initial
dwinter
parents:
diff changeset
134 foreach (entity_get_info() as $entity_type => $info) {
015d06b10d37 initial
dwinter
parents:
diff changeset
135 $bundles = apachesolr_get_index_bundles($env_id, $entity_type);
015d06b10d37 initial
dwinter
parents:
diff changeset
136 if (empty($bundles)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
137 continue;
015d06b10d37 initial
dwinter
parents:
diff changeset
138 }
015d06b10d37 initial
dwinter
parents:
diff changeset
139
015d06b10d37 initial
dwinter
parents:
diff changeset
140 $table = apachesolr_get_indexer_table($entity_type);
015d06b10d37 initial
dwinter
parents:
diff changeset
141 $query = db_select($table, 'asn')->condition('asn.status', 1)->condition('asn.bundle', $bundles);
015d06b10d37 initial
dwinter
parents:
diff changeset
142 $total += $query->countQuery()->execute()->fetchField();
015d06b10d37 initial
dwinter
parents:
diff changeset
143
015d06b10d37 initial
dwinter
parents:
diff changeset
144 // Get $last_entity_id and $last_changed.
015d06b10d37 initial
dwinter
parents:
diff changeset
145 $last_index_position = apachesolr_get_last_index_position($env_id, $entity_type);
015d06b10d37 initial
dwinter
parents:
diff changeset
146 $last_entity_id = $last_index_position['last_entity_id'];
015d06b10d37 initial
dwinter
parents:
diff changeset
147 $last_changed = $last_index_position['last_changed'];
015d06b10d37 initial
dwinter
parents:
diff changeset
148
015d06b10d37 initial
dwinter
parents:
diff changeset
149 // Find the remaining entities to index for this entity type.
015d06b10d37 initial
dwinter
parents:
diff changeset
150 $query = db_select($table, 'aie')
015d06b10d37 initial
dwinter
parents:
diff changeset
151 ->condition('aie.bundle', $bundles)
015d06b10d37 initial
dwinter
parents:
diff changeset
152 ->condition('aie.status', 1)
015d06b10d37 initial
dwinter
parents:
diff changeset
153 ->condition(db_or()
015d06b10d37 initial
dwinter
parents:
diff changeset
154 ->condition('aie.changed', $last_changed, '>')
015d06b10d37 initial
dwinter
parents:
diff changeset
155 ->condition(db_and()
015d06b10d37 initial
dwinter
parents:
diff changeset
156 ->condition('aie.changed', $last_changed, '<=')
015d06b10d37 initial
dwinter
parents:
diff changeset
157 ->condition('aie.entity_id', $last_entity_id, '>')))
015d06b10d37 initial
dwinter
parents:
diff changeset
158 ->addTag('apachesolr_index_' . $entity_type);
015d06b10d37 initial
dwinter
parents:
diff changeset
159
015d06b10d37 initial
dwinter
parents:
diff changeset
160
015d06b10d37 initial
dwinter
parents:
diff changeset
161 if ($table == 'apachesolr_index_entities') {
015d06b10d37 initial
dwinter
parents:
diff changeset
162 // Other, entity-specific tables don't need this condition.
015d06b10d37 initial
dwinter
parents:
diff changeset
163 $query->condition('aie.entity_type', $entity_type);
015d06b10d37 initial
dwinter
parents:
diff changeset
164 }
015d06b10d37 initial
dwinter
parents:
diff changeset
165 $remaining += $query->countQuery()->execute()->fetchField();
015d06b10d37 initial
dwinter
parents:
diff changeset
166 }
015d06b10d37 initial
dwinter
parents:
diff changeset
167 return array('remaining' => $remaining, 'total' => $total);
015d06b10d37 initial
dwinter
parents:
diff changeset
168 }
015d06b10d37 initial
dwinter
parents:
diff changeset
169
015d06b10d37 initial
dwinter
parents:
diff changeset
170 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
171 * Worker callback for apachesolr_index_entities().
015d06b10d37 initial
dwinter
parents:
diff changeset
172 *
015d06b10d37 initial
dwinter
parents:
diff changeset
173 * Loads and proccesses the entity queued for indexing and converts into one or
015d06b10d37 initial
dwinter
parents:
diff changeset
174 * more documents that are sent to the Apache Solr server for indexing.
015d06b10d37 initial
dwinter
parents:
diff changeset
175 *
015d06b10d37 initial
dwinter
parents:
diff changeset
176 * The entity is loaded as the user specified in the "apachesolr_index_user"
015d06b10d37 initial
dwinter
parents:
diff changeset
177 * system variable in order to prevent sentive data from being indexed and
015d06b10d37 initial
dwinter
parents:
diff changeset
178 * displayed to underprivileged users in search results. The index user defaults
015d06b10d37 initial
dwinter
parents:
diff changeset
179 * to a user ID of "0", which is the anonymous user.
015d06b10d37 initial
dwinter
parents:
diff changeset
180 *
015d06b10d37 initial
dwinter
parents:
diff changeset
181 * After the entity is loaded, it is converted to an array via the callback
015d06b10d37 initial
dwinter
parents:
diff changeset
182 * specified in the entity type's info array. The array that the entity is
015d06b10d37 initial
dwinter
parents:
diff changeset
183 * converted to is the model of the document sent to the Apache Solr server for
015d06b10d37 initial
dwinter
parents:
diff changeset
184 * indexing. This function allows develoeprs to modify the document by
015d06b10d37 initial
dwinter
parents:
diff changeset
185 * implementing the following hooks:
015d06b10d37 initial
dwinter
parents:
diff changeset
186 * - apachesolr_index_document_build()
015d06b10d37 initial
dwinter
parents:
diff changeset
187 * - apachesolr_index_document_build_ENTITY_TYPE()
015d06b10d37 initial
dwinter
parents:
diff changeset
188 * - apachesolr_index_documents_alter()
015d06b10d37 initial
dwinter
parents:
diff changeset
189 *
015d06b10d37 initial
dwinter
parents:
diff changeset
190 * @param stdClass $item
015d06b10d37 initial
dwinter
parents:
diff changeset
191 * The data returned by the queue table containing:
015d06b10d37 initial
dwinter
parents:
diff changeset
192 * - entity_id: An integer containing the unique identifier of the entity, for
015d06b10d37 initial
dwinter
parents:
diff changeset
193 * example a node ID or comment ID.
015d06b10d37 initial
dwinter
parents:
diff changeset
194 * - entity_type: The unique identifier for the entity, i.e. "node", "file".
015d06b10d37 initial
dwinter
parents:
diff changeset
195 * - bundle: The machine-readable name of the bundle the passed entity is
015d06b10d37 initial
dwinter
parents:
diff changeset
196 * associated with.
015d06b10d37 initial
dwinter
parents:
diff changeset
197 * - status: The "published" status of the entity. The status will also be set
015d06b10d37 initial
dwinter
parents:
diff changeset
198 * to "0" when entity is deleted but the Apache Solr server is unavailable.
015d06b10d37 initial
dwinter
parents:
diff changeset
199 * - changed: A timestamp flagging when the entity was last modified.
015d06b10d37 initial
dwinter
parents:
diff changeset
200 * @param string $env_id
015d06b10d37 initial
dwinter
parents:
diff changeset
201 * The machine name of the environment.
015d06b10d37 initial
dwinter
parents:
diff changeset
202 *
015d06b10d37 initial
dwinter
parents:
diff changeset
203 * @return array
015d06b10d37 initial
dwinter
parents:
diff changeset
204 * An associative array of documents that are sent to the Apache Solr server
015d06b10d37 initial
dwinter
parents:
diff changeset
205 * for indexing.
015d06b10d37 initial
dwinter
parents:
diff changeset
206 *
015d06b10d37 initial
dwinter
parents:
diff changeset
207 * @see apachesolr_index_nodes() for the old-skool version.
015d06b10d37 initial
dwinter
parents:
diff changeset
208 */
015d06b10d37 initial
dwinter
parents:
diff changeset
209 function apachesolr_index_entity_to_documents($item, $env_id) {
015d06b10d37 initial
dwinter
parents:
diff changeset
210 global $user;
015d06b10d37 initial
dwinter
parents:
diff changeset
211 drupal_save_session(FALSE);
015d06b10d37 initial
dwinter
parents:
diff changeset
212 $saved_user = $user;
015d06b10d37 initial
dwinter
parents:
diff changeset
213 // build the content for the index as an anonymous user to avoid exposing restricted fields and such.
015d06b10d37 initial
dwinter
parents:
diff changeset
214 // By setting a variable, indexing can take place as a different user
015d06b10d37 initial
dwinter
parents:
diff changeset
215 $uid = variable_get('apachesolr_index_user', 0);
015d06b10d37 initial
dwinter
parents:
diff changeset
216 if ($uid == 0) {
015d06b10d37 initial
dwinter
parents:
diff changeset
217 $user = drupal_anonymous_user();
015d06b10d37 initial
dwinter
parents:
diff changeset
218 }
015d06b10d37 initial
dwinter
parents:
diff changeset
219 else {
015d06b10d37 initial
dwinter
parents:
diff changeset
220 $user = user_load($uid);
015d06b10d37 initial
dwinter
parents:
diff changeset
221 }
015d06b10d37 initial
dwinter
parents:
diff changeset
222 // Pull out all of our pertinent data.
015d06b10d37 initial
dwinter
parents:
diff changeset
223 $entity_type = $item->entity_type;
015d06b10d37 initial
dwinter
parents:
diff changeset
224
015d06b10d37 initial
dwinter
parents:
diff changeset
225 // Entity cache will be reset at the end of the indexing algorithm, to use the cache properly whenever
015d06b10d37 initial
dwinter
parents:
diff changeset
226 // the code does another entity_load
015d06b10d37 initial
dwinter
parents:
diff changeset
227 $entity = entity_load($entity_type, array($item->entity_id));
015d06b10d37 initial
dwinter
parents:
diff changeset
228 $entity = $entity ? reset($entity) : FALSE;
015d06b10d37 initial
dwinter
parents:
diff changeset
229
015d06b10d37 initial
dwinter
parents:
diff changeset
230 if (empty($entity)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
231 // If the object failed to load, just stop.
015d06b10d37 initial
dwinter
parents:
diff changeset
232 return FALSE;
015d06b10d37 initial
dwinter
parents:
diff changeset
233 }
015d06b10d37 initial
dwinter
parents:
diff changeset
234
015d06b10d37 initial
dwinter
parents:
diff changeset
235 list($entity_id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
015d06b10d37 initial
dwinter
parents:
diff changeset
236
015d06b10d37 initial
dwinter
parents:
diff changeset
237 // Create a new document, and do the bare minimum on it.
015d06b10d37 initial
dwinter
parents:
diff changeset
238 $document = _apachesolr_index_process_entity_get_document($entity, $entity_type);
015d06b10d37 initial
dwinter
parents:
diff changeset
239
015d06b10d37 initial
dwinter
parents:
diff changeset
240 //Get the callback array to add stuff to the document
015d06b10d37 initial
dwinter
parents:
diff changeset
241 $callbacks = apachesolr_entity_get_callback($entity_type, 'document callback', $bundle);
015d06b10d37 initial
dwinter
parents:
diff changeset
242 $documents = array();
015d06b10d37 initial
dwinter
parents:
diff changeset
243 foreach ($callbacks as $callback) {
015d06b10d37 initial
dwinter
parents:
diff changeset
244 // Call a type-specific callback to add stuff to the document.
015d06b10d37 initial
dwinter
parents:
diff changeset
245 $documents = array_merge($documents, $callback($document, $entity, $entity_type, $env_id));
015d06b10d37 initial
dwinter
parents:
diff changeset
246 }
015d06b10d37 initial
dwinter
parents:
diff changeset
247
015d06b10d37 initial
dwinter
parents:
diff changeset
248 //do this for all possible documents that were returned by the callbacks
015d06b10d37 initial
dwinter
parents:
diff changeset
249 foreach ($documents as $document) {
015d06b10d37 initial
dwinter
parents:
diff changeset
250 // Call an all-entity hook to add stuff to the document.
015d06b10d37 initial
dwinter
parents:
diff changeset
251 module_invoke_all('apachesolr_index_document_build', $document, $entity, $entity_type, $env_id);
015d06b10d37 initial
dwinter
parents:
diff changeset
252
015d06b10d37 initial
dwinter
parents:
diff changeset
253 // Call a type-specific hook to add stuff to the document.
015d06b10d37 initial
dwinter
parents:
diff changeset
254 module_invoke_all('apachesolr_index_document_build_' . $entity_type, $document, $entity, $env_id);
015d06b10d37 initial
dwinter
parents:
diff changeset
255
015d06b10d37 initial
dwinter
parents:
diff changeset
256 // Final processing to ensure that the document is properly structured.
015d06b10d37 initial
dwinter
parents:
diff changeset
257 // All records must have a label field, which is used for user-friendly labeling.
015d06b10d37 initial
dwinter
parents:
diff changeset
258 if (empty($document->label)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
259 $document->label = '';
015d06b10d37 initial
dwinter
parents:
diff changeset
260 }
015d06b10d37 initial
dwinter
parents:
diff changeset
261
015d06b10d37 initial
dwinter
parents:
diff changeset
262 // All records must have a "content" field, which is used for fulltext indexing.
015d06b10d37 initial
dwinter
parents:
diff changeset
263 // If we don't have one, enter an empty value. This does mean that the entity
015d06b10d37 initial
dwinter
parents:
diff changeset
264 // will not be fulltext searchable.
015d06b10d37 initial
dwinter
parents:
diff changeset
265 if (empty($document->content)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
266 $document->content = '';
015d06b10d37 initial
dwinter
parents:
diff changeset
267 }
015d06b10d37 initial
dwinter
parents:
diff changeset
268
015d06b10d37 initial
dwinter
parents:
diff changeset
269 // All records must have a "teaser" field, which is used for abbreviated
015d06b10d37 initial
dwinter
parents:
diff changeset
270 // displays when no highlighted text is available.
015d06b10d37 initial
dwinter
parents:
diff changeset
271 if (empty($document->teaser)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
272 $document->teaser = truncate_utf8($document->content, 300, TRUE);
015d06b10d37 initial
dwinter
parents:
diff changeset
273 }
015d06b10d37 initial
dwinter
parents:
diff changeset
274
015d06b10d37 initial
dwinter
parents:
diff changeset
275 // Add additional indexing based on the body of each record.
015d06b10d37 initial
dwinter
parents:
diff changeset
276 apachesolr_index_add_tags_to_document($document, $document->content);
015d06b10d37 initial
dwinter
parents:
diff changeset
277 }
015d06b10d37 initial
dwinter
parents:
diff changeset
278
015d06b10d37 initial
dwinter
parents:
diff changeset
279 // Now allow modules to alter each other's additions for maximum flexibility.
015d06b10d37 initial
dwinter
parents:
diff changeset
280
015d06b10d37 initial
dwinter
parents:
diff changeset
281 // Hook to allow modifications of the retrieved results
015d06b10d37 initial
dwinter
parents:
diff changeset
282 foreach (module_implements('apachesolr_index_documents_alter') as $module) {
015d06b10d37 initial
dwinter
parents:
diff changeset
283 $function = $module . '_apachesolr_index_documents_alter';
015d06b10d37 initial
dwinter
parents:
diff changeset
284 $function($documents, $entity, $entity_type, $env_id);
015d06b10d37 initial
dwinter
parents:
diff changeset
285 }
015d06b10d37 initial
dwinter
parents:
diff changeset
286
015d06b10d37 initial
dwinter
parents:
diff changeset
287 // Restore the user.
015d06b10d37 initial
dwinter
parents:
diff changeset
288 $user = $saved_user;
015d06b10d37 initial
dwinter
parents:
diff changeset
289 drupal_save_session(TRUE);
015d06b10d37 initial
dwinter
parents:
diff changeset
290
015d06b10d37 initial
dwinter
parents:
diff changeset
291 return $documents;
015d06b10d37 initial
dwinter
parents:
diff changeset
292 }
015d06b10d37 initial
dwinter
parents:
diff changeset
293
015d06b10d37 initial
dwinter
parents:
diff changeset
294 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
295 * Index an array of documents to solr.
015d06b10d37 initial
dwinter
parents:
diff changeset
296 *
015d06b10d37 initial
dwinter
parents:
diff changeset
297 * @param $env_id
015d06b10d37 initial
dwinter
parents:
diff changeset
298 * @param array $documents
015d06b10d37 initial
dwinter
parents:
diff changeset
299 *
015d06b10d37 initial
dwinter
parents:
diff changeset
300 * @return bool|int number indexed, or FALSE on failure.
015d06b10d37 initial
dwinter
parents:
diff changeset
301 * @throws Exception
015d06b10d37 initial
dwinter
parents:
diff changeset
302 */
015d06b10d37 initial
dwinter
parents:
diff changeset
303 function apachesolr_index_send_to_solr($env_id, array $documents) {
015d06b10d37 initial
dwinter
parents:
diff changeset
304 try {
015d06b10d37 initial
dwinter
parents:
diff changeset
305 // Get the $solr object
015d06b10d37 initial
dwinter
parents:
diff changeset
306 $solr = apachesolr_get_solr($env_id);
015d06b10d37 initial
dwinter
parents:
diff changeset
307 // If there is no server available, don't continue.
015d06b10d37 initial
dwinter
parents:
diff changeset
308 if (!$solr->ping(variable_get('apachesolr_ping_timeout', 4))) {
015d06b10d37 initial
dwinter
parents:
diff changeset
309 throw new Exception(t('No Solr instance available during indexing.'));
015d06b10d37 initial
dwinter
parents:
diff changeset
310 }
015d06b10d37 initial
dwinter
parents:
diff changeset
311 }
015d06b10d37 initial
dwinter
parents:
diff changeset
312 catch (Exception $e) {
015d06b10d37 initial
dwinter
parents:
diff changeset
313 watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR);
015d06b10d37 initial
dwinter
parents:
diff changeset
314 return FALSE;
015d06b10d37 initial
dwinter
parents:
diff changeset
315 }
015d06b10d37 initial
dwinter
parents:
diff changeset
316 // Do not index when we do not have any documents to send
015d06b10d37 initial
dwinter
parents:
diff changeset
317 // Send TRUE because this is not an error
015d06b10d37 initial
dwinter
parents:
diff changeset
318 if (empty($documents)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
319 return TRUE;
015d06b10d37 initial
dwinter
parents:
diff changeset
320 }
015d06b10d37 initial
dwinter
parents:
diff changeset
321 // Send the document off to Solr.
015d06b10d37 initial
dwinter
parents:
diff changeset
322 watchdog('Apache Solr', 'Adding @count documents.', array('@count' => count($documents)));
015d06b10d37 initial
dwinter
parents:
diff changeset
323 try {
015d06b10d37 initial
dwinter
parents:
diff changeset
324 $docs_chunk = array_chunk($documents, 20);
015d06b10d37 initial
dwinter
parents:
diff changeset
325 foreach ($docs_chunk as $docs) {
015d06b10d37 initial
dwinter
parents:
diff changeset
326 $solr->addDocuments($docs);
015d06b10d37 initial
dwinter
parents:
diff changeset
327 }
015d06b10d37 initial
dwinter
parents:
diff changeset
328 watchdog('Apache Solr', 'Indexing succeeded on @count documents', array(
015d06b10d37 initial
dwinter
parents:
diff changeset
329 '@count' => count($documents),
015d06b10d37 initial
dwinter
parents:
diff changeset
330 ), WATCHDOG_INFO);
015d06b10d37 initial
dwinter
parents:
diff changeset
331 return count($documents);
015d06b10d37 initial
dwinter
parents:
diff changeset
332 }
015d06b10d37 initial
dwinter
parents:
diff changeset
333 catch (Exception $e) {
015d06b10d37 initial
dwinter
parents:
diff changeset
334 if (!empty($docs)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
335 foreach ($docs as $doc) {
015d06b10d37 initial
dwinter
parents:
diff changeset
336 $eids[] = $doc->entity_type . '/' . $doc->entity_id;
015d06b10d37 initial
dwinter
parents:
diff changeset
337 }
015d06b10d37 initial
dwinter
parents:
diff changeset
338 }
015d06b10d37 initial
dwinter
parents:
diff changeset
339 watchdog('Apache Solr', 'Indexing failed on one of the following entity ids: @eids <br /> !message', array(
015d06b10d37 initial
dwinter
parents:
diff changeset
340 '@eids' => implode(', ', $eids),
015d06b10d37 initial
dwinter
parents:
diff changeset
341 '!message' => nl2br(strip_tags($e->getMessage())),
015d06b10d37 initial
dwinter
parents:
diff changeset
342 ), WATCHDOG_ERROR);
015d06b10d37 initial
dwinter
parents:
diff changeset
343 return FALSE;
015d06b10d37 initial
dwinter
parents:
diff changeset
344 }
015d06b10d37 initial
dwinter
parents:
diff changeset
345 }
015d06b10d37 initial
dwinter
parents:
diff changeset
346
015d06b10d37 initial
dwinter
parents:
diff changeset
347 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
348 * Extract HTML tag contents from $text and add to boost fields.
015d06b10d37 initial
dwinter
parents:
diff changeset
349 *
015d06b10d37 initial
dwinter
parents:
diff changeset
350 * @param ApacheSolrDocument $document
015d06b10d37 initial
dwinter
parents:
diff changeset
351 * @param string $text
015d06b10d37 initial
dwinter
parents:
diff changeset
352 * must be stripped of control characters before hand.
015d06b10d37 initial
dwinter
parents:
diff changeset
353 *
015d06b10d37 initial
dwinter
parents:
diff changeset
354 */
015d06b10d37 initial
dwinter
parents:
diff changeset
355 function apachesolr_index_add_tags_to_document(ApacheSolrDocument $document, $text) {
015d06b10d37 initial
dwinter
parents:
diff changeset
356 $tags_to_index = variable_get('apachesolr_tags_to_index', array(
015d06b10d37 initial
dwinter
parents:
diff changeset
357 'h1' => 'tags_h1',
015d06b10d37 initial
dwinter
parents:
diff changeset
358 'h2' => 'tags_h2_h3',
015d06b10d37 initial
dwinter
parents:
diff changeset
359 'h3' => 'tags_h2_h3',
015d06b10d37 initial
dwinter
parents:
diff changeset
360 'h4' => 'tags_h4_h5_h6',
015d06b10d37 initial
dwinter
parents:
diff changeset
361 'h5' => 'tags_h4_h5_h6',
015d06b10d37 initial
dwinter
parents:
diff changeset
362 'h6' => 'tags_h4_h5_h6',
015d06b10d37 initial
dwinter
parents:
diff changeset
363 'u' => 'tags_inline',
015d06b10d37 initial
dwinter
parents:
diff changeset
364 'b' => 'tags_inline',
015d06b10d37 initial
dwinter
parents:
diff changeset
365 'i' => 'tags_inline',
015d06b10d37 initial
dwinter
parents:
diff changeset
366 'strong' => 'tags_inline',
015d06b10d37 initial
dwinter
parents:
diff changeset
367 'em' => 'tags_inline',
015d06b10d37 initial
dwinter
parents:
diff changeset
368 'a' => 'tags_a'
015d06b10d37 initial
dwinter
parents:
diff changeset
369 ));
015d06b10d37 initial
dwinter
parents:
diff changeset
370
015d06b10d37 initial
dwinter
parents:
diff changeset
371 // Strip off all ignored tags.
015d06b10d37 initial
dwinter
parents:
diff changeset
372 $text = strip_tags($text, '<' . implode('><', array_keys($tags_to_index)) . '>');
015d06b10d37 initial
dwinter
parents:
diff changeset
373
015d06b10d37 initial
dwinter
parents:
diff changeset
374 preg_match_all('@<(' . implode('|', array_keys($tags_to_index)) . ')[^>]*>(.*)</\1>@Ui', $text, $matches);
015d06b10d37 initial
dwinter
parents:
diff changeset
375 foreach ($matches[1] as $key => $tag) {
015d06b10d37 initial
dwinter
parents:
diff changeset
376 $tag = drupal_strtolower($tag);
015d06b10d37 initial
dwinter
parents:
diff changeset
377 // We don't want to index links auto-generated by the url filter.
015d06b10d37 initial
dwinter
parents:
diff changeset
378 if ($tag != 'a' || !preg_match('@(?:http://|https://|ftp://|mailto:|smb://|afp://|file://|gopher://|news://|ssl://|sslv2://|sslv3://|tls://|tcp://|udp://|www\.)[a-zA-Z0-9]+@', $matches[2][$key])) {
015d06b10d37 initial
dwinter
parents:
diff changeset
379 if (!isset($document->{$tags_to_index[$tag]})) {
015d06b10d37 initial
dwinter
parents:
diff changeset
380 $document->{$tags_to_index[$tag]} = '';
015d06b10d37 initial
dwinter
parents:
diff changeset
381 }
015d06b10d37 initial
dwinter
parents:
diff changeset
382 $document->{$tags_to_index[$tag]} .= ' ' . apachesolr_clean_text($matches[2][$key]);
015d06b10d37 initial
dwinter
parents:
diff changeset
383 }
015d06b10d37 initial
dwinter
parents:
diff changeset
384 }
015d06b10d37 initial
dwinter
parents:
diff changeset
385 }
015d06b10d37 initial
dwinter
parents:
diff changeset
386
015d06b10d37 initial
dwinter
parents:
diff changeset
387 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
388 * Returns a generic Solr document object for this entity.
015d06b10d37 initial
dwinter
parents:
diff changeset
389 *
015d06b10d37 initial
dwinter
parents:
diff changeset
390 * This function will do the basic processing for the document that is common
015d06b10d37 initial
dwinter
parents:
diff changeset
391 * to all entities, but virtually all entities will need their own additional
015d06b10d37 initial
dwinter
parents:
diff changeset
392 * processing.
015d06b10d37 initial
dwinter
parents:
diff changeset
393 *
015d06b10d37 initial
dwinter
parents:
diff changeset
394 * @param object $entity
015d06b10d37 initial
dwinter
parents:
diff changeset
395 * The entity for which we want a document.
015d06b10d37 initial
dwinter
parents:
diff changeset
396 * @param string $entity_type
015d06b10d37 initial
dwinter
parents:
diff changeset
397 * The type of entity we're processing.
015d06b10d37 initial
dwinter
parents:
diff changeset
398 * @return ApacheSolrDocument
015d06b10d37 initial
dwinter
parents:
diff changeset
399 */
015d06b10d37 initial
dwinter
parents:
diff changeset
400 function _apachesolr_index_process_entity_get_document($entity, $entity_type) {
015d06b10d37 initial
dwinter
parents:
diff changeset
401 list($entity_id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
015d06b10d37 initial
dwinter
parents:
diff changeset
402
015d06b10d37 initial
dwinter
parents:
diff changeset
403 $document = new ApacheSolrDocument();
015d06b10d37 initial
dwinter
parents:
diff changeset
404
015d06b10d37 initial
dwinter
parents:
diff changeset
405 // Define our url options in advance. This differs depending on the
015d06b10d37 initial
dwinter
parents:
diff changeset
406 // language
015d06b10d37 initial
dwinter
parents:
diff changeset
407 $languages = language_list();
015d06b10d37 initial
dwinter
parents:
diff changeset
408 $url_options = array('absolute' => TRUE);
015d06b10d37 initial
dwinter
parents:
diff changeset
409 if (isset($entity->language) && isset($languages[$entity->language])) {
015d06b10d37 initial
dwinter
parents:
diff changeset
410 $url_options = $url_options + array('language' => $languages[$entity->language]);
015d06b10d37 initial
dwinter
parents:
diff changeset
411 }
015d06b10d37 initial
dwinter
parents:
diff changeset
412
015d06b10d37 initial
dwinter
parents:
diff changeset
413 $document->id = apachesolr_document_id($entity_id, $entity_type);
015d06b10d37 initial
dwinter
parents:
diff changeset
414 $document->site = url(NULL, $url_options);
015d06b10d37 initial
dwinter
parents:
diff changeset
415 $document->hash = apachesolr_site_hash();
015d06b10d37 initial
dwinter
parents:
diff changeset
416
015d06b10d37 initial
dwinter
parents:
diff changeset
417 $document->entity_id = $entity_id;
015d06b10d37 initial
dwinter
parents:
diff changeset
418 $document->entity_type = $entity_type;
015d06b10d37 initial
dwinter
parents:
diff changeset
419 $document->bundle = $bundle;
015d06b10d37 initial
dwinter
parents:
diff changeset
420 $document->bundle_name = entity_bundle_label($entity_type, $bundle);
015d06b10d37 initial
dwinter
parents:
diff changeset
421
015d06b10d37 initial
dwinter
parents:
diff changeset
422 if (empty($entity->language)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
423 // 'und' is the language-neutral code in Drupal 7.
015d06b10d37 initial
dwinter
parents:
diff changeset
424 $document->language = LANGUAGE_NONE;
015d06b10d37 initial
dwinter
parents:
diff changeset
425 }
015d06b10d37 initial
dwinter
parents:
diff changeset
426 else {
015d06b10d37 initial
dwinter
parents:
diff changeset
427 $document->language = $entity->language;
015d06b10d37 initial
dwinter
parents:
diff changeset
428 }
015d06b10d37 initial
dwinter
parents:
diff changeset
429
015d06b10d37 initial
dwinter
parents:
diff changeset
430 $path = entity_uri($entity_type, $entity);
015d06b10d37 initial
dwinter
parents:
diff changeset
431 // A path is not a requirement of an entity
015d06b10d37 initial
dwinter
parents:
diff changeset
432 if (!empty($path)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
433 $document->path = $path['path'];
015d06b10d37 initial
dwinter
parents:
diff changeset
434 $document->url = url($path['path'], $path['options'] + $url_options);
015d06b10d37 initial
dwinter
parents:
diff changeset
435 // Path aliases can have important information about the content.
015d06b10d37 initial
dwinter
parents:
diff changeset
436 // Add them to the index as well.
015d06b10d37 initial
dwinter
parents:
diff changeset
437 if (function_exists('drupal_get_path_alias')) {
015d06b10d37 initial
dwinter
parents:
diff changeset
438 // Add any path alias to the index, looking first for language specific
015d06b10d37 initial
dwinter
parents:
diff changeset
439 // aliases but using language neutral aliases otherwise.
015d06b10d37 initial
dwinter
parents:
diff changeset
440 $output = drupal_get_path_alias($document->path, $document->language);
015d06b10d37 initial
dwinter
parents:
diff changeset
441 if ($output && $output != $document->path) {
015d06b10d37 initial
dwinter
parents:
diff changeset
442 $document->path_alias = $output;
015d06b10d37 initial
dwinter
parents:
diff changeset
443 }
015d06b10d37 initial
dwinter
parents:
diff changeset
444 }
015d06b10d37 initial
dwinter
parents:
diff changeset
445 }
015d06b10d37 initial
dwinter
parents:
diff changeset
446 return $document;
015d06b10d37 initial
dwinter
parents:
diff changeset
447 }
015d06b10d37 initial
dwinter
parents:
diff changeset
448
015d06b10d37 initial
dwinter
parents:
diff changeset
449 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
450 * Returns an array of rows from a query based on an indexing environment.
015d06b10d37 initial
dwinter
parents:
diff changeset
451 * @todo Remove the read only because it is not environment specific
015d06b10d37 initial
dwinter
parents:
diff changeset
452 *
015d06b10d37 initial
dwinter
parents:
diff changeset
453 * @param $env_id
015d06b10d37 initial
dwinter
parents:
diff changeset
454 * @param $entity_type
015d06b10d37 initial
dwinter
parents:
diff changeset
455 * @param $limit
015d06b10d37 initial
dwinter
parents:
diff changeset
456 *
015d06b10d37 initial
dwinter
parents:
diff changeset
457 * @return array list of row to index
015d06b10d37 initial
dwinter
parents:
diff changeset
458 */
015d06b10d37 initial
dwinter
parents:
diff changeset
459 function apachesolr_index_get_entities_to_index($env_id, $entity_type, $limit) {
015d06b10d37 initial
dwinter
parents:
diff changeset
460 $rows = array();
015d06b10d37 initial
dwinter
parents:
diff changeset
461 if (variable_get('apachesolr_read_only', 0)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
462 return $rows;
015d06b10d37 initial
dwinter
parents:
diff changeset
463 }
015d06b10d37 initial
dwinter
parents:
diff changeset
464 $bundles = apachesolr_get_index_bundles($env_id, $entity_type);
015d06b10d37 initial
dwinter
parents:
diff changeset
465 if (empty($bundles)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
466 return $rows;
015d06b10d37 initial
dwinter
parents:
diff changeset
467 }
015d06b10d37 initial
dwinter
parents:
diff changeset
468
015d06b10d37 initial
dwinter
parents:
diff changeset
469 $table = apachesolr_get_indexer_table($entity_type);
015d06b10d37 initial
dwinter
parents:
diff changeset
470 // Get $last_entity_id and $last_changed.
015d06b10d37 initial
dwinter
parents:
diff changeset
471 $last_index_position = apachesolr_get_last_index_position($env_id, $entity_type);
015d06b10d37 initial
dwinter
parents:
diff changeset
472 $last_entity_id = $last_index_position['last_entity_id'];
015d06b10d37 initial
dwinter
parents:
diff changeset
473 $last_changed = $last_index_position['last_changed'];
015d06b10d37 initial
dwinter
parents:
diff changeset
474
015d06b10d37 initial
dwinter
parents:
diff changeset
475 // Find the next batch of entities to index for this entity type. Note that
015d06b10d37 initial
dwinter
parents:
diff changeset
476 // for ordering we're grabbing the oldest first and then ordering by ID so
015d06b10d37 initial
dwinter
parents:
diff changeset
477 // that we get a definitive order.
015d06b10d37 initial
dwinter
parents:
diff changeset
478 // Also note that we fetch ALL fields from the indexer table
015d06b10d37 initial
dwinter
parents:
diff changeset
479 $query = db_select($table, 'aie')
015d06b10d37 initial
dwinter
parents:
diff changeset
480 ->fields('aie')
015d06b10d37 initial
dwinter
parents:
diff changeset
481 ->condition('aie.bundle', $bundles)
015d06b10d37 initial
dwinter
parents:
diff changeset
482 ->condition(db_or()
015d06b10d37 initial
dwinter
parents:
diff changeset
483 ->condition('aie.changed', $last_changed, '>')
015d06b10d37 initial
dwinter
parents:
diff changeset
484 ->condition(db_and()
015d06b10d37 initial
dwinter
parents:
diff changeset
485 ->condition('aie.changed', $last_changed, '<=')
015d06b10d37 initial
dwinter
parents:
diff changeset
486 ->condition('aie.entity_id', $last_entity_id, '>')))
015d06b10d37 initial
dwinter
parents:
diff changeset
487 ->orderBy('aie.changed', 'ASC')
015d06b10d37 initial
dwinter
parents:
diff changeset
488 ->orderBy('aie.entity_id', 'ASC')
015d06b10d37 initial
dwinter
parents:
diff changeset
489 ->addTag('apachesolr_index_' . $entity_type);
015d06b10d37 initial
dwinter
parents:
diff changeset
490
015d06b10d37 initial
dwinter
parents:
diff changeset
491 if ($table == 'apachesolr_index_entities') {
015d06b10d37 initial
dwinter
parents:
diff changeset
492 // Other, entity-specific tables don't need this condition.
015d06b10d37 initial
dwinter
parents:
diff changeset
493 $query->condition('aie.entity_type', $entity_type);
015d06b10d37 initial
dwinter
parents:
diff changeset
494 }
015d06b10d37 initial
dwinter
parents:
diff changeset
495 $query->range(0, $limit);
015d06b10d37 initial
dwinter
parents:
diff changeset
496 $records = $query->execute();
015d06b10d37 initial
dwinter
parents:
diff changeset
497
015d06b10d37 initial
dwinter
parents:
diff changeset
498 $status_callbacks = apachesolr_entity_get_callback($entity_type, 'status callback');
015d06b10d37 initial
dwinter
parents:
diff changeset
499 foreach ($records as $record) {
015d06b10d37 initial
dwinter
parents:
diff changeset
500 // Check status and status callbacks before sending to the index
015d06b10d37 initial
dwinter
parents:
diff changeset
501 if (is_array($status_callbacks)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
502 foreach($status_callbacks as $status_callback) {
015d06b10d37 initial
dwinter
parents:
diff changeset
503 if (is_callable($status_callback)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
504 // by placing $status in front we prevent calling any other callback
015d06b10d37 initial
dwinter
parents:
diff changeset
505 // after one status callback returned false
015d06b10d37 initial
dwinter
parents:
diff changeset
506 $record->status = $record->status && $status_callback($record->entity_id, $record->entity_type);
015d06b10d37 initial
dwinter
parents:
diff changeset
507 }
015d06b10d37 initial
dwinter
parents:
diff changeset
508 }
015d06b10d37 initial
dwinter
parents:
diff changeset
509 }
015d06b10d37 initial
dwinter
parents:
diff changeset
510 $rows[] = $record;
015d06b10d37 initial
dwinter
parents:
diff changeset
511 }
015d06b10d37 initial
dwinter
parents:
diff changeset
512 return $rows;
015d06b10d37 initial
dwinter
parents:
diff changeset
513 }
015d06b10d37 initial
dwinter
parents:
diff changeset
514
015d06b10d37 initial
dwinter
parents:
diff changeset
515 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
516 * Delete the whole index for an environment.
015d06b10d37 initial
dwinter
parents:
diff changeset
517 *
015d06b10d37 initial
dwinter
parents:
diff changeset
518 * @param string $env_id
015d06b10d37 initial
dwinter
parents:
diff changeset
519 * The machine name of the environment.
015d06b10d37 initial
dwinter
parents:
diff changeset
520 * @param string $entity_type
015d06b10d37 initial
dwinter
parents:
diff changeset
521 * (optional) specify to remove just this entity_type from the index.
015d06b10d37 initial
dwinter
parents:
diff changeset
522 * @param string $bundle
015d06b10d37 initial
dwinter
parents:
diff changeset
523 * (optional) also specify a bundle to remove just the bundle from
015d06b10d37 initial
dwinter
parents:
diff changeset
524 * the index.
015d06b10d37 initial
dwinter
parents:
diff changeset
525 */
015d06b10d37 initial
dwinter
parents:
diff changeset
526 function apachesolr_index_delete_index($env_id, $entity_type = NULL, $bundle = NULL) {
015d06b10d37 initial
dwinter
parents:
diff changeset
527 // Instantiate a new Solr object.
015d06b10d37 initial
dwinter
parents:
diff changeset
528 try {
015d06b10d37 initial
dwinter
parents:
diff changeset
529 $solr = apachesolr_get_solr($env_id);
015d06b10d37 initial
dwinter
parents:
diff changeset
530 $query = '*:*';
015d06b10d37 initial
dwinter
parents:
diff changeset
531
015d06b10d37 initial
dwinter
parents:
diff changeset
532 if (!empty($entity_type) && !empty($bundle)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
533 $query = "(bundle:$bundle AND entity_type:$entity_type) OR sm_parent_entity_bundle:{$entity_type}-{$bundle}";
015d06b10d37 initial
dwinter
parents:
diff changeset
534 }
015d06b10d37 initial
dwinter
parents:
diff changeset
535 elseif (!empty($bundle)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
536 $query = "(bundle:$bundle)";
015d06b10d37 initial
dwinter
parents:
diff changeset
537 }
015d06b10d37 initial
dwinter
parents:
diff changeset
538
015d06b10d37 initial
dwinter
parents:
diff changeset
539 // Allow other modules to modify the delete query.
015d06b10d37 initial
dwinter
parents:
diff changeset
540 // For example, use the site hash so that you only delete this site's
015d06b10d37 initial
dwinter
parents:
diff changeset
541 // content: $query = 'hash:' . apachesolr_site_hash()
015d06b10d37 initial
dwinter
parents:
diff changeset
542 drupal_alter('apachesolr_delete_by_query', $query);
015d06b10d37 initial
dwinter
parents:
diff changeset
543 $solr->deleteByQuery($query);
015d06b10d37 initial
dwinter
parents:
diff changeset
544 $solr->commit();
015d06b10d37 initial
dwinter
parents:
diff changeset
545
015d06b10d37 initial
dwinter
parents:
diff changeset
546 if (!empty($entity_type)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
547 $rebuild_callback = apachesolr_entity_get_callback($entity_type, 'reindex callback');
015d06b10d37 initial
dwinter
parents:
diff changeset
548 if (is_callable($rebuild_callback)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
549 $rebuild_callback($env_id, $bundle);
015d06b10d37 initial
dwinter
parents:
diff changeset
550 }
015d06b10d37 initial
dwinter
parents:
diff changeset
551 }
015d06b10d37 initial
dwinter
parents:
diff changeset
552 else {
015d06b10d37 initial
dwinter
parents:
diff changeset
553 apachesolr_index_mark_for_reindex($env_id);
015d06b10d37 initial
dwinter
parents:
diff changeset
554 }
015d06b10d37 initial
dwinter
parents:
diff changeset
555
015d06b10d37 initial
dwinter
parents:
diff changeset
556 apachesolr_set_last_index_updated($env_id, REQUEST_TIME);
015d06b10d37 initial
dwinter
parents:
diff changeset
557 }
015d06b10d37 initial
dwinter
parents:
diff changeset
558 catch (Exception $e) {
015d06b10d37 initial
dwinter
parents:
diff changeset
559 watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR);
015d06b10d37 initial
dwinter
parents:
diff changeset
560 }
015d06b10d37 initial
dwinter
parents:
diff changeset
561 }
015d06b10d37 initial
dwinter
parents:
diff changeset
562
015d06b10d37 initial
dwinter
parents:
diff changeset
563 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
564 * Delete from the index documents with the entity type and any of the excluded bundles.
015d06b10d37 initial
dwinter
parents:
diff changeset
565 *
015d06b10d37 initial
dwinter
parents:
diff changeset
566 * Also deletes all documents that have the entity type and bundle as a parent.
015d06b10d37 initial
dwinter
parents:
diff changeset
567 *
015d06b10d37 initial
dwinter
parents:
diff changeset
568 * @param string $env_id
015d06b10d37 initial
dwinter
parents:
diff changeset
569 * The machine name of the environment.
015d06b10d37 initial
dwinter
parents:
diff changeset
570 * @param string $entity_type
015d06b10d37 initial
dwinter
parents:
diff changeset
571 * @param array $excluded_bundles
015d06b10d37 initial
dwinter
parents:
diff changeset
572 *
015d06b10d37 initial
dwinter
parents:
diff changeset
573 * @return true on success, false on failure.
015d06b10d37 initial
dwinter
parents:
diff changeset
574 */
015d06b10d37 initial
dwinter
parents:
diff changeset
575 function apachesolr_index_delete_bundles($env_id, $entity_type, array $excluded_bundles) {
015d06b10d37 initial
dwinter
parents:
diff changeset
576 // Remove newly omitted bundles.
015d06b10d37 initial
dwinter
parents:
diff changeset
577 try {
015d06b10d37 initial
dwinter
parents:
diff changeset
578 $solr = apachesolr_get_solr($env_id);
015d06b10d37 initial
dwinter
parents:
diff changeset
579 foreach ($excluded_bundles as $bundle) {
015d06b10d37 initial
dwinter
parents:
diff changeset
580 $query = "(bundle:$bundle AND entity_type:$entity_type) OR sm_parent_entity_bundle:{$entity_type}-{$bundle}";
015d06b10d37 initial
dwinter
parents:
diff changeset
581
015d06b10d37 initial
dwinter
parents:
diff changeset
582 // Allow other modules to modify the delete query.
015d06b10d37 initial
dwinter
parents:
diff changeset
583 // For example, use the site hash so that you only delete this site's
015d06b10d37 initial
dwinter
parents:
diff changeset
584 // content: $query = 'hash:' . apachesolr_site_hash()
015d06b10d37 initial
dwinter
parents:
diff changeset
585 drupal_alter('apachesolr_delete_by_query', $query);
015d06b10d37 initial
dwinter
parents:
diff changeset
586 $solr->deleteByQuery($query);
015d06b10d37 initial
dwinter
parents:
diff changeset
587 }
015d06b10d37 initial
dwinter
parents:
diff changeset
588 if ($excluded_bundles) {
015d06b10d37 initial
dwinter
parents:
diff changeset
589 $solr->commit();
015d06b10d37 initial
dwinter
parents:
diff changeset
590 }
015d06b10d37 initial
dwinter
parents:
diff changeset
591 return TRUE;
015d06b10d37 initial
dwinter
parents:
diff changeset
592 }
015d06b10d37 initial
dwinter
parents:
diff changeset
593 catch (Exception $e) {
015d06b10d37 initial
dwinter
parents:
diff changeset
594 watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR);
015d06b10d37 initial
dwinter
parents:
diff changeset
595 return FALSE;
015d06b10d37 initial
dwinter
parents:
diff changeset
596 }
015d06b10d37 initial
dwinter
parents:
diff changeset
597 }
015d06b10d37 initial
dwinter
parents:
diff changeset
598
015d06b10d37 initial
dwinter
parents:
diff changeset
599 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
600 * Delete an entity from the index.
015d06b10d37 initial
dwinter
parents:
diff changeset
601 *
015d06b10d37 initial
dwinter
parents:
diff changeset
602 * Also deletes all documents that have the deleted document as a parent.
015d06b10d37 initial
dwinter
parents:
diff changeset
603 *
015d06b10d37 initial
dwinter
parents:
diff changeset
604 * @param string $env_id
015d06b10d37 initial
dwinter
parents:
diff changeset
605 * The machine name of the environment.
015d06b10d37 initial
dwinter
parents:
diff changeset
606 * @param string $entity_type
015d06b10d37 initial
dwinter
parents:
diff changeset
607 * @param string $entity_id
015d06b10d37 initial
dwinter
parents:
diff changeset
608 *
015d06b10d37 initial
dwinter
parents:
diff changeset
609 * @return true on success, false on failure.
015d06b10d37 initial
dwinter
parents:
diff changeset
610 */
015d06b10d37 initial
dwinter
parents:
diff changeset
611 function apachesolr_index_delete_entity_from_index($env_id, $entity_type, $entity_id) {
015d06b10d37 initial
dwinter
parents:
diff changeset
612 static $failed = FALSE;
015d06b10d37 initial
dwinter
parents:
diff changeset
613 if ($failed) {
015d06b10d37 initial
dwinter
parents:
diff changeset
614 return FALSE;
015d06b10d37 initial
dwinter
parents:
diff changeset
615 }
015d06b10d37 initial
dwinter
parents:
diff changeset
616 try {
015d06b10d37 initial
dwinter
parents:
diff changeset
617 $solr = apachesolr_get_solr($env_id);
015d06b10d37 initial
dwinter
parents:
diff changeset
618 $document_id = apachesolr_document_id($entity_id, $entity_type);
015d06b10d37 initial
dwinter
parents:
diff changeset
619 $query = "id:\"$document_id\" OR sm_parent_document_id:\"$document_id\"";
015d06b10d37 initial
dwinter
parents:
diff changeset
620 $solr->deleteByQuery($query);
015d06b10d37 initial
dwinter
parents:
diff changeset
621 apachesolr_set_last_index_updated($env_id, REQUEST_TIME);
015d06b10d37 initial
dwinter
parents:
diff changeset
622 return TRUE;
015d06b10d37 initial
dwinter
parents:
diff changeset
623 }
015d06b10d37 initial
dwinter
parents:
diff changeset
624 catch (Exception $e) {
015d06b10d37 initial
dwinter
parents:
diff changeset
625 watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR);
015d06b10d37 initial
dwinter
parents:
diff changeset
626 // Don't keep trying queries if they are failing.
015d06b10d37 initial
dwinter
parents:
diff changeset
627 $failed = TRUE;
015d06b10d37 initial
dwinter
parents:
diff changeset
628 return FALSE;
015d06b10d37 initial
dwinter
parents:
diff changeset
629 }
015d06b10d37 initial
dwinter
parents:
diff changeset
630 }
015d06b10d37 initial
dwinter
parents:
diff changeset
631
015d06b10d37 initial
dwinter
parents:
diff changeset
632 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
633 * Mark a certain entity type for a specific environment for reindexing.
015d06b10d37 initial
dwinter
parents:
diff changeset
634 *
015d06b10d37 initial
dwinter
parents:
diff changeset
635 * @param $env_id
015d06b10d37 initial
dwinter
parents:
diff changeset
636 * @param null $entity_type
015d06b10d37 initial
dwinter
parents:
diff changeset
637 */
015d06b10d37 initial
dwinter
parents:
diff changeset
638 function apachesolr_index_mark_for_reindex($env_id, $entity_type = NULL) {
015d06b10d37 initial
dwinter
parents:
diff changeset
639 foreach (entity_get_info() as $type => $entity_info) {
015d06b10d37 initial
dwinter
parents:
diff changeset
640 if (($type == $entity_type) || ($entity_type == NULL)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
641 if (isset($entity_info['apachesolr']) && ($entity_info['apachesolr']['indexable'])) {
015d06b10d37 initial
dwinter
parents:
diff changeset
642 $reindex_callback = apachesolr_entity_get_callback($type, 'reindex callback');
015d06b10d37 initial
dwinter
parents:
diff changeset
643 if (!empty($reindex_callback)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
644 call_user_func($reindex_callback, $env_id);
015d06b10d37 initial
dwinter
parents:
diff changeset
645 }
015d06b10d37 initial
dwinter
parents:
diff changeset
646 }
015d06b10d37 initial
dwinter
parents:
diff changeset
647 }
015d06b10d37 initial
dwinter
parents:
diff changeset
648 }
015d06b10d37 initial
dwinter
parents:
diff changeset
649 apachesolr_clear_last_index_position($env_id, $entity_type);
015d06b10d37 initial
dwinter
parents:
diff changeset
650 cache_clear_all('*', 'cache_apachesolr', TRUE);
015d06b10d37 initial
dwinter
parents:
diff changeset
651 }
015d06b10d37 initial
dwinter
parents:
diff changeset
652
015d06b10d37 initial
dwinter
parents:
diff changeset
653 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
654 * Sets what bundles on the specified entity type should be indexed.
015d06b10d37 initial
dwinter
parents:
diff changeset
655 *
015d06b10d37 initial
dwinter
parents:
diff changeset
656 * @param string $env_id
015d06b10d37 initial
dwinter
parents:
diff changeset
657 * The machine name of the environment.
015d06b10d37 initial
dwinter
parents:
diff changeset
658 * @param string $entity_type
015d06b10d37 initial
dwinter
parents:
diff changeset
659 * The entity type to index.
015d06b10d37 initial
dwinter
parents:
diff changeset
660 * @param array $bundles
015d06b10d37 initial
dwinter
parents:
diff changeset
661 * The machine names of the bundles to index.
015d06b10d37 initial
dwinter
parents:
diff changeset
662 *
015d06b10d37 initial
dwinter
parents:
diff changeset
663 * @throws Exception
015d06b10d37 initial
dwinter
parents:
diff changeset
664 */
015d06b10d37 initial
dwinter
parents:
diff changeset
665 function apachesolr_index_set_bundles($env_id, $entity_type, array $bundles) {
015d06b10d37 initial
dwinter
parents:
diff changeset
666 $transaction = db_transaction();
015d06b10d37 initial
dwinter
parents:
diff changeset
667 try {
015d06b10d37 initial
dwinter
parents:
diff changeset
668 db_delete('apachesolr_index_bundles')
015d06b10d37 initial
dwinter
parents:
diff changeset
669 ->condition('env_id', $env_id)
015d06b10d37 initial
dwinter
parents:
diff changeset
670 ->condition('entity_type', $entity_type)
015d06b10d37 initial
dwinter
parents:
diff changeset
671 ->execute();
015d06b10d37 initial
dwinter
parents:
diff changeset
672
015d06b10d37 initial
dwinter
parents:
diff changeset
673 if ($bundles) {
015d06b10d37 initial
dwinter
parents:
diff changeset
674 $insert = db_insert('apachesolr_index_bundles')
015d06b10d37 initial
dwinter
parents:
diff changeset
675 ->fields(array('env_id', 'entity_type', 'bundle'));
015d06b10d37 initial
dwinter
parents:
diff changeset
676
015d06b10d37 initial
dwinter
parents:
diff changeset
677 foreach ($bundles as $bundle) {
015d06b10d37 initial
dwinter
parents:
diff changeset
678 $insert->values(array(
015d06b10d37 initial
dwinter
parents:
diff changeset
679 'env_id' => $env_id,
015d06b10d37 initial
dwinter
parents:
diff changeset
680 'entity_type' => $entity_type,
015d06b10d37 initial
dwinter
parents:
diff changeset
681 'bundle' => $bundle,
015d06b10d37 initial
dwinter
parents:
diff changeset
682 ));
015d06b10d37 initial
dwinter
parents:
diff changeset
683 }
015d06b10d37 initial
dwinter
parents:
diff changeset
684 $insert->execute();
015d06b10d37 initial
dwinter
parents:
diff changeset
685 }
015d06b10d37 initial
dwinter
parents:
diff changeset
686 }
015d06b10d37 initial
dwinter
parents:
diff changeset
687 catch (Exception $e) {
015d06b10d37 initial
dwinter
parents:
diff changeset
688 $transaction->rollback();
015d06b10d37 initial
dwinter
parents:
diff changeset
689 // Re-throw the exception so we are aware of the failure.
015d06b10d37 initial
dwinter
parents:
diff changeset
690 throw $e;
015d06b10d37 initial
dwinter
parents:
diff changeset
691 }
015d06b10d37 initial
dwinter
parents:
diff changeset
692 }
015d06b10d37 initial
dwinter
parents:
diff changeset
693
015d06b10d37 initial
dwinter
parents:
diff changeset
694 // This really should be in core, but it isn't yet. When it gets added to core,
015d06b10d37 initial
dwinter
parents:
diff changeset
695 // we can remove this version.
015d06b10d37 initial
dwinter
parents:
diff changeset
696 // @see http://drupal.org/node/969180
015d06b10d37 initial
dwinter
parents:
diff changeset
697 if (!function_exists('entity_bundle_label')) {
015d06b10d37 initial
dwinter
parents:
diff changeset
698
015d06b10d37 initial
dwinter
parents:
diff changeset
699 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
700 * Returns the label of a bundle.
015d06b10d37 initial
dwinter
parents:
diff changeset
701 *
015d06b10d37 initial
dwinter
parents:
diff changeset
702 * @param string $entity_type
015d06b10d37 initial
dwinter
parents:
diff changeset
703 * The entity type; e.g. 'node' or 'user'.
015d06b10d37 initial
dwinter
parents:
diff changeset
704 * @param string $bundle_name
015d06b10d37 initial
dwinter
parents:
diff changeset
705 * The bundle for which we want the label from
015d06b10d37 initial
dwinter
parents:
diff changeset
706 *
015d06b10d37 initial
dwinter
parents:
diff changeset
707 * @return
015d06b10d37 initial
dwinter
parents:
diff changeset
708 * A string with the human-readable name of the bundle, or FALSE if not specified.
015d06b10d37 initial
dwinter
parents:
diff changeset
709 */
015d06b10d37 initial
dwinter
parents:
diff changeset
710 function entity_bundle_label($entity_type, $bundle_name) {
015d06b10d37 initial
dwinter
parents:
diff changeset
711 $labels = &drupal_static(__FUNCTION__, array());
015d06b10d37 initial
dwinter
parents:
diff changeset
712
015d06b10d37 initial
dwinter
parents:
diff changeset
713 if (empty($labels)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
714 foreach (entity_get_info() as $type => $info) {
015d06b10d37 initial
dwinter
parents:
diff changeset
715 foreach ($info['bundles'] as $bundle => $bundle_info) {
015d06b10d37 initial
dwinter
parents:
diff changeset
716 $labels[$type][$bundle] = !empty($bundle_info['label']) ? $bundle_info['label'] : FALSE;
015d06b10d37 initial
dwinter
parents:
diff changeset
717 }
015d06b10d37 initial
dwinter
parents:
diff changeset
718 }
015d06b10d37 initial
dwinter
parents:
diff changeset
719 }
015d06b10d37 initial
dwinter
parents:
diff changeset
720
015d06b10d37 initial
dwinter
parents:
diff changeset
721 return $labels[$entity_type][$bundle_name];
015d06b10d37 initial
dwinter
parents:
diff changeset
722 }
015d06b10d37 initial
dwinter
parents:
diff changeset
723
015d06b10d37 initial
dwinter
parents:
diff changeset
724 }
015d06b10d37 initial
dwinter
parents:
diff changeset
725
015d06b10d37 initial
dwinter
parents:
diff changeset
726 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
727 * Builds the node-specific information for a Solr document.
015d06b10d37 initial
dwinter
parents:
diff changeset
728 *
015d06b10d37 initial
dwinter
parents:
diff changeset
729 * @param ApacheSolrDocument $document
015d06b10d37 initial
dwinter
parents:
diff changeset
730 * The Solr document we are building up.
015d06b10d37 initial
dwinter
parents:
diff changeset
731 * @param object $node
015d06b10d37 initial
dwinter
parents:
diff changeset
732 * The entity we are indexing.
015d06b10d37 initial
dwinter
parents:
diff changeset
733 * @param string $entity_type
015d06b10d37 initial
dwinter
parents:
diff changeset
734 * The type of entity we're dealing with.
015d06b10d37 initial
dwinter
parents:
diff changeset
735 * @param string $env_id
015d06b10d37 initial
dwinter
parents:
diff changeset
736 * The type of entity we're dealing with.
015d06b10d37 initial
dwinter
parents:
diff changeset
737 *
015d06b10d37 initial
dwinter
parents:
diff changeset
738 * @return array A set of ApacheSolrDocument documents
015d06b10d37 initial
dwinter
parents:
diff changeset
739 */
015d06b10d37 initial
dwinter
parents:
diff changeset
740 function apachesolr_index_node_solr_document(ApacheSolrDocument $document, $node, $entity_type, $env_id) {
015d06b10d37 initial
dwinter
parents:
diff changeset
741 // None of these get added unless they are explicitly in our schema.xml
015d06b10d37 initial
dwinter
parents:
diff changeset
742 $document->label = apachesolr_clean_text($node->title);
015d06b10d37 initial
dwinter
parents:
diff changeset
743
015d06b10d37 initial
dwinter
parents:
diff changeset
744 // Build the node body.
015d06b10d37 initial
dwinter
parents:
diff changeset
745 $build = node_view($node, 'search_index', !empty($node->language) ? $node->language : LANGUAGE_NONE);
015d06b10d37 initial
dwinter
parents:
diff changeset
746 // Remove useless html crap out of the render.
015d06b10d37 initial
dwinter
parents:
diff changeset
747 unset($build['#theme']);
015d06b10d37 initial
dwinter
parents:
diff changeset
748 $text = drupal_render($build);
015d06b10d37 initial
dwinter
parents:
diff changeset
749 $document->content = apachesolr_clean_text($text);
015d06b10d37 initial
dwinter
parents:
diff changeset
750
015d06b10d37 initial
dwinter
parents:
diff changeset
751 // Adding the teaser
015d06b10d37 initial
dwinter
parents:
diff changeset
752 if (isset($node->teaser)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
753 $document->teaser = apachesolr_clean_text($node->teaser);
015d06b10d37 initial
dwinter
parents:
diff changeset
754 }
015d06b10d37 initial
dwinter
parents:
diff changeset
755 else {
015d06b10d37 initial
dwinter
parents:
diff changeset
756 $document->teaser = truncate_utf8($document->content, 300, TRUE);
015d06b10d37 initial
dwinter
parents:
diff changeset
757 }
015d06b10d37 initial
dwinter
parents:
diff changeset
758
015d06b10d37 initial
dwinter
parents:
diff changeset
759 // Path aliases can have important information about the content.
015d06b10d37 initial
dwinter
parents:
diff changeset
760 // Add them to the index as well.
015d06b10d37 initial
dwinter
parents:
diff changeset
761 if (function_exists('drupal_get_path_alias')) {
015d06b10d37 initial
dwinter
parents:
diff changeset
762 // Add any path alias to the index, looking first for language specific
015d06b10d37 initial
dwinter
parents:
diff changeset
763 // aliases but using language neutral aliases otherwise.
015d06b10d37 initial
dwinter
parents:
diff changeset
764 $language = empty($node->language) ? NULL : $node->language;
015d06b10d37 initial
dwinter
parents:
diff changeset
765 $path = 'node/' . $node->nid;
015d06b10d37 initial
dwinter
parents:
diff changeset
766 $output = drupal_get_path_alias($path, $language);
015d06b10d37 initial
dwinter
parents:
diff changeset
767 if ($output && $output != $path) {
015d06b10d37 initial
dwinter
parents:
diff changeset
768 $document->path_alias = $output;
015d06b10d37 initial
dwinter
parents:
diff changeset
769 }
015d06b10d37 initial
dwinter
parents:
diff changeset
770 }
015d06b10d37 initial
dwinter
parents:
diff changeset
771
015d06b10d37 initial
dwinter
parents:
diff changeset
772 // Author information
015d06b10d37 initial
dwinter
parents:
diff changeset
773 $document->ss_name = $node->name;
015d06b10d37 initial
dwinter
parents:
diff changeset
774 // We want the name to be searchable for keywords.
015d06b10d37 initial
dwinter
parents:
diff changeset
775 $document->tos_name = $node->name;
015d06b10d37 initial
dwinter
parents:
diff changeset
776
015d06b10d37 initial
dwinter
parents:
diff changeset
777 // Index formatted username so it can be searched and sorted on.
015d06b10d37 initial
dwinter
parents:
diff changeset
778 $account = (object) array('uid' => $node->uid, 'name' => $node->name);
015d06b10d37 initial
dwinter
parents:
diff changeset
779 $username = format_username($account);
015d06b10d37 initial
dwinter
parents:
diff changeset
780 $document->ss_name_formatted = $username;
015d06b10d37 initial
dwinter
parents:
diff changeset
781 $document->tos_name_formatted = $username;
015d06b10d37 initial
dwinter
parents:
diff changeset
782 $document->is_uid = $node->uid;
015d06b10d37 initial
dwinter
parents:
diff changeset
783 $document->bs_status = $node->status;
015d06b10d37 initial
dwinter
parents:
diff changeset
784 $document->bs_sticky = $node->sticky;
015d06b10d37 initial
dwinter
parents:
diff changeset
785 $document->bs_promote = $node->promote;
015d06b10d37 initial
dwinter
parents:
diff changeset
786 $document->is_tnid = $node->tnid;
015d06b10d37 initial
dwinter
parents:
diff changeset
787 $document->bs_translate = $node->translate;
015d06b10d37 initial
dwinter
parents:
diff changeset
788
015d06b10d37 initial
dwinter
parents:
diff changeset
789 // Language specific checks
015d06b10d37 initial
dwinter
parents:
diff changeset
790 if (empty($node->language)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
791 // 'und' is the language-neutral code in Drupal 7.
015d06b10d37 initial
dwinter
parents:
diff changeset
792 $document->ss_language = LANGUAGE_NONE;
015d06b10d37 initial
dwinter
parents:
diff changeset
793 }
015d06b10d37 initial
dwinter
parents:
diff changeset
794 else {
015d06b10d37 initial
dwinter
parents:
diff changeset
795 $document->ss_language = $node->language;
015d06b10d37 initial
dwinter
parents:
diff changeset
796 }
015d06b10d37 initial
dwinter
parents:
diff changeset
797
015d06b10d37 initial
dwinter
parents:
diff changeset
798 // Timestamp of the node
015d06b10d37 initial
dwinter
parents:
diff changeset
799 $document->ds_created = apachesolr_date_iso($node->created);
015d06b10d37 initial
dwinter
parents:
diff changeset
800 $document->ds_changed = apachesolr_date_iso($node->changed);
015d06b10d37 initial
dwinter
parents:
diff changeset
801
015d06b10d37 initial
dwinter
parents:
diff changeset
802 // Comment counts + time
015d06b10d37 initial
dwinter
parents:
diff changeset
803 if (isset($node->last_comment_timestamp) && !empty($node->comment_count)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
804 $document->ds_last_comment_timestamp = apachesolr_date_iso($node->last_comment_timestamp);
015d06b10d37 initial
dwinter
parents:
diff changeset
805 $document->ds_last_comment_or_change = apachesolr_date_iso(max($node->last_comment_timestamp, $node->changed));
015d06b10d37 initial
dwinter
parents:
diff changeset
806 $document->is_comment_count = $node->comment_count;
015d06b10d37 initial
dwinter
parents:
diff changeset
807 }
015d06b10d37 initial
dwinter
parents:
diff changeset
808 else {
015d06b10d37 initial
dwinter
parents:
diff changeset
809 $document->ds_last_comment_or_change = apachesolr_date_iso($node->changed);
015d06b10d37 initial
dwinter
parents:
diff changeset
810 }
015d06b10d37 initial
dwinter
parents:
diff changeset
811
015d06b10d37 initial
dwinter
parents:
diff changeset
812 // Fetch extra data normally not visible, including comments.
015d06b10d37 initial
dwinter
parents:
diff changeset
813 // We do this manually (with module_implements instead of node_invoke_nodeapi)
015d06b10d37 initial
dwinter
parents:
diff changeset
814 // because we want a keyed array to come back. Only in this way can we decide
015d06b10d37 initial
dwinter
parents:
diff changeset
815 // whether to index comments or not.
015d06b10d37 initial
dwinter
parents:
diff changeset
816 $extra = array();
015d06b10d37 initial
dwinter
parents:
diff changeset
817 $excludes = variable_get('apachesolr_exclude_nodeapi_types', array());
015d06b10d37 initial
dwinter
parents:
diff changeset
818 $exclude_nodeapi = isset($excludes[$node->type]) ? $excludes[$node->type] : array();
015d06b10d37 initial
dwinter
parents:
diff changeset
819
015d06b10d37 initial
dwinter
parents:
diff changeset
820 foreach (module_implements('node_update_index') as $module) {
015d06b10d37 initial
dwinter
parents:
diff changeset
821 // Invoke nodeapi if this module has not been excluded, for example,
015d06b10d37 initial
dwinter
parents:
diff changeset
822 // exclude 'comment' for a type to skip indexing its comments.
015d06b10d37 initial
dwinter
parents:
diff changeset
823 if (empty($exclude_nodeapi[$module])) {
015d06b10d37 initial
dwinter
parents:
diff changeset
824 $function = $module . '_node_update_index';
015d06b10d37 initial
dwinter
parents:
diff changeset
825 if ($output = $function($node)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
826 $extra[$module] = $output;
015d06b10d37 initial
dwinter
parents:
diff changeset
827 }
015d06b10d37 initial
dwinter
parents:
diff changeset
828 }
015d06b10d37 initial
dwinter
parents:
diff changeset
829 }
015d06b10d37 initial
dwinter
parents:
diff changeset
830
015d06b10d37 initial
dwinter
parents:
diff changeset
831 // Adding the text of the comments
015d06b10d37 initial
dwinter
parents:
diff changeset
832 if (isset($extra['comment'])) {
015d06b10d37 initial
dwinter
parents:
diff changeset
833 $comments = $extra['comment'];
015d06b10d37 initial
dwinter
parents:
diff changeset
834 // Remove comments from the extra fields
015d06b10d37 initial
dwinter
parents:
diff changeset
835 unset($extra['comment']);
015d06b10d37 initial
dwinter
parents:
diff changeset
836 $document->ts_comments = apachesolr_clean_text($comments);
015d06b10d37 initial
dwinter
parents:
diff changeset
837 // @todo: do we want to reproduce apachesolr_add_tags_to_document() for comments?
015d06b10d37 initial
dwinter
parents:
diff changeset
838 }
015d06b10d37 initial
dwinter
parents:
diff changeset
839 // If there are other extra fields, add them to the document
015d06b10d37 initial
dwinter
parents:
diff changeset
840 if (!empty($extra)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
841 // Use an omit-norms text field since this is generally going to be short; not
015d06b10d37 initial
dwinter
parents:
diff changeset
842 // really a full-text field.
015d06b10d37 initial
dwinter
parents:
diff changeset
843 $document->tos_content_extra = apachesolr_clean_text(implode(' ', $extra));
015d06b10d37 initial
dwinter
parents:
diff changeset
844 }
015d06b10d37 initial
dwinter
parents:
diff changeset
845
015d06b10d37 initial
dwinter
parents:
diff changeset
846 // Generic use case for future reference. Callbacks can
015d06b10d37 initial
dwinter
parents:
diff changeset
847 // allow you to send back multiple documents
015d06b10d37 initial
dwinter
parents:
diff changeset
848 $documents = array();
015d06b10d37 initial
dwinter
parents:
diff changeset
849 $documents[] = $document;
015d06b10d37 initial
dwinter
parents:
diff changeset
850 return $documents;
015d06b10d37 initial
dwinter
parents:
diff changeset
851 }
015d06b10d37 initial
dwinter
parents:
diff changeset
852
015d06b10d37 initial
dwinter
parents:
diff changeset
853 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
854 * Function that will be executed if the node bundles were updated.
015d06b10d37 initial
dwinter
parents:
diff changeset
855 * Currently it does nothing, but it could potentially do something later on.
015d06b10d37 initial
dwinter
parents:
diff changeset
856 *
015d06b10d37 initial
dwinter
parents:
diff changeset
857 * @param $env_id
015d06b10d37 initial
dwinter
parents:
diff changeset
858 * @param $existing_bundles
015d06b10d37 initial
dwinter
parents:
diff changeset
859 * @param $new_bundles
015d06b10d37 initial
dwinter
parents:
diff changeset
860 */
015d06b10d37 initial
dwinter
parents:
diff changeset
861 function apachesolr_index_node_bundles_changed($env_id, $existing_bundles, $new_bundles) {
015d06b10d37 initial
dwinter
parents:
diff changeset
862 // Nothing to do for now.
015d06b10d37 initial
dwinter
parents:
diff changeset
863 }
015d06b10d37 initial
dwinter
parents:
diff changeset
864
015d06b10d37 initial
dwinter
parents:
diff changeset
865 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
866 * Reindexing callback for ApacheSolr, for nodes.
015d06b10d37 initial
dwinter
parents:
diff changeset
867 *
015d06b10d37 initial
dwinter
parents:
diff changeset
868 * @param string $env_id
015d06b10d37 initial
dwinter
parents:
diff changeset
869 * The machine name of the environment.
015d06b10d37 initial
dwinter
parents:
diff changeset
870 * @param string|null $bundle
015d06b10d37 initial
dwinter
parents:
diff changeset
871 * (optional) The bundle type to reindex. If not used
015d06b10d37 initial
dwinter
parents:
diff changeset
872 * all bundles will be re-indexed.
015d06b10d37 initial
dwinter
parents:
diff changeset
873 *
015d06b10d37 initial
dwinter
parents:
diff changeset
874 * @return null
015d06b10d37 initial
dwinter
parents:
diff changeset
875 * returns NULL if the specified bundle is not in the indexable bundles list
015d06b10d37 initial
dwinter
parents:
diff changeset
876 *
015d06b10d37 initial
dwinter
parents:
diff changeset
877 * @throws Exception
015d06b10d37 initial
dwinter
parents:
diff changeset
878 */
015d06b10d37 initial
dwinter
parents:
diff changeset
879 function apachesolr_index_node_solr_reindex($env_id, $bundle = NULL) {
015d06b10d37 initial
dwinter
parents:
diff changeset
880 $indexer_table = apachesolr_get_indexer_table('node');
015d06b10d37 initial
dwinter
parents:
diff changeset
881 $transaction = db_transaction();
015d06b10d37 initial
dwinter
parents:
diff changeset
882 try {
015d06b10d37 initial
dwinter
parents:
diff changeset
883 $indexable_bundles = apachesolr_get_index_bundles($env_id, 'node');
015d06b10d37 initial
dwinter
parents:
diff changeset
884
015d06b10d37 initial
dwinter
parents:
diff changeset
885 if ($bundle && !empty($indexable_bundles) && !in_array($bundle, $indexable_bundles)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
886 // The bundle specified is not in the indexable bundles list.
015d06b10d37 initial
dwinter
parents:
diff changeset
887 return NULL;
015d06b10d37 initial
dwinter
parents:
diff changeset
888 }
015d06b10d37 initial
dwinter
parents:
diff changeset
889
015d06b10d37 initial
dwinter
parents:
diff changeset
890 // Leave status 0 rows - those need to be
015d06b10d37 initial
dwinter
parents:
diff changeset
891 // removed from the index later.
015d06b10d37 initial
dwinter
parents:
diff changeset
892 $delete = db_delete($indexer_table);
015d06b10d37 initial
dwinter
parents:
diff changeset
893 $delete->condition('status', 1);
015d06b10d37 initial
dwinter
parents:
diff changeset
894
015d06b10d37 initial
dwinter
parents:
diff changeset
895 if (!empty($bundle)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
896 $delete->condition('bundle', $bundle);
015d06b10d37 initial
dwinter
parents:
diff changeset
897 }
015d06b10d37 initial
dwinter
parents:
diff changeset
898 elseif (!empty($indexable_bundles)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
899 $delete->condition('bundle', $indexable_bundles, 'IN');
015d06b10d37 initial
dwinter
parents:
diff changeset
900 }
015d06b10d37 initial
dwinter
parents:
diff changeset
901
015d06b10d37 initial
dwinter
parents:
diff changeset
902 $delete->execute();
015d06b10d37 initial
dwinter
parents:
diff changeset
903
015d06b10d37 initial
dwinter
parents:
diff changeset
904 $select = db_select('node', 'n');
015d06b10d37 initial
dwinter
parents:
diff changeset
905 $select->condition('status', 1);
015d06b10d37 initial
dwinter
parents:
diff changeset
906 $select->addExpression("'node'", 'entity_type');
015d06b10d37 initial
dwinter
parents:
diff changeset
907 $select->addField('n', 'nid', 'entity_id');
015d06b10d37 initial
dwinter
parents:
diff changeset
908 $select->addField('n', 'type', 'bundle');
015d06b10d37 initial
dwinter
parents:
diff changeset
909 $select->addField('n', 'status', 'status');
015d06b10d37 initial
dwinter
parents:
diff changeset
910 $select->addExpression(REQUEST_TIME, 'changed');
015d06b10d37 initial
dwinter
parents:
diff changeset
911
015d06b10d37 initial
dwinter
parents:
diff changeset
912 if ($bundle) {
015d06b10d37 initial
dwinter
parents:
diff changeset
913 // Mark all nodes of the specified content type for reindexing.
015d06b10d37 initial
dwinter
parents:
diff changeset
914 $select->condition('n.type', $bundle);
015d06b10d37 initial
dwinter
parents:
diff changeset
915 }
015d06b10d37 initial
dwinter
parents:
diff changeset
916 elseif (!empty($indexable_bundles)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
917 // Restrict reindex to content types in the indexable bundles list.
015d06b10d37 initial
dwinter
parents:
diff changeset
918 $select->condition('n.type', $indexable_bundles, 'IN');
015d06b10d37 initial
dwinter
parents:
diff changeset
919 }
015d06b10d37 initial
dwinter
parents:
diff changeset
920
015d06b10d37 initial
dwinter
parents:
diff changeset
921 $insert = db_insert($indexer_table)
015d06b10d37 initial
dwinter
parents:
diff changeset
922 ->fields(array('entity_id', 'bundle', 'status', 'entity_type', 'changed'))
015d06b10d37 initial
dwinter
parents:
diff changeset
923 ->from($select)
015d06b10d37 initial
dwinter
parents:
diff changeset
924 ->execute();
015d06b10d37 initial
dwinter
parents:
diff changeset
925 }
015d06b10d37 initial
dwinter
parents:
diff changeset
926 catch (Exception $e) {
015d06b10d37 initial
dwinter
parents:
diff changeset
927 $transaction->rollback();
015d06b10d37 initial
dwinter
parents:
diff changeset
928 throw $e;
015d06b10d37 initial
dwinter
parents:
diff changeset
929 }
015d06b10d37 initial
dwinter
parents:
diff changeset
930 }
015d06b10d37 initial
dwinter
parents:
diff changeset
931
015d06b10d37 initial
dwinter
parents:
diff changeset
932 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
933 * Status callback for ApacheSolr, for nodes.
015d06b10d37 initial
dwinter
parents:
diff changeset
934 * after indexing a certain amount of nodes
015d06b10d37 initial
dwinter
parents:
diff changeset
935 *
015d06b10d37 initial
dwinter
parents:
diff changeset
936 * @param $entity_id
015d06b10d37 initial
dwinter
parents:
diff changeset
937 * @param $entity_type
015d06b10d37 initial
dwinter
parents:
diff changeset
938 *
015d06b10d37 initial
dwinter
parents:
diff changeset
939 * @return int
015d06b10d37 initial
dwinter
parents:
diff changeset
940 * The status of the node
015d06b10d37 initial
dwinter
parents:
diff changeset
941 */
015d06b10d37 initial
dwinter
parents:
diff changeset
942 function apachesolr_index_node_status_callback($entity_id, $entity_type) {
015d06b10d37 initial
dwinter
parents:
diff changeset
943 // Make sure we have a boolean value.
015d06b10d37 initial
dwinter
parents:
diff changeset
944 // Anything different from 1 becomes zero
015d06b10d37 initial
dwinter
parents:
diff changeset
945 $entity = entity_load($entity_type, array($entity_id));
015d06b10d37 initial
dwinter
parents:
diff changeset
946 $entity = $entity ? reset($entity) : FALSE;
015d06b10d37 initial
dwinter
parents:
diff changeset
947
015d06b10d37 initial
dwinter
parents:
diff changeset
948 if (empty($entity)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
949 // If the object failed to load, just stop.
015d06b10d37 initial
dwinter
parents:
diff changeset
950 return FALSE;
015d06b10d37 initial
dwinter
parents:
diff changeset
951 }
015d06b10d37 initial
dwinter
parents:
diff changeset
952 $status = ($entity->status == 1 ? 1 : 0);
015d06b10d37 initial
dwinter
parents:
diff changeset
953 return $status;
015d06b10d37 initial
dwinter
parents:
diff changeset
954 }
015d06b10d37 initial
dwinter
parents:
diff changeset
955
015d06b10d37 initial
dwinter
parents:
diff changeset
956 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
957 * Callback that converts term_reference field into an array
015d06b10d37 initial
dwinter
parents:
diff changeset
958 *
015d06b10d37 initial
dwinter
parents:
diff changeset
959 * @param object $node
015d06b10d37 initial
dwinter
parents:
diff changeset
960 * @param string $field_name
015d06b10d37 initial
dwinter
parents:
diff changeset
961 * @param string $index_key
015d06b10d37 initial
dwinter
parents:
diff changeset
962 * @param array $field_info
015d06b10d37 initial
dwinter
parents:
diff changeset
963 * @return array $fields
015d06b10d37 initial
dwinter
parents:
diff changeset
964 * fields that will be indexed for this term reference
015d06b10d37 initial
dwinter
parents:
diff changeset
965 */
015d06b10d37 initial
dwinter
parents:
diff changeset
966 function apachesolr_term_reference_indexing_callback($node, $field_name, $index_key, array $field_info) {
015d06b10d37 initial
dwinter
parents:
diff changeset
967 // Keep ancestors cached
015d06b10d37 initial
dwinter
parents:
diff changeset
968 $ancestors = &drupal_static(__FUNCTION__, array());
015d06b10d37 initial
dwinter
parents:
diff changeset
969
015d06b10d37 initial
dwinter
parents:
diff changeset
970 $fields = array();
015d06b10d37 initial
dwinter
parents:
diff changeset
971 $vocab_names = array();
015d06b10d37 initial
dwinter
parents:
diff changeset
972 if (!empty($node->{$field_name}) && function_exists('taxonomy_get_parents_all')) {
015d06b10d37 initial
dwinter
parents:
diff changeset
973 $field = $node->$field_name;
015d06b10d37 initial
dwinter
parents:
diff changeset
974 list($lang, $items) = each($field);
015d06b10d37 initial
dwinter
parents:
diff changeset
975 foreach ($items as $item) {
015d06b10d37 initial
dwinter
parents:
diff changeset
976 // Triple indexing of tids lets us do efficient searches (on tid)
015d06b10d37 initial
dwinter
parents:
diff changeset
977 // and do accurate per field or per-vocabulary faceting.
015d06b10d37 initial
dwinter
parents:
diff changeset
978
015d06b10d37 initial
dwinter
parents:
diff changeset
979 // By including the ancestors to a term in the index we make
015d06b10d37 initial
dwinter
parents:
diff changeset
980 // sure that searches for general categories match specific
015d06b10d37 initial
dwinter
parents:
diff changeset
981 // categories, e.g. Fruit -> apple, a search for fruit will find
015d06b10d37 initial
dwinter
parents:
diff changeset
982 // content categorized with apple.
015d06b10d37 initial
dwinter
parents:
diff changeset
983 if (!isset($ancestors[$item['tid']])) {
015d06b10d37 initial
dwinter
parents:
diff changeset
984 $ancestors[$item['tid']] = taxonomy_get_parents_all($item['tid']);
015d06b10d37 initial
dwinter
parents:
diff changeset
985 }
015d06b10d37 initial
dwinter
parents:
diff changeset
986 foreach ($ancestors[$item['tid']] as $ancestor) {
015d06b10d37 initial
dwinter
parents:
diff changeset
987 // Index parent term against the field. Note that this happens
015d06b10d37 initial
dwinter
parents:
diff changeset
988 // regardless of whether the facet is set to show as a hierarchy or not.
015d06b10d37 initial
dwinter
parents:
diff changeset
989 // We would need a separate field if we were to index terms without any
015d06b10d37 initial
dwinter
parents:
diff changeset
990 // hierarchy at all.
015d06b10d37 initial
dwinter
parents:
diff changeset
991 // If the term is singular, then we cannot add another value to the
015d06b10d37 initial
dwinter
parents:
diff changeset
992 // document as the field is single
015d06b10d37 initial
dwinter
parents:
diff changeset
993 if ($field_info['multiple'] == true) {
015d06b10d37 initial
dwinter
parents:
diff changeset
994 $fields[] = array(
015d06b10d37 initial
dwinter
parents:
diff changeset
995 'key' => $index_key,
015d06b10d37 initial
dwinter
parents:
diff changeset
996 'value' => $ancestor->tid,
015d06b10d37 initial
dwinter
parents:
diff changeset
997 );
015d06b10d37 initial
dwinter
parents:
diff changeset
998 }
015d06b10d37 initial
dwinter
parents:
diff changeset
999 $fields[] = array(
015d06b10d37 initial
dwinter
parents:
diff changeset
1000 'key' => 'tid',
015d06b10d37 initial
dwinter
parents:
diff changeset
1001 'value' => $ancestor->tid,
015d06b10d37 initial
dwinter
parents:
diff changeset
1002 );
015d06b10d37 initial
dwinter
parents:
diff changeset
1003 $fields[] = array(
015d06b10d37 initial
dwinter
parents:
diff changeset
1004 'key' => 'im_vid_' . $ancestor->vid,
015d06b10d37 initial
dwinter
parents:
diff changeset
1005 'value' => $ancestor->tid,
015d06b10d37 initial
dwinter
parents:
diff changeset
1006 );
015d06b10d37 initial
dwinter
parents:
diff changeset
1007 $name = apachesolr_clean_text($ancestor->name);
015d06b10d37 initial
dwinter
parents:
diff changeset
1008 $vocab_names[$ancestor->vid][] = $name;
015d06b10d37 initial
dwinter
parents:
diff changeset
1009 // We index each name as a string for cross-site faceting
015d06b10d37 initial
dwinter
parents:
diff changeset
1010 // using the vocab name rather than vid in field construction .
015d06b10d37 initial
dwinter
parents:
diff changeset
1011 $fields[] = array(
015d06b10d37 initial
dwinter
parents:
diff changeset
1012 'key' => 'sm_vid_' . apachesolr_vocab_name($ancestor->vid),
015d06b10d37 initial
dwinter
parents:
diff changeset
1013 'value' => $name,
015d06b10d37 initial
dwinter
parents:
diff changeset
1014 );
015d06b10d37 initial
dwinter
parents:
diff changeset
1015 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1016 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1017 // Index the term names into a text field for MLT queries and keyword searching.
015d06b10d37 initial
dwinter
parents:
diff changeset
1018 foreach ($vocab_names as $vid => $names) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1019 $fields[] = array(
015d06b10d37 initial
dwinter
parents:
diff changeset
1020 'key' => 'tm_vid_' . $vid . '_names',
015d06b10d37 initial
dwinter
parents:
diff changeset
1021 'value' => implode(' ', $names),
015d06b10d37 initial
dwinter
parents:
diff changeset
1022 );
015d06b10d37 initial
dwinter
parents:
diff changeset
1023 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1024 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1025 return $fields;
015d06b10d37 initial
dwinter
parents:
diff changeset
1026 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1027
015d06b10d37 initial
dwinter
parents:
diff changeset
1028 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
1029 * Helper function - return a safe (PHP identifier) vocabulary name.
015d06b10d37 initial
dwinter
parents:
diff changeset
1030 *
015d06b10d37 initial
dwinter
parents:
diff changeset
1031 * @param integer $vid
015d06b10d37 initial
dwinter
parents:
diff changeset
1032 * @return string
015d06b10d37 initial
dwinter
parents:
diff changeset
1033 */
015d06b10d37 initial
dwinter
parents:
diff changeset
1034 function apachesolr_vocab_name($vid) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1035 $names = &drupal_static(__FUNCTION__, array());
015d06b10d37 initial
dwinter
parents:
diff changeset
1036
015d06b10d37 initial
dwinter
parents:
diff changeset
1037 if (!isset($names[$vid])) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1038 $vocab_name = db_query('SELECT v.name FROM {taxonomy_vocabulary} v WHERE v.vid = :vid', array(':vid' => $vid))->fetchField();
015d06b10d37 initial
dwinter
parents:
diff changeset
1039 $names[$vid] = preg_replace('/[^a-zA-Z0-9_\x7f-\xff]/', '_', $vocab_name);
015d06b10d37 initial
dwinter
parents:
diff changeset
1040 // Fallback for names ending up all as '_'.
015d06b10d37 initial
dwinter
parents:
diff changeset
1041 $check = rtrim($names[$vid], '_');
015d06b10d37 initial
dwinter
parents:
diff changeset
1042 if (!$check) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1043 $names[$vid] = '_' . $vid . '_';
015d06b10d37 initial
dwinter
parents:
diff changeset
1044 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1045 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1046 return $names[$vid];
015d06b10d37 initial
dwinter
parents:
diff changeset
1047 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1048
015d06b10d37 initial
dwinter
parents:
diff changeset
1049 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
1050 * Callback that converts list module field into an array
015d06b10d37 initial
dwinter
parents:
diff changeset
1051 * For every multivalued value we also add a single value to be able to
015d06b10d37 initial
dwinter
parents:
diff changeset
1052 * use the stats
015d06b10d37 initial
dwinter
parents:
diff changeset
1053 *
015d06b10d37 initial
dwinter
parents:
diff changeset
1054 * @param object $entity
015d06b10d37 initial
dwinter
parents:
diff changeset
1055 * @param string $field_name
015d06b10d37 initial
dwinter
parents:
diff changeset
1056 * @param string $index_key
015d06b10d37 initial
dwinter
parents:
diff changeset
1057 * @param array $field_info
015d06b10d37 initial
dwinter
parents:
diff changeset
1058 * @return array $fields
015d06b10d37 initial
dwinter
parents:
diff changeset
1059 */
015d06b10d37 initial
dwinter
parents:
diff changeset
1060 function apachesolr_fields_default_indexing_callback($entity, $field_name, $index_key, array $field_info) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1061 $fields = array();
015d06b10d37 initial
dwinter
parents:
diff changeset
1062 $numeric = TRUE;
015d06b10d37 initial
dwinter
parents:
diff changeset
1063 if (!empty($entity->{$field_name})) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1064 $field = $entity->$field_name;
015d06b10d37 initial
dwinter
parents:
diff changeset
1065 list($lang, $values) = each($field);
015d06b10d37 initial
dwinter
parents:
diff changeset
1066 switch ($field_info['index_type']) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1067 case 'integer':
015d06b10d37 initial
dwinter
parents:
diff changeset
1068 case 'half-int':
015d06b10d37 initial
dwinter
parents:
diff changeset
1069 case 'sint':
015d06b10d37 initial
dwinter
parents:
diff changeset
1070 case 'tint':
015d06b10d37 initial
dwinter
parents:
diff changeset
1071 case 'thalf-int':
015d06b10d37 initial
dwinter
parents:
diff changeset
1072 case 'boolean':
015d06b10d37 initial
dwinter
parents:
diff changeset
1073 $function = 'intval';
015d06b10d37 initial
dwinter
parents:
diff changeset
1074 break;
015d06b10d37 initial
dwinter
parents:
diff changeset
1075 case 'float':
015d06b10d37 initial
dwinter
parents:
diff changeset
1076 case 'double':
015d06b10d37 initial
dwinter
parents:
diff changeset
1077 case 'sfloat':
015d06b10d37 initial
dwinter
parents:
diff changeset
1078 case 'sdouble':
015d06b10d37 initial
dwinter
parents:
diff changeset
1079 case 'tfloat':
015d06b10d37 initial
dwinter
parents:
diff changeset
1080 case 'tdouble':
015d06b10d37 initial
dwinter
parents:
diff changeset
1081 $function = 'apachesolr_floatval';
015d06b10d37 initial
dwinter
parents:
diff changeset
1082 break;
015d06b10d37 initial
dwinter
parents:
diff changeset
1083 default:
015d06b10d37 initial
dwinter
parents:
diff changeset
1084 $numeric = FALSE;
015d06b10d37 initial
dwinter
parents:
diff changeset
1085 $function = 'apachesolr_clean_text';
015d06b10d37 initial
dwinter
parents:
diff changeset
1086 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1087 for ($i = 0; $i < count($values); $i++) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1088 $fields[] = array(
015d06b10d37 initial
dwinter
parents:
diff changeset
1089 'key' => $index_key,
015d06b10d37 initial
dwinter
parents:
diff changeset
1090 'value' => $function($values[$i]['value']),
015d06b10d37 initial
dwinter
parents:
diff changeset
1091 );
015d06b10d37 initial
dwinter
parents:
diff changeset
1092 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1093 // Also store the first value of the field in a singular index for multi value fields
015d06b10d37 initial
dwinter
parents:
diff changeset
1094 if ($field_info['multiple'] && $numeric && !empty($values[0])) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1095 $singular_field_info = $field_info;
015d06b10d37 initial
dwinter
parents:
diff changeset
1096 $singular_field_info['multiple'] = FALSE;
015d06b10d37 initial
dwinter
parents:
diff changeset
1097 $single_key = apachesolr_index_key($singular_field_info);
015d06b10d37 initial
dwinter
parents:
diff changeset
1098 $fields[] = array(
015d06b10d37 initial
dwinter
parents:
diff changeset
1099 'key' => $single_key,
015d06b10d37 initial
dwinter
parents:
diff changeset
1100 'value' => $function($values[0]['value']),
015d06b10d37 initial
dwinter
parents:
diff changeset
1101 );
015d06b10d37 initial
dwinter
parents:
diff changeset
1102 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1103 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1104 return $fields;
015d06b10d37 initial
dwinter
parents:
diff changeset
1105 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1106
015d06b10d37 initial
dwinter
parents:
diff changeset
1107 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
1108 * This function is used during indexing to normalize the DATE and DATETIME
015d06b10d37 initial
dwinter
parents:
diff changeset
1109 * fields into the appropriate format for Apache Solr.
015d06b10d37 initial
dwinter
parents:
diff changeset
1110 *
015d06b10d37 initial
dwinter
parents:
diff changeset
1111 * @param object $entity
015d06b10d37 initial
dwinter
parents:
diff changeset
1112 * @param string $field_name
015d06b10d37 initial
dwinter
parents:
diff changeset
1113 * @param string $index_key
015d06b10d37 initial
dwinter
parents:
diff changeset
1114 * @param array $field_info
015d06b10d37 initial
dwinter
parents:
diff changeset
1115 * @return array $fields
015d06b10d37 initial
dwinter
parents:
diff changeset
1116 */
015d06b10d37 initial
dwinter
parents:
diff changeset
1117 function apachesolr_date_default_indexing_callback($entity, $field_name, $index_key, array $field_info) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1118 $fields = array();
015d06b10d37 initial
dwinter
parents:
diff changeset
1119 if (!empty($entity->{$field_name})) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1120 $field = $entity->$field_name;
015d06b10d37 initial
dwinter
parents:
diff changeset
1121 list($lang, $values) = each($field);
015d06b10d37 initial
dwinter
parents:
diff changeset
1122 // Construct a Solr-ready date string in UTC time zone based on the field's date string and time zone.
015d06b10d37 initial
dwinter
parents:
diff changeset
1123 $tz = new DateTimeZone(isset($field['timezone']) ? $field['timezone'] : 'UTC');
015d06b10d37 initial
dwinter
parents:
diff changeset
1124
015d06b10d37 initial
dwinter
parents:
diff changeset
1125 // $fields may end up having two values; one for the start date
015d06b10d37 initial
dwinter
parents:
diff changeset
1126 // and one for the end date.
015d06b10d37 initial
dwinter
parents:
diff changeset
1127 foreach ($values as $value) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1128 if ($date = date_create($value['value'], $tz)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1129 $index_value = apachesolr_date_iso($date->format('U'));
015d06b10d37 initial
dwinter
parents:
diff changeset
1130 $fields[] = array(
015d06b10d37 initial
dwinter
parents:
diff changeset
1131 'key' => $index_key,
015d06b10d37 initial
dwinter
parents:
diff changeset
1132 'value' => $index_value,
015d06b10d37 initial
dwinter
parents:
diff changeset
1133 );
015d06b10d37 initial
dwinter
parents:
diff changeset
1134 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1135
015d06b10d37 initial
dwinter
parents:
diff changeset
1136 if (isset($value['value2'])) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1137 if ($date = date_create($value['value2'], $tz)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1138 $index_value = apachesolr_date_iso($date->format('U'));
015d06b10d37 initial
dwinter
parents:
diff changeset
1139 $fields[] = array(
015d06b10d37 initial
dwinter
parents:
diff changeset
1140 // The value2 element is the end date. Therefore it gets indexed
015d06b10d37 initial
dwinter
parents:
diff changeset
1141 // into its own Solr field.
015d06b10d37 initial
dwinter
parents:
diff changeset
1142 'key' => $index_key . '_end',
015d06b10d37 initial
dwinter
parents:
diff changeset
1143 'value' => $index_value,
015d06b10d37 initial
dwinter
parents:
diff changeset
1144 );
015d06b10d37 initial
dwinter
parents:
diff changeset
1145 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1146 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1147 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1148 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1149 return $fields;
015d06b10d37 initial
dwinter
parents:
diff changeset
1150 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1151
015d06b10d37 initial
dwinter
parents:
diff changeset
1152 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
1153 * This function is used during indexing to normalize the DATESTAMP fields
015d06b10d37 initial
dwinter
parents:
diff changeset
1154 * into the appropriate format for Apache Solr.
015d06b10d37 initial
dwinter
parents:
diff changeset
1155 *
015d06b10d37 initial
dwinter
parents:
diff changeset
1156 * @param object $entity
015d06b10d37 initial
dwinter
parents:
diff changeset
1157 * @param string $field_name
015d06b10d37 initial
dwinter
parents:
diff changeset
1158 * @param string $index_key
015d06b10d37 initial
dwinter
parents:
diff changeset
1159 * @param array $field_info
015d06b10d37 initial
dwinter
parents:
diff changeset
1160 * @return array $fields
015d06b10d37 initial
dwinter
parents:
diff changeset
1161 */
015d06b10d37 initial
dwinter
parents:
diff changeset
1162 function apachesolr_datestamp_default_indexing_callback($entity, $field_name, $index_key, array $field_info) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1163 $fields = array();
015d06b10d37 initial
dwinter
parents:
diff changeset
1164 if (!empty($entity->{$field_name})) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1165 // $fields may end up having two values; one for the start date
015d06b10d37 initial
dwinter
parents:
diff changeset
1166 // and one for the end date.
015d06b10d37 initial
dwinter
parents:
diff changeset
1167 $field = $entity->$field_name;
015d06b10d37 initial
dwinter
parents:
diff changeset
1168 list($lang, $values) = each($field);
015d06b10d37 initial
dwinter
parents:
diff changeset
1169
015d06b10d37 initial
dwinter
parents:
diff changeset
1170 foreach ($values as $value) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1171 if (isset($value['value']) && $value['value'] != 0) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1172 $index_value = apachesolr_date_iso($value['value']);
015d06b10d37 initial
dwinter
parents:
diff changeset
1173 $fields[] = array(
015d06b10d37 initial
dwinter
parents:
diff changeset
1174 'key' => $index_key,
015d06b10d37 initial
dwinter
parents:
diff changeset
1175 'value' => $index_value,
015d06b10d37 initial
dwinter
parents:
diff changeset
1176 );
015d06b10d37 initial
dwinter
parents:
diff changeset
1177 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1178 if (isset($value['value2']) && $value['value'] != 0) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1179 $index_value = apachesolr_date_iso($value['value2']);
015d06b10d37 initial
dwinter
parents:
diff changeset
1180 $fields[] = array(
015d06b10d37 initial
dwinter
parents:
diff changeset
1181 // The value2 element is the end date. Therefore it gets indexed
015d06b10d37 initial
dwinter
parents:
diff changeset
1182 // into its own Solr field.
015d06b10d37 initial
dwinter
parents:
diff changeset
1183 'key' => $index_key . '_end',
015d06b10d37 initial
dwinter
parents:
diff changeset
1184 'value' => $index_value,
015d06b10d37 initial
dwinter
parents:
diff changeset
1185 );
015d06b10d37 initial
dwinter
parents:
diff changeset
1186 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1187 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1188 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1189 return $fields;
015d06b10d37 initial
dwinter
parents:
diff changeset
1190 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1191
015d06b10d37 initial
dwinter
parents:
diff changeset
1192 function apachesolr_floatval($value) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1193 return sprintf('%0.20f', $value);
015d06b10d37 initial
dwinter
parents:
diff changeset
1194 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1195
015d06b10d37 initial
dwinter
parents:
diff changeset
1196 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
1197 * Indexing callback for the node_reference module
015d06b10d37 initial
dwinter
parents:
diff changeset
1198 * by the references module
015d06b10d37 initial
dwinter
parents:
diff changeset
1199 *
015d06b10d37 initial
dwinter
parents:
diff changeset
1200 * @param object $entity
015d06b10d37 initial
dwinter
parents:
diff changeset
1201 * @param string $field_name
015d06b10d37 initial
dwinter
parents:
diff changeset
1202 * @param string $index_key
015d06b10d37 initial
dwinter
parents:
diff changeset
1203 * @param array $field_info
015d06b10d37 initial
dwinter
parents:
diff changeset
1204 * @return array $fields
015d06b10d37 initial
dwinter
parents:
diff changeset
1205 */
015d06b10d37 initial
dwinter
parents:
diff changeset
1206 function apachesolr_nodereference_indexing_callback($entity, $field_name, $index_key, array $field_info) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1207 $fields = array();
015d06b10d37 initial
dwinter
parents:
diff changeset
1208 if (!empty($entity->{$field_name})) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1209 $index_key = apachesolr_index_key($field_info);
015d06b10d37 initial
dwinter
parents:
diff changeset
1210 foreach ($entity->$field_name as $field_references) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1211 foreach ($field_references as $reference) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1212 if ($index_value = (!empty($reference['nid'])) ? $reference['nid'] : FALSE) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1213 $fields[] = array(
015d06b10d37 initial
dwinter
parents:
diff changeset
1214 'key' => $index_key,
015d06b10d37 initial
dwinter
parents:
diff changeset
1215 'value' => $index_value,
015d06b10d37 initial
dwinter
parents:
diff changeset
1216 );
015d06b10d37 initial
dwinter
parents:
diff changeset
1217 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1218 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1219 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1220 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1221 return $fields;
015d06b10d37 initial
dwinter
parents:
diff changeset
1222 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1223
015d06b10d37 initial
dwinter
parents:
diff changeset
1224 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
1225 * Indexing callback for the user_reference module
015d06b10d37 initial
dwinter
parents:
diff changeset
1226 * by the references module
015d06b10d37 initial
dwinter
parents:
diff changeset
1227 *
015d06b10d37 initial
dwinter
parents:
diff changeset
1228 * @param object $entity
015d06b10d37 initial
dwinter
parents:
diff changeset
1229 * @param string $field_name
015d06b10d37 initial
dwinter
parents:
diff changeset
1230 * @param string $index_key
015d06b10d37 initial
dwinter
parents:
diff changeset
1231 * @param array $field_info
015d06b10d37 initial
dwinter
parents:
diff changeset
1232 * @return array $fields
015d06b10d37 initial
dwinter
parents:
diff changeset
1233 */
015d06b10d37 initial
dwinter
parents:
diff changeset
1234 function apachesolr_userreference_indexing_callback($entity, $field_name, $index_key, array $field_info) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1235 $fields = array();
015d06b10d37 initial
dwinter
parents:
diff changeset
1236 if (!empty($entity->$field_name)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1237 $index_key = apachesolr_index_key($field_info);
015d06b10d37 initial
dwinter
parents:
diff changeset
1238 foreach ($entity->$field_name as $field_references) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1239 foreach ($field_references as $reference) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1240 if ($index_value = (isset($reference['uid']) && strlen($reference['uid'])) ? $reference['uid'] : FALSE) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1241 $fields[] = array(
015d06b10d37 initial
dwinter
parents:
diff changeset
1242 'key' => $index_key,
015d06b10d37 initial
dwinter
parents:
diff changeset
1243 'value' => $index_value,
015d06b10d37 initial
dwinter
parents:
diff changeset
1244 );
015d06b10d37 initial
dwinter
parents:
diff changeset
1245 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1246 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1247 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1248 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1249 return $fields;
015d06b10d37 initial
dwinter
parents:
diff changeset
1250 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1251
015d06b10d37 initial
dwinter
parents:
diff changeset
1252 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
1253 * Indexing callback for entityreference fields.
015d06b10d37 initial
dwinter
parents:
diff changeset
1254 *
015d06b10d37 initial
dwinter
parents:
diff changeset
1255 * @param object $entity
015d06b10d37 initial
dwinter
parents:
diff changeset
1256 * @param string $field_name
015d06b10d37 initial
dwinter
parents:
diff changeset
1257 * @param string $index_key
015d06b10d37 initial
dwinter
parents:
diff changeset
1258 * @param array $field_info
015d06b10d37 initial
dwinter
parents:
diff changeset
1259 * @return array $fields
015d06b10d37 initial
dwinter
parents:
diff changeset
1260 *
015d06b10d37 initial
dwinter
parents:
diff changeset
1261 */
015d06b10d37 initial
dwinter
parents:
diff changeset
1262 function apachesolr_entityreference_indexing_callback($entity, $field_name, $index_key, $field_info) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1263 $fields = array();
015d06b10d37 initial
dwinter
parents:
diff changeset
1264 if (!empty($entity->{$field_name})) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1265
015d06b10d37 initial
dwinter
parents:
diff changeset
1266 // Gets entity type and index key. We need to prefix the ID with the entity
015d06b10d37 initial
dwinter
parents:
diff changeset
1267 // type so we know what entity we are dealing with in the mapping callback.
015d06b10d37 initial
dwinter
parents:
diff changeset
1268 $entity_type = $field_info['field']['settings']['target_type'];
015d06b10d37 initial
dwinter
parents:
diff changeset
1269 $index_key = apachesolr_index_key($field_info);
015d06b10d37 initial
dwinter
parents:
diff changeset
1270
015d06b10d37 initial
dwinter
parents:
diff changeset
1271 // Iterates over all references and adds them to the fields.
015d06b10d37 initial
dwinter
parents:
diff changeset
1272 foreach ($entity->$field_name as $entity_references) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1273 foreach ($entity_references as $reference) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1274 if ($id = (!empty($reference['target_id'])) ? $reference['target_id'] : FALSE) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1275 $fields[] = array(
015d06b10d37 initial
dwinter
parents:
diff changeset
1276 'key' => $index_key,
015d06b10d37 initial
dwinter
parents:
diff changeset
1277 'value' => $entity_type . ':' . $id,
015d06b10d37 initial
dwinter
parents:
diff changeset
1278 );
015d06b10d37 initial
dwinter
parents:
diff changeset
1279 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1280 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1281 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1282 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1283 return $fields;
015d06b10d37 initial
dwinter
parents:
diff changeset
1284 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1285
015d06b10d37 initial
dwinter
parents:
diff changeset
1286 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
1287 * Extract HTML tag contents from $text and add to boost fields.
015d06b10d37 initial
dwinter
parents:
diff changeset
1288 *
015d06b10d37 initial
dwinter
parents:
diff changeset
1289 * $text must be stripped of control characters before hand.
015d06b10d37 initial
dwinter
parents:
diff changeset
1290 *
015d06b10d37 initial
dwinter
parents:
diff changeset
1291 * @param ApacheSolrDocument $document
015d06b10d37 initial
dwinter
parents:
diff changeset
1292 * @param type $text
015d06b10d37 initial
dwinter
parents:
diff changeset
1293 */
015d06b10d37 initial
dwinter
parents:
diff changeset
1294 function apachesolr_add_tags_to_document(ApacheSolrDocument $document, $text) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1295 $tags_to_index = variable_get('apachesolr_tags_to_index', array(
015d06b10d37 initial
dwinter
parents:
diff changeset
1296 'h1' => 'tags_h1',
015d06b10d37 initial
dwinter
parents:
diff changeset
1297 'h2' => 'tags_h2_h3',
015d06b10d37 initial
dwinter
parents:
diff changeset
1298 'h3' => 'tags_h2_h3',
015d06b10d37 initial
dwinter
parents:
diff changeset
1299 'h4' => 'tags_h4_h5_h6',
015d06b10d37 initial
dwinter
parents:
diff changeset
1300 'h5' => 'tags_h4_h5_h6',
015d06b10d37 initial
dwinter
parents:
diff changeset
1301 'h6' => 'tags_h4_h5_h6',
015d06b10d37 initial
dwinter
parents:
diff changeset
1302 'u' => 'tags_inline',
015d06b10d37 initial
dwinter
parents:
diff changeset
1303 'b' => 'tags_inline',
015d06b10d37 initial
dwinter
parents:
diff changeset
1304 'i' => 'tags_inline',
015d06b10d37 initial
dwinter
parents:
diff changeset
1305 'strong' => 'tags_inline',
015d06b10d37 initial
dwinter
parents:
diff changeset
1306 'em' => 'tags_inline',
015d06b10d37 initial
dwinter
parents:
diff changeset
1307 'a' => 'tags_a'
015d06b10d37 initial
dwinter
parents:
diff changeset
1308 ));
015d06b10d37 initial
dwinter
parents:
diff changeset
1309
015d06b10d37 initial
dwinter
parents:
diff changeset
1310 // Strip off all ignored tags.
015d06b10d37 initial
dwinter
parents:
diff changeset
1311 $text = strip_tags($text, '<' . implode('><', array_keys($tags_to_index)) . '>');
015d06b10d37 initial
dwinter
parents:
diff changeset
1312
015d06b10d37 initial
dwinter
parents:
diff changeset
1313 preg_match_all('@<(' . implode('|', array_keys($tags_to_index)) . ')[^>]*>(.*)</\1>@Ui', $text, $matches);
015d06b10d37 initial
dwinter
parents:
diff changeset
1314 foreach ($matches[1] as $key => $tag) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1315 $tag = strtolower($tag);
015d06b10d37 initial
dwinter
parents:
diff changeset
1316 // We don't want to index links auto-generated by the url filter.
015d06b10d37 initial
dwinter
parents:
diff changeset
1317 if ($tag != 'a' || !preg_match('@(?:http://|https://|ftp://|mailto:|smb://|afp://|file://|gopher://|news://|ssl://|sslv2://|sslv3://|tls://|tcp://|udp://|www\.)[a-zA-Z0-9]+@', $matches[2][$key])) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1318 if (!isset($document->{$tags_to_index[$tag]})) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1319 $document->{$tags_to_index[$tag]} = '';
015d06b10d37 initial
dwinter
parents:
diff changeset
1320 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1321 $document->{$tags_to_index[$tag]} .= ' ' . apachesolr_clean_text($matches[2][$key]);
015d06b10d37 initial
dwinter
parents:
diff changeset
1322 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1323 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1324 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1325
015d06b10d37 initial
dwinter
parents:
diff changeset
1326 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
1327 * hook_cron() helper to try to make the index table consistent with their
015d06b10d37 initial
dwinter
parents:
diff changeset
1328 * respective entity table.
015d06b10d37 initial
dwinter
parents:
diff changeset
1329 */
015d06b10d37 initial
dwinter
parents:
diff changeset
1330 function apachesolr_index_node_check_table() {
015d06b10d37 initial
dwinter
parents:
diff changeset
1331 // Check for unpublished content that wasn't deleted from the index.
015d06b10d37 initial
dwinter
parents:
diff changeset
1332 $table = apachesolr_get_indexer_table('node');
015d06b10d37 initial
dwinter
parents:
diff changeset
1333 // We do not check more nodes than double the cron limit per time
015d06b10d37 initial
dwinter
parents:
diff changeset
1334 // Update or delete at most this many in each Solr query.
015d06b10d37 initial
dwinter
parents:
diff changeset
1335 $limit = variable_get('apachesolr_cron_mass_limit', 500);
015d06b10d37 initial
dwinter
parents:
diff changeset
1336 $query = db_select($table, 'aien')
015d06b10d37 initial
dwinter
parents:
diff changeset
1337 ->fields('n', array('nid', 'status'))
015d06b10d37 initial
dwinter
parents:
diff changeset
1338 ->where('aien.status <> n.status')
015d06b10d37 initial
dwinter
parents:
diff changeset
1339 ->range(0, ($limit * 2))
015d06b10d37 initial
dwinter
parents:
diff changeset
1340 ->addTag('apachesolr_index_node');
015d06b10d37 initial
dwinter
parents:
diff changeset
1341 $query->innerJoin('node', 'n', 'n.nid = aien.entity_id');
015d06b10d37 initial
dwinter
parents:
diff changeset
1342 $nodes = $query->execute()->fetchAllAssoc('nid');
015d06b10d37 initial
dwinter
parents:
diff changeset
1343
015d06b10d37 initial
dwinter
parents:
diff changeset
1344 $node_lists = array_chunk($nodes, $limit, TRUE);
015d06b10d37 initial
dwinter
parents:
diff changeset
1345 foreach ($node_lists as $nodes) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1346 watchdog('Apache Solr', 'On cron running apachesolr_nodeapi_mass_update() on nids @nids', array('@nids' => implode(',', array_keys($nodes))), WATCHDOG_NOTICE);
015d06b10d37 initial
dwinter
parents:
diff changeset
1347 if (!apachesolr_index_nodeapi_mass_update($nodes, $table)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1348 // Solr query failed - so stop trying.
015d06b10d37 initial
dwinter
parents:
diff changeset
1349 break;
015d06b10d37 initial
dwinter
parents:
diff changeset
1350 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1351 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1352
015d06b10d37 initial
dwinter
parents:
diff changeset
1353 // Check for deleted content that wasn't deleted from the index.
015d06b10d37 initial
dwinter
parents:
diff changeset
1354 $query = db_select($table, 'aien')
015d06b10d37 initial
dwinter
parents:
diff changeset
1355 ->isNull('n.nid')
015d06b10d37 initial
dwinter
parents:
diff changeset
1356 ->range(0, ($limit*2));
015d06b10d37 initial
dwinter
parents:
diff changeset
1357 $query->addExpression('aien.entity_id', 'nid');
015d06b10d37 initial
dwinter
parents:
diff changeset
1358 $query->leftJoin('node', 'n', 'n.nid = aien.entity_id');
015d06b10d37 initial
dwinter
parents:
diff changeset
1359 $nodes = $query->execute()->fetchAllAssoc('nid');
015d06b10d37 initial
dwinter
parents:
diff changeset
1360 $node_lists = array_chunk($nodes, $limit, TRUE);
015d06b10d37 initial
dwinter
parents:
diff changeset
1361
015d06b10d37 initial
dwinter
parents:
diff changeset
1362 foreach ($node_lists as $nodes) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1363 watchdog('Apache Solr', 'On cron running apachesolr_nodeapi_mass_delete() on nids @nids', array('@nids' => implode(',', array_keys($nodes))), WATCHDOG_NOTICE);
015d06b10d37 initial
dwinter
parents:
diff changeset
1364 if (!apachesolr_index_nodeapi_mass_delete($nodes, $table)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1365 // Solr query failed - so stop trying.
015d06b10d37 initial
dwinter
parents:
diff changeset
1366 break;
015d06b10d37 initial
dwinter
parents:
diff changeset
1367 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1368 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1369 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1370
015d06b10d37 initial
dwinter
parents:
diff changeset
1371 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
1372 * Mass Update nodes from the solr indexer table
015d06b10d37 initial
dwinter
parents:
diff changeset
1373 *
015d06b10d37 initial
dwinter
parents:
diff changeset
1374 * @param array $nodes
015d06b10d37 initial
dwinter
parents:
diff changeset
1375 * @param string $table
015d06b10d37 initial
dwinter
parents:
diff changeset
1376 * @return boolean
015d06b10d37 initial
dwinter
parents:
diff changeset
1377 * true if we mass updated, false if failed
015d06b10d37 initial
dwinter
parents:
diff changeset
1378 */
015d06b10d37 initial
dwinter
parents:
diff changeset
1379 function apachesolr_index_nodeapi_mass_update(array $nodes, $table = NULL) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1380 if (empty($nodes)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1381 return TRUE;
015d06b10d37 initial
dwinter
parents:
diff changeset
1382 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1383 if (empty($table)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1384 $table = apachesolr_get_indexer_table('node');
015d06b10d37 initial
dwinter
parents:
diff changeset
1385 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1386
015d06b10d37 initial
dwinter
parents:
diff changeset
1387 if (apachesolr_environment_variable_get(apachesolr_default_environment(), 'apachesolr_read_only', APACHESOLR_READ_WRITE) == APACHESOLR_READ_ONLY) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1388 return TRUE;
015d06b10d37 initial
dwinter
parents:
diff changeset
1389 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1390
015d06b10d37 initial
dwinter
parents:
diff changeset
1391 $published_ids = array();
015d06b10d37 initial
dwinter
parents:
diff changeset
1392 $unpublished_ids = array();
015d06b10d37 initial
dwinter
parents:
diff changeset
1393 foreach ($nodes as $node) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1394 if ($node->status) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1395 $published_ids[$node->nid] = apachesolr_document_id($node->nid);
015d06b10d37 initial
dwinter
parents:
diff changeset
1396 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1397 else {
015d06b10d37 initial
dwinter
parents:
diff changeset
1398 $unpublished_ids[$node->nid] = apachesolr_document_id($node->nid);
015d06b10d37 initial
dwinter
parents:
diff changeset
1399 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1400 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1401 try {
015d06b10d37 initial
dwinter
parents:
diff changeset
1402 $env_id = apachesolr_default_environment();
015d06b10d37 initial
dwinter
parents:
diff changeset
1403 $solr = apachesolr_get_solr($env_id);
015d06b10d37 initial
dwinter
parents:
diff changeset
1404 $solr->deleteByMultipleIds($unpublished_ids);
015d06b10d37 initial
dwinter
parents:
diff changeset
1405 apachesolr_set_last_index_updated($env_id, REQUEST_TIME);
015d06b10d37 initial
dwinter
parents:
diff changeset
1406
015d06b10d37 initial
dwinter
parents:
diff changeset
1407 // There was no exception, so update the table.
015d06b10d37 initial
dwinter
parents:
diff changeset
1408 if ($published_ids) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1409 db_update($table)
015d06b10d37 initial
dwinter
parents:
diff changeset
1410 ->fields(array('changed' => REQUEST_TIME, 'status' => 1))
015d06b10d37 initial
dwinter
parents:
diff changeset
1411 ->condition('entity_id', array_keys($published_ids), 'IN')
015d06b10d37 initial
dwinter
parents:
diff changeset
1412 ->execute();
015d06b10d37 initial
dwinter
parents:
diff changeset
1413 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1414 if ($unpublished_ids) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1415 db_update($table)
015d06b10d37 initial
dwinter
parents:
diff changeset
1416 ->fields(array('changed' => REQUEST_TIME, 'status' => 0))
015d06b10d37 initial
dwinter
parents:
diff changeset
1417 ->condition('entity_id', array_keys($unpublished_ids), 'IN')
015d06b10d37 initial
dwinter
parents:
diff changeset
1418 ->execute();
015d06b10d37 initial
dwinter
parents:
diff changeset
1419 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1420 return TRUE;
015d06b10d37 initial
dwinter
parents:
diff changeset
1421 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1422 catch (Exception $e) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1423 watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR);
015d06b10d37 initial
dwinter
parents:
diff changeset
1424 return FALSE;
015d06b10d37 initial
dwinter
parents:
diff changeset
1425 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1426 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1427
015d06b10d37 initial
dwinter
parents:
diff changeset
1428 /**
015d06b10d37 initial
dwinter
parents:
diff changeset
1429 * Mass delete nodes from the solr indexer tables.
015d06b10d37 initial
dwinter
parents:
diff changeset
1430 *
015d06b10d37 initial
dwinter
parents:
diff changeset
1431 * @param array $nodes
015d06b10d37 initial
dwinter
parents:
diff changeset
1432 * @param string $table
015d06b10d37 initial
dwinter
parents:
diff changeset
1433 * @return boolean
015d06b10d37 initial
dwinter
parents:
diff changeset
1434 * true if we mass updated, false if failed
015d06b10d37 initial
dwinter
parents:
diff changeset
1435 */
015d06b10d37 initial
dwinter
parents:
diff changeset
1436 function apachesolr_index_nodeapi_mass_delete(array $nodes, $table = NULL) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1437 if (empty($nodes)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1438 return TRUE;
015d06b10d37 initial
dwinter
parents:
diff changeset
1439 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1440 if (empty($table)) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1441 $table = apachesolr_get_indexer_table('node');
015d06b10d37 initial
dwinter
parents:
diff changeset
1442 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1443
015d06b10d37 initial
dwinter
parents:
diff changeset
1444 if (apachesolr_environment_variable_get(apachesolr_default_environment(), 'apachesolr_read_only', APACHESOLR_READ_WRITE) == APACHESOLR_READ_ONLY) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1445 return TRUE;
015d06b10d37 initial
dwinter
parents:
diff changeset
1446 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1447
015d06b10d37 initial
dwinter
parents:
diff changeset
1448 $ids = array();
015d06b10d37 initial
dwinter
parents:
diff changeset
1449 $nids = array();
015d06b10d37 initial
dwinter
parents:
diff changeset
1450 foreach ($nodes as $node) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1451 $ids[] = apachesolr_document_id($node->nid);
015d06b10d37 initial
dwinter
parents:
diff changeset
1452 $nids[] = $node->nid;
015d06b10d37 initial
dwinter
parents:
diff changeset
1453 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1454 try {
015d06b10d37 initial
dwinter
parents:
diff changeset
1455 $env_id = apachesolr_default_environment();
015d06b10d37 initial
dwinter
parents:
diff changeset
1456 $solr = apachesolr_get_solr($env_id);
015d06b10d37 initial
dwinter
parents:
diff changeset
1457 $solr->deleteByMultipleIds($ids);
015d06b10d37 initial
dwinter
parents:
diff changeset
1458 apachesolr_set_last_index_updated($env_id, REQUEST_TIME);
015d06b10d37 initial
dwinter
parents:
diff changeset
1459 // There was no exception, so update the table.
015d06b10d37 initial
dwinter
parents:
diff changeset
1460 db_delete($table)
015d06b10d37 initial
dwinter
parents:
diff changeset
1461 ->condition('entity_id', $nids, 'IN')
015d06b10d37 initial
dwinter
parents:
diff changeset
1462 ->execute();
015d06b10d37 initial
dwinter
parents:
diff changeset
1463 return TRUE;
015d06b10d37 initial
dwinter
parents:
diff changeset
1464 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1465 catch (Exception $e) {
015d06b10d37 initial
dwinter
parents:
diff changeset
1466 watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR);
015d06b10d37 initial
dwinter
parents:
diff changeset
1467 return FALSE;
015d06b10d37 initial
dwinter
parents:
diff changeset
1468 }
015d06b10d37 initial
dwinter
parents:
diff changeset
1469 }