Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/search/search.module @ 5:12f9dff5fda9 tip
Update to Drupal core 8.7.1
author | Chris Cannam |
---|---|
date | Thu, 09 May 2019 15:34:47 +0100 |
parents | a9cd425dd02b |
children |
comparison
equal
deleted
inserted
replaced
4:a9cd425dd02b | 5:12f9dff5fda9 |
---|---|
3 /** | 3 /** |
4 * @file | 4 * @file |
5 * Enables site-wide keyword searching. | 5 * Enables site-wide keyword searching. |
6 */ | 6 */ |
7 | 7 |
8 use Drupal\Core\Url; | |
8 use Drupal\Component\Utility\Html; | 9 use Drupal\Component\Utility\Html; |
9 use Drupal\Component\Utility\Unicode; | 10 use Drupal\Component\Utility\Unicode; |
10 use Drupal\Core\Cache\Cache; | 11 use Drupal\Core\Cache\Cache; |
11 use Drupal\Core\Database\Query\Condition; | 12 use Drupal\Core\Database\Query\Condition; |
12 use Drupal\Core\Form\FormStateInterface; | 13 use Drupal\Core\Form\FormStateInterface; |
77 $output .= '<h3>' . t('About') . '</h3>'; | 78 $output .= '<h3>' . t('About') . '</h3>'; |
78 $output .= '<p>' . t('The Search module provides the ability to set up search pages based on plugins provided by other modules. In Drupal core, there are two page-type plugins: the Content page type provides keyword searching for content managed by the Node module, and the Users page type provides keyword searching for registered users. Contributed modules may provide other page-type plugins. For more information, see the <a href=":search-module">online documentation for the Search module</a>.', [':search-module' => 'https://www.drupal.org/documentation/modules/search']) . '</p>'; | 79 $output .= '<p>' . t('The Search module provides the ability to set up search pages based on plugins provided by other modules. In Drupal core, there are two page-type plugins: the Content page type provides keyword searching for content managed by the Node module, and the Users page type provides keyword searching for registered users. Contributed modules may provide other page-type plugins. For more information, see the <a href=":search-module">online documentation for the Search module</a>.', [':search-module' => 'https://www.drupal.org/documentation/modules/search']) . '</p>'; |
79 $output .= '<h3>' . t('Uses') . '</h3>'; | 80 $output .= '<h3>' . t('Uses') . '</h3>'; |
80 $output .= '<dl>'; | 81 $output .= '<dl>'; |
81 $output .= '<dt>' . t('Configuring search pages') . '</dt>'; | 82 $output .= '<dt>' . t('Configuring search pages') . '</dt>'; |
82 $output .= '<dd>' . t('To configure search pages, visit the <a href=":search-settings">Search pages page</a>. In the Search pages section, you can add a new search page, edit the configuration of existing search pages, enable and disable search pages, and choose the default search page. Each enabled search page has a URL path starting with <em>search</em>, and each will appear as a tab or local task link on the <a href=":search-url">search page</a>; you can configure the text that is shown in the tab. In addition, some search page plugins have additional settings that you can configure for each search page.', [':search-settings' => \Drupal::url('entity.search_page.collection'), ':search-url' => \Drupal::url('search.view')]) . '</dd>'; | 83 $output .= '<dd>' . t('To configure search pages, visit the <a href=":search-settings">Search pages page</a>. In the Search pages section, you can add a new search page, edit the configuration of existing search pages, enable and disable search pages, and choose the default search page. Each enabled search page has a URL path starting with <em>search</em>, and each will appear as a tab or local task link on the <a href=":search-url">search page</a>; you can configure the text that is shown in the tab. In addition, some search page plugins have additional settings that you can configure for each search page.', [':search-settings' => Url::fromRoute('entity.search_page.collection')->toString(), ':search-url' => Url::fromRoute('search.view')->toString()]) . '</dd>'; |
83 $output .= '<dt>' . t('Managing the search index') . '</dt>'; | 84 $output .= '<dt>' . t('Managing the search index') . '</dt>'; |
84 $output .= '<dd>' . t('Some search page plugins, such as the core Content search page, index searchable text using the Drupal core search index, and will not work unless content is indexed. Indexing is done during <em>cron</em> runs, so it requires a <a href=":cron">cron maintenance task</a> to be set up. There are also several settings affecting indexing that can be configured on the <a href=":search-settings">Search pages page</a>: the number of items to index per cron run, the minimum word length to index, and how to handle Chinese, Japanese, and Korean characters.', [':cron' => \Drupal::url('system.cron_settings'), ':search-settings' => \Drupal::url('entity.search_page.collection')]) . '</dd>'; | 85 $output .= '<dd>' . t('Some search page plugins, such as the core Content search page, index searchable text using the Drupal core search index, and will not work unless content is indexed. Indexing is done during <em>cron</em> runs, so it requires a <a href=":cron">cron maintenance task</a> to be set up. There are also several settings affecting indexing that can be configured on the <a href=":search-settings">Search pages page</a>: the number of items to index per cron run, the minimum word length to index, and how to handle Chinese, Japanese, and Korean characters.', [':cron' => Url::fromRoute('system.cron_settings')->toString(), ':search-settings' => Url::fromRoute('entity.search_page.collection')->toString()]) . '</dd>'; |
85 $output .= '<dd>' . t('Modules providing search page plugins generally ensure that 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. However, there are some actions related to the structure of your site that 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=":search-settings">Search pages page</a>. If you have a lot of content on your site, it may take several cron runs for the content to be reindexed.', [':search-settings' => \Drupal::url('entity.search_page.collection')]) . '</dd>'; | 86 $output .= '<dd>' . t('Modules providing search page plugins generally ensure that 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. However, there are some actions related to the structure of your site that 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=":search-settings">Search pages page</a>. If you have a lot of content on your site, it may take several cron runs for the content to be reindexed.', [':search-settings' => Url::fromRoute('entity.search_page.collection')->toString()]) . '</dd>'; |
86 $output .= '<dt>' . t('Displaying the Search block') . '</dt>'; | 87 $output .= '<dt>' . t('Displaying the Search block') . '</dt>'; |
87 $output .= '<dd>' . t('The Search module includes a block, which can be enabled and configured on the <a href=":blocks">Block layout page</a>, if you have the Block module enabled; the default block title is Search, and it is the Search form block in the Forms category, if you wish to add another instance. The block is available to users with the <a href=":search_permission">Use search</a> permission, and it performs a search using the configured default search page.', [':blocks' => (\Drupal::moduleHandler()->moduleExists('block')) ? \Drupal::url('block.admin_display') : '#', ':search_permission' => \Drupal::url('user.admin_permissions', [], ['fragment' => 'module-search'])]) . '</dd>'; | 88 $output .= '<dd>' . t('The Search module includes a block, which can be enabled and configured on the <a href=":blocks">Block layout page</a>, if you have the Block module enabled; the default block title is Search, and it is the Search form block in the Forms category, if you wish to add another instance. The block is available to users with the <a href=":search_permission">Use search</a> permission, and it performs a search using the configured default search page.', [':blocks' => (\Drupal::moduleHandler()->moduleExists('block')) ? Url::fromRoute('block.admin_display')->toString() : '#', ':search_permission' => Url::fromRoute('user.admin_permissions', [], ['fragment' => 'module-search'])->toString()]) . '</dd>'; |
88 $output .= '<dt>' . t('Searching your site') . '</dt>'; | 89 $output .= '<dt>' . t('Searching your site') . '</dt>'; |
89 $output .= '<dd>' . t('Users with <a href=":search_permission">Use search</a> permission can use the Search block and <a href=":search">Search page</a>. Users with the <a href=":node_permission">View published content</a> permission can use configured search pages of type <em>Content</em> to search for content containing exact keywords; in addition, users with <a href=":search_permission">Use advanced search</a> permission can use more complex search filtering. Users with the <a href=":user_permission">View user information</a> permission can use configured search pages of type <em>Users</em> to search for active users containing the keyword anywhere in the username, and users with the <a href=":user_permission">Administer users</a> permission can search for active and blocked users, by email address or username keyword.', [':search' => \Drupal::url('search.view'), ':search_permission' => \Drupal::url('user.admin_permissions', [], ['fragment' => 'module-search']), ':node_permission' => \Drupal::url('user.admin_permissions', [], ['fragment' => 'module-node']), ':user_permission' => \Drupal::url('user.admin_permissions', [], ['fragment' => 'module-user'])]) . '</dd>'; | 90 $output .= '<dd>' . t('Users with <a href=":search_permission">Use search</a> permission can use the Search block and <a href=":search">Search page</a>. Users with the <a href=":node_permission">View published content</a> permission can use configured search pages of type <em>Content</em> to search for content containing exact keywords; in addition, users with <a href=":search_permission">Use advanced search</a> permission can use more complex search filtering. Users with the <a href=":user_permission">View user information</a> permission can use configured search pages of type <em>Users</em> to search for active users containing the keyword anywhere in the username, and users with the <a href=":user_permission">Administer users</a> permission can search for active and blocked users, by email address or username keyword.', [':search' => Url::fromRoute('search.view')->toString(), ':search_permission' => Url::fromRoute('user.admin_permissions', [], ['fragment' => 'module-search'])->toString(), ':node_permission' => Url::fromRoute('user.admin_permissions', [], ['fragment' => 'module-node'])->toString(), ':user_permission' => Url::fromRoute('user.admin_permissions', [], ['fragment' => 'module-user'])->toString()]) . '</dd>'; |
90 $output .= '<dt>' . t('Extending the Search module') . '</dt>'; | 91 $output .= '<dt>' . t('Extending the Search module') . '</dt>'; |
91 $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=":porterstemmer_url">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=":solr_url">Apache Solr</a> or <a href=":sphinx_url">Sphinx</a>. There are also contributed modules that provide additional search pages. These and other <a href=":contrib-search">search-related contributed modules</a> can be downloaded by visiting Drupal.org.', [':contrib-search' => 'https://www.drupal.org/project/project_module?f[2]=im_vid_3%3A105', ':porterstemmer_url' => 'https://www.drupal.org/project/porterstemmer', ':solr_url' => 'https://www.drupal.org/project/apachesolr', ':sphinx_url' => 'https://www.drupal.org/project/sphinx']) . '</dd>'; | 92 $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=":porterstemmer_url">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=":solr_url">Apache Solr</a> or <a href=":sphinx_url">Sphinx</a>. There are also contributed modules that provide additional search pages. These and other <a href=":contrib-search">search-related contributed modules</a> can be downloaded by visiting Drupal.org.', [':contrib-search' => 'https://www.drupal.org/project/project_module?f[2]=im_vid_3%3A105', ':porterstemmer_url' => 'https://www.drupal.org/project/porterstemmer', ':solr_url' => 'https://www.drupal.org/project/apachesolr', ':sphinx_url' => 'https://www.drupal.org/project/sphinx']) . '</dd>'; |
92 $output .= '</dl>'; | 93 $output .= '</dl>'; |
93 return $output; | 94 return $output; |
94 } | 95 } |
138 * @param string|null $langcode | 139 * @param string|null $langcode |
139 * (optional) Language code of the item to remove from the search index. If | 140 * (optional) Language code of the item to remove from the search index. If |
140 * omitted, all items matching $sid and $type are cleared. | 141 * omitted, all items matching $sid and $type are cleared. |
141 */ | 142 */ |
142 function search_index_clear($type = NULL, $sid = NULL, $langcode = NULL) { | 143 function search_index_clear($type = NULL, $sid = NULL, $langcode = NULL) { |
143 $query_index = db_delete('search_index'); | 144 $connection = \Drupal::database(); |
144 $query_dataset = db_delete('search_dataset'); | 145 $query_index = $connection->delete('search_index'); |
146 $query_dataset = $connection->delete('search_dataset'); | |
145 if ($type) { | 147 if ($type) { |
146 $query_index->condition('type', $type); | 148 $query_index->condition('type', $type); |
147 $query_dataset->condition('type', $type); | 149 $query_dataset->condition('type', $type); |
148 if ($sid) { | 150 if ($sid) { |
149 $query_index->condition('sid', $sid); | 151 $query_index->condition('sid', $sid); |
209 * | 211 * |
210 * This function is called on shutdown to ensure that {search_total} is always | 212 * This function is called on shutdown to ensure that {search_total} is always |
211 * up to date (even if cron times out or otherwise fails). | 213 * up to date (even if cron times out or otherwise fails). |
212 */ | 214 */ |
213 function search_update_totals() { | 215 function search_update_totals() { |
216 $connection = \Drupal::database(); | |
214 // Update word IDF (Inverse Document Frequency) counts for new/changed words. | 217 // Update word IDF (Inverse Document Frequency) counts for new/changed words. |
215 foreach (search_dirty() as $word => $dummy) { | 218 foreach (search_dirty() as $word => $dummy) { |
216 // Get total count | 219 // Get total count |
217 $total = db_query("SELECT SUM(score) FROM {search_index} WHERE word = :word", [':word' => $word], ['target' => 'replica'])->fetchField(); | 220 $total = db_query("SELECT SUM(score) FROM {search_index} WHERE word = :word", [':word' => $word], ['target' => 'replica'])->fetchField(); |
218 // Apply Zipf's law to equalize the probability distribution. | 221 // Apply Zipf's law to equalize the probability distribution. |
219 $total = log10(1 + 1 / (max(1, $total))); | 222 $total = log10(1 + 1 / (max(1, $total))); |
220 db_merge('search_total') | 223 $connection->merge('search_total') |
221 ->key('word', $word) | 224 ->key('word', $word) |
222 ->fields(['count' => $total]) | 225 ->fields(['count' => $total]) |
223 ->execute(); | 226 ->execute(); |
224 } | 227 } |
225 // Find words that were deleted from search_index, but are still in | 228 // Find words that were deleted from search_index, but are still in |
229 $or = new Condition('OR'); | 232 $or = new Condition('OR'); |
230 foreach ($result as $word) { | 233 foreach ($result as $word) { |
231 $or->condition('word', $word->realword); | 234 $or->condition('word', $word->realword); |
232 } | 235 } |
233 if (count($or) > 0) { | 236 if (count($or) > 0) { |
234 db_delete('search_total') | 237 $connection->delete('search_total') |
235 ->condition($or) | 238 ->condition($or) |
236 ->execute(); | 239 ->execute(); |
237 } | 240 } |
238 } | 241 } |
239 | 242 |
534 | 537 |
535 // Remove the item $sid from the search index, and invalidate the relevant | 538 // Remove the item $sid from the search index, and invalidate the relevant |
536 // cache tags. | 539 // cache tags. |
537 search_index_clear($type, $sid, $langcode); | 540 search_index_clear($type, $sid, $langcode); |
538 | 541 |
542 $connection = \Drupal::database(); | |
539 // Insert cleaned up data into dataset | 543 // Insert cleaned up data into dataset |
540 db_insert('search_dataset') | 544 $connection->insert('search_dataset') |
541 ->fields([ | 545 ->fields([ |
542 'sid' => $sid, | 546 'sid' => $sid, |
543 'langcode' => $langcode, | 547 'langcode' => $langcode, |
544 'type' => $type, | 548 'type' => $type, |
545 'data' => $accum, | 549 'data' => $accum, |
550 // Insert results into search index | 554 // Insert results into search index |
551 foreach ($scored_words as $word => $score) { | 555 foreach ($scored_words as $word => $score) { |
552 // If a word already exists in the database, its score gets increased | 556 // If a word already exists in the database, its score gets increased |
553 // appropriately. If not, we create a new record with the appropriate | 557 // appropriately. If not, we create a new record with the appropriate |
554 // starting score. | 558 // starting score. |
555 db_merge('search_index') | 559 $connection->merge('search_index') |
556 ->keys([ | 560 ->keys([ |
557 'word' => $word, | 561 'word' => $word, |
558 'sid' => $sid, | 562 'sid' => $sid, |
559 'langcode' => $langcode, | 563 'langcode' => $langcode, |
560 'type' => $type, | 564 'type' => $type, |
583 * @param string $langcode | 587 * @param string $langcode |
584 * (optional) The language code to clear. If omitted, everything matching | 588 * (optional) The language code to clear. If omitted, everything matching |
585 * $type and $sid is marked. | 589 * $type and $sid is marked. |
586 */ | 590 */ |
587 function search_mark_for_reindex($type = NULL, $sid = NULL, $langcode = NULL) { | 591 function search_mark_for_reindex($type = NULL, $sid = NULL, $langcode = NULL) { |
588 $query = db_update('search_dataset') | 592 $query = \Drupal::database()->update('search_dataset') |
589 ->fields(['reindex' => REQUEST_TIME]) | 593 ->fields(['reindex' => REQUEST_TIME]) |
590 // Only mark items that were not previously marked for reindex, so that | 594 // Only mark items that were not previously marked for reindex, so that |
591 // marked items maintain their priority by request time. | 595 // marked items maintain their priority by request time. |
592 ->condition('reindex', 0); | 596 ->condition('reindex', 0); |
593 | 597 |