MediaWiki:Gadget-libUtil.js

/** * * This is a library for containing common used functions on Commons *  BUT that could be also useful to other wikis * * Inclusion-scope: *  "used by one or better more of the default or in one of the 5 most popular gadgets" *  "and is potentially useful for other tools on Commons" * Keep in mind: It's a library. Do not change any variable-state or the UI until a method is called. * * All things that are specific to Commons (as a media hoster) should go into MediaWiki:Gadget-libCommons.js. * * Coding convetion: * Do not self-refer inside a function using "this"; instead use "lc" * to allow reusing these functions in an altered scope * * Derived from MediaWiki:Gadgetprototype.js * * Invoke jsHint-validation on save. * * @author Rillke, 2012 */

/*global $:false, mw:false*/ /*jshint curly:false*/

( function { "use strict";

if (!mw.libs.commons) mw.libs.commons = {}; var lc = mw.libs.commons;

$.extend(mw.libs.commons, {	/**	* Some pages are related to a user. 	* For example Special:Contributions/Rillke lists the contributions of User:Rillke.	* @author	*     Rillke, one RegExp by DieBuche	*	* @example	*      mw.libs.commons.guessUser;	*	* @context {mw.libs.commons} or any other	* @return {string} The user name guessed from URL or wgPageName.	*/	guessUser: function {		var title, target, user;

user = mw.config.get( 'wgRelevantUserName' ); if ( user ) { return user; }

switch ( mw.config.get( 'wgNamespaceNumber' ) ) { case 3: // User_talk case 2: // User return mw.config.get( 'wgPageName' ) .match( /.*?\:(.*?)(\/.*)*$/ )[ 1 ]; case -1: // Special pages try { switch ( mw.config.get( 'wgCanonicalSpecialPageName' ) ) { case 'CentralAuth': // T131740 target = mw.util.getParamValue( 'target' ); if ( target ) { return target; }							title = mw.util.getParamValue( 'title' ); if ( title ) { title = title.match( /Special\:(?:CentralAuth)\/(.*)$/ ); if ( title ) { return title[ 1 ]; }							}							if ( /Special\:(?:CentralAuth)\//.test( location.href ) ) { return decodeURIComponent(									location.href.match( /Special\:(?:CentralAuth)\/(.*?)(?:[\?&].*)?$/ )[ 1 ] ); }							break; case 'Log': if ( mw.util.getParamValue( 'page' ) && /User:+./.test( mw.util.getParamValue( 'page' ) ) ) { return mw.util.getParamValue( 'page' ) .replace( 'User:', '' ); }							break; }				} catch ( ex ) {} break; }	},	/**	* source: MediaWiki:Gadget-AjaxQuickDelete.js * Very simple date formatter. Replaces the substrings "YYYY", "MM" and "DD" in a	* given string with the UTC year, month and day numbers respectively. * Also replaces "MON" with the English full month name and "DAY" with the unpadded day. *	* wgMonthNames can't be used because it is in Page content language which is	* the user's language on Special:-pages *	* @example *     mw.libs.commons.formatDate('year=YYYY|month=MON|day=DD'); *	* @param {string} fmt The (format) template-string * @param {Date} date A date to use * @param {Date} fallbackDate If date was empty, use this date instead. If it's also empty the current date will be used. *	* @return {string} A formatted date-string. */	monthNamesInSiteLang: ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], formatDate: function (fmt, date, fallbackDate) { var pad0 = function (s) { return (s > 9 ? s : "0" + s); }; // zero-pad to two digits date = date || fallbackDate || new Date; var month = date.getUTCMonth + 1; fmt = fmt.replace(/YYYY/g, date.getUTCFullYear); fmt = fmt.replace(/MM/g, pad0(month)); date = date.getUTCDate; fmt = fmt.replace(/DD/g, pad0(date)); fmt = fmt.replace(/MON/g, lc.monthNamesInSiteLang[month]); fmt = fmt.replace(/DAY/g, date); return fmt; },

/**	* Get the talk page that belongs to a given page title *	*	* @example *     // Returns "Commons talk:Foo" *     mw.libs.commons.getTalkPageFromTitle('Commons:Foo'); *	* @param {string} title * @return {string} talk page */	getTalkPageFromTitle: function (title) { var rens = /^(.+)\:/, pref = title.match(rens), nsid = -1, newPref;

if (pref) { pref = pref[1].toLowerCase.replace(/ /g, '_'); } else { pref = ''; }		nsid = mw.config.get('wgNamespaceIds')[pref]; // If it was not a talk page, increment namespace id		if (0 === nsid % 2) nsid++; newPref = mw.config.get('wgFormattedNamespaces')[nsid] + ':'; if (pref) { title = title.replace(/^.+\:/, newPref); } else { title = newPref + title; }		return title; },

/**	* Get the file title from a file source link *	*	* @example *     mw.libs.commons.titleFromImgSrc($('img').attr('src')); *	* @param {string} src The image source (upload.wikimedia.org ...) *	* @return {string} Either the file title (without File:-namespace), if successful or undefined * @deprecated */	titleFromImgSrc: function(src) { mw.log.warn( ".titleFromImgSrc is deprecated. Use mw.Title.newFromImg instead." ); try { return decodeURIComponent(src).match(/\/[a-f0-9]\/[a-f0-9]{2}\/(\S+\.\S{2,5})\//)[1].replace(/_/g, ' '); } catch (ex) { try { return decodeURIComponent(src).match(/thumb\.php.*(?:\?|\&)f=(\S+\.\S{2,5})(?:\&.+)?$/)[1].replace(/_/g, ' '); } catch (ex2) { try { return decodeURIComponent(src).match(/\/[a-f0-9]\/[a-f0-9]{2}\/(\S+\.\S{2,5})$/)[1].replace(/_/g, ' '); } catch (ex3) {} }		}	} });

}); //