Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix RFPP for normal users #190

Merged
merged 1 commit into from

3 participants

@azatoth
Owner

The current protection data was only read in for sysops

While changing that, removing the old protectionLevel as it was unused
and remove the async nature of the call.

@atlight atlight was assigned
@Amalthea Amalthea was assigned
@atlight atlight was assigned
@atlight
Collaborator

Synchronous requests are evil...

When you posted this I was halfway through my own fix - I'll put it up into a separate branch when I'm finished, to see if there are differences.

@azatoth
Owner

well, the actual request is async:-P only that it continues in the done() callback instead of relying on a timer.

@atlight
Collaborator

I would happily merge this if L133 comment was addressed... although I'm not too sure why you're using mw.Api - you yourself wrote Morebits.wiki.api (aka Wikipedia.api) with its proper error handling :)

Other issue is if the initial API request fails for whatever reason, the whole thing is needlessly doomed, and the user doesn't even get told why (error gets logged to the console, not Morebits.status).

@azatoth
Owner
@atlight
Collaborator

If we're going to make more extensive use of mw.Api I think we would need to write a wrapper, to unify the error handling and integrate it with the Status system.

@azatoth
Owner
@atlight
Collaborator

Could you check this code on testwiki once done? I haven't been able to get it to work... maybe my test copy of morebits is outdated

@azatoth
Owner
@atlight
Collaborator

That's my approach too, but my skeleton Twinkle backbone in https://test.wikipedia.org/wiki/User:This,_that_and_the_other/common.js has to be manually updated from time to time...

@azatoth
Owner
@atlight
Collaborator

Seems fine

@azatoth Fix RFPP for normal users
The current protection data was only read in for sysops
263a383
@azatoth azatoth merged commit 4651c68 into master
@azatoth azatoth deleted the fix_rfpp branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 22, 2013
  1. Fix RFPP for normal users

    authored
    The current protection data was only read in for sysops
This page is out of date. Refresh to see the latest.
Showing with 76 additions and 58 deletions.
  1. +76 −58 modules/twinkleprotect.js
