Extension:AllowAnchorTags

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

Release status: stable

Implementation Tag
Description Adds <anchor></anchor> tags and parses them to <a href=''></a> tags.
Last version 1.0
MediaWiki 1.9.3
License No license specified
Download #Download & screenshot
Example #Download & screenshot
Check usage and version matrix

Contents

What can this extension do? [edit]

Adds <anchor></anchor> tags and parses them to <a href=></a> tags.

Usage [edit]

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

Installation [edit]

To install/test the extension, save the code into a file called AllowAnchorTags.php add the following to the end of your LocalSettings.php file in your MediaWiki installation folder:

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

Code [edit]

<?php
 
//Extension credits that show up on Special:Version
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'AllowAnchorTags',
        'version' => 1.0,
        'author' => ' Amolsodhi',
        'url' => 'http://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]

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]

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]