r96401 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96400‎ | r96401 | r96402 >
Date:03:06, 7 September 2011
Author:svemir
Status:deferred (Comments)
Tags:
Comment:
emulate Wikipedia search where ~ prefix prevents automatic redirect
use strict comparison for suggest mode (some folks may still have it set to true)
remove methods that need not be overwritten
use $this->db consistently (with backward-compatibility check for MW prior to r77109)
Modified paths:
  • /trunk/extensions/SphinxSearch/SphinxMWSearch.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SphinxSearch/SphinxMWSearch.php
@@ -19,8 +19,18 @@
2020 var $db;
2121 var $sphinx_client = null;
2222
23 - function __construct( $db ) {
24 - parent::__construct( $db );
 23+ /**
 24+ * Do not go to a near match if query prefixed with ~
 25+ *
 26+ * @param $searchterm String
 27+ * @return Title
 28+ */
 29+ public static function getNearMatch( $searchterm ) {
 30+ if ( $searchterm[ 0 ] === '~' ) {
 31+ return null;
 32+ } else {
 33+ return parent::getNearMatch( $searchterm );
 34+ }
2535 }
2636
2737 /**
@@ -84,19 +94,12 @@
8595 }
8696
8797 /**
88 - * We do a weighted title/body search, no need to return titles separately
89 - */
90 - function searchTitle() {
91 - return null;
92 - }
93 -
94 - /**
9598 * @return SphinxClient: ready to run or false if term is empty
9699 */
97100 function prepareSphinxClient( &$term ) {
98101 global $wgSphinxSearch_sortmode, $wgSphinxSearch_sortby, $wgSphinxSearch_host,
99 - $wgSphinxSearch_port, $wgSphinxSearch_index_weights, $wgSphinxSearch_index,
100 - $wgSphinxSearch_mode, $wgSphinxMatchAll, $wgSphinxSearch_maxmatches,
 102+ $wgSphinxSearch_port, $wgSphinxSearch_index_weights,
 103+ $wgSphinxSearch_mode, $wgSphinxSearch_maxmatches,
101104 $wgSphinxSearch_cutoff, $wgSphinxSearch_weights;
102105
103106 // don't do anything for blank searches
@@ -152,44 +155,31 @@
153156 return $cl;
154157 }
155158
156 - /**
157 - * @return Boolean: can we list/unlist redirects
158 - */
159 - function acceptListRedirects() {
160 - return true;
161 - }
162 -
163 - /**
164 - * @return String: allowed query characters
165 - */
166 - public static function legalSearchChars() {
167 - return "A-Za-z_'./\"!~0-9\\x80-\\xFF\\-";
168 - }
169 -
170159 }
171160
172161 class SphinxMWSearchResultSet extends SearchResultSet {
 162+
173163 var $mNdx = 0;
174 - var $sphinx_client = null;
 164+ var $sphinx_client;
175165 var $mSuggestion = '';
 166+ var $db;
176167
177168 function __construct( $resultSet, $terms, $sphinx_client, $dbr ) {
178 - global $wgSphinxSearch_index;
179 -
180169 $this->sphinx_client = $sphinx_client;
181170 $this->mResultSet = array();
 171+ $this->db = $dbr ? $dbr : wfGetDB( DB_SLAVE );
182172
183173 if ( is_array( $resultSet ) && isset( $resultSet['matches'] ) ) {
184174 foreach ( $resultSet['matches'] as $id => $docinfo ) {
185 - $res = $dbr->select(
 175+ $res = $this->db->select(
186176 'page',
187177 array( 'page_id', 'page_title', 'page_namespace' ),
188178 array( 'page_id' => $id ),
189179 __METHOD__,
190180 array()
191181 );
192 - if ( $dbr->numRows( $res ) > 0 ) {
193 - $this->mResultSet[] = $dbr->fetchObject( $res );
 182+ if ( $this->db->numRows( $res ) > 0 ) {
 183+ $this->mResultSet[] = $this->db->fetchObject( $res );
194184 }
195185 }
196186 }
@@ -208,11 +198,11 @@
209199
210200 if ( $wgSphinxSuggestMode ) {
211201 $this->mSuggestion = '';
212 - if ( $wgSphinxSuggestMode == 'enchant' ) {
 202+ if ( $wgSphinxSuggestMode === 'enchant' ) {
213203 $this->suggestWithEnchant();
214 - } elseif ( $wgSphinxSuggestMode == 'soundex' ) {
 204+ } elseif ( $wgSphinxSuggestMode === 'soundex' ) {
215205 $this->suggestWithSoundex();
216 - } elseif ( $wgSphinxSuggestMode == 'aspell' ) {
 206+ } elseif ( $wgSphinxSuggestMode === 'aspell' ) {
217207 $this->suggestWithAspell();
218208 }
219209 if ($this->mSuggestion) {
@@ -259,9 +249,8 @@
260250 * Default (weak) suggestions implementation relies on MySQL soundex
261251 */
262252 function suggestWithSoundex() {
263 - $dbr = wfGetDB( DB_SLAVE );
264 - $joined_terms = $dbr->addQuotes( join( ' ', $this->mTerms ) );
265 - $res = $dbr->select(
 253+ $joined_terms = $this->db->addQuotes( join( ' ', $this->mTerms ) );
 254+ $res = $this->db->select(
266255 array( 'page' ),
267256 array( 'page_title' ),
268257 array(
@@ -275,7 +264,7 @@
276265 'LIMIT' => 1
277266 )
278267 );
279 - $suggestion = $dbr->fetchObject( $res );
 268+ $suggestion = $this->db->fetchObject( $res );
280269 if ( is_object( $suggestion ) ) {
281270 $this->mSuggestion = trim( $suggestion->page_title );
282271 }

Sign-offs

UserFlagDate
Nikerabbitinspected08:24, 7 September 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r77109Move storing of $db down to SearchEngine...platonides16:08, 22 November 2010

Comments

#Comment by Platonides (talk | contribs)   21:24, 8 September 2011

Where is that ~ trick documented?

#Comment by Svemir Brkic (talk | contribs)   22:16, 8 September 2011

http://en.wikipedia.org/wiki/Help:Searching#Search_engine_features Avoiding automatic direction to page - Adding a tilde (~) at the beginning of a search word forces a search...

Status & tagging log