View
134 modules/twinkleprotect.js
@@ -25,6 +25,8 @@ Twinkle.protect = function twinkleprotect() {
};
Twinkle.protect.callback = function twinkleprotectCallback() {
+ Twinkle.protect.protectionLevel = null;
+
var Window = new Morebits.simpleWindow( 620, 530 );
Window.setTitle( Morebits.userIsInGroup( 'sysop' ) ? "Apply, request or tag page protection" : "Request or tag page protection" );
Window.setScriptName( "Twinkle" );
@@ -88,67 +90,84 @@ Twinkle.protect.callback = function twinkleprotectCallback() {
result.actiontype[0].dispatchEvent( evt );
// get current protection level asynchronously
- Morebits.wiki.actionCompleted.postfix = false; // avoid Action: completed notice
if (Morebits.userIsInGroup('sysop')) {
- var query = {
- action: 'query',
- prop: 'info|flagged',
- inprop: 'protection',
- titles: mw.config.get('wgPageName')
- };
+ Morebits.wiki.actionCompleted.postfix = false; // avoid Action: completed notice
Morebits.status.init($('div[name="currentprot"] span').last()[0]);
- var statelem = new Morebits.status("Current protection level");
- var wpapi = new Morebits.wiki.api("retrieving...", query, Twinkle.protect.callback.protectionLevel, statelem);
- wpapi.post();
}
+ Twinkle.protect.fetchProtectionLevel();
};
-Twinkle.protect.protectionLevel = null;
-
-Twinkle.protect.callback.protectionLevel = function twinkleprotectCallbackProtectionLevel(apiobj) {
- var xml = apiobj.getXML();
- var result = [];
- var current = [];
-
- $(xml).find('pr, flagged').each(function(index, protectionEntry) {
- var $protectionEntry = $(protectionEntry);
- var rawtype, type, level, expiry, cascade = false;
-
- if (protectionEntry.tagName.toLowerCase() === "flagged") {
- rawtype = 'stabilize';
- type = "Pending changes";
- level = $protectionEntry.attr('protection_level');
- expiry = $protectionEntry.attr('protection_expiry');
- } else {
- rawtype = $protectionEntry.attr('type');
- type = Morebits.string.toUpperCaseFirstChar($protectionEntry.attr('type'));
- level = $protectionEntry.attr('level');
- expiry = $protectionEntry.attr('expiry');
- cascade = $protectionEntry.attr('cascade') === '';
- }
- current.push({type: rawtype, level: level, expiry: expiry, cascade: cascade});
- var boldnode = document.createElement('b');
- boldnode.textContent = type + ": " + level;
- result.push(boldnode);
- if (expiry === 'infinity') {
- result.push(" (indefinite) ");
- } else {
- result.push(" (expires " + new Date(expiry).toUTCString() + ") ");
- }
- if (cascade) {
- result.push("(cascading) ");
+Twinkle.protect.protectionLevel = null; // a string, or null if no protection (only filled for sysops)
+Twinkle.protect.currentProtectionLevels = null; // an array of objects { type, level, expiry, cascade }
+
+Twinkle.protect.fetchProtectionLevel = function twinkleprotectFetchProtectionLevel() {
+
+ var api = new mw.Api();
+ api.get({
+ format: 'json',
+ indexpageids: true,
+ action: 'query',
+ prop: 'info|flagged',
+ inprop: 'protection',
+ titles: mw.config.get('wgPageName')
+ })
+ .done(function(data){
+ var pageid = data.query.pageids[0];
+ var page = data.query.pages[pageid];
+ var result = [];
+ var current = [];
+
+ var updateResult = function(label, level, expiry, cascade) {
+ // for sysops, stringify, so they can base their decision on existing protection
+ if (Morebits.userIsInGroup('sysop')) {
+ var boldnode = document.createElement('b');
+ boldnode.textContent = label + ": " + level;
+ result.push(boldnode);
+ if (expiry === 'infinity') {
+ result.push(" (indefinite) ");
+ } else {
+ result.push(" (expires " + new Date(expiry).toUTCString() + ") ");
+ }
+ if (cascade) {
+ result.push("(cascading) ");
+ }
+ }
+ };
+
+ $.each(page.protection, function( index, protection ) {
+ current.push({
+ type: protection.type,
+ level: protection.level,
+ expiry: protection.expiry,
+ cascade: protection.cascade && protection.cascade === ''
+ });
+ updateResult( Morebits.string.toUpperCaseFirstChar(protection.type), protection.level, protection.expiry, protection.cascade );
+ });
+
+ if(page.flagged) {
+ current.push({
+ type: 'stabilize',
+ level: page.flagged.protection_level,
+ expiry: page.flagged.protection_expiry
+ });
+ // FlaggedRevision gives bad date
+ updateResult( 'Pending Changes', page.flagged.protection_level, null , false );
+ }
+
+ // show the protection level to sysops
+ if (Morebits.userIsInGroup('sysop')) {
+ if (!result.length) {
+ var boldnode = document.createElement('b');
+ boldnode.textContent = "no protection";
+ result.push(boldnode);
+ }
+ Twinkle.protect.protectionLevel = result;
+ Morebits.status.init($('div[name="currentprot"] span').last()[0]);
+ Morebits.status.info("Current protection level", Twinkle.protect.protectionLevel);
}
- });
- if (!result.length) {
- var boldnode = document.createElement('b');
- boldnode.textContent = "no protection";
- result.push(boldnode);
- }
- Twinkle.protect.protectionLevel = result;
- Twinkle.protect.currentProtectionLevel = current;
- apiobj.statelem.info(result);
- window.setTimeout(function() { Morebits.wiki.actionCompleted.postfix = "completed"; }, 500); // restore actionCompleted message
+ Twinkle.protect.currentProtectionLevels = current;
+ });
};
Twinkle.protect.callback.changeAction = function twinkleprotectCallbackChangeAction(e) {
@@ -540,8 +559,7 @@ Twinkle.protect.callback.changeAction = function twinkleprotectCallbackChangeAct
// re-add protection level text, if it's available
if (Twinkle.protect.protectionLevel) {
Morebits.status.init($('div[name="currentprot"] span').last()[0]);
- // seems unneeded
- //Morebits.status.info("Current protection level", Twinkle.protect.protectionLevel);
+ Morebits.status.info("Current protection level", Twinkle.protect.protectionLevel);
}
// reduce vertical height of dialog
@@ -1301,7 +1319,7 @@ Twinkle.protect.callbacks = {
// else we post it under decrease
var increase = false;
- $.each(Twinkle.protect.currentProtectionLevel, function(i,v){
+ $.each(Twinkle.protect.currentProtectionLevels, function(i,v){
if( Twinkle.protect.protectionWeight[Twinkle.protect.protectionPresetsInfo[params.category][v.type]] >
Twinkle.protect.protectionWeight[v.level] ) {
increase = true;
@@ -1309,7 +1327,7 @@ Twinkle.protect.callbacks = {
}
});
- if ( increase || Twinkle.protect.currentProtectionLevel.length === 0 ) {
+ if ( increase || Twinkle.protect.currentProtectionLevels.length === 0 ) {
reg = /(\n==\s*Current requests for increase in protection level\s*==\s*\n\s*\{\{[^\}\}]+\}\}\s*\n)/;
} else {
reg = /(\n==\s*Current requests for reduction in protection level\s*==\s*\n\s*\{\{[^\}\}]+\}\}\s*\n)/;
Something went wrong with that request. Please try again.