Jump to content

Snippets/Direct imagelinks to Commons: Difference between revisions

From mediawiki.org
Content deleted Content added
No edit summary
Code: @version 2014-08-09 + ( /\?/.test( currVal ) ? '&' : '?' ) + 'uselang=' + mw.config.get( 'wgUserLanguage' ) based on ar:s:MediaWiki:Gadget-directLinkToCommons.js
Line 16: Line 16:
* @source www.mediawiki.org/wiki/Snippets/Direct_imagelinks_to_Commons
* @source www.mediawiki.org/wiki/Snippets/Direct_imagelinks_to_Commons
* @author Krinkle
* @author Krinkle
* @version 2014-05-14
* @version 2014-08-09
*/
*/
if ( mw.config.get( 'wgNamespaceNumber', 0 ) >= 0 ) {
if ( mw.config.get( 'wgNamespaceNumber', 0 ) >= 0 ) {
Line 33: Line 33:
return currVal
return currVal
.replace( localBasePath, commonsBasePath )
.replace( localBasePath, commonsBasePath )
.replace( localBaseScript, commonsBaseScript );
.replace( localBaseScript, commonsBaseScript ) +
( /\?/.test( currVal ) ? '&' : '?' ) +
'uselang=' + mw.config.get( 'wgUserLanguage' );
}
}
} );
} );

Revision as of 01:23, 10 August 2014

How to use Snippets
List of Snippets
Direct imagelinks to Commons
Language(s): JavaScript
Compatible with: MediaWiki 1.23+ 

Description

Files hosted at Commons have local image links redirected to Commons. Supports HTTPS.

Code

/**
 * Direct imagelinks to Commons
 *
 * Required modules: jquery.mwExtension, mediawiki.util
 *
 * @source www.mediawiki.org/wiki/Snippets/Direct_imagelinks_to_Commons
 * @author Krinkle
 * @version 2014-08-09
 */
if ( mw.config.get( 'wgNamespaceNumber', 0 ) >= 0 ) {
	mw.hook( 'wikipage.content' ).add( function ( $content ) {
		var
			uploadBaseRe = /^\/\/upload\.wikimedia\.org\/wikipedia\/commons/,

			localBasePath = new RegExp( '^' + $.escapeRE( mw.util.getUrl( mw.config.get( 'wgFormattedNamespaces' )['6'] + ':' ) ) ),
			localBaseScript = new RegExp( '^' + $.escapeRE( 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:';

		$content.find( 'a.image' ).attr( 'href', function ( i, currVal ) {
			if ( uploadBaseRe.test( $( this ).find( 'img' ).attr( 'src' ) ) ) {
				return currVal
					.replace( localBasePath, commonsBasePath )
					.replace( localBaseScript, commonsBaseScript ) +
					( /\?/.test( currVal ) ? '&' : '?' ) +
					'uselang=' + mw.config.get( 'wgUserLanguage' );
			}
		} );
	} );
}

Notes

As of December 2012, installed as gadget (enabled by default) at:

In January 2014, it was also installed at:

On January 19th, 2014, it was installed also at:

It is furthermore included in the following files:

See also