User:Yitzi/wikia.js

/* */ /* */ $('.DBQuery').each(function { var lines = $(this).text.split(($(this).text.indexOf('\r') != -1) ? '\r' : '\n'); var database = ; var formId = ; var resultId = ''; var properties = ["test", "test2"]; var entries = [];

// Give an error in the results area function showError(str) { $('#' + resultId).empty.append($(' ').addClass('jcError').text(str)); } // Parse config for (var i in lines) { var temp = lines[i].split('=', 2); if (temp.length != 2) {   continue; } switch ($.trim(temp[0])) {   case 'database': database = ($.trim(temp[1])); break; case 'form': formId = ($.trim(temp[1])); break; case 'result': resultId = ($.trim(temp[1])); break; } }

//Dealing with formatting: First to convert links from wikitext to html.. function convertLinks(string) { var aliaslinkre=/\[\[(.*?)\|(.*?)\]\]/; var linkre=/\[\[(.*?)\]\]/; linktext=string.match(aliaslinkre); while (linktext!=null) {   replacement='<"a href="http://runescape.wikia.com/wiki/'+linktext[1]+'>'+linktext[2]+''; string=string.replace(aliaslinkre,replacement); linktext=string.match(aliaslinkre); } linktext=string.match(linkre); while (linktext!=null) {   replacement='<"a href="http://runescape.wikia.com/wiki/'+linktext[1]+'>'+linktext[1]+''; string=string.replace(linkre,replacement); linktext=string.match(linkre); } return(string); } //And second to strip all html tags. function stripHTML(string) { var linkre=/<(.*?)>/g; string=string.replace(linkre,""); return(string); }

//Now for the actual search-and-test mechanic. //First check if a single value matches the query function checkeligvalue(DBvalue,querymins,querymaxes,queryvalues,mode) { var DBmin; var DBmax; var temp=DBvalue.split('-'); if (temp.length==2) {     DBmin=temp[0]; DBmax=temp[1]; }   else {     DBmin=DBvalue; DBmax=DBvalue; } //two cases, based on mode: switch (mode) {   //Either these are the allowed values case 0: for (var l in queryvalues) {        //Four possibilities for acceptance: first is DB value equals query value. if (DBvalue==queryvalues[l]) {         return(true); }       //Or DB value is in the query's range. if (DBvalue>=querymins[l]&& DBvalue<=querymaxes[l]) {         return(true); }       //Or DB range contains query value. if (DBmin<=queryvalues[l]&& DBmax>=queryvalues[l]) {         return(true); }       //Or DB range and query range overlap. if (DBmax>=querymins[l]&& DBmin<=querymaxes[l]) {         return(true); }       }      return(false); //or the disallowed values. case 1: for (var l in queryvalues) {        //Three possibilities for rejection: first is DB value equals query value. if (DBvalue==queryvalues[l]) {         return(false); }       //Or DB value is in the query's range. if (DBvalue>=querymins[l]&& DBvalue<=querymaxes[l]) {         return(false); }       //Or DB range is contained in query range. if (DBmax<=querymaxes[l]&& DBmin>=querymins[l]) {         return(false); }       }      return(true); } } //Now use that to check if a single variable matches the query in any value function checkeligvar(DBentry,queryentry,mode,allownone) { if (allownone==true&&$.trim(DBentry)=='') {   return (true); } var DBvalues=DBentry.split(','); for (var k in DBvalues) {   DBvalues[k]=$.trim(DBvalues[k]); } var queryvalues=queryentry.split(','); var querymins=[]; var querymaxes=[]; for (var k in queryvalues) {   queryvalues[k]=$.trim(queryvalues[k]); var temp=queryvalues[k].split('-'); if (temp.length==2) {     querymins[k]=temp[0]; querymaxes[k]=temp[1]; }   else {     querymins[k]=queryvalues[k]; querymaxes[k]=queryvalues[k]; }   }  for (var k in DBvalues) {   if (checkeligvalue(DBvalues[k],querymins,querymaxes,queryvalues,mode)==true) {     return (true); }   }  return (false); } //Now use that to check if the entire line matches the query function checkeligline(DBline,queryentries,modes,allownones) { var entryre=/ [\s\S]*?<\/td>/g; DBentries=DBline.match(entryre); for (var j in properties) {   if (checkeligvar(stripHTML(DBentries[j]),queryentries[j],modes[j],allownones[j])==false) {     return (false); }   }  return (true); } //Now to construct the list of entries fulfilling a particular query: function query(queryentries,modes,allownones) { eligentries=[]; for (var i in entries) {   if (i==0) {     continue; }   if (checkeligline(entries[i],queryentries,modes,allownones)==true) {     eligentries.push(entries[i]); }   }  return (eligentries); }

//So that's how you get a list fitting one query, but we want to be able to combine them. //First, to add. function addDBtables(current,target) { var temp=current.concat(target); var out = current; for (var i in target) {   if (out.indexof(target[i])==-1) {     out.push(target[i]); }   }  return (out); } //Next, to subtract function subtractDBtables(current,target) { var out=[]; for (var i in current) {   if (target.indexof(current[i])==-1) {     out.push(current[i]); }   }  return (out); } //And last, to intersect function intersectDBtables(current,target) { var out=[]; for (var i in current) {   if (target.indexof(current[i])==-1) {     out.push(current[i]); }   }  return (out); }

//Next, to show and hide the table. function showtable(tablediv, button) { button.value="Hide table"; button.onclick=function{hidetable(tablediv,button)}; var $tableloc=tablediv.children(".wikitable.sortable"); $tableloc.empty; var tableentries=tablediv.tableentries; for (var i in tableentries) {   temp=tableentries[i]; $tableloc.append(temp); } } function hidetable(tablediv, button) { button.value="Show table"; button.onclick=function{show(tablediv,button)}; var $tableloc=tablediv.children(".wikitable.sortable"); $tableloc.empty; }

//Now to use two tables to produce a new one. //First, by replacing the old table: function replaceTable(sourceTable,modifyMethodno,secondTableName,newName,button) { temp=document.getElementById(newName); if (temp!=null) {   alert("That name is already in use."); return(false); } secondTable=document.getElementById(secondTableName); switch(modifyMethodno) {   case 0: var newTableEntries=sourceTable.tableentries; break; case 1: var newTableEntries=addDBtables(sourceTable.tableentries,secondTable.tableentries); break; case 2: var newTableEntries=subtractDBtables(sourceTable.tableentries,secondTable.tableentries); break; case 3: var newTableEntries=intersectDBtables(sourceTable.tableentries,secondTable.tableentries); break; } sourceTable.id=newName; captionloc=sourceTable.children("caption")[0]; captionloc.innerHTML=newName; sourceTable.tableentries=newTableEntries; if (button.value=="Hide Table") {   showtable(sourceTable,button); } }  // And next, by making a new one. function newmodTable(sourceTable,modifyMethodno,secondTableName,newName,button) { temp=document.getElementById(newName); if (temp!=null) {   alert("That name is already in use."); return(false); } secondTable=document.getElementById(secondTableName); switch(modifyMethodno) {   case 0: var newTableEntries=sourceTable.tableentries; break; case 1: var newTableEntries=addDBtables(sourceTable.tableentries,secondTable.tableentries); break; case 2: var newTableEntries=subtractDBtables(sourceTable.tableentries,secondTable.tableentries); break; case 3: var newTableEntries=intersectDBtables(sourceTable.tableentries,secondTable.tableentries); break; } new DBtablediv(newName,newTableEntries); }

function DBtablediv(name,tableentries) { temp=document.getElementById(name); if (temp!=null) {   alert("That name is already in use."); return(false); } this.innerHTML='  '; this.id=name; this.tableentries=tableentries; var tableloc={}; tableloc.innerHTML=' '; $('#' + name).append(tableloc); var button1=document.createElement("input"); button1.type="button"; button1.value="Show Table"; button1.onclick=function{showtable(this,button1)}; $('#' + name).append(button1); var delButton=document.createElement("input"); delButton.type="button"; delButton.value="Delete this table"; delButton.onclick=function{$('#' + name).remove}; $('#' + name).append(delButton); $('#' + name).append(" Modification options:"); var modifyMethod=document.createElement("select"); var temp=document.createElement("option"); temp.text='Copy (do not modify)'; modifyMethod.add(temp,null); var temp=document.createElement("option"); temp.text='Add'; modifyMethod.add(temp,null); var temp=document.createElement("option"); temp.text='Subtract)'; modifyMethod.add(temp,null);  var temp=document.createElement("option");  temp.text='Intersect';  modifyMethod.add(temp,null);  $('#' + name).append(modifyMethod);  $('#' + name).append("Modify this table by")  var modifyBy=document.createElement("input");  modifyBy.type="text";  $('#' + name).append(modifyBy);  $('#' + name).append("Name for new table:")  var newName=document.createElement("input");  newName.type="text";  $('#' + name).append(newName);   var button2=document.createElement("input");  button2.type="button";  button2.value="Replace this table";  button2.onclick=function{replaceTable(this,modifyMethod.selectedIndex,modifyBy.value,newName.value,button1)};  $('#' + name).append(button2);  var button3=document.createElement("input");  button3.type="button";  button3.value="Create as new table";  button3.onclick=function{newmodTable(this,modifyMethod.selectedIndex,modifyBy.value,newName.value)}; $('#' + name).append(button3); alert("button 3 attached"); $('#bodyContent #' + resultId + ',#WikiaArticle #' + resultId).append("testing").append(this); alert("done"); alert(this); }

//And now for the function to make the actual query form. function createQueryTable { var $queryTable=$(' ').addClass('wikitable jcTable'); for (var i in properties) {   var $tr = $(' '); $tr.append(' '+properties[i]+' '); var querymode=document.createElement("select"); var temp=document.createElement("option"); temp.text='Allowed values:'; querymode.add(temp,null); var temp=document.createElement("option"); temp.text='Disallowed values:'; querymode.add(temp,null); querymode.selectedIndex=1; querymode.id=properties[i]+"queryMode"; var $td = $(' '); $td.append(querymode); $tr.append($td); var $td = $(' '); var queryValues=document.createElement("input"); queryValues.type="text"; queryValues.id=properties[i]+'queryValue'; $td.append(queryValues); $tr.append($td); var allowNone=document.createElement("input"); allowNone.id=properties[i]+'allowNone'; allowNone.type="checkbox"; allowNone.checked=true; var $td = $(' '); $td.append('Allow empty entries').append(allowNone); $tr.append($td); $queryTable.append($tr); } $form = $(' ').attr({'action': '#', 'id': 'jcForm' + formId}).submit(function    {    queryentries=[];    modes=[];    allownones=[];    for (var i in properties)      {      temp=document.getElementById(properties[i]+'queryValue');      queryentries.push(temp.value);      temp=document.getElementById(properties[i]+'queryMode');      modes.push(temp.selectedIndex);      temp=document.getElementById(properties[i]+'allowNone');      allownones.push(temp.checked);      }    newEntries=query(queryentries,modes,allownones);    new DBtablediv(queryName.value,newEntries);    }); $form.append($queryTable); $form.append(' '); $form.append('Name for query-results table:'); var queryName=document.createElement("input"); queryName.type="text"; $form.append(queryName); $form.append($(' ').attr({type: 'submit', value: 'Submit'})); $('#bodyContent #' + formId + ',#WikiaArticle #' + formId).empty.append($form); }

function loadDB(data) { //Load database into "entries" variable var pages = data.query.pages; var page=null; for ( var i in pages ) {   pageId = pages[i].pageid; } var page=data.query.pages[pageId]; pagecontent = page.revisions[0]['*']; var linere=/ [\s\S]*?<\/tr>/g; entries=pagecontent.match(linere); //Change links to HTML. for (var i in entries) {   entries[i]=convertLinks(entries[i]); } //The headings in the first line, sans markup, become the properties of the database entries var temp=entries[0]; var headingre=/ [\s\S]*?<\/th>/g; properties=temp.match(headingre); for (var i in properties) {   properties[i]=stripHTML(properties[i]); } createQueryTable; }

$.ajax( {  data:     {    'action': 'query',    'prop': 'revisions',    'titles': database,    'rvprop': 'content',    'redirects': '',    'format': 'json'    },  dataType: 'json',  success: loadDB,  error: showError,  url: mw.config.get( 'wgScriptPath' ) + '/api.php',  timeout: 10000 // millisec  });

}); /* */ /* */