User:B/rescaledsidebar.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// This function will add a link to your Wikipedia toolbar.  It is required both for [[User:Howcheng/quickimgdelete.js|Howcheng's image deletion script]] and my template replacement script.
// It is not the right way to do things - we should be using mw.loader, but that doesn't seem to be working right now for some reason and plugging in this old code is easier than looking up
// the new API parameters.  Please, MediaWiki developers, don't have breaking changes.

/* This code may be problematic when it comes to titles with ampersands, etc, which are stored as ''& amp ;'' in HTML (without the spaces).
  A solution that solves this is the following:
    return document.title.substr(0, document.title.lastIndexOf(' - Wikipedia, the free'));
*/

function addLinkUserBRescaledSidebar(where, url, name, id, title, key, after){
    //* where is the id of the toolbar where the button should be added;
    //   i.e. one of "p-cactions", "p-personal", "p-navigation", or "p-tb".
    //
    //* url is the URL which will be called when the button is clicked.
    //   javascript: urls can be used to do more complex things.
    //
    //* name is what will appear as the name of the button.
    //
    //* id is the id of the button; it's best to define one.  
    //   Use a prefix to make sure its unique. Optional.
    //
    //* title is the tooltip title that gives a longer description 
    //   of the button; if you define a accesskey, mention it here. Optional.
    //
    //* key is the char you want for the accesskey. Optional.
    //
    //* after is the id of the button you want to follow this one. Optional.
    //
    var na = document.createElement('a');
    na.href = url;
    na.appendChild(document.createTextNode(name));
    var li = document.createElement('li');
    if(id) li.id = id;
    li.appendChild(na);
    var tabs = document.getElementById(where).getElementsByTagName('div')[0].getElementsByTagName('ul')[0];

    if(after) 
{
        tabs.insertBefore(li,document.getElementById(after));
    } else {
        tabs.appendChild(li);
    }
/*
    if(id) {
        if(key && title) { ta[id] = [key, title]; }
        else if(key) { ta[id] = [key, '']; }
        else if(title) { ta[id] = ['', title];} 
    }
    // re-render the title and accesskeys from existing code in wikibits.js
    akeytt();*/
    return li;
}


if ( mw.config.get('wgNamespaceNumber') == 6 ) {
	// Note: this code stopped working in January 2019
	/*
	mw.loader.using( ['mediawiki.util', 'mediawiki.api'], function() {
		var rescaled = mw.util.addPortletLink( 'p-tb', '#',
			'rescaled', 'ca-rescaled', 'Rescaled per F5', 's');
		$( rescaled ).click( do_it );
	});*/
	
	// Temporary workaround until we figure out why the above stopped working
    addLinkUserBRescaledSidebar('p-tb', 'javascript:OnUserBRescaledSidebarClick();', 'Remove Orphaned old version', 'ca-rescaled', 'Remove old version per F5');
}
 
function OnUserBRescaledSidebarClick() {
	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);
		var todelete = [];
		$.each( imgs, function ( index, value ) {
			if ( index === 0 ) {
				return;
			}
			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 [[WP:F5|F5]]'
		}).done( function( data ) {
				mw.notify("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 [[WP:F5|F5]])',
			text: text
		}).done( function ( data ) {
			mw.notify("Removed the template."); setTimeout(function() { window.location.reload();}, 3000);
		});
 
	});
}