User:Mr.Z-man.sock/funcs.js

/* Mr.Z-man's helper functions for Wikipedia JS scripts.

// Show an error if the browser doesn't support AJAX function AJAXtest { if (!wfSupportsAjax) { jsMsg(' Your browser does not seem to support AJAX, which is required for the reviewSpeedy script. '); return false; } return true; }

// Creates a button  function js_button(idname, value, func) { var e = document.createElement('input'); e.type = 'button'; e.id = idname; e.name = idname; e.value = value e.onclick = func return e; }

// Creates an element text function js_element(elem, text) { var e = document.createElement(elem); e.appendChild(text); return e; }

// Creates a function js_br { var e = document.createElement('br'); return e; }

// Creates a link text // Set redlink to true to create a redlink function js_link(target, text, redlink) { var link = document.createElement('a'); link.href = mw.config.get('wgArticlePath').replace('$1', encodeURIComponent(target)) link.title = target; link.appendChild(text); if (redlink) { link.className = "new"; link.href+= '?action=edit&redlink=1'; link.title+= " (page does not exist)"; } return link; }

// Create a userlinks thingy ( User (talk | contribs) ) // FIXME: needs a way to bypass API lookup function js_userlinks(user, admin) { if (user == 'unknown') { var ret = document.createElement('tt'); ret.appendChild(document.createTextNode(user)); return ret; } var req = sajax_init_object; req.open("GET", mw.config.get('wgScriptPath') + "/api.php?action=query&format=json&indexpageids&titles="+encodeURIComponent("User:"+user+"|User talk:"+user), false); req.send(null); var response = eval('(' + req.responseText + ')'); delete req; var user = '' var talk = '' for (var i=0;i<2;i++) { var pageid = response['query']['pageids'][i]; var page = response['query']['pages'][pageid]; try { if (page['missing'] == '') { var missing = true; } else { var missing = false; }   } catch(e) { var missing = false; }   var title = page['title']; var link = js_link(title, document.createTextNode(''), missing); if (page['ns'] == "2") { user = link; user.appendChild(document.createTextNode(title.replace("User:", ''))); } else { talk = link; talk.appendChild(document.createTextNode("Talk")); } }  var contribs = js_link("Special:Contributions/"+title, document.createTextNode("contribs"), false); var userlinks = document.createElement('span') userlinks.className = 'mw-usertoollinks'; userlinks.appendChild(user); userlinks.appendChild(document.createTextNode(' (')); userlinks.appendChild(talk);  userlinks.appendChild(document.createTextNode(' | '));  userlinks.appendChild(contribs);  if (admin) {    userlinks.appendChild(document.createTextNode(' | '));    var block = js_link("Special:Block/"+title, document.createTextNode("block"), false);    userlinks.appendChild(block);     }  userlinks.appendChild(document.createTextNode(')')); return userlinks; }

function js_getToken { var req = sajax_init_object; req.open("GET", mw.config.get('wgScriptPath') + "/api.php?action=query&prop=info&indexpageids=1&intoken=edit&format=json&titles="+encodeURIComponent(reviewSpeedy_PageName), false); req.send(null); var response = eval('(' + req.responseText + ')'); var pageid = response['query']['pageids'][0]; var token = encodeURIComponent(response['query']['pages'][pageid]['edittoken']); delete req; return token; }

function js_getPageText(title) { var req = sajax_init_object; req.open("GET", mw.config.get('wgScriptPath') + "/api.php?action=query&prop=revisions&rvprop=content&format=json&indexpageids=1&titles="+encodeURIComponent(title), false); req.send(null); var response = eval('(' + req.responseText + ')'); pageid = response['query']['pageids'][0]; if (pageid == "-1") { delete req; return ''; } pagetext = response['query']['pages'][pageid]['revisions'][0]['*']; delete req; return pagetext; }

// Delete a page // func=function to call when finished // retry=should it tell func to retry if it fails function js_deletePage(title, token, summary, func, retry) { var req = sajax_init_object; var params = "action=delete&format=json&token="+token+"&title="+title+"&reason="+summary+"&requestid="+title; var url = mw.config.get('wgScriptPath') + "/api.php"; req.open("POST", url, true); 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 && req.status == 200) { func(req, retry, token) }}; req.send(params); }

// Parse some wikitext function js_parse(text) { var req = sajax_init_object; req.open("GET", mw.config.get('wgScriptPath') + "/api.php?action=parse&format=json&title="+encodeURIComponent(mw.config.get('wgPageName'))+"&text="+encodeURIComponent(text)+"&prop=text", false); req.send(null); var response = eval('(' + req.responseText + ')'); var ptext = response['parse']['text']['*']; delete req; return ptext.replace(/ (.*?)<\/p>\n\n\n/m, '$1'); }

/* Format a timestamp from the API Choices are: 16:12, January 15, 2001 = 1 16:12, 15 January 2001 = 2 16:12, 2001 January 15 = 3 2001-01-15T16:12:34    = 4

function js_timeFormat(time, format) { var patt = /(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)Z/ var month = time.replace(patt, "$2") switch (month) { case "01": month = "January"; break; case "02": month = "February"; break; case "03": month = "March"; break; case "04": month = "April"; break; case "05": month = "May"; break; case "06": month = "June"; break; case "07": month = "July"; break; case "08": month = "August"; break; case "09": month = "September"; break; case "10": month = "October"; break; case "11": month = "November"; break; case "12": month = "December"; break; default: month = "Error"; } var time; switch (format) { case 1: time = time.replace(patt, "$4:$5, " + month + " $3, $1"); break; case 2: time = time.replace(patt, "$4:$5, $3 " + month + " $1"); break; case 3: time = time.replace(patt, "$4:$5, $1 " + month + " $3"); break; case 4: time = time.replace(patt, "$1-$2-$3T$4:$5:$6"); break; default: time = time; } return time; }