MediaWiki:Gadget-ExtraTabs2.js

From Wikiversity
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/**
 * 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, quotmark:single, undef:true, unused:true, trailing: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,
	// Quick reference to these
	conf = mw.config.get([
		'wgTitle',
		'wgUserLanguage',
		'wgNamespaceNumber',
		'wgCanonicalSpecialPageName',
		'wgNamespaceIds'
	]),
	username = conf.wgTitle.split('/')[0],
	nsIds = conf.wgNamespaceIds,
	// Hostname of main wiki for use in WikiSense
	project = 'commons.wikimedia.org';

extraTabs = {
	doInjectGroup: function ( group ) {
		$.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.
	 *
	 */
	getLinks: function ( key ) {
		switch ( key ) {
			case 'file':
				return [
				{
					target: mw.util.getUrl( 'Special:GlobalUsage/'+ conf.wgTitle ),
					label: 'checkusage',
					portlet: 'p-cactions',
					id: 'ca-checkusage'
				}, {
					target: '//tools.wmflabs.org/wikisense/CommonSense.php?' + $.param({ 'go-clean': 'yes', 'i': conf.wgTitle }),
					label: 'categorize',
					portlet: 'p-cactions',
					id: 'ca-commonsense'
				}, {
					target: mw.util.wikiScript() + '?' + $.param({ page: 'File:'+ conf.wgTitle, title: 'Special:Log' }),
					label: 'log',
					portlet: 'p-cactions',
					id: 'ca-log'
				}, {
					target: mw.util.wikiScript() + '?' + $.param({ title: 'File:'+ conf.wgTitle, 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: '//tools.wmflabs.org/wikisense/OrphanImages.php?' + $.param({ wiki: project, img_user_text: username }),
					label: 'orphans',
					portlet: 'p-cactions',
					id: 'ca-orphans'
				}, {
					target: '//tools.wmflabs.org/wikisense/UntaggedImages.php?' + $.param({ wiki: project, img_user_text: username }),
					label: 'untagged',
					portlet: 'p-cactions',
					id: 'ca-untagged'
				}, {
					target: '//tools.wmflabs.org/glamtools/glamorous.php?' + $.param({ doit: 'Do it!', username: username }),
					label: 'GLAMorous',
					portlet: 'p-cactions',
					id: 'ca-glamorous'
				}];
			case 'category':
				return [
				{
					target: '//tools.wmflabs.org/catscan3/catscan2.php?' + $.param({ language: 'commons', depth:'3', 'ns[6]':'1', categories: conf.wgTitle }),
					label: 'catscan2',
					portlet: 'p-cactions',
					id: 'ca-catscan2'
				}, {
					target: '//tools.wmflabs.org/glamtools/glamorous.php?' + $.param({ doit: 'Do it!', category: conf.wgTitle }),
					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)
mw.messages.set({
"extratabs-checkusage-label": "Global usage",
"extratabs-categorize-label": "Find categories",
"extratabs-log-label": "Log",
"extratabs-gallery-label": "Gallery",
"extratabs-orphans-label": "Orphans",
"extratabs-untagged-label": "Untagged",
"extratabs-tree-label": "Tree",
"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 ( conf.wgTitle.match( /^d+px-/ ) ) {
					var baseTitle = conf.wgTitle.replace( /^d+px-/, '' );
					extraTabs.doInjectGroup([{
						target: mw.util.getUrl( 'File:' + baseTitle ),
						label: 'unthumb',
						portlet: 'p-cactions',
						id: 'ca-unthumb'
					}]);
				}
				// Link to wikipedia file-page
				extraTabs.doInjectGroup([{
					target: '//' + mw.message( 'extratabs-wplangcode' ).plain() + '.wikipedia.org/wiki/File:' + conf.wgTitle,
					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
$.when(
	extraTabs.init(),
	$.ready
).done( extraTabs.startInjection );

}( jQuery, mediaWiki ) );