Saltar para o conteúdo

MediaWiki:Gadget-desambiguacoes.js: diferenças entre revisões

Origem: Wikipédia, a enciclopédia livre.
Conteúdo apagado Conteúdo adicionado
mw.util.addCSS( '.bkl-link {background: #FF0;}' );
Uso de mw.hook( 'wikipage.content' ).add(...) em vez de $(...)
Linha 213: Linha 213:


if ( mw.config.get( 'wgNamespaceNumber' ) >= 0 ) {
if ( mw.config.get( 'wgNamespaceNumber' ) >= 0 ) {
$( bklCheck.execute );
mw.hook( 'wikipage.content' ).add( bklCheck.execute );
}
}

Revisão das 20h22min de 27 de março de 2014

/*jshint laxbreak:true*/
/*global mw, $ */
/**
 * @source: [[:de:MediaWiki:Gadget-bkl-check.js]]
 * @author: [[:de:User:APPER]]
 * @author: [[:de:User:Codeispoetry]]
 * @author: [[:de:User:PDD]]
 */

var bklCheck = {
	cat : {
		'Categoria:Desambiguação' : {
			className   : 'bkl-link',
			titleAppend : ' (desambiguação)',
			htmlAppend  : '<sup class="bkl-link-sup">desambig</sup>'},
		'Categoria:Desambiguações de nomes' : {
			className   : 'bkl-link',
			titleAppend : ' (desambiguação)',
			htmlAppend  : '<sup class="bkl-link-sup">desambig</sup>'},
		'Categoria:Desambiguações de prenomes' : {
			className   : 'bkl-link',
			titleAppend : ' (desambiguação)',
			htmlAppend  : '<sup class="bkl-link-sup">desambig</sup>'}
	},

	queryUrlView: {
		'format': 'json',
		'action': 'query',
		'prop': 'categories',
		'pageids': mw.config.get( 'wgArticleId' ),
		'generator': 'links',
		'redirects': '',
		'gpllimit': 'max',
		'gplnamespace': '0',
		'cllimit': 'max',
		'indexpageids': '',
		'nocache': mw.config.get( 'wgCurRevisionId' ) //Break client caching, when page has been edited
	},
	queryUrlPreview: {
		'format': 'json',
		'action': 'query',
		'prop': 'categories',
		'redirects': '',
		'cllimit': 'max',
		'indexpageids': ''
	},
	titles            : {},
	count             : 0,
	previewQueryCount : 0,
	execute : function () {
		if ( window.bklCheckOnlyCheckMainNS && mw.config.get( 'wgNamespaceNumber' ) !== 0 ) {
			return;
		}
		mw.util.addCSS( '.bkl-link {background: #FF0;}' );
		//Use &clcategories to reduce needed queries
		var	cats = [], action = mw.config.get( 'wgAction' ),
			name, prev;
		for ( name in bklCheck.cat ) {
			if ( bklCheck.cat[name].className ) {
				cats.push( name );
			}
		}
		bklCheck.queryUrlView.clcategories = cats.join( '|' );
		bklCheck.queryUrlPreview.clcategories = cats.join( '|' );
		if ( action === 'submit' )	{
			bklCheck.doPreviewQueries();
		} else if ( $.inArray(action, ['view', 'historysubmit', 'purge']) > -1 ) {
			$.getJSON( mw.util.wikiScript( 'api' ), bklCheck.queryUrlView, bklCheck.viewResultArrived );
		} else {
			//"Show preview on first edit" enabled?
			prev = document.getElementById( 'wikiPreview' );
			if ( prev && prev.firstChild ) {
				$.getJSON( mw.util.wikiScript( 'api' ), bklCheck.queryUrlView, bklCheck.viewResultArrived );
			}
		}
	},

	storeTitles : function ( res ) {
		if ( !res || !res.query || !res.query.pageids ) {
			return;
		}
		var	q = res.query,
			redirects = {},
			i, j, k, r, page, cat;
		for ( i = 0; q.redirects && i < q.redirects.length; i++ ) {
			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++ ) {
			page = q.pages[q.pageids[i]];
			if ( page.missing === '' || page.ns !== 0 || !page.categories ) {
				continue;
			}
			for ( j = 0; j < page.categories.length; j++ ) {
				cat = bklCheck.cat[page.categories[j].title];
				if ( !cat ) {
					continue;
				}
				bklCheck.count++;
				bklCheck.titles[page.title] = cat;
				if ( !redirects[page.title] ) {
					break;
				}
				for ( 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' ),
			i, cat;
		if ( !links ) {
			return;
		}
		for ( i = 0; i < links.length; i++ ) {
			if ( links[i].className === 'image' ) {
				continue; //Don't mess with images!
			}
			cat = bklCheck.titles[links[i].title];
			if ( !cat ) {
				continue;
			}
			links[i].innerHTML = '<span class="' + cat.className + '" title="' + links[i].title
					+ cat.titleAppend + '">' + links[i].innerHTML	+ cat.htmlAppend + '</span>';
		}
	},

	viewResultArrived : function ( res ) {
		var c;
		bklCheck.storeTitles( res );
		if ( res && res['query-continue'] ) {
			c = res['query-continue'];
			if ( c.categories ) {
				bklCheck.queryUrlView.clcontinue = c.categories.clcontinue;
				$.getJSON( mw.util.wikiScript( 'api' ), bklCheck.queryUrlView, bklCheck.viewResultArrived );
			} else if ( c.links ) {
				bklCheck.queryUrlView.gplcontinue = c.links.gplcontinue;
				$.getJSON( mw.util.wikiScript( 'api' ), bklCheck.queryUrlView, bklCheck.viewResultArrived );
			}
		} else {
			bklCheck.markLinks();
		}
	},

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

	doPreviewQueries : function () {
		var	links = bklCheck.getLinks( 'wikiPreview' ),
			titles = [], unique = {}, siteRegex, namespaceRegex,
			m, i;
		if ( !links ) {
			return;
		}
		siteRegex = new RegExp( $.escapeRE( mw.config.get( 'wgServer' ) + 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
		namespaceRegex = /^((Usuário|Utilizador|Wikipédia|Ficheiro|MediaWiki|Predefinição|Ajuda|Categoria|Portal)(_Discussão)?|Especial|Discussão):/i;
		for ( i = 0; i < links.length; i++ ) {
			if ( !links[i].title || !( 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( m[1].replace( /_/g, '%20' ) ); //Avoid normalization of titles
			if ( titles.length < 50 ) {
				continue;
			}
			new bklCheck.PreviewQuery( titles );
			titles=[];
		}
		if ( titles.length ) {
			new bklCheck.PreviewQuery( titles );
		}
	},

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

bklCheck.PreviewQuery.prototype.doQuery = function ( data ) {
	data.titles = this.titles;
	$.getJSON( mw.util.wikiScript( 'api' ), data, this.resultArrived );
};

bklCheck.PreviewQuery.prototype.resultArrived = function ( res ) {
	bklCheck.storeTitles( res );
	if ( res && res['query-continue'] && res['query-continue'].categories ) {
		bklCheck.queryUrlPreview.clcontinue = res['query-continue'].categories.clcontinue;
		this.doQuery( bklCheck.queryUrlPreview );
	} else {
		bklCheck.previewQueryCount--;
	}
	if ( !bklCheck.previewQueryCount ) {
		bklCheck.markLinks();
	}
};

if ( mw.config.get( 'wgNamespaceNumber' ) >= 0 ) {
	mw.hook( 'wikipage.content' ).add( bklCheck.execute );
}