MediaWiki:CommonsDelinker.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
var functionName = mw.util.getParamValue("function");
 
function universal_replace() {
    if (/^User(?:_talk)?:CommonsDelinker\/commands/.test(mw.config.get('wgPageName'))) {
        var old_image = mw.util.getParamValue("oldimage").replace(/_/g, ' ');
        if ( old_image === null ) {
            old_image = prompt('What is the name of the old image (without: "File:")');
        }
        var new_image = mw.util.getParamValue("newimage").replace(/_/g, ' ');
        if ( new_image === null ) {
            new_image = prompt('What is the name of the new image (without: "File:")');
        }
        var reason = mw.util.getParamValue("replace_reason").replace(/_/g, ' ');
        if ( reason === null ) {
            reason = '';
        }
 
        document.editform.wpTextbox1.value += '{{universal replace|' + old_image + '|' + new_image + '|reason=' + reason + '}}';
        document.editform.wpSummary.value = 'universal replace: [[:File:' + old_image + ']] → [[:File:' + new_image + ']] (' + reason + ')';
        if (typeof window.DelinkerSave == "undefined") {
            document.editform.wpDiff.click();
        } else {
            document.editform.wpSave.click();
        }
    }
}
 
function move_cat() {
    var oldcat = decodeURI(mw.util.getParamValue("oldcat")).replace(/_/g, ' '),
    	newcat = decodeURI(mw.util.getParamValue("newcat")).replace(/_/g, ' '),
    	reason = decodeURI(mw.util.getParamValue("reason") || '');
    var user = mw.config.get('wgUserName');
    if (reason) reason = '|reason=' + reason;
    if (mw.config.get( 'wgPageName' ) == 'User:CommonsDelinker/commands' || mw.config.get( 'wgPageName' ) == 'User_talk:CommonsDelinker/commands') {
        document.editform.wpTextbox1.value += '{{move cat|' + oldcat + '|' + newcat + reason + '|user=' + user + '}}';
        document.editform.wpSummary.value = 'Adding move cat: [[:Category:' + oldcat + ']] → [[:Category:' + newcat + ']]';
        document.editform.wpDiff.click();
    }
}

function approve_move_cat() {
	var scriptloc = "([[MediaWiki:CommonsDelinker.js|script]]) ";
    var oldcat = decodeURI(mw.util.getParamValue("oldcat")).replace(/_/g, ' '),
    	newcat = decodeURI(mw.util.getParamValue("newcat")).replace(/_/g, ' '),
    	user = decodeURI(mw.util.getParamValue("user")).replace(/_/g, ' '),
    	reason = decodeURI(mw.util.getParamValue("reason") || '');
	var answer = window.confirm("Do you want to approve the following catmove: Category:" + oldcat +" to Category: " + newcat +" ?");
    if (reason) reason = '|reason=' + reason;
    if (mw.config.get( 'wgPageName' ) == 'User:CommonsDelinker/commands') {
        document.editform.wpTextbox1.value += '{{move cat|' + oldcat + '|' + newcat + reason + '|user=' + user + '}}';
        document.editform.wpSummary.value = scriptloc + 'Adding approved move cat request: [[:Category:' + oldcat + ']] → [[:Category:' + newcat + ']]';
        if (answer === true) {
        	$.when( mw.loader.using( 'mediawiki.api' ), $.ready ).then( function () {
				var regex = new RegExp("{{move cat\\|" + oldcat + "\\|.*\n");
					new mw.Api().edit(
					         'User_talk:CommonsDelinker/commands/Category_moves',
					         function ( revision ) {
					             return {
					                 text: revision.content.replace( regex, '' ),
					                 summary: scriptloc + 'Removing approved cat move command.',
					                minor: true
					             };
					         }
					     )
					    .then( function () {
					         console.log( 'Saved! ');
					     } );
					} );
    		document.editform.wpSave.click();
		} else {
			document.editform.wpDiff.click();
		}
    }
}
 
for (var key in mw.config.get('wgUserGroups')) {
    if ((mw.config.get('wgUserGroups')[key] == 'sysop' ||  mw.config.get('wgUserGroups')[key] == 'filemover' ) && functionName == 'replace') {
        $(universal_replace);
        break;
    } else if (mw.config.get('wgUserGroups')[key] == 'autoconfirmed' && functionName == 'movecat') {
        $(move_cat);
        break;
    } else if (mw.config.get('wgUserGroups')[key] == 'sysop' && functionName == 'apprmovecat') {
        $(approve_move_cat);
        break;
    }
}