MediaWiki
REL1_22
|
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 00189 ## Namespaces - defaults to 0 00190 if ( !is_null( $this->namespaces ) ) { // null -> search all 00191 if ( count( $this->namespaces ) < 1 ) { 00192 $query .= ' AND page_namespace = 0'; 00193 } else { 00194 $namespaces = $this->db->makeList( $this->namespaces ); 00195 $query .= " AND page_namespace IN ($namespaces)"; 00196 } 00197 } 00198 00199 $query .= " ORDER BY score DESC, page_id DESC"; 00200 00201 $query .= $this->db->limitResult( '', $this->limit, $this->offset ); 00202 00203 wfDebug( "searchQuery returned: $query \n" ); 00204 00205 return $query; 00206 } 00207 00208 ## Most of the work of these two functions are done automatically via triggers 00209 00210 function update( $pageid, $title, $text ) { 00211 ## We don't want to index older revisions 00212 $SQL = "UPDATE pagecontent SET textvector = NULL WHERE old_id IN " . 00213 "(SELECT rev_text_id FROM revision WHERE rev_page = " . intval( $pageid ) . 00214 " ORDER BY rev_text_id DESC OFFSET 1)"; 00215 $this->db->query( $SQL ); 00216 return true; 00217 } 00218 00219 function updateTitle( $id, $title ) { 00220 return true; 00221 } 00222 00223 } ## end of the SearchPostgres class 00224 00228 class PostgresSearchResult extends SearchResult { 00229 function __construct( $row ) { 00230 parent::__construct( $row ); 00231 $this->score = $row->score; 00232 } 00233 function getScore() { 00234 return $this->score; 00235 } 00236 } 00237 00241 class PostgresSearchResultSet extends SqlSearchResultSet { 00242 function __construct( $resultSet, $terms ) { 00243 parent::__construct( $resultSet, $terms ); 00244 } 00245 00246 function next() { 00247 $row = $this->mResultSet->fetchObject(); 00248 if ( $row === false ) { 00249 return false; 00250 } else { 00251 return new PostgresSearchResult( $row ); 00252 } 00253 } 00254 }