[Mediawiki-l] Easy creation of page sequences

Jean-Marc van Leerdam j.m.van.leerdam at gmail.com
Tue Sep 22 20:27:04 UTC 2009


Hi all,

Just thought I would give an update to my earlier request. I have
created a simple parserfunction to get the result I want (at the
moment combined with a template to wrap the calls to the
parserfunction).

It's a start, which I want to expand in several ways:
- have the parserfunction retrieve the index page itself instead of
using a template to transclude the page content into the indexpage
parameter
- support alternate name for the page name to display as link
- support links with (i18n) 'Previous' 'Next' text instead of page title

The good part is that the index page only needs a plain list of pages
that provide the sequence:
PageA
PageB
PageC
PageD
PageE


Currently the previous/next links are placed via a template call:

{{PrevNext|IndexPage|CurrentPage}}

The template contains:

<code>
<span class="prevnext">{{#bpprev:{{{2|{{PAGENAME}}}}}|{{:{{{1}}}}}}}
&bull; {{#bpnext:{{{2|{{PAGENAME}}}}}|{{:{{{1}}}}}}} </span>
</code>

The parserfunction:

<code>
# Define a setup function
$wgExtensionFunctions[] = 'jmlBrowsePage_Setup';
# Add a hook to initialise the magic word
$wgHooks['LanguageGetMagic'][]       = 'jmlBrowsePage_Magic';

function jmlBrowsePage_Setup() {
    global $wgParser;
    # Set a function hook associating the "example" magic word with our function
    $wgParser->setFunctionHook( 'bpnext', 'jmlBrowsePage_Next' );
    $wgParser->setFunctionHook( 'bpprev', 'jmlBrowsePage_Prev' );
}

function jmlBrowsePage_Magic( &$magicWords, $langCode ) {
    # Add the magic word
    # The first array element is case sensitive, in this case it is
not case sensitive
    # All remaining elements are synonyms for our parser function
    $magicWords['bpnext'] = array( 0, 'bpnext' );
    $magicWords['bpprev'] = array( 0, 'bpprev' );
    # unless we return true, other parser functions extensions won't get loaded.
    return true;
}

function jmlBrowsePage_Next( &$parser, $pagename = '', $indexlist = '' ) {
    $parser->disableCache();

    // Match the line after a line: /^Line\r?\n(.*)/m
    $regex = "/^$pagename\\r?\\n(.*)/m";
    $result = preg_match( $regex, $indexlist, $results );

    if ($results)
    {
      $output = "[[$results[1]]]";
    } else {
      $output = "";
    }

    return $output;
}

function jmlBrowsePage_Prev( &$parser, $pagename = '', $indexlist = '' ) {
    $parser->disableCache();

    // Match the line before a line: /(.*)\r?\nLine\r?\n/m
    $regex = "/(.*)\\r?\\n$pagename\\r?\\n/m";
    $result = preg_match( $regex, $indexlist, $results );

    if ($results)
    {
      $output = "[[$results[1]]]";
    } else {
      $output = "";
    }

    return $output;
}

</code>

If there is demand for this extension I am willing to complete it and
donate it to the community by submitting it to Mediawiki.

-- 
Regards,

Jean-Marc
--
.       ___
.  @@  // \\      "De Chelonian Mobile"
. (_,\/ \_/ \     TortoiseSVN
.   \ \_/_\_/>    The coolest Interface to (Sub)Version Control
.   /_/   \_\     http://tortoisesvn.net



More information about the MediaWiki-l mailing list