Extension:AllowGetParamsInWikilinks

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
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 UrlGetParameters
Author(s) (carallatalk)
Latest version 0.4 (2009-04-27)
MediaWiki 1.14
Database changes No
License GPL
Download See section Code
Hooks used
LinkBegin

Translate the AllowGetParamsInWikilinks extension if possible

Check usage and version matrix; code metrics

Introduction[edit | edit source]

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]]

Usage[edit | edit source]

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]]
  • ...

Download instructions and Installation[edit | edit source]

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.

Code[edit | edit source]

File AllowGetParamsInWikilinks.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>
 */
 
if ( !defined( 'MEDIAWIKI' ) ) {
        die( 'This file is an extension to MediaWiki and thus not a valid entry point.' );
}
 
$wgExtensionCredits['other'][] = array(
    'name'            => 'AllowGetParamsInWikilinks',
    'url'             => 'https://mediawiki.org/wiki/Extension:AllowGetParamsInWikilinks',
    'description'     => 'Allows get parameters for known articles in wikilinks',
    'author'          => 'Carlos A.',
    'version'         => '0.4'
);
 
$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 = ( (strpos($argumento, '=') != false) ? split('=', $argumento, 2) : array($argumento,'1') ); # avoid error with parameters like '?test' due to change them into '?test=1'
                $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;
}

See also[edit | edit source]