f9cd9a14b3f35ff316f6bf4a5155ac7fe4f55bbe
[yaffs-website] / web / core / modules / search / src / Plugin / views / argument / Search.php
1 <?php
2
3 namespace Drupal\search\Plugin\views\argument;
4
5 use Drupal\Core\Database\Query\Condition;
6 use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
7 use Drupal\views\Plugin\views\display\DisplayPluginBase;
8 use Drupal\views\ViewExecutable;
9 use Drupal\views\Views;
10
11 /**
12  * Argument handler for search keywords.
13  *
14  * @ingroup views_argument_handlers
15  *
16  * @ViewsArgument("search")
17  */
18 class Search extends ArgumentPluginBase {
19
20   /**
21    * A search query to use for parsing search keywords.
22    *
23    * @var \Drupal\search\ViewsSearchQuery
24    */
25   protected $searchQuery = NULL;
26
27   /**
28    * The search type name (value of {search_index}.type in the database).
29    *
30    * @var string
31    */
32   protected $searchType;
33
34   /**
35    * {@inheritdoc}
36    */
37   public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
38     parent::init($view, $display, $options);
39
40     $this->searchType = $this->definition['search_type'];
41   }
42
43   /**
44    * Sets up and parses the search query.
45    *
46    * @param string $input
47    *   The search keywords entered by the user.
48    */
49   protected function queryParseSearchExpression($input) {
50     if (!isset($this->searchQuery)) {
51       $this->searchQuery = db_select('search_index', 'i', ['target' => 'replica'])->extend('Drupal\search\ViewsSearchQuery');
52       $this->searchQuery->searchExpression($input, $this->searchType);
53       $this->searchQuery->publicParseSearchExpression();
54     }
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   public function query($group_by = FALSE) {
61     $required = FALSE;
62     $this->queryParseSearchExpression($this->argument);
63     if (!isset($this->searchQuery)) {
64       $required = TRUE;
65     }
66     else {
67       $words = $this->searchQuery->words();
68       if (empty($words)) {
69         $required = TRUE;
70       }
71     }
72     if ($required) {
73       if ($this->operator == 'required') {
74         $this->query->addWhere(0, 'FALSE');
75       }
76     }
77     else {
78       $search_index = $this->ensureMyTable();
79
80       $search_condition = new Condition('AND');
81
82       // Create a new join to relate the 'search_total' table to our current 'search_index' table.
83       $definition = [
84         'table' => 'search_total',
85         'field' => 'word',
86         'left_table' => $search_index,
87         'left_field' => 'word',
88       ];
89       $join = Views::pluginManager('join')->createInstance('standard', $definition);
90       $search_total = $this->query->addRelationship('search_total', $join, $search_index);
91
92       // Add the search score field to the query.
93       $this->search_score = $this->query->addField('', "$search_index.score * $search_total.count", 'score', ['function' => 'sum']);
94
95       // Add the conditions set up by the search query to the views query.
96       $search_condition->condition("$search_index.type", $this->searchType);
97       $search_dataset = $this->query->addTable('node_search_dataset');
98       $conditions = $this->searchQuery->conditions();
99       $condition_conditions =& $conditions->conditions();
100       foreach ($condition_conditions as $key => &$condition) {
101         // Make sure we just look at real conditions.
102         if (is_numeric($key)) {
103           // Replace the conditions with the table alias of views.
104           $this->searchQuery->conditionReplaceString('d.', "$search_dataset.", $condition);
105         }
106       }
107       $search_conditions =& $search_condition->conditions();
108       $search_conditions = array_merge($search_conditions, $condition_conditions);
109
110       // Add the keyword conditions, as is done in
111       // SearchQuery::prepareAndNormalize(), but simplified because we are
112       // only concerned with relevance ranking so we do not need to normalize.
113       $or = new Condition('OR');
114       foreach ($words as $word) {
115         $or->condition("$search_index.word", $word);
116       }
117       $search_condition->condition($or);
118
119       // Add the GROUP BY and HAVING expressions to the query.
120       $this->query->addWhere(0, $search_condition);
121       $this->query->addGroupBy("$search_index.sid");
122       $matches = $this->searchQuery->matches();
123       $placeholder = $this->placeholder();
124       $this->query->addHavingExpression(0, "COUNT(*) >= $placeholder", [$placeholder => $matches]);
125     }
126
127     // Set to NULL to prevent PDO exception when views object is cached
128     // and to clear out memory.
129     $this->searchQuery = NULL;
130   }
131
132 }