Extension:AllowGetParamsInWikilinks

From MediaWiki.org

Jump to: navigation, search

         

Manual on MediaWiki Extensions
List of MediaWiki Extensions
Crystal Clear action run.png
Allow Get Parameters in Wiki Links

Release status: stable

Implementation  Parser extension, Link markup
Description Allows user defined get parameters for known articles, to be used in conjunction with extensions such as Extension:UrlGetParameters
Last Version  0.4 (April, 27, 2009)
MediaWiki  1.14 (probably previous versions too)
License GPL
Download no link

check usage (experimental)

Contents

[edit] Introduction

Mediawiki understand the ... wikilinks as fully textual, so if you want to pass custom parameters to an article for later retrieving them using the Extension:UrlGetParameters extension, you should use an external link. This external link has its custom "external" css class, which provides a custom icon. If you can deal with this icon, you have no problem. But if you have external urls in your wiki text (and maybe you open them in an external window), you will not want an "external" behaviour for your internal links.

This extension enables the creation of wikilinks which allow get parameters as in the next example:

[[My/Article?param1=value1&param2=value2|Go to My/Article]]

[edit] Usage

Just use the classic GET format in your wikilinks

  • [[My/Article1?param1=value1]]
  • [[My/Article2?param1=value1&param2=value2]]
  • [[My/Article2?param1=value1&param2=value2|Go to]]
  • ...

[edit] Download instructions and Installation

Please cut and paste the code found below and place it in $IP/extensions/AllowGetParamsInWikilinks/AllowGetParamsInWikilinks.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

[edit] Code

File AllowGetParamsInWikikinks.php

<?php
 
/**
 * Allow Get Params In WikiLinks
 *
 * Funcion que hace que se permita el paso de parametros "get" en un enlce tipo WikiLink,
 * de la forma [[destino?param1=valor1&param2=valor2]]
 *
 * Actualmente, un wikilink no permite esto y se traduce en una pagina entera con nombre
 * "destino?param1=valor1&param2=valor2". Esta extension arregla esto para que se le pasen
 * los parametros tipo "get" a la pagina "destino"
 *
 * Posibles BUGS conocidos: cuando un articulo tiene ? en su texto y va a haber otro que tenga
 * un titulo exactamente igual, pero mas largo. Mientras no existe, se tomaria como un paso de
 * parametros. Una vez definido el articulo, ya se llegaria a el sin problemas porque se
 * reconoceria como un articulo existente; sin embargo, no se podría pasar parametros al artículo
 * corto.
 *
 * @author Carlos A. <caralla76@gmail.com>
 */
 
$wgExtensionCredits['other'][] = array(
    'name'            => 'AllowGetParamsInWikilinks',
    'url'            => 'http://mediawiki.org/wiki/Extension:AllowGetParamsInWikilinks',
    'description'    => 'Allows get parameters for known articles in wikilinks',
    'author'        => '[mailto:caralla76@gmail.com Carlos A.]',
);
 
$wgHooks['LinkBegin'][] = 'efAllowGetParamsInWikiLinks';
function efAllowGetParamsInWikiLinks($skin, $target, &$text, &$customAttribs, &$query, &$options, &$ret) {
 
        for ($i=0;$i<count($options) && ($options[$i]!='broken');$i++);
 
 
        if ($i>=count($options)) {
                /** known link */
                return true;
        }
        $brokenPos = $i;
        $direccion = parse_url($target->getText());
 
        if (!array_key_exists('query',$direccion)) {
                /** it is not written in the form target?query */
                return true;
        }
 
        if (isset($direccion['user']) ||
                isset($direccion['pass']) ||
                isset($direccion['host']) ||
                isset($direccion['scheme'])) {
                /** complex urls are not the target of this extension */
                return true;
        }
 
	/** patch for solving problems with Special pages */
	$pos = strpos($target->getPrefixedText(), "?");
	$direccion['path'] = substr($target->getPrefixedText(), 0, $pos);   
 
	$tituloArticulo = Title::newFromText($direccion['path']);
        if (!$tituloArticulo->isKnown()) {
                return true;
        }
        $nuevosArgumentos = array();
        foreach (explode('&',$direccion['query']) as $argumento) {
                $valores = split('=',$argumento,2);
                $nuevosArgumentos[$valores[0]] = $valores[1];
        }
        $query = array_merge($query,$nuevosArgumentos);
 
        /** customAttribs probably is not preloaded, but we use it in case it is */
        $myattribs = $customAttribs;
        $myattribs['href'] = $tituloArticulo->getLinkUrl();
        $myattribs['href'] = wfAppendQuery( $myattribs['href'], wfArrayToCgi( $query ) );
 
        /** Preparing the link text and the title */
        if (is_null($text))
                $text = htmlspecialchars($tituloArticulo->getPrefixedText());
 
        $myattribs['title'] = $tituloArticulo->getPrefixedText();
 
        /** Create the link */
        $ret = Xml::openElement( 'a', $myattribs ) . $text . Xml::closeElement( 'a' );
        return false;
}

[edit] See also

Extension:UrlGetParameters