43 $q = $this->
searchQuery( $term,
'titlevector',
'page_title' );
44 $olderror = error_reporting( E_ERROR );
46 $resultSet =
$dbr->query( $q,
'SearchPostgres',
true );
47 error_reporting( $olderror );
52 $q = $this->
searchQuery( $term,
'textvector',
'old_text' );
53 $olderror = error_reporting( E_ERROR );
55 $resultSet =
$dbr->query( $q,
'SearchPostgres',
true );
56 error_reporting( $olderror );
69 wfDebug(
"parseQuery received: $term \n" );
71 # # No backslashes allowed
72 $term = preg_replace(
'/\\\/',
'', $term );
74 # # Collapse parens into nearby words:
75 $term = preg_replace(
'/\s*\(\s*/',
' (', $term );
76 $term = preg_replace(
'/\s*\)\s*/',
') ', $term );
78 # # Treat colons as word separators:
79 $term = preg_replace(
'/:/',
' ', $term );
83 if ( preg_match_all(
'/([-!]?)(\S+)\s*/', $term, $m, PREG_SET_ORDER ) ) {
84 foreach ( $m as $terms ) {
85 if ( strlen( $terms[1] ) ) {
86 $searchstring .=
' & !';
88 if ( strtolower( $terms[2] ) ===
'and' ) {
89 $searchstring .=
' & ';
90 } elseif ( strtolower( $terms[2] ) ===
'or' || $terms[2] ===
'|' ) {
91 $searchstring .=
' | ';
92 } elseif ( strtolower( $terms[2] ) ===
'not' ) {
93 $searchstring .=
' & !';
95 $searchstring .=
" & $terms[2]";
100 # # Strip out leading junk
101 $searchstring = preg_replace(
'/^[\s\&\|]+/',
'', $searchstring );
103 # # Remove any doubled-up operators
104 $searchstring = preg_replace(
'/([\!\&\|]) +(?:[\&\|] +)+/',
"$1 ", $searchstring );
106 # # Remove any non-spaced operators (e.g. "Zounds!")
107 $searchstring = preg_replace(
'/([^ ])[\!\&\|]/',
"$1", $searchstring );
109 # # Remove any trailing whitespace or operators
110 $searchstring = preg_replace(
'/[\s\!\&\|]+$/',
'', $searchstring );
112 # # Remove unnecessary quotes around everything
113 $searchstring = preg_replace(
'/^[\'"](.*)[\'"]$/',
"$1", $searchstring );
115 # # Quote the whole thing
117 $searchstring =
$dbr->addQuotes( $searchstring );
119 wfDebug(
"parseQuery returned: $searchstring \n" );
121 return $searchstring;
132 # Get the SQL fragment for the given term
135 # # We need a separate query here so gin does not complain about empty searches
136 $sql =
"SELECT to_tsquery($searchstring)";
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" );
143 $top =
$res->fetchRow()[0];
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, pagecontent 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 " .
158 if ( preg_match_all(
"/'([^']+)'/", $top, $m, PREG_SET_ORDER ) ) {
159 foreach ( $m as $terms ) {
160 $this->searchTerms[$terms[1]] = $terms[1];
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, pagecontent 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)";
174 # # Namespaces - defaults to 0
175 if ( !is_null( $this->namespaces ) ) {
176 if ( count( $this->namespaces ) < 1 ) {
177 $query .=
' AND page_namespace = 0';
180 $query .=
" AND page_namespace IN ($namespaces)";
184 $query .=
" ORDER BY score DESC, page_id DESC";
186 $query .=
$dbr->limitResult(
'', $this->limit, $this->offset );
188 wfDebug(
"searchQuery returned: $query \n" );
193 # # Most of the work of these two functions are done automatically via triggers
196 # # We don't want to index older revisions
197 $slotRoleStore = MediaWikiServices::getInstance()->getSlotRoleStore();
198 $sql =
"UPDATE pagecontent 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 = " . $slotRoleStore->getId( SlotRecord::MAIN ) .
" " .
206 " AND c.content_id = s.slot_content_id " .
207 " ORDER BY old_rev_text_id DESC OFFSET 1)";
209 $dbw = $this->lb->getConnectionRef(
DB_MASTER );