1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * Enables site-wide keyword searching. |
---|
6 | */ |
---|
7 | |
---|
8 | /** |
---|
9 | * Matches Unicode character classes to exclude from the search index. |
---|
10 | * |
---|
11 | * See: http://www.unicode.org/Public/UNIDATA/UCD.html#General_Category_Values |
---|
12 | * |
---|
13 | * The index only contains the following character classes: |
---|
14 | * Lu Letter, Uppercase |
---|
15 | * Ll Letter, Lowercase |
---|
16 | * Lt Letter, Titlecase |
---|
17 | * Lo Letter, Other |
---|
18 | * Nd Number, Decimal Digit |
---|
19 | * No Number, Other |
---|
20 | */ |
---|
21 | define('PREG_CLASS_SEARCH_EXCLUDE', |
---|
22 | '\x{0}-\x{2f}\x{3a}-\x{40}\x{5b}-\x{60}\x{7b}-\x{bf}\x{d7}\x{f7}\x{2b0}-'. |
---|
23 | '\x{385}\x{387}\x{3f6}\x{482}-\x{489}\x{559}-\x{55f}\x{589}-\x{5c7}\x{5f3}-'. |
---|
24 | '\x{61f}\x{640}\x{64b}-\x{65e}\x{66a}-\x{66d}\x{670}\x{6d4}\x{6d6}-\x{6ed}'. |
---|
25 | '\x{6fd}\x{6fe}\x{700}-\x{70f}\x{711}\x{730}-\x{74a}\x{7a6}-\x{7b0}\x{901}-'. |
---|
26 | '\x{903}\x{93c}\x{93e}-\x{94d}\x{951}-\x{954}\x{962}-\x{965}\x{970}\x{981}-'. |
---|
27 | '\x{983}\x{9bc}\x{9be}-\x{9cd}\x{9d7}\x{9e2}\x{9e3}\x{9f2}-\x{a03}\x{a3c}-'. |
---|
28 | '\x{a4d}\x{a70}\x{a71}\x{a81}-\x{a83}\x{abc}\x{abe}-\x{acd}\x{ae2}\x{ae3}'. |
---|
29 | '\x{af1}-\x{b03}\x{b3c}\x{b3e}-\x{b57}\x{b70}\x{b82}\x{bbe}-\x{bd7}\x{bf0}-'. |
---|
30 | '\x{c03}\x{c3e}-\x{c56}\x{c82}\x{c83}\x{cbc}\x{cbe}-\x{cd6}\x{d02}\x{d03}'. |
---|
31 | '\x{d3e}-\x{d57}\x{d82}\x{d83}\x{dca}-\x{df4}\x{e31}\x{e34}-\x{e3f}\x{e46}-'. |
---|
32 | '\x{e4f}\x{e5a}\x{e5b}\x{eb1}\x{eb4}-\x{ebc}\x{ec6}-\x{ecd}\x{f01}-\x{f1f}'. |
---|
33 | '\x{f2a}-\x{f3f}\x{f71}-\x{f87}\x{f90}-\x{fd1}\x{102c}-\x{1039}\x{104a}-'. |
---|
34 | '\x{104f}\x{1056}-\x{1059}\x{10fb}\x{10fc}\x{135f}-\x{137c}\x{1390}-\x{1399}'. |
---|
35 | '\x{166d}\x{166e}\x{1680}\x{169b}\x{169c}\x{16eb}-\x{16f0}\x{1712}-\x{1714}'. |
---|
36 | '\x{1732}-\x{1736}\x{1752}\x{1753}\x{1772}\x{1773}\x{17b4}-\x{17db}\x{17dd}'. |
---|
37 | '\x{17f0}-\x{180e}\x{1843}\x{18a9}\x{1920}-\x{1945}\x{19b0}-\x{19c0}\x{19c8}'. |
---|
38 | '\x{19c9}\x{19de}-\x{19ff}\x{1a17}-\x{1a1f}\x{1d2c}-\x{1d61}\x{1d78}\x{1d9b}-'. |
---|
39 | '\x{1dc3}\x{1fbd}\x{1fbf}-\x{1fc1}\x{1fcd}-\x{1fcf}\x{1fdd}-\x{1fdf}\x{1fed}-'. |
---|
40 | '\x{1fef}\x{1ffd}-\x{2070}\x{2074}-\x{207e}\x{2080}-\x{2101}\x{2103}-\x{2106}'. |
---|
41 | '\x{2108}\x{2109}\x{2114}\x{2116}-\x{2118}\x{211e}-\x{2123}\x{2125}\x{2127}'. |
---|
42 | '\x{2129}\x{212e}\x{2132}\x{213a}\x{213b}\x{2140}-\x{2144}\x{214a}-\x{2b13}'. |
---|
43 | '\x{2ce5}-\x{2cff}\x{2d6f}\x{2e00}-\x{3005}\x{3007}-\x{303b}\x{303d}-\x{303f}'. |
---|
44 | '\x{3099}-\x{309e}\x{30a0}\x{30fb}\x{30fd}\x{30fe}\x{3190}-\x{319f}\x{31c0}-'. |
---|
45 | '\x{31cf}\x{3200}-\x{33ff}\x{4dc0}-\x{4dff}\x{a015}\x{a490}-\x{a716}\x{a802}'. |
---|
46 | '\x{a806}\x{a80b}\x{a823}-\x{a82b}\x{e000}-\x{f8ff}\x{fb1e}\x{fb29}\x{fd3e}'. |
---|
47 | '\x{fd3f}\x{fdfc}-\x{fe6b}\x{feff}-\x{ff0f}\x{ff1a}-\x{ff20}\x{ff3b}-\x{ff40}'. |
---|
48 | '\x{ff5b}-\x{ff65}\x{ff70}\x{ff9e}\x{ff9f}\x{ffe0}-\x{fffd}'); |
---|
49 | |
---|
50 | /** |
---|
51 | * Matches all 'N' Unicode character classes (numbers) |
---|
52 | */ |
---|
53 | define('PREG_CLASS_NUMBERS', |
---|
54 | '\x{30}-\x{39}\x{b2}\x{b3}\x{b9}\x{bc}-\x{be}\x{660}-\x{669}\x{6f0}-\x{6f9}'. |
---|
55 | '\x{966}-\x{96f}\x{9e6}-\x{9ef}\x{9f4}-\x{9f9}\x{a66}-\x{a6f}\x{ae6}-\x{aef}'. |
---|
56 | '\x{b66}-\x{b6f}\x{be7}-\x{bf2}\x{c66}-\x{c6f}\x{ce6}-\x{cef}\x{d66}-\x{d6f}'. |
---|
57 | '\x{e50}-\x{e59}\x{ed0}-\x{ed9}\x{f20}-\x{f33}\x{1040}-\x{1049}\x{1369}-'. |
---|
58 | '\x{137c}\x{16ee}-\x{16f0}\x{17e0}-\x{17e9}\x{17f0}-\x{17f9}\x{1810}-\x{1819}'. |
---|
59 | '\x{1946}-\x{194f}\x{2070}\x{2074}-\x{2079}\x{2080}-\x{2089}\x{2153}-\x{2183}'. |
---|
60 | '\x{2460}-\x{249b}\x{24ea}-\x{24ff}\x{2776}-\x{2793}\x{3007}\x{3021}-\x{3029}'. |
---|
61 | '\x{3038}-\x{303a}\x{3192}-\x{3195}\x{3220}-\x{3229}\x{3251}-\x{325f}\x{3280}-'. |
---|
62 | '\x{3289}\x{32b1}-\x{32bf}\x{ff10}-\x{ff19}'); |
---|
63 | |
---|
64 | /** |
---|
65 | * Matches all 'P' Unicode character classes (punctuation) |
---|
66 | */ |
---|
67 | define('PREG_CLASS_PUNCTUATION', |
---|
68 | '\x{21}-\x{23}\x{25}-\x{2a}\x{2c}-\x{2f}\x{3a}\x{3b}\x{3f}\x{40}\x{5b}-\x{5d}'. |
---|
69 | '\x{5f}\x{7b}\x{7d}\x{a1}\x{ab}\x{b7}\x{bb}\x{bf}\x{37e}\x{387}\x{55a}-\x{55f}'. |
---|
70 | '\x{589}\x{58a}\x{5be}\x{5c0}\x{5c3}\x{5f3}\x{5f4}\x{60c}\x{60d}\x{61b}\x{61f}'. |
---|
71 | '\x{66a}-\x{66d}\x{6d4}\x{700}-\x{70d}\x{964}\x{965}\x{970}\x{df4}\x{e4f}'. |
---|
72 | '\x{e5a}\x{e5b}\x{f04}-\x{f12}\x{f3a}-\x{f3d}\x{f85}\x{104a}-\x{104f}\x{10fb}'. |
---|
73 | '\x{1361}-\x{1368}\x{166d}\x{166e}\x{169b}\x{169c}\x{16eb}-\x{16ed}\x{1735}'. |
---|
74 | '\x{1736}\x{17d4}-\x{17d6}\x{17d8}-\x{17da}\x{1800}-\x{180a}\x{1944}\x{1945}'. |
---|
75 | '\x{2010}-\x{2027}\x{2030}-\x{2043}\x{2045}-\x{2051}\x{2053}\x{2054}\x{2057}'. |
---|
76 | '\x{207d}\x{207e}\x{208d}\x{208e}\x{2329}\x{232a}\x{23b4}-\x{23b6}\x{2768}-'. |
---|
77 | '\x{2775}\x{27e6}-\x{27eb}\x{2983}-\x{2998}\x{29d8}-\x{29db}\x{29fc}\x{29fd}'. |
---|
78 | '\x{3001}-\x{3003}\x{3008}-\x{3011}\x{3014}-\x{301f}\x{3030}\x{303d}\x{30a0}'. |
---|
79 | '\x{30fb}\x{fd3e}\x{fd3f}\x{fe30}-\x{fe52}\x{fe54}-\x{fe61}\x{fe63}\x{fe68}'. |
---|
80 | '\x{fe6a}\x{fe6b}\x{ff01}-\x{ff03}\x{ff05}-\x{ff0a}\x{ff0c}-\x{ff0f}\x{ff1a}'. |
---|
81 | '\x{ff1b}\x{ff1f}\x{ff20}\x{ff3b}-\x{ff3d}\x{ff3f}\x{ff5b}\x{ff5d}\x{ff5f}-'. |
---|
82 | '\x{ff65}'); |
---|
83 | |
---|
84 | /** |
---|
85 | * Matches all CJK characters that are candidates for auto-splitting |
---|
86 | * (Chinese, Japanese, Korean). |
---|
87 | * Contains kana and BMP ideographs. |
---|
88 | */ |
---|
89 | define('PREG_CLASS_CJK', '\x{3041}-\x{30ff}\x{31f0}-\x{31ff}\x{3400}-\x{4db5}'. |
---|
90 | '\x{4e00}-\x{9fbb}\x{f900}-\x{fad9}'); |
---|
91 | |
---|
92 | /** |
---|
93 | * Implementation of hook_help(). |
---|
94 | */ |
---|
95 | function search_help($path, $arg) { |
---|
96 | switch ($path) { |
---|
97 | case 'admin/help#search': |
---|
98 | $output = '<p>'. t('The search module adds the ability to search for content by keywords. Search is often the only practical way to find content on a large site, and is useful for finding both users and posts.') .'</p>'; |
---|
99 | $output .= '<p>'. t('To provide keyword searching, the search engine maintains an index of words found in your site\'s content. To build and maintain this index, a correctly configured <a href="@cron">cron maintenance task</a> is required. Indexing behavior can be adjusted using the <a href="@searchsettings">search settings page</a>; for example, the <em>Number of items to index per cron run</em> sets the maximum number of items indexed in each pass of a <a href="@cron">cron maintenance task</a>. If necessary, reduce this number to prevent timeouts and memory errors when indexing.', array('@cron' => url('admin/reports/status'), '@searchsettings' => url('admin/settings/search'))) .'</p>'; |
---|
100 | $output .= '<p>'. t('For more information, see the online handbook entry for <a href="@search">Search module</a>.', array('@search' => 'http://drupal.org/handbook/modules/search/')) .'</p>'; |
---|
101 | return $output; |
---|
102 | case 'admin/settings/search': |
---|
103 | return '<p>'. t('The search engine maintains an index of words found in your site\'s content. To build and maintain this index, a correctly configured <a href="@cron">cron maintenance task</a> is required. Indexing behavior can be adjusted using the settings below.', array('@cron' => url('admin/reports/status'))) .'</p>'; |
---|
104 | case 'search#noresults': |
---|
105 | return t('<ul> |
---|
106 | <li>Check if your spelling is correct.</li> |
---|
107 | <li>Remove quotes around phrases to match each word individually: <em>"blue smurf"</em> will match less than <em>blue smurf</em>.</li> |
---|
108 | <li>Consider loosening your query with <em>OR</em>: <em>blue smurf</em> will match less than <em>blue OR smurf</em>.</li> |
---|
109 | </ul>'); |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * Implementation of hook_theme() |
---|
115 | */ |
---|
116 | function search_theme() { |
---|
117 | return array( |
---|
118 | 'search_theme_form' => array( |
---|
119 | 'arguments' => array('form' => NULL), |
---|
120 | 'template' => 'search-theme-form', |
---|
121 | ), |
---|
122 | 'search_block_form' => array( |
---|
123 | 'arguments' => array('form' => NULL), |
---|
124 | 'template' => 'search-block-form', |
---|
125 | ), |
---|
126 | 'search_result' => array( |
---|
127 | 'arguments' => array('result' => NULL, 'type' => NULL), |
---|
128 | 'file' => 'search.pages.inc', |
---|
129 | 'template' => 'search-result', |
---|
130 | ), |
---|
131 | 'search_results' => array( |
---|
132 | 'arguments' => array('results' => NULL, 'type' => NULL), |
---|
133 | 'file' => 'search.pages.inc', |
---|
134 | 'template' => 'search-results', |
---|
135 | ), |
---|
136 | ); |
---|
137 | } |
---|
138 | |
---|
139 | /** |
---|
140 | * Implementation of hook_perm(). |
---|
141 | */ |
---|
142 | function search_perm() { |
---|
143 | return array('search content', 'use advanced search', 'administer search'); |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | * Implementation of hook_block(). |
---|
148 | */ |
---|
149 | function search_block($op = 'list', $delta = 0) { |
---|
150 | if ($op == 'list') { |
---|
151 | $blocks[0]['info'] = t('Search form'); |
---|
152 | // Not worth caching. |
---|
153 | $blocks[0]['cache'] = BLOCK_NO_CACHE; |
---|
154 | return $blocks; |
---|
155 | } |
---|
156 | else if ($op == 'view' && user_access('search content')) { |
---|
157 | $block['content'] = drupal_get_form('search_block_form'); |
---|
158 | $block['subject'] = t('Search'); |
---|
159 | return $block; |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | /** |
---|
164 | * Implementation of hook_menu(). |
---|
165 | */ |
---|
166 | function search_menu() { |
---|
167 | $items['search'] = array( |
---|
168 | 'title' => 'Search', |
---|
169 | 'page callback' => 'search_view', |
---|
170 | 'access arguments' => array('search content'), |
---|
171 | 'type' => MENU_SUGGESTED_ITEM, |
---|
172 | 'file' => 'search.pages.inc', |
---|
173 | ); |
---|
174 | $items['admin/settings/search'] = array( |
---|
175 | 'title' => 'Search settings', |
---|
176 | 'description' => 'Configure relevance settings for search and other indexing options', |
---|
177 | 'page callback' => 'drupal_get_form', |
---|
178 | 'page arguments' => array('search_admin_settings'), |
---|
179 | 'access arguments' => array('administer search'), |
---|
180 | 'type' => MENU_NORMAL_ITEM, |
---|
181 | 'file' => 'search.admin.inc', |
---|
182 | ); |
---|
183 | $items['admin/settings/search/wipe'] = array( |
---|
184 | 'title' => 'Clear index', |
---|
185 | 'page callback' => 'drupal_get_form', |
---|
186 | 'page arguments' => array('search_wipe_confirm'), |
---|
187 | 'access arguments' => array('administer search'), |
---|
188 | 'type' => MENU_CALLBACK, |
---|
189 | 'file' => 'search.admin.inc', |
---|
190 | ); |
---|
191 | $items['admin/reports/search'] = array( |
---|
192 | 'title' => 'Top search phrases', |
---|
193 | 'description' => 'View most popular search phrases.', |
---|
194 | 'page callback' => 'dblog_top', |
---|
195 | 'page arguments' => array('search'), |
---|
196 | 'access arguments' => array('access site reports'), |
---|
197 | 'file' => 'dblog.admin.inc', |
---|
198 | 'file path' => drupal_get_path('module', 'dblog'), |
---|
199 | ); |
---|
200 | |
---|
201 | foreach (module_implements('search') as $name) { |
---|
202 | $items['search/'. $name .'/%menu_tail'] = array( |
---|
203 | 'title callback' => 'module_invoke', |
---|
204 | 'title arguments' => array($name, 'search', 'name', TRUE), |
---|
205 | 'page callback' => 'search_view', |
---|
206 | 'page arguments' => array($name), |
---|
207 | 'access callback' => '_search_menu', |
---|
208 | 'access arguments' => array($name), |
---|
209 | 'type' => MENU_LOCAL_TASK, |
---|
210 | 'parent' => 'search', |
---|
211 | 'file' => 'search.pages.inc', |
---|
212 | ); |
---|
213 | } |
---|
214 | return $items; |
---|
215 | } |
---|
216 | |
---|
217 | function _search_menu($name) { |
---|
218 | return user_access('search content') && module_invoke($name, 'search', 'name'); |
---|
219 | } |
---|
220 | |
---|
221 | /** |
---|
222 | * Wipes a part of or the entire search index. |
---|
223 | * |
---|
224 | * @param $sid |
---|
225 | * (optional) The SID of the item to wipe. If specified, $type must be passed |
---|
226 | * too. |
---|
227 | * @param $type |
---|
228 | * (optional) The type of item to wipe. |
---|
229 | */ |
---|
230 | function search_wipe($sid = NULL, $type = NULL, $reindex = FALSE) { |
---|
231 | if ($type == NULL && $sid == NULL) { |
---|
232 | module_invoke_all('search', 'reset'); |
---|
233 | } |
---|
234 | else { |
---|
235 | db_query("DELETE FROM {search_dataset} WHERE sid = %d AND type = '%s'", $sid, $type); |
---|
236 | db_query("DELETE FROM {search_index} WHERE sid = %d AND type = '%s'", $sid, $type); |
---|
237 | // Don't remove links if re-indexing. |
---|
238 | if (!$reindex) { |
---|
239 | db_query("DELETE FROM {search_node_links} WHERE sid = %d AND type = '%s'", $sid, $type); |
---|
240 | } |
---|
241 | } |
---|
242 | } |
---|
243 | |
---|
244 | /** |
---|
245 | * Marks a word as dirty (or retrieves the list of dirty words). This is used |
---|
246 | * during indexing (cron). Words which are dirty have outdated total counts in |
---|
247 | * the search_total table, and need to be recounted. |
---|
248 | */ |
---|
249 | function search_dirty($word = NULL) { |
---|
250 | static $dirty = array(); |
---|
251 | if ($word !== NULL) { |
---|
252 | $dirty[$word] = TRUE; |
---|
253 | } |
---|
254 | else { |
---|
255 | return $dirty; |
---|
256 | } |
---|
257 | } |
---|
258 | |
---|
259 | /** |
---|
260 | * Implementation of hook_cron(). |
---|
261 | * |
---|
262 | * Fires hook_update_index() in all modules and cleans up dirty words (see |
---|
263 | * search_dirty). |
---|
264 | */ |
---|
265 | function search_cron() { |
---|
266 | // We register a shutdown function to ensure that search_total is always up |
---|
267 | // to date. |
---|
268 | register_shutdown_function('search_update_totals'); |
---|
269 | |
---|
270 | // Update word index |
---|
271 | foreach (module_list() as $module) { |
---|
272 | module_invoke($module, 'update_index'); |
---|
273 | } |
---|
274 | } |
---|
275 | |
---|
276 | /** |
---|
277 | * This function is called on shutdown to ensure that search_total is always |
---|
278 | * up to date (even if cron times out or otherwise fails). |
---|
279 | */ |
---|
280 | function search_update_totals() { |
---|
281 | // Update word IDF (Inverse Document Frequency) counts for new/changed words |
---|
282 | foreach (search_dirty() as $word => $dummy) { |
---|
283 | // Get total count |
---|
284 | $total = db_result(db_query("SELECT SUM(score) FROM {search_index} WHERE word = '%s'", $word)); |
---|
285 | // Apply Zipf's law to equalize the probability distribution |
---|
286 | $total = log10(1 + 1/(max(1, $total))); |
---|
287 | db_query("UPDATE {search_total} SET count = %f WHERE word = '%s'", $total, $word); |
---|
288 | if (!db_affected_rows()) { |
---|
289 | @db_query("INSERT INTO {search_total} (word, count) VALUES ('%s', %f)", $word, $total); |
---|
290 | } |
---|
291 | } |
---|
292 | // Find words that were deleted from search_index, but are still in |
---|
293 | // search_total. We use a LEFT JOIN between the two tables and keep only the |
---|
294 | // rows which fail to join. |
---|
295 | $result = db_query("SELECT t.word AS realword, i.word FROM {search_total} t LEFT JOIN {search_index} i ON t.word = i.word WHERE i.word IS NULL"); |
---|
296 | while ($word = db_fetch_object($result)) { |
---|
297 | db_query("DELETE FROM {search_total} WHERE word = '%s'", $word->realword); |
---|
298 | } |
---|
299 | } |
---|
300 | |
---|
301 | /** |
---|
302 | * Simplifies a string according to indexing rules. |
---|
303 | */ |
---|
304 | function search_simplify($text) { |
---|
305 | // Decode entities to UTF-8 |
---|
306 | $text = decode_entities($text); |
---|
307 | |
---|
308 | // Lowercase |
---|
309 | $text = drupal_strtolower($text); |
---|
310 | |
---|
311 | // Call an external processor for word handling. |
---|
312 | search_invoke_preprocess($text); |
---|
313 | |
---|
314 | // Simple CJK handling |
---|
315 | if (variable_get('overlap_cjk', TRUE)) { |
---|
316 | $text = preg_replace_callback('/['. PREG_CLASS_CJK .']+/u', 'search_expand_cjk', $text); |
---|
317 | } |
---|
318 | |
---|
319 | // To improve searching for numerical data such as dates, IP addresses |
---|
320 | // or version numbers, we consider a group of numerical characters |
---|
321 | // separated only by punctuation characters to be one piece. |
---|
322 | // This also means that searching for e.g. '20/03/1984' also returns |
---|
323 | // results with '20-03-1984' in them. |
---|
324 | // Readable regexp: ([number]+)[punctuation]+(?=[number]) |
---|
325 | $text = preg_replace('/(['. PREG_CLASS_NUMBERS .']+)['. PREG_CLASS_PUNCTUATION .']+(?=['. PREG_CLASS_NUMBERS .'])/u', '\1', $text); |
---|
326 | |
---|
327 | // The dot, underscore and dash are simply removed. This allows meaningful |
---|
328 | // search behavior with acronyms and URLs. |
---|
329 | $text = preg_replace('/[._-]+/', '', $text); |
---|
330 | |
---|
331 | // With the exception of the rules above, we consider all punctuation, |
---|
332 | // marks, spacers, etc, to be a word boundary. |
---|
333 | $text = preg_replace('/['. PREG_CLASS_SEARCH_EXCLUDE .']+/u', ' ', $text); |
---|
334 | |
---|
335 | return $text; |
---|
336 | } |
---|
337 | |
---|
338 | /** |
---|
339 | * Basic CJK tokenizer. Simply splits a string into consecutive, overlapping |
---|
340 | * sequences of characters ('minimum_word_size' long). |
---|
341 | */ |
---|
342 | function search_expand_cjk($matches) { |
---|
343 | $min = variable_get('minimum_word_size', 3); |
---|
344 | $str = $matches[0]; |
---|
345 | $l = drupal_strlen($str); |
---|
346 | // Passthrough short words |
---|
347 | if ($l <= $min) { |
---|
348 | return ' '. $str .' '; |
---|
349 | } |
---|
350 | $tokens = ' '; |
---|
351 | // FIFO queue of characters |
---|
352 | $chars = array(); |
---|
353 | // Begin loop |
---|
354 | for ($i = 0; $i < $l; ++$i) { |
---|
355 | // Grab next character |
---|
356 | $current = drupal_substr($str, 0, 1); |
---|
357 | $str = substr($str, strlen($current)); |
---|
358 | $chars[] = $current; |
---|
359 | if ($i >= $min - 1) { |
---|
360 | $tokens .= implode('', $chars) .' '; |
---|
361 | array_shift($chars); |
---|
362 | } |
---|
363 | } |
---|
364 | return $tokens; |
---|
365 | } |
---|
366 | |
---|
367 | /** |
---|
368 | * Splits a string into tokens for indexing. |
---|
369 | */ |
---|
370 | function search_index_split($text) { |
---|
371 | static $last = NULL; |
---|
372 | static $lastsplit = NULL; |
---|
373 | |
---|
374 | if ($last == $text) { |
---|
375 | return $lastsplit; |
---|
376 | } |
---|
377 | // Process words |
---|
378 | $text = search_simplify($text); |
---|
379 | $words = explode(' ', $text); |
---|
380 | array_walk($words, '_search_index_truncate'); |
---|
381 | |
---|
382 | // Save last keyword result |
---|
383 | $last = $text; |
---|
384 | $lastsplit = $words; |
---|
385 | |
---|
386 | return $words; |
---|
387 | } |
---|
388 | |
---|
389 | /** |
---|
390 | * Helper function for array_walk in search_index_split. |
---|
391 | */ |
---|
392 | function _search_index_truncate(&$text) { |
---|
393 | $text = truncate_utf8($text, 50); |
---|
394 | } |
---|
395 | |
---|
396 | /** |
---|
397 | * Invokes hook_search_preprocess() in modules. |
---|
398 | */ |
---|
399 | function search_invoke_preprocess(&$text) { |
---|
400 | foreach (module_implements('search_preprocess') as $module) { |
---|
401 | $text = module_invoke($module, 'search_preprocess', $text); |
---|
402 | } |
---|
403 | } |
---|
404 | |
---|
405 | /** |
---|
406 | * Update the full-text search index for a particular item. |
---|
407 | * |
---|
408 | * @param $sid |
---|
409 | * A number identifying this particular item (e.g. node id). |
---|
410 | * |
---|
411 | * @param $type |
---|
412 | * A string defining this type of item (e.g. 'node') |
---|
413 | * |
---|
414 | * @param $text |
---|
415 | * The content of this item. Must be a piece of HTML text. |
---|
416 | * |
---|
417 | * @ingroup search |
---|
418 | */ |
---|
419 | function search_index($sid, $type, $text) { |
---|
420 | $minimum_word_size = variable_get('minimum_word_size', 3); |
---|
421 | |
---|
422 | // Link matching |
---|
423 | global $base_url; |
---|
424 | $node_regexp = '@href=[\'"]?(?:'. preg_quote($base_url, '@') .'/|'. preg_quote(base_path(), '@') .')(?:\?q=)?/?((?![a-z]+:)[^\'">]+)[\'">]@i'; |
---|
425 | |
---|
426 | // Multipliers for scores of words inside certain HTML tags. |
---|
427 | // Note: 'a' must be included for link ranking to work. |
---|
428 | $tags = array('h1' => 25, |
---|
429 | 'h2' => 18, |
---|
430 | 'h3' => 15, |
---|
431 | 'h4' => 12, |
---|
432 | 'h5' => 9, |
---|
433 | 'h6' => 6, |
---|
434 | 'u' => 3, |
---|
435 | 'b' => 3, |
---|
436 | 'i' => 3, |
---|
437 | 'strong' => 3, |
---|
438 | 'em' => 3, |
---|
439 | 'a' => 10); |
---|
440 | |
---|
441 | // Strip off all ignored tags to speed up processing, but insert space before/after |
---|
442 | // them to keep word boundaries. |
---|
443 | $text = str_replace(array('<', '>'), array(' <', '> '), $text); |
---|
444 | $text = strip_tags($text, '<'. implode('><', array_keys($tags)) .'>'); |
---|
445 | |
---|
446 | // Split HTML tags from plain text. |
---|
447 | $split = preg_split('/\s*<([^>]+?)>\s*/', $text, -1, PREG_SPLIT_DELIM_CAPTURE); |
---|
448 | // Note: PHP ensures the array consists of alternating delimiters and literals |
---|
449 | // and begins and ends with a literal (inserting $null as required). |
---|
450 | |
---|
451 | $tag = FALSE; // Odd/even counter. Tag or no tag. |
---|
452 | $link = FALSE; // State variable for link analyser |
---|
453 | $score = 1; // Starting score per word |
---|
454 | $accum = ' '; // Accumulator for cleaned up data |
---|
455 | $tagstack = array(); // Stack with open tags |
---|
456 | $tagwords = 0; // Counter for consecutive words |
---|
457 | $focus = 1; // Focus state |
---|
458 | |
---|
459 | $results = array(0 => array()); // Accumulator for words for index |
---|
460 | |
---|
461 | foreach ($split as $value) { |
---|
462 | if ($tag) { |
---|
463 | // Increase or decrease score per word based on tag |
---|
464 | list($tagname) = explode(' ', $value, 2); |
---|
465 | $tagname = drupal_strtolower($tagname); |
---|
466 | // Closing or opening tag? |
---|
467 | if ($tagname[0] == '/') { |
---|
468 | $tagname = substr($tagname, 1); |
---|
469 | // If we encounter unexpected tags, reset score to avoid incorrect boosting. |
---|
470 | if (!count($tagstack) || $tagstack[0] != $tagname) { |
---|
471 | $tagstack = array(); |
---|
472 | $score = 1; |
---|
473 | } |
---|
474 | else { |
---|
475 | // Remove from tag stack and decrement score |
---|
476 | $score = max(1, $score - $tags[array_shift($tagstack)]); |
---|
477 | } |
---|
478 | if ($tagname == 'a') { |
---|
479 | $link = FALSE; |
---|
480 | } |
---|
481 | } |
---|
482 | else { |
---|
483 | if (isset($tagstack[0]) && $tagstack[0] == $tagname) { |
---|
484 | // None of the tags we look for make sense when nested identically. |
---|
485 | // If they are, it's probably broken HTML. |
---|
486 | $tagstack = array(); |
---|
487 | $score = 1; |
---|
488 | } |
---|
489 | else { |
---|
490 | // Add to open tag stack and increment score |
---|
491 | array_unshift($tagstack, $tagname); |
---|
492 | $score += $tags[$tagname]; |
---|
493 | } |
---|
494 | if ($tagname == 'a') { |
---|
495 | // Check if link points to a node on this site |
---|
496 | if (preg_match($node_regexp, $value, $match)) { |
---|
497 | $path = drupal_get_normal_path($match[1]); |
---|
498 | if (preg_match('!(?:node|book)/(?:view/)?([0-9]+)!i', $path, $match)) { |
---|
499 | $linknid = $match[1]; |
---|
500 | if ($linknid > 0) { |
---|
501 | // Note: ignore links to uncachable nodes to avoid redirect bugs. |
---|
502 | $node = db_fetch_object(db_query('SELECT n.title, n.nid, n.vid, r.format FROM {node} n INNER JOIN {node_revisions} r ON n.vid = r.vid WHERE n.nid = %d', $linknid)); |
---|
503 | if (filter_format_allowcache($node->format)) { |
---|
504 | $link = TRUE; |
---|
505 | $linktitle = $node->title; |
---|
506 | } |
---|
507 | } |
---|
508 | } |
---|
509 | } |
---|
510 | } |
---|
511 | } |
---|
512 | // A tag change occurred, reset counter. |
---|
513 | $tagwords = 0; |
---|
514 | } |
---|
515 | else { |
---|
516 | // Note: use of PREG_SPLIT_DELIM_CAPTURE above will introduce empty values |
---|
517 | if ($value != '') { |
---|
518 | if ($link) { |
---|
519 | // Check to see if the node link text is its URL. If so, we use the target node title instead. |
---|
520 | if (preg_match('!^https?://!i', $value)) { |
---|
521 | $value = $linktitle; |
---|
522 | } |
---|
523 | } |
---|
524 | $words = search_index_split($value); |
---|
525 | foreach ($words as $word) { |
---|
526 | // Add word to accumulator |
---|
527 | $accum .= $word .' '; |
---|
528 | $num = is_numeric($word); |
---|
529 | // Check wordlength |
---|
530 | if ($num || drupal_strlen($word) >= $minimum_word_size) { |
---|
531 | // Normalize numbers |
---|
532 | if ($num) { |
---|
533 | $word = (int)ltrim($word, '-0'); |
---|
534 | } |
---|
535 | |
---|
536 | // Links score mainly for the target. |
---|
537 | if ($link) { |
---|
538 | if (!isset($results[$linknid])) { |
---|
539 | $results[$linknid] = array(); |
---|
540 | } |
---|
541 | $results[$linknid][] = $word; |
---|
542 | // Reduce score of the link caption in the source. |
---|
543 | $focus *= 0.2; |
---|
544 | } |
---|
545 | // Fall-through |
---|
546 | if (!isset($results[0][$word])) { |
---|
547 | $results[0][$word] = 0; |
---|
548 | } |
---|
549 | $results[0][$word] += $score * $focus; |
---|
550 | |
---|
551 | // Focus is a decaying value in terms of the amount of unique words up to this point. |
---|
552 | // From 100 words and more, it decays, to e.g. 0.5 at 500 words and 0.3 at 1000 words. |
---|
553 | $focus = min(1, .01 + 3.5 / (2 + count($results[0]) * .015)); |
---|
554 | } |
---|
555 | $tagwords++; |
---|
556 | // Too many words inside a single tag probably mean a tag was accidentally left open. |
---|
557 | if (count($tagstack) && $tagwords >= 15) { |
---|
558 | $tagstack = array(); |
---|
559 | $score = 1; |
---|
560 | } |
---|
561 | } |
---|
562 | } |
---|
563 | } |
---|
564 | $tag = !$tag; |
---|
565 | } |
---|
566 | |
---|
567 | search_wipe($sid, $type, TRUE); |
---|
568 | |
---|
569 | // Insert cleaned up data into dataset |
---|
570 | db_query("INSERT INTO {search_dataset} (sid, type, data, reindex) VALUES (%d, '%s', '%s', %d)", $sid, $type, $accum, 0); |
---|
571 | |
---|
572 | // Insert results into search index |
---|
573 | foreach ($results[0] as $word => $score) { |
---|
574 | // Try inserting first because this will succeed most times, but because |
---|
575 | // the database collates similar words (accented and non-accented), the |
---|
576 | // insert can fail, in which case we need to add the word scores together. |
---|
577 | @db_query("INSERT INTO {search_index} (word, sid, type, score) VALUES ('%s', %d, '%s', %f)", $word, $sid, $type, $score); |
---|
578 | if (!db_affected_rows()) { |
---|
579 | db_query("UPDATE {search_index} SET score = score + %f WHERE word = '%s' AND sid = %d AND type = '%s'", $score, $word, $sid, $type); |
---|
580 | } |
---|
581 | search_dirty($word); |
---|
582 | } |
---|
583 | unset($results[0]); |
---|
584 | |
---|
585 | // Get all previous links from this item. |
---|
586 | $result = db_query("SELECT nid, caption FROM {search_node_links} WHERE sid = %d AND type = '%s'", $sid, $type); |
---|
587 | $links = array(); |
---|
588 | while ($link = db_fetch_object($result)) { |
---|
589 | $links[$link->nid] = $link->caption; |
---|
590 | } |
---|
591 | |
---|
592 | // Now store links to nodes. |
---|
593 | foreach ($results as $nid => $words) { |
---|
594 | $caption = implode(' ', $words); |
---|
595 | if (isset($links[$nid])) { |
---|
596 | if ($links[$nid] != $caption) { |
---|
597 | // Update the existing link and mark the node for reindexing. |
---|
598 | db_query("UPDATE {search_node_links} SET caption = '%s' WHERE sid = %d AND type = '%s' AND nid = %d", $caption, $sid, $type, $nid); |
---|
599 | search_touch_node($nid); |
---|
600 | } |
---|
601 | // Unset the link to mark it as processed. |
---|
602 | unset($links[$nid]); |
---|
603 | } |
---|
604 | else { |
---|
605 | // Insert the existing link and mark the node for reindexing. |
---|
606 | db_query("INSERT INTO {search_node_links} (caption, sid, type, nid) VALUES ('%s', %d, '%s', %d)", $caption, $sid, $type, $nid); |
---|
607 | search_touch_node($nid); |
---|
608 | } |
---|
609 | } |
---|
610 | // Any left-over links in $links no longer exist. Delete them and mark the nodes for reindexing. |
---|
611 | foreach ($links as $nid => $caption) { |
---|
612 | db_query("DELETE FROM {search_node_links} WHERE sid = %d AND type = '%s' AND nid = %d", $sid, $type, $nid); |
---|
613 | search_touch_node($nid); |
---|
614 | } |
---|
615 | } |
---|
616 | |
---|
617 | /** |
---|
618 | * Change a node's changed timestamp to 'now' to force reindexing. |
---|
619 | * |
---|
620 | * @param $nid |
---|
621 | * The nid of the node that needs reindexing. |
---|
622 | */ |
---|
623 | function search_touch_node($nid) { |
---|
624 | db_query("UPDATE {search_dataset} SET reindex = %d WHERE sid = %d AND type = 'node'", time(), $nid); |
---|
625 | } |
---|
626 | |
---|
627 | /** |
---|
628 | * Implementation of hook_nodeapi(). |
---|
629 | */ |
---|
630 | function search_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) { |
---|
631 | switch ($op) { |
---|
632 | // Transplant links to a node into the target node. |
---|
633 | case 'update index': |
---|
634 | $result = db_query("SELECT caption FROM {search_node_links} WHERE nid = %d", $node->nid); |
---|
635 | $output = array(); |
---|
636 | while ($link = db_fetch_object($result)) { |
---|
637 | $output[] = $link->caption; |
---|
638 | } |
---|
639 | if (count($output)) { |
---|
640 | return '<a>('. implode(', ', $output) .')</a>'; |
---|
641 | } |
---|
642 | break; |
---|
643 | // Reindex the node when it is updated. The node is automatically indexed |
---|
644 | // when it is added, simply by being added to the node table. |
---|
645 | case 'update': |
---|
646 | search_touch_node($node->nid); |
---|
647 | break; |
---|
648 | } |
---|
649 | } |
---|
650 | |
---|
651 | /** |
---|
652 | * Implementation of hook_comment(). |
---|
653 | */ |
---|
654 | function search_comment($a1, $op) { |
---|
655 | switch ($op) { |
---|
656 | // Reindex the node when comments are added or changed |
---|
657 | case 'insert': |
---|
658 | case 'update': |
---|
659 | case 'delete': |
---|
660 | case 'publish': |
---|
661 | case 'unpublish': |
---|
662 | search_touch_node(is_array($a1) ? $a1['nid'] : $a1->nid); |
---|
663 | break; |
---|
664 | } |
---|
665 | } |
---|
666 | |
---|
667 | /** |
---|
668 | * Extract a module-specific search option from a search query. e.g. 'type:book' |
---|
669 | */ |
---|
670 | function search_query_extract($keys, $option) { |
---|
671 | if (preg_match('/(^| )'. $option .':([^ ]*)( |$)/i', $keys, $matches)) { |
---|
672 | return $matches[2]; |
---|
673 | } |
---|
674 | } |
---|
675 | |
---|
676 | /** |
---|
677 | * Return a query with the given module-specific search option inserted in. |
---|
678 | * e.g. 'type:book'. |
---|
679 | */ |
---|
680 | function search_query_insert($keys, $option, $value = '') { |
---|
681 | if (search_query_extract($keys, $option)) { |
---|
682 | $keys = trim(preg_replace('/(^| )'. $option .':[^ ]*/i', '', $keys)); |
---|
683 | } |
---|
684 | if ($value != '') { |
---|
685 | $keys .= ' '. $option .':'. $value; |
---|
686 | } |
---|
687 | return $keys; |
---|
688 | } |
---|
689 | |
---|
690 | /** |
---|
691 | * Parse a search query into SQL conditions. |
---|
692 | * |
---|
693 | * We build two queries that matches the dataset bodies. @See do_search for |
---|
694 | * more about these. |
---|
695 | * |
---|
696 | * @param $text |
---|
697 | * The search keys. |
---|
698 | * @return |
---|
699 | * A list of six elements. |
---|
700 | * * A series of statements AND'd together which will be used to provide all |
---|
701 | * possible matches. |
---|
702 | * * Arguments for this query part. |
---|
703 | * * A series of exact word matches OR'd together. |
---|
704 | * * Arguments for this query part. |
---|
705 | * * A bool indicating whether this is a simple query or not. Negative |
---|
706 | * terms, presence of both AND / OR make this FALSE. |
---|
707 | * * A bool indicating the presence of a lowercase or. Maybe the user |
---|
708 | * wanted to use OR. |
---|
709 | */ |
---|
710 | function search_parse_query($text) { |
---|
711 | $keys = array('positive' => array(), 'negative' => array()); |
---|
712 | |
---|
713 | // Tokenize query string |
---|
714 | preg_match_all('/ (-?)("[^"]+"|[^" ]+)/i', ' '. $text, $matches, PREG_SET_ORDER); |
---|
715 | |
---|
716 | if (count($matches) < 1) { |
---|
717 | return NULL; |
---|
718 | } |
---|
719 | |
---|
720 | // Classify tokens |
---|
721 | $or = FALSE; |
---|
722 | $warning = ''; |
---|
723 | $simple = TRUE; |
---|
724 | foreach ($matches as $match) { |
---|
725 | $phrase = FALSE; |
---|
726 | // Strip off phrase quotes |
---|
727 | if ($match[2]{0} == '"') { |
---|
728 | $match[2] = substr($match[2], 1, -1); |
---|
729 | $phrase = TRUE; |
---|
730 | $simple = FALSE; |
---|
731 | } |
---|
732 | // Simplify keyword according to indexing rules and external preprocessors |
---|
733 | $words = search_simplify($match[2]); |
---|
734 | // Re-explode in case simplification added more words, except when matching a phrase |
---|
735 | $words = $phrase ? array($words) : preg_split('/ /', $words, -1, PREG_SPLIT_NO_EMPTY); |
---|
736 | // Negative matches |
---|
737 | if ($match[1] == '-') { |
---|
738 | $keys['negative'] = array_merge($keys['negative'], $words); |
---|
739 | } |
---|
740 | // OR operator: instead of a single keyword, we store an array of all |
---|
741 | // OR'd keywords. |
---|
742 | elseif ($match[2] == 'OR' && count($keys['positive'])) { |
---|
743 | $last = array_pop($keys['positive']); |
---|
744 | // Starting a new OR? |
---|
745 | if (!is_array($last)) { |
---|
746 | $last = array($last); |
---|
747 | } |
---|
748 | $keys['positive'][] = $last; |
---|
749 | $or = TRUE; |
---|
750 | continue; |
---|
751 | } |
---|
752 | // AND operator: implied, so just ignore it |
---|
753 | elseif ($match[2] == 'AND' || $match[2] == 'and') { |
---|
754 | $warning = $match[2]; |
---|
755 | continue; |
---|
756 | } |
---|
757 | |
---|
758 | // Plain keyword |
---|
759 | else { |
---|
760 | if ($match[2] == 'or') { |
---|
761 | $warning = $match[2]; |
---|
762 | } |
---|
763 | if ($or) { |
---|
764 | // Add to last element (which is an array) |
---|
765 | $keys['positive'][count($keys['positive']) - 1] = array_merge($keys['positive'][count($keys['positive']) - 1], $words); |
---|
766 | } |
---|
767 | else { |
---|
768 | $keys['positive'] = array_merge($keys['positive'], $words); |
---|
769 | } |
---|
770 | } |
---|
771 | $or = FALSE; |
---|
772 | } |
---|
773 | |
---|
774 | // Convert keywords into SQL statements. |
---|
775 | $query = array(); |
---|
776 | $query2 = array(); |
---|
777 | $arguments = array(); |
---|
778 | $arguments2 = array(); |
---|
779 | $matches = 0; |
---|
780 | $simple_and = FALSE; |
---|
781 | $simple_or = FALSE; |
---|
782 | // Positive matches |
---|
783 | foreach ($keys['positive'] as $key) { |
---|
784 | // Group of ORed terms |
---|
785 | if (is_array($key) && count($key)) { |
---|
786 | $simple_or = TRUE; |
---|
787 | $queryor = array(); |
---|
788 | $any = FALSE; |
---|
789 | foreach ($key as $or) { |
---|
790 | list($q, $num_new_scores) = _search_parse_query($or, $arguments2); |
---|
791 | $any |= $num_new_scores; |
---|
792 | if ($q) { |
---|
793 | $queryor[] = $q; |
---|
794 | $arguments[] = $or; |
---|
795 | } |
---|
796 | } |
---|
797 | if (count($queryor)) { |
---|
798 | $query[] = '('. implode(' OR ', $queryor) .')'; |
---|
799 | // A group of OR keywords only needs to match once |
---|
800 | $matches += ($any > 0); |
---|
801 | } |
---|
802 | } |
---|
803 | // Single ANDed term |
---|
804 | else { |
---|
805 | $simple_and = TRUE; |
---|
806 | list($q, $num_new_scores, $num_valid_words) = _search_parse_query($key, $arguments2); |
---|
807 | if ($q) { |
---|
808 | $query[] = $q; |
---|
809 | $arguments[] = $key; |
---|
810 | if (!$num_valid_words) { |
---|
811 | $simple = FALSE; |
---|
812 | } |
---|
813 | // Each AND keyword needs to match at least once |
---|
814 | $matches += $num_new_scores; |
---|
815 | } |
---|
816 | } |
---|
817 | } |
---|
818 | if ($simple_and && $simple_or) { |
---|
819 | $simple = FALSE; |
---|
820 | } |
---|
821 | // Negative matches |
---|
822 | foreach ($keys['negative'] as $key) { |
---|
823 | list($q) = _search_parse_query($key, $arguments2, TRUE); |
---|
824 | if ($q) { |
---|
825 | $query[] = $q; |
---|
826 | $arguments[] = $key; |
---|
827 | $simple = FALSE; |
---|
828 | } |
---|
829 | } |
---|
830 | $query = implode(' AND ', $query); |
---|
831 | |
---|
832 | // Build word-index conditions for the first pass |
---|
833 | $query2 = substr(str_repeat("i.word = '%s' OR ", count($arguments2)), 0, -4); |
---|
834 | |
---|
835 | return array($query, $arguments, $query2, $arguments2, $matches, $simple, $warning); |
---|
836 | } |
---|
837 | |
---|
838 | /** |
---|
839 | * Helper function for search_parse_query(); |
---|
840 | */ |
---|
841 | function _search_parse_query(&$word, &$scores, $not = FALSE) { |
---|
842 | $num_new_scores = 0; |
---|
843 | $num_valid_words = 0; |
---|
844 | // Determine the scorewords of this word/phrase |
---|
845 | if (!$not) { |
---|
846 | $split = explode(' ', $word); |
---|
847 | foreach ($split as $s) { |
---|
848 | $num = is_numeric($s); |
---|
849 | if ($num || drupal_strlen($s) >= variable_get('minimum_word_size', 3)) { |
---|
850 | $s = $num ? ((int)ltrim($s, '-0')) : $s; |
---|
851 | if (!isset($scores[$s])) { |
---|
852 | $scores[$s] = $s; |
---|
853 | $num_new_scores++; |
---|
854 | } |
---|
855 | $num_valid_words++; |
---|
856 | } |
---|
857 | } |
---|
858 | } |
---|
859 | // Return matching snippet and number of added words |
---|
860 | return array("d.data ". ($not ? 'NOT ' : '') ."LIKE '%% %s %%'", $num_new_scores, $num_valid_words); |
---|
861 | } |
---|
862 | |
---|
863 | /** |
---|
864 | * Do a query on the full-text search index for a word or words. |
---|
865 | * |
---|
866 | * This function is normally only called by each module that support the |
---|
867 | * indexed search (and thus, implements hook_update_index()). |
---|
868 | * |
---|
869 | * Results are retrieved in two logical passes. However, the two passes are |
---|
870 | * joined together into a single query. And in the case of most simple |
---|
871 | * queries the second pass is not even used. |
---|
872 | * |
---|
873 | * The first pass selects a set of all possible matches, which has the benefit |
---|
874 | * of also providing the exact result set for simple "AND" or "OR" searches. |
---|
875 | * |
---|
876 | * The second portion of the query further refines this set by verifying |
---|
877 | * advanced text conditions (such negative or phrase matches) |
---|
878 | * |
---|
879 | * @param $keywords |
---|
880 | * A search string as entered by the user. |
---|
881 | * |
---|
882 | * @param $type |
---|
883 | * A string identifying the calling module. |
---|
884 | * |
---|
885 | * @param $join1 |
---|
886 | * (optional) Inserted into the JOIN part of the first SQL query. |
---|
887 | * For example "INNER JOIN {node} n ON n.nid = i.sid". |
---|
888 | * |
---|
889 | * @param $where1 |
---|
890 | * (optional) Inserted into the WHERE part of the first SQL query. |
---|
891 | * For example "(n.status > %d)". |
---|
892 | * |
---|
893 | * @param $arguments1 |
---|
894 | * (optional) Extra SQL arguments belonging to the first query. |
---|
895 | * |
---|
896 | * @param $columns2 |
---|
897 | * (optional) Inserted into the SELECT pat of the second query. Must contain |
---|
898 | * a column selected as 'score'. |
---|
899 | * defaults to 'i.relevance AS score' |
---|
900 | * |
---|
901 | * @param $join2 |
---|
902 | * (optional) Inserted into the JOIN par of the second SQL query. |
---|
903 | * For example "INNER JOIN {node_comment_statistics} n ON n.nid = i.sid" |
---|
904 | * |
---|
905 | * @param $arguments2 |
---|
906 | * (optional) Extra SQL arguments belonging to the second query parameter. |
---|
907 | * |
---|
908 | * @param $sort_parameters |
---|
909 | * (optional) SQL arguments for sorting the final results. |
---|
910 | * Default: 'ORDER BY score DESC' |
---|
911 | * |
---|
912 | * @return |
---|
913 | * An array of objects for the search results. |
---|
914 | * |
---|
915 | * @ingroup search |
---|
916 | */ |
---|
917 | function do_search($keywords, $type, $join1 = '', $where1 = '1 = 1', $arguments1 = array(), $columns2 = 'i.relevance AS score', $join2 = '', $arguments2 = array(), $sort_parameters = 'ORDER BY score DESC') { |
---|
918 | $query = search_parse_query($keywords); |
---|
919 | |
---|
920 | if ($query[2] == '') { |
---|
921 | form_set_error('keys', t('You must include at least one positive keyword with @count characters or more.', array('@count' => variable_get('minimum_word_size', 3)))); |
---|
922 | } |
---|
923 | if ($query[6]) { |
---|
924 | if ($query[6] == 'or') { |
---|
925 | drupal_set_message(t('Search for either of the two terms with uppercase <strong>OR</strong>. For example, <strong>cats OR dogs</strong>.')); |
---|
926 | } |
---|
927 | } |
---|
928 | if ($query === NULL || $query[0] == '' || $query[2] == '') { |
---|
929 | return array(); |
---|
930 | } |
---|
931 | |
---|
932 | // Build query for keyword normalization. |
---|
933 | $conditions = "$where1 AND ($query[2]) AND i.type = '%s'"; |
---|
934 | $arguments1 = array_merge($arguments1, $query[3], array($type)); |
---|
935 | $join = "INNER JOIN {search_total} t ON i.word = t.word $join1"; |
---|
936 | if (!$query[5]) { |
---|
937 | $conditions .= " AND ($query[0])"; |
---|
938 | $arguments1 = array_merge($arguments1, $query[1]); |
---|
939 | $join .= " INNER JOIN {search_dataset} d ON i.sid = d.sid AND i.type = d.type"; |
---|
940 | } |
---|
941 | |
---|
942 | // Calculate maximum keyword relevance, to normalize it. |
---|
943 | $select = "SELECT SUM(i.score * t.count) AS score FROM {search_index} i $join WHERE $conditions GROUP BY i.type, i.sid HAVING COUNT(*) >= %d ORDER BY score DESC"; |
---|
944 | $arguments = array_merge($arguments1, array($query[4])); |
---|
945 | $normalize = db_result(db_query_range($select, $arguments, 0, 1)); |
---|
946 | if (!$normalize) { |
---|
947 | return array(); |
---|
948 | } |
---|
949 | $columns2 = str_replace('i.relevance', '('. (1.0 / $normalize) .' * SUM(i.score * t.count))', $columns2); |
---|
950 | |
---|
951 | // Build query to retrieve results. |
---|
952 | $select = "SELECT i.type, i.sid, $columns2 FROM {search_index} i $join $join2 WHERE $conditions GROUP BY i.type, i.sid HAVING COUNT(*) >= %d"; |
---|
953 | $count_select = "SELECT COUNT(*) FROM ($select) n1"; |
---|
954 | $arguments = array_merge($arguments2, $arguments1, array($query[4])); |
---|
955 | |
---|
956 | // Do actual search query |
---|
957 | $result = pager_query("$select $sort_parameters", 10, 0, $count_select, $arguments); |
---|
958 | $results = array(); |
---|
959 | while ($item = db_fetch_object($result)) { |
---|
960 | $results[] = $item; |
---|
961 | } |
---|
962 | return $results; |
---|
963 | } |
---|
964 | |
---|
965 | /** |
---|
966 | * Helper function for grabbing search keys. |
---|
967 | */ |
---|
968 | function search_get_keys() { |
---|
969 | static $return; |
---|
970 | if (!isset($return)) { |
---|
971 | // Extract keys as remainder of path |
---|
972 | // Note: support old GET format of searches for existing links. |
---|
973 | $path = explode('/', $_GET['q'], 3); |
---|
974 | $keys = empty($_REQUEST['keys']) ? '' : $_REQUEST['keys']; |
---|
975 | $return = count($path) == 3 ? $path[2] : $keys; |
---|
976 | } |
---|
977 | return $return; |
---|
978 | } |
---|
979 | |
---|
980 | /** |
---|
981 | * @defgroup search Search interface |
---|
982 | * @{ |
---|
983 | * The Drupal search interface manages a global search mechanism. |
---|
984 | * |
---|
985 | * Modules may plug into this system to provide searches of different types of |
---|
986 | * data. Most of the system is handled by search.module, so this must be enabled |
---|
987 | * for all of the search features to work. |
---|
988 | * |
---|
989 | * There are three ways to interact with the search system: |
---|
990 | * - Specifically for searching nodes, you can implement |
---|
991 | * hook_nodeapi('update index') and hook_nodeapi('search result'). However, |
---|
992 | * note that the search system already indexes all visible output of a node, |
---|
993 | * i.e. everything displayed normally by hook_view() and hook_nodeapi('view'). |
---|
994 | * This is usually sufficient. You should only use this mechanism if you want |
---|
995 | * additional, non-visible data to be indexed. |
---|
996 | * - Implement hook_search(). This will create a search tab for your module on |
---|
997 | * the /search page with a simple keyword search form. |
---|
998 | * - Implement hook_update_index(). This allows your module to use Drupal's |
---|
999 | * HTML indexing mechanism for searching full text efficiently. |
---|
1000 | * |
---|
1001 | * If your module needs to provide a more complicated search form, then you need |
---|
1002 | * to implement it yourself without hook_search(). In that case, you should |
---|
1003 | * define it as a local task (tab) under the /search page (e.g. /search/mymodule) |
---|
1004 | * so that users can easily find it. |
---|
1005 | */ |
---|
1006 | |
---|
1007 | /** |
---|
1008 | * Render a search form. |
---|
1009 | * |
---|
1010 | * @param $action |
---|
1011 | * Form action. Defaults to "search". |
---|
1012 | * @param $keys |
---|
1013 | * The search string entered by the user, containing keywords for the search. |
---|
1014 | * @param $type |
---|
1015 | * The type of search to render the node for. Must be the name of module |
---|
1016 | * which implements hook_search(). Defaults to 'node'. |
---|
1017 | * @param $prompt |
---|
1018 | * A piece of text to put before the form (e.g. "Enter your keywords") |
---|
1019 | * @return |
---|
1020 | * A Form API array for the search form. |
---|
1021 | */ |
---|
1022 | function search_form(&$form_state, $action = '', $keys = '', $type = NULL, $prompt = NULL) { |
---|
1023 | |
---|
1024 | // Add CSS |
---|
1025 | drupal_add_css(drupal_get_path('module', 'search') .'/search.css', 'module', 'all', FALSE); |
---|
1026 | |
---|
1027 | if (!$action) { |
---|
1028 | $action = url('search/'. $type); |
---|
1029 | } |
---|
1030 | if (is_null($prompt)) { |
---|
1031 | $prompt = t('Enter your keywords'); |
---|
1032 | } |
---|
1033 | |
---|
1034 | $form = array( |
---|
1035 | '#action' => $action, |
---|
1036 | '#attributes' => array('class' => 'search-form'), |
---|
1037 | ); |
---|
1038 | $form['module'] = array('#type' => 'value', '#value' => $type); |
---|
1039 | $form['basic'] = array('#type' => 'item', '#title' => $prompt, '#id' => 'edit-keys'); |
---|
1040 | $form['basic']['inline'] = array('#prefix' => '<div class="container-inline">', '#suffix' => '</div>'); |
---|
1041 | $form['basic']['inline']['keys'] = array( |
---|
1042 | '#type' => 'textfield', |
---|
1043 | '#title' => '', |
---|
1044 | '#default_value' => $keys, |
---|
1045 | '#size' => $prompt ? 40 : 20, |
---|
1046 | '#maxlength' => 255, |
---|
1047 | ); |
---|
1048 | // processed_keys is used to coordinate keyword passing between other forms |
---|
1049 | // that hook into the basic search form. |
---|
1050 | $form['basic']['inline']['processed_keys'] = array('#type' => 'value', '#value' => array()); |
---|
1051 | $form['basic']['inline']['submit'] = array('#type' => 'submit', '#value' => t('Search')); |
---|
1052 | |
---|
1053 | return $form; |
---|
1054 | } |
---|
1055 | |
---|
1056 | /** |
---|
1057 | * Form builder; Output a search form for the search block and the theme's search box. |
---|
1058 | * |
---|
1059 | * @ingroup forms |
---|
1060 | * @see search_box_form_submit() |
---|
1061 | * @see search-block-form.tpl.php |
---|
1062 | */ |
---|
1063 | function search_box(&$form_state, $form_id) { |
---|
1064 | $form[$form_id] = array( |
---|
1065 | '#title' => t('Search this site'), |
---|
1066 | '#type' => 'textfield', |
---|
1067 | '#size' => 15, |
---|
1068 | '#default_value' => '', |
---|
1069 | '#attributes' => array('title' => t('Enter the terms you wish to search for.')), |
---|
1070 | ); |
---|
1071 | $form['submit'] = array('#type' => 'submit', '#value' => t('Search')); |
---|
1072 | $form['#submit'][] = 'search_box_form_submit'; |
---|
1073 | |
---|
1074 | return $form; |
---|
1075 | } |
---|
1076 | |
---|
1077 | /** |
---|
1078 | * Process a block search form submission. |
---|
1079 | */ |
---|
1080 | function search_box_form_submit($form, &$form_state) { |
---|
1081 | // The search form relies on control of the redirect destination for its |
---|
1082 | // functionality, so we override any static destination set in the request, |
---|
1083 | // for example by drupal_access_denied() or drupal_not_found() |
---|
1084 | // (see http://drupal.org/node/292565). |
---|
1085 | if (isset($_REQUEST['destination'])) { |
---|
1086 | unset($_REQUEST['destination']); |
---|
1087 | } |
---|
1088 | if (isset($_REQUEST['edit']['destination'])) { |
---|
1089 | unset($_REQUEST['edit']['destination']); |
---|
1090 | } |
---|
1091 | |
---|
1092 | $form_id = $form['form_id']['#value']; |
---|
1093 | $form_state['redirect'] = 'search/node/'. trim($form_state['values'][$form_id]); |
---|
1094 | } |
---|
1095 | |
---|
1096 | /** |
---|
1097 | * Process variables for search-theme-form.tpl.php. |
---|
1098 | * |
---|
1099 | * The $variables array contains the following arguments: |
---|
1100 | * - $form |
---|
1101 | * |
---|
1102 | * @see search-theme-form.tpl.php |
---|
1103 | */ |
---|
1104 | function template_preprocess_search_theme_form(&$variables) { |
---|
1105 | $variables['search'] = array(); |
---|
1106 | $hidden = array(); |
---|
1107 | // Provide variables named after form keys so themers can print each element independently. |
---|
1108 | foreach (element_children($variables['form']) as $key) { |
---|
1109 | $type = $variables['form'][$key]['#type']; |
---|
1110 | if ($type == 'hidden' || $type == 'token') { |
---|
1111 | $hidden[] = drupal_render($variables['form'][$key]); |
---|
1112 | } |
---|
1113 | else { |
---|
1114 | $variables['search'][$key] = drupal_render($variables['form'][$key]); |
---|
1115 | } |
---|
1116 | } |
---|
1117 | // Hidden form elements have no value to themers. No need for separation. |
---|
1118 | $variables['search']['hidden'] = implode($hidden); |
---|
1119 | // Collect all form elements to make it easier to print the whole form. |
---|
1120 | $variables['search_form'] = implode($variables['search']); |
---|
1121 | } |
---|
1122 | |
---|
1123 | /** |
---|
1124 | * Process variables for search-block-form.tpl.php. |
---|
1125 | * |
---|
1126 | * The $variables array contains the following arguments: |
---|
1127 | * - $form |
---|
1128 | * |
---|
1129 | * @see search-block-form.tpl.php |
---|
1130 | */ |
---|
1131 | function template_preprocess_search_block_form(&$variables) { |
---|
1132 | $variables['search'] = array(); |
---|
1133 | $hidden = array(); |
---|
1134 | // Provide variables named after form keys so themers can print each element independently. |
---|
1135 | foreach (element_children($variables['form']) as $key) { |
---|
1136 | $type = $variables['form'][$key]['#type']; |
---|
1137 | if ($type == 'hidden' || $type == 'token') { |
---|
1138 | $hidden[] = drupal_render($variables['form'][$key]); |
---|
1139 | } |
---|
1140 | else { |
---|
1141 | $variables['search'][$key] = drupal_render($variables['form'][$key]); |
---|
1142 | } |
---|
1143 | } |
---|
1144 | // Hidden form elements have no value to themers. No need for separation. |
---|
1145 | $variables['search']['hidden'] = implode($hidden); |
---|
1146 | // Collect all form elements to make it easier to print the whole form. |
---|
1147 | $variables['search_form'] = implode($variables['search']); |
---|
1148 | } |
---|
1149 | |
---|
1150 | /** |
---|
1151 | * Perform a standard search on the given keys, and return the formatted results. |
---|
1152 | */ |
---|
1153 | function search_data($keys = NULL, $type = 'node') { |
---|
1154 | |
---|
1155 | if (isset($keys)) { |
---|
1156 | if (module_hook($type, 'search')) { |
---|
1157 | $results = module_invoke($type, 'search', 'search', $keys); |
---|
1158 | if (isset($results) && is_array($results) && count($results)) { |
---|
1159 | if (module_hook($type, 'search_page')) { |
---|
1160 | return module_invoke($type, 'search_page', $results); |
---|
1161 | } |
---|
1162 | else { |
---|
1163 | return theme('search_results', $results, $type); |
---|
1164 | } |
---|
1165 | } |
---|
1166 | } |
---|
1167 | } |
---|
1168 | } |
---|
1169 | |
---|
1170 | /** |
---|
1171 | * Returns snippets from a piece of text, with certain keywords highlighted. |
---|
1172 | * Used for formatting search results. |
---|
1173 | * |
---|
1174 | * @param $keys |
---|
1175 | * A string containing a search query. |
---|
1176 | * |
---|
1177 | * @param $text |
---|
1178 | * The text to extract fragments from. |
---|
1179 | * |
---|
1180 | * @return |
---|
1181 | * A string containing HTML for the excerpt. |
---|
1182 | */ |
---|
1183 | function search_excerpt($keys, $text) { |
---|
1184 | // We highlight around non-indexable or CJK characters. |
---|
1185 | $boundary = '(?:(?<=['. PREG_CLASS_SEARCH_EXCLUDE . PREG_CLASS_CJK .'])|(?=['. PREG_CLASS_SEARCH_EXCLUDE . PREG_CLASS_CJK .']))'; |
---|
1186 | |
---|
1187 | // Extract positive keywords and phrases |
---|
1188 | preg_match_all('/ ("([^"]+)"|(?!OR)([^" ]+))/', ' '. $keys, $matches); |
---|
1189 | $keys = array_merge($matches[2], $matches[3]); |
---|
1190 | |
---|
1191 | // Prepare text |
---|
1192 | $text = ' '. strip_tags(str_replace(array('<', '>'), array(' <', '> '), $text)) .' '; |
---|
1193 | array_walk($keys, '_search_excerpt_replace'); |
---|
1194 | $workkeys = $keys; |
---|
1195 | |
---|
1196 | // Extract a fragment per keyword for at most 4 keywords. |
---|
1197 | // First we collect ranges of text around each keyword, starting/ending |
---|
1198 | // at spaces. |
---|
1199 | // If the sum of all fragments is too short, we look for second occurrences. |
---|
1200 | $ranges = array(); |
---|
1201 | $included = array(); |
---|
1202 | $length = 0; |
---|
1203 | while ($length < 256 && count($workkeys)) { |
---|
1204 | foreach ($workkeys as $k => $key) { |
---|
1205 | if (strlen($key) == 0) { |
---|
1206 | unset($workkeys[$k]); |
---|
1207 | unset($keys[$k]); |
---|
1208 | continue; |
---|
1209 | } |
---|
1210 | if ($length >= 256) { |
---|
1211 | break; |
---|
1212 | } |
---|
1213 | // Remember occurrence of key so we can skip over it if more occurrences |
---|
1214 | // are desired. |
---|
1215 | if (!isset($included[$key])) { |
---|
1216 | $included[$key] = 0; |
---|
1217 | } |
---|
1218 | // Locate a keyword (position $p), then locate a space in front (position |
---|
1219 | // $q) and behind it (position $s) |
---|
1220 | if (preg_match('/'. $boundary . $key . $boundary .'/iu', $text, $match, PREG_OFFSET_CAPTURE, $included[$key])) { |
---|
1221 | $p = $match[0][1]; |
---|
1222 | if (($q = strpos($text, ' ', max(0, $p - 60))) !== FALSE) { |
---|
1223 | $end = substr($text, $p, 80); |
---|
1224 | if (($s = strrpos($end, ' ')) !== FALSE) { |
---|
1225 | $ranges[$q] = $p + $s; |
---|
1226 | $length += $p + $s - $q; |
---|
1227 | $included[$key] = $p + 1; |
---|
1228 | } |
---|
1229 | else { |
---|
1230 | unset($workkeys[$k]); |
---|
1231 | } |
---|
1232 | } |
---|
1233 | else { |
---|
1234 | unset($workkeys[$k]); |
---|
1235 | } |
---|
1236 | } |
---|
1237 | else { |
---|
1238 | unset($workkeys[$k]); |
---|
1239 | } |
---|
1240 | } |
---|
1241 | } |
---|
1242 | |
---|
1243 | // If we didn't find anything, return the beginning. |
---|
1244 | if (count($ranges) == 0) { |
---|
1245 | return truncate_utf8($text, 256) .' ...'; |
---|
1246 | } |
---|
1247 | |
---|
1248 | // Sort the text ranges by starting position. |
---|
1249 | ksort($ranges); |
---|
1250 | |
---|
1251 | // Now we collapse overlapping text ranges into one. The sorting makes it O(n). |
---|
1252 | $newranges = array(); |
---|
1253 | foreach ($ranges as $from2 => $to2) { |
---|
1254 | if (!isset($from1)) { |
---|
1255 | $from1 = $from2; |
---|
1256 | $to1 = $to2; |
---|
1257 | continue; |
---|
1258 | } |
---|
1259 | if ($from2 <= $to1) { |
---|
1260 | $to1 = max($to1, $to2); |
---|
1261 | } |
---|
1262 | else { |
---|
1263 | $newranges[$from1] = $to1; |
---|
1264 | $from1 = $from2; |
---|
1265 | $to1 = $to2; |
---|
1266 | } |
---|
1267 | } |
---|
1268 | $newranges[$from1] = $to1; |
---|
1269 | |
---|
1270 | // Fetch text |
---|
1271 | $out = array(); |
---|
1272 | foreach ($newranges as $from => $to) { |
---|
1273 | $out[] = substr($text, $from, $to - $from); |
---|
1274 | } |
---|
1275 | $text = (isset($newranges[0]) ? '' : '... ') . implode(' ... ', $out) .' ...'; |
---|
1276 | |
---|
1277 | // Highlight keywords. Must be done at once to prevent conflicts ('strong' and '<strong>'). |
---|
1278 | $text = preg_replace('/'. $boundary .'('. implode('|', $keys) .')'. $boundary .'/iu', '<strong>\0</strong>', $text); |
---|
1279 | return $text; |
---|
1280 | } |
---|
1281 | |
---|
1282 | /** |
---|
1283 | * @} End of "defgroup search". |
---|
1284 | */ |
---|
1285 | |
---|
1286 | /** |
---|
1287 | * Helper function for array_walk in search_except. |
---|
1288 | */ |
---|
1289 | function _search_excerpt_replace(&$text) { |
---|
1290 | $text = preg_quote($text, '/'); |
---|
1291 | } |
---|
1292 | |
---|
1293 | function search_forms() { |
---|
1294 | $forms['search_theme_form']= array( |
---|
1295 | 'callback' => 'search_box', |
---|
1296 | 'callback arguments' => array('search_theme_form'), |
---|
1297 | ); |
---|
1298 | $forms['search_block_form']= array( |
---|
1299 | 'callback' => 'search_box', |
---|
1300 | 'callback arguments' => array('search_block_form'), |
---|
1301 | ); |
---|
1302 | return $forms; |
---|
1303 | } |
---|