MediaWiki:Gadget-twinkleunlink.js: Difference between revisions

Localise
m (1 revision imported)
 
(Localise)
 
(4 intermediate revisions by 2 users not shown)
Line 10: Line 10:
  ****************************************
  ****************************************
  * Mode of invocation:    Tab ("Unlink")
  * Mode of invocation:    Tab ("Unlink")
  * Active on:              Non-special pages, except Wikipedia:Sandbox
  * Active on:              Non-special pages, except Sarkarverse:Sandbox
  * Config directives in:  TwinkleConfig
  * Config directives in:  TwinkleConfig
  */
  */


Twinkle.unlink = function twinkleunlink() {
Twinkle.unlink = function twinkleunlink() {
if( mw.config.get('wgNamespaceNumber') < 0 || mw.config.get('wgPageName') === 'Wikipedia:Sandbox' ) {
if( mw.config.get('wgNamespaceNumber') < 0 || mw.config.get('wgPageName') === 'Sarkarverse:Sandbox' ) {
return;
return;
}
}
Twinkle.addPortletLink( Twinkle.unlink.callback, "Unlink", "tw-unlink", "Unlink backlinks" );
// Restrict to extended confirmed users. This is done here instead of above to avoid an unnecessary API call.
mw.user.getRights().then(function (rights) {
if( rights.indexOf('extendedconfirmed') !== -1 ) {
Twinkle.addPortletLink( Twinkle.unlink.callback, "Unlink", "tw-unlink", "Unlink backlinks" );
}
});
};
};


Line 39: Line 44:
Window.setTitle( "Unlink backlinks" + (mw.config.get('wgNamespaceNumber') === 6 ? " and file usages" : "") );
Window.setTitle( "Unlink backlinks" + (mw.config.get('wgNamespaceNumber') === 6 ? " and file usages" : "") );
Window.setScriptName( "Twinkle" );
Window.setScriptName( "Twinkle" );
Window.addFooterLink( "Twinkle help", "WP:TW/DOC#unlink" );
Window.addFooterLink( "Twinkle help", "SV:TW/DOC#unlink" );


var form = new Morebits.quickForm( Twinkle.unlink.callback.evaluate );
var form = new Morebits.quickForm( Twinkle.unlink.callback.evaluate );


// prepend some basic documentation
// prepend some basic documentation
var node1 = Morebits.htmlNode("code", "[[" + Morebits.pageNameNorm + "|link text]]")
var node1 = Morebits.htmlNode("code", "[[" + Morebits.pageNameNorm + "|link text]]");
var node2 = Morebits.htmlNode("code", "link text");
var node2 = Morebits.htmlNode("code", "link text");
node1.style.fontFamily = node2.style.fontFamily = "monospace";
node1.style.fontFamily = node2.style.fontFamily = "monospace";
Line 51: Line 56:
type: 'div',
type: 'div',
style: 'margin-bottom: 0.5em',
style: 'margin-bottom: 0.5em',
label: [  
label: [
'This tool allows you to unlink all incoming links ("backlinks") that point to this page' +  
'This tool allows you to unlink all incoming links ("backlinks") that point to this page' +
(mw.config.get('wgNamespaceNumber') === 6 ? ", and/or hide all inclusions of this file by wrapping them in <!-- --> comment markup" : "") +  
(mw.config.get('wgNamespaceNumber') === 6 ? ", and/or hide all inclusions of this file by wrapping them in <!-- --> comment markup" : "") +
". For instance, ",
". For instance, ",
node1,
node1,
Line 80: Line 85:
'iulimit': Morebits.userIsInGroup( 'sysop' ) ? 5000 : 500, // 500 is max for normal users, 5000 for bots and sysops
'iulimit': Morebits.userIsInGroup( 'sysop' ) ? 5000 : 500, // 500 is max for normal users, 5000 for bots and sysops
'blnamespace': Twinkle.getPref('unlinkNamespaces'),
'blnamespace': Twinkle.getPref('unlinkNamespaces'),
'iunamespace': Twinkle.getPref('unlinkNamespaces')
'iunamespace': Twinkle.getPref('unlinkNamespaces'),
'rawcontinue': true
};
};
} else {
} else {
Line 89: Line 95:
'blfilterredir': 'nonredirects',
'blfilterredir': 'nonredirects',
'bllimit': Morebits.userIsInGroup( 'sysop' ) ? 5000 : 500, // 500 is max for normal users, 5000 for bots and sysops
'bllimit': Morebits.userIsInGroup( 'sysop' ) ? 5000 : 500, // 500 is max for normal users, 5000 for bots and sysops
'blnamespace': Twinkle.getPref('unlinkNamespaces')
'blnamespace': Twinkle.getPref('unlinkNamespaces'),
'rawcontinue': true
};
};
}
}
Line 105: Line 112:


