Extension:SMW ParserFunctionAskMore

From MediaWiki.org

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

Release status: stable

Implementation Parser function
Description Makes Semantic MediaWiki's <ask>...</ask> tag into a ParserFunction {{#askmore}}. So you can use it in a Template and feed template parameters into the query, even the {{{...}}} parameters which is not possible with the {{#ask}} implemented natively by Semantic MediaWiki.
Author(s) S.O.E. Ansems
Last Version 1.1
MediaWiki 1.10.x or higher
License No license specified
Download see below

Contents

[edit] Introduction

This extension enables you to pass template parameters to the Semantic MediaWiki <ask> tag. The syntax is only marginally modified. Its based on the SMW_InlineQueryParserFunction made by Jim R. Wilson. It has recently been renamed to avoid conflict with the {{#ask}} parserfunction that is now native to Semantic MediaWiki.

[edit] Rationale

Eventhough Semantic MediaWiki now natively supports a parser function {{#ask}}, there is still reason to use this parserfunction. Although very similar, it has one great advantage over the SMW native {{#ask}} parser function, which is that it accepts template parameters. This feature enables you to use it in semantic templates that takes custom parameters.

[edit] Installation

  1. Copy the code from below into a file called SMW_ParserFunctionAskMore.php.
  2. Drop this script in $IP/extensions/SemanticMediaWiki
    Note: $IP is your MediaWiki install dir.
  3. Enable the extension by adding this line to your LocalSettings.php:
    require_once('extensions/SemanticMediaWiki/SMW_ParserFunctionAskMore.php');
    
    Note: Make sure this appears later in LocalSettings than the include_once() call which enables Semantic MediaWiki itself!

[edit] Usage

To query for an unordered list of results in extension tag form, you might do this:

<ask format="ul">[[some relation to::Some Page]]</ask>

The equivalent parser function form is this:

{{#askmore:format=ul|[[some relation to::Some Page]]}}

-or-

{{#askmore:[[some relation to::Some Page]]|format=ul}}

-or-

{{#askmore:format=ul|query=[[some relation to::Some Page]]}}

You may provide all parameters that the original <ask> tag supported provided that you do not use the quotes around the values. One might deduce from the code that these are re-inserted by the parser extension.

Furthermore you can use the template parameters which allows you declare more interesting semantic templates. An example:

{{#askmore:format=ul 
  | [[Category:Extensions]] 
  | [[Refers to::{{{template_parameter_1}}}]]
  | [[Changed by::{{{anotherone}}}]]
}}

Good luck!

[edit] Code

<?php
$wgExtensionCredits['parserhook'][] = array(
'name'         => 'Semantic MediaWiki AskMore parser function',
'version'      => '1.1.0', // Jan 30, 2008.
'description'  => 'Provides the <tt><nowiki>{{#askmore}}</nowiki></tt> parserfunction which is similar to the SMW native <tt><nowiki>{{#ask}}</nowiki></tt> parser function, but '."''".'with'."''".' support for template parameters.',
'author'       => 'S.O.E. Ansems',
'url'          => 'http://www.mediawiki.org/wiki/Extension:SMW_ParserFunctionAsk',
);
 
$wgExtensionFunctions[] = 'smwParserFunctionAskMore_Setup';
$wgHooks['LanguageGetMagic'][]       = 'smwParserFunctionAskMore_Magic';
 
function smwParserFunctionAskMore_Setup() {
    global $wgParser;
    $wgParser->setFunctionHook( 'askmore', 'smwParserFunctionAskMore_Render' );
}
 
function smwParserFunctionAskMore_Magic( &$magicWords, $langCode ) {
    $magicWords['askmore'] = array( 0, 'askmore' );
    return true;
}
 
function smwParserFunctionAskMore_Render( &$parser ) {
	// Get the parameters that were passed to this function
	$params = func_get_args();
    array_shift( $params );
    $query = '';
    $askParams = '';
 
    foreach ($params as $key=>$param) {
 
    	// Test if the parameter looks like '{name}={val}'
    	if (strpos($param,'=')===false) {
    		// It didn't so we assume it is the query...
            $query = trim($param);
        } else {
        	// It did, now test if the {name} part is actually a name and
        	// not '[[paramter:' from a query like '[[parameter:=value]]'...
 
            list($name, $val) = split('=',$param,2);
            if(ctype_alpha($name)===true){
            	// Sucess, we've got an actual parameter...
            	$name = strtolower(trim($name));
            	$val = trim($val);
            	$askParams .= " {$name}=\"{$val}\"";
            } else {
            	// Nope, we've been fooled...
            	$query = trim($param);
            }
        }
    }
    return "<ask{$askParams}>{$query}</ask>";
}
Personal tools