42 $olderror = error_reporting( E_ERROR );
43 $resultSet = $this->db->query( $q,
'SearchPostgres',
true );
44 error_reporting( $olderror );
50 $olderror = error_reporting( E_ERROR );
51 $resultSet = $this->db->query( $q,
'SearchPostgres',
true );
52 error_reporting( $olderror );
65 wfDebug(
"parseQuery received: $term \n" );
67 # # No backslashes allowed
70 # # Collapse parens into nearby words:
71 $term = preg_replace(
'/\s*\(\s*/',
' (',
$term );
72 $term = preg_replace(
'/\s*\)\s*/',
') ',
$term );
74 # # Treat colons as word separators:
79 if ( preg_match_all(
'/([-!]?)(\S+)\s*/',
$term, $m, PREG_SET_ORDER ) ) {
80 foreach ( $m
as $terms ) {
81 if ( strlen( $terms[1] ) ) {
82 $searchstring .=
' & !';
84 if ( strtolower( $terms[2] ) ===
'and' ) {
85 $searchstring .=
' & ';
86 } elseif ( strtolower( $terms[2] ) ===
'or' || $terms[2] ===
'|' ) {
87 $searchstring .=
' | ';
88 } elseif ( strtolower( $terms[2] ) ===
'not' ) {
89 $searchstring .=
' & !';
91 $searchstring .=
" & $terms[2]";
96 # # Strip out leading junk
97 $searchstring = preg_replace(
'/^[\s\&\|]+/',
'', $searchstring );
99 # # Remove any doubled-up operators
100 $searchstring = preg_replace(
'/([\!\&\|]) +(?:[\&\|] +)+/',
"$1 ", $searchstring );
102 # # Remove any non-spaced operators (e.g. "Zounds!")
103 $searchstring = preg_replace(
'/([^ ])[\!\&\|]/',
"$1", $searchstring );
105 # # Remove any trailing whitespace or operators
106 $searchstring = preg_replace(
'/[\s\!\&\|]+$/',
'', $searchstring );
108 # # Remove unnecessary quotes around everything
109 $searchstring = preg_replace(
'/^[\'"](.*)[\'"]$/',
"$1", $searchstring );
111 # # Quote the whole thing
112 $searchstring = $this->db->addQuotes( $searchstring );
114 wfDebug(
"parseQuery returned: $searchstring \n" );
116 return $searchstring;
127 # Get the SQL fragment for the given term
130 # # We need a separate query here so gin does not complain about empty searches
131 $sql =
"SELECT to_tsquery($searchstring)";
132 $res = $this->db->query( $sql );
134 # # TODO: Better output (example to catch: one 'two)
135 die(
"Sorry, that was not a valid search string. Please go back and try again" );
137 $top =
$res->fetchRow()[0];
139 $this->searchTerms = [];
140 if ( $top ===
"" ) { # #
e.g.
if only stopwords
are used XXX
return something better
141 $query =
"SELECT page_id, page_namespace, page_title, 0 AS score " .
142 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
143 "AND r.rev_text_id = c.old_id AND 1=0";
146 if ( preg_match_all(
"/'([^']+)'/", $top, $m, PREG_SET_ORDER ) ) {
147 foreach ( $m
as $terms ) {
148 $this->searchTerms[$terms[1]] = $terms[1];
152 $query =
"SELECT page_id, page_namespace, page_title, " .
153 "ts_rank($fulltext, to_tsquery($searchstring), 5) 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 $fulltext @@ to_tsquery($searchstring)";
158 # # Namespaces - defaults to 0
161 $query .=
' AND page_namespace = 0';
164 $query .=
" AND page_namespace IN ($namespaces)";
168 $query .=
" ORDER BY score DESC, page_id DESC";
170 $query .= $this->db->limitResult(
'', $this->limit, $this->offset );
172 wfDebug(
"searchQuery returned: $query \n" );
177 # # Most of the work of these two functions are done automatically via triggers
180 # # We don't want to index older revisions
181 $sql =
"UPDATE pagecontent SET textvector = NULL WHERE textvector IS NOT NULL and old_id IN " .
182 "(SELECT DISTINCT rev_text_id FROM revision WHERE rev_page = " . intval( $pageid ) .
183 " ORDER BY rev_text_id DESC OFFSET 1)";
184 $this->db->query( $sql );