MediaWiki:Gadget-customEditIntro.js

Note: After saving, you may have to bypass your browser's cache to see the changes. Mozilla / Firefox / Safari: hold down Shift while clicking Reload, or press Ctrl-Shift-R (Cmd-Shift-R on Apple Mac); IE: hold Ctrl while clicking Refresh, or press Ctrl-F5; Konqueror: simply click the Reload button, or press F5; Opera users may need to completely clear their cache in Tools→Preferences. — More skins

/*********************

This is now enabled site wide in common.js!
This file is no longer used.

***************************/


/*global addOnloadHook, getElementsByClassName */
/*jslint white: true, browser: true, undef: true, nomen: true, eqeqeq: true, bitwise: true, regexp: true, newcap: true, immed: true, indent: 1 */
/*members April, August, December, February, January, July, June, March, 
    May, November, October, September, UTC, dateFunc, dateRegex, 
    getElementById, getElementsByTagName, getTime, href, indexOf, length, 
    oldNews, parseDate, referrer, replace, title
*/

//This is here until its finished testing, and concensuss is reached to put it on the main js.

function addEditIntro(template) {
//This function assumes that all edit links are in proper order (as in the title arg is first argument), and proper case
//This function appends &editintro=template to any link that edits the CURRENT page.
//template parameter should already be escaped

 //var as = document.getElementsByTagName('a');
 var bodyContent = document.getElementById('bodyContent');
 var list = getElementsByClassName((bodyContent ? bodyContent : document), "span", "editsection");
 list[list.length] = document.getElementById('ca-edit');

 for (var i = 0; i < list.length; i++) {
  var a = list[i].getElementsByTagName('a')[0];
  if (!a) {
   continue; //just in case
  }
  if (a.href.indexOf('&action=edit') === -1) { //if not an edit link
   continue;
  }
  if (a.href.indexOf('?title=' + mw.config.get('wgPageName')) === -1) { //not a link to this page
   continue;
  }
  if (a.href.indexOf('&editintro') !== -1) { //if it already has an editintro don't add another
   continue;
  }
  a.href = a.href + '&editintro=' + template;
 }

}

function doEditIntro() {
//called onload
 try {
  if (mw.config.get('wgNamespaceNumber') !== 0 || mw.config.get('wgPageName') === 'Main_Page') {
   return;
  }
  var notCurrent = doEditIntro.oldNews();
  var fromPedia = (document.referrer.indexOf('http://en.wikipedia.org') === 0);
  if (notCurrent) {
   addEditIntro('Template:Editintro_notcurrent');
  } else if (fromPedia) {
   addEditIntro('Template:Editintro_from_wikipedia');
  }
 }
 catch (e) {
  //in case of different skin and can't find cat links or something.
 }

}

doEditIntro.oldNews = function () {
 var cats = document.getElementById('catlinks').getElementsByTagName('a');
 var pubDate, pubCat, archiveCat, tmp, catName;
 for (var i = 0; i < cats.length; i++) {
  catName = cats[i].title;
  tmp = doEditIntro.parseDate(catName);
  if (!isNaN(tmp)) {
   pubDate = tmp;
  }
  if (catName === "Category:Published") {
   pubCat = true;
  }
  if (catName === "Category:Archived" || catName === "Category:AutoArchived") {
   archiveCat = true;
  }
 } //end getting cats

 if (!pubDate || !pubCat || archiveCat) {
 //not in a date category, not published, or already archived.
  return false;
 }
 if (((new Date()).getTime() - pubDate) > 48 * 60 * 60 * 1000) { //if more than 48h elasped
  return true;
 }
 else {
  return false;
 }
}

doEditIntro.parseDate = function (datestring) {
 // + sign converts to number
 //Returns NaN in even of parse failure.
 var date = datestring.replace(doEditIntro.dateRegex, doEditIntro.dateFunc);
 if (date === datestring) {
  return NaN;
 }
 return +date;
}
 
doEditIntro.dateRegex = /^Category:(January|February|March|April|May|June|July|August|September|October|November|December) (\d\d?), (20\d\d)$/;

doEditIntro.dateFunc = function (str, month, day, year) {
 //Since JS month starts at month 0
 
 var months = {'January' : 0, 'February': 1, 'March': 2, 'April': 3, 'May': 4, 'June': 5, 'July': 6, 'August': 7, 'September': 8, 'October': 9,  'November': 10,  'December': 11};

 return Date.UTC(year, months[month], day);
 
}
$(doEditIntro);