MediaWiki 1.41.2
SearchPostgres.php
Go to the documentation of this file.
1<?php
29
43 protected function doSearchTitleInDB( $term ) {
44 $q = $this->searchQuery( $term, 'titlevector' );
45 $olderror = error_reporting( E_ERROR );
46 $dbr = $this->dbProvider->getReplicaDatabase();
47 // phpcs:ignore MediaWiki.Usage.DbrQueryUsage.DbrQueryFound
48 $resultSet = $dbr->query( $q, 'SearchPostgres', IDatabase::QUERY_SILENCE_ERRORS );
49 error_reporting( $olderror );
50 return new SqlSearchResultSet( $resultSet, $this->searchTerms );
51 }
52
53 protected function doSearchTextInDB( $term ) {
54 $q = $this->searchQuery( $term, 'textvector' );
55 $olderror = error_reporting( E_ERROR );
56 $dbr = $this->dbProvider->getReplicaDatabase();
57 // phpcs:ignore MediaWiki.Usage.DbrQueryUsage.DbrQueryFound
58 $resultSet = $dbr->query( $q, 'SearchPostgres', IDatabase::QUERY_SILENCE_ERRORS );
59 error_reporting( $olderror );
60 return new SqlSearchResultSet( $resultSet, $this->searchTerms );
61 }
62
71 private function parseQuery( $term ) {
72 wfDebug( "parseQuery received: $term" );
73
74 // No backslashes allowed
75 $term = preg_replace( '/\\\\/', '', $term );
76
77 // Collapse parens into nearby words:
78 $term = preg_replace( '/\s*\‍(\s*/', ' (', $term );
79 $term = preg_replace( '/\s*\‍)\s*/', ') ', $term );
80
81 // Treat colons as word separators:
82 $term = preg_replace( '/:/', ' ', $term );
83
84 $searchstring = '';
85 $m = [];
86 if ( preg_match_all( '/([-!]?)(\S+)\s*/', $term, $m, PREG_SET_ORDER ) ) {
87 foreach ( $m as $terms ) {
88 if ( strlen( $terms[1] ) ) {
89 $searchstring .= ' & !';
90 }
91 if ( strtolower( $terms[2] ) === 'and' ) {
92 $searchstring .= ' & ';
93 } elseif ( strtolower( $terms[2] ) === 'or' || $terms[2] === '|' ) {
94 $searchstring .= ' | ';
95 } elseif ( strtolower( $terms[2] ) === 'not' ) {
96 $searchstring .= ' & !';
97 } else {
98 $searchstring .= " & $terms[2]";
99 }
100 }
101 }
102
103 // Strip out leading junk
104 $searchstring = preg_replace( '/^[\s\&\|]+/', '', $searchstring );
105
106 // Remove any doubled-up operators
107 $searchstring = preg_replace( '/([\!\&\|]) +(?:[\&\|] +)+/', "$1 ", $searchstring );
108
109 // Remove any non-spaced operators (e.g. "Zounds!")
110 $searchstring = preg_replace( '/([^ ])[\!\&\|]/', "$1", $searchstring );
111
112 // Remove any trailing whitespace or operators
113 $searchstring = preg_replace( '/[\s\!\&\|]+$/', '', $searchstring );
114
115 // Remove unnecessary quotes around everything
116 $searchstring = preg_replace( '/^[\'"](.*)[\'"]$/', "$1", $searchstring );
117
118 // Quote the whole thing
119 $dbr = $this->dbProvider->getReplicaDatabase();
120 $searchstring = $dbr->addQuotes( $searchstring );
121
122 wfDebug( "parseQuery returned: $searchstring" );
123
124 return $searchstring;
125 }
126
133 private function searchQuery( $term, $fulltext ) {
134 # Get the SQL fragment for the given term
135 $searchstring = $this->parseQuery( $term );
136
137 // We need a separate query here so gin does not complain about empty searches
138 $sql = "SELECT to_tsquery($searchstring)";
139 $dbr = $this->dbProvider->getReplicaDatabase();
140 // phpcs:ignore MediaWiki.Usage.DbrQueryUsage.DbrQueryFound
141 $res = $dbr->query( $sql, __METHOD__ );
142 if ( !$res ) {
143 // TODO: Better output (example to catch: one 'two)
144 die( "Sorry, that was not a valid search string. Please go back and try again" );
145 }
146 $top = $res->fetchRow()[0];
147
148 $this->searchTerms = [];
149 $slotRoleStore = MediaWikiServices::getInstance()->getSlotRoleStore();
150 if ( $top === "" ) { // e.g. if only stopwords are used XXX return something better
151 $query = "SELECT page_id, page_namespace, page_title, 0 AS score " .
152 "FROM page p, revision r, slots s, content c, \"text\" pc " .
153 "WHERE p.page_latest = r.rev_id " .
154 "AND s.slot_revision_id = r.rev_id " .
155 "AND s.slot_role_id = " . $slotRoleStore->getId( SlotRecord::MAIN ) . " " .
156 "AND c.content_id = s.slot_content_id " .
157 "AND pc.old_id = substring( c.content_address from '^tt:([0-9]+)$' )::int " .
158 "AND 1=0";
159 } else {
160 $m = [];
161 if ( preg_match_all( "/'([^']+)'/", $top, $m, PREG_SET_ORDER ) ) {
162 foreach ( $m as $terms ) {
163 $this->searchTerms[$terms[1]] = $terms[1];
164 }
165 }
166
167 $query = "SELECT page_id, page_namespace, page_title, " .
168 "ts_rank($fulltext, to_tsquery($searchstring), 5) AS score " .
169 "FROM page p, revision r, slots s, content c, \"text\" pc " .
170 "WHERE p.page_latest = r.rev_id " .
171 "AND s.slot_revision_id = r.rev_id " .
172 "AND s.slot_role_id = " . $slotRoleStore->getId( SlotRecord::MAIN ) . " " .
173 "AND c.content_id = s.slot_content_id " .
174 "AND pc.old_id = substring( c.content_address from '^tt:([0-9]+)$' )::int " .
175 "AND $fulltext @@ to_tsquery($searchstring)";
176 }
177 // Namespaces - defaults to main
178 if ( $this->namespaces !== null ) { // null -> search all
179 if ( count( $this->namespaces ) < 1 ) {
180 $query .= ' AND page_namespace = ' . NS_MAIN;
181 } else {
182 $namespaces = $dbr->makeList( $this->namespaces );
183 $query .= " AND page_namespace IN ($namespaces)";
184 }
185 }
186
187 $query .= " ORDER BY score DESC, page_id DESC";
188
189 $query .= $dbr->limitResult( '', $this->limit, $this->offset );
190
191 wfDebug( "searchQuery returned: $query" );
192
193 return $query;
194 }
195
196 // Most of the work of these two functions are done automatically via triggers
197
198 public function update( $pageid, $title, $text ) {
199 // We don't want to index older revisions
200 $slotRoleStore = MediaWikiServices::getInstance()->getSlotRoleStore();
201 $sql = "UPDATE \"text\" SET textvector = NULL " .
202 "WHERE textvector IS NOT NULL " .
203 "AND old_id IN " .
204 "(SELECT DISTINCT substring( c.content_address from '^tt:([0-9]+)$' )::int AS old_rev_text_id " .
205 " FROM content c, slots s, revision r " .
206 " WHERE r.rev_page = $pageid " .
207 " AND s.slot_revision_id = r.rev_id " .
208 " AND s.slot_role_id = " . $slotRoleStore->getId( SlotRecord::MAIN ) . " " .
209 " AND c.content_id = s.slot_content_id " .
210 " ORDER BY old_rev_text_id DESC OFFSET 1)";
211
212 $dbw = $this->dbProvider->getPrimaryDatabase();
213 $dbw->query( $sql, __METHOD__ );
214
215 return true;
216 }
217
218 public function updateTitle( $id, $title ) {
219 return true;
220 }
221}
const NS_MAIN
Definition Defines.php:64
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Service locator for MediaWiki core services.
Value object representing a content slot associated with a page revision.
Base search engine base class for database-backed searches.
int[] null $namespaces
Search engine hook base class for Postgres.
doSearchTitleInDB( $term)
Perform a full text search query via tsearch2 and return a result set.
update( $pageid, $title, $text)
Create or update the search index record for the given page.
doSearchTextInDB( $term)
Perform a full text search query and return a result set.
updateTitle( $id, $title)
Update a search index record's title only.
This class is used for different SQL-based search engines shipped with MediaWiki.
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:36