Extension:ShortLinks

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
OOjs UI icon advanced.svg
ShortLinks
Release status: stable
Implementation Parser extension
Description Makes all URLs on the wiki short URLs.
Author(s) Zachary Sheetstalk
Latest version 1.0 (2011-08-09)
MediaWiki 1.6+
PHP 4.0.4+
License Free
Download See below
Hooks used
GetLocalURL
GetInternalURL
Translate the ShortLinks extension if it is available at translatewiki.net
Check usage and version matrix.

What can this extension do?[edit]

This extension will make all internal links conform to the same style when using Short URLs. Without this or a similar solution, "action" links such as edit and history continue to use the long format. This will catch those links and convert them to their short forms.

Usage[edit]

This extension uses the $wgScript and $wgArticlePath variables from LocalSettings.php. These should already be set if your wiki is using the Short URLs setup.

Download instructions[edit]

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

Installation[edit]

To install this extension, add the following to LocalSettings.php:

require_once("$IP/extensions/ShortLinks/ShortLinks.php");

Code[edit]

<?php
if (!defined('MEDIAWIKI')) {
    echo "This is an extension to the MediaWiki package and cannot be run standalone.\n";
    exit(-1);
}

// Extension credits that will show up on Special:Version
$wgExtensionCredits['parserhook'][] = array(
    'path'           => __FILE__,
    'name'           => 'Short Links',
    'version'        => '1.0',
    'author'         => 'Zachary Sheets',
    'url'            => 'http://www.mediawiki.org/wiki/Extension:ShortLinks',
    'description'    => 'Makes all URLs on the wiki short URLs.'
);

$wgHooks['GetLocalURL'][] = 'fnShortLinks';
$wgHooks['GetInternalURL'][] = 'fnShortLinks';
function fnShortLinks($title, $url, $query) {
    global $wgScript, $wgArticlePath;
    if (strpos($url, $wgScript.'?') !== false) {
        $url = preg_replace(':'.str_replace('.', '\.', $wgScript).'\?title=([^&]+)(&(.*))?:', str_replace('$1', '', $wgArticlePath)."$1?$3", $url);
    }
    return true;
}

Known issues[edit]