User:Schnark/mostEdited.js

/** * User:Schnark/mostEdited.js * * User script to show the pages which were edited most in the last time * * @author Michael Müller (User:Schnark) * @license GPL v2 (+ CC-BY-SA as all pages in this wiki) * * * Copyright (C) 2011 Michael Müller * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to * * Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor * Boston, MA  02110-1301, USA. * * */ /*global jQuery: true, mw: true */ ( function ( $ ) {

/** * pagesList contains information for every edited page in the form * 'Pagename': { *   oldsize: 1234, *	newsize: 4321, *	edits: 123, *	users: ['A', 'B'], *	sections: { *		'Section A': { edits: 12 } *	} * } */

var pagesList = {};

/** * This function gets all recent changes in the namespaces starting at the start time. * After the last API call has been done it will call the callback function * @param start {string} start time (timestamp) * @param namespaces {string} namespaces to show, either empty for all or something like '0|1|5' * @param callback {function} function after the last API call */ function getAPIRecentChanges ( start, namespaces, callback ) { var data = { action: 'query', list: 'recentchanges', rcstart: start, rcdir: 'newer', rclimit: 'max', rcprop: 'user|comment|title|sizes', format: 'json' };	if ( namespaces ) { data.rcnamespace = namespaces; }	$.getJSON( mw.util.wikiScript( 'api' ), data, function ( json ) {		if ( json && json.query && json.query.recentchanges ) {			var rc = json.query.recentchanges;			for ( var i = 0; i < rc.length; i++ ) {				var edit = rc[i];				if ( !( edit.title in pagesList ) ) {					pagesList[edit.title] = {						oldsize: edit.oldlen,						edits: 0,						users: [],						sections: {}					};				}				pagesList[edit.title].edits++;				pagesList[edit.title].newsize = edit.newlen;				if ( $.inArray( edit.user, pagesList[edit.title].users ) === -1 ) {					pagesList[edit.title].users.push( edit.user );				}				var section = /^\/\*\s*(.*?)\s*\*\//.exec( edit.comment );				if ( section ) {					section = section[1];				}				if ( section ) {					if ( !( section in pagesList[edit.title].sections ) ) {						pagesList[edit.title].sections[section] = {edits: 1};					} else {						pagesList[edit.title].sections[section]++; }				}			}		}		if ( json && json['query-continue'] && json['query-continue'].recentchanges ) { getAPIRecentChanges( json['query-continue'].recentchanges.rcstart, namespaces, callback ); } else { callback; }	}); }

/** * get the pages/sections with the most edits * @param data {object} data about the number of edits, entries must have the form^ *	'Name': {edits: 123} * @param count {number} number of pages/sections to get * @return {array} list of the pages/sections with the most edits (decreasing order) */

function getMostEdited ( data, count ) { var items = []; for ( var item in data ) { items.push( item ); }	items.sort( function ( a, b ) {		return data[b].edits - data[a].edits;	} ); return items.slice( 0, count ); }

/** * get HTML for one entry * @param page {string} name of the page to get HTML for * @return {string} HTML */

function generatePageHTML ( page ) { var data = pagesList[page]; var html = mw.html.element( 'h2', {}, new mw.html.Raw( mw.html.element( 'a', {href: mw.util.wikiGetlink( page ), title: page}, page ) ) );	html += mw.html.element( 'ul', {}, new mw.html.Raw( mw.html.element( 'li', {}, data.edits + ' edits' ) + // FIXME localize mw.html.element( 'li', {}, data.users.length + ' users' ) + // FIXME localize mw.html.element( 'li', {}, data.newsize - data.oldsize + ' bytes' ) // FIXME localize ) );	var sections = getMostEdited( data.sections, 3 ); // FIXME configure for ( var i = 0; i < sections.length; i++ ) { html += mw.html.element( 'h3', {}, new mw.html.Raw( mw.html.element( 'a',				{href: mw.util.wikiGetlink( page ) + '#' + sections[i]},				sections[i] ) ) ); // FIXME some more information }	return html; }

/** * get HTML for complete list * @return {string} HTML */

function generateHTML { var pages = getMostEdited ( pagesList, 10 ), // FIXME configure html = ''; for ( var i = 0; i < pages.length; i++ ) { html += generatePageHTML( pages[i] ); }	return html; }

/** * get HTML for header * @return {string} HTML */

function generateHeaderHTML { $( '#firstHeading' ).text( 'Most edited pages' ); // FIXME localize var html = ''; html += " Sorry, you can't select namespaces etc. yet!  "; // FIXME html += mw.html.element( 'div', {id: 'mostEditedContainer'} ); return html; }

/** * called when user clicks submit button */

function submitQuery { var ago = new Date( now = ( new Date ).getTime - (60 * 60 * 1000) ), // 1 hour ago, FIXME user input start = String( ago.getUTCFullYear ) + String( ago.getUTCMonth + 101 ).substr( 1 ) + String( ago.getUTCDate + 100 ).substr( 1 ) + String( ago.getUTCHours + 100 ).substr (1 ) + String( ago.getUTCMinutes + 100 ).substr( 1 ) + String( ago.getUTCSeconds + 100 ).substr( 1 ), namespaces = ''; // FIXME user input $( '#submitButton' ).attr( 'disabled', 'disabled' ); pagesList = {}; getAPIRecentChanges( start, namespaces, function {		$( '#mostEditedContainer' ).html( generateHTML );		$( '#submitButton' ).removeAttr( 'disabled' );	}); }

/** * initialises the page */

function init { document.title = 'Most edited pages'; // FIXME localize mw.util.$content.html( generateHeaderHTML ); $( '#submitButton' ).click( submitQuery ).click; }

if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Blankpage' &&	mw.util.getParamValue( 'action' ) === 'mostedited' ) {	$(init); }

} )( jQuery ); //