Diferencia entre revisiones de «MediaWiki:Gadget-tag-status-change.js»

De La Coppermind
Ir a la navegación Ir a la búsqueda
m (+fn names)
(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 muestra una edición intermedia de otro usuario)
Línea 2: Línea 2:
   
 
// 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
diff.querySelectorAll('.diff-addedline').forEach(function find_status_change(el) {
 
  +
if (mw.config.get('wgNamespaceNumber') != 0) return;
if (!/\{\{(stub|partial|complete)\}\}/.test(el.innerText)) return
 
  +
const row = el.parentElement
 
  +
const status_tags = ['[sS]tub', '[pP]artial', '[cC]omplete'];
const rem = row.querySelector('.diff-deletedline')
 
  +
const add = row.querySelector('.diff-addedline')
 
 
diff.querySelectorAll('.diff-deletedline').forEach(function find_status_change(el) {
const msg = rem.innerText.trim() +' => '+ add.innerText.trim()
 
 
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
 
// 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 29: Línea 50:
 
reason: msg,
 
reason: msg,
 
}).then(function notify_success() {
 
}).then(function notify_success() {
mw.notify('revision tagged with '+ msg)
+
mw.notify('revision tagged with '+ msg);
 
})
 
})
   

Revisión actual del 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);
})


})

})