MediaWiki:Gadget-desambiguacoes.js e Ajuda:Página de testes: diferenças entre as páginas

Origem: Wikipédia, a enciclopédia livre.
(Diferenças entre páginas)
Conteúdo apagado Conteúdo adicionado
teste
 
Migração para o MW 1.17: importScriptURImw.loader.load; addOnloadHook$; wg*mw.config.get('wg*'); $(...); +$.inArray; rxEscape$.escapeRE; trad. domínios; +{...} nos if's e loops; agrupamento de var's; -erros (jshint.com)
 
Linha 1: Linha 1:
/**
* @source: [[:de:MediaWiki:Gadget-bkl-check.js]]
* @author: [[:de:User:APPER]]
* @author: [[:de:User:Codeispoetry]]
* @author: [[:de:User:PDD]]
*/

var bklCheck = {
var bklCheck = {
cat : {
cat : {
Linha 9: Linha 16:
titleAppend : ' (desambiguação)',
titleAppend : ' (desambiguação)',
htmlAppend : '<sup class="bkl-link-sup">desambig</sup>'},
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 : wgScriptPath + '/api.php?action=query&format=json'
queryUrlView : mw.config.get( 'wgScriptPath' ) + '/api.php?action=query&format=json'
+ '&prop=categories&pageids=' + wgArticleId + '&callback=bklCheck.viewResultArrived'
+ '&prop=categories&pageids=' + mw.config.get( 'wgArticleId' ) + '&callback=bklCheck.viewResultArrived'
+ '&generator=links&redirects&gpllimit=max&gplnamespace=0&cllimit=max&indexpageids'
+ '&generator=links&redirects&gpllimit=max&gplnamespace=0&cllimit=max&indexpageids'
+ '&nocache=' + wgCurRevisionId, //Break client caching, when page has been edited
+ '&nocache=' + mw.config.get( 'wgCurRevisionId' ), //Break client caching, when page has been edited
queryUrlPreview : wgScriptPath + '/api.php?action=query&format=json&prop=categories'
queryUrlPreview : mw.config.get( 'wgScriptPath' ) + '/api.php?action=query&format=json&prop=categories'
+ '&cllimit=max&redirects&indexpageids',
+ '&cllimit=max&redirects&indexpageids',
titles : {},
titles : {},
Linha 22: Linha 33:


execute : function () {
execute : function () {
if ( window.bklCheckOnlyCheckMainNS && wgNamespaceNumber != 0 ) return;
if ( window.bklCheckOnlyCheckMainNS && mw.config.get( 'wgNamespaceNumber' ) !== 0 ) {
return;
}
//Use &clcategories to reduce needed queries
//Use &clcategories to reduce needed queries
var cats = [];
var cats = [], action = mw.config.get( 'wgAction' ),
for ( var name in bklCheck.cat ) if ( bklCheck.cat[name].className )
name, prev, head;
for ( name in bklCheck.cat ) {
if ( bklCheck.cat[name].className ) {
cats.push( encodeURIComponent( name ) );
cats.push( encodeURIComponent( name ) );
}
}
bklCheck.queryUrlView += '&clcategories=' + cats.join( '|' );
bklCheck.queryUrlView += '&clcategories=' + cats.join( '|' );
bklCheck.queryUrlPreview += '&clcategories=' + cats.join( '|' );
bklCheck.queryUrlPreview += '&clcategories=' + cats.join( '|' );
if ( wgAction == 'submit' ) bklCheck.doPreviewQueries();
if ( action === 'submit' ) {
bklCheck.doPreviewQueries();
else if ( wgAction == 'view' || wgAction == 'historysubmit' || wgAction == 'purge' )
} else if ( $.inArray(action, ['view', 'historysubmit', 'purge']) > -1 ) {
importScriptURI( bklCheck.queryUrlView );
mw.loader.load( bklCheck.queryUrlView );
else {//"Show preview on first edit" enabled?
} else {
//"Show preview on first edit" enabled?
var prev = document.getElementById( 'wikiPreview' );
prev = document.getElementById( 'wikiPreview' );
if ( prev && prev.firstChild ) importScriptURI( bklCheck.queryUrlView );
if ( prev && prev.firstChild ) {
mw.loader.load( bklCheck.queryUrlView );
}
}
}
//Make sure that our style is put before other css so users can override it easily
//Make sure that our style is put before other css so users can override it easily
var head = document.getElementsByTagName( 'head' )[0];
head = document.getElementsByTagName( 'head' )[0];
head.insertBefore( appendCSS( '.bkl-link {background-color:#FFFF00;}' ), head.firstChild );
head.insertBefore( appendCSS( '.bkl-link {background-color:#FFFF00;}' ), head.firstChild );
},
},


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


markLinks : function () {
markLinks : function () {
if ( !bklCheck.count ) return;
if ( !bklCheck.count ) {
return;
}
var links = bklCheck.getLinks( 'wikiPreview' ) || bklCheck.getLinks( 'bodyContent' )
var links = bklCheck.getLinks( 'wikiPreview' ) || bklCheck.getLinks( 'bodyContent' )
|| bklCheck.getLinks( 'mw_contentholder' ) || bklCheck.getLinks( 'article' );
|| bklCheck.getLinks( 'mw_contentholder' ) || bklCheck.getLinks( 'article' ),
i, cat;
if ( !links ) return;
for ( var i = 0; i < links.length; i++ ) {
if ( !links ) {
return;
if ( links[i].className == 'image' ) continue; //Don't mess with images!
}
var cat = bklCheck.titles[links[i].title];
for ( i = 0; i < links.length; i++ ) {
if ( !cat ) continue;
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
links[i].innerHTML = '<span class="' + cat.className + '" title="' + links[i].title
+ cat.titleAppend + '">' + links[i].innerHTML + cat.htmlAppend + '</span>';
+ cat.titleAppend + '">' + links[i].innerHTML + cat.htmlAppend + '</span>';
Linha 81: Linha 123:


viewResultArrived : function ( res ) {
viewResultArrived : function ( res ) {
var c;
bklCheck.storeTitles( res );
bklCheck.storeTitles( res );
if ( res && res['query-continue'] ) {
if ( res && res['query-continue'] ) {
var c = res['query-continue'];
c = res['query-continue'];
if ( c.categories ) {
if ( c.categories ) {
importScriptURI( bklCheck.queryUrlView + '&clcontinue='
mw.loader.load( bklCheck.queryUrlView + '&clcontinue='
+ encodeURIComponent( c.categories.clcontinue ) );
+ encodeURIComponent( c.categories.clcontinue ) );
} else if ( c.links ) {
} else if ( c.links ) {
bklCheck.queryUrlView = bklCheck.queryUrlView.replace( /&gplcontinue=.*|$/,
bklCheck.queryUrlView = bklCheck.queryUrlView.replace( /&gplcontinue=.*|$/,
'&gplcontinue=' + encodeURIComponent( c.links.gplcontinue ) );
'&gplcontinue=' + encodeURIComponent( c.links.gplcontinue ) );
importScriptURI( bklCheck.queryUrlView );
mw.loader.load( bklCheck.queryUrlView );
}
}
} else bklCheck.markLinks();
} else {
bklCheck.markLinks();
}
},
},


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


doPreviewQueries : function () {
doPreviewQueries : function () {
var links = bklCheck.getLinks( 'wikiPreview' );
var links = bklCheck.getLinks( 'wikiPreview' ),
titles = [], unique = {}, siteRegex, namespaceRegex,
if ( !links ) return;
m, i;
var titles=[]; var m;
var unique = {};
if ( !links ) {
return;
var rxEscape = function(s) {return s.replace( /([\/\.\*\+\?\|\(\)\[\]\{\}\\])/g, '\\$1' );};
}
var siteRegex = new RegExp( rxEscape( wgServer ) + rxEscape( wgArticlePath.replace( /\$1/, '' ) ) + '([^#]*)' );
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
//We only care for main ns pages, so we can filter out the most common cases to save some requests
var namespaceRegex = /^((Benutzer|Wikipedia|Datei|MediaWiki|Vorlage|Hilfe|Kategorie|Portal)(_Diskussion)?|Spe[cz]ial|Diskussion):/i;
namespaceRegex = /^((Usuário|Utilizador|Wikipédia|Ficheiro|MediaWiki|Predefinição|Ajuda|Categoria|Portal)(_Discussão)?|Especial|Discussão):/i;
for ( var i = 0; i < links.length; i++ ) {
for ( i = 0; i < links.length; i++ ) {
if ( !links[i].title || !( m = links[i].href.match( siteRegex ) )
if ( !links[i].title || !( m = links[i].href.match( siteRegex ) )
|| m[1].match( namespaceRegex ) || unique[m[1]] ) continue;
|| m[1].match( namespaceRegex ) || unique[m[1]] ) {
continue;
}
unique[m[1]] = true; //Avoid requesting same title multiple times
unique[m[1]] = true; //Avoid requesting same title multiple times
titles.push( m[1].replace( /_/g, '%20' ) ); //Avoid normalization of titles
titles.push( m[1].replace( /_/g, '%20' ) ); //Avoid normalization of titles
if ( titles.length < 50 ) continue;
if ( titles.length < 50 ) {
continue;
}
new bklCheck.PreviewQuery( titles );
new bklCheck.PreviewQuery( titles );
titles=[];
titles=[];
}
}
if ( titles.length ) new bklCheck.PreviewQuery( titles );
if ( titles.length ) {
new bklCheck.PreviewQuery( titles );
}
},
},


Linha 130: Linha 182:


bklCheck.PreviewQuery.prototype.doQuery = function ( url ) {
bklCheck.PreviewQuery.prototype.doQuery = function ( url ) {
var q = this;
var q = this,
var req = sajax_init_object();
req = sajax_init_object();
if ( !req ) return;
if ( !req ) {
return;
}
req.open( 'POST', url, true );
req.open( 'POST', url, true );
req.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
req.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
req.onreadystatechange = function () {
req.onreadystatechange = function () {
if ( req.readyState == 4 && req.status == 200 )
if ( req.readyState === 4 && req.status === 200 ) {
eval( 'q.resultArrived(' + req.responseText + ');' );
eval( 'q.resultArrived(' + req.responseText + ');' );
}
};
};
req.send( q.data );
req.send( q.data );
delete req;
};
};


Linha 148: Linha 202:
this.doQuery( bklCheck.queryUrlPreview + '&clcontinue='
this.doQuery( bklCheck.queryUrlPreview + '&clcontinue='
+ encodeURIComponent( res['query-continue'].categories.clcontinue ) );
+ encodeURIComponent( res['query-continue'].categories.clcontinue ) );
} else bklCheck.previewQueryCount--;
} else {
bklCheck.previewQueryCount--;
}
if ( !bklCheck.previewQueryCount ) bklCheck.markLinks();
if ( !bklCheck.previewQueryCount ) {
bklCheck.markLinks();
}
};
};


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

Revisão das 15h27min de 4 de março de 2011

/**

* @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  : 'desambig'}, 'Categoria:Desambiguações de nomes' : { className  : 'bkl-link', titleAppend : ' (desambiguação)', htmlAppend  : 'desambig'}, 'Categoria:Desambiguações de prenomes' : { className  : 'bkl-link', titleAppend : ' (desambiguação)', htmlAppend  : 'desambig'} },

queryUrlView  : mw.config.get( 'wgScriptPath' ) + '/api.php?action=query&format=json' + '&prop=categories&pageids=' + mw.config.get( 'wgArticleId' ) + '&callback=bklCheck.viewResultArrived' + '&generator=links&redirects&gpllimit=max&gplnamespace=0&cllimit=max&indexpageids' + '&nocache=' + mw.config.get( 'wgCurRevisionId' ), //Break client caching, when page has been edited queryUrlPreview  : mw.config.get( 'wgScriptPath' ) + '/api.php?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; } //Use &clcategories to reduce needed queries var cats = [], action = mw.config.get( 'wgAction' ), name, prev, head; for ( name in bklCheck.cat ) { if ( bklCheck.cat[name].className ) { cats.push( encodeURIComponent( name ) ); } } bklCheck.queryUrlView += '&clcategories=' + cats.join( '|' ); bklCheck.queryUrlPreview += '&clcategories=' + cats.join( '|' ); if ( action === 'submit' ) { bklCheck.doPreviewQueries(); } else if ( $.inArray(action, ['view', 'historysubmit', 'purge']) > -1 ) { mw.loader.load( bklCheck.queryUrlView ); } else { //"Show preview on first edit" enabled? prev = document.getElementById( 'wikiPreview' ); if ( prev && prev.firstChild ) { mw.loader.load( bklCheck.queryUrlView ); } } //Make sure that our style is put before other css so users can override it easily head = document.getElementsByTagName( 'head' )[0]; head.insertBefore( appendCSS( '.bkl-link {background-color:#FFFF00;}' ), head.firstChild ); },

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 = '' + links[i].innerHTML + cat.htmlAppend + ''; } },

viewResultArrived : function ( res ) { var c; bklCheck.storeTitles( res ); if ( res && res['query-continue'] ) { c = res['query-continue']; if ( c.categories ) { mw.loader.load( bklCheck.queryUrlView + '&clcontinue=' + encodeURIComponent( c.categories.clcontinue ) ); } else if ( c.links ) { bklCheck.queryUrlView = bklCheck.queryUrlView.replace( /&gplcontinue=.*|$/, '&gplcontinue=' + encodeURIComponent( c.links.gplcontinue ) ); mw.loader.load( bklCheck.queryUrlView ); } } else { bklCheck.markLinks(); } },

PreviewQuery : function ( titles ) { bklCheck.previewQueryCount++; //We have to keep the titles in memory in case we get a query-continue this.data = '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 ( url ) { var q = this, req = sajax_init_object(); if ( !req ) { return; } req.open( 'POST', url, true ); req.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' ); req.onreadystatechange = function () { if ( req.readyState === 4 && req.status === 200 ) { eval( 'q.resultArrived(' + req.responseText + ');' ); } }; req.send( q.data ); };

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

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