Projet

Général

Profil

Paste
Télécharger (50 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / search / search.module @ 76597ebf

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file
5
 * Enables site-wide keyword searching.
6
 */
7
8
/**
9
 * Matches all 'N' Unicode character classes (numbers)
10
 */
11
define('PREG_CLASS_NUMBERS',
12
  '\x{30}-\x{39}\x{b2}\x{b3}\x{b9}\x{bc}-\x{be}\x{660}-\x{669}\x{6f0}-\x{6f9}' .
13
  '\x{966}-\x{96f}\x{9e6}-\x{9ef}\x{9f4}-\x{9f9}\x{a66}-\x{a6f}\x{ae6}-\x{aef}' .
14
  '\x{b66}-\x{b6f}\x{be7}-\x{bf2}\x{c66}-\x{c6f}\x{ce6}-\x{cef}\x{d66}-\x{d6f}' .
15
  '\x{e50}-\x{e59}\x{ed0}-\x{ed9}\x{f20}-\x{f33}\x{1040}-\x{1049}\x{1369}-' .
16
  '\x{137c}\x{16ee}-\x{16f0}\x{17e0}-\x{17e9}\x{17f0}-\x{17f9}\x{1810}-\x{1819}' .
17
  '\x{1946}-\x{194f}\x{2070}\x{2074}-\x{2079}\x{2080}-\x{2089}\x{2153}-\x{2183}' .
18
  '\x{2460}-\x{249b}\x{24ea}-\x{24ff}\x{2776}-\x{2793}\x{3007}\x{3021}-\x{3029}' .
19
  '\x{3038}-\x{303a}\x{3192}-\x{3195}\x{3220}-\x{3229}\x{3251}-\x{325f}\x{3280}-' .
20
  '\x{3289}\x{32b1}-\x{32bf}\x{ff10}-\x{ff19}');
21
22
/**
23
 * Matches all 'P' Unicode character classes (punctuation)
24
 */
25
define('PREG_CLASS_PUNCTUATION',
26
  '\x{21}-\x{23}\x{25}-\x{2a}\x{2c}-\x{2f}\x{3a}\x{3b}\x{3f}\x{40}\x{5b}-\x{5d}' .
27
  '\x{5f}\x{7b}\x{7d}\x{a1}\x{ab}\x{b7}\x{bb}\x{bf}\x{37e}\x{387}\x{55a}-\x{55f}' .
28
  '\x{589}\x{58a}\x{5be}\x{5c0}\x{5c3}\x{5f3}\x{5f4}\x{60c}\x{60d}\x{61b}\x{61f}' .
29
  '\x{66a}-\x{66d}\x{6d4}\x{700}-\x{70d}\x{964}\x{965}\x{970}\x{df4}\x{e4f}' .
30
  '\x{e5a}\x{e5b}\x{f04}-\x{f12}\x{f3a}-\x{f3d}\x{f85}\x{104a}-\x{104f}\x{10fb}' .
31
  '\x{1361}-\x{1368}\x{166d}\x{166e}\x{169b}\x{169c}\x{16eb}-\x{16ed}\x{1735}' .
32
  '\x{1736}\x{17d4}-\x{17d6}\x{17d8}-\x{17da}\x{1800}-\x{180a}\x{1944}\x{1945}' .
33
  '\x{2010}-\x{2027}\x{2030}-\x{2043}\x{2045}-\x{2051}\x{2053}\x{2054}\x{2057}' .
34
  '\x{207d}\x{207e}\x{208d}\x{208e}\x{2329}\x{232a}\x{23b4}-\x{23b6}\x{2768}-' .
35
  '\x{2775}\x{27e6}-\x{27eb}\x{2983}-\x{2998}\x{29d8}-\x{29db}\x{29fc}\x{29fd}' .
36
  '\x{3001}-\x{3003}\x{3008}-\x{3011}\x{3014}-\x{301f}\x{3030}\x{303d}\x{30a0}' .
37
  '\x{30fb}\x{fd3e}\x{fd3f}\x{fe30}-\x{fe52}\x{fe54}-\x{fe61}\x{fe63}\x{fe68}' .
38
  '\x{fe6a}\x{fe6b}\x{ff01}-\x{ff03}\x{ff05}-\x{ff0a}\x{ff0c}-\x{ff0f}\x{ff1a}' .
39
  '\x{ff1b}\x{ff1f}\x{ff20}\x{ff3b}-\x{ff3d}\x{ff3f}\x{ff5b}\x{ff5d}\x{ff5f}-' .
40
  '\x{ff65}');
41
42
/**
43
 * Matches CJK (Chinese, Japanese, Korean) letter-like characters.
44
 *
45
 * This list is derived from the "East Asian Scripts" section of
46
 * http://www.unicode.org/charts/index.html, as well as a comment on
47
 * http://unicode.org/reports/tr11/tr11-11.html listing some character
48
 * ranges that are reserved for additional CJK ideographs.
49
 *
50
 * The character ranges do not include numbers, punctuation, or symbols, since
51
 * these are handled separately in search. Note that radicals and strokes are
52
 * considered symbols. (See
53
 * http://www.unicode.org/Public/UNIDATA/extracted/DerivedGeneralCategory.txt)
54
 *
55
 * @see search_expand_cjk()
56
 */
57
define('PREG_CLASS_CJK', '\x{1100}-\x{11FF}\x{3040}-\x{309F}\x{30A1}-\x{318E}' .
58
  '\x{31A0}-\x{31B7}\x{31F0}-\x{31FF}\x{3400}-\x{4DBF}\x{4E00}-\x{9FCF}' .
59
  '\x{A000}-\x{A48F}\x{A4D0}-\x{A4FD}\x{A960}-\x{A97F}\x{AC00}-\x{D7FF}' .
60
  '\x{F900}-\x{FAFF}\x{FF21}-\x{FF3A}\x{FF41}-\x{FF5A}\x{FF66}-\x{FFDC}' .
61
  '\x{20000}-\x{2FFFD}\x{30000}-\x{3FFFD}');
62
63
/**
64
 * Implements hook_help().
65
 */
66
function search_help($path, $arg) {
67
  switch ($path) {
68
    case 'admin/help#search':
69
      $output = '';
70
      $output .= '<h3>' . t('About') . '</h3>';
71
      $output .= '<p>' . t('The Search module provides the ability to index and search for content by exact keywords, and for users by username or e-mail. For more information, see the online handbook entry for <a href="@search-module">Search module</a>.', array('@search-module' => 'http://drupal.org/documentation/modules/search/', '@search' => url('search'))) . '</p>';
72
      $output .= '<h3>' . t('Uses') . '</h3>';
73
      $output .= '<dl>';
74
      $output .= '<dt>' . t('Searching content and users') . '</dt>';
75
      $output .= '<dd>' . t('Users with <em>Use search</em> permission can use the search block and <a href="@search">Search page</a>. Users with the <em>View published content</em> permission can search for content containing exact keywords. Users with the <em>View user profiles</em> permission can search for users containing the keyword anywhere in the user name, and users with the <em>Administer users</em> permission can search for users by email address. Additionally, users with <em>Use advanced search</em> permission can find content using more complex search methods and filtering by choosing the <em>Advanced search</em> option on the <a href="@search">Search page</a>.', array('@search' => url('search'))) . '</dd>';
76
      $output .= '<dt>' . t('Indexing content with cron') . '</dt>';
77
      $output .= '<dd>' . t('To provide keyword searching, the search engine maintains an index of words found in the content and its fields, along with text added to your content by other modules (such as comments from the core Comment module, and taxonomy terms from the core Taxonomy module). To build and maintain this index, a correctly configured <a href="@cron">cron maintenance task</a> is required. Users with <em>Administer search</em> permission can further configure the cron settings on the <a href="@searchsettings">Search settings page</a>.', array('@cron' => 'http://drupal.org/cron', '@searchsettings' => url('admin/config/search/settings'))) . '</dd>';
78
      $output .= '<dt>' . t('Content reindexing') . '</dt>';
79
      $output .= '<dd>' . t('Content-related actions on your site (creating, editing, or deleting content and comments) automatically cause affected content items to be marked for indexing or reindexing at the next cron run. When content is marked for reindexing, the previous content remains in the index until cron runs, at which time it is replaced by the new content. Unlike content-related actions, actions related to the structure of your site do not cause affected content to be marked for reindexing. Examples of structure-related actions that affect content include deleting or editing taxonomy terms, enabling or disabling modules that add text to content (such as Taxonomy, Comment, and field-providing modules), and modifying the fields or display parameters of your content types. If you take one of these actions and you want to ensure that the search index is updated to reflect your changed site structure, you can mark all content for reindexing by clicking the "Re-index site" button on the <a href="@searchsettings">Search settings page</a>. If you have a lot of content on your site, it may take several cron runs for the content to be reindexed.', array('@searchsettings' => url('admin/config/search/settings'))) . '</dd>';
80
      $output .= '<dt>' . t('Configuring search settings') . '</dt>';
81
      $output .= '<dd>' . t('Indexing behavior can be adjusted using the <a href="@searchsettings">Search settings page</a>. Users with <em>Administer search</em> permission can control settings such as the <em>Number of items to index per cron run</em>, <em>Indexing settings</em> (word length), <em>Active search modules</em>, and <em>Content ranking</em>, which lets you adjust the priority in which indexed content is returned in results.', array('@searchsettings' => url('admin/config/search/settings'))) . '</dd>';
82
      $output .= '<dt>' . t('Search block') . '</dt>';
83
      $output .= '<dd>' . t('The Search module includes a default <em>Search form</em> block, which can be enabled and configured on the <a href="@blocks">Blocks administration page</a>. The block is available to users with the <em>Search content</em> permission.', array('@blocks' => url('admin/structure/block'))) . '</dd>';
84
      $output .= '<dt>' . t('Extending Search module') . '</dt>';
85
      $output .= '<dd>' . t('By default, the Search module only supports exact keyword matching in content searches. You can modify this behavior by installing a language-specific stemming module for your language (such as <a href="http://drupal.org/project/porterstemmer">Porter Stemmer</a> for American English), which allows words such as walk, walking, and walked to be matched in the Search module. Another approach is to use a third-party search technology with stemming or partial word matching features built in, such as <a href="http://drupal.org/project/apachesolr">Apache Solr</a> or <a href="http://drupal.org/project/sphinx">Sphinx</a>. These and other <a href="@contrib-search">search-related contributed modules</a> can be downloaded by visiting Drupal.org.', array('@contrib-search' => 'http://drupal.org/project/modules?filters=tid%3A105')) . '</dd>';
86
      $output .= '</dl>';
87
      return $output;
88
    case 'admin/config/search/settings':
89
      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>';
90
    case 'search#noresults':
91
      return t('<ul>
92
<li>Check if your spelling is correct.</li>
93
<li>Remove quotes around phrases to search for each word individually. <em>bike shed</em> will often show more results than <em>&quot;bike shed&quot;</em>.</li>
94
<li>Consider loosening your query with <em>OR</em>. <em>bike OR shed</em> will often show more results than <em>bike shed</em>.</li>
95
</ul>');
96
  }
97
}
98
99
/**
100
 * Implements hook_theme().
101
 */
102
function search_theme() {
103
  return array(
104
    'search_block_form' => array(
105
      'render element' => 'form',
106
      'template' => 'search-block-form',
107
    ),
108
    'search_result' => array(
109
      'variables' => array('result' => NULL, 'module' => NULL),
110
      'file' => 'search.pages.inc',
111
      'template' => 'search-result',
112
    ),
113
    'search_results' => array(
114
      'variables' => array('results' => NULL, 'module' => NULL),
115
      'file' => 'search.pages.inc',
116
      'template' => 'search-results',
117
    ),
118
  );
119
}
120
121
/**
122
 * Implements hook_permission().
123
 */
124
function search_permission() {
125
  return array(
126
    'administer search' => array(
127
      'title' => t('Administer search'),
128
    ),
129
    'search content' => array(
130
      'title' => t('Use search'),
131
    ),
132
    'use advanced search' => array(
133
      'title' => t('Use advanced search'),
134
    ),
135
  );
136
}
137
138
/**
139
 * Implements hook_block_info().
140
 */
141
function search_block_info() {
142
  $blocks['form']['info'] = t('Search form');
143
  // Not worth caching.
144
  $blocks['form']['cache'] = DRUPAL_NO_CACHE;
145
  $blocks['form']['properties']['administrative'] = TRUE;
146
  return $blocks;
147
}
148
149
/**
150
 * Implements hook_block_view().
151
 */
152
function search_block_view($delta = '') {
153
  if (user_access('search content')) {
154
    $block['content'] = drupal_get_form('search_block_form');
155
    return $block;
156
  }
157
}
158
159
/**
160
 * Implements hook_menu().
161
 */
162
function search_menu() {
163
  $items['search'] = array(
164
    'title' => 'Search',
165
    'page callback' => 'search_view',
166
    'access callback' => 'search_is_active',
167
    'type' => MENU_SUGGESTED_ITEM,
168
    'file' => 'search.pages.inc',
169
  );
170
  $items['admin/config/search/settings'] = array(
171
    'title' => 'Search settings',
172
    'description' => 'Configure relevance settings for search and other indexing options.',
173
    'page callback' => 'drupal_get_form',
174
    'page arguments' => array('search_admin_settings'),
175
    'access arguments' => array('administer search'),
176
    'weight' => -10,
177
    'file' => 'search.admin.inc',
178
  );
179
  $items['admin/config/search/settings/reindex'] = array(
180
    'title' => 'Clear index',
181
    'page callback' => 'drupal_get_form',
182
    'page arguments' => array('search_reindex_confirm'),
183
    'access arguments' => array('administer search'),
184
    'type' => MENU_VISIBLE_IN_BREADCRUMB,
185
    'file' => 'search.admin.inc',
186
  );
187
188
  // Add paths for searching. We add each module search path twice: once without
189
  // and once with %menu_tail appended. The reason for this is that we want to
190
  // preserve keywords when switching tabs, and also to have search tabs
191
  // highlighted properly. The only way to do that within the Drupal menu
192
  // system appears to be having two sets of tabs. See discussion on issue
193
  // http://drupal.org/node/245103 for details.
194
195
  drupal_static_reset('search_get_info');
196
  $default_info = search_get_default_module_info();
197
  if ($default_info) {
198
    foreach (search_get_info() as $module => $search_info) {
199
      $path = 'search/' . $search_info['path'];
200
      $items[$path] = array(
201
        'title' => $search_info['title'],
202
        'page callback' => 'search_view',
203
        'page arguments' => array($module, ''),
204
        'access callback' => '_search_menu_access',
205
        'access arguments' => array($module),
206
        'type' => MENU_LOCAL_TASK,
207
        'file' => 'search.pages.inc',
208
        'weight' => $module == $default_info['module'] ? -10 : 0,
209
      );
210
      $items["$path/%menu_tail"] = array(
211
        'title' => $search_info['title'],
212
        'load arguments' => array('%map', '%index'),
213
        'page callback' => 'search_view',
214
        'page arguments' => array($module, 2),
215
        'access callback' => '_search_menu_access',
216
        'access arguments' => array($module),
217
        // The default local task points to its parent, but this item points to
218
        // where it should so it should not be changed.
219
        'type' => MENU_LOCAL_TASK,
220
        'file' => 'search.pages.inc',
221
        'weight' => 0,
222
        // These tabs are not subtabs.
223
        'tab_root' => 'search/' . $default_info['path'] . '/%',
224
        // These tabs need to display at the same level.
225
        'tab_parent' => 'search/' . $default_info['path'],
226
      );
227
    }
228
  }
229
  return $items;
230
}
231
232
/**
233
 * Determines access for the ?q=search path.
234
 */
235
function search_is_active() {
236
  // This path cannot be accessed if there are no active modules.
237
  return user_access('search content') && search_get_info();
238
}
239
240
/**
241
 * Returns information about available search modules.
242
 *
243
 * @param $all
244
 *   If TRUE, information about all enabled modules implementing
245
 *   hook_search_info() will be returned. If FALSE (default), only modules that
246
 *   have been set to active on the search settings page will be returned.
247
 *
248
 * @return
249
 *   Array of hook_search_info() return values, keyed by module name. The
250
 *   'title' and 'path' array elements will be set to defaults for each module
251
 *   if not supplied by hook_search_info(), and an additional array element of
252
 *   'module' will be added (set to the module name).
253
 */
254
function search_get_info($all = FALSE) {
255
  $search_hooks = &drupal_static(__FUNCTION__);
256
257
  if (!isset($search_hooks)) {
258
    foreach (module_implements('search_info') as $module) {
259
      $search_hooks[$module] = call_user_func($module . '_search_info');
260
      // Use module name as the default value.
261
      $search_hooks[$module] += array('title' => $module, 'path' => $module);
262
      // Include the module name itself in the array.
263
      $search_hooks[$module]['module'] = $module;
264
    }
265
  }
266
267
  if ($all) {
268
    return $search_hooks;
269
  }
270
271
  $active = variable_get('search_active_modules', array('node', 'user'));
272
  return array_intersect_key($search_hooks, array_flip($active));
273
}
274
275
/**
276
 * Returns information about the default search module.
277
 *
278
 * @return
279
 *    The search_get_info() array element for the default search module, if any.
280
 */
281
function search_get_default_module_info() {
282
  $info = search_get_info();
283
  $default = variable_get('search_default_module', 'node');
284
  if (isset($info[$default])) {
285
    return $info[$default];
286
  }
287
  // The variable setting does not match any active module, so just return
288
  // the info for the first active module (if any).
289
  return reset($info);
290
}
291
292
/**
293
 * Access callback for search tabs.
294
 */
295
function _search_menu_access($name) {
296
  return user_access('search content') && (!function_exists($name . '_search_access') || module_invoke($name, 'search_access'));
297
}
298
299
/**
300
 * Clears a part of or the entire search index.
301
 *
302
 * @param $sid
303
 *   (optional) The ID of the item to remove from the search index. If
304
 *   specified, $module must also be given. Omit both $sid and $module to clear
305
 *   the entire search index.
306
 * @param $module
307
 *   (optional) The machine-readable name of the module for the item to remove
308
 *   from the search index.
309
 */
310
function search_reindex($sid = NULL, $module = NULL, $reindex = FALSE) {
311
  if ($module == NULL && $sid == NULL) {
312
    module_invoke_all('search_reset');
313
  }
314
  else {
315
    db_delete('search_dataset')
316
      ->condition('sid', $sid)
317
      ->condition('type', $module)
318
      ->execute();
319
    db_delete('search_index')
320
      ->condition('sid', $sid)
321
      ->condition('type', $module)
322
      ->execute();
323
    // Don't remove links if re-indexing.
324
    if (!$reindex) {
325
      db_delete('search_node_links')
326
        ->condition('sid', $sid)
327
        ->condition('type', $module)
328
        ->execute();
329
    }
330
  }
331
}
332
333
/**
334
 * Marks a word as "dirty" (changed), or retrieves the list of dirty words.
335
 *
336
 * This is used during indexing (cron). Words that are dirty have outdated
337
 * total counts in the search_total table, and need to be recounted.
338
 */
339
function search_dirty($word = NULL) {
340
  $dirty = &drupal_static(__FUNCTION__, array());
341
  if ($word !== NULL) {
342
    $dirty[$word] = TRUE;
343
  }
344
  else {
345
    return $dirty;
346
  }
347
}
348
349
/**
350
 * Implements hook_cron().
351
 *
352
 * Fires hook_update_index() in all modules and cleans up dirty words.
353
 *
354
 * @see search_dirty()
355
 */
356
function search_cron() {
357
  // We register a shutdown function to ensure that search_total is always up
358
  // to date.
359
  drupal_register_shutdown_function('search_update_totals');
360
361
  foreach (variable_get('search_active_modules', array('node', 'user')) as $module) {
362
    // Update word index
363
    module_invoke($module, 'update_index');
364
  }
365
}
366
367
/**
368
 * Updates the {search_total} database table.
369
 *
370
 * This function is called on shutdown to ensure that {search_total} is always
371
 * up to date (even if cron times out or otherwise fails).
372
 */
373
function search_update_totals() {
374
  // Update word IDF (Inverse Document Frequency) counts for new/changed words.
375
  foreach (search_dirty() as $word => $dummy) {
376
    // Get total count
377
    $total = db_query("SELECT SUM(score) FROM {search_index} WHERE word = :word", array(':word' => $word), array('target' => 'slave'))->fetchField();
378
    // Apply Zipf's law to equalize the probability distribution.
379
    $total = log10(1 + 1/(max(1, $total)));
380
    db_merge('search_total')
381
      ->key(array('word' => $word))
382
      ->fields(array('count' => $total))
383
      ->execute();
384
  }
385
  // Find words that were deleted from search_index, but are still in
386
  // search_total. We use a LEFT JOIN between the two tables and keep only the
387
  // rows which fail to join.
388
  $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", array(), array('target' => 'slave'));
389
  $or = db_or();
390
  foreach ($result as $word) {
391
    $or->condition('word', $word->realword);
392
  }
393
  if (count($or) > 0) {
394
    db_delete('search_total')
395
      ->condition($or)
396
      ->execute();
397
  }
398
}
399
400
/**
401
 * Simplifies a string according to indexing rules.
402
 *
403
 * @param $text
404
 *   Text to simplify.
405
 *
406
 * @return
407
 *   Simplified text.
408
 *
409
 * @see hook_search_preprocess()
410
 */
411
function search_simplify($text) {
412
  // Decode entities to UTF-8
413
  $text = decode_entities($text);
414
415
  // Lowercase
416
  $text = drupal_strtolower($text);
417
418
  // Call an external processor for word handling.
419
  search_invoke_preprocess($text);
420
421
  // Simple CJK handling
422
  if (variable_get('overlap_cjk', TRUE)) {
423
    $text = preg_replace_callback('/[' . PREG_CLASS_CJK . ']+/u', 'search_expand_cjk', $text);
424
  }
425
426
  // To improve searching for numerical data such as dates, IP addresses
427
  // or version numbers, we consider a group of numerical characters
428
  // separated only by punctuation characters to be one piece.
429
  // This also means that searching for e.g. '20/03/1984' also returns
430
  // results with '20-03-1984' in them.
431
  // Readable regexp: ([number]+)[punctuation]+(?=[number])
432
  $text = preg_replace('/([' . PREG_CLASS_NUMBERS . ']+)[' . PREG_CLASS_PUNCTUATION . ']+(?=[' . PREG_CLASS_NUMBERS . '])/u', '\1', $text);
433
434
  // Multiple dot and dash groups are word boundaries and replaced with space.
435
  // No need to use the unicode modifer here because 0-127 ASCII characters
436
  // can't match higher UTF-8 characters as the leftmost bit of those are 1.
437
  $text = preg_replace('/[.-]{2,}/', ' ', $text);
438
439
  // The dot, underscore and dash are simply removed. This allows meaningful
440
  // search behavior with acronyms and URLs. See unicode note directly above.
441
  $text = preg_replace('/[._-]+/', '', $text);
442
443
  // With the exception of the rules above, we consider all punctuation,
444
  // marks, spacers, etc, to be a word boundary.
445
  $text = preg_replace('/[' . PREG_CLASS_UNICODE_WORD_BOUNDARY . ']+/u', ' ', $text);
446
447
  // Truncate everything to 50 characters.
448
  $words = explode(' ', $text);
449
  array_walk($words, '_search_index_truncate');
450
  $text = implode(' ', $words);
451
452
  return $text;
453
}
454
455
/**
456
 * Splits CJK (Chinese, Japanese, Korean) text into tokens.
457
 *
458
 * The Search module matches exact words, where a word is defined to be a
459
 * sequence of characters delimited by spaces or punctuation. CJK languages are
460
 * written in long strings of characters, though, not split up into words. So
461
 * in order to allow search matching, we split up CJK text into tokens
462
 * consisting of consecutive, overlapping sequences of characters whose length
463
 * is equal to the 'minimum_word_size' variable. This tokenizing is only done if
464
 * the 'overlap_cjk' variable is TRUE.
465
 *
466
 * @param $matches
467
 *   This function is a callback for preg_replace_callback(), which is called
468
 *   from search_simplify(). So, $matches is an array of regular expression
469
 *   matches, which means that $matches[0] contains the matched text -- a string
470
 *   of CJK characters to tokenize.
471
 *
472
 * @return
473
 *   Tokenized text, starting and ending with a space character.
474
 */
475
function search_expand_cjk($matches) {
476
  $min = variable_get('minimum_word_size', 3);
477
  $str = $matches[0];
478
  $length = drupal_strlen($str);
479
  // If the text is shorter than the minimum word size, don't tokenize it.
480
  if ($length <= $min) {
481
    return ' ' . $str . ' ';
482
  }
483
  $tokens = ' ';
484
  // Build a FIFO queue of characters.
485
  $chars = array();
486
  for ($i = 0; $i < $length; $i++) {
487
    // Add the next character off the beginning of the string to the queue.
488
    $current = drupal_substr($str, 0, 1);
489
    $str = substr($str, strlen($current));
490
    $chars[] = $current;
491
    if ($i >= $min - 1) {
492
      // Make a token of $min characters, and add it to the token string.
493
      $tokens .= implode('', $chars) . ' ';
494
      // Shift out the first character in the queue.
495
      array_shift($chars);
496
    }
497
  }
498
  return $tokens;
499
}
500
501
/**
502
 * Simplifies and splits a string into tokens for indexing.
503
 */
504
function search_index_split($text) {
505
  $last = &drupal_static(__FUNCTION__);
506
  $lastsplit = &drupal_static(__FUNCTION__ . ':lastsplit');
507
508
  if ($last == $text) {
509
    return $lastsplit;
510
  }
511
  // Process words
512
  $text = search_simplify($text);
513
  $words = explode(' ', $text);
514
515
  // Save last keyword result
516
  $last = $text;
517
  $lastsplit = $words;
518
519
  return $words;
520
}
521
522
/**
523
 * Helper function for array_walk in search_index_split.
524
 */
525
function _search_index_truncate(&$text) {
526
  if (is_numeric($text)) {
527
    $text = ltrim($text, '0');
528
  }
529
  $text = truncate_utf8($text, 50);
530
}
531
532
/**
533
 * Invokes hook_search_preprocess() in modules.
534
 */
535
function search_invoke_preprocess(&$text) {
536
  foreach (module_implements('search_preprocess') as $module) {
537
    $text = module_invoke($module, 'search_preprocess', $text);
538
  }
539
}
540
541
/**
542
 * Update the full-text search index for a particular item.
543
 *
544
 * @param $sid
545
 *   An ID number identifying this particular item (e.g., node ID).
546
 * @param $module
547
 *   The machine-readable name of the module that this item comes from (a module
548
 *   that implements hook_search_info()).
549
 * @param $text
550
 *   The content of this item. Must be a piece of HTML or plain text.
551
 *
552
 * @ingroup search
553
 */
554
function search_index($sid, $module, $text) {
555
  $minimum_word_size = variable_get('minimum_word_size', 3);
556
557
  // Link matching
558
  global $base_url;
559
  $node_regexp = '@href=[\'"]?(?:' . preg_quote($base_url, '@') . '/|' . preg_quote(base_path(), '@') . ')(?:\?q=)?/?((?![a-z]+:)[^\'">]+)[\'">]@i';
560
561
  // Multipliers for scores of words inside certain HTML tags. The weights are stored
562
  // in a variable so that modules can overwrite the default weights.
563
  // Note: 'a' must be included for link ranking to work.
564
  $tags = variable_get('search_tag_weights', array(
565
    'h1' => 25,
566
    'h2' => 18,
567
    'h3' => 15,
568
    'h4' => 12,
569
    'h5' => 9,
570
    'h6' => 6,
571
    'u' => 3,
572
    'b' => 3,
573
    'i' => 3,
574
    'strong' => 3,
575
    'em' => 3,
576
    'a' => 10));
577
578
  // Strip off all ignored tags to speed up processing, but insert space before/after
579
  // them to keep word boundaries.
580
  $text = str_replace(array('<', '>'), array(' <', '> '), $text);
581
  $text = strip_tags($text, '<' . implode('><', array_keys($tags)) . '>');
582
583
  // Split HTML tags from plain text.
584
  $split = preg_split('/\s*<([^>]+?)>\s*/', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
585
  // Note: PHP ensures the array consists of alternating delimiters and literals
586
  // and begins and ends with a literal (inserting $null as required).
587
588
  $tag = FALSE; // Odd/even counter. Tag or no tag.
589
  $link = FALSE; // State variable for link analyzer
590
  $score = 1; // Starting score per word
591
  $accum = ' '; // Accumulator for cleaned up data
592
  $tagstack = array(); // Stack with open tags
593
  $tagwords = 0; // Counter for consecutive words
594
  $focus = 1; // Focus state
595
596
  $results = array(0 => array()); // Accumulator for words for index
597
598
  foreach ($split as $value) {
599
    if ($tag) {
600
      // Increase or decrease score per word based on tag
601
      list($tagname) = explode(' ', $value, 2);
602
      $tagname = drupal_strtolower($tagname);
603
      // Closing or opening tag?
604
      if ($tagname[0] == '/') {
605
        $tagname = substr($tagname, 1);
606
        // If we encounter unexpected tags, reset score to avoid incorrect boosting.
607
        if (!count($tagstack) || $tagstack[0] != $tagname) {
608
          $tagstack = array();
609
          $score = 1;
610
        }
611
        else {
612
          // Remove from tag stack and decrement score
613
          $score = max(1, $score - $tags[array_shift($tagstack)]);
614
        }
615
        if ($tagname == 'a') {
616
          $link = FALSE;
617
        }
618
      }
619
      else {
620
        if (isset($tagstack[0]) && $tagstack[0] == $tagname) {
621
          // None of the tags we look for make sense when nested identically.
622
          // If they are, it's probably broken HTML.
623
          $tagstack = array();
624
          $score = 1;
625
        }
626
        else {
627
          // Add to open tag stack and increment score
628
          array_unshift($tagstack, $tagname);
629
          $score += $tags[$tagname];
630
        }
631
        if ($tagname == 'a') {
632
          // Check if link points to a node on this site
633
          if (preg_match($node_regexp, $value, $match)) {
634
            $path = drupal_get_normal_path($match[1]);
635
            if (preg_match('!(?:node|book)/(?:view/)?([0-9]+)!i', $path, $match)) {
636
              $linknid = $match[1];
637
              if ($linknid > 0) {
638
                $node = db_query('SELECT title, nid, vid FROM {node} WHERE nid = :nid', array(':nid' => $linknid), array('target' => 'slave'))->fetchObject();
639
                $link = TRUE;
640
                $linktitle = $node->title;
641
              }
642
            }
643
          }
644
        }
645
      }
646
      // A tag change occurred, reset counter.
647
      $tagwords = 0;
648
    }
649
    else {
650
      // Note: use of PREG_SPLIT_DELIM_CAPTURE above will introduce empty values
651
      if ($value != '') {
652
        if ($link) {
653
          // Check to see if the node link text is its URL. If so, we use the target node title instead.
654
          if (preg_match('!^https?://!i', $value)) {
655
            $value = $linktitle;
656
          }
657
        }
658
        $words = search_index_split($value);
659
        foreach ($words as $word) {
660
          // Add word to accumulator
661
          $accum .= $word . ' ';
662
          // Check wordlength
663
          if (is_numeric($word) || drupal_strlen($word) >= $minimum_word_size) {
664
            // Links score mainly for the target.
665
            if ($link) {
666
              if (!isset($results[$linknid])) {
667
                $results[$linknid] = array();
668
              }
669
              $results[$linknid][] = $word;
670
              // Reduce score of the link caption in the source.
671
              $focus *= 0.2;
672
            }
673
            // Fall-through
674
            if (!isset($results[0][$word])) {
675
              $results[0][$word] = 0;
676
            }
677
            $results[0][$word] += $score * $focus;
678
679
            // Focus is a decaying value in terms of the amount of unique words up to this point.
680
            // From 100 words and more, it decays, to e.g. 0.5 at 500 words and 0.3 at 1000 words.
681
            $focus = min(1, .01 + 3.5 / (2 + count($results[0]) * .015));
682
          }
683
          $tagwords++;
684
          // Too many words inside a single tag probably mean a tag was accidentally left open.
685
          if (count($tagstack) && $tagwords >= 15) {
686
            $tagstack = array();
687
            $score = 1;
688
          }
689
        }
690
      }
691
    }
692
    $tag = !$tag;
693
  }
694
695
  search_reindex($sid, $module, TRUE);
696
697
  // Insert cleaned up data into dataset
698
  db_insert('search_dataset')
699
    ->fields(array(
700
      'sid' => $sid,
701
      'type' => $module,
702
      'data' => $accum,
703
      'reindex' => 0,
704
    ))
705
    ->execute();
706
707
  // Insert results into search index
708
  foreach ($results[0] as $word => $score) {
709
    // If a word already exists in the database, its score gets increased
710
    // appropriately. If not, we create a new record with the appropriate
711
    // starting score.
712
    db_merge('search_index')
713
      ->key(array(
714
        'word' => $word,
715
        'sid' => $sid,
716
        'type' => $module,
717
      ))
718
      ->fields(array('score' => $score))
719
      ->expression('score', 'score + :score', array(':score' => $score))
720
      ->execute();
721
    search_dirty($word);
722
  }
723
  unset($results[0]);
724
725
  // Get all previous links from this item.
726
  $result = db_query("SELECT nid, caption FROM {search_node_links} WHERE sid = :sid AND type = :type", array(
727
    ':sid' => $sid,
728
    ':type' => $module
729
  ), array('target' => 'slave'));
730
  $links = array();
731
  foreach ($result as $link) {
732
    $links[$link->nid] = $link->caption;
733
  }
734
735
  // Now store links to nodes.
736
  foreach ($results as $nid => $words) {
737
    $caption = implode(' ', $words);
738
    if (isset($links[$nid])) {
739
      if ($links[$nid] != $caption) {
740
        // Update the existing link and mark the node for reindexing.
741
        db_update('search_node_links')
742
          ->fields(array('caption' => $caption))
743
          ->condition('sid', $sid)
744
          ->condition('type', $module)
745
          ->condition('nid', $nid)
746
          ->execute();
747
        search_touch_node($nid);
748
      }
749
      // Unset the link to mark it as processed.
750
      unset($links[$nid]);
751
    }
752
    elseif ($sid != $nid || $module != 'node') {
753
      // Insert the existing link and mark the node for reindexing, but don't
754
      // reindex if this is a link in a node pointing to itself.
755
      db_insert('search_node_links')
756
        ->fields(array(
757
          'caption' => $caption,
758
          'sid' => $sid,
759
          'type' => $module,
760
          'nid' => $nid,
761
        ))
762
        ->execute();
763
      search_touch_node($nid);
764
    }
765
  }
766
  // Any left-over links in $links no longer exist. Delete them and mark the nodes for reindexing.
767
  foreach ($links as $nid => $caption) {
768
    db_delete('search_node_links')
769
      ->condition('sid', $sid)
770
      ->condition('type', $module)
771
      ->condition('nid', $nid)
772
      ->execute();
773
    search_touch_node($nid);
774
  }
775
}
776
777
/**
778
 * Changes a node's changed timestamp to 'now' to force reindexing.
779
 *
780
 * @param $nid
781
 *   The node ID of the node that needs reindexing.
782
 */
783
function search_touch_node($nid) {
784
  db_update('search_dataset')
785
    ->fields(array('reindex' => REQUEST_TIME))
786
    ->condition('type', 'node')
787
    ->condition('sid', $nid)
788
    ->execute();
789
}
790
791
/**
792
 * Implements hook_node_update_index().
793
 */
794
function search_node_update_index($node) {
795
  // Transplant links to a node into the target node.
796
  $result = db_query("SELECT caption FROM {search_node_links} WHERE nid = :nid", array(':nid' => $node->nid), array('target' => 'slave'));
797
  $output = array();
798
  foreach ($result as $link) {
799
    $output[] = $link->caption;
800
  }
801
  if (count($output)) {
802
    return '<a>(' . implode(', ', $output) . ')</a>';
803
  }
804
}
805
806
/**
807
 * Implements hook_node_update().
808
 */
809
function search_node_update($node) {
810
  // Reindex the node when it is updated. The node is automatically indexed
811
  // when it is added, simply by being added to the node table.
812
  search_touch_node($node->nid);
813
}
814
815
/**
816
 * Implements hook_comment_insert().
817
 */
818
function search_comment_insert($comment) {
819
  // Reindex the node when comments are added.
820
  search_touch_node($comment->nid);
821
}
822
823
/**
824
 * Implements hook_comment_update().
825
 */
826
function search_comment_update($comment) {
827
  // Reindex the node when comments are changed.
828
  search_touch_node($comment->nid);
829
}
830
831
/**
832
 * Implements hook_comment_delete().
833
 */
834
function search_comment_delete($comment) {
835
  // Reindex the node when comments are deleted.
836
  search_touch_node($comment->nid);
837
}
838
839
/**
840
 * Implements hook_comment_publish().
841
 */
842
function search_comment_publish($comment) {
843
  // Reindex the node when comments are published.
844
  search_touch_node($comment->nid);
845
}
846
847
/**
848
 * Implements hook_comment_unpublish().
849
 */
850
function search_comment_unpublish($comment) {
851
  // Reindex the node when comments are unpublished.
852
  search_touch_node($comment->nid);
853
}
854
855
/**
856
 * Extracts a module-specific search option from a search expression.
857
 *
858
 * Search options are added using search_expression_insert(), and retrieved
859
 * using search_expression_extract(). They take the form option:value, and
860
 * are added to the ordinary keywords in the search expression.
861
 *
862
 * @param $expression
863
 *   The search expression to extract from.
864
 * @param $option
865
 *   The name of the option to retrieve from the search expression.
866
 *
867
 * @return
868
 *   The value previously stored in the search expression for option $option,
869
 *   if any. Trailing spaces in values will not be included.
870
 */
871
function search_expression_extract($expression, $option) {
872
  if (preg_match('/(^| )' . $option . ':([^ ]*)( |$)/i', $expression, $matches)) {
873
    return $matches[2];
874
  }
875
}
876
877
/**
878
 * Adds a module-specific search option to a search expression.
879
 *
880
 * Search options are added using search_expression_insert(), and retrieved
881
 * using search_expression_extract(). They take the form option:value, and
882
 * are added to the ordinary keywords in the search expression.
883
 *
884
 * @param $expression
885
 *   The search expression to add to.
886
 * @param $option
887
 *   The name of the option to add to the search expression.
888
 * @param $value
889
 *   The value to add for the option. If present, it will replace any previous
890
 *   value added for the option. Cannot contain any spaces or | characters, as
891
 *   these are used as delimiters. If you want to add a blank value $option: to
892
 *   the search expression, pass in an empty string or a string that is composed
893
 *   of only spaces. To clear a previously-stored option without adding a
894
 *   replacement, pass in NULL for $value or omit.
895
 *
896
 * @return
897
 *   $expression, with any previous value for this option removed, and a new
898
 *   $option:$value pair added if $value was provided.
899
 */
900
function search_expression_insert($expression, $option, $value = NULL) {
901
  // Remove any previous values stored with $option.
902
  $expression = trim(preg_replace('/(^| )' . $option . ':[^ ]*/i', '', $expression));
903
904
  // Set new value, if provided.
905
  if (isset($value)) {
906
    $expression .= ' ' . $option . ':' . trim($value);
907
  }
908
  return $expression;
909
}
910
911
/**
912
 * @defgroup search Search interface
913
 * @{
914
 * The Drupal search interface manages a global search mechanism.
915
 *
916
 * Modules may plug into this system to provide searches of different types of
917
 * data. Most of the system is handled by search.module, so this must be enabled
918
 * for all of the search features to work.
919
 *
920
 * There are three ways to interact with the search system:
921
 * - Specifically for searching nodes, you can implement
922
 *   hook_node_update_index() and hook_node_search_result(). However, note that
923
 *   the search system already indexes all visible output of a node; i.e.,
924
 *   everything displayed normally by hook_view() and hook_node_view(). This is
925
 *   usually sufficient. You should only use this mechanism if you want
926
 *   additional, non-visible data to be indexed.
927
 * - Implement hook_search_info(). This will create a search tab for your module
928
 *   on the /search page with a simple keyword search form. You will also need
929
 *   to implement hook_search_execute() to perform the search.
930
 * - Implement hook_update_index(). This allows your module to use Drupal's
931
 *   HTML indexing mechanism for searching full text efficiently.
932
 *
933
 * If your module needs to provide a more complicated search form, then you need
934
 * to implement it yourself without hook_search_info(). In that case, you should
935
 * define it as a local task (tab) under the /search page (e.g. /search/mymodule)
936
 * so that users can easily find it.
937
 */
938
939
/**
940
 * Builds a search form.
941
 *
942
 * @param $action
943
 *   Form action. Defaults to "search/$path", where $path is the search path
944
 *   associated with the module in its hook_search_info(). This will be
945
 *   run through url().
946
 * @param $keys
947
 *   The search string entered by the user, containing keywords for the search.
948
 * @param $module
949
 *   The search module to render the form for: a module that implements
950
 *   hook_search_info(). If not supplied, the default search module is used.
951
 * @param $prompt
952
 *   Label for the keywords field. Defaults to t('Enter your keywords') if NULL.
953
 *   Supply '' to omit.
954
 *
955
 * @return
956
 *   A Form API array for the search form.
957
 */
958
function search_form($form, &$form_state, $action = '', $keys = '', $module = NULL, $prompt = NULL) {
959
  $module_info = FALSE;
960
  if (!$module) {
961
    $module_info = search_get_default_module_info();
962
  }
963
  else {
964
    $info = search_get_info();
965
    $module_info = isset($info[$module]) ? $info[$module] : FALSE;
966
  }
967
968
  // Sanity check.
969
  if (!$module_info) {
970
    form_set_error(NULL, t('Search is currently disabled.'), 'error');
971
    return $form;
972
  }
973
974
  if (!$action) {
975
    $action = 'search/' . $module_info['path'];
976
  }
977
  if (!isset($prompt)) {
978
    $prompt = t('Enter your keywords');
979
  }
980
981
  $form['#action'] = url($action);
982
  // Record the $action for later use in redirecting.
983
  $form_state['action'] = $action;
984
  $form['#attributes']['class'][] = 'search-form';
985
  $form['module'] = array('#type' => 'value', '#value' => $module);
986
  $form['basic'] = array('#type' => 'container', '#attributes' => array('class' => array('container-inline')));
987
  $form['basic']['keys'] = array(
988
    '#type' => 'textfield',
989
    '#title' => $prompt,
990
    '#default_value' => $keys,
991
    '#size' => $prompt ? 40 : 20,
992
    '#maxlength' => 255,
993
  );
994
  // processed_keys is used to coordinate keyword passing between other forms
995
  // that hook into the basic search form.
996
  $form['basic']['processed_keys'] = array('#type' => 'value', '#value' => '');
997
  $form['basic']['submit'] = array('#type' => 'submit', '#value' => t('Search'));
998
999
  return $form;
1000
}
1001
1002
/**
1003
 * Form builder; Output a search form for the search block's search box.
1004
 *
1005
 * @ingroup forms
1006
 * @see search_box_form_submit()
1007
 * @see search-block-form.tpl.php
1008
 */
1009
function search_box($form, &$form_state, $form_id) {
1010
  $form[$form_id] = array(
1011
    '#type' => 'textfield',
1012
    '#title' => t('Search'),
1013
    '#title_display' => 'invisible',
1014
    '#size' => 15,
1015
    '#default_value' => '',
1016
    '#attributes' => array('title' => t('Enter the terms you wish to search for.')),
1017
  );
1018
  $form['actions'] = array('#type' => 'actions');
1019
  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Search'));
1020
  $form['#submit'][] = 'search_box_form_submit';
1021
1022
  return $form;
1023
}
1024
1025
/**
1026
 * Process a block search form submission.
1027
 */
1028
function search_box_form_submit($form, &$form_state) {
1029
  // The search form relies on control of the redirect destination for its
1030
  // functionality, so we override any static destination set in the request,
1031
  // for example by drupal_access_denied() or drupal_not_found()
1032
  // (see http://drupal.org/node/292565).
1033
  if (isset($_GET['destination'])) {
1034
    unset($_GET['destination']);
1035
  }
1036
1037
  // Check to see if the form was submitted empty.
1038
  // If it is empty, display an error message.
1039
  // (This method is used instead of setting #required to TRUE for this field
1040
  // because that results in a confusing error message.  It would say a plain
1041
  // "field is required" because the search keywords field has no title.
1042
  // The error message would also complain about a missing #title field.)
1043
  if ($form_state['values']['search_block_form'] == '') {
1044
    form_set_error('keys', t('Please enter some keywords.'));
1045
  }
1046
1047
  $form_id = $form['form_id']['#value'];
1048
  $info = search_get_default_module_info();
1049
  if ($info) {
1050
    $form_state['redirect'] = 'search/' . $info['path'] . '/' . trim($form_state['values'][$form_id]);
1051
  }
1052
  else {
1053
    form_set_error(NULL, t('Search is currently disabled.'), 'error');
1054
  }
1055
}
1056
1057
/**
1058
 * Process variables for search-block-form.tpl.php.
1059
 *
1060
 * The $variables array contains the following arguments:
1061
 * - $form
1062
 *
1063
 * @see search-block-form.tpl.php
1064
 */
1065
function template_preprocess_search_block_form(&$variables) {
1066
  $variables['search'] = array();
1067
  $hidden = array();
1068
  // Provide variables named after form keys so themers can print each element independently.
1069
  foreach (element_children($variables['form']) as $key) {
1070
    $type = isset($variables['form'][$key]['#type']) ? $variables['form'][$key]['#type'] : '';
1071
    if ($type == 'hidden' || $type == 'token') {
1072
      $hidden[] = drupal_render($variables['form'][$key]);
1073
    }
1074
    else {
1075
      $variables['search'][$key] = drupal_render($variables['form'][$key]);
1076
    }
1077
  }
1078
  // Hidden form elements have no value to themers. No need for separation.
1079
  $variables['search']['hidden'] = implode($hidden);
1080
  // Collect all form elements to make it easier to print the whole form.
1081
  $variables['search_form'] = implode($variables['search']);
1082
}
1083
1084
/**
1085
 * Performs a search by calling hook_search_execute().
1086
 *
1087
 * @param $keys
1088
 *   Keyword query to search on.
1089
 * @param $module
1090
 *   Search module to search.
1091
 * @param $conditions
1092
 *   Optional array of additional search conditions.
1093
 *
1094
 * @return
1095
 *   Renderable array of search results. No return value if $keys are not
1096
 *   supplied or if the given search module is not active.
1097
 */
1098
function search_data($keys, $module, $conditions = NULL) {
1099
  if (module_hook($module, 'search_execute')) {
1100
    $results = module_invoke($module, 'search_execute', $keys, $conditions);
1101
    if (module_hook($module, 'search_page')) {
1102
      return module_invoke($module, 'search_page', $results);
1103
    }
1104
    else {
1105
      return array(
1106
        '#theme' => 'search_results',
1107
        '#results' => $results,
1108
        '#module' => $module,
1109
      );
1110
    }
1111
  }
1112
}
1113
1114
/**
1115
 * Returns snippets from a piece of text, with certain keywords highlighted.
1116
 * Used for formatting search results.
1117
 *
1118
 * @param $keys
1119
 *   A string containing a search query.
1120
 *
1121
 * @param $text
1122
 *   The text to extract fragments from.
1123
 *
1124
 * @return
1125
 *   A string containing HTML for the excerpt.
1126
 */
1127
function search_excerpt($keys, $text) {
1128
  // We highlight around non-indexable or CJK characters.
1129
  $boundary = '(?:(?<=[' . PREG_CLASS_UNICODE_WORD_BOUNDARY . PREG_CLASS_CJK . '])|(?=[' . PREG_CLASS_UNICODE_WORD_BOUNDARY . PREG_CLASS_CJK . ']))';
1130
1131
  // Extract positive keywords and phrases
1132
  preg_match_all('/ ("([^"]+)"|(?!OR)([^" ]+))/', ' ' . $keys, $matches);
1133
  $keys = array_merge($matches[2], $matches[3]);
1134
1135
  // Prepare text by stripping HTML tags and decoding HTML entities.
1136
  $text = strip_tags(str_replace(array('<', '>'), array(' <', '> '), $text));
1137
  $text = decode_entities($text);
1138
1139
  // Slash-escape quotes in the search keyword string.
1140
  array_walk($keys, '_search_excerpt_replace');
1141
  $workkeys = $keys;
1142
1143
  // Extract fragments around keywords.
1144
  // First we collect ranges of text around each keyword, starting/ending
1145
  // at spaces, trying to get to 256 characters.
1146
  // If the sum of all fragments is too short, we look for second occurrences.
1147
  $ranges = array();
1148
  $included = array();
1149
  $foundkeys = array();
1150
  $length = 0;
1151
  while ($length < 256 && count($workkeys)) {
1152
    foreach ($workkeys as $k => $key) {
1153
      if (strlen($key) == 0) {
1154
        unset($workkeys[$k]);
1155
        unset($keys[$k]);
1156
        continue;
1157
      }
1158
      if ($length >= 256) {
1159
        break;
1160
      }
1161
      // Remember occurrence of key so we can skip over it if more occurrences
1162
      // are desired.
1163
      if (!isset($included[$key])) {
1164
        $included[$key] = 0;
1165
      }
1166
      // Locate a keyword (position $p, always >0 because $text starts with a
1167
      // space). First try bare keyword, but if that doesn't work, try to find a
1168
      // derived form from search_simplify().
1169
      $p = 0;
1170
      if (preg_match('/' . $boundary . $key . $boundary . '/iu', $text, $match, PREG_OFFSET_CAPTURE, $included[$key])) {
1171
        $p = $match[0][1];
1172
      }
1173
      else {
1174
        $info = search_simplify_excerpt_match($key, $text, $included[$key], $boundary);
1175
        if ($info['where']) {
1176
          $p = $info['where'];
1177
          if ($info['keyword']) {
1178
            $foundkeys[] = $info['keyword'];
1179
          }
1180
        }
1181
      }
1182
      // Now locate a space in front (position $q) and behind it (position $s),
1183
      // leaving about 60 characters extra before and after for context.
1184
      // Note that a space was added to the front and end of $text above.
1185
      if ($p) {
1186
        if (($q = strpos(' ' . $text, ' ', max(0, $p - 61))) !== FALSE) {
1187
          $end = substr($text . ' ', $p, 80);
1188
          if (($s = strrpos($end, ' ')) !== FALSE) {
1189
            // Account for the added spaces.
1190
            $q = max($q - 1, 0);
1191
            $s = min($s, strlen($end) - 1);
1192
            $ranges[$q] = $p + $s;
1193
            $length += $p + $s - $q;
1194
            $included[$key] = $p + 1;
1195
          }
1196
          else {
1197
            unset($workkeys[$k]);
1198
          }
1199
        }
1200
        else {
1201
          unset($workkeys[$k]);
1202
        }
1203
      }
1204
      else {
1205
        unset($workkeys[$k]);
1206
      }
1207
    }
1208
  }
1209
1210
  if (count($ranges) == 0) {
1211
    // We didn't find any keyword matches, so just return the first part of the
1212
    // text. We also need to re-encode any HTML special characters that we
1213
    // entity-decoded above.
1214
    return check_plain(truncate_utf8($text, 256, TRUE, TRUE));
1215
  }
1216
1217
  // Sort the text ranges by starting position.
1218
  ksort($ranges);
1219
1220
  // Now we collapse overlapping text ranges into one. The sorting makes it O(n).
1221
  $newranges = array();
1222
  foreach ($ranges as $from2 => $to2) {
1223
    if (!isset($from1)) {
1224
      $from1 = $from2;
1225
      $to1 = $to2;
1226
      continue;
1227
    }
1228
    if ($from2 <= $to1) {
1229
      $to1 = max($to1, $to2);
1230
    }
1231
    else {
1232
      $newranges[$from1] = $to1;
1233
      $from1 = $from2;
1234
      $to1 = $to2;
1235
    }
1236
  }
1237
  $newranges[$from1] = $to1;
1238
1239
  // Fetch text
1240
  $out = array();
1241
  foreach ($newranges as $from => $to) {
1242
    $out[] = substr($text, $from, $to - $from);
1243
  }
1244
1245
  // Let translators have the ... separator text as one chunk.
1246
  $dots = explode('!excerpt', t('... !excerpt ... !excerpt ...'));
1247
1248
  $text = (isset($newranges[0]) ? '' : $dots[0]) . implode($dots[1], $out) . $dots[2];
1249
  $text = check_plain($text);
1250
1251
  // Slash-escape quotes in keys found in a derived form and merge with original keys.
1252
  array_walk($foundkeys, '_search_excerpt_replace');
1253
  $keys = array_merge($keys, $foundkeys);
1254
1255
  // Highlight keywords. Must be done at once to prevent conflicts ('strong' and '<strong>').
1256
  $text = preg_replace('/' . $boundary . '(' . implode('|', $keys) . ')' . $boundary . '/iu', '<strong>\0</strong>', $text);
1257
  return $text;
1258
}
1259
1260
/**
1261
 * @} End of "defgroup search".
1262
 */
1263
1264
/**
1265
 * Helper function for array_walk() in search_excerpt().
1266
 */
1267
function _search_excerpt_replace(&$text) {
1268
  $text = preg_quote($text, '/');
1269
}
1270
1271
/**
1272
 * Find words in the original text that matched via search_simplify().
1273
 *
1274
 * This is called in search_excerpt() if an exact match is not found in the
1275
 * text, so that we can find the derived form that matches.
1276
 *
1277
 * @param $key
1278
 *   The keyword to find.
1279
 * @param $text
1280
 *   The text to search for the keyword.
1281
 * @param $offset
1282
 *   Offset position in $text to start searching at.
1283
 * @param $boundary
1284
 *   Text to include in a regular expression that will match a word boundary.
1285
 *
1286
 * @return
1287
 *   FALSE if no match is found. If a match is found, return an associative
1288
 *   array with element 'where' giving the position of the match, and element
1289
 *   'keyword' giving the actual word found in the text at that position.
1290
 */
1291
function search_simplify_excerpt_match($key, $text, $offset, $boundary) {
1292
  $pos = NULL;
1293
  $simplified_key = search_simplify($key);
1294
  $simplified_text = search_simplify($text);
1295
1296
  // Return immediately if simplified key or text are empty.
1297
  if (!$simplified_key || !$simplified_text) {
1298
    return FALSE;
1299
  }
1300
1301
  // Check if we have a match after simplification in the text.
1302
  if (!preg_match('/' . $boundary . $simplified_key . $boundary . '/iu', $simplified_text, $match, PREG_OFFSET_CAPTURE, $offset)) {
1303
    return FALSE;
1304
  }
1305
1306
  // If we get here, we have a match. Now find the exact location of the match
1307
  // and the original text that matched. Start by splitting up the text by all
1308
  // potential starting points of the matching text and iterating through them.
1309
  $split = array_filter(preg_split('/' . $boundary . '/iu', $text, -1, PREG_SPLIT_OFFSET_CAPTURE), '_search_excerpt_match_filter');
1310
  foreach ($split as $value) {
1311
    // Skip starting points before the offset.
1312
    if ($value[1] < $offset) {
1313
      continue;
1314
    }
1315
1316
    // Check a window of 80 characters after the starting point for a match,
1317
    // based on the size of the excerpt window.
1318
    $window = substr($text, $value[1], 80);
1319
    $simplified_window = search_simplify($window);
1320
    if (strpos($simplified_window, $simplified_key) === 0) {
1321
      // We have a match in this window. Store the position of the match.
1322
      $pos = $value[1];
1323
      // Iterate through the text in the window until we find the full original
1324
      // matching text.
1325
      $length = strlen($window);
1326
      for ($i = 1; $i <= $length; $i++) {
1327
        $keyfound = substr($text, $value[1], $i);
1328
        if ($simplified_key == search_simplify($keyfound)) {
1329
          break;
1330
        }
1331
      }
1332
      break;
1333
    }
1334
  }
1335
1336
  return $pos ? array('where' => $pos, 'keyword' => $keyfound) : FALSE;
1337
}
1338
1339
/**
1340
 * Helper function for array_filter() in search_search_excerpt_match().
1341
 */
1342
function _search_excerpt_match_filter($var) {
1343
  return strlen(trim($var[0]));
1344
}
1345
1346
/**
1347
 * Implements hook_forms().
1348
 */
1349
function search_forms() {
1350
  $forms['search_block_form']= array(
1351
    'callback' => 'search_box',
1352
    'callback arguments' => array('search_block_form'),
1353
  );
1354
  return $forms;
1355
}