44 $q = $this->searchQuery( $term,
'titlevector' );
45 $olderror = error_reporting( E_ERROR );
46 $dbr = $this->dbProvider->getReplicaDatabase();
48 $resultSet = $dbr->query( $q,
'SearchPostgres', IDatabase::QUERY_SILENCE_ERRORS );
49 error_reporting( $olderror );
54 $q = $this->searchQuery( $term,
'textvector' );
55 $olderror = error_reporting( E_ERROR );
56 $dbr = $this->dbProvider->getReplicaDatabase();
58 $resultSet = $dbr->query( $q,
'SearchPostgres', IDatabase::QUERY_SILENCE_ERRORS );
59 error_reporting( $olderror );
71 private function parseQuery( $term ) {
72 wfDebug(
"parseQuery received: $term" );
75 $term = preg_replace(
'/\\\\/',
'', $term );
78 $term = preg_replace(
'/\s*\(\s*/',
' (', $term );
79 $term = preg_replace(
'/\s*\)\s*/',
') ', $term );
82 $term = preg_replace(
'/:/',
' ', $term );
86 if ( preg_match_all(
'/([-!]?)(\S+)\s*/', $term, $m, PREG_SET_ORDER ) ) {
87 foreach ( $m as $terms ) {
88 if ( strlen( $terms[1] ) ) {
89 $searchstring .=
' & !';
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 .=
' & !';
98 $searchstring .=
" & $terms[2]";
104 $searchstring = preg_replace(
'/^[\s\&\|]+/',
'', $searchstring );
107 $searchstring = preg_replace(
'/([\!\&\|]) +(?:[\&\|] +)+/',
"$1 ", $searchstring );
110 $searchstring = preg_replace(
'/([^ ])[\!\&\|]/',
"$1", $searchstring );
113 $searchstring = preg_replace(
'/[\s\!\&\|]+$/',
'', $searchstring );
116 $searchstring = preg_replace(
'/^[\'"](.*)[\'"]$/',
"$1", $searchstring );
119 $dbr = $this->dbProvider->getReplicaDatabase();
120 $searchstring = $dbr->addQuotes( $searchstring );
122 wfDebug(
"parseQuery returned: $searchstring" );
124 return $searchstring;
133 private function searchQuery( $term, $fulltext ) {
134 # Get the SQL fragment for the given term
135 $searchstring = $this->parseQuery( $term );
138 $sql =
"SELECT to_tsquery($searchstring)";
139 $dbr = $this->dbProvider->getReplicaDatabase();
141 $res = $dbr->query( $sql, __METHOD__ );
144 die(
"Sorry, that was not a valid search string. Please go back and try again" );
146 $top = $res->fetchRow()[0];
148 $this->searchTerms = [];
149 $slotRoleStore = MediaWikiServices::getInstance()->getSlotRoleStore();
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 " .
161 if ( preg_match_all(
"/'([^']+)'/", $top, $m, PREG_SET_ORDER ) ) {
162 foreach ( $m as $terms ) {
163 $this->searchTerms[$terms[1]] = $terms[1];
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)";
178 if ( $this->namespaces !==
null ) {
179 if ( count( $this->namespaces ) < 1 ) {
180 $query .=
' AND page_namespace = ' .
NS_MAIN;
183 $query .=
" AND page_namespace IN ($namespaces)";
187 $query .=
" ORDER BY score DESC, page_id DESC";
189 $query .= $dbr->limitResult(
'', $this->limit, $this->offset );
191 wfDebug(
"searchQuery returned: $query" );
198 public function update( $pageid, $title, $text ) {
200 $slotRoleStore = MediaWikiServices::getInstance()->getSlotRoleStore();
201 $sql =
"UPDATE \"text\" SET textvector = NULL " .
202 "WHERE textvector IS NOT NULL " .
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)";
212 $dbw = $this->dbProvider->getPrimaryDatabase();
213 $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.
Base search engine base class for database-backed searches.
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.