WikimediaSearch

Summary: Get a functional search form like on wikipedia pages
Version: 2007-09-21
Prerequisites: nothing
Status: new
Maintainer: Sven

Questions answered by this recipe

How to get a search form like seen on wikipedia pages, which means:

  • Using one text input field for both searching and going directly to a wikipage
  • Going to the default search page if nothing was entered
  • Getting even a much more functional form by using a little javascript

Description

Simple wikipedia like

Replace the existing search form in your template with this form (you might want to change the labels, ...):

    <form action='{$ScriptUrl}'>
        <label for="q">Searching:</label>
        <input type="text" class="text" name="q" id="q" />
        <input type='hidden' name='n' value='{$FullName}' />
        <input type='hidden' name='action' value='gosearch' />
        <input type="submit" class="button" name="gotype" value="go" />
        <input type="submit" class="button" name="gotype" value="search" />
    </form>

And this tiny code of PHP in a recipie or your local config file:

if($action == 'gosearch') {
    if($_GET['gotype'] == 'go') {
      header("Location: ${ScriptUrl}?n=$_GET[q]"); exit; }
    else $action = 'search';
}

You're done - this will work exactly like the wikipedia implementation with a minimum of effort.

The advanced javascript way

This is just a little extension to the static solution. Simply replace the search form in your template file with this tiny code:

    <form action='{$ScriptUrl}'>
        <input type="text" class="text" name="q" id="q" value="Search" onFocus="if(this.value=='Search')this.value='';"
                 onBlur="if(this.value=='')this.value='Search';" />
        <input type='hidden' name='n' value='{$FullName}' />
        <input type='hidden' name='action' value='gosearch' />
        <input type="submit" class="button" name="gotype" value="go" />
        <input type="submit" class="button" name="gotype" value="search" />
    </form>

and this will replace the PHP code shown above:

if($action == 'gosearch') {
    # if the user only hit a button without entering an own search term,
    # we'll refer him to the default search page of the PmWiki installation
    # which will give him further instructions
    if($_GET['q'] == 'Search' || empty($_GET['q'])) {
        $_GET['gotype'] = 'go; $_GET['q'] = 'PmWiki.Search'; }
    # go to the article with the name...
    if($_GET['gotype'] == 'go') {
        header("Location: ${ScriptUrl}?n=$_GET[q]"); exit; }
    # perform a real search
    else $action = 'search';
}

You might notice that the default page shown by PmWiki if a page doesn't exist is not very verbose, compared to the page shown by wikimedia. Site.PageNotFound is your friend - I've written this page from the scratch, giving the reader a possibility to create the article, check the group or perform a full text search.

Notes

Release Notes

If the recipe has multiple releases, then release notes can be placed here. Note that it's often easier for people to work with "release dates" instead of "version numbers".

See Also

Contributors

  • Sven K.

Comments

User notes? : If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.