MediaWiki  REL1_24
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->query( $q, 'SearchPostgres', true );
00044         error_reporting( $olderror );
00045         return new SqlSearchResultSet( $resultSet, $this->searchTerms );
00046     }
00047 
00048     function searchText( $term ) {
00049         $q = $this->searchQuery( $term, 'textvector', 'old_text' );
00050         $olderror = error_reporting( E_ERROR );
00051         $resultSet = $this->db->query( $q, 'SearchPostgres', true );
00052         error_reporting( $olderror );
00053         return new SqlSearchResultSet( $resultSet, $this->searchTerms );
00054     }
00055 
00064     function parseQuery( $term ) {
00065 
00066         wfDebug( "parseQuery received: $term \n" );
00067 
00068         ## No backslashes allowed
00069         $term = preg_replace( '/\\\/', '', $term );
00070 
00071         ## Collapse parens into nearby words:
00072         $term = preg_replace( '/\s*\(\s*/', ' (', $term );
00073         $term = preg_replace( '/\s*\)\s*/', ') ', $term );
00074 
00075         ## Treat colons as word separators:
00076         $term = preg_replace( '/:/', ' ', $term );
00077 
00078         $searchstring = '';
00079         $m = array();
00080         if ( preg_match_all( '/([-!]?)(\S+)\s*/', $term, $m, PREG_SET_ORDER ) ) {
00081             foreach ( $m as $terms ) {
00082                 if ( strlen( $terms[1] ) ) {
00083                     $searchstring .= ' & !';
00084                 }
00085                 if ( strtolower( $terms[2] ) === 'and' ) {
00086                     $searchstring .= ' & ';
00087                 }
00088                 elseif ( strtolower( $terms[2] ) === 'or' or $terms[2] === '|' ) {
00089                     $searchstring .= ' | ';
00090                 }
00091                 elseif ( strtolower( $terms[2] ) === 'not' ) {
00092                     $searchstring .= ' & !';
00093                 }
00094                 else {
00095                     $searchstring .= " & $terms[2]";
00096                 }
00097             }
00098         }
00099 
00100         ## Strip out leading junk
00101         $searchstring = preg_replace( '/^[\s\&\|]+/', '', $searchstring );
00102 
00103         ## Remove any doubled-up operators
00104         $searchstring = preg_replace( '/([\!\&\|]) +(?:[\&\|] +)+/', "$1 ", $searchstring );
00105 
00106         ## Remove any non-spaced operators (e.g. "Zounds!")
00107         $searchstring = preg_replace( '/([^ ])[\!\&\|]/', "$1", $searchstring );
00108 
00109         ## Remove any trailing whitespace or operators
00110         $searchstring = preg_replace( '/[\s\!\&\|]+$/', '', $searchstring );
00111 
00112         ## Remove unnecessary quotes around everything
00113         $searchstring = preg_replace( '/^[\'"](.*)[\'"]$/', "$1", $searchstring );
00114 
00115         ## Quote the whole thing
00116         $searchstring = $this->db->addQuotes( $searchstring );
00117 
00118         wfDebug( "parseQuery returned: $searchstring \n" );
00119 
00120         return $searchstring;
00121 
00122     }
00123 
00131     function searchQuery( $term, $fulltext, $colname ) {
00132         # Get the SQL fragment for the given term
00133         $searchstring = $this->parseQuery( $term );
00134 
00135         ## We need a separate query here so gin does not complain about empty searches
00136         $sql = "SELECT to_tsquery($searchstring)";
00137         $res = $this->db->query( $sql );
00138         if ( !$res ) {
00139             ## TODO: Better output (example to catch: one 'two)
00140             die( "Sorry, that was not a valid search string. Please go back and try again" );
00141         }
00142         $top = $res->fetchRow();
00143         $top = $top[0];
00144 
00145         $this->searchTerms = array();
00146         if ( $top === "" ) { ## e.g. if only stopwords are used XXX return something better
00147             $query = "SELECT page_id, page_namespace, page_title, 0 AS score " .
00148                 "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
00149                 "AND r.rev_text_id = c.old_id AND 1=0";
00150         }
00151         else {
00152             $m = array();
00153             if ( preg_match_all( "/'([^']+)'/", $top, $m, PREG_SET_ORDER ) ) {
00154                 foreach ( $m as $terms ) {
00155                     $this->searchTerms[$terms[1]] = $terms[1];
00156                 }
00157             }
00158 
00159             $query = "SELECT page_id, page_namespace, page_title, " .
00160             "ts_rank($fulltext, to_tsquery($searchstring), 5) AS score " .
00161             "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
00162             "AND r.rev_text_id = c.old_id AND $fulltext @@ to_tsquery($searchstring)";
00163         }
00164 
00165         ## Namespaces - defaults to 0
00166         if ( !is_null( $this->namespaces ) ) { // null -> search all
00167             if ( count( $this->namespaces ) < 1 ) {
00168                 $query .= ' AND page_namespace = 0';
00169             } else {
00170                 $namespaces = $this->db->makeList( $this->namespaces );
00171                 $query .= " AND page_namespace IN ($namespaces)";
00172             }
00173         }
00174 
00175         $query .= " ORDER BY score DESC, page_id DESC";
00176 
00177         $query .= $this->db->limitResult( '', $this->limit, $this->offset );
00178 
00179         wfDebug( "searchQuery returned: $query \n" );
00180 
00181         return $query;
00182     }
00183 
00184     ## Most of the work of these two functions are done automatically via triggers
00185 
00186     function update( $pageid, $title, $text ) {
00187         ## We don't want to index older revisions
00188         $sql = "UPDATE pagecontent SET textvector = NULL WHERE textvector IS NOT NULL and old_id IN " .
00189                 "(SELECT rev_text_id FROM revision WHERE rev_page = " . intval( $pageid ) .
00190                 " ORDER BY rev_text_id DESC OFFSET 1)";
00191         $this->db->query( $sql );
00192         return true;
00193     }
00194 
00195     function updateTitle( $id, $title ) {
00196         return true;
00197     }
00198 
00199 }