MediaWiki  1.28.0
SearchPostgres.php
Go to the documentation of this file.
1 <?php
40  function searchTitle( $term ) {
41  $q = $this->searchQuery( $term, 'titlevector', 'page_title' );
42  $olderror = error_reporting( E_ERROR );
43  $resultSet = $this->db->query( $q, 'SearchPostgres', true );
44  error_reporting( $olderror );
45  return new SqlSearchResultSet( $resultSet, $this->searchTerms );
46  }
47 
48  function searchText( $term ) {
49  $q = $this->searchQuery( $term, 'textvector', 'old_text' );
50  $olderror = error_reporting( E_ERROR );
51  $resultSet = $this->db->query( $q, 'SearchPostgres', true );
52  error_reporting( $olderror );
53  return new SqlSearchResultSet( $resultSet, $this->searchTerms );
54  }
55 
64  function parseQuery( $term ) {
65 
66  wfDebug( "parseQuery received: $term \n" );
67 
68  # # No backslashes allowed
69  $term = preg_replace( '/\\\/', '', $term );
70 
71  # # Collapse parens into nearby words:
72  $term = preg_replace( '/\s*\(\s*/', ' (', $term );
73  $term = preg_replace( '/\s*\)\s*/', ') ', $term );
74 
75  # # Treat colons as word separators:
76  $term = preg_replace( '/:/', ' ', $term );
77 
78  $searchstring = '';
79  $m = [];
80  if ( preg_match_all( '/([-!]?)(\S+)\s*/', $term, $m, PREG_SET_ORDER ) ) {
81  foreach ( $m as $terms ) {
82  if ( strlen( $terms[1] ) ) {
83  $searchstring .= ' & !';
84  }
85  if ( strtolower( $terms[2] ) === 'and' ) {
86  $searchstring .= ' & ';
87  } elseif ( strtolower( $terms[2] ) === 'or' || $terms[2] === '|' ) {
88  $searchstring .= ' | ';
89  } elseif ( strtolower( $terms[2] ) === 'not' ) {
90  $searchstring .= ' & !';
91  } else {
92  $searchstring .= " & $terms[2]";
93  }
94  }
95  }
96 
97  # # Strip out leading junk
98  $searchstring = preg_replace( '/^[\s\&\|]+/', '', $searchstring );
99 
100  # # Remove any doubled-up operators
101  $searchstring = preg_replace( '/([\!\&\|]) +(?:[\&\|] +)+/', "$1 ", $searchstring );
102 
103  # # Remove any non-spaced operators (e.g. "Zounds!")
104  $searchstring = preg_replace( '/([^ ])[\!\&\|]/', "$1", $searchstring );
105 
106  # # Remove any trailing whitespace or operators
107  $searchstring = preg_replace( '/[\s\!\&\|]+$/', '', $searchstring );
108 
109  # # Remove unnecessary quotes around everything
110  $searchstring = preg_replace( '/^[\'"](.*)[\'"]$/', "$1", $searchstring );
111 
112  # # Quote the whole thing
113  $searchstring = $this->db->addQuotes( $searchstring );
114 
115  wfDebug( "parseQuery returned: $searchstring \n" );
116 
117  return $searchstring;
118 
119  }
120 
128  function searchQuery( $term, $fulltext, $colname ) {
129  # Get the SQL fragment for the given term
130  $searchstring = $this->parseQuery( $term );
131 
132  # # We need a separate query here so gin does not complain about empty searches
133  $sql = "SELECT to_tsquery($searchstring)";
134  $res = $this->db->query( $sql );
135  if ( !$res ) {
136  # # TODO: Better output (example to catch: one 'two)
137  die( "Sorry, that was not a valid search string. Please go back and try again" );
138  }
139  $top = $res->fetchRow()[0];
140 
141  $this->searchTerms = [];
142  if ( $top === "" ) { # # e.g. if only stopwords are used XXX return something better
143  $query = "SELECT page_id, page_namespace, page_title, 0 AS score " .
144  "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
145  "AND r.rev_text_id = c.old_id AND 1=0";
146  } else {
147  $m = [];
148  if ( preg_match_all( "/'([^']+)'/", $top, $m, PREG_SET_ORDER ) ) {
149  foreach ( $m as $terms ) {
150  $this->searchTerms[$terms[1]] = $terms[1];
151  }
152  }
153 
154  $query = "SELECT page_id, page_namespace, page_title, " .
155  "ts_rank($fulltext, to_tsquery($searchstring), 5) AS score " .
156  "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
157  "AND r.rev_text_id = c.old_id AND $fulltext @@ to_tsquery($searchstring)";
158  }
159 
160  # # Namespaces - defaults to 0
161  if ( !is_null( $this->namespaces ) ) { // null -> search all
162  if ( count( $this->namespaces ) < 1 ) {
163  $query .= ' AND page_namespace = 0';
164  } else {
165  $namespaces = $this->db->makeList( $this->namespaces );
166  $query .= " AND page_namespace IN ($namespaces)";
167  }
168  }
169 
170  $query .= " ORDER BY score DESC, page_id DESC";
171 
172  $query .= $this->db->limitResult( '', $this->limit, $this->offset );
173 
174  wfDebug( "searchQuery returned: $query \n" );
175 
176  return $query;
177  }
178 
179  # # Most of the work of these two functions are done automatically via triggers
180 
181  function update( $pageid, $title, $text ) {
182  # # We don't want to index older revisions
183  $sql = "UPDATE pagecontent SET textvector = NULL WHERE textvector IS NOT NULL and old_id IN " .
184  "(SELECT DISTINCT rev_text_id FROM revision WHERE rev_page = " . intval( $pageid ) .
185  " ORDER BY rev_text_id DESC OFFSET 1)";
186  $this->db->query( $sql );
187  return true;
188  }
189 
190  function updateTitle( $id, $title ) {
191  return true;
192  }
193 
194 }
you don t have to do a grep find to see where the $wgReverseTitle variable is used
Definition: hooks.txt:117
external whereas SearchGetNearMatch runs after $term
Definition: hooks.txt:2713
searchTitle($term)
Perform a full text search query via tsearch2 and return a result set.
update($pageid, $title, $text)
null for the local wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition: hooks.txt:1555
to move a page</td >< td > &*You are moving the page across namespaces
in this case you re responsible for computing and outputting the entire conflict i e
Definition: hooks.txt:1376
wfDebug($text, $dest= 'all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
The ContentHandler facility adds support for arbitrary content types on wiki instead of relying on wikitext for everything It was introduced in MediaWiki Each kind of and so on Built in content types are
parseQuery($term)
Transform the user's search string into a better form for tsearch2 Returns an SQL fragment consisting...
$res
Definition: database.txt:21
This class is used for different SQL-based search engines shipped with MediaWiki. ...
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:953
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
int[] null $namespaces
updateTitle($id, $title)
searchQuery($term, $fulltext, $colname)
Construct the full SQL query to do the search.
Base search engine base class for database-backed searches.
Search engine hook base class for Postgres.