Extension:ShortLinks

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear action run.png
ShortLinks

Release status: stable

Implementation Parser extension
Description Makes all URLs on the wiki short URLs.
Author(s) Zachary SheetsTalk
Last version 1.0 (2011-08-09)
MediaWiki 1.6+
PHP 4.0.4+
License Free
Download See below
Hooks used
GetLocalURL

GetInternalURL

Check usage (experimental)

Contents

[edit] What can this extension do?

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.

[edit] Usage

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.

[edit] Download instructions

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.

[edit] Installation

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

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

[edit] Code

<?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;
}
Personal tools
Namespaces

Variants
Actions
Navigation
Support
Download
Development
Communication
Print/export
Toolbox