User:The Earwig/orfurrev.js

if ( mw.config.get('wgNamespaceNumber') == 6 ) { mw.loader.using( ['mediawiki.util'], function {		var portlet = mw.util.addPortletLink( 'p-cactions', '#', 'orfurrev', 'ca-orfurrev', 'Delete old revisions per F5');		$( portlet ).click( do_it );	}); }

function do_it { var api = new mw.Api; var flag = false; var action_finished = function(action) { mw.notify(action); if (!flag) { flag = true; return; }		setTimeout(function {			window.location.reload;		}, 1000); }

$("#ca-orfurrev").click(false).fadeOut(500); api.get( {		action: 'query',		prop: 'imageinfo|revisions',		titles: mw.config.get('wgPageName'),		iiprop: 'archivename',		iilimit: 'max',		rvprop: 'content',		rvlimit: '1'	}).done( function ( data ) {		var pgdata = data.query.pages[mw.config.get('wgArticleId')];		var imgs = pgdata.imageinfo;		console.log(imgs);		var todelete = [];		$.each( imgs, function ( index, value ) { if ( index !== 0 ) todelete.push( value.archivename.split('!')[0] ); });		api.postWithToken( 'delete', { action: 'revisiondelete', type: 'oldimage', target: mw.config.get('wgPageName'), ids: todelete.join('|'), hide: 'content', reason: 'Orphaned non-free file(s) deleted per F5' }).done( function( data ) { action_finished("Deleted old revision."); });

// Also edit! var text = pgdata.revisions[0]['*']; // from drilnoth's script text = text.replace(/\n*\{\{\s?(Non\-free reduced|Orphaned non\-free revisions)\s?\|\s?(?:\d|date)?\=?(\d\d\:\d\d\,\s\d\d?\s.*\s\d\d\d\d(\s\(UTC\))?|.*\d\d?(?:\s[a-z]+)?\s\d\d\d\d)\s?\}\}/ig,''); text = text.trim; api.postWithToken( 'edit', {			action: 'edit',			title: mw.config.get('wgPageName'),			summary: 'Previous version(s) deleted per F5)', text: text }).done( function ( data ) { action_finished("Removed the template."); });	}); }