User:James086/orphanednonfreerevisions.js

if ( mw.config.get('wgNamespaceNumber') == 6 ) { var orphrevs = mw.util.addPortletLink( 'p-cactions', '#',		'orphrevs', 'ca-orphrevs', 'Rescaled per F5'); }

$( orphrevs ).click( function {	gettoken( get_old_stuff ); });

function gettoken( callback ) { var api = new mw.Api; api.get( {		action: 'tokens',		type: 'delete'	} ).done( function ( data ) {		callback( data.tokens.deletetoken );	} ); }

function delete_img( namething, deltoken ) { var api = new mw.Api; console.log("Inside delete_img"); api.post( {		action: 'delete',		title: mw.config.get('wgPageName'),		oldimage: namething,		reason: 'orphaned nonfree revisions, CSD F5',		token: deltoken,	} ).done( function ( data ) {		mw.notify("Deleted old revision.");	}); }

function get_old_stuff( token ) { var api = new mw.Api; 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);		$.each( imgs, function ( index, value ) { if ( index == 0 ) { return; }			delete_img(value.archivename, token); });		// Also edit!		var text = pgdata.revisions[0]['*'];		text = text.replace(/\n*\{\{\s?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.post( { action: 'edit', title: mw.config.get('wgPageName'), summary: 'Previous version(s) deleted per F5)',			text: text,			token: mw.user.tokens.get('editToken')		}).done( function ( data ) {			mw.notify("Removed the template.");		});

}); }