MediaWiki
REL1_20
|
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 00151 function searchQuery( $term, $fulltext, $colname ) { 00152 # Get the SQL fragment for the given term 00153 $searchstring = $this->parseQuery( $term ); 00154 00155 ## We need a separate query here so gin does not complain about empty searches 00156 $SQL = "SELECT to_tsquery($searchstring)"; 00157 $res = $this->db->query($SQL); 00158 if (!$res) { 00159 ## TODO: Better output (example to catch: one 'two) 00160 die ("Sorry, that was not a valid search string. Please go back and try again"); 00161 } 00162 $top = $res->fetchRow(); 00163 $top = $top[0]; 00164 00165 if ($top === "") { ## e.g. if only stopwords are used XXX return something better 00166 $query = "SELECT page_id, page_namespace, page_title, 0 AS score ". 00167 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " . 00168 "AND r.rev_text_id = c.old_id AND 1=0"; 00169 } 00170 else { 00171 $m = array(); 00172 if( preg_match_all("/'([^']+)'/", $top, $m, PREG_SET_ORDER ) ) { 00173 foreach( $m as $terms ) { 00174 $this->searchTerms[$terms[1]] = $terms[1]; 00175 } 00176 } 00177 00178 $query = "SELECT page_id, page_namespace, page_title, ". 00179 "ts_rank($fulltext, to_tsquery($searchstring), 5) AS score ". 00180 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " . 00181 "AND r.rev_text_id = c.old_id AND $fulltext @@ to_tsquery($searchstring)"; 00182 } 00183 00184 ## Redirects 00185 if (! $this->showRedirects) 00186 $query .= ' AND page_is_redirect = 0'; 00187 00188 ## Namespaces - defaults to 0 00189 if( !is_null($this->namespaces) ){ // null -> search all 00190 if ( count($this->namespaces) < 1) 00191 $query .= ' AND page_namespace = 0'; 00192 else { 00193 $namespaces = $this->db->makeList( $this->namespaces ); 00194 $query .= " AND page_namespace IN ($namespaces)"; 00195 } 00196 } 00197 00198 $query .= " ORDER BY score DESC, page_id DESC"; 00199 00200 $query .= $this->db->limitResult( '', $this->limit, $this->offset ); 00201 00202 wfDebug( "searchQuery returned: $query \n" ); 00203 00204 return $query; 00205 } 00206 00207 ## Most of the work of these two functions are done automatically via triggers 00208 00209 function update( $pageid, $title, $text ) { 00210 ## We don't want to index older revisions 00211 $SQL = "UPDATE pagecontent SET textvector = NULL WHERE old_id IN ". 00212 "(SELECT rev_text_id FROM revision WHERE rev_page = " . intval( $pageid ) . 00213 " ORDER BY rev_text_id DESC OFFSET 1)"; 00214 $this->db->query($SQL); 00215 return true; 00216 } 00217 00218 function updateTitle( $id, $title ) { 00219 return true; 00220 } 00221 00222 } ## end of the SearchPostgres class 00223 00227 class PostgresSearchResult extends SearchResult { 00228 function __construct( $row ) { 00229 parent::__construct($row); 00230 $this->score = $row->score; 00231 } 00232 function getScore() { 00233 return $this->score; 00234 } 00235 } 00236 00240 class PostgresSearchResultSet extends SqlSearchResultSet { 00241 function __construct( $resultSet, $terms ) { 00242 parent::__construct( $resultSet, $terms ); 00243 } 00244 00245 function next() { 00246 $row = $this->mResultSet->fetchObject(); 00247 if( $row === false ) { 00248 return false; 00249 } else { 00250 return new PostgresSearchResult( $row ); 00251 } 00252 } 00253 }