comparison core/modules/node/src/Plugin/Search/NodeSearch.php @ 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
4 4
5 use Drupal\Core\Access\AccessResult; 5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Cache\CacheableMetadata; 6 use Drupal\Core\Cache\CacheableMetadata;
7 use Drupal\Core\Config\Config; 7 use Drupal\Core\Config\Config;
8 use Drupal\Core\Database\Connection; 8 use Drupal\Core\Database\Connection;
9 use Drupal\Core\Database\Database;
9 use Drupal\Core\Database\Query\SelectExtender; 10 use Drupal\Core\Database\Query\SelectExtender;
10 use Drupal\Core\Database\StatementInterface; 11 use Drupal\Core\Database\StatementInterface;
11 use Drupal\Core\Entity\EntityManagerInterface; 12 use Drupal\Core\Entity\EntityManagerInterface;
12 use Drupal\Core\Extension\ModuleHandlerInterface; 13 use Drupal\Core\Extension\ModuleHandlerInterface;
13 use Drupal\Core\Form\FormStateInterface; 14 use Drupal\Core\Form\FormStateInterface;
33 * ) 34 * )
34 */ 35 */
35 class NodeSearch extends ConfigurableSearchPluginBase implements AccessibleInterface, SearchIndexingInterface { 36 class NodeSearch extends ConfigurableSearchPluginBase implements AccessibleInterface, SearchIndexingInterface {
36 37
37 /** 38 /**
38 * A database connection object. 39 * The current database connection.
39 * 40 *
40 * @var \Drupal\Core\Database\Connection 41 * @var \Drupal\Core\Database\Connection
41 */ 42 */
42 protected $database; 43 protected $database;
44
45 /**
46 * The replica database connection.
47 *
48 * @var \Drupal\Core\Database\Connection
49 */
50 protected $databaseReplica;
43 51
44 /** 52 /**
45 * An entity manager object. 53 * An entity manager object.
46 * 54 *
47 * @var \Drupal\Core\Entity\EntityManagerInterface 55 * @var \Drupal\Core\Entity\EntityManagerInterface
135 $container->get('module_handler'), 143 $container->get('module_handler'),
136 $container->get('config.factory')->get('search.settings'), 144 $container->get('config.factory')->get('search.settings'),
137 $container->get('language_manager'), 145 $container->get('language_manager'),
138 $container->get('renderer'), 146 $container->get('renderer'),
139 $container->get('messenger'), 147 $container->get('messenger'),
140 $container->get('current_user') 148 $container->get('current_user'),
149 $container->get('database.replica')
141 ); 150 );
142 } 151 }
143 152
144 /** 153 /**
145 * Constructs a \Drupal\node\Plugin\Search\NodeSearch object. 154 * Constructs a \Drupal\node\Plugin\Search\NodeSearch object.
149 * @param string $plugin_id 158 * @param string $plugin_id
150 * The plugin_id for the plugin instance. 159 * The plugin_id for the plugin instance.
151 * @param mixed $plugin_definition 160 * @param mixed $plugin_definition
152 * The plugin implementation definition. 161 * The plugin implementation definition.
153 * @param \Drupal\Core\Database\Connection $database 162 * @param \Drupal\Core\Database\Connection $database
154 * A database connection object. 163 * The current database connection.
155 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager 164 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
156 * An entity manager object. 165 * An entity manager object.
157 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler 166 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
158 * A module manager object. 167 * A module manager object.
159 * @param \Drupal\Core\Config\Config $search_settings 168 * @param \Drupal\Core\Config\Config $search_settings
164 * The renderer. 173 * The renderer.
165 * @param \Drupal\Core\Messenger\MessengerInterface $messenger 174 * @param \Drupal\Core\Messenger\MessengerInterface $messenger
166 * The messenger. 175 * The messenger.
167 * @param \Drupal\Core\Session\AccountInterface $account 176 * @param \Drupal\Core\Session\AccountInterface $account
168 * The $account object to use for checking for access to advanced search. 177 * The $account object to use for checking for access to advanced search.
169 */ 178 * @param \Drupal\Core\Database\Connection|null $database_replica
170 public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, Config $search_settings, LanguageManagerInterface $language_manager, RendererInterface $renderer, MessengerInterface $messenger, AccountInterface $account = NULL) { 179 * (Optional) the replica database connection.
180 */
181 public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, Config $search_settings, LanguageManagerInterface $language_manager, RendererInterface $renderer, MessengerInterface $messenger, AccountInterface $account = NULL, Connection $database_replica = NULL) {
171 $this->database = $database; 182 $this->database = $database;
183 $this->databaseReplica = $database_replica ?: $database;
172 $this->entityManager = $entity_manager; 184 $this->entityManager = $entity_manager;
173 $this->moduleHandler = $module_handler; 185 $this->moduleHandler = $module_handler;
174 $this->searchSettings = $search_settings; 186 $this->searchSettings = $search_settings;
175 $this->languageManager = $language_manager; 187 $this->languageManager = $language_manager;
176 $this->renderer = $renderer; 188 $this->renderer = $renderer;
234 */ 246 */
235 protected function findResults() { 247 protected function findResults() {
236 $keys = $this->keywords; 248 $keys = $this->keywords;
237 249
238 // Build matching conditions. 250 // Build matching conditions.
239 $query = $this->database 251 $query = $this->databaseReplica
240 ->select('search_index', 'i', ['target' => 'replica']) 252 ->select('search_index', 'i')
241 ->extend('Drupal\search\SearchQuery') 253 ->extend('Drupal\search\SearchQuery')
242 ->extend('Drupal\Core\Database\Query\PagerSelectExtender'); 254 ->extend('Drupal\Core\Database\Query\PagerSelectExtender');
243 $query->join('node_field_data', 'n', 'n.nid = i.sid AND n.langcode = i.langcode'); 255 $query->join('node_field_data', 'n', 'n.nid = i.sid AND n.langcode = i.langcode');
244 $query->condition('n.status', 1) 256 $query->condition('n.status', 1)
245 ->addTag('node_access') 257 ->addTag('node_access')
348 $this->addCacheableDependency(CacheableMetadata::createFromRenderArray($build)); 360 $this->addCacheableDependency(CacheableMetadata::createFromRenderArray($build));
349 $rendered .= ' ' . $this->moduleHandler->invoke('comment', 'node_update_index', [$node]); 361 $rendered .= ' ' . $this->moduleHandler->invoke('comment', 'node_update_index', [$node]);
350 362
351 $extra = $this->moduleHandler->invokeAll('node_search_result', [$node]); 363 $extra = $this->moduleHandler->invokeAll('node_search_result', [$node]);
352 364
353 $language = $this->languageManager->getLanguage($item->langcode);
354 $username = [ 365 $username = [
355 '#theme' => 'username', 366 '#theme' => 'username',
356 '#account' => $node->getOwner(), 367 '#account' => $node->getOwner(),
357 ]; 368 ];
358 369
359 $result = [ 370 $result = [
360 'link' => $node->url('canonical', ['absolute' => TRUE, 'language' => $language]), 371 'link' => $node->toUrl('canonical', ['absolute' => TRUE])->toString(),
361 'type' => $type->label(), 372 'type' => $type->label(),
362 'title' => $node->label(), 373 'title' => $node->label(),
363 'node' => $node, 374 'node' => $node,
364 'extra' => $extra, 375 'extra' => $extra,
365 'score' => $item->calculated_score, 376 'score' => $item->calculated_score,
436 public function updateIndex() { 447 public function updateIndex() {
437 // Interpret the cron limit setting as the maximum number of nodes to index 448 // Interpret the cron limit setting as the maximum number of nodes to index
438 // per cron run. 449 // per cron run.
439 $limit = (int) $this->searchSettings->get('index.cron_limit'); 450 $limit = (int) $this->searchSettings->get('index.cron_limit');
440 451
441 $query = db_select('node', 'n', ['target' => 'replica']); 452 $query = $this->databaseReplica->select('node', 'n');
442 $query->addField('n', 'nid'); 453 $query->addField('n', 'nid');
443 $query->leftJoin('search_dataset', 'sd', 'sd.sid = n.nid AND sd.type = :type', [':type' => $this->getPluginId()]); 454 $query->leftJoin('search_dataset', 'sd', 'sd.sid = n.nid AND sd.type = :type', [':type' => $this->getPluginId()]);
444 $query->addExpression('CASE MAX(sd.reindex) WHEN NULL THEN 0 ELSE 1 END', 'ex'); 455 $query->addExpression('CASE MAX(sd.reindex) WHEN NULL THEN 0 ELSE 1 END', 'ex');
445 $query->addExpression('MAX(sd.reindex)', 'ex2'); 456 $query->addExpression('MAX(sd.reindex)', 'ex2');
446 $query->condition( 457 $query->condition(