MediaWiki  1.23.12
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->resultObject( $this->db->query( $q, 'SearchPostgres', true ) );
44  error_reporting( $olderror );
45  if ( !$resultSet ) {
46  // Needed for "Query requires full scan, GIN doesn't support it"
47  return new SearchResultTooMany();
48  }
49  return new PostgresSearchResultSet( $resultSet, $this->searchTerms );
50  }
51 
52  function searchText( $term ) {
53  $q = $this->searchQuery( $term, 'textvector', 'old_text' );
54  $olderror = error_reporting( E_ERROR );
55  $resultSet = $this->db->resultObject( $this->db->query( $q, 'SearchPostgres', true ) );
56  error_reporting( $olderror );
57  if ( !$resultSet ) {
58  return new SearchResultTooMany();
59  }
60  return new PostgresSearchResultSet( $resultSet, $this->searchTerms );
61  }
62 
71  function parseQuery( $term ) {
72 
73  wfDebug( "parseQuery received: $term \n" );
74 
75  ## No backslashes allowed
76  $term = preg_replace( '/\\\/', '', $term );
77 
78  ## Collapse parens into nearby words:
79  $term = preg_replace( '/\s*\(\s*/', ' (', $term );
80  $term = preg_replace( '/\s*\)\s*/', ') ', $term );
81 
82  ## Treat colons as word separators:
83  $term = preg_replace( '/:/', ' ', $term );
84 
85  $searchstring = '';
86  $m = array();
87  if ( preg_match_all( '/([-!]?)(\S+)\s*/', $term, $m, PREG_SET_ORDER ) ) {
88  foreach ( $m as $terms ) {
89  if ( strlen( $terms[1] ) ) {
90  $searchstring .= ' & !';
91  }
92  if ( strtolower( $terms[2] ) === 'and' ) {
93  $searchstring .= ' & ';
94  }
95  elseif ( strtolower( $terms[2] ) === 'or' or $terms[2] === '|' ) {
96  $searchstring .= ' | ';
97  }
98  elseif ( strtolower( $terms[2] ) === 'not' ) {
99  $searchstring .= ' & !';
100  }
101  else {
102  $searchstring .= " & $terms[2]";
103  }
104  }
105  }
106 
107  ## Strip out leading junk
108  $searchstring = preg_replace( '/^[\s\&\|]+/', '', $searchstring );
109 
110  ## Remove any doubled-up operators
111  $searchstring = preg_replace( '/([\!\&\|]) +(?:[\&\|] +)+/', "$1 ", $searchstring );
112 
113  ## Remove any non-spaced operators (e.g. "Zounds!")
114  $searchstring = preg_replace( '/([^ ])[\!\&\|]/', "$1", $searchstring );
115 
116  ## Remove any trailing whitespace or operators
117  $searchstring = preg_replace( '/[\s\!\&\|]+$/', '', $searchstring );
118 
119  ## Remove unnecessary quotes around everything
120  $searchstring = preg_replace( '/^[\'"](.*)[\'"]$/', "$1", $searchstring );
121 
122  ## Quote the whole thing
123  $searchstring = $this->db->addQuotes( $searchstring );
124 
125  wfDebug( "parseQuery returned: $searchstring \n" );
126 
127  return $searchstring;
128 
129  }
130 
138  function searchQuery( $term, $fulltext, $colname ) {
139  # Get the SQL fragment for the given term
140  $searchstring = $this->parseQuery( $term );
141 
142  ## We need a separate query here so gin does not complain about empty searches
143  $sql = "SELECT to_tsquery($searchstring)";
144  $res = $this->db->query( $sql );
145  if ( !$res ) {
146  ## TODO: Better output (example to catch: one 'two)
147  die( "Sorry, that was not a valid search string. Please go back and try again" );
148  }
149  $top = $res->fetchRow();
150  $top = $top[0];
151 
152  if ( $top === "" ) { ## e.g. if only stopwords are used XXX return something better
153  $query = "SELECT page_id, page_namespace, page_title, 0 AS score " .
154  "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
155  "AND r.rev_text_id = c.old_id AND 1=0";
156  }
157  else {
158  $m = array();
159  if ( preg_match_all( "/'([^']+)'/", $top, $m, PREG_SET_ORDER ) ) {
160  foreach ( $m as $terms ) {
161  $this->searchTerms[$terms[1]] = $terms[1];
162  }
163  }
164 
165  $query = "SELECT page_id, page_namespace, page_title, " .
166  "ts_rank($fulltext, to_tsquery($searchstring), 5) AS score " .
167  "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
168  "AND r.rev_text_id = c.old_id AND $fulltext @@ to_tsquery($searchstring)";
169  }
170 
171  ## Namespaces - defaults to 0
172  if ( !is_null( $this->namespaces ) ) { // null -> search all
173  if ( count( $this->namespaces ) < 1 ) {
174  $query .= ' AND page_namespace = 0';
175  } else {
176  $namespaces = $this->db->makeList( $this->namespaces );
177  $query .= " AND page_namespace IN ($namespaces)";
178  }
179  }
180 
181  $query .= " ORDER BY score DESC, page_id DESC";
182 
183  $query .= $this->db->limitResult( '', $this->limit, $this->offset );
184 
185  wfDebug( "searchQuery returned: $query \n" );
186 
187  return $query;
188  }
189 
190  ## Most of the work of these two functions are done automatically via triggers
191 
192  function update( $pageid, $title, $text ) {
193  ## We don't want to index older revisions
194  $sql = "UPDATE pagecontent SET textvector = NULL WHERE textvector IS NOT NULL and old_id IN " .
195  "(SELECT DISTINCT rev_text_id FROM revision WHERE rev_page = " . intval( $pageid ) .
196  " ORDER BY rev_text_id DESC OFFSET 1)";
197  $this->db->query( $sql );
198  return true;
199  }
200 
201  function updateTitle( $id, $title ) {
202  return true;
203  }
204 
205 } ## end of the SearchPostgres class
206 
211  function __construct( $row ) {
212  parent::__construct( $row );
213  $this->score = $row->score;
214  }
215 
216  function getScore() {
217  return $this->score;
218  }
219 }
220 
225  function __construct( $resultSet, $terms ) {
226  parent::__construct( $resultSet, $terms );
227  }
228 
229  function next() {
230  $row = $this->mResultSet->fetchObject();
231  if ( $row === false ) {
232  return false;
233  } else {
234  return new PostgresSearchResult( $row );
235  }
236  }
237 }
SearchPostgres\update
update( $pageid, $title, $text)
Create or update the search index record for the given page.
Definition: SearchPostgres.php:192
of
globals txt Globals are evil The original MediaWiki code relied on globals for processing context far too often MediaWiki development since then has been a story of slowly moving context out of global variables and into objects Storing processing context in object member variables allows those objects to be reused in a much more flexible way Consider the elegance of
Definition: globals.txt:10
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
SearchPostgres\searchText
searchText( $term)
Perform a full text search query and return a result set.
Definition: SearchPostgres.php:52
PostgresSearchResultSet\__construct
__construct( $resultSet, $terms)
Definition: SearchPostgres.php:225
SearchPostgres\parseQuery
parseQuery( $term)
Transform the user's search string into a better form for tsearch2 Returns an SQL fragment consisting...
Definition: SearchPostgres.php:71
PostgresSearchResultSet\next
next()
Fetches next search result, or false.
Definition: SearchPostgres.php:229
e
in this case you re responsible for computing and outputting the entire conflict i e
Definition: hooks.txt:1038
PostgresSearchResult\__construct
__construct( $row)
Definition: SearchPostgres.php:211
SearchPostgres
Search engine hook base class for Postgres.
Definition: SearchPostgres.php:31
SearchPostgres\searchQuery
searchQuery( $term, $fulltext, $colname)
Construct the full SQL query to do the search.
Definition: SearchPostgres.php:138
PostgresSearchResult\getScore
getScore()
Definition: SearchPostgres.php:216
SearchPostgres\searchTitle
searchTitle( $term)
Perform a full text search query via tsearch2 and return a result set.
Definition: SearchPostgres.php:40
SearchDatabase
Base search engine base class for database-backed searches.
Definition: SearchDatabase.php:29
namespaces
to move a page</td >< td > &*You are moving the page across namespaces
Definition: All_system_messages.txt:2677
SearchResult
Definition: SearchResult.php:30
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
wfDebug
wfDebug( $text, $dest='all')
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:980
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
SqlSearchResultSet
This class is used for different SQL-based search engines shipped with MediaWiki.
Definition: SearchResultSet.php:140
SearchResultTooMany
Definition: SearchEngine.php:565
SearchEngine\$namespaces
$namespaces
Definition: SearchEngine.php:37
only
published in in Madrid In the first edition of the Vocabolario for was published In in Rotterdam was the Dictionnaire Universel ! html< p > The first monolingual dictionary written in a Romance language was< i > Sebastián Covarrubias</i >< i > Tesoro de la lengua castellana o published in in Madrid In the first edition of the< i > Vocabolario dell< a href="/index.php?title=Accademia_della_Crusca&amp;action=edit&amp;redlink=1" class="new" title="Accademia della Crusca (page does not exist)"> Accademia della Crusca</a ></i > for was published In in Rotterdam was the< i > Dictionnaire Universel</i ></p > ! end ! test Italics and ! wikitext foo ! html< p >< i > foo</i ></p > !end ! test Italics and ! wikitext foo ! html< p >< i > foo</i ></p > !end ! test Italics and ! wikitext foo ! html< p >< i > foo</i ></p > !end ! test Italics and ! wikitext foo ! html php< p >< i > foo</i ></p > ! html parsoid< p >< i > foo</i >< b ></b ></p > !end ! test Italics and ! wikitext foo ! html< p >< i > foo</i ></p > !end ! test Italics and ! wikitext foo ! html< p >< b > foo</b ></p > !end ! test Italics and ! wikitext foo ! html< p >< b > foo</b ></p > !end ! test Italics and ! wikitext foo ! html php< p >< b > foo</b ></p > ! html parsoid< p >< b > foo</b >< i ></i ></p > !end ! test Italics and ! wikitext foo ! html< p >< i > foo</i ></p > !end ! test Italics and ! wikitext foo ! html< p >< b > foo</b ></p > !end ! test Italics and ! wikitext foo ! html< p >< b > foo</b ></p > !end ! test Italics and ! wikitext foo ! html php< p >< b > foo</b ></p > ! html parsoid< p >< b > foo</b >< i ></i ></p > !end ! test Italics and ! options ! wikitext foo ! html< p >< b >< i > foo</i ></b ></p > !end ! test Italics and ! wikitext foo ! html< p >< i >< b > foo</b ></i ></p > !end ! test Italics and ! wikitext foo ! html< p >< i >< b > foo</b ></i ></p > !end ! test Italics and ! wikitext foo ! html< p >< i >< b > foo</b ></i ></p > !end ! test Italics and ! wikitext foo bar ! html< p >< i > foo< b > bar</b ></i ></p > !end ! test Italics and ! wikitext foo bar ! html< p >< i > foo< b > bar</b ></i ></p > !end ! test Italics and ! wikitext foo bar ! html< p >< i > foo< b > bar</b ></i ></p > !end ! test Italics and ! wikitext foo bar ! html php< p >< b > foo</b > bar</p > ! html parsoid< p >< b > foo</b > bar< i ></i ></p > !end ! test Italics and ! wikitext foo bar ! html php< p >< b > foo</b > bar</p > ! html parsoid< p >< b > foo</b > bar< b ></b ></p > !end ! test Italics and ! wikitext this is about foo s family ! html< p >< i > this is about< b > foo s family</b ></i ></p > !end ! test Italics and ! wikitext this is about foo s family ! html< p >< i > this is about< b > foo s</b > family</i ></p > !end ! test Italics and ! wikitext this is about foo s family ! html< p >< b > this is about< i > foo</i ></b >< i > s family</i ></p > !end ! test Italics and ! options ! wikitext this is about foo s family ! html< p >< i > this is about</i > foo< b > s family</b ></p > !end ! test Italics and ! wikitext this is about foo s family ! html< p >< b > this is about< i > foo s</i > family</b ></p > !end ! test Italicized possessive ! wikitext The s talk page ! html< p > The< i >< a href="/wiki/Main_Page" title="Main Page"> Main Page</a ></i > s talk page</p > ! end ! test Parsoid only
Definition: parserTests.txt:396
$term
the value to return A Title object or null whereas SearchGetNearMatch runs after $term
Definition: hooks.txt:2130
are
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
Definition: contenthandler.txt:5
used
you don t have to do a grep find to see where the $wgReverseTitle variable is used
Definition: hooks.txt:117
as
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
SearchPostgres\updateTitle
updateTitle( $id, $title)
Update a search index record's title only.
Definition: SearchPostgres.php:201
$query
return true to allow those checks to and false if checking is done use this to change the tables headers temp or archived zone change it to an object instance and return false override the list derivative used the name of the old file when set the default code will be skipped add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition: hooks.txt:1105
PostgresSearchResultSet
Definition: SearchPostgres.php:224
$res
$res
Definition: database.txt:21
PostgresSearchResult
Definition: SearchPostgres.php:210