User:Manishearth/orphantabs.js

This is an old revision of this page, as edited by Manishearth (talk | contribs) at 09:15, 13 November 2008. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/*
===================================================================================================
This script works, but is'nt user-friendly.
Ask me how to use it.
[[User:Manishearth]]
I am testing it on [[Cryptonemiales]]
===================================================================================================

*/
var currentLinkWin
var deOrphanDocumentLinkArray = new Array()
var linkACounter=0
var howManyActuallyDeOrphaned = 0
var linkAlreadyThere = 0
var deOrphaning = false
var deOrphanPgName = ""
var deOrphanProcessFirstCall=false
addOnloadHook(function () {
var gServerPre;
var gServerPost;
gServerPre = "http://www.google.com/search?hl=en&rlz=1G1GGLQ_ENUS255&as_q="
gServerPost="&as_epq=&as_oq=&as_eq=&num=100&lr=&as_filetype=&ft=i&as_sitesearch=" + wgServer + "&as_qdr=all&as_rights=&as_occt=any&cr=&as_nlo=&as_nhi=&safe=images"
var pgname = encodeURIComponent( wgTitle );


addPortletLink("p-cactions", wgServer + "/wiki/Special:WhatLinksHere/" + pgname, "Parents", "ca-parents" , "What links here");


addPortletLink("p-cactions", gServerPre + pgname + gServerPost, "De-orphan", "ca-de-orphan", "Find all Wikipedia pages with this page name in them"); 

addPortletLink("p-cactions","javascript:deOrphanWikify(prompt(\"What page are you de-orphaning?\",\"\"))", "De-orphan-wikify", "ca-de-orphan-wikify", "Wikify to deorphan another page"); 

addPortletLink("p-cactions", "javascript:initDeOrphan(prompt(\"What page\?\"\,\"\"));", "De-orphan-complete", "ca-de-orphan", "Find all Wikipedia pages with this page name in them"); 

});
var intID;
var sWin;
var allLinks = new Array()
function deOrphanWikify(pgName){
//alert("dowing")
document.editform.wpTextbox1.value = orphanRawWikify(pgName,document.editform.wpTextbox1.value)
document.editform.wpSummary.value = "Adding link to orphaned [[" + pgname+ "|article]], Wikiproject Orphanage: [[WP:ORPHAN|You can help!]]"  
}
function orphanRawWikify(pgName,text){
rXP = new RegExp("([ '\"()<>,.])(" + pgName + ")([ '\"()<>,.])", "gi")
var t = text.replace(rXP, "$1[[" + pgName + " |$2]]$3")
return t;
}
function initDeOrphan(pgName){
sWin = window.open("http://en.wikipedia.org/w/index.php?title=Special:Search&ns0=1&ns1=1&ns2=1&ns3=1&ns4=1&ns5=1&ns6=1&ns7=1&ns8=1&ns9=1&ns10=1&ns11=1&ns12=1&ns13=1&ns14=1&ns15=1&ns100=1&ns101=1&redirs=1&search=%22" + pgName + "%22&limit=100&offset=0","","")
deOrphanPgName = pgName
deOrphanProcessFirstCall=true
intID = setInterval('checkWin()', 3000)
}
function checkWin(){
if(sWin.document.readyState == "complete"){
clearInterval(intID)
//alert("passes clear")
CreateArray()
}
}
function CreateArray(){
//alert("creating")
		var sLinkArray = new Array()
var allLinksArray = new Array()
		var allLists = sWin.document.getElementsByTagName("ul")
//alert(allLists.length)
		for (i=0;i<allLists.length;i++){
			if (allLists[i].className == "mw-search-results"){
//alert(allLists[i].innerHTML)
allLinksArray = returnArray(allLists[i].innerHTML)
			}
		}
deOrphanDocumentLinkArray = allLinksArray
//alert("final a:\n" + deOrphanDocumentLinkArray)
openALinkWin()

};
function returnArray(text){
	var orphanCompleteRegxp = new RegExp("<a[^<]*<\/a>","gi")
	//alert(orphanCompleteRegxp)
	anchTags = new Array()
	//alert(text.match(orphanCompleteRegxp).toString())
	//alert(text.match(orphanCompleteRegxp).length)
	anchTags = text.match(orphanCompleteRegxp)
	allImpLinks = new Array()
	var orphanAnchRegxp = new RegExp("href=\"([^<>=\"]*)\"","i")
		for (i=0;i<anchTags.length;i++){
		allImpLinks[i] = anchTags[i].match(orphanAnchRegxp)[0].split("=")[1].replace(/\"/g,"")
	}
var colorRectifier = "\""
//Just to return syntax highlighting to normal
	sWin.close()
	return allImpLinks;
}
function openLinkWins(linkArray){
var linkWin = window.open("about:blank","","")
linkWin.___location.replace(wgServer+"/w/index.php?title=" + linkArray[0].replace(/\/wiki\//,"") + "&action=edit")


}
function openALinkWin(){
currentLinkWin = window.open("about:blank","","")
currentLinkWin.___location.replace(wgServer+"/w/index.php?title=" + deOrphanDocumentLinkArray[linkACounter].replace(/\/wiki\//,"") + "&action=edit")
if(!currentLinkWin.addOnloadHook){
currentLinkWin.onload=function(){
currentLinkWin.addOnloadHook(function () {
openALInnerFunct()
});
}
}else{
if(!currentLinkWin.document.loaded){
currentLinkWin.addOnloadHook(function () {
openALInnerFunct()
});
}else{
openALInnerFunct()
}

}
}

function closeTheWindow(){
//alert("closing")
if(!currentLinkWin.addOnloadHook){
currentLinkWin.onload=function(){
currentLinkWin.addOnloadHook(function () {
closeTWInnerFunct()
});
}
}else{
if(!currentLinkWin.document.loaded){
currentLinkWin.addOnloadHook(function () {
closeTWInnerFunct()
});
}else{
closeTWInnerFunct()
}
}
}

function endOfLinkWins(){
var choice = confirm(linkAlreadyThere + " pages linked to " + deOrphanPgName + " before de-orphaning.\n" + howManyActuallyDeOrphaned + " pages were linked to " + deOrphanPgName + "\n" + (linkAlreadyThere + howManyActuallyDeOrphaned) + " pages link to " + deOrphanPgName + "now\n De-orphan? (Press cancel to tag the page with \'do-attempt\'.")
currentLinkWin = window.open("about:blank","","")
currentLinkWin.___location.replace(wgServer+"/w/index.php?title=" + deOrphanPgName + "&action=edit")
if(!currentLinkWin.addOnloadHook){
currentLinkWin.onload=function(){
currentLinkWin.addOnloadHook(function () {
addOrphanTag(choice)
});
}
}else{
if(!currentLinkWin.document.loaded){
currentLinkWin.addOnloadHook(function () {
addOrphanTag(choice)
});
}else{
addOrphanTag(choice)
}
}
}

function addOrphanTag(choice){
currentLinkWin.editform.wpTextbox1.value = addOrphanTagRaw(choice, currentLinkWin.editform.wpTextbox1.value)
var editSum = ""
if(choice){
editSum = "Successfully de-orphaned! Wikiproject Orphanage: [[WP:ORPHAN|You can help!]]"
}else{
editSum = "Unsuccesful de-orphan attempt, Wikiproject Orphanage: [[WP:ORPHAN|You can help!]]"
}
currentLinkWin.editform.wpSummary.value=editSum
currentLinkWin.close()
alert("De-orphaning over!!!")
}

function openALInnerFunct(){
if(currentLinkWin.wgCanonicalNamespace == ""){
if(orphanRawWikify(deOrphanPgName, currentLinkWin.editform.wpTextbox1.value) == currentLinkWin.editform.wpTextbox1.value){
linkAlreadyThere++
}else{
howManyActuallyDeOrphaned++
currentLinkWin.deOrphanWikify(deOrphanPgName)
currentLinkWin.document.editform.submit()
//alert("submitting")
setTimeout("closeTheWindow()",1000)
}
}else{
closeTheWindow()
}
}
function closeTWInnerFunct(){
currentLinkWin.close()
linkACounter++
if(linkACounter!=deOrphanDocumentLinkArray.length){
openALinkWin()
}else{
endOfLinkWins()
}

}
function addOrphanTagRaw(choice, val){
var rVal
if(choice){
	if(val.replace("{{orphan}}", "") != val){
		rVal = val.replace("{{orphan}}", "")
		alert(1)
	}else if(/{{orphan\|\wdate=[^\}]*}}/i.test(val)){
		rVal = val.replace(/{{orphan\|\wdate=[^\}]*}}/i, "")
		alert(2)
	}else if(val.indexOf("{{articleissues") !=-1){
		rVal=val.replace(/\|\w?orphan=[^\|\}]*/,"")
		rVal=rVal.replace(/\|\w?do-attempt=[^\|\}]*/,"")
		alert(4)
	}else{
		rVal=val
		alert(5)
	}
}else{
	if(val.replace("{{orphan}}", "") != val){
		rVal = val.replace("{{orphan}}", "{{do-attempt|att=November}}")
		alert(21)
	}else if(new RegExp("{{orphan|date=[^\}]}}","i").test(val)){
		rVal = val.replace(/{{orphan\|date=([^\}]*)}}/i, "{{do-attempt|date=$1|att=November}}")
		alert(22)
	}else if(new RegExp("{{do-attempt|date=[^\|\}]*\|att=[^\|\}]*}}","i").test(val)){
		rVal = val.replace(/({{do-attempt|date=[^\|\}]*\|att=)[^\|\}]*}}/, "$1November}}")

	}else if(val.indexOf("{{articleissues") !=-1){
		rVal=rVal.replace(/\|\w?do-attempt=[^\|\}]*/,"\|do-attempt=November")
		alert(23)
	}else{
		rVal= val
	}
}
return rVal
}