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