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 ($.trim(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); } }

//Next, a constructor function to create a new table-containing div. function DBtablediv(name,tableentries) { this.innerHTML='  '; alert(tableentries); }

//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  });

}); /* */ /* */