Snippets/Direct imagelinks to Commons

From MediaWiki.org
Jump to: navigation, search
How to use Snippets
List of Snippets
Crystal Clear action run.png
Direct imagelinks to Commons
Language(s): javascript
Compatible with:

MediaWiki 1.18+ 

Contents

Description [edit]

Files hosted at Commons have local image links redirected to Commons. Supports the secure server (but not the deprecated secure.wikimedia.org).

Code [edit]

/**
 * Direct imagelinks to Commons
 *
 * @source mediawiki.org/wiki/Snippets/Direct_imagelinks_to_Commons
 * @author [[commons:User:Krinkle]]
 * @version 9
 */
if ( mw.config.get( 'wgNamespaceNumber', 0 ) >= 0 ) {
        $( function () {
                var
                        uploadBaseRe = /^\/\/upload\.wikimedia\.org\/wikipedia\/commons/,
 
                        localBasePath = mw.util.wikiGetlink( mw.config.get( 'wgFormattedNamespaces' )['6'] + ':' ),
                        localBaseScript = mw.util.wikiScript() + '?title=' + mw.util.wikiUrlencode( mw.config.get( 'wgFormattedNamespaces' )['6'] + ':' ),
 
                        commonsBasePath =  '//commons.wikimedia.org/wiki/File:',
                        commonsBaseScript =  '//commons.wikimedia.org/w/index.php?title=File:';
 
                $( 'a.image' ).attr( 'href', function ( i, currVal ) {
                        if ( uploadBaseRe.test( $(this).find( 'img' ).attr( 'src' ) ) ) {
                                return currVal
                                        .replace( localBasePath, commonsBasePath )
                                        .replace( localBaseScript, commonsBaseScript );
                        }
                });
        });
}

Notes [edit]

As of December 2012, this code is used as (enabled by default) gadget in dewiki, eswiki, fawiki, nlwiki, ruwiki and incubatorwiki.

See also [edit]