User:BZPN/MassRollback2.js: Difference between revisions
From Test Wiki
Content deleted Content added
No edit summary Tag: Tag test |
No edit summary |
||
| Line 14: | Line 14: | ||
createUI: function() { |
createUI: function() { |
||
| ⚫ | |||
const $container = $(` |
const $container = $(` |
||
<div id="mass-rollback-container" style="border: 1px solid #ccc; padding: 15px; margin-bottom: 20px; border-radius: 4px; background: #f9f9f9; display: none;"> |
<div id="mass-rollback-container" style="border: 1px solid #ccc; padding: 15px; margin-bottom: 20px; border-radius: 4px; background: #f9f9f9; display: none;"> |
||
| Line 129: | Line 128: | ||
</div> |
</div> |
||
`); |
`); |
||
| ⚫ | |||
const $toggleButton = $(` |
const $toggleButton = $(` |
||
| Line 139: | Line 137: | ||
$toggleButton.on('click', function() { |
$toggleButton.on('click', function() { |
||
$('#mass-rollback-container').slideToggle(); |
$('#mass-rollback-container').slideToggle(); |
||
| ⚫ | |||
| ⚫ | |||
btn.text(btn.text() === 'Open MassRollback' ? 'Show/Hide MassRollback' : 'Show/Hide MassRollback'); |
|||
}); |
}); |
||
| ⚫ | |||
$('#mw-content-text').prepend($toggleButton).prepend($container); |
$('#mw-content-text').prepend($toggleButton).prepend($container); |
||
| Line 149: | Line 143: | ||
bindEvents: function() { |
bindEvents: function() { |
||
| ⚫ | |||
$('#rollback-reason').on('change', function() { |
$('#rollback-reason').on('change', function() { |
||
$('#custom-reason').toggle($(this).val() === 'other'); |
$('#custom-reason').toggle($(this).val() === 'other'); |
||
| Line 173: | Line 166: | ||
}); |
}); |
||
// Dodaj checkbox przy edycjach w widoku kontrybucji |
|||
$('li[data-mw-revid]').each(function() { |
$('li[data-mw-revid]').each(function() { |
||
const $li = $(this); |
const $li = $(this); |
||
| Line 304: | Line 298: | ||
}); |
}); |
||
} |
} |
||
$('#filtered-edits-container').css("display", "block"); |
$('#filtered-edits-container').css("display", "block").slideDown(); |
||
$('#filtered-edits-container').slideDown(); |
|||
}); |
}); |
||
}, |
}, |
||
| Line 337: | Line 330: | ||
}, |
}, |
||
// 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'); |
const userName = mw.config.get('wgRelevantUserName'); |
||
| Line 342: | Line 336: | ||
? $('#custom-reason').val() |
? $('#custom-reason').val() |
||
: $('#rollback-reason option:selected').text(); |
: $('#rollback-reason option:selected').text(); |
||
| ⚫ | |||
const api = new mw.Api(); |
const api = new mw.Api(); |
||
| ⚫ | |||
// Grupujemy edycje wg tytułu strony |
|||
| ⚫ | |||
const groups = {}; |
|||
| ⚫ | |||
if (!groups[contrib.title]) { |
|||
groups[contrib.title] = []; |
|||
| ⚫ | |||
groups[contrib.title].push(contrib); |
|||
| ⚫ | |||
const promises = []; |
|||
// Dla każdej strony – sortuj wg daty malejąco i wykryj ciąg edycji |
|||
for (let title in groups) { |
|||
let group = groups[title].sort((a, b) => new Date(b.timestamp) - new Date(a.timestamp)); |
|||
let latest = group[0]; |
|||
// Rozpoczynamy ciąg od najnowszej edycji |
|||
let chain = [latest]; |
|||
let currentParent = latest.parentid; |
|||
// Przeglądamy kolejne edycje dla tego samego tytułu |
|||
for (let i = 1; i < group.length; i++) { |
|||
| ⚫ | |||
// Jeżeli kolejna edycja jest dokładnie poprzednikiem poprzednio znalezionego elementu, dodajemy do ciągu |
|||
if (parseInt(contrib.revid, 10) === parseInt(currentParent, 10)) { |
|||
chain.push(contrib); |
|||
currentParent = contrib.parentid; |
|||
| ⚫ | |||
break; |
|||
| ⚫ | |||
| ⚫ | |||
// Wykonaj rollback tylko, jeśli mamy przynajmniej jedną edycję (zawsze prawda) |
|||
promises.push(api.postWithToken('csrf', { |
|||
action: 'edit', |
action: 'edit', |
||
undo: latest.revid, |
|||
undoafter: currentParent, |
|||
title: |
title: title, |
||
summary: summary |
summary: summary |
||
}); |
})); |
||
} |
} |
||
return Promise.all(promises); |
return Promise.all(promises); |
||
}, |
}, |
||
rollbackSelected: async function() { |
rollbackSelected: async function() { |
||
// Pobieramy zaznaczone edycje z listy kontrybucji |
|||
const selected = $('.rollback-checkbox:checked').map(function() { |
const selected = $('.rollback-checkbox:checked').map(function() { |
||
return { |
return { |
||
title: $(this).data('title'), |
title: $(this).data('title'), |
||
revid: $(this).data('revid'), |
revid: $(this).data('revid'), |
||
parentid: $(this).data('parentid') |
parentid: $(this).data('parentid'), |
||
// Wartość timestamp może nie być dostępna w tym widoku – można opcjonalnie dodać dodatkowy pobór danych |
|||
timestamp: $(this).closest('li').find('.mw-contributions-timestamp').text() || new Date().toISOString() |
|||
}; |
}; |
||
}).get(); |
}).get(); |
||
| Line 386: | Line 412: | ||
title: $(this).data('title'), |
title: $(this).data('title'), |
||
revid: $(this).data('revid'), |
revid: $(this).data('revid'), |
||
parentid: $(this).data('parentid') |
parentid: $(this).data('parentid'), |
||
timestamp: new Date().toISOString() // Jeśli API już zwraca timestamp, można go tutaj użyć |
|||
}; |
}; |
||
}).get(); |
}).get(); |
||
| Line 410: | Line 437: | ||
const api = new mw.Api(); |
const api = new mw.Api(); |
||
try { |
try { |
||
// Dodajemy "timestamp" by móc grupować edycje |
|||
const data = await api.get({ |
const data = await api.get({ |
||
action: 'query', |
action: 'query', |
||
| Line 415: | Line 443: | ||
ucuser: userName, |
ucuser: userName, |
||
uclimit: 'max', |
uclimit: 'max', |
||
ucprop: 'ids|title|parentid' |
ucprop: 'ids|title|parentid|timestamp' |
||
}); |
}); |
||
const contributions = data.query.usercontribs; |
const contributions = data.query.usercontribs; |
||