Skip to content

Commit

Permalink
warn: Hack to restore user talk link after rollback
Browse files Browse the repository at this point in the history
The old, static-page rollback system was replaced by a AJAX version
which no longer displays the confirmation page. Instead, a mw.notify
popup is shown if the rollback is successful.

Unfortunately, there is no good way to hook into the rollback success
logic, so I had to come up with this awful hack. It replaces the
mw.notify function with an override, which adds a link to the talk page
of the user whose edits are being reverted when it detects that a
rollback success message is being displayed.

Hopefully we won't need to keep this around for too long...

See Phabricator tasks T88044 and T136375 for more info.

Also a minor punctuation fix.
  • Loading branch information
atlight committed May 27, 2016
1 parent c98d08c commit f53706a
Showing 1 changed file with 65 additions and 12 deletions.
77 changes: 65 additions & 12 deletions modules/twinklewarn.js
Expand Up @@ -18,22 +18,75 @@ Twinkle.warn = function twinklewarn() {
Twinkle.addPortletLink( Twinkle.warn.callback, "Warn", "tw-warn", "Warn/notify user" );
}

// modify URL of talk page on rollback success pages
// Modify URL of talk page on rollback success pages. This is only used
// when a user Ctrl+clicks on a rollback link.
if( mw.config.get('wgAction') === 'rollback' ) {
var $vandalTalkLink = $("#mw-rollback-success").find(".mw-usertoollinks a").first();
if ( $vandalTalkLink.length ) {
Twinkle.warn.makeVandalTalkLink($vandalTalkLink);
$vandalTalkLink.css("font-weight", "bold");
$vandalTalkLink.wrapInner($("<span/>").attr("title", "If appropriate, you can use Twinkle to warn the user about their edits to this page."));

var extraParam = "vanarticle=" + mw.util.rawurlencode(Morebits.pageNameNorm);
var href = $vandalTalkLink.attr("href");
if (href.indexOf("?") === -1) {
$vandalTalkLink.attr("href", href + "?" + extraParam);
} else {
$vandalTalkLink.attr("href", href + "&" + extraParam);
}
}
}

// Override the mw.notify function to allow us to inject a link into the
// rollback success popup. Only users with the 'rollback' right need this,
// but we have no nice way of knowing who has that right (what with global
// groups and the like)
else if( mw.config.get('wgAction') === 'history' ) {
mw.notifyOriginal = mw.notify;
mw.notify = function mwNotifyTwinkleOverride(message, options) {
// This is a horrible, awful hack to add a link to the rollback success
// popup. All other notification popups should be left untouched.
// It won't work for people whose user language is not English.
// As it's a hack, it's liable to stop working or break sometimes,
// particularly if the text or format of the confirmation message
// (MediaWiki:Rollback-success-notify) changes.
var regexMatch;
if ( options && options.title && mw.msg && options.title === mw.msg('actioncomplete') &&
message && $.isArray(message) && message[0] instanceof HTMLParagraphElement &&
(regexMatch = /^Reverted edits by (.+);\s+changed/.exec(message[0].innerText))
) {
// Create a nicely-styled paragraph to place the link in
var $p = $('<p/>');
$p.css("margin", "0.5em -1.5em -1.5em");
$p.css("padding", "0.5em 1.5em 0.8em");
$p.css("border-top", "1px #666 solid");
$p.css("cursor", "default");
$p.click(function(e) { e.stopPropagation(); });

// Create the new talk link and append it to the end of the message
var $vandalTalkLink = $('<a/>');
$vandalTalkLink.text("Warn user with Twinkle");
//$vandalTalkLink.css("display", "block");
$vandalTalkLink.attr("href", mw.util.getUrl("User talk:" + regexMatch[1]));
Twinkle.warn.makeVandalTalkLink($vandalTalkLink);

$p.append($vandalTalkLink);
message[0].appendChild($p.get()[0]);

// Don't auto-hide the notification. It only stays around for 5 seconds by
// default, which might not be enough time for the user to read it and
// click the link
options.autoHide = false;
}
mw.notifyOriginal.apply(mw, arguments);
};
}

// for testing, use:
// mw.notify([ $("<p>Reverted edits by foo; changed</p>")[0] ], { title: mw.msg('actioncomplete') } );
};

Twinkle.warn.makeVandalTalkLink = function($vandalTalkLink) {
$vandalTalkLink.wrapInner($("<span/>").attr("title", "If appropriate, you can use Twinkle to warn the user about their edits to this page."));

var extraParam = "vanarticle=" + mw.util.rawurlencode(Morebits.pageNameNorm);
var href = $vandalTalkLink.attr("href");
if (href.indexOf("?") === -1) {
$vandalTalkLink.attr("href", href + "?" + extraParam);
} else {
$vandalTalkLink.attr("href", href + "&" + extraParam);
}
};

Twinkle.warn.callback = function twinklewarnCallback() {
Expand Down Expand Up @@ -67,8 +120,8 @@ Twinkle.warn.callback = function twinklewarnCallback() {
main_group.append( { type: 'option', label: 'Warning (3)', value: 'level3', selected: ( defaultGroup === 3 ) } );
main_group.append( { type: 'option', label: 'Final warning (4)', value: 'level4', selected: ( defaultGroup === 4 ) } );
main_group.append( { type: 'option', label: 'Only warning (4im)', value: 'level4im', selected: ( defaultGroup === 5 ) } );
main_group.append( { type: 'option', label: 'Single issue notices', value: 'singlenotice', selected: ( defaultGroup === 6 ) } );
main_group.append( { type: 'option', label: 'Single issue warnings', value: 'singlewarn', selected: ( defaultGroup === 7 ) } );
main_group.append( { type: 'option', label: 'Single-issue notices', value: 'singlenotice', selected: ( defaultGroup === 6 ) } );
main_group.append( { type: 'option', label: 'Single-issue warnings', value: 'singlewarn', selected: ( defaultGroup === 7 ) } );
if( Twinkle.getPref( 'customWarningList' ).length ) {
main_group.append( { type: 'option', label: 'Custom warnings', value: 'custom', selected: ( defaultGroup === 9 ) } );
}
Expand Down

2 comments on commit f53706a

@Amorymeltzer
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@atlight Is this still needed? Just discovering this, and from the VPT archive it seems the issue was pushed back the day after this commit. mwRollback doesn't happen in-place (though some scripts do enable it) with or without twinkle, so I don't think this is necessary?

@atlight
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to say, I have no recollection of writing this at all. Feel free to remove the mw.notify override if it is no longer applicable. Presumably the punctuation fix in the warn module can stay though.

Please sign in to comment.