Skip to content

Commit

Permalink
protect: prompt if the user has asked the protecting admin about the …
Browse files Browse the repository at this point in the history
…protection when requesting unprotection

Prevent tracking down logs of circular page moves, some refactoring

Record if there was a stable log even if it's not currently pc protected
  • Loading branch information
MusikAnimal committed Apr 18, 2019
1 parent eba1580 commit 0fe4e21
Showing 1 changed file with 66 additions and 7 deletions.
73 changes: 66 additions & 7 deletions modules/twinkleprotect.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,31 @@ Twinkle.protect.callback = function twinkleprotectCallback() {
// { edit: { level: "sysop", expiry: <some date>, cascade: true }, ... }
Twinkle.protect.currentProtectionLevels = {};

// returns a jQuery Deferred object, usage:
// Twinkle.protect.fetchProtectingAdmin(apiObject, pageName).done(function(admin_username) { ...code... });
Twinkle.protect.fetchProtectingAdmin = function twinkleprotectFetchProtectingAdmin(api, pageName, logIds) {
logIds = logIds || [];

return api.get({
format: 'json',
action: 'query',
list: 'logevents',
letitle: pageName,
letype: 'protect'
}).then(function( data ) {
// don't check log entries that have already been checked (e.g. don't go into an infinite loop!)
var event = data.query ? $.grep(data.query.logevents, function(le) { return $.inArray(le.logid, logIds); })[0] : null;
if (!event) {
// fail gracefully
return null;
} else if (event.action === "move_prot") {
return twinkleprotectFetchProtectingAdmin( api, event.params.oldtitle_title, logIds.concat(event.logid) );
} else {
return event.user;
}
});
};

Twinkle.protect.fetchProtectionLevel = function twinkleprotectFetchProtectionLevel() {

var api = new mw.Api();
Expand All @@ -123,7 +148,7 @@ Twinkle.protect.fetchProtectionLevel = function twinkleprotectFetchProtectionLev
$.when.apply($, [protectDeferred, stableDeferred]).done(function(protectData, stableData){
var pageid = protectData[0].query.pageids[0];
var page = protectData[0].query.pages[pageid];
var current = {};
var current = {}, adminEditDeferred;

$.each(page.protection, function( index, protection ) {
if (protection.type !== "aft") {
Expand All @@ -132,21 +157,42 @@ Twinkle.protect.fetchProtectionLevel = function twinkleprotectFetchProtectionLev
expiry: protection.expiry,
cascade: protection.cascade === ''
};
// logs report last admin who made changes to either edit/move/create protection, regardless if they only modified one of them
if (!adminEditDeferred) {
adminEditDeferred = Twinkle.protect.fetchProtectingAdmin(api, mw.config.get('wgPageName'));
}
}
});

Twinkle.protect.hasStableLog = !!stableData[0].query.logevents.length;

if (page.flagged) {
// note that stable settings aren't logged when page is moved, so we don't need to use fetchProtectingAdmin
current.stabilize = {
level: page.flagged.protection_level,
expiry: page.flagged.protection_expiry
expiry: page.flagged.protection_expiry,
admin: Twinkle.protect.hasStableLog ? stableData[0].query.logevents[0].user : null
};
}

// show the protection level and log info
Twinkle.protect.hasProtectLog = !!protectData[0].query.logevents.length;
Twinkle.protect.hasStableLog = !!stableData[0].query.logevents.length;
Twinkle.protect.currentProtectionLevels = current;
Twinkle.protect.callback.showLogAndCurrentProtectInfo();

if (adminEditDeferred) {
adminEditDeferred.done(function(admin) {
if (admin) {
$.each(['edit', 'move', 'create'], function(i, type) {
if (Twinkle.protect.currentProtectionLevels[type]) {
Twinkle.protect.currentProtectionLevels[type].admin = admin;
}
});
}
Twinkle.protect.callback.showLogAndCurrentProtectInfo();
});
} else {
Twinkle.protect.callback.showLogAndCurrentProtectInfo();
}
});
};

Expand All @@ -156,13 +202,16 @@ Twinkle.protect.callback.showLogAndCurrentProtectInfo = function twinkleprotectC
if (Twinkle.protect.hasProtectLog || Twinkle.protect.hasStableLog) {
var $linkMarkup = $("<span>");

if (Twinkle.protect.hasProtectLog)
if (Twinkle.protect.hasProtectLog) {
$linkMarkup.append(
$( '<a target="_blank" href="' + mw.util.getUrl('Special:Log', {action: 'view', page: mw.config.get('wgPageName'), type: 'protect'}) + '">protection log</a>' ),
Twinkle.protect.hasStableLog ? $("<span> &bull; </span>") : null
);
if (Twinkle.protect.hasStableLog)
}

if (Twinkle.protect.hasStableLog) {
$linkMarkup.append($( '<a target="_blank" href="' + mw.util.getUrl('Special:Log', {action: 'view', page: mw.config.get('wgPageName'), type: 'stable'}) + '">pending changes log</a>)' ));
}

Morebits.status.init($('div[name="hasprotectlog"] span')[0]);
Morebits.status.warn(
Expand All @@ -186,7 +235,13 @@ Twinkle.protect.callback.showLogAndCurrentProtectInfo = function twinkleprotectC
if (settings.cascade) {
protectionNode.push("(cascading) ");
}
if (settings.admin) {
var adminLink = '<a target="_blank" href="' + mw.util.getUrl('User talk:' + settings.admin) + '">' + settings.admin + '</a>';
protectionNode.push($("<span>by " + adminLink + "&nbsp;</span>")[0]);
}
protectionNode.push($("<span> \u2022 </span>")[0]);
});
protectionNode = protectionNode.slice(0, -1); // remove the trailing bullet
statusLevel = 'warn';
} else {
protectionNode.push($("<b>no protection</b>")[0]);
Expand Down Expand Up @@ -1159,7 +1214,11 @@ Twinkle.protect.callback.evaluate = function twinkleprotectCallbackEvaluate(e) {
typename = 'create protection';
break;
case 'unprotect':
/* falls through */
var admins = $.map(Twinkle.protect.currentProtectionLevels, function(pl) { return pl.admin ? 'User:' + pl.admin : null; });
if (admins.length && !confirm('Have you attempted to contact the protecting admins (' + $.unique(admins).join(', ') + ') first?' )) {
return false;
}
// otherwise falls through
default:
typename = 'unprotection';
break;
Expand Down

0 comments on commit 0fe4e21

Please sign in to comment.