Extension:Link Attributes
|
Link Attributes Release status: stable |
|
|---|---|
| Implementation | Parser function |
| Description | |
| Author(s) | Toby Inkster |
| Last version | 1.0 (2009-03-09) |
| MediaWiki | tested on 1.14.0 |
| License | GPL |
| Download | see below |
| Check usage and version matrix | |
This extension adds support for the rel, rev and class attributes on links.
The syntax is a little unorthodox. To set the rel attribute, place one or more tokens in double-parentheses at the end of the link title. e.g.:
[http://tobyinkster.co.uk/ My website((me home))]
The above would be converted to the following link:
<a href="http://tobyinkster.co.uk/" rel="me home" class="external">My website</a>
To set the class attribute, the same syntax is used, but tokens which represent classes are prefixed with a dot:
[http://tobyinkster.co.uk/ My website((me .class1 home .class2))]
Becomes:
<a href="http://tobyinkster.co.uk/" rel="me home" class="external class1 class2">My website</a>
Similarly, prefixing with a tilde (~) sets the rev attribute.
Tokens may be prefixed by a minus sign (-) to indicate that you don't want to add the token, but remove it. For example:
[http://tobyinkster.co.uk/ My website((me -.external home))]
Becomes:
<a href="http://tobyinkster.co.uk/" rel="me home">My website</a>
The "nofollow" rel token is considered untouchable though.
Installation [edit]
You need to patch includes/Linker.php. Find the makeExternalLink function and replace the existing wfRunHooks line with the following:
$success = wfRunHooks('LinkerMakeExternalLink', array( &$url, &$text, &$link, &$attribs, &$linktype, $this ) );
This allows the external link hook function access to a bit more information than it otherwise has. Hopefully MediaWiki 1.15.0 will incorporate this patch. This however seems to become unnecessary when the commented line in the linkattr_ExternalLink-function is used instead of the line above it, MediaWiki version 1.18.0 .
Add the following to LocalSettings.php:
require_once("extensions/linkAttributes.php");
Copy and paste the following as extensions/linkAttributes.php:
<?php /** * Link Attributes - easy modification of rel/rev/class on <a> elements. * * @author Toby Inkster <http://tobyinkster.co.uk/> * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later */ /** * Protect against register_globals vulnerabilities. * This line must be present before any global variable is referenced. */ if( !defined( 'MEDIAWIKI' ) ) { echo( "This is an extension to the MediaWiki package and cannot be run standalone.\n" ); die( -1 ); } // Extension credits that will show up on Special:Version $wgExtensionCredits['parserhook'][] = array( 'name' => 'Link Attributes', 'version' => '1.0', 'author' => 'Toby Inkster', 'url' => 'http://www.mediawiki.org/wiki/Extension:Link_Attributes', 'description' => 'easy modification of rel/rev/class on <a> elements' ); $wgHooks['LinkEnd'][] = 'linkattr_InternalLink'; $wgHooks['LinkerMakeExternalLink'][] = 'linkattr_ExternalLink'; function linkattr_ModifyLink (&$text, &$attribs, $isExternal = 0) { if ( preg_match('/^(.+)\(\((.*)\)\)$/', $text, $matches) ) { $text = trim($matches[1]); $rels = preg_split('/\s+/', $matches[2]); foreach ($rels as $r) { if ($isExternal && (strtolower($r)=='-nofollow')) continue; # Not allowed!! if ((substr($r, 0, 2) == '-~' || substr($r, 0, 2) == '~-') && isset($attribs['rev'])) $attribs['rev'] = str_ireplace(substr($r, 2), '', $attribs['rev']); elseif ((substr($r, 0, 2) == '-.' || substr($r, 0, 2) == '.-') && isset($attribs['class'])) $attribs['class'] = str_ireplace(substr($r, 2), '', $attribs['class']); elseif ((substr($r, 0, 1) == '-') && isset($attribs['rel'])) $attribs['rel'] = str_ireplace(substr($r, 1), '', $attribs['rel']); elseif (substr($r, 0, 1) == '~') $attribs['rev'] .= ' ' . substr($r, 1); elseif (substr($r, 0, 1) == '.') $attribs['class'] .= ' ' . substr($r, 1); else $attribs['rel'] .= ' ' . $r; } if (isset($attribs['rel'])) $attribs['rel'] = trim(preg_replace('/\s+/', ' ', $attribs['rel'])); if (isset($attribs['rev'])) $attribs['rev'] = trim(preg_replace('/\s+/', ' ', $attribs['rev'])); if (isset($attribs['class'])) $attribs['class'] = trim(preg_replace('/\s+/', ' ', $attribs['class'])); } } function linkattr_InternalLink ($skin, $target, $options, &$text, &$attribs, &$ret) { linkattr_ModifyLink($text, $attribs); return true; } function linkattr_ExternalLink (&$url, &$text, &$link, &$attribs, &$linktype, $skin) { $attribsText = $skin->getExternalLinkAttributes( $url, $text, 'external '.$linktype); # $attribsText = Html::expandAttributes( array( 'class' => 'external '.$linktype ) ); $mergedattribs = array_merge($attribs, Sanitizer::decodeTagAttributes($attribsText)); linkattr_ModifyLink($text, $mergedattribs, 1); if ($mergedattribs) $attribsText = Xml::expandAttributes( $mergedattribs ); $link = sprintf('<a href="%s"%s>%s</a>', $url, $attribsText, $text); return false; }