Diferencia entre revisiones de «MediaWiki:Gadget-tag-status-change.js»
m (guard combo diffs from tagging wrong revision) |
(Don't tag outside of articles and only tag if the old version also has a status tag and it is a different status tag) |
||
(No se muestran 2 ediciones intermedias de otro usuario) | |||
Línea 1: | Línea 1: | ||
mw.hook('wikipage.diff').add(function () { |
mw.hook('wikipage.diff').add(function tag_status_change() { |
||
// 2018-11-06 add status-change tags to pages with diffs that change the status |
// 2018-11-06 add status-change tags to pages with diffs that change the status |
||
const diff = document.querySelector('.diff') |
const diff = document.querySelector('.diff'); |
||
if (!diff) return; |
if (!diff) return; |
||
// exclude pages outside ns 0 |
|||
⚫ | |||
if (mw.config.get('wgNamespaceNumber') != 0) return; |
|||
⚫ | |||
⚫ | |||
const status_tags = ['[sS]tub', '[pP]artial', '[cC]omplete']; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
// FIXME: the above selectors do not work in newer diffs |
|||
if (!rem || !add) return; |
|||
var other_tags = []; |
|||
var i; |
|||
for (i=0; i<status_tags.length; i+=1) { |
|||
var st = new RegExp('\{\{' + status_tags[i] + '\}\}'); |
|||
if (!st.test(rem.innerText)) { |
|||
other_tags.push(status_tags[i]); |
|||
} |
|||
} |
|||
if (other_tags.length == status_tags.length) return; |
|||
other_tags = other_tags.toString().replaceAll(',', '|'); |
|||
var ot = new RegExp('\{\{(' + other_tags + ')\}\}'); |
|||
⚫ | |||
⚫ | |||
// guard against tagging latest revision in multi-change diffs |
// guard against tagging latest revision in multi-change diffs |
||
if (diff.querySelector('.diff-multi')) { |
if (diff.querySelector('.diff-multi')) { |
||
// TODO: determine which of the revisions to tag? |
// TODO: determine which of the revisions to tag? |
||
console.warn('gadget:', 'tag-status-change', 'ignored on combination diff') |
console.warn('gadget:', 'tag-status-change', 'ignored on combination diff'); |
||
mw.notify('theses revisions contain a potential status-change ('+ msg +') which has been ignored') |
mw.notify('theses revisions contain a potential status-change ('+ msg +') which has been ignored'); |
||
return; |
return; |
||
} |
} |
||
Línea 23: | Línea 44: | ||
// send the notification |
// send the notification |
||
;(new mw.Api).postWithToken('csrf', |
;(new mw.Api).postWithToken('csrf', { |
||
action: 'tag', |
|||
⚫ | |||
revid: mw.config.get('wgRevisionId'), |
|||
add: 'status-change', |
|||
reason: msg, |
|||
}).then(function notify_success() { |
|||
⚫ | |||
}) |
}) |
||
Revisión actual - 20:31 7 dic 2023
mw.hook('wikipage.diff').add(function tag_status_change() { // 2018-11-06 add status-change tags to pages with diffs that change the status const diff = document.querySelector('.diff'); if (!diff) return; // exclude pages outside ns 0 if (mw.config.get('wgNamespaceNumber') != 0) return; const status_tags = ['[sS]tub', '[pP]artial', '[cC]omplete']; diff.querySelectorAll('.diff-deletedline').forEach(function find_status_change(el) { const row = el.parentElement; const rem = row.querySelector('.diff-deletedline'); const add = row.querySelector('.diff-addedline'); // FIXME: the above selectors do not work in newer diffs if (!rem || !add) return; var other_tags = []; var i; for (i=0; i<status_tags.length; i+=1) { var st = new RegExp('\{\{' + status_tags[i] + '\}\}'); if (!st.test(rem.innerText)) { other_tags.push(status_tags[i]); } } if (other_tags.length == status_tags.length) return; other_tags = other_tags.toString().replaceAll(',', '|'); var ot = new RegExp('\{\{(' + other_tags + ')\}\}'); if (!ot.test(add.innerText)) return; const msg = rem.innerText.trim() +' => '+ add.innerText.trim(); // guard against tagging latest revision in multi-change diffs if (diff.querySelector('.diff-multi')) { // TODO: determine which of the revisions to tag? console.warn('gadget:', 'tag-status-change', 'ignored on combination diff'); mw.notify('theses revisions contain a potential status-change ('+ msg +') which has been ignored'); return; } console.warn('gadget:', 'tag-status-change') // send the notification ;(new mw.Api).postWithToken('csrf', { action: 'tag', revid: mw.config.get('wgRevisionId'), add: 'status-change', reason: msg, }).then(function notify_success() { mw.notify('revision tagged with '+ msg); }) }) })