User:Rholton/fixdisambig.js

addOnloadHook ( init_fixdisambig ) ;

fixdisambig_server = "" ;

fixdisambig_real_target = "" ;

function fixdisambig_get_selection ()

{

if (window.getSelection) return window.getSelection();

else if (document.getSelection) return document.getSelection();

else if (document.selection) return document.selection.createRange().text;

return "" ;

}

function fixdisambig_get_query(name) {

var value=null;

var query=window.location.search;

if(query != "") {

var kk=query.indexOf(name+"=");

if(kk >= 0) {

kk=kk+name.length+1;

var ll=query.indexOf("&", kk);

if(ll < 0)ll=query.length;

value=query.substring(kk, ll);

for(kk=0; kk

if(value.charAt(kk) == '+') {

value=value.substring(0, kk)+" "+value.substring(kk+1,value.length);

}

}

value=unescape(value);

}

}

return value;

}

function fixdisambig_get_referrer_for_disambig () {

var catlinks = document.getElementById ( "catlinks" ) ;

if ( !catlinks ) return "" ;

var as = catlinks.getElementsByTagName ( "a" ) ;

var is_disambig = 0 ;

for ( var ai = 0 ; ai < as.length ; ai++ ) {

var a = as[ai] ;

var h = a.getAttribute('href').split('/Category:').pop() ;

h = h.split('_').join(' ') ;

if ( h != "Disambiguation" &&

h != "Lists of ambiguous numbers" &&

h != "Lists of ambiguous human names"

) continue ;

is_disambig = 1 ;

break ;

}

if ( !is_disambig ) return "" ;

fixdisambig_real_target = wgPageName ;

var sitesub = document.getElementById('contentSub') ;

if ( sitesub ) {

var a = sitesub.getElementsByTagName('a') ;

if ( a.length > 0 ) {

a = a[0] ;

fixdisambig_real_target = a.getAttribute ( 'title' ) ;

}

}

var r = document.referrer ;

if ( r.substr ( 0 , 29 ) == "http://en.wikipedia.org/wiki/" ) {

fixdisambig_server = r.substr ( 0 , 29 ) ;

r = r.substr ( 29 ) ;

} else if ( r.substr ( 0 , 47 ) == "https://secure.wikimedia.org/wikipedia/en/wiki/" ) {

fixdisambig_server = r.substr ( 0 , 47 ) ;

r = r.substr ( 47 ) ;

}

else r = "" ;

return r ;

}

function fix_disambig_replace ( rtext , rwhat , rwith , rend ) {

return rtext.split ( rwhat + rend ) . join ( rwith + rend ) ;

}

function do_fix_disambig ( fd , rwith ) { //fd=Sir_William_Jones rwith=William_Jones_(philologist)

rwith = "[[" + rwith ;

rwith = rwith.split("_").join(" ") ;

var fd1 = fd ;

fd = "[[" + fd ; // [[Sir_William_Jones

var fd2 = fd.split("_").join(" ") ; // [[Sir William Jones

document.editform.wpMinoredit.checked = true ;

document.editform.wpSummary.value = "Replacing disambiguation link to " + fd2 +

"]] with direct link to " + rwith + "]] (using FixDisambig.js)" ;

var text = document.editform.wpTextbox1.value ;

text = fix_disambig_replace ( text , fd , rwith , "|" ) ; // if already piped, don't mess with the pipe

text = fix_disambig_replace ( text , fd , rwith+"|"+fd1 , "]]" ) ; // if no pipe, just add the pipe

text = fix_disambig_replace ( text , fd2 , rwith , "|" ) ;

text = fix_disambig_replace ( text , fd2 , rwith+"|"+fd1 , "]]" ) ;

document.editform.wpTextbox1.value = text ;

document.getElementById("wpDiff").click() ; // Run diff

}

function init_fixdisambig () {

var fd = fixdisambig_get_query ( "fix_disambig" ) ;

var rwith = fixdisambig_get_query ( "fix_disambig_with" ) ;

if ( fd && rwith && fd != "" && rwith != "" ) {

do_fix_disambig ( fd , rwith ) ;

return ;

}

var r = fixdisambig_get_referrer_for_disambig () ;

if ( r == "" ) return ;

var out = '

" ;

var tools = document.getElementById("p-tb") ;

tools = tools.getElementsByTagName("ul")[0] ;

tools.innerHTML += out ; // Yeah, I know...

}

function run_fix_disambig () {

var sel = fixdisambig_get_selection () ;

if ( sel == "" ) {

var r2 = wgPageName.split("_").join(" ") + " ()" ;

sel = prompt ( "Please enter the new target (you could have skipped this by selecting the new title in the page text):" , r2 ) ;

if ( !sel || sel == "" || sel == r2 ) return ;

}

var r = fixdisambig_get_referrer_for_disambig () ;

document.location = fixdisambig_server + r + "?action=edit&fix_disambig=" +

escape ( fixdisambig_real_target ) + "&fix_disambig_with=" + escape ( sel ) ;

}