44 $q = $this->searchQuery( $term,
'titlevector' );
45 $olderror = error_reporting( E_ERROR );
46 $dbr = $this->dbProvider->getReplicaDatabase();
48 '@phan-var IDatabase $dbr';
50 $resultSet = $dbr->query( $q,
'SearchPostgres', IDatabase::QUERY_SILENCE_ERRORS );
51 error_reporting( $olderror );
56 $q = $this->searchQuery( $term,
'textvector' );
57 $olderror = error_reporting( E_ERROR );
58 $dbr = $this->dbProvider->getReplicaDatabase();
60 '@phan-var IDatabase $dbr';
62 $resultSet = $dbr->query( $q,
'SearchPostgres', IDatabase::QUERY_SILENCE_ERRORS );
63 error_reporting( $olderror );
75 private function parseQuery( $term ) {
76 wfDebug(
"parseQuery received: $term" );
79 $term = preg_replace(
'/\\\\/',
'', $term );
82 $term = preg_replace(
'/\s*\(\s*/',
' (', $term );
83 $term = preg_replace(
'/\s*\)\s*/',
') ', $term );
86 $term = preg_replace(
'/:/',
' ', $term );
90 if ( preg_match_all(
'/([-!]?)(\S+)\s*/', $term, $m, PREG_SET_ORDER ) ) {
91 foreach ( $m as $terms ) {
92 if ( strlen( $terms[1] ) ) {
93 $searchstring .=
' & !';
95 if ( strtolower( $terms[2] ) ===
'and' ) {
96 $searchstring .=
' & ';
97 } elseif ( strtolower( $terms[2] ) ===
'or' || $terms[2] ===
'|' ) {
98 $searchstring .=
' | ';
99 } elseif ( strtolower( $terms[2] ) ===
'not' ) {
100 $searchstring .=
' & !';
102 $searchstring .=
" & $terms[2]";
108 $searchstring = preg_replace(
'/^[\s\&\|]+/',
'', $searchstring );
111 $searchstring = preg_replace(
'/([\!\&\|]) +(?:[\&\|] +)+/',
"$1 ", $searchstring );
114 $searchstring = preg_replace(
'/([^ ])[\!\&\|]/',
"$1", $searchstring );
117 $searchstring = preg_replace(
'/[\s\!\&\|]+$/',
'', $searchstring );
120 $searchstring = preg_replace(
'/^[\'"](.*)[\'"]$/',
"$1", $searchstring );
123 $dbr = $this->dbProvider->getReplicaDatabase();
124 $searchstring = $dbr->addQuotes( $searchstring );
126 wfDebug(
"parseQuery returned: $searchstring" );
128 return $searchstring;
137 private function searchQuery( $term, $fulltext ) {
138 # Get the SQL fragment for the given term
139 $searchstring = $this->parseQuery( $term );
142 $sql =
"SELECT to_tsquery($searchstring)";
143 $dbr = $this->dbProvider->getReplicaDatabase();
145 '@phan-var IDatabase $dbr';
147 $res = $dbr->query( $sql, __METHOD__ );
150 die(
"Sorry, that was not a valid search string. Please go back and try again" );
152 $top = $res->fetchRow()[0];
154 $this->searchTerms = [];
155 $slotRoleStore = MediaWikiServices::getInstance()->getSlotRoleStore();
157 $query =
"SELECT page_id, page_namespace, page_title, 0 AS score " .
158 "FROM page p, revision r, slots s, content c, \"text\" pc " .
159 "WHERE p.page_latest = r.rev_id " .
160 "AND s.slot_revision_id = r.rev_id " .
161 "AND s.slot_role_id = " .
162 $dbr->addQuotes( $slotRoleStore->acquireId( SlotRecord::MAIN ) ) .
" " .
163 "AND c.content_id = s.slot_content_id " .
164 "AND pc.old_id = substring( c.content_address from '^tt:([0-9]+)$' )::int " .
168 if ( preg_match_all(
"/'([^']+)'/", $top, $m, PREG_SET_ORDER ) ) {
169 foreach ( $m as $terms ) {
170 $this->searchTerms[$terms[1]] = $terms[1];
174 $query =
"SELECT page_id, page_namespace, page_title, " .
175 "ts_rank($fulltext, to_tsquery($searchstring), 5) AS score " .
176 "FROM page p, revision r, slots s, content c, \"text\" pc " .
177 "WHERE p.page_latest = r.rev_id " .
178 "AND s.slot_revision_id = r.rev_id " .
179 "AND s.slot_role_id = " . $dbr->addQuotes(
180 $slotRoleStore->acquireId( SlotRecord::MAIN ) ) .
" " .
181 "AND c.content_id = s.slot_content_id " .
182 "AND pc.old_id = substring( c.content_address from '^tt:([0-9]+)$' )::int " .
183 "AND $fulltext @@ to_tsquery($searchstring)";
186 if ( $this->namespaces !==
null ) {
187 if ( count( $this->namespaces ) < 1 ) {
188 $query .=
' AND page_namespace = ' .
NS_MAIN;
191 $query .=
" AND page_namespace IN ($namespaces)";
195 $query .=
" ORDER BY score DESC, page_id DESC";
197 $query .= $dbr->limitResult(
'', $this->limit, $this->offset );
199 wfDebug(
"searchQuery returned: $query" );
206 public function update( $pageid, $title, $text ) {
208 $slotRoleStore = MediaWikiServices::getInstance()->getSlotRoleStore();
209 $dbw = $this->dbProvider->getPrimaryDatabase();
210 $sql =
"UPDATE \"text\" SET textvector = NULL " .
211 "WHERE textvector IS NOT NULL " .
213 "(SELECT DISTINCT substring( c.content_address from '^tt:([0-9]+)$' )::int AS old_rev_text_id " .
214 " FROM content c, slots s, revision r " .
215 " WHERE r.rev_page = $pageid " .
216 " AND s.slot_revision_id = r.rev_id " .
217 " AND s.slot_role_id = " .
218 $dbw->addQuotes( $slotRoleStore->acquireId( SlotRecord::MAIN ) ) .
" " .
219 " AND c.content_id = s.slot_content_id " .
220 " ORDER BY old_rev_text_id DESC OFFSET 1)";
222 $dbw->query( $sql, __METHOD__ );
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
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.