MediaWiki master
SearchPostgres.php
Go to the documentation of this file.
1<?php
15
29 protected function doSearchTitleInDB( $term ) {
30 $q = $this->searchQuery( $term, 'titlevector' );
31 $olderror = error_reporting( E_ERROR );
32 $dbr = $this->dbProvider->getReplicaDatabase();
33 // The real type is still IDatabase, but IReplicaDatabase is used for safety.
34 '@phan-var IDatabase $dbr';
35 // phpcs:ignore MediaWiki.Usage.DbrQueryUsage.DbrQueryFound
36 $resultSet = $dbr->query( $q, 'SearchPostgres', IDatabase::QUERY_SILENCE_ERRORS );
37 error_reporting( $olderror );
38 return new SqlSearchResultSet( $resultSet, $this->searchTerms );
39 }
40
42 protected function doSearchTextInDB( $term ) {
43 $q = $this->searchQuery( $term, 'textvector' );
44 $olderror = error_reporting( E_ERROR );
45 $dbr = $this->dbProvider->getReplicaDatabase();
46 // The real type is still IDatabase, but IReplicaDatabase is used for safety.
47 '@phan-var IDatabase $dbr';
48 // phpcs:ignore MediaWiki.Usage.DbrQueryUsage.DbrQueryFound
49 $resultSet = $dbr->query( $q, 'SearchPostgres', IDatabase::QUERY_SILENCE_ERRORS );
50 error_reporting( $olderror );
51 return new SqlSearchResultSet( $resultSet, $this->searchTerms );
52 }
53
62 private function parseQuery( $term ) {
63 wfDebug( "parseQuery received: $term" );
64
65 // No backslashes allowed
66 $term = preg_replace( '/\\\\/', '', $term );
67
68 // Collapse parens into nearby words:
69 $term = preg_replace( '/\s*\‍(\s*/', ' (', $term );
70 $term = preg_replace( '/\s*\‍)\s*/', ') ', $term );
71
72 // Treat colons as word separators:
73 $term = preg_replace( '/:/', ' ', $term );
74
75 $searchstring = '';
76 $m = [];
77 if ( preg_match_all( '/([-!]?)(\S+)\s*/', $term, $m, PREG_SET_ORDER ) ) {
78 foreach ( $m as $terms ) {
79 if ( $terms[1] !== '' ) {
80 $searchstring .= ' & !';
81 }
82 if ( strtolower( $terms[2] ) === 'and' ) {
83 $searchstring .= ' & ';
84 } elseif ( strtolower( $terms[2] ) === 'or' || $terms[2] === '|' ) {
85 $searchstring .= ' | ';
86 } elseif ( strtolower( $terms[2] ) === 'not' ) {
87 $searchstring .= ' & !';
88 } else {
89 $searchstring .= " & $terms[2]";
90 }
91 }
92 }
93
94 // Strip out leading junk
95 $searchstring = preg_replace( '/^[\s\&\|]+/', '', $searchstring );
96
97 // Remove any doubled-up operators
98 $searchstring = preg_replace( '/([\!\&\|]) +(?:[\&\|] +)+/', "$1 ", $searchstring );
99
100 // Remove any non-spaced operators (e.g. "Zounds!")
101 $searchstring = preg_replace( '/([^ ])[\!\&\|]/', "$1", $searchstring );
102
103 // Remove any trailing whitespace or operators
104 $searchstring = preg_replace( '/[\s\!\&\|]+$/', '', $searchstring );
105
106 // Remove unnecessary quotes around everything
107 $searchstring = preg_replace( '/^[\'"](.*)[\'"]$/', "$1", $searchstring );
108
109 // Quote the whole thing
110 $dbr = $this->dbProvider->getReplicaDatabase();
111 $searchstring = $dbr->addQuotes( $searchstring );
112
113 wfDebug( "parseQuery returned: $searchstring" );
114
115 return $searchstring;
116 }
117
124 private function searchQuery( $term, $fulltext ) {
125 # Get the SQL fragment for the given term
126 $searchstring = $this->parseQuery( $term );
127
128 // We need a separate query here so gin does not complain about empty searches
129 $sql = "SELECT to_tsquery($searchstring)";
130 $dbr = $this->dbProvider->getReplicaDatabase();
131 // The real type is still IDatabase, but IReplicaDatabase is used for safety.
132 '@phan-var IDatabase $dbr';
133 // phpcs:ignore MediaWiki.Usage.DbrQueryUsage.DbrQueryFound
134 $res = $dbr->query( $sql, __METHOD__ );
135 if ( !$res ) {
136 // TODO: Better output (example to catch: one 'two)
137 die( "Sorry, that was not a valid search string. Please go back and try again" );
138 }
139 $top = $res->fetchRow()[0];
140
141 $this->searchTerms = [];
142 $slotRoleStore = MediaWikiServices::getInstance()->getSlotRoleStore();
143 if ( $top === "" ) { // e.g. if only stopwords are used XXX return something better
144 $query = "SELECT page_id, page_namespace, page_title, 0 AS score " .
145 "FROM page p, revision r, slots s, content c, \"text\" pc " .
146 "WHERE p.page_latest = r.rev_id " .
147 "AND s.slot_revision_id = r.rev_id " .
148 "AND s.slot_role_id = " .
149 $dbr->addQuotes( $slotRoleStore->acquireId( SlotRecord::MAIN ) ) . " " .
150 "AND c.content_id = s.slot_content_id " .
151 "AND pc.old_id = substring( c.content_address from '^tt:([0-9]+)$' )::int " .
152 "AND 1=0";
153 } else {
154 $m = [];
155 if ( preg_match_all( "/'([^']+)'/", $top, $m, PREG_SET_ORDER ) ) {
156 foreach ( $m as $terms ) {
157 $this->searchTerms[$terms[1]] = $terms[1];
158 }
159 }
160
161 $query = "SELECT page_id, page_namespace, page_title, " .
162 "ts_rank($fulltext, to_tsquery($searchstring), 5) AS score " .
163 "FROM page p, revision r, slots s, content c, \"text\" pc " .
164 "WHERE p.page_latest = r.rev_id " .
165 "AND s.slot_revision_id = r.rev_id " .
166 "AND s.slot_role_id = " . $dbr->addQuotes(
167 $slotRoleStore->acquireId( SlotRecord::MAIN ) ) . " " .
168 "AND c.content_id = s.slot_content_id " .
169 "AND pc.old_id = substring( c.content_address from '^tt:([0-9]+)$' )::int " .
170 "AND $fulltext @@ to_tsquery($searchstring)";
171 }
172 // Namespaces - defaults to main
173 if ( $this->namespaces !== null ) { // null -> search all
174 if ( count( $this->namespaces ) < 1 ) {
175 $query .= ' AND page_namespace = ' . NS_MAIN;
176 } else {
177 $namespaces = $dbr->makeList( $this->namespaces );
178 $query .= " AND page_namespace IN ($namespaces)";
179 }
180 }
181
182 $query .= " ORDER BY score DESC, page_id DESC";
183
184 $query .= $dbr->limitResult( '', $this->limit, $this->offset );
185
186 wfDebug( "searchQuery returned: $query" );
187
188 return $query;
189 }
190
191 // Most of the work of these two functions are done automatically via triggers
192
194 public function update( $pageid, $title, $text ) {
195 // We don't want to index older revisions
196 $slotRoleStore = MediaWikiServices::getInstance()->getSlotRoleStore();
197 $dbw = $this->dbProvider->getPrimaryDatabase();
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 = " .
206 $dbw->addQuotes( $slotRoleStore->acquireId( SlotRecord::MAIN ) ) . " " .
207 " AND c.content_id = s.slot_content_id " .
208 " ORDER BY old_rev_text_id DESC OFFSET 1)";
209
210 $dbw->query( $sql, __METHOD__ );
211
212 return true;
213 }
214
216 public function updateTitle( $id, $title ) {
217 return true;
218 }
219}
const NS_MAIN
Definition Defines.php:51
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.Title and text should be pre-processed....
doSearchTextInDB( $term)
Perform a full text search query and return a result set.SqlSearchResultSet|null
updateTitle( $id, $title)
Update a search index record's title only.Title should be pre-processed. STUB
This class is used for different SQL-based search engines shipped with MediaWiki.
Interface to a relational database.
Definition IDatabase.php:31