MediaWiki:Gadget-ExtraTabs2.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
/**
* Extra toollinks for MediaWiki's interface.
*
* Written for Wikimedia Commons, may be useful on other wikis as well.
* Originally written by Arnomane in 2006, rewritten in 2011 by Krinkle.
*
* @rev 2015-06-02
* @author Duesentrieb, 2006
* @author Avatar, 2006
* @author Arnomane, 2006–2007
* @author Dbenbenn, 2006
* @author Timo Tijhof (Krinkle), 2010–2015
*/
/* jshint browser:true, curly:true, es3:true, latedef:true, undef:true, unused:true, onevar:true */
/* global jQuery, mediaWiki */

// Disable the old version, just in case it's also loaded
window.load_extratabs = false;

( function ( $, mw ) {
'use strict';

var extraTabs, promise,
	// Quick reference to these
	conf = mw.config.get( [
		'wgCanonicalSpecialPageName',
		'wgNamespaceIds',
		'wgNamespaceNumber',
		'wgPageName',
		'wgRelevantUserName',
		'wgUserLanguage'
	] ),
	pagename = mw.util.wikiUrlencode( conf.wgPageName ),
	username = conf.wgRelevantUserName,
	nsIds = conf.wgNamespaceIds;

extraTabs = {
	doInjectGroup: function ( group ) {
		promise.done( function () {
			$.each( group, function ( i, l ) {
			// 'label' can be a key or the message itself
				var msg = mw.message( 'extratabs-' + l.label + '-label' ),
					tooltip = mw.message( 'extratabs-' + l.label + '-tooltip' );
				msg = msg.exists() ? msg.toString() : l.label;
				tooltip = tooltip.exists() ? tooltip.toString() : l.tooltip;
				mw.util.addPortletLink( l.portlet, l.target, msg, l.id, tooltip );
			} );
		} );
	},

/**
 * Ge interface links to be added.
 * Grouped by category, containing an array of objects with
 * keys for target, label, id and portlet.
 *
 * Consider changing mw.util.getUrl() to mw.Title.newFromText( 'User:Foo' ).getUrl()
 * (returns the link with localized namespace prefix).
 */
	getLinks: function ( key ) {
		switch ( key ) {
			case 'file':
				return [
					{
						target: mw.util.getUrl( 'Special:GlobalUsage/' + conf.wgPageName.replace( 'File:', '' ) ),
						label: 'checkusage',
						portlet: 'p-cactions',
						id: 'ca-checkusage'
					}, {
						target: mw.util.wikiScript() + '?' + $.param( { page: conf.wgPageName, title: 'Special:Log' } ),
						label: 'log',
						portlet: 'p-cactions',
						id: 'ca-log'
					}, {
						target: mw.util.wikiScript() + '?' + $.param( { title: conf.wgPageName, action: 'purge' } ),
						label: 'purge',
						portlet: 'p-cactions',
						id: 'ca-purge'
					} ];
			case 'user':
				return [
					{
						target: mw.util.getUrl( 'Special:ListFiles/' + username ),
						label: 'gallery',
						portlet: 'p-cactions',
						id: 'ca-gallery'
					}, {
						target: 'https://glamtools.toolforge.org/glamorous.php?' + $.param( { doit: 'Do it!', username: username } ),
						label: 'GLAMorous',
						portlet: 'p-cactions',
						id: 'ca-glamorous'
					} ];
			case 'category':
				return [
					{
						target: 'https://glamtools.toolforge.org/glamorous.php?' + $.param( { doit: 'Do it!', category: pagename } ),
						label: 'GLAMorous',
						portlet: 'p-tb',
						id: 'tb-glamorous'
					} ];
			default:
				return [];
		}
	},

/**
 * Initialization
 * @return {Promise}
 */
	init: function () {
	/* jshint quotmark:false */

	/**
	 * Localization
	 *
	 * To be moved into the MediaWiki-namespace once ResourceLoader 2.0
	 * is ready and Gadgets supports msgs. That'll make translating a lot easier
	 * and removes the need to importScript() another JS-page in init().
	 */
	// (not indented to make it easier to copy/paste for translations)
		var uploads = mw.message( 'Uploadslink-portlet-label' );
		mw.messages.set( {
			'extratabs-checkusage-label': 'Global usage',
			'extratabs-categorize-label': 'Find categories',
			'extratabs-log-label': 'Log',
			'extratabs-orphans-label': 'Orphans',
			'extratabs-untagged-label': 'Untagged',
			'extratabs-tree-label': 'Tree',
			'extratabs-gallery-label': uploads.exists() ? uploads.toString() : 'Uploads',
			'extratabs-catscan2-label': 'CatScan',
			'extratabs-catdown-label': 'Download all',
			'extratabs-GLAMorous-label': 'Usage of all files',
			'extratabs-GLAMorous-tooltip': 'Check usage of files (GLAMorous-Tool) on wikis of the Wikimedia Foundation',
			'extratabs-blocklog-label': 'Block log',
			'extratabs-purge-label': 'Purge',
			'extratabs-thumbPurge-label': 'Generate thumbnail',
			'extratabs-wplangcode': 'en',
			'extratabs-wppage-label': '$1-wiki'
		} );
		if ( conf.wgUserLanguage === 'en' ) {
			return $.Deferred().resolve();
		}

		// Import another set of messages, which either
		// does nothing (if the subpage doesn't exist)
		// or re-sets one or more messages into the user language
		return $.ajax( {
			url: '//commons.wikimedia.org/w/index.php?title=MediaWiki:Gadget-ExtraTabs2.js/' + conf.wgUserLanguage + '&action=raw&ctype=text/javascript',
			dataType: 'script',
			cache: true,
			timeout: 3000
		} );
	},
/**
 * Injection of interface links.
 * Shouldn't be called until the document is ready.
 */
	startInjection: function () {
		switch ( conf.wgNamespaceNumber ) {
			case nsIds.special:
				if ( conf.wgCanonicalSpecialPageName === 'Blockip' ) {
					username = $( '#mw-bi-target' ).val();
					if ( username ) {
						extraTabs.doInjectGroup( [ {
							target: mw.util.wikiScript() + '?' + $.param( { title: 'Special:Log', type: 'block', page: 'User:' + username } ),
							label: 'blocklog',
							portlet: 'p-cactions',
							id: 'ca-blocklog'
						} ] );
					}
				} else if ( conf.wgCanonicalSpecialPageName === 'Contributions' ) {
					username = document.getElementsByName( 'target' )[ 0 ].value;
					extraTabs.doInjectGroup( extraTabs.getLinks( 'user' ) );
				}
				break;
			case nsIds.user:
			case nsIds.user_talk:
				extraTabs.doInjectGroup( extraTabs.getLinks( 'user' ) );
				break;
			case nsIds.file:
			case nsIds.file_talk:
				extraTabs.doInjectGroup( extraTabs.getLinks( 'file' ) );
				// Unthumb stuff (Links to File:Foo.jpg from File:123px-Foo.jpg)
				if ( pagename.match( /^d+px-/ ) ) {
					extraTabs.doInjectGroup( [ {
						target: mw.util.getUrl( pagename.replace( /^d+px-/, '' ) ),
						label: 'unthumb',
						portlet: 'p-cactions',
						id: 'ca-unthumb'
					} ] );
				}
				// Link to Wikipedia file-page
				extraTabs.doInjectGroup( [ {
					target: '//' + mw.msg( 'extratabs-wplangcode' ) + '.wikipedia.org/wiki/' + pagename,
					label: mw.msg( 'extratabs-wppage-label', mw.msg( 'extratabs-wplangcode' ) ),
					portlet: 'p-cactions',
					id: 'ca-wppage'
				} ] );
				break;
			case nsIds.category:
				extraTabs.doInjectGroup( extraTabs.getLinks( 'category' ) );
				break;
		}
	}
};

// Explose publically
mw.commonsExtraTabs = extraTabs;

// Show tabs as soon as the DOM is ready, and localisation is loaded
promise = $.when(
	mw.loader.using('mediawiki.api').then( function () {
		return new mw.Api().loadMessagesIfMissing(['Uploadslink-portlet-label']);
	}).then( extraTabs.init ),
	$.ready
);

promise.done( extraTabs.startInjection );

}( jQuery, mediaWiki ) );