Extension:UrlGetParameters

From MediaWiki.org

Jump to: navigation, search

         

Manual on MediaWiki Extensions
List of MediaWiki Extensions
Crystal Clear action run.png
UrlGetParameters

Release status: stable

Implementation  Parser function
Description Provides a parserfunction {{#urlget:...}} which allows access to the url parameters in your page. Note: caching for the page you use this extension on should be disabled.
Author(s)  S.O.E. Ansems
Last Version  1.0
MediaWiki  1.10.x or higher
License No license specified
Download see below

check usage (experimental)

Contents

[edit] Introduction

This extension enables you to access the url get parameters of the page. Nothing fancy, but it may be useful for dynamic pages that, depending on the link to the page, should render differently.

[edit] Installation

  1. Copy the code from below into a file called UrlGetParameters.php.
  2. Drop this script in $IP/extensions/UrlGetParameters
    Note: $IP is your MediaWiki install dir.
  3. Enable the extension by adding this line to your LocalSettings.php:
    require_once('extensions/UrlGetParameters/UrlGetParameters.php');
    

[edit] Usage

To include the value of a url get parameter in your page you might do this:

{{#urlget:parameter-name}}

Where parameter-name is the name of the parameter whose value you want. If the parameter is not found, the extension will result in nothing. Alternativly you may also specify a default value as follows:

{{#urlget:parameter-name|default-value}}

When the parameter parameter-name is not available, then the value default-value is returned.

Note: Most likely your wiki installation will cache pages. As a result the parameters that will show in the page may not the ones in the url. To overcome this 'limitation' you should disable caching for the pages you want to use the extension on. This can be done by installing another extension such as Winter which has a magic word nocache. So the example above would look like this:

{{#nocache}}
{{#urlget:parameter-name|default-value}}

The example above will show the provided parameters regardless of caching on the system.

Good luck!

[edit] Code

<?php
$wgExtensionCredits['parserhook'][] = array(
'name'         => 'URL Get Parameters parser extension',
'version'      => '1.0.0', // Feb 21, 2008.
'description'  => 'Provides the <tt><nowiki>{{#urlget:...}}</nowiki></tt> parserfunction which enables access to the url get parameters.',
'author'       => 'S.O.E. Ansems',
'url'          => 'http://www.mediawiki.org/wiki/Extension:UrlGetParameters',
);
 
$wgExtensionFunctions[] = 'urlGetParameters_Setup';
$wgHooks['LanguageGetMagic'][] = 'urlGetParameters_Magic';
 
function urlGetParameters_Setup() {
    global $wgParser;
    $wgParser->setFunctionHook( 'urlget', 'urlGetParameters_Render' );
}
 
function urlGetParameters_Magic( &$magicWords, $langCode ) {
    $magicWords['urlget'] = array( 0, 'urlget' );
    return true;
}
 
function urlGetParameters_Render( &$parser ) {
	// {{#urlget:paramname|defaultvalue}}
 
	// Get the parameters that were passed to this function
    $params = func_get_args();
    array_shift( $params );
 
    if(in_array($params[0],array_keys($_GET))===true)
		return $_GET[$params[0]];
 
   	if(count($params)>1)
		return $params[1];
 
	return "";
}