MediaWiki:Common.js/User:Bawolff/sandbox/powerFlag

From Wikinews, the free news source you can write!
Jump to navigation Jump to search

//

/*global Bawolff, addOnloadHook, $j*/
/*jslint  browser: true, eqeqeq: true, immed: true, newcap: true, nomen: true, plusplus: false, regexp: true, undef: true, white: true, indent: 1 */

mw.loader.load( '//en.wikinews.org/w/extensions/UsabilityInitiative/js/js2.combined.min.js?6' );
mw.loader.load( '/w/index.php?title=' + 'User:Bawolff/mwapilib.js' + '&action=raw&ctype=text/javascript' );

var powerFlag = {};
powerFlag.outgoingReq = 0;

////en.wikinews.org/w/api.php?action=query&prop=revisions&titles=Main%20Page&rvprop=ids|flagged|user|comment&rvlimit=500
/*var req = new Bawolff.mwapi.Request({action: "query", rawcontinue: "", prop: "revisions", titles: "Main Page", rvprop: "ids|flagged|user", rvlimit: "10"});*/

powerFlag.start = function () {
 document.getElementById('powerFlag').innerHTML = '<form onsubmit="powerFlag.submit(); return false" id="powerFlagForm" action="javascript:void 0"><label for="articleName">What article: </label><input value="Main Page" id="articleName" name="articleName"><input type="submit" value="Go!"></form><div id="powerFlagRevs"></div>';
if (location.search.indexOf('pf-title=') !== -1) {
var title = decodeURIComponent(location.search.replace(/[?&]pf-title=([^?&]*)/, "$1"))
//careful with title, as it is unsafe.
document.getElementById('articleName').value = title;
powerFlag.submit();
}
//give pagename suggestions
os_initHandlers("articleName", "powerFlagForm", document.getElementById('articleName'));
}

powerFlag.submit = function (rvstartid) {
 var titles = document.getElementById('articleName').value;

 var req = new Bawolff.mwapi.Request({action: "query", rawcontinue: "", prop: "revisions", titles: titles, rvprop: "ids|flagged|user|timestamp|comment", rvlimit: "50"});
 //if continued query.
 if (rvstartid) {
  req.setParam("rvstartid", rvstartid);
 }
 req.send(powerFlag.makeTable);

}

/*
<table id="PowerFlagRevTable" >
<caption>Revision information for PageName</caption>
<thead>
<tr><th>Revision Number</th><th>Revision by</th><th>edited at</th><th>Flagged by</th><th>Flagged at</th><th>Current level</th><th>Set level</th></tr>
</thead>
<tbody>
<tr id="revREVID"><td>...

*/

