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 = []; var entries = []; var queries = {}; var placeholder=document.createTextNode(""); // 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=/\[\[(.*?)\]\]/;  var linktext=string.match(aliaslinkre);  while (linktext!=null) {   var replacement='<"a href="http://runescape.wikia.com/wiki/'+linktext[1]+'>'+linktext[2]+''; var string=string.replace(aliaslinkre,replacement); linktext=string.match(aliaslinkre); } linktext=string.match(linkre); while (linktext!=null) {   var replacement='<"a href="http://runescape.wikia.com/wiki/'+linktext[1]+'>'+linktext[1]+''; var string=string.replace(linkre,replacement); linktext=string.match(linkre); } return(string); } //And second to strip all html tags. function stripHTML(string) { var linkre=/<(.*?)>/g; var 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&&!isNaN(temp[0])&&!isNaN(temp[1])) {     DBMin=parseFloat(temp[0]); DBMax=parseFloat(temp[1]); }   else {     DBValue=DBValue.toLowerCase; DBMin=DBValue; DBMax=DBValue; } //two cases, based on mode: switch (mode) {   //Either these are the allowed values case 0: for (var i in queryValues) {        //Four possibilities for acceptance: first is DB value equals query value. if (DBValue==queryValues[i]) {         return(true); }       //Or DB value is in the query's range. if (DBValue>=queryMins[i]&& DBValue<=queryMaxes[i]) {         return(true); }       //Or DB range contains query value. if (DBMin<=queryValues[i]&& DBMax>=queryValues[i]) {         return(true); }       //Or DB range and query range overlap. if (DBMax>=queryMins[i]&& DBMin<=queryMaxes[i]) {         return(true); }       }      return(false); //or the disallowed values. case 1: for (var i in queryValues) {        //Three possibilities for rejection: first is DB value equals query value. if (DBvalue==queryValues[i]) {         return(false); }       //Or DB value is in the query's range. if (DBValue>=queryMins[i]&& DBValue<=queryMaxes[i]) {         return(false); }       //Or DB range is contained in query range. if (DBMax<=queryMaxes[i]&& DBMin>=queryMins[i]) {         return(false); }       }      return(true); } } //Now use that to check if a single variable matches the query in any value function checkEligVar(DBEntry,queryEntry,mode) { var DBValues=DBEntry.split(','); for (var i in DBValues) {   DBValues[i]=$.trim(DBValues[i]); } var queryValues=queryEntry.split(','); var queryMins=[]; var queryMaxes=[]; for (var i in queryValues) {   queryValues[i]=$.trim(queryValues[i]); var temp=queryValues[i].split('-'); if (temp.length==2&&!isNaN(temp[0])&&!isNaN(temp[1])) {     queryMins[i]=parseFloat(temp[0]); queryMaxes[i]=parseFloat(temp[1]); }   else {     queryValues[i]=queryValues[i].toLowerCase queryMins[i]=queryValues[i]; queryMaxes[i]=queryValues[i]; }   }  for (var i in DBValues) {   if (checkEligValue(DBValues[i],queryMins,queryMaxes,queryValues,mode)==true) {     return (true); }   }  return (false); } //Now use that to check if the entire line matches the query function checkEligLine(DBLine,enables,queryEntries,modes) { var entryRE=/ [\s\S]*?<\/td>/g; var DBEntries=DBLine.match(entryRE); for (var i in properties) {   if (enables[i]==true&&checkEligVar(stripHTML(DBEntries[i]),queryEntries[i],modes[i])==false) {     return (false); }   }  return (true); } //Now to construct the list of entries fulfilling a particular query: function query(enables,queryEntries,modes) { var eligentries=[]; for (var i in entries) {   if (i==0) {     continue; }   if (checkEligLine(entries[i],enables,queryEntries,modes)==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 lastly, 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); }

//All the DOM elements associated with a query are organized by the properties of the queries element, //so next is a constructor for those properties

