MediaWiki
REL1_19
|
00001 <?php 00031 class SearchPostgres extends SearchEngine { 00032 00036 protected $db; 00041 function __construct( $db ) { 00042 parent::__construct( $db ); 00043 } 00044 00053 function searchTitle( $term ) { 00054 $q = $this->searchQuery( $term , 'titlevector', 'page_title' ); 00055 $olderror = error_reporting(E_ERROR); 00056 $resultSet = $this->db->resultObject( $this->db->query( $q, 'SearchPostgres', true ) ); 00057 error_reporting($olderror); 00058 if (!$resultSet) { 00059 // Needed for "Query requires full scan, GIN doesn't support it" 00060 return new SearchResultTooMany(); 00061 } 00062 return new PostgresSearchResultSet( $resultSet, $this->searchTerms ); 00063 } 00064 00065 function searchText( $term ) { 00066 $q = $this->searchQuery( $term, 'textvector', 'old_text' ); 00067 $olderror = error_reporting(E_ERROR); 00068 $resultSet = $this->db->resultObject( $this->db->query( $q, 'SearchPostgres', true ) ); 00069 error_reporting($olderror); 00070 if (!$resultSet) { 00071 return new SearchResultTooMany(); 00072 } 00073 return new PostgresSearchResultSet( $resultSet, $this->searchTerms ); 00074 } 00075 00084 function parseQuery( $term ) { 00085 00086 wfDebug( "parseQuery received: $term \n" ); 00087 00088 ## No backslashes allowed 00089 $term = preg_replace('/\\\/', '', $term); 00090 00091 ## Collapse parens into nearby words: 00092 $term = preg_replace('/\s*\(\s*/', ' (', $term); 00093 $term = preg_replace('/\s*\)\s*/', ') ', $term); 00094 00095 ## Treat colons as word separators: 00096 $term = preg_replace('/:/', ' ', $term); 00097 00098 $searchstring = ''; 00099 $m = array(); 00100 if( preg_match_all('/([-!]?)(\S+)\s*/', $term, $m, PREG_SET_ORDER ) ) { 00101 foreach( $m as $terms ) { 00102 if (strlen($terms[1])) { 00103 $searchstring .= ' & !'; 00104 } 00105 if (strtolower($terms[2]) === 'and') { 00106 $searchstring .= ' & '; 00107 } 00108 elseif (strtolower($terms[2]) === 'or' or $terms[2] === '|') { 00109 $searchstring .= ' | '; 00110 } 00111 elseif (strtolower($terms[2]) === 'not') { 00112 $searchstring .= ' & !'; 00113 } 00114 else { 00115 $searchstring .= " & $terms[2]"; 00116 } 00117 } 00118 } 00119 00120 ## Strip out leading junk 00121 $searchstring = preg_replace('/^[\s\&\|]+/', '', $searchstring); 00122 00123 ## Remove any doubled-up operators 00124 $searchstring = preg_replace('/([\!\&\|]) +(?:[\&\|] +)+/', "$1 ", $searchstring); 00125 00126 ## Remove any non-spaced operators (e.g. "Zounds!") 00127 $searchstring = preg_replace('/([^ ])[\!\&\|]/', "$1", $searchstring); 00128 00129 ## Remove any trailing whitespace or operators 00130 $searchstring = preg_replace('/[\s\!\&\|]+$/', '', $searchstring); 00131 00132 ## Remove unnecessary quotes around everything 00133 $searchstring = preg_replace('/^[\'"](.*)[\'"]$/', "$1", $searchstring); 00134 00135 ## Quote the whole thing 00136 $searchstring = $this->db->addQuotes($searchstring); 00137 00138 wfDebug( "parseQuery returned: $searchstring \n" ); 00139 00140 return $searchstring; 00141 00142 } 00143 00150 function searchQuery( $term, $fulltext, $colname ) { 00151 # Get the SQL fragment for the given term 00152 $searchstring = $this->parseQuery( $term ); 00153 00154 ## We need a separate query here so gin does not complain about empty searches 00155 $SQL = "SELECT to_tsquery($searchstring)"; 00156 $res = $this->db->query($SQL); 00157 if (!$res) { 00158 ## TODO: Better output (example to catch: one 'two) 00159 die ("Sorry, that was not a valid search string. Please go back and try again"); 00160 } 00161 $top = $res->fetchRow(); 00162 $top = $top[0]; 00163 00164 if ($top === "") { ## e.g. if only stopwords are used XXX return something better 00165 $query = "SELECT page_id, page_namespace, page_title, 0 AS score ". 00166 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " . 00167 "AND r.rev_text_id = c.old_id AND 1=0"; 00168 } 00169 else { 00170 $m = array(); 00171 if( preg_match_all("/'([^']+)'/", $top, $m, PREG_SET_ORDER ) ) { 00172 foreach( $m as $terms ) { 00173 $this->searchTerms[$terms[1]] = $terms[1]; 00174 } 00175 } 00176 00177 $query = "SELECT page_id, page_namespace, page_title, ". 00178 "ts_rank($fulltext, to_tsquery($searchstring), 5) AS score ". 00179 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " . 00180 "AND r.rev_text_id = c.old_id AND $fulltext @@ to_tsquery($searchstring)"; 00181 } 00182 00183 ## Redirects 00184 if (! $this->showRedirects) 00185 $query .= ' AND page_is_redirect = 0'; 00186 00187 ## Namespaces - defaults to 0 00188 if( !is_null($this->namespaces) ){ // null -> search all 00189 if ( count($this->namespaces) < 1) 00190 $query .= ' AND page_namespace = 0'; 00191 else { 00192 $namespaces = $this->db->makeList( $this->namespaces ); 00193 $query .= " AND page_namespace IN ($namespaces)"; 00194 } 00195 } 00196 00197 $query .= " ORDER BY score DESC, page_id DESC"; 00198 00199 $query .= $this->db->limitResult( '', $this->limit, $this->offset ); 00200 00201 wfDebug( "searchQuery returned: $query \n" ); 00202 00203 return $query; 00204 } 00205 00206 ## Most of the work of these two functions are done automatically via triggers 00207 00208 function update( $pageid, $title, $text ) { 00209 ## We don't want to index older revisions 00210 $SQL = "UPDATE pagecontent SET textvector = NULL WHERE old_id IN ". 00211 "(SELECT rev_text_id FROM revision WHERE rev_page = " . intval( $pageid ) . 00212 " ORDER BY rev_text_id DESC OFFSET 1)"; 00213 $this->db->query($SQL); 00214 return true; 00215 } 00216 00217 function updateTitle( $id, $title ) { 00218 return true; 00219 } 00220 00221 } ## end of the SearchPostgres class 00222 00226 class PostgresSearchResult extends SearchResult { 00227 function __construct( $row ) { 00228 parent::__construct($row); 00229 $this->score = $row->score; 00230 } 00231 function getScore() { 00232 return $this->score; 00233 } 00234 } 00235 00239 class PostgresSearchResultSet extends SqlSearchResultSet { 00240 function __construct( $resultSet, $terms ) { 00241 parent::__construct( $resultSet, $terms ); 00242 } 00243 00244 function next() { 00245 $row = $this->mResultSet->fetchObject(); 00246 if( $row === false ) { 00247 return false; 00248 } else { 00249 return new PostgresSearchResult( $row ); 00250 } 00251 } 00252 }