MediaWiki  1.33.0
SearchPostgres.php
Go to the documentation of this file.
1 <?php
28 
42  protected function doSearchTitleInDB( $term ) {
43  $q = $this->searchQuery( $term, 'titlevector', 'page_title' );
44  $olderror = error_reporting( E_ERROR );
45  $resultSet = $this->db->query( $q, 'SearchPostgres', true );
46  error_reporting( $olderror );
47  return new SqlSearchResultSet( $resultSet, $this->searchTerms );
48  }
49 
50  protected function doSearchTextInDB( $term ) {
51  $q = $this->searchQuery( $term, 'textvector', 'old_text' );
52  $olderror = error_reporting( E_ERROR );
53  $resultSet = $this->db->query( $q, 'SearchPostgres', true );
54  error_reporting( $olderror );
55  return new SqlSearchResultSet( $resultSet, $this->searchTerms );
56  }
57 
66  private function parseQuery( $term ) {
67  wfDebug( "parseQuery received: $term \n" );
68 
69  # # No backslashes allowed
70  $term = preg_replace( '/\\\/', '', $term );
71 
72  # # Collapse parens into nearby words:
73  $term = preg_replace( '/\s*\(\s*/', ' (', $term );
74  $term = preg_replace( '/\s*\)\s*/', ') ', $term );
75 
76  # # Treat colons as word separators:
77  $term = preg_replace( '/:/', ' ', $term );
78 
79  $searchstring = '';
80  $m = [];
81  if ( preg_match_all( '/([-!]?)(\S+)\s*/', $term, $m, PREG_SET_ORDER ) ) {
82  foreach ( $m as $terms ) {
83  if ( strlen( $terms[1] ) ) {
84  $searchstring .= ' & !';
85  }
86  if ( strtolower( $terms[2] ) === 'and' ) {
87  $searchstring .= ' & ';
88  } elseif ( strtolower( $terms[2] ) === 'or' || $terms[2] === '|' ) {
89  $searchstring .= ' | ';
90  } elseif ( strtolower( $terms[2] ) === 'not' ) {
91  $searchstring .= ' & !';
92  } else {
93  $searchstring .= " & $terms[2]";
94  }
95  }
96  }
97 
98  # # Strip out leading junk
99  $searchstring = preg_replace( '/^[\s\&\|]+/', '', $searchstring );
100 
101  # # Remove any doubled-up operators
102  $searchstring = preg_replace( '/([\!\&\|]) +(?:[\&\|] +)+/', "$1 ", $searchstring );
103 
104  # # Remove any non-spaced operators (e.g. "Zounds!")
105  $searchstring = preg_replace( '/([^ ])[\!\&\|]/', "$1", $searchstring );
106 
107  # # Remove any trailing whitespace or operators
108  $searchstring = preg_replace( '/[\s\!\&\|]+$/', '', $searchstring );
109 
110  # # Remove unnecessary quotes around everything
111  $searchstring = preg_replace( '/^[\'"](.*)[\'"]$/', "$1", $searchstring );
112 
113  # # Quote the whole thing
114  $searchstring = $this->db->addQuotes( $searchstring );
115 
116  wfDebug( "parseQuery returned: $searchstring \n" );
117 
118  return $searchstring;
119  }
120 
128  private 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  $slotRoleStore = MediaWikiServices::getInstance()->getSlotRoleStore();
143  if ( $top === "" ) { # # e.g. if only stopwords are used XXX return something better
144  $query = "SELECT page_id, page_namespace, page_title, 0 AS score " .
145  "FROM page p, revision r, slots s, content c, pagecontent pc " .
146  "WHERE p.page_latest = r.rev_id " .
147  "AND s.slot_revision_id = r.rev_id " .
148  "AND s.slot_role_id = " . $slotRoleStore->getId( SlotRecord::MAIN ) . " " .
149  "AND c.content_id = s.slot_content_id " .
150  "AND pc.old_id = substring( c.content_address from '^tt:([0-9]+)$' )::int " .
151  "AND 1=0";
152  } else {
153  $m = [];
154  if ( preg_match_all( "/'([^']+)'/", $top, $m, PREG_SET_ORDER ) ) {
155  foreach ( $m as $terms ) {
156  $this->searchTerms[$terms[1]] = $terms[1];
157  }
158  }
159 
160  $query = "SELECT page_id, page_namespace, page_title, " .
161  "ts_rank($fulltext, to_tsquery($searchstring), 5) AS score " .
162  "FROM page p, revision r, slots s, content c, pagecontent pc " .
163  "WHERE p.page_latest = r.rev_id " .
164  "AND s.slot_revision_id = r.rev_id " .
165  "AND s.slot_role_id = " . $slotRoleStore->getId( SlotRecord::MAIN ) . " " .
166  "AND c.content_id = s.slot_content_id " .
167  "AND pc.old_id = substring( c.content_address from '^tt:([0-9]+)$' )::int " .
168  "AND $fulltext @@ to_tsquery($searchstring)";
169  }
170  # # Namespaces - defaults to 0
171  if ( !is_null( $this->namespaces ) ) { // null -> search all
172  if ( count( $this->namespaces ) < 1 ) {
173  $query .= ' AND page_namespace = 0';
174  } else {
175  $namespaces = $this->db->makeList( $this->namespaces );
176  $query .= " AND page_namespace IN ($namespaces)";
177  }
178  }
179 
180  $query .= " ORDER BY score DESC, page_id DESC";
181 
182  $query .= $this->db->limitResult( '', $this->limit, $this->offset );
183 
184  wfDebug( "searchQuery returned: $query \n" );
185 
186  return $query;
187  }
188 
189  # # Most of the work of these two functions are done automatically via triggers
190 
191  function update( $pageid, $title, $text ) {
192  # # We don't want to index older revisions
193  $slotRoleStore = MediaWikiServices::getInstance()->getSlotRoleStore();
194  $sql = "UPDATE pagecontent SET textvector = NULL " .
195  "WHERE textvector IS NOT NULL " .
196  "AND old_id IN " .
197  "(SELECT DISTINCT substring( c.content_address from '^tt:([0-9]+)$' )::int AS old_rev_text_id " .
198  " FROM content c, slots s, revision r " .
199  " WHERE r.rev_page = $pageid " .
200  " AND s.slot_revision_id = r.rev_id " .
201  " AND s.slot_role_id = " . $slotRoleStore->getId( SlotRecord::MAIN ) . " " .
202  " AND c.content_id = s.slot_content_id " .
203  " ORDER BY old_rev_text_id DESC OFFSET 1)";
204  $this->db->query( $sql );
205  return true;
206  }
207 
208  function updateTitle( $id, $title ) {
209  return true;
210  }
211 
212 }
SearchPostgres\update
update( $pageid, $title, $text)
Create or update the search index record for the given page.
Definition: SearchPostgres.php:191
captcha-old.count
count
Definition: captcha-old.py:249
SearchPostgres\parseQuery
parseQuery( $term)
Transform the user's search string into a better form for tsearch2 Returns an SQL fragment consisting...
Definition: SearchPostgres.php:66
SearchEngine\$namespaces
int[] null $namespaces
Definition: SearchEngine.php:41
$res
$res
Definition: database.txt:21
SearchPostgres
Search engine hook base class for Postgres.
Definition: SearchPostgres.php:33
php
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
SearchPostgres\searchQuery
searchQuery( $term, $fulltext, $colname)
Construct the full SQL query to do the search.
Definition: SearchPostgres.php:128
SearchDatabase
Base search engine base class for database-backed searches.
Definition: SearchDatabase.php:31
$query
null for the 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:1588
namespaces
to move a page</td >< td > &*You are moving the page across namespaces
Definition: All_system_messages.txt:2670
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
SearchPostgres\doSearchTitleInDB
doSearchTitleInDB( $term)
Perform a full text search query via tsearch2 and return a result set.
Definition: SearchPostgres.php:42
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:949
$term
whereas SearchGetNearMatch runs after $term
Definition: hooks.txt:2878
e
in this case you re responsible for computing and outputting the entire conflict i e
Definition: hooks.txt:1423
SqlSearchResultSet
This class is used for different SQL-based search engines shipped with MediaWiki.
Definition: SqlSearchResultSet.php:9
SearchPostgres\doSearchTextInDB
doSearchTextInDB( $term)
Perform a full text search query and return a result set.
Definition: SearchPostgres.php:50
used
you don t have to do a grep find to see where the $wgReverseTitle variable is used
Definition: hooks.txt:115
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
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
MediaWikiServices
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 MediaWikiServices
Definition: injection.txt:23
SearchPostgres\updateTitle
updateTitle( $id, $title)
Update a search index record's title only.
Definition: SearchPostgres.php:208
Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:39