User:Bawolff/DYKVerified.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/******
To use, add the following line to [[special:mypage/monobook.js]] (or [[special:mypage/vector.js]] depending on skin pref)
 importScript('User:Bawolff/DYKVerified.js');

This script looks for the code <span id='DYKUpdateVerified'/> and replaces it with an update button. IF you also want it in the sidebar under the toolbox, add the following line (in addition to the importScript):
 addOnloadHook(function () {addPortletLink('p-tb', 'javascript:DYKUpdateVerified();void%200', 'Update DYKVerified');});

This script scans through [[T:TDYK]], finds are entries that are current candidates that have been either verified or verified AGF (identifying them by the 16px check mark image), and pastes them on to another page (currently [[user:Bawolff/DYKVerified.js]])

As with all my scripts, feel free to send my comments, suggestions, hate mail, etc. Since this is the topic of an active thread at http://en.wikipedia.org/wiki/Wikipedia_talk:Did_you_know#A_more_comprehensive_proposal , comments should go there. Otherwise you can also find me on irc (nick bawolff on #wikinews on irc.freenode.net) or leave a message on my talk page (I check [[wikinews:user talk:Bawolff]] more often then i check my local wikipedia talk page, but i check both quite regurally). 
<source> //Prevent PST
********/


mw.loader.load('http://en.wikinews.org/w/index.php?action=raw&ctype=text/javascript&title=User:Bawolff/mwapilib2.js')

function filterDYKSuggest(text) {
 var lines = text.split('\n');
 var intro = '<noinclude>\n{{ambox|text=This page contains DYK suggestions that have been verified. It is copied from [[T:TDYK]] by a script. If you wish to add a comment to one of these entries, please do so on [[T:TDYK]]. Last updated on {{subst:#time:r|{{subst:REVISIONTIMESTAMP}}}} by {{subst:user|{{subst:REVISIONUSER}}}} <div id="DYKUpdateVerified"/>}}\n<!--\n************************\nPLEASE DO NOT EDIT THIS PAGE. EDIT [[T:TDYK]] INSTEAD.\n Edits to this page will be lost\n\n****************-->\n</noinclude>\n'
 var out = '';
 var mode = 0;
 var h2 = /^==[^=].*[^=]==\s*$/, h3 = /^===[^=].*[^=]===\s*$/, h4 = /^====[^=].*[^=]====\s*$/;
 var verified = /\[\[[FfIi][mMiI][aALl][gGEe][eE]?:Symbol confirmed\.svg\|16px\]\]|\[\[[FfIi][mMiI][aALl][gGEe][eE]?:Pictogram voting keep\.svg\|16px\]\]/;
 var interestingSection = /^==Candidate entries==$|^==Older nominations==$/;
 var buffer = '';
 loop: for (var i = 0; i < lines.length; i++) {
  switch (mode) {
   case 0: //before text
    if (lines[i].match(interestingSection)) {
     //alert('cand');
     //out += lines[i] + '\n';
     mode++;
     continue loop;
    }
   break;
   case 1: //in possibly but not yet interesting part
    if (lines[i].match(h2)) { // next section
     //alert('match h2' + lines[i] + mode);
     if (!lines[i].match(interestingSection)) {
      mode--; //back to start
     }
     out += '\n------\n';
     continue loop;
    }
    if (lines[i].match(h3)) {
     buffer = ''; //reset buffer
     out += lines[i] + '\n';
     continue loop;
    }
    if (lines[i].match(h4)) {
     buffer = lines[i] + '\n';
     continue loop;
    }
    if (lines[i].match(verified)) {
     mode++;
    }
    buffer += lines[i] + '\n';
   break;
   case 2: //in interesting nom.
    if (lines[i].match(h2)) { // next section
     //alert('match h2' + lines[i] + mode);
     out += buffer;
     if (!lines[i].match(interestingSection)) {
      mode--; //back to start
     }
     mode--; //back one. (This is intentional to have double --)
     out += '\n------\n';
     continue loop; //done
    }
    if (lines[i].match(h3)) {
     out += buffer;
     buffer = ''; //reset buffer
     out += lines[i] + '\n';
     mode--;
     continue loop;
    }
    if (lines[i].match(h4)) {
     out += buffer;
     buffer = lines[i] + '\n';
     mode--;
     continue loop;
    }
   buffer += lines[i] + '\n'
   break;
   default:
    break loop;
  }
 }
 return intro + out;
}
function DYKUpdateVerified () {
 var target = 'User:Bawolff/DYKVerified';

 var container = document.getElementById('DYKUpdateVerified');
 if (container) {
  var buttons = container.getElementsByTagName('button');
  if (buttons.length === 1) {
   buttons[0].disabled = true;
  }
 }

 api('template_talk:Did you know').
 getPage().
 lift(filterDYKSuggest).
 setDefaultSummary('Updating Verified DYK (using experimental [[User:Bawolff/DYKVerified.js]])').
 savePage(target).
 lift(function () { 
  if (mw.config.get('wgPageName') === target) {
   location.reload();
  }
  else {
   alert('DYKVerified updated.');
  }
 }).
 exec();
}
addOnloadHook(function () {
var button = document.getElementById('DYKUpdateVerified');

if (button) button.innerHTML = "<button type='button' onclick='DYKUpdateVerified();'>Update verified DYK list</button>";

});

//</source>