30 $q = $this->searchQuery( $term,
'titlevector' );
31 $olderror = error_reporting( E_ERROR );
32 $dbr = $this->dbProvider->getReplicaDatabase();
34 '@phan-var IDatabase $dbr';
36 $resultSet = $dbr->query( $q,
'SearchPostgres', IDatabase::QUERY_SILENCE_ERRORS );
37 error_reporting( $olderror );
43 $q = $this->searchQuery( $term,
'textvector' );
44 $olderror = error_reporting( E_ERROR );
45 $dbr = $this->dbProvider->getReplicaDatabase();
47 '@phan-var IDatabase $dbr';
49 $resultSet = $dbr->query( $q,
'SearchPostgres', IDatabase::QUERY_SILENCE_ERRORS );
50 error_reporting( $olderror );
62 private function parseQuery( $term ) {
63 wfDebug(
"parseQuery received: $term" );
66 $term = preg_replace(
'/\\\\/',
'', $term );
69 $term = preg_replace(
'/\s*\(\s*/',
' (', $term );
70 $term = preg_replace(
'/\s*\)\s*/',
') ', $term );
73 $term = preg_replace(
'/:/',
' ', $term );
77 if ( preg_match_all(
'/([-!]?)(\S+)\s*/', $term, $m, PREG_SET_ORDER ) ) {
78 foreach ( $m as $terms ) {
79 if ( $terms[1] !==
'' ) {
80 $searchstring .=
' & !';
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 .=
' & !';
89 $searchstring .=
" & $terms[2]";
95 $searchstring = preg_replace(
'/^[\s\&\|]+/',
'', $searchstring );
98 $searchstring = preg_replace(
'/([\!\&\|]) +(?:[\&\|] +)+/',
"$1 ", $searchstring );
101 $searchstring = preg_replace(
'/([^ ])[\!\&\|]/',
"$1", $searchstring );
104 $searchstring = preg_replace(
'/[\s\!\&\|]+$/',
'', $searchstring );
107 $searchstring = preg_replace(
'/^[\'"](.*)[\'"]$/',
"$1", $searchstring );
110 $dbr = $this->dbProvider->getReplicaDatabase();
111 $searchstring = $dbr->addQuotes( $searchstring );
113 wfDebug(
"parseQuery returned: $searchstring" );
115 return $searchstring;
124 private function searchQuery( $term, $fulltext ) {
125 # Get the SQL fragment for the given term
126 $searchstring = $this->parseQuery( $term );
129 $sql =
"SELECT to_tsquery($searchstring)";
130 $dbr = $this->dbProvider->getReplicaDatabase();
132 '@phan-var IDatabase $dbr';
134 $res = $dbr->query( $sql, __METHOD__ );
137 die(
"Sorry, that was not a valid search string. Please go back and try again" );
139 $top = $res->fetchRow()[0];
141 $this->searchTerms = [];
142 $slotRoleStore = MediaWikiServices::getInstance()->getSlotRoleStore();
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 " .
155 if ( preg_match_all(
"/'([^']+)'/", $top, $m, PREG_SET_ORDER ) ) {
156 foreach ( $m as $terms ) {
157 $this->searchTerms[$terms[1]] = $terms[1];
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)";
173 if ( $this->namespaces !==
null ) {
174 if ( count( $this->namespaces ) < 1 ) {
175 $query .=
' AND page_namespace = ' .
NS_MAIN;
178 $query .=
" AND page_namespace IN ($namespaces)";
182 $query .=
" ORDER BY score DESC, page_id DESC";
184 $query .= $dbr->limitResult(
'', $this->limit, $this->offset );
186 wfDebug(
"searchQuery returned: $query" );
194 public function update( $pageid, $title, $text ) {
196 $slotRoleStore = MediaWikiServices::getInstance()->getSlotRoleStore();
197 $dbw = $this->dbProvider->getPrimaryDatabase();
198 $sql =
"UPDATE \"text\" SET textvector = NULL " .
199 "WHERE textvector IS NOT NULL " .
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)";
210 $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.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