MediaWiki:Gadget-DetectorHomonimos.js: Diferenzas entre revisións
Aparencia
Contido eliminado Contido engadido
Migration: importScriptURI → mw.loader.load |
Sync with de:Special:PermaLink/145531452, which does not use sajax_init_object() (deprecated function removed from MediaWiki. See mw:ResourceLoader/Migration guide (users)#Sajax) |
||
Liña 1: | Liña 1: | ||
//=========================================================================== |
//=========================================================================== |
||
// Basado en |
// Basado en https://de.wikipedia.org/wiki/MediaWiki:Gadget-bkl-check.js |
||
// obra do usuario APPER |
// obra do usuario APPER |
||
// Highlight links to disambiguation pages with the CSS class 'bkl-link', |
|||
// traído e adaptado da es.wikipedia |
|||
// In both the normal page view and in the preview |
|||
//=========================================================================== |
//=========================================================================== |
||
// Example: [[Antrazith]] (misspelling) -> [[Anthrazit]] (disambiguation) |
|||
window.bklCheck = { |
window.bklCheck = { |
||
cat : { |
cat : { |
||
'Categoría:Homónimos' : { |
'Categoría:Homónimos' : { |
||
className : 'bkl-link bkl-link-inner', //bkl-link-inner kept for b/c reasons |
className : 'bkl-link bkl-link-inner', // bkl-link-inner kept for b/c reasons |
||
titleAppend : ' (homónimos)', |
titleAppend : ' (homónimos)', |
||
htmlAppend : '<sup class="bkl-link-sup">HOMÓNIMOS</sup>'}, |
htmlAppend : '<sup class="bkl-link-sup">HOMÓNIMOS</sup>'}, |
||
Liña 17: | Liña 20: | ||
}, |
}, |
||
queryParamsView : { |
|||
queryUrlView : mw.util.wikiScript('api') + '?action=query&format=json' |
|||
action: 'query', |
|||
+ '&prop=categories&pageids=' + mw.config.get( 'wgArticleId' ) + '&callback=bklCheck.viewResultArrived' |
|||
⚫ | |||
+ '&generator=links&redirects&gpllimit=max&gplnamespace=0&cllimit=max&indexpageids' |
|||
pageids: mw.config.get('wgArticleId'), |
|||
generator: 'links', |
|||
queryUrlPreview : mw.util.wikiScript('api') + '?action=query&format=json&prop=categories' |
|||
redirects: '', |
|||
+ '&cllimit=max&redirects&indexpageids', |
|||
gpllimit: 'max', |
|||
gplnamespace: 0, |
|||
cllimit: 'max', |
|||
indexpageids: '', |
|||
requestid: mw.config.get('wgCurRevisionId') // Break client caching, when page has been edited |
|||
⚫ | |||
queryParamsPreview : { |
|||
action: 'query', |
|||
prop: 'categories', |
|||
cllimit: 'max', |
|||
redirects: '', |
|||
indexpageids: '' |
|||
}, |
|||
titles : {}, |
titles : {}, |
||
count : 0, |
count : 0, |
||
Liña 28: | Liña 44: | ||
execute : function () { |
execute : function () { |
||
if ( window.bklCheckOnlyCheckMainNS && mw.config.get( |
if ( window.bklCheckOnlyCheckMainNS && mw.config.get('wgNamespaceNumber') !== 0 ) return; |
||
//Use &clcategories to reduce needed queries |
var api = new mw.Api(); |
||
// Use &clcategories to reduce needed queries |
|||
var cats = []; |
var cats = []; |
||
for ( var name in bklCheck.cat ) if ( bklCheck.cat[name].className ) |
for ( var name in bklCheck.cat ) if ( bklCheck.cat[name].className ) |
||
cats.push |
cats.push( name ); |
||
bklCheck. |
bklCheck.queryParamsView.clcategories = cats.join( '|' ); |
||
bklCheck. |
bklCheck.queryParamsPreview.clcategories = cats.join( '|' ); |
||
var action = mw.config.get('wgAction'); |
|||
if ( action === 'submit' ) bklCheck.doPreviewQueries( api ); |
|||
else if ( |
else if ( action === 'view' || action === 'historysubmit' || action === 'purge' ) |
||
bklCheck.doQuery( api, bklCheck.viewResultArrived, bklCheck.queryParamsView ); |
|||
else {//"Show preview on first edit" enabled? |
else { // "Show preview on first edit" enabled? |
||
var prev = document.getElementById( 'wikiPreview' ); |
var prev = document.getElementById( 'wikiPreview' ); |
||
if ( prev && prev.firstChild ) |
if ( prev && prev.firstChild ) bklCheck.doQuery( api, bklCheck.viewResultArrived, bklCheck.queryParamsView ); |
||
} |
} |
||
//Make sure that our style is put before other css so users can override it easily |
|||
//var head = document.getElementsByTagName( 'head' )[0]; |
|||
//head.insertBefore( mw.util.addCSS( '.bkl-link {background-color:#FFFF00;}' ), head.firstChild ); |
|||
mw.util.addCSS( '.bkl-link {background-color:#FFFF00;}' ); |
|||
}, |
}, |
||
Liña 57: | Liña 71: | ||
redirects[r.to].push( r.from ); |
redirects[r.to].push( r.from ); |
||
} |
} |
||
for ( |
for ( i = 0; i < q.pageids.length; i++ ) { |
||
var page = q.pages[q.pageids[i]]; |
var page = q.pages[q.pageids[i]]; |
||
if ( page.missing === '' || page.ns !== 0 || !page.categories ) continue; |
if ( page.missing === '' || page.ns !== 0 || !page.categories ) continue; |
||
Liña 79: | Liña 93: | ||
if ( !links ) return; |
if ( !links ) return; |
||
for ( var i = 0; i < links.length; i++ ) { |
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! |
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 ); |
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 |
if ( title ) title = title.charAt(0).toUpperCase() + title.slice(1); // make first character uppercase |
||
Liña 85: | Liña 99: | ||
if ( !cat ) continue; |
if ( !cat ) continue; |
||
links[i].innerHTML = '<span class="' + cat.className + '" title="' + |
links[i].innerHTML = '<span class="' + cat.className + '" title="' + |
||
mw.html.escape(title + cat.titleAppend) + '">' + links[i].innerHTML + cat.htmlAppend + '</span>'; |
|||
} |
} |
||
}, |
}, |
||
viewResultArrived : function ( res ) { |
viewResultArrived : function ( api, res ) { |
||
bklCheck.storeTitles( res ); |
bklCheck.storeTitles( res ); |
||
if ( res && res[' |
if ( res && res['continue'] ) { |
||
bklCheck.doQuery( api, bklCheck.viewResultArrived, bklCheck.queryParamsView, res['continue'] ); |
|||
⚫ | |||
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(); |
} else bklCheck.markLinks(); |
||
}, |
}, |
||
PreviewQuery : function ( titles ) { |
PreviewQuery : function ( api, titles ) { |
||
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 |
||
bklCheck.queryParamsPreview.titles = titles.join( '|' ); |
|||
this.doQuery( bklCheck. |
this.doQuery( api, bklCheck.resultArrived, bklCheck.queryParamsPreview ); |
||
}, |
}, |
||
doPreviewQueries : function () { |
doPreviewQueries : function ( api ) { |
||
var links = bklCheck.getLinks( 'wikiPreview' ); |
var links = bklCheck.getLinks( 'wikiPreview' ); |
||
if ( !links ) return; |
if ( !links ) return; |
||
var titles=[]; var m; |
var titles=[]; var m; |
||
var unique = {}; |
var unique = {}; |
||
⚫ | |||
var rxEscape = function(s) {return s.replace( /([\/\.\*\+\?\|\(\)\[\]\{\}\\])/g, '\\$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 = /^((User|Wikipedia|File|MediaWiki|Template|Help|Category|Portal)(_talk)?|Special|Talk):/i; |
var namespaceRegex = /^((User|Wikipedia|File|MediaWiki|Template|Help|Category|Portal)(_talk)?|Special|Talk):/i; |
||
Liña 123: | Liña 128: | ||
if ( !( m = links[i].href.match( siteRegex ) ) |
if ( !( 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( decodeURIComponent( m[1].replace( /_/g, '%20' ) ) ); // Avoid normalization of titles |
||
if ( titles.length < 50 ) continue; |
if ( titles.length < 50 ) continue; |
||
bklCheck.PreviewQuery( api, titles ); |
|||
titles=[]; |
titles=[]; |
||
} |
} |
||
if ( titles.length ) |
if ( titles.length ) bklCheck.PreviewQuery( api, titles ); |
||
}, |
}, |
||
Liña 138: | Liña 143: | ||
}; |
}; |
||
bklCheck |
bklCheck.doQuery = function ( api, callback, queryParams, continueParams ) { |
||
api.get( |
|||
var q = this; |
|||
$.extend( {}, queryParams, continueParams || { 'continue': '' } ) |
|||
var req = sajax_init_object(); /* FIXME: see [[ResourceLoader/Migration guide (users)#Sajax]] */ |
|||
).then( function( data ) { |
|||
callback( api, data ); |
|||
} ).fail( function( code, data ) { |
|||
req.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' ); |
|||
var extraText; |
|||
req.onreadystatechange = function () { |
|||
if ( |
if ( code === 'http' && data ) { |
||
extraText = ( data.xhr && data.xhr.status ? '[' + data.xhr.status + ']' : '' ) |
|||
+ ( data.textStatus ? '' + data.textStatus + ( data.exception ? ': ' : '' ) : '' ) |
|||
⚫ | |||
+ ( data.exception ? '' + data.exception : '' ); |
|||
req.send( q.data ); |
|||
} else { |
|||
delete req; |
|||
extraText = code + ( data && data.error && data.error.info ? ': ' + data.error.info : '' ); |
|||
⚫ | |||
mw.notify( |
|||
'BKL-Check: Fehler beim Ermitteln der Begriffsklärungen' + |
|||
' (' + extraText + ')', |
|||
{ tag: 'bklCheck-error' } |
|||
); |
|||
} ); |
|||
}; |
}; |
||
bklCheck |
bklCheck.resultArrived = function ( api, res ) { |
||
bklCheck.storeTitles( res ); |
bklCheck.storeTitles( res ); |
||
if ( res && res[' |
if ( res && res['continue'] ) { |
||
this.doQuery( bklCheck. |
this.doQuery( api, bklCheck.resultArrived, bklCheck.queryParamsPreview, res['continue'] ); |
||
+ encodeURIComponent( res['query-continue'].categories.clcontinue ) ); |
|||
} else bklCheck.previewQueryCount--; |
} else bklCheck.previewQueryCount--; |
||
if ( !bklCheck.previewQueryCount ) bklCheck.markLinks(); |
if ( !bklCheck.previewQueryCount ) bklCheck.markLinks(); |
Revisión como estaba o 29 de agosto de 2015 ás 13:01
//===========================================================================
// Basado en https://de.wikipedia.org/wiki/MediaWiki:Gadget-bkl-check.js
// obra do usuario APPER
// Highlight links to disambiguation pages with the CSS class 'bkl-link',
// In both the normal page view and in the preview
//===========================================================================
// Example: [[Antrazith]] (misspelling) -> [[Anthrazit]] (disambiguation)
window.bklCheck = {
cat : {
'Categoría:Homónimos' : {
className : 'bkl-link bkl-link-inner', // bkl-link-inner kept for b/c reasons
titleAppend : ' (homónimos)',
htmlAppend : '<sup class="bkl-link-sup">HOMÓNIMOS</sup>'},
'Categoría:Sen relevancia aparente' : {
className : 'bkl-link bkl-link-inner',
titleAppend : ' (Sen relevancia aparente)',
htmlAppend : '<sup class="bkl-link-sup">SRA</sup>'}
},
queryParamsView : {
action: 'query',
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',
prop: 'categories',
cllimit: 'max',
redirects: '',
indexpageids: ''
},
titles : {},
count : 0,
previewQueryCount : 0,
execute : function () {
if ( window.bklCheckOnlyCheckMainNS && mw.config.get('wgNamespaceNumber') !== 0 ) return;
var api = new mw.Api();
// 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( api );
else if ( action === 'view' || action === 'historysubmit' || action === 'purge' )
bklCheck.doQuery( api, bklCheck.viewResultArrived, bklCheck.queryParamsView );
else { // "Show preview on first edit" enabled?
var prev = document.getElementById( 'wikiPreview' );
if ( prev && prev.firstChild ) bklCheck.doQuery( api, 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 = '<span class="' + cat.className + '" title="' +
mw.html.escape(title + cat.titleAppend) + '">' + links[i].innerHTML + cat.htmlAppend + '</span>';
}
},
viewResultArrived : function ( api, res ) {
bklCheck.storeTitles( res );
if ( res && res['continue'] ) {
bklCheck.doQuery( api, bklCheck.viewResultArrived, bklCheck.queryParamsView, res['continue'] );
} else bklCheck.markLinks();
},
PreviewQuery : function ( api, 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( api, bklCheck.resultArrived, bklCheck.queryParamsPreview );
},
doPreviewQueries : function ( api ) {
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 = /^((User|Wikipedia|File|MediaWiki|Template|Help|Category|Portal)(_talk)?|Special|Talk):/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( api, titles );
titles=[];
}
if ( titles.length ) bklCheck.PreviewQuery( api, titles );
},
getLinks : function ( id ) {
var el = document.getElementById( id );
return el && el.getElementsByTagName( 'a' );
}
};
bklCheck.doQuery = function ( api, callback, queryParams, continueParams ) {
api.get(
$.extend( {}, queryParams, continueParams || { 'continue': '' } )
).then( function( data ) {
callback( api, 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(
'BKL-Check: Fehler beim Ermitteln der Begriffsklärungen' +
' (' + extraText + ')',
{ tag: 'bklCheck-error' }
);
} );
};
bklCheck.resultArrived = function ( api, res ) {
bklCheck.storeTitles( res );
if ( res && res['continue'] ) {
this.doQuery( api, bklCheck.resultArrived, bklCheck.queryParamsPreview, res['continue'] );
} else bklCheck.previewQueryCount--;
if ( !bklCheck.previewQueryCount ) bklCheck.markLinks();
};
if ( mw.config.get( 'wgNamespaceNumber' ) >= 0 ) $( bklCheck.execute );