User:BZPN/MassRollback2.js: Difference between revisions

From Test Wiki
Content deleted Content added
BZPN (talk | contribs)
No edit summary
BZPN (talk | contribs)
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 529: Line 529:


try {
try {
await self.performRollback([$(this).data('mr-edit')]);
const edit = $(this).data('mr-edit');
// If username missing from config, try from RC line extraction
if (!mw.config.get('wgRelevantUserName') && edit.user) {
mw.config.set('wgRelevantUserName', edit.user);
}
await self.performRollback([edit]);
mw.notify('Successfully rolled back 1 edit.', { type: 'success' });
mw.notify('Successfully rolled back 1 edit.', { type: 'success' });
if (confirmResult.leaveWarning) {
if (confirmResult.leaveWarning) {
await self.leaveUserWarning(confirmResult.warningType);
await self.leaveUserWarning(confirmResult.warningType, edit.user || null);
}
}
if (confirmResult.reportVIP) {
if (confirmResult.reportVIP) {
Line 549: Line 554:
const revid = $line.data('mw-revid') || $line.attr('data-mw-revid');
const revid = $line.data('mw-revid') || $line.attr('data-mw-revid');
const parentid = $line.data('mw-rollback-revision') || $line.attr('data-mw-prev-revid') || $line.attr('data-mw-rollback-revision');
const parentid = $line.data('mw-rollback-revision') || $line.attr('data-mw-prev-revid') || $line.attr('data-mw-rollback-revision');
// Try to get page title
let title = $line.find('.mw-title, .mw-changeslist-title, a.mw-changeslist-title').first().text().trim();
let title = $line.find('.mw-title, .mw-changeslist-title, a.mw-changeslist-title').first().text().trim();
if (!title) {
if (!title) {
Line 554: Line 560:
title = ($plink.attr('title') || '').trim();
title = ($plink.attr('title') || '').trim();
}
}
// Try to get username (author of the edit) from the RC line
let user = '';
// Prefer the direct user page link, not talk/contribs
const $userAnchor = $line.find('a.mw-userlink[href*="/wiki/User:"]:not([href*="User_talk"]):not([href*="Special:Contributions"])').first();
if ($userAnchor.length) {
user = ($userAnchor.find('bdi').text() || $userAnchor.text() || '').trim();
}
// Fallback: from talk link href
if (!user) {
const $talk = $line.find('a[href*="/wiki/User_talk:"]').first();
if ($talk.length) {
const href = $talk.attr('href');
const m = href && href.match(/User_talk:([^?#]+)/);
if (m) user = decodeURIComponent(m[1]);
}
}
// Fallback: from Special:Contributions link href
if (!user) {
const $contribs = $line.find('a[href*="Special:Contributions/"]').first();
if ($contribs.length) {
const href = $contribs.attr('href');
const m = href && href.match(/Special:Contributions\/([^?#]+)/);
if (m) user = decodeURIComponent(m[1]);
}
}
user = user || null;
if (revid && parentid && title) {
if (revid && parentid && title) {
return { title: title, revid: revid, parentid: parentid, timestamp: new Date().toISOString() };
return { title: title, revid: revid, parentid: parentid, timestamp: new Date().toISOString(), user: user || null };
}
}
// Fallback from diff link
const $diff = $line.find('a[href*="diff="][href*="oldid="]').first();
const $diff = $line.find('a[href*="diff="][href*="oldid="]').first();
if ($diff.length) {
if ($diff.length) {
Line 563: Line 596:
const pid = params.oldid || params.prev || '';
const pid = params.oldid || params.prev || '';
if (!title) title = ($diff.attr('title') || '').replace(/^Special:Diff\/\d+/, '').trim();
if (!title) title = ($diff.attr('title') || '').replace(/^Special:Diff\/\d+/, '').trim();
if (rv && title) return { title: title, revid: rv, parentid: pid, timestamp: new Date().toISOString() };
if (rv && title) return { title: title, revid: rv, parentid: pid, timestamp: new Date().toISOString(), user: user || null };
}
}
return null;
return null;
Line 929: Line 962:
// Nowa wersja: grupujemy edycje wg tytułu i dla każdej grupy wykrywamy ciąg kolejnych edycji.
// Nowa wersja: grupujemy edycje wg tytułu i dla każdej grupy wykrywamy ciąg kolejnych edycji.
performRollback: function(contributions) {
performRollback: function(contributions) {
const userName = mw.config.get('wgRelevantUserName');
// Ensure userName is available (RC may not set wgRelevantUserName)
let userName = mw.config.get('wgRelevantUserName');
if (!userName && contributions && contributions.length && contributions[0].user) {
userName = contributions[0].user;
}
const reason = $('#rollback-reason').val() === 'other'
const reason = $('#rollback-reason').val() === 'other'
? $('#custom-reason').val()
? $('#custom-reason').val()
: $('#rollback-reason option:selected').text();
: $('#rollback-reason option:selected').text();
const summary = `[[Help:Revert a page|Reverted]] edit(s) by [[User:${userName}|${userName}]]: ${reason} ([[User:BZPN/MassRollback|MR]])`;
const summary = userName
? `[[Help:Revert a page|Reverted]] edit(s) by [[User:${userName}|${userName}]]: ${reason} ([[User:BZPN/MassRollback|MR]])`
: `[[Help:Revert a page|Reverted]] edit(s): ${reason} ([[User:BZPN/MassRollback|MR]])`;
const api = new mw.Api();
const api = new mw.Api();

// If username still not available, fallback to extracting from contributions array
if (!userName) {
const fromList = (contributions || []).map(c => c.user).filter(Boolean)[0];
if (fromList) userName = fromList;
}


// Group edits by page title
// Group edits by page title
Line 987: Line 1,032:
mw.notify(`Successfully rolled back ${selected.length} edits.`, { type: 'success' });
mw.notify(`Successfully rolled back ${selected.length} edits.`, { type: 'success' });
if (confirmResult.leaveWarning) {
if (confirmResult.leaveWarning) {
const targetUser = (selected[0] && selected[0].user) ? selected[0].user : null;
await this.leaveUserWarning(confirmResult.warningType);
await this.leaveUserWarning(confirmResult.warningType, targetUser);
}
}
if (confirmResult.reportVIP) {
if (confirmResult.reportVIP) {
Line 1,019: Line 1,065:
mw.notify(`Successfully rolled back ${selected.length} filtered edits.`, { type: 'success' });
mw.notify(`Successfully rolled back ${selected.length} filtered edits.`, { type: 'success' });
if (confirmResult.leaveWarning) {
if (confirmResult.leaveWarning) {
const targetUser = (selected[0] && selected[0].user) ? selected[0].user : null;
await this.leaveUserWarning(confirmResult.warningType);
await this.leaveUserWarning(confirmResult.warningType, targetUser);
}
}
if (confirmResult.reportVIP) {
if (confirmResult.reportVIP) {
Line 1,053: Line 1,100:
mw.notify(`Successfully rolled back all ${contributions.length} edits.`, { type: 'success' });
mw.notify(`Successfully rolled back all ${contributions.length} edits.`, { type: 'success' });
if (confirmResult.leaveWarning) {
if (confirmResult.leaveWarning) {
const targetUser = (contributions[0] && contributions[0].user) ? contributions[0].user : null;
await this.leaveUserWarning(confirmResult.warningType);
await this.leaveUserWarning(confirmResult.warningType, targetUser);
}
}
if (confirmResult.reportVIP) {
if (confirmResult.reportVIP) {
Line 1,128: Line 1,176:


// Leave a talk page warning using a chosen template (subst) under current month section
// Leave a talk page warning using a chosen template (subst) under current month section
leaveUserWarning: async function(templateName) {
leaveUserWarning: async function(templateName, targetUser) {
const api = new mw.Api();
const api = new mw.Api();
const userName = mw.config.get('wgRelevantUserName');
const userName = targetUser || mw.config.get('wgRelevantUserName');
if (!userName) {
mw.notify('Could not determine user name for talk page warning.', { type: 'error' });
return;
}
const title = `User talk:${userName}`;
const title = `User talk:${userName}`;