42 $olderror = error_reporting( E_ERROR );
43 $resultSet = $this->db->resultObject( $this->db->query( $q,
'SearchPostgres',
true ) );
44 error_reporting( $olderror );
54 $olderror = error_reporting( E_ERROR );
55 $resultSet = $this->db->resultObject( $this->db->query( $q,
'SearchPostgres',
true ) );
56 error_reporting( $olderror );
73 wfDebug(
"parseQuery received: $term \n" );
75 ## No backslashes allowed
78 ## Collapse parens into nearby words:
79 $term = preg_replace(
'/\s*\(\s*/',
' (',
$term );
80 $term = preg_replace(
'/\s*\)\s*/',
') ',
$term );
82 ## Treat colons as word separators:
87 if ( preg_match_all(
'/([-!]?)(\S+)\s*/',
$term, $m, PREG_SET_ORDER ) ) {
88 foreach ( $m
as $terms ) {
89 if ( strlen( $terms[1] ) ) {
90 $searchstring .=
' & !';
92 if ( strtolower( $terms[2] ) ===
'and' ) {
93 $searchstring .=
' & ';
95 elseif ( strtolower( $terms[2] ) ===
'or' or $terms[2] ===
'|' ) {
96 $searchstring .=
' | ';
98 elseif ( strtolower( $terms[2] ) ===
'not' ) {
99 $searchstring .=
' & !';
102 $searchstring .=
" & $terms[2]";
107 ## Strip out leading junk
108 $searchstring = preg_replace(
'/^[\s\&\|]+/',
'', $searchstring );
110 ## Remove any doubled-up operators
111 $searchstring = preg_replace(
'/([\!\&\|]) +(?:[\&\|] +)+/',
"$1 ", $searchstring );
113 ## Remove any non-spaced operators (e.g. "Zounds!")
114 $searchstring = preg_replace(
'/([^ ])[\!\&\|]/',
"$1", $searchstring );
116 ## Remove any trailing whitespace or operators
117 $searchstring = preg_replace(
'/[\s\!\&\|]+$/',
'', $searchstring );
119 ## Remove unnecessary quotes around everything
120 $searchstring = preg_replace(
'/^[\'"](.*)[\'"]$/',
"$1", $searchstring );
122 ## Quote the whole thing
123 $searchstring = $this->db->addQuotes( $searchstring );
125 wfDebug(
"parseQuery returned: $searchstring \n" );
127 return $searchstring;
139 # Get the SQL fragment for the given term
142 ## We need a separate query here so gin does not complain about empty searches
143 $sql =
"SELECT to_tsquery($searchstring)";
144 $res = $this->db->query( $sql );
146 ## TODO: Better output (example to catch: one 'two)
147 die(
"Sorry, that was not a valid search string. Please go back and try again" );
149 $top =
$res->fetchRow();
152 if ( $top ===
"" ) { ##
e.g.
if only stopwords
are used XXX
return something better
153 $query =
"SELECT page_id, page_namespace, page_title, 0 AS score " .
154 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
155 "AND r.rev_text_id = c.old_id AND 1=0";
159 if ( preg_match_all(
"/'([^']+)'/", $top, $m, PREG_SET_ORDER ) ) {
160 foreach ( $m
as $terms ) {
161 $this->searchTerms[$terms[1]] = $terms[1];
165 $query =
"SELECT page_id, page_namespace, page_title, " .
166 "ts_rank($fulltext, to_tsquery($searchstring), 5) AS score " .
167 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
168 "AND r.rev_text_id = c.old_id AND $fulltext @@ to_tsquery($searchstring)";
171 ## Namespaces - defaults to 0
174 $query .=
' AND page_namespace = 0';
177 $query .=
" AND page_namespace IN ($namespaces)";
181 $query .=
" ORDER BY score DESC, page_id DESC";
183 $query .= $this->db->limitResult(
'', $this->limit, $this->offset );
185 wfDebug(
"searchQuery returned: $query \n" );
190 ## Most of the work of these two functions are done automatically via triggers
193 ## We don't want to index older revisions
194 $sql =
"UPDATE pagecontent SET textvector = NULL WHERE old_id IN " .
195 "(SELECT rev_text_id FROM revision WHERE rev_page = " . intval( $pageid ) .
196 " ORDER BY rev_text_id DESC OFFSET 1)";
197 $this->db->query( $sql );
212 parent::__construct( $row );
213 $this->score = $row->score;
226 parent::__construct( $resultSet, $terms );
230 $row = $this->mResultSet->fetchObject();
231 if ( $row ===
false ) {