User:Whiteknight/pagetester.js

Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
  • Mozilla / Firefox / Safari: hold down Shift while clicking Reload, or press Ctrl-Shift-R (Cmd-Shift-R on Apple Mac);
  • Internet Explorer: 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.
var pt = {}
pt.formspan = "WKTestFormSpan";
pt.textspan = "WKTestSpan";
pt.bookName = "";
pt.bookNameE = "";
pt._form = null;
pt.getForm = function() {
  return pt._form;
}
pt.getBookName = function() {
  pt.bookName = pt.booknameB.value;
  pt.bookNameE = pt.bookName.replace(/ /g, "_");
  return pt.bookName;
}
pt.createForm = function() {
  var div = wk.spanText(pt.formspan, "");
  if(div == null) return;
  pt.booknameB = wk.makeInput("WKTestBookNameB", "")
  pt._form = wk.makeElement("form", {name:"WKTestForm", method:"GET"}, [
    "Page Name:",
    pt.booknameB,
    wk.makeButton("WKTestSubmitB", "Get",   pt.prepTest),
    wk.makeButton("WKTestClearB",  "Clear", function(){wk.spanText(pt.textspan)})
  ]);
  div.appendChild(pt._form);
}
$(pt.createForm);
pt.prepTest = function() {
  if(pt.getBookName() == "") return;
  wk.spanText(pt.textspan, "Loading Page...");
  wk.loadWikiText(pt.bookName, pt._prepTest2);
}
pt._prepTest2 = function(text) {
  var len = text.length;
  if(len == 0) {
     wk.spanText(pt.textspan, "Page [[" + wk.wikiLinkText(pt.bookname) + "]] does not exist");
     return;
  }
  if(len >= 9 && text.substring(0, 9).toLowerCase() == "#redirect")
  {
    var dest = text.substring(10);
    dest = dest.replace("[[", "");
    dest = dest.replace("]]", "");
    res.innerHTML = "Page redirects to [[" + wk.wikiLinkText(dest) + "]]";
    return;
  }
  var words = text.match(/\S+/g).length;

  var links;
  var links_len = 0;
  if(text.match(/\[\[[^\]]+\]\]/)) {
    links = text.match(/\[\[([^\]]+)\]\]/g);
    for(i = 0; i < links.length; i++) {
      links[i] = links[i].replace(/\|[^\]]+\]\]/, "");
    }
    links_len = links.length;
  }

  var cats;
  var cats_len = 0;
  if(text.match(/\[\[category:[^\]]+\]\]/i)) {
    cats = text.match(/\[\[category\:([^\]]+)\]\]/ig);
    cats_len = cats.length;
  }
  
  var tempsa;
  var temps = [];
  var temps_len = 0;
  if(text.match(/\{\{[^\}]+\}\}/)) {
    tempsa = text.match(/\{\{([^\}]+)\}\}/g);
    tempsa.sort();
    for(i = 0; i < tempsa.length; i++) {
      tempsa[i] = tempsa[i].replace(/{{/, "");
      tempsa[i] = tempsa[i].replace(/}}/, "");
      tempsa[i] = tempsa[i].replace(/\|[^\}]+$/, "");
      if(tempsa[i].match(/^\/.+\/$/)) {
        tempsa[i] = tempsa[i].replace(/^\/(.+)\/$/, ":" + pt.bookName + "/$1");
      }
      if(tempsa[i].substr(0, 10) == "Wikibooks:") tempsa[i] = ":" + tempsa[i];
      if(tempsa[i].substr(0, 5) == "User:") tempsa[i] = ":" + tempsa[i];
    }
    j = 1;
    temps[0] = tempsa[0];
    for(i = 1; i < tempsa.length; i++) {

      if(tempsa[i] == tempsa[i - 1]) {
        continue;
      } else {
        temps[j++] = tempsa[i];
      }
    }
    temps_len = temps.length;
  }

  wk.spanText(pt.textspan, "Page [[" +
    wk.wikiLinkText(pt.bookName) +
    "]] exists, and contains: <br><ul>" + 
    "<li>Characters: " + len + 
    "<li>Words: " + words +
    "<li>Links: " + links_len + 
    "<li>Categories: " + cats_len + 
    "<li>Templates: " + temps_len +
    "</ul><br><b>Templates:</b><br>");
  for(i = 0; i < temps.length; i++) {
    if(temps[i].substr(0,1) == ':') {
      temps[i] = temps[i].substr(1);
      wk.appendSpanText(pt.textspan, "[[" + wk.wikiLinkText(temps[i]) + "]]<br>");
    } else {
      wk.appendSpanText(pt.textspan, "{{" + wk.wikiLinkText("Template:" + temps[i], temps[i]) + 
                                     "}}<br>");
    }
  }
  wk.appendSpanText(pt.textspan, "<b>Categories:</b><br>");
  for(i = 0; i < cats_len; i++) {
    cats[i] = cats[i].replace("[[", "");
    cats[i] = cats[i].replace("]]", "");
    wk.appendSpanText(pt.textspan, "[[" + wk.wikiLinkText(cats[i]) + "]]<br>");
  }
  wk.appendSpanText(pt.textspan, "<b>Links:</b><br>");
  for(i = 0; i < links_len; i++) {
    links[i] = links[i].replace("[[", "");
    links[i] = links[i].replace("]]", "");
    wk.appendSpanText(pt.textspan, "[[" + wk.wikiLinkText(links[i]) + "]]<br>");
  }
}