Page MenuHomePhabricator

SpecialSearch.php : Using a hook to override search
Closed, DeclinedPublic

Description

Author: sylvain_machefert

Description:
On my wiki, I needed to add a new search of similar words than the ones searched (for example, search "Yovano" should find "Jovano" and "йовано", search "Katysha" find "Катюша"...).
(this is still in beta, need a lot of improvements, you can see results at http://tousauxbalkans.free.fr ).

To do that, I created an extension with a copy/paste of a piece of code from SpecialSearch for regular searches, plus my own code for guessing similar words.

I'll try to create a patch from CVS if find how to do this, but here I explain the few things I had to change in SpecialSearch :

  1. in showResults function, I added the call to SearchOverride hook if exists. 2 parameters : $this = the object SpecialSearch, and $term = the searched word(s).

<pre>
if (wfRunHooks('SearchOverride', array($this, $term))) {
$search =& $this->getSearchEngine();
$titleMatches = $search->searchTitle( $term );
$textMatches = $search->searchText( $term );

...<snip>...

if( $num || $this->offset ) {

		$wgOut->addHTML( "<p>{$prevnext}</p>\n" );

}
}
$wgOut->addHTML( $this->powerSearchBox( $term ) );
wfProfileOut( $fname );
</pre>

  1. function &getSearchEngine() is tagged as private, must be public. My hook need to create a search engine !
  1. My hook need also to access to limit, offset, and powerSearchOptions(). Make all them public, not private.

Version: 1.4.x
Severity: enhancement

Details

Reference
bz4026

Event Timeline

bzimport raised the priority of this task from to Lowest.Nov 21 2014, 8:58 PM
bzimport added a project: MediaWiki-Search.
bzimport set Reference to bz4026.
bzimport added a subscriber: Unknown Object (MLST).

I don't quite understand the point of this; the whole search engine is
pluggable by subclassing it and specifying your class in the config.

What is this supposed to do that's different, and why?

sylvain_machefert wrote:

SpecialSearch does 3 searches :

  • if $term is the title of a page -> go to the page
  • searchTitle($term)
  • searchText($term)

I don't want to change these 3 steps, I just want to add 2 more thins :

  • before the search (maybe it's configurable in LocalSettings, how ?) : $search->strictMatching = false;
  • after the 3 steps, searchText(MyExtension::getSimilarWords($term))

On the chat, when I asked how to do this, the only way was "create a hook, add if wfRunHooks("searchOverride")...
so I create my hook with a copy/paste of the already existing steps searchTitle($term) and searchText($term).

Is there a way to do what I want without modifying SpecialSearch ?

der.scotty wrote:

I had also done this,

SearchMySQL4.php:33: $strictMatching = false;
SpecialSearch.php:39(+): $search = bpm_extend_search($search);

robchur wrote:

Extend the existing plugin and override as needed. Using a pluggable mechanism
here is a lot cleaner than using a hook.