Extension:AllowAnchorTags

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

Release status: stable

Implementation Tag
Description Adds <anchor></anchor> tags and parses them to <a href=''></a> tags.
Latest version 1.0
MediaWiki 1.9+
Database changes No
License No license specified
Download #Download & screenshot
Example #Download & screenshot
Tags
<anchor>
Hooks used
ParserAfterStrip

Translate the AllowAnchorTags extension if it is available at translatewiki.net

Check usage and version matrix; code metrics

The AllowAnchorTags extension adds <anchor></anchor> tags and parses them to <a href=></a> tags.

Usage[edit | edit source]

The URL must be specified in the following format: <anchor url='http://someurl.com' target='_blank'>Some Text</anchor>

Installation[edit | edit source]

  • Copy the code into a file and extract the file(s) in a directory called AllowAnchorTags in your extensions/ folder. If you're a developer and this extension is in a Git repository, then instead you should clone the repository.
  • Add the following code at the bottom of your LocalSettings.php:
require_once "$IP/extensions/AllowAnchorTags/AllowAnchorTags.php";
  • Done! Navigate to "Special:Version" on your wiki to verify that the extension is successfully installed.

Code[edit | edit source]

AllowAnchorTags.php
<?php

//Extension credits that show up on Special:Version
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'AllowAnchorTags',
        'version' => 1.0,
        'author' => ' Amolsodhi',
        'url' => 'https://www.mediawiki.org/wiki/Extension:AllowAnchorTags',
        'description' => 'Adds <tt>&lt;anchor&gt;</tt> tag and parses it to <tt><nowiki><a href=""></a></nowiki></tt> tags',
);

# Defines the main function to be executed for this extension.
$wgExtensionFunctions[] = 'addAnchorTag';

# Sets the hook to be executed once the parser has stripped HTML tags.
$wgHooks['ParserAfterStrip'][] = 'addAnchorTag';

# This function initiates the hook for the parser to convert <anchor></anchor>
# tags to <a href=''></a> tags.
function addAnchorTag() {
        // Declaring the global parser..
        global $wgParser;

        // Setting the hook to parse <anchor></anchor> tags from the parser output..
        $wgParser->setHook( 'anchor', 'startAddAnchor' );
       
        // Extensions required to return true
        return true;
}

# This function extracts the parameters from the <anchor></anchor> tags and
# the text between the <anchor> and </anchor> tags and formats them as "<a href=''>"
# tags and writes them in the document.
function startAddAnchor( $input, $argv ) {
        // Matching to see if the URL matches the prefixes in $wgUrlProtocols..
        if (preg_match("/^(http:\/\/|https:\/\/|ftp:\/\/|irc:\/\/|gopher:\/\/|news:|mailto:)/", $argv['url'])) {
                // Fetching the 'url' parameter..
                $url = $argv['url'];
        } else {
                $url = '';
        }

        // Fetching the 'target' parameter..
        $target = $argv['target'];

        if ($url != '' && $target != '') {
                // If a target parameter exists then print the link with it..
                return "<a href=\"" . htmlspecialchars($url) . "\" target=\"" . htmlspecialchars($target) . "\">" . htmlspecialchars($input) . "</a>";
        } else if ($url != '') {
                // If a target parameter does not exist then just print the link with the 'href' URL..
                return "<a href=\"" . htmlspecialchars($url) . "\">" . htmlspecialchars($input) . "</a>";
        } else {
                return '';
        }
}

Modification to allow the use of <IMG...>[edit | edit source]

Replace

        if ($url != '' && $target != '') {
                // If a target parameter exists then print the link with it..
                return "<a href=\"" . htmlspecialchars($url) . "\" target=\"" . htmlspecialchars($target) . "\">" . htmlspecialchars($input) . "</a>";
        } else if ($url != '') {
                // If a target parameter does not exist then just print the link with the 'href' URL..
                return "<a href=\"" . htmlspecialchars($url) . "\">" . htmlspecialchars($input) . "</a>";

with

        if (strncasecmp($input,'<img ',5) == 0) {
                $body = "<" . htmlspecialchars(preg_replace("/(^<)(.+)(>$)/","$2",$input)) . ">";
        } else {
                $body = htmlspecialchars($input);
        }
        if ($url != '' && $target != '') {
                 // If a target parameter exists then print the link with it..
                return "<a href=\"" . htmlspecialchars($url) . "\" target=\"" . htmlspecialchars($target) . "\">" . $body . "</a>";
        } else if ($url != '') {
                // If a target parameter does not exist then just print the link with the 'href' URL..
                return "<a href=\"" . htmlspecialchars($url) . "\">" . $body . "</a>";

When using <IMG...> in this way, you must use single quotes - for example:

<anchor url='http://www.site.tld'><img align='left' src='url'></anchor>

Error message fix[edit | edit source]

Change the line

$target = $argv['target'];

to

if(isset($argv['target'])) {$target = $argv['target'];} else { $target = '';}

to eliminate an annoying Undefined index: error message. Here's a gzipped tar file with both this fix and the <img...> change in it. Fetch it if you have access to gunzip and tar. You'll have to rename it or change your LocalSettings.php to use this version.

Download & screenshot[edit | edit source]

See also[edit | edit source]