User:AFigureOfBlue/toolkit/old.js

// //Specifically "wiki-related" functions, mostly API-querying tools, all contained in the "wiki" object var wiki = {}; //Basic function to make synchronous POST requests to the API. Argument is just a list of params, without a leading "?" or "format" designation. Returns a JSON response from the API (or an XML response, if "xml" is passed as the second argument). wiki.query = function(url, format){ if(!format){ var format = "xml"; } var req = sajax_init_object; req.open("POST", "http://en.wikipedia.org/w/api.php", false); var params = url + "&format=" + format; req.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); req.setRequestHeader("Content-length", params.length); req.setRequestHeader("Connection", "close"); req.send(params); if(format == "xml"){ return req.responseXML; } else if(format == "json"){ return eval('(' + req.responseText + ')'); } else { return req; } };

//Make an asynchronous API request to edit a page. First argument is a list of params, without a leading "?". Second argument is a string of JavaScript code to run when the response is returned. wiki.query_async = function(url, callback){ var req = sajax_init_object; req.open("POST", "http://en.wikipedia.org/w/api.php"); var params = url; req.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); req.setRequestHeader("Content-length", params.length); req.setRequestHeader("Connection", "close"); req.onreadystatechange = function{ if(req.readyState == 4){ if(req.status == 200){ eval(callback); } } }; req.send(params); };

wiki.edit = function(url, callback){ wiki.query_async("action=edit&token=" + wiki.token("edit") + "&" + url, callback); };

wiki.del = function(url, callback){ wiki.query_async("action=delete&token=" + wiki.token("delete") + "&" + url, callback); };

wiki.token = function(type, pagename){ if(!pagename){ var pagename = wgPageName; } var req = wiki.query("action=query&prop=info&intoken=" + enc(type) + "&titles=" + encodeURIComponent(pagename)); return encodeURIComponent(req.getElementsByTagName("page")[0].getAttribute(type + "token")); };

//Runs an API query that returns the text of the current version of the page. wiki.page_content = function(pagename){ if(!pagename){ var pagename = wgPageName; } var req = wiki.query("action=query&prop=revisions&titles=" + encodeURIComponent(pagename) + "&rvprop=content"); return req.getElementsByTagName("rev")[0].firstChild.nodeValue; };

//Runs an API query that returns the username of pagename's creator. wiki.page_creator = function(pagename){ if(!pagename){ var pagename = wgPageName; } var req = wiki.query("action=query&prop=revisions&titles=" + encodeURIComponent(pagename) + "&rvlimit=1&rvprop-user&rvdir=newer"); return req.getElementsByTagName("rev")[0].getAttribute("user"); };

wiki.userpage_user = function(pagename){ if(!pagename){ var pagename = wgTitle; } return pagename.replace(/User(_talk)?\:/, '').replace(/([^\/]*).*/, '$1'); }; //