MediaWiki:NewPagesUser.js

/** * Find new pages created by the supplied user * Looks for an element with ID "js-newpagesuser" */ (function (mw, $) {	'use strict';

var i18n = { // localized message strings en: { // English (default) nsMain: 'Main', errUser: 'Error: User is required', errNamespace: 'Error: Namespace is required', legendTop: 'New pages by user', legendSub: 'Namespaces', labelUser: 'Display pages by user', inputGo: 'Go', inputAll: 'Select all', inputNone: 'Deselect all', inputFlip: 'Toggle all', statQuery: 'Querying page contributions... $1',				statNone: 'No contributions found for $1', statDone: 'Query complete' },			fr: { // Français (French, or something like it) nsMain: 'Principal', errUser: 'Erreur: L\'utilisateur est requis', errNamespace: 'Erreur: L\'espace de noms est requis', legendTop: 'Nouvelles pages par l\'utilisateur', legendSub: 'Espaces de noms', labelUser: 'Afficher les pages par l\'utilisateur', inputGo: 'Aller', inputAll: 'Sélectionner tout', inputNone: 'Desélectionner tout', inputFlip: 'Basculer tout', statQuery: 'Demandant les contributions aux pages... $1',				statNone: 'Aucune contribution retrouvée pour $1', statDone: 'La requête est terminée' }		},		PREFIX = 'js-newpagesuser', ns = [], pages, user, count;

// get the parent IDs of selected revisions // output revision info if there is no parent function getParents { $.post('/api.php', {			format: 'json',			action: 'query',			prop: 'revisions',			rvprop: 'ids|timestamp',			revids: pages.slice(0, 50).join('|')		}, function (data) {			var				output = '',				a = [],				t, i;

if (data.query) { t = data.query.pages; // make it an array to sort it				// because the query unsorts it				for (i in t) { if (t.hasOwnProperty(i)) { a.push(t[i]); }				}				a.sort(function (x, y) {					return (x.revisions[0].revid < y.revisions[0].revid) ?						1 : -1;				}); for ( i = 0; i < a.length; ++i ) { // new pages are pages without a parent t = a[i].revisions[0]; if (t.parentid === 0) { t = new Date(t.timestamp).toUTCString; output += String.prototype.concat(							' ',								t.substr(5, 11).replace(/^0/, ' ') + ', ',								t.substr(17, 8).replace(/^0/, ' ') + ': ',							' ',							'',								a[i].title,							' '						); }				}				$('#' + PREFIX + '-list').append(output); if (pages.length) { // there's more data to get getParents; return; }				$('#' + PREFIX + '-list').append(i18n.statDone); } else if (data.error) { $('#' + PREFIX + '-list').append(					data.error.code + ': ' + data.error.info				); }			// re-enable Go, error or not $('#' + PREFIX + '-go').prop('disabled', false); });		pages = pages.slice(50);	}

// get all the user contributions // keep only the earliest contribution for each page // c (optional) = continuation function getPages(c) { var o = { format: 'json', action: 'query', list: 'usercontribs', uclimit: 500, ucprop: 'ids', ucuser: user },			ucns = [], all = true, i;

for ( i = 0; i < ns.length; ++i ) { all = all && ns[i].checked; if (ns[i].checked) { ucns.push(ns[i].id); }		}		// don't bother defining ucnamespace if they're all checked if (!all) { o.ucnamespace = ucns.join('|'); }		// first time or continuation ? if (c) { o.ucstart = c;			++count; } else { pages = {}; count = 1; }		$('#' + PREFIX + '-list').empty .append(i18n.statQuery.replace('$1', count)); $.post('/api.php', o, function (data) {			var				a, i;

if (data.query) { a = data.query.usercontribs; for ( i = 0; i < a.length; ++i ) { // keep only the earliest contribution for each page if ((pages[a[i].pageid] === undefined) ||						(a[i].revid < pages[a[i].pageid])) { pages[a[i].pageid] = a[i].revid; }				}				if (data['query-continue']) { // there's more data to get getPages(data['query-continue'].usercontribs.ucstart); return; }				// make an array of rev IDs for sorting a = []; for (i in pages) { if (pages.hasOwnProperty(i)) { a.push(pages[i]); }				}				if (a.length === 0) { $('#' + PREFIX + '-list').empty .append(i18n.statNone.replace('$1', user)); // re-enabled Go for error exit $('#' + PREFIX + '-go').prop('disabled', false); return; }				a.sort(function (x, y) {					// descending rev IDs					return (x < y) ? 1 : -1;				}); pages = a;				$('#' + PREFIX + '-list').empty; getParents; } else if (data.error) { $('#' + PREFIX + '-list').empty.append(					data.error.code + ': ' + data.error.info				); // re-enable Go for error exit $('#' + PREFIX + '-go').prop('disabled', false); }		});	}

// make and display the control field function makeControls { var fieldset = $(String.prototype.concat( ' ',					' ' + i18n.legendTop + ' ', ' ',								' ',							' ',						' ',					' ',				' ',				'' )),			i, j, s;

// make a 4-column table of namespaces j = 4 * Math.ceil(ns.length / 4); s = ''; for ( i = 0; i < j; ++i ) { if (4 * Math.floor(i / 4) === i) { s += ' '; }			if (i < ns.length) { s += String.prototype.concat(					'',						'',						'',							ns[i].name,						' ',					' '				); } else { s += ' '; }			if (4 * Math.floor(i / 4) === i - 3) { s += ' '; }		}		fieldset.find('#' + PREFIX + '-nspaces').prepend(s); // define the button actions fieldset.find('#' + PREFIX + '-go').click(function {			var				any = false,				i;

this.disabled = true; // no double-clicking user = $('#' + PREFIX + '-user').val .replace(/^\s+/, ).replace(/\s+$/, ); if (user === '') { $('#' + PREFIX + '-list').empty .append(i18n.errUser); // re-enable for error exit this.disabled = false; return; }			for ( i = 0; i < ns.length; ++i ) { ns[i].checked = $('#' + PREFIX + '-ns-' + ns[i].id).prop('checked'); any = any || ns[i].checked; }			if (!any) { $('#' + PREFIX + '-list').empty .append(i18n.errNamespace); // re-enable for error exit this.disabled = false; return; }			getPages; });		fieldset.find('#' + PREFIX + '-nsall').click(function { $('[id^=' + PREFIX + '-ns-]').prop('checked', true); });		fieldset.find('#' + PREFIX + '-nsnone').click(function { $('[id^=' + PREFIX + '-ns-]').prop('checked', false); });		fieldset.find('#' + PREFIX + '-nsflip').click(function { $('[id^=' + PREFIX + '-ns-]').each(function {				this.checked = !this.checked;			}); });		$('#' + PREFIX).empty.append(fieldset);	}

// check for the signature ID & exit if not found // otherwise, crank it up	$(function {		var			wgUserLanguage = mw.config.get('wgUserLanguage');

if ($('#' + PREFIX).length === 0) { return; // nothing to do		} // select localization if (i18n[wgUserLanguage] === undefined) { wgUserLanguage = wgUserLanguage.split('-')[0]; if (i18n[wgUserLanguage] === undefined) { wgUserLanguage = 'en'; }		}		i18n = i18n[wgUserLanguage]; // get namespace IDs and names $.post('/api.php', {			format: 'json',			action: 'query',			meta: 'siteinfo',			siprop: 'namespaces'		}, function (data) {			var				i, o, n;

if (data.query) { o = data.query.namespaces; for (i in o) { // make an array of namespace IDs and names // skip Media: and Special: if (o.hasOwnProperty(i) && (o[i].id > -1)) { n = o[i]['*']; ns.push({							id: o[i].id,							name: (n === '') ? '(' + i18n.nsMain + ')' : n						}); }				}				makeControls; }		});	}); }(mediaWiki, jQuery));