powerFlag.makeTable = function (xmlDoc) {
 var page = xmlDoc.getElementsByTagName('page')[0].getAttribute('title');
 var pageURL = mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=' + encodeURIComponent(page);
 var qcont = xmlDoc.getElementsByTagName('query-continue');
 var nextQ;
 if (qcont.length !== 0) {
  nextQ = qcont[0].firstChild.getAttribute('rvstartid');
 }
 else {
  nextQ = false;
 }
 

 var table = '<form id="FlagTable" action="#" onsubmit="powerFlag.commit(); return false"><table id="PowerFlagRevTable" class="wikitable">';
 table += '<caption>Revision information for <a href="'
 table += pageURL;
 table += '">' + page + '</a></caption>';
 table += '<thead><tr><th>Revision</th><th>Revision by</th><th>Edited at</th><th>Flagged by</th><th>Flagged at</th><th>Current level</th><th>Set level</th></tr></thead>';
 table += '<tbody>';
 //start each row

 var revs = xmlDoc.getElementsByTagName('rev');
 var revID, editor, flagger, editTime, flagTime, level, levelReadable, cur, isFlagged, curf, summary;
 for (var i = 0; i < revs.length; i++) {
  //start getting replacement text
  cur = revs[i];
  editor = cur.getAttribute('user');
  revID = cur.getAttribute('revid');
  editTime = Bawolff.mwapi.parseAPIDate(cur.getAttribute('timestamp')).toUTCString();
  summary = cur.getAttribute('comment');
  curf = cur.firstChild;
  if (curf && curf.tagName === "flagged") {
   isFlagged = true;
   flagTime = Bawolff.mwapi.parseAPIDate(curf.getAttribute('timestamp')).toUTCString();
   levelReadable = curf.getAttribute('level_text');
   flagger = curf.getAttribute('user');
   level = curf.firstChild.getAttribute('accuracy');
   levelReadable += " (" + level + ")";
  }
  else {
   isFlagged = false;
  }
 // start making table row.
  table += "<tr id='rev" + revID + "'><td><a href='" + pageURL + "&oldid=" + revID + "' title='Edit Summary: " + summary + "'>" + revID + "</a></td>";
  table += "<td>" + editor + "</td>";
  table += "<td>" + editTime + "</td>";

  if (isFlagged) {
   table += "<td>" + flagger + "</td>";
   table += "<td>" + flagTime + "</td>";
   table += "<td class='flagLevel setting" + level + "'>" + levelReadable + "</td>";
   table += '<td><select name="new_flag" onchange="powerFlag.onchangeHandle(' + revID + ')" id="selRev' + revID + '">';
   table += '<option value="0">Unflagged (0)</option>';
   table += "<option value='1' " + (level === "1" ? "selected" : "" ) + " >Sighted (1)</option>";
   table += "<option value='2' " + (level === "2" ? "selected" : "" ) + " >Quality (2)</option>";
   table += "</select></td></tr>";
  }
  else {
   table += "<td>N/A</td><td>Never</td><td class='flagLevel setting0'>Unflagged (0)</td>";
   table += '<td><select name="new_flag" onchange="powerFlag.onchangeHandle(' + revID + ')" id="selRev' + revID + '">';
   table += '<option value="0" selected>Unflagged (0)</option>';
   table += "<option value='1'>Sighted (1)</option>";
   table += "<option value='2'>Quality (2)</option>";
   table += "</select></td></tr>";
  }

 }
 table += "</tbody></table>";
 table += '<label for="FlagComment">Flag Changes Summary: </label><input size="50" id="FlagComment" name="FlagComment" value="Re-flagged using [[User:Bawolff/sandbox/powerFlag]]" />';
 table += ' <input type="submit" value="Commit changes"/></form>';

 if (nextQ) { //this could break if user changes page.
 table += "<br/><a href=\"javascript:powerFlag.submit('" + nextQ + "');void 0;\" >Next 50 &gt;&gt;</a>";
 }
 //add to page
 document.getElementById('powerFlagRevs').innerHTML = table;

}


powerFlag.onchangeHandle = function (rev) {
//var rev = this.id.replace(/^selRev(\d+)$/, "#rev$1");
 if (rev === "") throw new Error("couldn't find revision number from onchange handler");

 rev = '#rev' + rev;

 $j(rev + ' select')[0].className = 'modifiedFlag';
 $j(rev)[0].className = 'modifiedFlag';
 $j(rev + ' td').css('background-color',  '#ccddff');
}

powerFlag.commit = function () {
 //$j('select.modifiedFlag')[2].value
 var comment = $j('#FlagComment')[0].value;
 var selects = $j('select.modifiedFlag');
 var rev, level;
 var callback = function() {
  powerFlag.outgoingReq--;
  if (powerFlag.outgoingReq === 0) {
   jsMsg("<big>All Requests completed successfully. The specific revisions should be flagged as appropriate now</big>");
   powerFlag.submit(); //refresh table.
  }
 }
 for (var i = 0; i < selects.length; i++) {
  level = selects[i].value;
  rev = selects[i].id.substring(6);
  //FIXME, handle errors.
  powerFlag.outgoingReq++;
  Bawolff.mwapi.sight({comment: comment, level: level, revid: rev}, callback);  
 }
  
}

addOnloadHook(powerFlag.start);
//