r96398 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96397‎ | r96398 | r96399 >
Date:02:31, 7 September 2011
Author:svemir
Status:deferred (Comments)
Tags:
Comment:
"Did you mean" functionality now has three modes: enchant, soundex, aspell
(command-line aspell support copied from old version of the extension,
for those that do not want to mess with enchant)
Modified paths:
  • /trunk/extensions/SphinxSearch/SphinxMWSearch.php (modified) (history)
  • /trunk/extensions/SphinxSearch/SphinxSearch.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SphinxSearch/SphinxMWSearch.php
@@ -210,8 +210,10 @@
211211 $this->mSuggestion = '';
212212 if ( $wgSphinxSuggestMode == 'enchant' ) {
213213 $this->suggestWithEnchant();
214 - } else {
 214+ } elseif ( $wgSphinxSuggestMode == 'soundex' ) {
215215 $this->suggestWithSoundex();
 216+ } elseif ( $wgSphinxSuggestMode == 'aspell' ) {
 217+ $this->suggestWithAspell();
216218 }
217219 if ($this->mSuggestion) {
218220 return true;
@@ -232,7 +234,6 @@
233235 $suggestion_found = false;
234236 $full_suggestion = '';
235237 foreach ( $this->mTerms as $word ) {
236 - $suggestions = array();
237238 if ( !enchant_dict_check($dict, $word) ) {
238239 $suggestions = enchant_dict_suggest($dict, $word);
239240 while ( count( $suggestions ) ) {
@@ -280,6 +281,56 @@
281282 }
282283 }
283284
 285+ function suggestWithAspell() {
 286+ global $wgUser, $wgSphinxSearchPersonalDictionary, $wgSphinxSearchAspellPath;
 287+
 288+ // aspell will only return mis-spelled words, so remember all here
 289+ $words = $this->mTerms;
 290+ $word_suggestions = array();
 291+ foreach ( $words as $word ) {
 292+ $word_suggestions[ $word ] = $word;
 293+ }
 294+
 295+ // prepare the system call with optional dictionary
 296+ $aspellcommand = 'echo ' . escapeshellarg( join( ' ', $words ) ) .
 297+ ' | ' . escapeshellarg( $wgSphinxSearchAspellPath ) .
 298+ ' -a --ignore-accents --ignore-case';
 299+ if ( $wgUser ) {
 300+ $aspellcommand .= ' --lang=' . $wgUser->getDefaultOption( 'language' );
 301+ }
 302+ if ( $wgSphinxSearchPersonalDictionary ) {
 303+ $aspellcommand .= ' --home-dir=' . dirname( $wgSphinxSearchPersonalDictionary );
 304+ $aspellcommand .= ' -p ' . basename( $wgSphinxSearchPersonalDictionary );
 305+ }
 306+
 307+ // run aspell
 308+ $shell_return = shell_exec( $aspellcommand );
 309+
 310+ // parse return line by line
 311+ $returnarray = explode( "\n", $shell_return );
 312+ $suggestion_needed = false;
 313+ foreach ( $returnarray as $key => $value ) {
 314+ // lines with suggestions start with &
 315+ if ( $value[0] === '&' ) {
 316+ $correction = explode( ' ', $value );
 317+ $word = $correction[ 1 ];
 318+ $suggestions = substr( $value, strpos( $value, ':' ) + 2 );
 319+ $suggestions = explode( ', ', $suggestions );
 320+ if ( count( $suggestions ) ) {
 321+ $guess = array_shift( $suggestions );
 322+ if ( strtolower( $word ) != strtolower( $guess ) ) {
 323+ $word_suggestions[ $word ] = $guess;
 324+ $suggestion_needed = true;
 325+ }
 326+ }
 327+ }
 328+ }
 329+
 330+ if ( $suggestion_needed ) {
 331+ $this->mSuggestion = join( ' ', $word_suggestions );
 332+ }
 333+ }
 334+
284335 /**
285336 * @return String: suggested query, null if none
286337 */
Index: trunk/extensions/SphinxSearch/SphinxSearch.php
@@ -25,20 +25,19 @@
2626
2727 # To completely disable the default search and replace it with SphinxSearch,
2828 # set this BEFORE including SphinxSearch.php in LocalSettings.php
29 -# $wgSearchType = 'SphinxSearch';
 29+# $wgSearchType = 'SphinxMWSearch';
3030
31 -# prior to version 0.8.0 there was a SphinxSearch search type
 31+# Prior to version 0.8.0 there was a SphinxSearch search type
3232 if ( $wgSearchType == 'SphinxSearch' ) {
3333 $wgSearchType == 'SphinxMWSearch';
3434 }
3535
36 -# To use the new approach (added in 0.7.2) set it to SphinxMWSearch
3736 if ( $wgSearchType == 'SphinxMWSearch' ) {
3837 $wgAutoloadClasses['SphinxMWSearch'] = $dir . 'SphinxMWSearch.php';
3938 $wgDisableSearchUpdate = true;
4039 }
4140
42 -# this assumes you have copied sphinxapi.php from your Sphinx
 41+# This assumes you have copied sphinxapi.php from your Sphinx
4342 # installation folder to your SphinxSearch extension folder
4443 # not needed if you install http://pecl.php.net/package/sphinx
4544 if ( !class_exists( 'SphinxClient' ) ) {
@@ -75,23 +74,16 @@
7675 # Set to true to use MW's default search snippets and highlighting
7776 $wgSphinxSearchMWHighlighter = false;
7877
79 -# #########################################################
80 -# Use Aspell to suggest possible misspellings. This can be provided via
81 -# PHP pspell module (http://www.php.net/manual/en/ref.pspell.php)
82 -# or command line insterface to ASpell
 78+# Should the suggestion (Did you mean?) mode be enabled?
 79+# Possible values: enchant, soundex, aspell
 80+$wgSphinxSuggestMode = '';
8381
84 -# Should the suggestion mode be enabled?
85 -$wgSphinxSuggestMode = false;
 82+# Path to aspell, adjust value if not in the system path
 83+$wgSphinxSearchAspellPath = 'aspell';
8684
87 -# Path to personal dictionary (for example personal.en.pws.) Needed only if using a personal dictionary
 85+# Path to (optional) personal aspell dictionary
8886 $wgSphinxSearchPersonalDictionary = '';
8987
90 -# Path to Aspell. Used only if your PHP does not have the pspell extension.
91 -$wgSphinxSearchAspellPath = "/usr/bin/aspell";
92 -
93 -# Path to aspell location and language data files. Do not set if not sure.
94 -$wgSphinxSearchPspellDictionaryDir = '';
95 -
9688 # How many matches searchd will keep in RAM while searching
9789 $wgSphinxSearch_maxmatches = 1000;
9890

Comments

#Comment by Nikerabbit (talk | contribs)   08:19, 7 September 2011
+		if ( $wgUser ) {
+			$aspellcommand .= ' --lang=' . $wgUser->getDefaultOption( 'language' );
+		}

This if statement is always true. But I think you actually want to use $wgContLanguageCode here.

#Comment by Svemir Brkic (talk | contribs)   11:32, 7 September 2011

Good to have folks reviewing extension commits again :-)

In r96414 I changed it to use $wgLanguageCode ($wgContLanguageCode was deprecated per Manual_talk:$wgLanguageCode)

Status & tagging log