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