[Mediawiki-l] mediawiki 1.4 & 1.5rc4, substring search, how?

Klein.Thomas at eae.com Klein.Thomas at eae.com
Wed Sep 28 10:32:16 UTC 2005


Now, here is the class for substring search without to change the source
and MySQL4!

The class SearchMySQL4SubString is a copy of SearchMySQL4.

Make a new file in subdir 'includes' with this name 'SearchMySQL4SubString'
and copy
the next code into this file:

<?php
/**
 * Search engine hook for MySQL 4+
 * @package MediaWiki
 * @subpackage Search
 */

require_once( 'SearchMySQL.php' );

/**
 * @package MediaWiki
 * @subpackage Search
 */
class SearchMySQL4SubString extends SearchMySQL {
      var $strictMatching = true;

      /** @todo document */
      function SearchMySQL4SubString( &$db ) {
            $this->db =& $db;
      }

      function legalSearchChars() {
            return "A-Za-z_'0-9\\x80-\\xFF\\-*?+";
      }

      /** @todo document */
      function parseQuery( $filteredText, $fulltext ) {
            global $wgContLang;
            $lc = SearchEngine::legalSearchChars();
            $searchon = '';
            $this->searchTerms = array();

            wfDebug( "parseQuery filteredText is: '$filteredText'\n" );
            wfDebug( "parseQuery fulltext is: '$fulltext'\n" );

            # FIXME: This doesn't handle parenthetical expressions.
            if( preg_match_all( '/([-+<>~]?)(([' . $lc .
']+)(\*?)|"[^"]*")/',
                    $filteredText, $m, PREG_SET_ORDER ) ) {
                  foreach( $m as $terms ) {
                        if( $searchon !== '' ) $searchon .= ' ';
                        $searchon .= $terms[1] .
$wgContLang->stripForSearch( $terms[2] );
                        if( !empty( $terms[3] ) ) {
                              $regexp = preg_quote( $terms[3], '/' );
                              if( $terms[4] ) $regexp .= "[0-9A-Za-z_]+";
                        } else {
                              $regexp = preg_quote( str_replace( '"', '',
$terms[2] ), '/' );
                        }
                        $this->searchTerms[] = $regexp;
                  }
                  wfDebug( "Would search with '$searchon'\n" );
                  wfDebug( "Match with /\b" . implode( '\b|\b',
$this->searchTerms ) . "\b/\n" );
            } else {
                  wfDebug( "Can't understand search query
'{$this->filteredText}'\n" );
            }

            $searchon = $this->db->strencode( $searchon );
            $field = $this->getIndexField( $fulltext );
            return " MATCH($field) AGAINST('$searchon' IN BOOLEAN MODE) ";
      }
}
?>

And add to lines into your 'LocalSettings.php':

require_once( 'SearchMySQL4SubString.php' );

$wgSearchType         = SearchMySQL4SubString;


Now the search function have the same syntax of MySQL4.

Also, see http://dev.mysql.com/doc/mysql/en/fulltext-boolean.html for
details
about full text sql queries in MySQL.


Mit freundlichen Grüssen / Kind regards

i.A. Thomas  Klein
OPS Koordination / OPS coordination

EAE software GmbH
Kornkamp 8
22926 Ahrensburg/Germany

Tel :     +49 4102/480-513
Fax:     +49 4102/480-561
e-mail:  mailto:klein.thomas at eae.com
http://www.eae.com/




More information about the MediaWiki-l mailing list