Extension:SuppressExternalLinks

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
OOjs UI icon advanced.svg
SuppressExternalLinks
Release status: beta
Implementation Skin
Description Modifies links external to the wiki so they cannot be clicked.
Author(s) Big Ztalk
Latest version 1.0 (2011-07-11)
MediaWiki 1.17.0
License Public domain
Download SuppressExternalLinks-1.0.tar.gz
Translate the SuppressExternalLinks extension if it is available at translatewiki.net
Check usage and version matrix.

What can this extension do?[edit]

This extension modifies external links so they cannot be clicked. Hovering over the link shows the URL. This may be useful if say, you use an imported wiki on an Intranet with no WWW access. Suppressing the external links will prevent your users clicking them and getting 404 errors when the pages cannot be found.

The links are suppressed by replacing the <a> tag with a <span> tag. The href attribute of the <a> is then copied to the title attribute of the <span>. CSS can be used to style the spans so they look the same as the anchor tags that relaced (see example in css folder).

Screenshot[edit]

On the left a normal external link. On the right a suppressed external link.

Download instructions[edit]

Please download the archive found here and unpack it to the extensions folder of your MediaWiki installation.

Installation[edit]

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

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

Code[edit]

<?php
/**
 * SuppressExternalLinks - this extension modifies external links so they cannot be
 * clicked. Hovering over the link shows the URL.
 *
 * The links are suppressed by replacing the <a> tag with a <span> tag. The href attribute
 * of the <a> is then copied to the title attribute of the <span>. CSS can be used to style
 * the spans so they look the same (see example in css folder).
 *  
 * To activate this extension, add the following into your LocalSettings.php file:
 * require_once("$IP/extensions/SuppressExternalLinks/SuppressExternalLinks.php");
 *
 * @ingroup Extensions
 * @author Big Z
 * @version 1.0
 * @link http://www.mediawiki.org/wiki/Extension:SuppressExternalLinks Documentation
 * @license Public domain
 */
 
/**
 * 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['other'][] = array(
        'path'           => __FILE__,
        'name'           => 'SuppressExternalLinks',
        'version'        => '1.0',
        'author'         => 'Big Z', 
        'url'            => 'http://www.mediawiki.org/wiki/Extension:SuppressExternalLinks',
        'descriptionmsg' => 'description',
);

// Internationalisation
$wgExtensionMessagesFiles['SuppressExternalLinks'] = dirname( __FILE__ ) . '/SuppressExternalLinks.i18n.php';

// Register hooks
$wgHooks['LinkerMakeExternalLink'][] = 'selMakeExternalLink';
$wgHooks['OutputPageParserOutput'][] = 'selOutputPageParserOutput';

// Hook implementation to modify the external links
function selMakeExternalLink( &$url, &$text, &$link, &$attribs, $linktype ) {
    ////////////////////////////////////////
    // Preserve as many attributes as we can
    ////////////////////////////////////////
    
    // class attribute - adding our new classes
    $newAttribs = array();
    if ( isset( $attribs['class'] ) ) {
        $newAttribs['class'] = $attribs['class'] . " external $linktype";
    } else {
        $newAttribs['class'] =  "external $linktype";
    }

    // dir attribute
    if ( isset( $attribs['dir'] ) ) {
        $newAttribs['dir'] = $attribs['dir'];
    }
    
    // id attribute
    if ( isset( $attribs['id'] ) ) {
        $newAttribs['id'] = $attribs['id'];
    }
  
    // lang attribute
    if ( isset( $attribs['lang'] ) ) {
        $newAttribs['lang'] = $attribs['lang'];
    }
    
    // style attribute
    if ( isset( $attribs['style'] ) ) {
        $newAttribs['style'] = $attribs['style'];
    }

    // title attribute - modifying to the URL
    $newAttribs['title'] = $url;
  
    // xml:lang attribute
    if ( isset( $attribs['xml:lang'] ) ) {
        $newAttribs['xml:lang'] = $attribs['xml:lang'];
    }
    
    // Fill in the replacement link
    $attribsText = Html::expandAttributes( $newAttribs );
    $link = "<span $attribsText>$text</span>";
    return false;
}

// Hook implementation to add our CSS to each page
function selOutputPageParserOutput( &$out, $parseroutput ) {
    global $wgExtensionAssetsPath;
    $cssFile = "$wgExtensionAssetsPath/SuppressExternalLinks/css/SuppressExternalLinks.css";
    $out->addExtensionStyle($cssFile);
    return false;
}