Extension:Search Suggest

From MediaWiki.org

Jump to: navigation, search
Manual on MediaWiki Extensions
List of MediaWiki Extensions
Search Suggest

Release status: beta

Implementation Search, Ajax
Description Very Light weight (under 5k) search suggestion using mediawiki's built-in ajax.
Author(s) Adam Meyer (noverflowTalk)
Last Version 0.2
MediaWiki 1.10
License I give up all rights. You may do what you please with this code.
Download Download
Hooks used OutputPageBeforeHTML

Contents

[edit] What can this extension do?

This extension takes the idea of MediaWiki's built-in Ajax search, and makes it much more useful. It places the search results directly under the search bar, and does not take over the content (like the built in feature does).

The search looks for the word (or whatever you are looking for) within the article names, and not just at the beginning by default. There is an option to change this setting.

[edit] Usage

You should be able to just install it, reload your cache, and see it work. If it is working, you will see article names appear under the search bar when searching.

[edit] Installation

  • Create a new folder in your extensions called SearchSuggest
  • To install this extension, add the following to LocalSettings.php:
require_once("$IP/extensions/SearchSuggest/SearchSuggest.php");
  • Ajax must also be enabled. This is the default from MediaWiki 1.11 to newer versions. For older versions of MediaWiki, you might need to add the following to LocalSettings.php:
$wgUseAjax = true;
  • Monobook
    • As the code is here, it only works in the Monobook skin. This can be easily changed in the JavaScript.
newdiv.id = "searchsuggest";
var searchdiv = document.getElementById("searchBody");
searchdiv.appendChild(newdiv);

change searchBody of var searchdiv = document.getElementById("searchBody"); to the name of the div you want the results to appear in.

[edit] Code

[edit] PHP

Copy the following, and save it in the SearchSuggest folder as SearchSuggest.php

See below for the CSS and JavaScript

<?php
/**
 * SearchSuggest extension - a search extension utilizing
 * MediaWiki's built-in Ajax functions
 *
 * @author Adam Meyer
 * @copyright © 2008 Adam Meyer
 */
//See below under "function wfAjaxSearchSuggest" for configuration
 
if( !defined( 'MEDIAWIKI' ) ) {
        echo( "This file is part of an extension to the MediaWiki software and cannot be used standalone.\n" );
        die( 1 );
}
 
$wgExtensionFunctions[] = 'wfAjaxSearch';
$wgAjaxExportList[] = 'wfAjaxSearchSuggest';
$wgHooks['OutputPageBeforeHTML'][] = 'wfAjaxSearchSuggestHeaders';
 
$wgExtensionCredits['other'][] = array(
    'name' => 'SearchSuggest',
    'author' => 'Adam Meyer',
    'version' => '0.2',
    'url' => 'http://www.mediawiki.org/wiki/Extension:Search_Suggest',
    'description' => 'Ajax-based Search Suggest that places results below search bar.',
);
 
function wfAjaxSearch() {
        global $wgUseAjax;
        if (!$wgUseAjax) {
                wfDebug('wfAjaxSearch: $wgUseAjax is not enabled, aborting extension setup.');
                return;
        }
}
 
function wfAjaxSearchSuggest( $term ) {
 
        //configure the following to change settings
        $limit = 8; //number of results to spit back
        $location = 1; //set to 1 to search anywhere in the article name, set to 0 to search only at the begining
 
 
        global $wgContLang, $wgOut;
        $response = new AjaxResponse();
        $db = wfGetDB( DB_SLAVE );
        $l = new Linker;
 
        $term = str_replace(' ', '_', $term);
 
        if($location == 1){
 
                $res = $db->select( 'page', 'page_title',
                                array(  'page_namespace' => 0,
                                        "UPPER(page_title) LIKE '%" .$db->strencode( strtoupper ($term)). "%'" ),
                                        "wfSajaxSearch",
                                        array( 'LIMIT' => $limit )
                                );
        } else {           
 
                $res = $db->select( 'page', 'page_title',
                        array(  'page_namespace' => 0,
                                "UPPER(page_title) LIKE '". $db->strencode( strtoupper ($term)) ."%'" ),
                                "wfSajaxSearch",
                                array( 'LIMIT' => $limit )
                        );         
        }
 
        $r = "";    
        while ($row = $db->fetchObject( $res ) ){
                $nt = Title::newFromDBkey( $row->page_title );         
                $r .= '<li>' . $l->makeKnownLinkObj( $nt ) . "</li>\n";
 
        }
        $html ='<ul>' .$r .'</ul>';
 
        return $html;
}
 
function wfAjaxSearchSuggestHeaders(){
        global $wgOut;
        wfAjaxSetSearchSuggestHeaders($wgOut);
        return false;
}
 
 
function wfAjaxSetSearchSuggestHeaders($outputPage) {
        global $wgJsMimeType, $wgStylePath, $wgScriptPath;
        $outputPage->addLink( 
                array( 
                        'rel' => 'stylesheet', 
                        'type' => 'text/css', 
                        'href' => $wgScriptPath.'/extensions/SearchSuggest/SearchSuggest.css' 
                ) 
        );
        $outputPage->addScript( "<script type=\"{$wgJsMimeType}\" src=\"$wgScriptPath/extensions/SearchSuggest/SearchSuggest.js\">"."</script>\n");
        $outputPage->addScript( "<script type=\"{$wgJsMimeType}\">hookEvent(\"load\", ss_ajax_onload);</script>\n" );
}

[edit] JavaScript

Copy the following, and save it in the SearchSuggest folder as SearchSuggest.js

var ss_memory = null;
 
function SearchCall(){
    var newdiv = document.getElementById("searchsuggest");
    if (!newdiv) {
        var newdiv = document.createElement("div");
        newdiv.id = "searchsuggest";
        var searchdiv = document.getElementById("searchBody");
        searchdiv.appendChild(newdiv);
    }
    var x = document.getElementById("searchInput").value;
    if (x == ss_memory) {
        return;
    }
    ss_memory = x;
    document.getElementById("searchsuggest").style.display = 'none';
    if (x.length < 30 && x.length > 1 && x.value != "") {
        sajax_do_call("wfAjaxSearchSuggest", [x], newdiv);
        document.getElementById("searchsuggest").style.display = 'block';
    }
}
 
function ss_ajax_onload(){
    var x = document.getElementById('searchInput');
    x.onkeyup = function(){
        SearchCall();
    };
}

[edit] CSS

Copy the following, and save it in the searchsuggest folder as SearchSuggest.css (this can all be changed without issues on-wiki by editing your wiki's MediaWiki:Common.css)

#searchsuggest {
        text-align: left;
}
 
#searchsuggest ul{
        list-style: none;
        margin: 0;
 
}
 
#searchsuggest li {
        padding: 2px 5px;
        white-space: nowrap;
        text-align: left;
}
 
#searchsuggest li:hover {
        background: #888;
        margin-left: 1em;
        text-decoration: underline;
}

[edit] See also

Personal tools