Extension:ExtendAnchorTags

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

Release status: experimental

Implementation Tag
Description Adds <xa></xa> tags and parses them to <a class(es) href=''></a> tags.
Author(s) jeffmcneill
Latest version 0.5
MediaWiki 1.7.3, 1.9.3, 1.11
License No license specified
Download No link
0.5 added javascript: protocol support; 0.4 added itpc:// protocol option
Added rights

Released under GPL 3

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

Check usage and version matrix; code metrics

What can this extension do?[edit | edit source]

The purpose is to allow added class values to be included in anchor tags, to support POSH and microformats. Builds on the AllowAnchorTags extension. Adds support for <xa></xa> tags and parses them to <a class href=></a> tags.

  • Can support class, rel, and target

Usage[edit | edit source]

The URL must be specified in the following format: <xa class='some class(es)' href='http://someurl.com' target='_blank'>Some Text</xa>.

  • Both class and target are optional, but including one or the other is sort of the point of this extension.

Installation[edit | edit source]

To install/test the extension kindly add it to the end of your LocalSettings.php file in your MediaWiki installation folder.

Parameters[edit | edit source]

Changes to LocalSettings.php[edit | edit source]

require_once("extensions/ExtendAnchorTags.php");

Code[edit | edit source]

/**
 * ExtendAnchorTags.php
 * v.0.5 adds javascript: protocol support
 * v.0.4 adds itpc:// protocol support
 * v.0.3 has return codes to support mw 1.11
 * This extension creates the tag <xa></xa> allowing for class(es) and link attributes in anchor tags
 * written by Jeff McNeill http://jeffmcneill.com/
 * building on the work of AllowAnchorTags http://www.mediawiki.org/wiki/Extension:AllowAnchorTags
 * To activate the functionality of this extension include the following in LocalSettings.php file:
 * require_once('extensions/ExtendAnchorTags.php');
 */
 
#Defines the main function to be executed for this extension.
$wgExtensionFunctions[] = 'extendAnchorTag';
 
# Sets the hook to be executed once the parser has stripped HTML tags.
$wgHooks['ParserAfterStrip'][] = 'extendAnchorTag';
 
# This function initiates the hook for the parser to convert <xa></xa>
# tags to <a href=''></a> tags.
function extendAnchorTag() {
	// Declaring the global parser..
	global $wgParser;
 
	// Setting the hook to parse <xa></xa> tags from the parser output..
	$wgParser->setHook( 'xa', 'startExtendAnchor' );
	return(true);
}
 
# This function extracts the parameters from the <xa></xa> tags and
# the text between the <xa> and </xa> tags and formats them as "<a href=''>"
# tags and writes them in the document.
function startExtendAnchor( $input, $argv ) {
	// Matching to see if the URL matches the prefixes in $wgUrlProtocols.. plus some others
	if (preg_match("/^(http:\/\/|https:\/\/|ftp:\/\/|irc:\/\/|gopher:\/\/|news:|mailto:|skype:|xmpp:|itunes:|#|itpc:\/\/|javascript:)/", $argv['href'])) {
		// Fetching the 'href' parameter..
		$href = $argv['href'];
	} else {
		$href = '';
	}
 
	// Fetching the 'target' parameter..
	if(isset($argv['target'])) {
		$target = $argv['target'];
	} else {
		$target = '';
	}
 
	if (strncasecmp($input,'<img ',5) == 0) {
		$body = "<" . htmlspecialchars(preg_replace("/(^<)(.+)(>$)/","$2",$input)) . ">";
	} else {
		$body = htmlspecialchars($input);
	}
 
	// Fetching the 'class' parameter..
	if(isset($argv['class'])) {
		$class = $argv['class'];
	} else {
		$class = '';
	}
 
	// Fetching the 'rel' parameter..
	if(isset($argv['rel'])) {
		$rel = $argv['rel'];
	} else {
		$rel = '';
	}
 
	// Fetching the 'alt' parameter.. // note still need to incorporate this below
	if(isset($argv['alt'])) {
		$alt = $argv['alt'];
	} else {
		$alt = '';
	}
 
	if ($href != '' && $target != '' && $class != '' && $rel != '' ) {
		// all four
		return "<a" . " class=\"" . $class . "\"" . " rel=\"" . $rel . "\"" . " href=\"" . htmlspecialchars($href) . "\"" . " target=\"" . $target . "\">" . $body . "</a>";
	} else if ($href != '' && $target != '' && $class != '' ) {
		// target and class
		return "<a" . " class=\"" . $class . "\"" . " href=\"" . htmlspecialchars($href) . "\"" . " target=\"" . $target . "\">" . $body . "</a>";
	} else if ($href != '' && $target != '' && $rel != '' ) {
		// target and rel
		return "<a" . " rel=\"" . $rel . "\"" . " href=\"" . htmlspecialchars($href) . "\"" . " target=\"" . $target . "\">" . $body . "</a>";
	} else if ($href != '' && $class != '' && $rel != '' ) {
		// class and rel
		return "<a" . " class=\"" . $class . "\"" . " rel=\"" . $rel . "\"" . " href=\"" . htmlspecialchars($href) . "\">" . $body . "</a>";
	} else if ($href != '' && $class != '' ) {
		// class
		return "<a" . " class=\"" . $class . "\"" . " href=\"" . htmlspecialchars($href) . "\">" . $body . "</a>";
	} else if ($href != '' && $target != '' ) {
		// target 
		return "<a" . " href=\"" . htmlspecialchars($href) . "\"" . " target=\"" . $target . "\">" . $body . "</a>";
	} else if ($href != '' && $rel != '' ) {
		// rel
		return "<a" . " rel=\"" . $rel . "\"" . " href=\"" . htmlspecialchars($href) . "\">" . $body . "</a>";
	} else if ($href != '' ) {
		// nothing
		return "<a" . " href=\"" . htmlspecialchars($href) . "\">" . $body . "</a>";
	} else {
		return(false);
	}
}

To-do[edit | edit source]

  • Support for alt=""
  • Support to work within templates