Twinkle.unlink.callback.evaluate = function twinkleunlinkCallbackEvaluate(event) {
Twinkle.unlink.callback.evaluate = function twinkleunlinkCallbackEvaluate(event) {
Twinkle.unlink.backlinksdone = 0;
Twinkle.unlink.imageusagedone = 0;
function processunlink(pages, imageusage) {
var statusIndicator = new Morebits.status((imageusage ? 'Unlinking instances of file usage' : 'Unlinking backlinks'), '0%');
var total = pages.length;  // removing doubling of this number - no apparent reason for it
Morebits.wiki.addCheckpoint();
if( !pages.length ) {
statusIndicator.info( '100% (completed)' );
Morebits.wiki.removeCheckpoint();
return;
}
// get an edit token
var params = { reason: reason, imageusage: imageusage, globalstatus: statusIndicator, current: 0, total: total };
for (var i = 0; i < pages.length; ++i)
{
var myparams = $.extend({}, params);
var articlepage = new Morebits.wiki.page(pages[i], 'Unlinking in article "' + pages[i] + '"');
articlepage.setCallbackParameters(myparams);
articlepage.setBotEdit(true);  // unlink considered a floody operation
articlepage.load(imageusage ? Twinkle.unlink.callbacks.unlinkImageInstances : Twinkle.unlink.callbacks.unlinkBacklinks);
}
}
var reason = event.target.reason.value;
var reason = event.target.reason.value;
if (!reason) {
if (!reason) {
Line 138: Line 118:
}
}


var backlinks, imageusage;
var backlinks = [], imageusage = [];
if( event.target.backlinks ) {
if( event.target.backlinks ) {
backlinks = Twinkle.unlink.getChecked2(event.target.backlinks);
backlinks = Twinkle.unlink.getChecked2(event.target.backlinks);
Line 148: Line 128:
Morebits.simpleWindow.setButtonsEnabled( false );
Morebits.simpleWindow.setButtonsEnabled( false );
Morebits.status.init( event.target );
Morebits.status.init( event.target );
Morebits.wiki.addCheckpoint();
 
if (backlinks) {
var pages = Morebits.array.uniq(backlinks.concat(imageusage));
processunlink(backlinks, false);
 
}
var unlinker = new Morebits.batchOperation("Unlinking backlinks" + (imageusage ? " and instances of file usage" : ""));
if (imageusage) {
unlinker.setOption("preserveIndividualStatusLines", true);
processunlink(imageusage, true);
unlinker.setPageList(pages);
}
var params = { reason: reason, unlinker: unlinker };
Morebits.wiki.removeCheckpoint();
unlinker.run(function(pageName) {
var wikipedia_page = new Morebits.wiki.page(pageName, "Unlinking in article \"" + pageName + "\"");
wikipedia_page.setBotEdit(true); // unlink considered a floody operation
var innerParams = $.extend({}, params);
innerParams.doBacklinks = backlinks && backlinks.indexOf(pageName) !== -1;
innerParams.doImageusage = imageusage && imageusage.indexOf(pageName) !== -1;
wikipedia_page.setCallbackParameters(innerParams);
wikipedia_page.load(Twinkle.unlink.callbacks.unlinkBacklinks);
});
};
};
Twinkle.unlink.backlinksdone = 0;
Twinkle.unlink.imageusagedone = 0;


Twinkle.unlink.callbacks = {
Twinkle.unlink.callbacks = {
Line 189: Line 174:
type: 'div',
type: 'div',
label: "Selected namespaces: " + namespaces.join(', '),
label: "Selected namespaces: " + namespaces.join(', '),
tooltip: "You can change this with your Twinkle preferences, at [[WP:TWPREFS]]"
tooltip: "You can change this with your Twinkle preferences, at [[SV:TWPREFS]]"
});
});
if ($(xmlDoc).find('query-continue').length) {
if ($(xmlDoc).find('query-continue').length) {
Line 235: Line 220:
type: 'div',
type: 'div',
label: "Selected namespaces: " + namespaces.join(', '),
label: "Selected namespaces: " + namespaces.join(', '),
tooltip: "You can change this with your Twinkle preferences, at [[WP:TWPREFS]]"
tooltip: "You can change this with your Twinkle preferences, at [[SV:TWPREFS]]"
});
});
if ($(xmlDoc).find('query-continue').length) {
if ($(xmlDoc).find('query-continue').length) {
Line 282: Line 267:
},
},
unlinkBacklinks: function twinkleunlinkCallbackUnlinkBacklinks(pageobj) {
unlinkBacklinks: function twinkleunlinkCallbackUnlinkBacklinks(pageobj) {
var text, oldtext;
var oldtext = pageobj.getPageText();
text = oldtext = pageobj.getPageText();
var params = pageobj.getCallbackParameters();
var params = pageobj.getCallbackParameters();
var wikiPage = new Morebits.wikitext.page(oldtext);
var summaryText = "", warningString = false;
var text;


var wikiPage = new Morebits.wikitext.page(text);
// remove image usages
wikiPage.removeLink(Morebits.pageNameNorm);
if (params.doImageusage) {
text = wikiPage.getText();
wikiPage.commentOutImage(mw.config.get('wgTitle'), 'Commented out');
if (text === oldtext) {
text = wikiPage.getText();
// Nothing to do, return
// did we actually make any changes?
Twinkle.unlink.callbacks.success(pageobj);
if (text === oldtext) {
Morebits.wiki.actionCompleted();
warningString = "file usages";
return;
} else {
summaryText = "Commenting out use(s) of file";
oldtext = text;
}
}
}


pageobj.setPageText(text);
// remove backlinks
pageobj.setEditSummary("Removing link(s) to \"" + Morebits.pageNameNorm + "\": " + params.reason + "." + Twinkle.getPref('summaryAd'));
if (params.doBacklinks) {
pageobj.setCreateOption('nocreate');
wikiPage.removeLink(Morebits.pageNameNorm);
pageobj.save(Twinkle.unlink.callbacks.success);
text = wikiPage.getText();
},
// did we actually make any changes?
unlinkImageInstances: function twinkleunlinkCallbackUnlinkImageInstances(pageobj) {
if (text === oldtext) {
var text, oldtext;
warningString = (warningString ? "backlinks or file usages" : "backlinks");
text = oldtext = pageobj.getPageText();
} else {
var params = pageobj.getCallbackParameters();
summaryText = (summaryText ? (summaryText + " / ") : "") + "Removing link(s) to";
oldtext = text;
}
}


var wikiPage = new Morebits.wikitext.page(text);
if (warningString) {
wikiPage.commentOutImage(mw.config.get('wgTitle'), 'Commented out');
// nothing to do!
text = wikiPage.getText();
pageobj.getStatusElement().error("Didn't find any " + warningString + " on the page.");
if (text === oldtext) {
params.unlinker.workerFailure(pageobj);
// Nothing to do, return
Twinkle.unlink.callbacks.success(pageobj);
Morebits.wiki.actionCompleted();
return;
return;
}
}


pageobj.setPageText(text);
pageobj.setPageText(text);
pageobj.setEditSummary("Commenting out use(s) of file \"" + Morebits.pageNameNorm + "\": " + params.reason + "." + Twinkle.getPref('summaryAd'));
pageobj.setEditSummary(summaryText + " \"" + Morebits.pageNameNorm + "\": " + params.reason + "." + Twinkle.getPref('summaryAd'));
pageobj.setCreateOption('nocreate');
pageobj.setCreateOption('nocreate');
pageobj.save(Twinkle.unlink.callbacks.success);
pageobj.save(params.unlinker.workerSuccess, params.unlinker.workerFailure);
},
success: function twinkleunlinkCallbackSuccess(pageobj) {
var params = pageobj.getCallbackParameters();
var total = params.total;
var now = parseInt( 100 * (params.imageusage ? ++(Twinkle.unlink.imageusagedone) : ++(Twinkle.unlink.backlinksdone))/total, 10 ) + '%';
params.globalstatus.update( now );
if((params.imageusage ? Twinkle.unlink.imageusagedone : Twinkle.unlink.backlinksdone) >= total) {
params.globalstatus.info( now + ' (completed)' );
Morebits.wiki.removeCheckpoint();
}
}
}
};
};
14,061

edits