User:Splarka/dabfinder.js

From Wikipedia, the free encyclopedia

If a message on your talk page led you here, please be wary of who left it. Code that you insert on this page could contain malicious content capable of compromising your account. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. If this is a .js page, the code will be executed when previewing the page.
Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.
/* Disambiguation lookup script, version [0.2.0]
Originally from: http://en.wikipedia.org/wiki/User:Splarka/dabfinder.js
 
Notes:
* Uses the API using head/appendchild(script) and callback to avoid ajax.
* Alt command finds and hilights redirects with simple CSS append (class="mw-redirect").
 
Operation:
* Adds portlet button (or works with URI parameter &finddab=true)
** Makes call to MediaWiki:Disambiguationspage links, generates array of these.
*** Makes call to generator=links / prop=templates on page title.
**** Builds array of registered links that link to disambiguation pages (that contain a disambiguation template).
***** Iterates over all <a> link objects on page, matching any that link to disambiguation, and applies green border.
***** .dablink descendant links are given a dashed border, all other found links are given a solid border.
 
This is a bit messy but at the time was the easiest and most thorough way to do it I could think of.
Of course, a bot might be much more efficient, but this is handy for quick on-the-fly live checking.
 
To do:
* test it
* centralized link list in contentSub?
* ?
*/
 
var dabnames = new Array();
if(wgNamespaceNumber != -1) addOnloadHook(findDABsButton)
function findDABsButton() {
  addPortletLink('p-tb','javascript:findDABs()','Find disambiguations','t-dab');
  if(queryString('finddab')=='true') findDABs();
  addPortletLink('p-tb','javascript:findRDRs()','Find redirects','t-rdr');
  if(queryString('findrdr')=='true') findRDRs();
}
 
function findRDRs() {
  appendCSS('.mw-redirect { background-color: #ffff00;}\n#t-rdr:after {content:" (redirects hilighted)"; background-color:#ffff00; }');
}
 
function findDABs() {
  var dab = document.getElementById('t-dab');
  if(dab) injectSpinner(dab,'dab');
  var url = wgServer + wgScriptPath + '/api.php?action=query&prop=links&tlnamespace=10&format=json&callback=findDABsCB&titles=MediaWiki:Disambiguationspage';
  importScriptURI(url);
}
 
function findDABsCB(obj) {
  if(!obj['query'] || !obj['query']['pages']) return
  for(var i in obj['query']['pages']) var links = obj['query']['pages'][i]['links']
  if(!links) return
  for(var i=0;i<links.length;i++) {
    dabnames[dabnames.length] = links[i]['title'];
  }
  var url = wgServer + wgScriptPath + '/api.php?action=query&redirects&generator=links&prop=templates&format=json&callback=findDABlinksCB&titles=' + encodeURIComponent(wgPageName);
  importScriptURI(url);
}
 
function findDABlinksCB(obj) {
  var dablinks = new Array();
  if(!obj['query'] || !obj['query']['pages']) return
  appendCSS('.dablink-found {border: 2px solid #00ff00}\n .dablink .dablink-found {border:2px dashed #00ff00}');
 
  var links = new Array()
  for(var i in obj['query']['pages']) {
    for(var j in obj['query']['pages'][i]['templates']) {
      var tpl = obj['query']['pages'][i]['templates'][j]['title'];
      for(var k=0;k<dabnames.length;k++) {
        if(tpl == dabnames[k]) {
          dablinks[dablinks.length] = obj['query']['pages'][i]['title'];
        }
      }
    }
  }
  if(obj['query']['redirects']) {
    var dablen = dablinks.length   //don't iterate over additions.
    for(var i in obj['query']['redirects']) {
      for(var j=0;j<dablen;j++) {
        if(obj['query']['redirects'][i]['to'] == dablinks[j]) {
          dablinks[dablinks.length] = obj['query']['redirects'][i]['from'];
        }
      }
    }
  }
  var links = (document.getElementById('content')) ? document.getElementById('content').getElementsByTagName('a') : document.getElementById('mw_content').getElementsByTagName('a')
  var found = 0;
  for(var i=0;i<links.length;i++) {
    for(var j=0;j<dablinks.length;j++) {
      //to match "Foo (bar)" with "/wiki/Foo_%28bar%29", have to do some hacky string manipulation
      var dablink = wgArticlePath.replace(/\$1/,'') + escape(dablinks[j].replace(/ /g,'_'));
      if(links[i].href.replace(wgServer,'') == dablink) {
        links[i].className += ' dablink-found';
        found++;
      }
    }
  }
 
  var dab = document.getElementById('t-dab');
  if(dab) {
    removeSpinner('dab');
    dab.appendChild(document.createElement('br'));
    if(found > 0) {
      var span = document.createElement('span');
      span.appendChild(document.createTextNode(found + ' links to disambiguation pages found.'));
      span.className = 'dablink-found';
      dab.appendChild(span);
    } else {
      dab.appendChild(document.createTextNode('No disambiguation links found.'));
    }
  } else {
    alert(found + ' links to disambiguation pages found.');
  }
}
 
function queryString(p) {
  var re = RegExp('[&?]' + p + '=([^&]*)');
  var matches;
  if (matches = re.exec(document.location)) {
    try { 
      return decodeURI(matches[1]);
    } catch (e) {
    }
  }
  return null;
}