MediaWiki:Gadget-false-blue-links.js: Różnice pomiędzy wersjami

Z Wikisłownika – wolnego słownika wielojęzycznego
Usunięta treść Dodana treść
brak obiektu data.query (undefined) w Specjalna:Niezmienny link/4708105
hasla zakwalifikowane jako 'termin obcy w j. pol.' umieszczalo pod id pl-foreign zgodnie z MediaWiki:Gadget-langdata.js, jednak linki do tych sekcji sa automatycznie przekierowywane do #pl -> utnij koncowe '-foreign'
Linia 67: Linia 67:
if ( iso ) {
if ( iso ) {
iso = iso.replace( '-foreign', '' );
fbl.langSections[ title ] = fbl.langSections[ title ] || {};
fbl.langSections[ title ] = fbl.langSections[ title ] || {};
fbl.langSections[ title ][ iso ] = true;
fbl.langSections[ title ][ iso ] = true;
Linia 76: Linia 77:
function extractSelfSections( pageName, $content ) {
function extractSelfSections( pageName, $content ) {
$content.find( '.primary-lang-code' ).each( function () {
$content.find( '.primary-lang-code' ).each( function () {
if ( this.id ) {
var id = this.id;
if ( id ) {
id = id.replace( '-foreign', '' );
fbl.langSections[ pageName ] = fbl.langSections[ pageName ] || {};
fbl.langSections[ pageName ] = fbl.langSections[ pageName ] || {};
fbl.langSections[ pageName ][ this.id ] = true;
fbl.langSections[ pageName ][ id ] = true;
}
}
} );
} );

Wersja z 01:42, 1 lip 2015

/**
 * Zaznacza innym kolorem linki do nieistniejących sekcji językowych istniejących haseł.
 * Przeróbka autorstwa [[User:Olaf]] skryptu [[User:Beau]] do zabarwiania disambigów:
 *  https://pl.wikipedia.org/wiki/MediaWiki:Gadget-mark-disambigs.js
 * Dalsze modyfikacje: [[User:Peter Bowman]]
 */

var fbl = mw.libs.falseBlueLinks = {
	langSections: {},
	
	ignoredClasses: [
		'.new',
		'.extiw',
		'.external',
		'.internal',
		'.image',
		'.mw-redirect',
		'.cancelLink'
	]
};

var api, worklist, languageCodes, pathPrefix;

function makeRequest( cont ) {
	var query = $.extend( {
		titles:       mw.config.get( 'wgPageName' ),
		prop:        'categories',
		cllimit:     'max',
		clshow:      '!hidden',
		generator:   'links',
		gpllimit:    'max',
		gplnamespace: 0,
		redirects:    1,
		'continue':   ''
	}, cont );
	
	return api.get( query ).then( function ( data ) {
		$.each( data.query && data.query.pages || [], function () {
			if ( !this.categories ) {
				return true;
			}
			
			worklist[ this.title ] = ( worklist[ this.title ] || [] )
				.concat( $.map( this.categories, function ( v ) {
					return v.title.slice( 10 ); // 'Kategoria:'.length
				} ) );
		} );
		
		if ( data[ 'continue' ] ) {
			return makeRequest( data[ 'continue' ] );
		}
	} );
}

function extractLangSections() {
	var re = /^(.*?) \((?:indeks|formy fleksyjne)\)$/;
	
	$.each( worklist, function ( title, categories ) {
		$.each( categories, function ( i, cat ) {
			var iso, m = cat.match( re );
			
			if ( !m || !m[ 1 ] ) {
				return true;
			}
			
			iso = languageCodes[ m[ 1 ] ];
			
			if ( iso ) {
				iso = iso.replace( '-foreign', '' );
				fbl.langSections[ title ] = fbl.langSections[ title ] || {};
				fbl.langSections[ title ][ iso ] = true;
			}
		} );
	} );
}

function extractSelfSections( pageName, $content ) {
	$content.find( '.primary-lang-code' ).each( function () {
		var id = this.id;
		
		if ( id ) {
			id = id.replace( '-foreign', '' );
			fbl.langSections[ pageName ] = fbl.langSections[ pageName ] || {};
			fbl.langSections[ pageName ][ id ] = true;
		}
	} );
}

function decodeFragment( s ) {
	return decodeURIComponent(
		s.replace( /\./g, '%' ).replace( /_/g, ' ' )
	);
}

function extractArticleName( s ) {
	return decodeURI(
		s.replace( pathPrefix, '' ).replace( /_/g, ' ' )
	);
}

function filterLinks( i, link ) {
	var title, fragment, m, iso;
	var uri = new mw.Uri( link.getAttribute( 'href' ) );
	
	if (
		!uri.fragment ||
		!$.isEmptyObject( uri.query ) ||
		uri.path.indexOf( ':' ) !== -1
	) {
		return false;
	}
	
	title = extractArticleName( uri.path );
	fragment = decodeFragment( uri.fragment );
	m = fragment.match( new RegExp( '^' + title + ' \\((?:język )?(.+)\\)$' ) );
	
	iso = ( m && m[ 1 ] )
		? languageCodes[ m[ 1 ] ]
		: fragment;
	
	return (
		iso &&
		fbl.langSections[ title ] &&
		!fbl.langSections[ title ][ iso ] &&
		!fbl.langSections[ title ].inter &&
		!(
			( iso === 'zh' || iso === 'ja' ) &&
			!fbl.langSections[ title ][ 'zh-char' ]
		)
	);
}

function onPageReady() {
	var pageName = mw.config.get( 'wgPageName' ).replace( /_/g, ' ' );
	var $content = $( '#mw-content-text' );
	
	if (
		!fbl.langSections[ pageName ] &&
		$content.find( '.selflink' ).length
	) {
		extractSelfSections( pageName, $content );
	}
	
	if ( !$.isEmptyObject( fbl.langSections ) ) {
		$content
			.find( mw.format(
				'a[href^="$1"]:not($2)',
				pathPrefix,
				fbl.ignoredClasses.join( ', ' )
			) )
			.filter( filterLinks )
			.addClass( 'mw-false-blue' );
	}
}

if (
	mw.config.get( 'wgAction' ) === 'view' &&
	mw.config.get( 'wgNamespaceNumber' ) >= 0 &&
	mw.config.get( 'wgNamespaceNumber' ) !== 14 // Kategoria
) {
	api = new mw.Api();
	worklist = {};
	languageCodes = mw.config.get( 'lang2code' );
	pathPrefix = mw.format( mw.config.get( 'wgArticlePath' ), '' );
	
	makeRequest().done( extractLangSections ).done( function () {
		$( onPageReady );
	} );
}