MediaWiki:Gadget-DetectaDesambiguaciones.js

//=========================================================================== // Basado en http://de.wikipedia.org/wiki/MediaWiki:Gadget-bkl-check.js // obra del usuario APPER // traído y adaptado a es.wikipedia por Poco a poco // Actualizado por Superzerocool, basado en https://de.wikipedia.org/w/index.php?oldid=145531452 //===========================================================================

window.bklCheck = { cat : { 'Categoría:Wikipedia:Desambiguación' : { className  : 'bkl-link bkl-link-inner', //bkl-link-inner kept for b/c reasons titleAppend : ' (desambiguación)', htmlAppend : 'Desam '}, 'Categoría:Wikipedia:Sin relevancia aparente' : { className  : 'bkl-link bkl-link-inner', titleAppend : ' (Sin relevancia aparente)', htmlAppend : 'SRA '} },

queryParamsView     : { action: 'query', format: 'json', prop: 'categories', pageids: mw.config.get('wgArticleId'), generator: 'links', redirects: '', gpllimit: 'max', gplnamespace: 0, cllimit: 'max', indexpageids: '', requestid: mw.config.get('wgCurRevisionId') // Break client caching, when page has been edited },	queryParamsPreview  : { action: 'query', format: 'json', prop: 'categories', cllimit: 'max', redirects: '', indexpageids: '' },	titles           : {}, count            : 0, previewQueryCount : 0,

execute : function { if ( window.bklCheckOnlyCheckMainNS && mw.config.get('wgNamespaceNumber') !== 0 ) return; mw.util.addCSS( '.bkl-link {background-color:#FFFF00;} .mw-disambig-tail {display:none;}' ); // Use &clcategories to reduce needed queries var cats = []; for ( var name in bklCheck.cat ) if ( bklCheck.cat[name].className ) cats.push( name ); bklCheck.queryParamsView.clcategories = cats.join( '|' ); bklCheck.queryParamsPreview.clcategories = cats.join( '|' ); var action = mw.config.get('wgAction'); if ( action === 'submit' )	bklCheck.doPreviewQueries; else if ( action === 'view' || action === 'historysubmit' || action === 'purge' ) bklCheck.doQuery( bklCheck.viewResultArrived, bklCheck.queryParamsView ); else { // "Show preview on first edit" enabled? var prev = document.getElementById( 'wikiPreview' ); if ( prev && prev.firstChild ) bklCheck.doQuery( bklCheck.viewResultArrived, bklCheck.queryParamsView ); }	},

storeTitles : function ( res ) { if ( !res || !res.query || !res.query.pageids ) return; var q = res.query; var redirects = {}; for ( var i = 0; q.redirects && i < q.redirects.length; i++ ) { var r = q.redirects[i]; if ( !redirects[r.to] ) redirects[r.to] = []; redirects[r.to].push( r.from ); }		for ( i = 0; i < q.pageids.length; i++ ) { var page = q.pages[q.pageids[i]]; if ( page.missing === '' || page.ns !== 0 || !page.categories ) continue; for ( var j = 0; j < page.categories.length; j++ ) { var cat = bklCheck.cat[page.categories[j].title]; if ( !cat ) continue; bklCheck.count++; bklCheck.titles[page.title] = cat; if ( !redirects[page.title] ) break; for ( var k = 0; k < redirects[page.title].length; k++ ) bklCheck.titles[redirects[page.title][k]] = cat; break; }		}	},

markLinks : function { if ( !bklCheck.count ) return; var links = bklCheck.getLinks( 'wikiPreview' ) || bklCheck.getLinks( 'bodyContent' ) || bklCheck.getLinks( 'mw_contentholder' ) || bklCheck.getLinks( 'article' ); if ( !links ) return; for ( var i = 0; i < links.length; i++ ) { if ( links[i].className === 'image' || links[i].className.indexOf('external') !== -1 ) continue; // Don't mess with images or external links! var title = links[i].title || ( links[i].childNodes[0] && links[i].childNodes[0].nodeValue ); if ( title ) title = title.charAt(0).toUpperCase + title.slice(1); // make first character uppercase var cat = bklCheck.titles[title]; if ( !cat ) continue; links[i].innerHTML = '' + links[i].innerHTML + cat.htmlAppend + ' '; }	},

viewResultArrived : function ( res ) { bklCheck.storeTitles( res ); if ( res && res['continue'] ) { bklCheck.doQuery( bklCheck.viewResultArrived, bklCheck.queryParamsView, res['continue'] ); } else bklCheck.markLinks; },

PreviewQuery : function ( titles ) { bklCheck.previewQueryCount++; //We have to keep the titles in memory in case we get a query-continue bklCheck.queryParamsPreview.titles = titles.join( '|' ); this.doQuery( bklCheck.resultArrived, bklCheck.queryParamsPreview ); },

doPreviewQueries : function { var links = bklCheck.getLinks( 'wikiPreview' ); if ( !links ) return; var titles=[]; var m;		var unique = {}; var siteRegex = new RegExp( mw.RegExp.escape( mw.config.get( 'wgServer' ) ) + mw.RegExp.escape( mw.config.get( 'wgArticlePath' ).replace( /\$1/, '' ) ) + '([^#]*)' ); //We only care for main ns pages, so we can filter out the most common cases to save some requests var namespaceRegex = /^((Usuario|Wikipedia|Datei|MediaWiki|Vorlage|Hilfe|Categoría|Portal)(_Discusión)?|Spe[cz]ial|Diskussion):/i; for ( var i = 0; i < links.length; i++ ) { if ( !( m = links[i].href.match( siteRegex ) )				|| m[1].match( namespaceRegex ) || unique[m[1]] ) continue; unique[m[1]] = true; // Avoid requesting same title multiple times titles.push( decodeURIComponent( m[1].replace( /_/g, '%20' ) ) ); // Avoid normalization of titles if ( titles.length < 50 ) continue; bklCheck.PreviewQuery( titles ); titles=[]; }		if ( titles.length ) bklCheck.PreviewQuery( titles ); },

getLinks : function ( id ) { var el = document.getElementById( id ); return el && el.getElementsByTagName( 'a' ); } };

bklCheck.doQuery = function ( callback, queryParams, continueParams ) { $.getJSON(		mw.util.wikiScript( 'api' ),		$.extend( {}, queryParams, continueParams || { 'continue': '' } )	).done( function( data ) {		callback( data );	} ).fail( function( code, data ) {		var extraText;		if ( code === 'http' && data ) {			extraText = ( data.xhr && data.xhr.status ? '[' + data.xhr.status + ']' : '' )			+ ( data.textStatus ?  + data.textStatus + ( data.exception ? ': ' :  ) : '' )			+ ( data.exception ?  + data.exception :  );		} else {			extraText = code + ( data && data.error && data.error.info ? ': ' + data.error.info : '' );		}		mw.notify( 'Detector de desambiguación: Error en la recuperación de datos de la API' + ' (' + extraText + ')', { tag: 'bklCheck-error' } );	} ); };

bklCheck.resultArrived = function ( res ) { bklCheck.storeTitles( res ); if ( res && res['continue'] ) { this.doQuery( bklCheck.resultArrived, bklCheck.queryParamsPreview, res['continue'] ); } else bklCheck.previewQueryCount--; if ( !bklCheck.previewQueryCount ) bklCheck.markLinks; };

if ( mw.config.get( 'wgNamespaceNumber' ) >= 0 ) $( bklCheck.execute );