function queryRecords(name,div,table,showHide,resultList,modifyMethod,modifyBy,newName) { this.name=name; this.div=div; this.table=table; this.showHide=showHide; this.resultList=resultList; this.modifyMethod=modifyMethod; this.modifyBy=modifyBy; this.newName=newName; }

//Next, to show and hide the table. function showTable { this.value="Hide table"; this.onclick=hideTable; var queryObject=this.queryObject; var table=queryObject.table; queryObject.div.replaceChild(placeholder, table); //So that the browser doesn't have to redraw the window at each step. var fc=table.firstChild; while (fc) {   table.removeChild(fc); fc=table.firstChild; } var cap=table.createCaption; cap.innerHTML=queryObject.name; alert (queryObject.resultList); //debugging alert ("setting tableEntries"); //debugging var tableEntries=queryObject.resultList; alert ("beginning tablerow adds"); //debugging for (var i in tableEntries) {   temp=tableEntries[i]; table.appendChild(temp); } alert ("finished tablerow adds"); alert (placeholder); //debugging queryObject.div.replaceChild(table,placeholder); } function hideTable { this.value="Show table"; this.onclick=showTable; var table=this.queryObject.table; queryObject.div.replaceChild(placeholder, table); var fc=table.firstChild; while (fc) {   table.removeChild(fc); fc=table.firstChild; } var cap=table.createCaption; cap.innerHTML=this.queryObject.name; queryObject.div.replaceChild(table,placeholder); }

//Now to use two tables to produce a new one. //First, by replacing the old table: function replaceTable { var queryObject=this.queryObject; var newName=queryObject.newName.value; if (newName in queries) {   alert("That name is already in use."); return(false); } var secondTableName=queryObject.modifyBy.value; var modifyMethodNo=queryObject.modifyMethod.selectedIndex; switch(modifyMethodNo) {   case 0: var newTableEntries=queryObject.resultList; break; case 1: if (secondTableName in queries) {       var newTableEntries=addDBtables(queryObject.resultList,queries[secondTableName].resultList); break; }     else {       alert("Cannot modify by that table; it does not exist"); return (false); }   case 2: if (secondTableName in queries) {       var newTableEntries=subtractDBtables(queryObject.resultList,queries[secondTableName].resultList); break; }     else {       alert("Cannot modify by that table; it does not exist"); return (false); }   case 3: if (secondTableName in queries) {       var newTableEntries=intersectDBtables(queryObject.resultList,queries[secondTableName].resultList); break; }     else {       alert("Cannot modify by that table; it does not exist"); return (false); }   }  queries[newName]=queryObject; delete queries[queryObject.name]; queryObject.name=newName; queryObject.resultList=newTableEntries; var button=showHides[newName]; if (button.value=="Hide Table") {   showTable.call(button); } else {   hideTable.call(button); } }  // And next, by making a new one. function newModTable { var queryObject=this.queryObject; var newName=queryObject.newName.value; if (newName in queries) {   alert("That name is already in use."); return(false); } var secondTableName=queryObject.modifyBy.value; var modifyMethodNo=queryObject.modifyMethod.selectedIndex; switch(modifyMethodNo) {   case 0: var newTableEntries=queryObject.resultList; break; case 1: if (secondTableName in queries) {       var newTableEntries=addDBtables(queryObject.resultList,queries[secondTableName].resultList); break; }     else {       alert("Cannot modify by that table; it does not exist"); return (false); }   case 2: if (secondTableName in queries) {       var newTableEntries=subtractDBtables(queryObject.resultList,queries[secondTableName].resultList); break; }     else {       alert("Cannot modify by that table; it does not exist"); return (false); }   case 3: if (secondTableName in queries) {       var newTableEntries=intersectDBtables(queryObject.resultList,queries[secondTableName].resultList); break; }     else {       alert("Cannot modify by that table; it does not exist"); return (false); }   }  createResultTableStructure(newName,newTableEntries); }

//Next, to delete an existing table.

