diff --git a/core/modules/help_topics/help_topics.install b/core/modules/help_topics/help_topics.install index b0b604f0a9..5d0ca155a9 100644 --- a/core/modules/help_topics/help_topics.install +++ b/core/modules/help_topics/help_topics.install @@ -18,7 +18,7 @@ function help_topics_schema() { 'unsigned' => TRUE, 'not null' => TRUE, ], - 'plugin_type' => [ + 'plugin_id' => [ 'description' => 'The help plugin type the item comes from', 'type' => 'varchar_ascii', 'length' => 255, @@ -42,7 +42,7 @@ function help_topics_schema() { ], 'primary key' => ['sid'], 'indexes' => [ - 'plugin_type' => ['plugin_type'], + 'plugin_id' => ['plugin_id'], 'id' => ['id'], ], ]; diff --git a/core/modules/help_topics/src/Plugin/Search/HelpSearch.php b/core/modules/help_topics/src/Plugin/Search/HelpSearch.php index 4967f0abc6..9c4e5bf63b 100644 --- a/core/modules/help_topics/src/Plugin/Search/HelpSearch.php +++ b/core/modules/help_topics/src/Plugin/Search/HelpSearch.php @@ -193,14 +193,16 @@ protected function findResults() { if (!$this->account->hasPermission('access administration pages')) { return NULL; } - $denied_permissions = array_filter($this->database + $permissions = $this->database ->select('help_search_items', 'hsi') + ->distinct() ->fields('hsi', ['permission']) - ->groupBy('hsi.permission') + ->condition('permission', '', '<>') ->execute() - ->fetchCol(), function ($permission) { - return $permission && !$this->account->hasPermission($permission); - }); + ->fetchCol(); + $denied_permissions = array_filter($permissions, function ($permission) { + return !$this->account->hasPermission($permission); + }); $query = $this->database ->select('search_index', 'i') @@ -208,8 +210,8 @@ protected function findResults() { ->condition('i.langcode', $this->languageManager->getCurrentLanguage()->getId()) ->extend(SearchQuery::class) ->extend(PagerSelectExtender::class); - $query->join('help_search_items', 'hsi', 'i.sid = hsi.sid AND i.type = :type', [':type' => $this->getPluginId()]); - if (count($denied_permissions)) { + $query->innerJoin('help_search_items', 'hsi', 'i.sid = hsi.sid AND i.type = :type', [':type' => $this->getPluginId()]); + if ($denied_permissions) { $query->condition('hsi.permission', $denied_permissions, 'NOT IN'); } $query->searchExpression($this->getKeywords(), $this->getPluginId()); @@ -258,9 +260,9 @@ protected function prepareResults(StatementInterface $found) { foreach ($found as $item) { $record = $this->database->select('help_search_items', 'hsi') ->condition('sid', $item->sid) - ->fields('hsi', ['plugin_type', 'id']) + ->fields('hsi', ['plugin_id', 'id']) ->execute()->fetchObject(); - $type = $record->plugin_type; + $type = $record->plugin_id; if (!isset($plugins[$type])) { $plugins[$type] = $this->getSectionPlugin($type); } @@ -301,11 +303,11 @@ public function updateIndex() { $limit = (int) $this->searchSettings->get('index.cron_limit'); $query = $this->database->select('help_search_items', 'hsi'); - $query->fields('hsi', ['sid', 'plugin_type', 'id']); + $query->fields('hsi', ['sid', 'plugin_id', 'id']); $query->leftJoin('search_dataset', 'sd', 'sd.sid = hsi.sid AND sd.type = :type', [':type' => $this->getPluginId()]); $query->where('sd.sid IS NULL'); $query->groupBy('hsi.sid') - ->groupBy('hsi.plugin_type') + ->groupBy('hsi.plugin_id') ->groupBy('hsi.id') ->range(0, $limit); $items = $query->execute()->fetchAll(); @@ -314,11 +316,11 @@ public function updateIndex() { // been indexed before, but are currently marked as needing a re-index. if (count($items) < $limit) { $query = $this->database->select('help_search_items', 'hsi'); - $query->fields('hsi', ['sid', 'plugin_type', 'id']); + $query->fields('hsi', ['sid', 'plugin_id', 'id']); $query->leftJoin('search_dataset', 'sd', 'sd.sid = hsi.sid AND sd.type = :type', [':type' => $this->getPluginId()]); $query->condition('sd.reindex', 0, '<>'); $query->groupBy('hsi.sid') - ->groupBy('hsi.plugin_type') + ->groupBy('hsi.plugin_id') ->groupBy('hsi.id') ->range(0, $limit - count($items)); $items = $items + $query->execute()->fetchAll(); @@ -329,7 +331,7 @@ public function updateIndex() { $plugins = []; foreach ($items as $item) { - $type = $item->plugin_type; + $type = $item->plugin_id; if (!isset($plugins[$type])) { $plugins[$type] = $this->getSectionPlugin($type); } @@ -366,13 +368,12 @@ public function markForReindex() { // Update the list of help items. Start by fetching the existing list, // so we can remove items not found at the end. $old_list = $this->database->select('help_search_items', 'hsi') - ->fields('hsi', ['sid', 'id', 'plugin_type', 'permission']) - ->execute() - ->fetchAll(); + ->fields('hsi', ['sid', 'id', 'plugin_id', 'permission']) + ->execute(); $old_list_ordered = []; $sids_to_remove = []; foreach ($old_list as $item) { - $old_list_ordered[$item->plugin_type][$item->id] = $item; + $old_list_ordered[$item->plugin_id][$item->id] = $item; $sids_to_remove[$item->sid] = $item->sid; } @@ -383,11 +384,7 @@ public function markForReindex() { continue; } $permission = $plugin_definition['permission'] ?? ''; - $items = $plugin->listSearchableTopics(); - if (!count($items)) { - continue; - } - foreach ($items as $id) { + foreach ($plugin->listSearchableTopics() as $id) { if (isset($old_list_ordered[$plugin_id][$id])) { $old_item = $old_list_ordered[$plugin_id][$id]; if ($old_item->permission == $permission) { @@ -408,7 +405,7 @@ public function markForReindex() { // New record, create it. $this->database->insert('help_search_items') ->fields([ - 'plugin_type' => $plugin_id, + 'plugin_id' => $plugin_id, 'permission' => $permission, 'id' => $id, ]) @@ -479,16 +476,16 @@ protected function removeItemsFromIndex($sids) { /** * Instantiates a help section plugin and verifies it is searchable. * - * @param string $plugin_type + * @param string $plugin_id * Type of plugin to instantiate. * * @return \Drupal\help_topics\SearchableHelpInterface|false * Plugin object, or FALSE if it is not a searchable type. */ - protected function getSectionPlugin($plugin_type) { + protected function getSectionPlugin($plugin_id) { /** @var \Drupal\help\HelpSectionPluginInterface $plugin */ - $plugin = $this->helpManager->createInstance($plugin_type); - if ($plugin && $plugin instanceof SearchableHelpInterface) { + $plugin = $this->helpManager->createInstance($plugin_id); + if ($plugin instanceof SearchableHelpInterface) { return $plugin; } // Intentionally return boolean to allow caching of results. diff --git a/core/modules/help_topics/tests/src/Functional/HelpTopicSearchTest.php b/core/modules/help_topics/tests/src/Functional/HelpTopicSearchTest.php index 48cbb84765..40b81e1ee6 100644 --- a/core/modules/help_topics/tests/src/Functional/HelpTopicSearchTest.php +++ b/core/modules/help_topics/tests/src/Functional/HelpTopicSearchTest.php @@ -55,14 +55,12 @@ protected function setUp() { // Run cron until the topics are fully indexed, with a limit of 100 runs // to avoid infinite loops. - $num_runs = 0; - $indexed = FALSE; + $num_runs = 100; $plugin = HelpSearch::create($this->container, [], 'foo', []); - while (($num_runs < 100) && !$indexed) { + do { $this->cronRun(); $status = $plugin->indexStatus(); - $indexed = ($status['remaining'] == $status['total']); - } + } while (--$num_runs && $status['remaining'] != $status['total']); // Visit the Search settings page and verify it says 100% indexed. $this->drupalGet('admin/config/search/pages');