function deleteTable { var queryObject=this.queryObject; document.getElementById(resultId).removeChild(queryObject.div); delete queries[queryObject.name]; } //Next, a function to create a new table-containing div. function createResultTableStructure(name,resultList) { var newTableDiv=document.createElement("div"); newTableDiv.appendChild(document.createElement("br")); var newTable=document.createElement("table"); newTable.createCaption.innerHTML=name; newTableDiv.appendChild(newTable); var button1=document.createElement("input"); button1.type="button"; button1.value="Show Table"; button1.onclick=showTable; newTableDiv.appendChild(button1); var delButton=document.createElement("input"); delButton.type="button"; delButton.value="Delete this table"; delButton.onclick=deleteTable; newTableDiv.appendChild(delButton); newTableDiv.appendChild(document.createElement("br")); newTableDiv.appendChild(document.createTextNode("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); newTableDiv.appendChild(modifyMethod); newTableDiv.appendChild(document.createTextNode("Modify this table by")); var modifyBy=document.createElement("input"); modifyBy.type="text"; newTableDiv.appendChild(modifyBy); newTableDiv.appendChild(document.createTextNode("Name for new table:")); var newName=document.createElement("input"); newName.type="text"; newTableDiv.appendChild(newName); var button2=document.createElement("input"); button2.type="button"; button2.value="Replace this table"; button2.onclick=replaceTable; newTableDiv.appendChild(button2); var button3=document.createElement("input"); button3.type="button"; button3.value="Create as new table"; button3.onclick=newModTable; newTableDiv.appendChild(button3); newTableDiv.appendChild(document.createElement("br")); record=new queryRecords(name,newTableDiv,newTable,button1,resultList,modifyMethod,modifyBy,newName); button1.queryObject=record; delButton.queryObject=record; button2.queryObject=record; button3.queryObject=record; queries[name]=record; document.getElementById(resultId).appendChild(newTableDiv); }

//And now for the function to make the actual query form. function createQueryTableStructure { var queryTable=document.createElement("table"); var enablesInputs=[]; var queryEntriesInputs=[]; var modesInputs=[]; for (var i in properties) {   var newRow=queryTable.insertRow(i); var header=document.createElement("th"); header.appendChild(document.createTextNode(properties[i])); newRow.appendChild(header); var enablesLoc=newRow.insertCell(1); var enablesInput=document.createElement("input"); enablesInput.type="checkbox"; enablesInput.checked=false; enablesInputs.push(enablesInput); enablesLoc.appendChild(document.createTextNode("Restrict by this property")); enablesLoc.appendChild(enablesInput); var entriesLoc=newRow.insertCell(2); var modesInput=document.createElement("select"); var temp=document.createElement("option"); temp.text='Allowed values:'; modesInput.add(temp,null); var temp=document.createElement("option"); temp.text='Disallowed values:'; modesInput.add(temp,null); modesInputs.push(modesInput); entriesLoc.appendChild(modesInput); var entriesInput=document.createElement("input"); entriesInput.type="text"; queryEntriesInputs.push(entriesInput); entriesLoc.appendChild(entriesInput); } var queryLoc=document.getElementById(formId); queryLoc.appendChild(queryTable); queryLoc.appendChild(document.createTextNode('Name for query-results table:')); var resultTableName=document.createElement('input'); resultTableName.type="text"; queryLoc.appendChild(resultTableName); var queryButton=document.createElement('input'); queryButton.type="button"; queryButton.value="Create table of results"; queryButton.onclick=function {   if (resultTableName.value in queries) {     alert("That name is already in use."); return(false); }   var enables=[]; var queryEntries=[]; var modes=[]; for (i in enablesInputs) {     enables.push(enablesInputs[i].checked); queryEntries.push(queryEntriesInputs[i].value); modes.push(modesInputs[i].selectedIndex); }   var resultList=query(enables,queryEntries,modes); createResultTableStructure(resultTableName.value,resultList); } queryLoc.appendChild(queryButton); document.getElementbyId(resultId).appendChild(queryLoc); }

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]); } createQueryTableStructure; } $.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  });

}); /* */ /* */