מדיה ויקי:סקריפטים/92.js

מתוך ויקיפדיה, האנציקלופדיה החופשית

הערה: לאחר הפרסום, ייתכן שיהיה צורך לנקות את זיכרון המטמון (cache) של הדפדפן כדי להבחין בשינויים.

  • פיירפוקס / ספארי: להחזיק את המקש Shift בעת לחיצה על טעינה מחדש (Reload) או ללחוץ על צירוף המקשים Ctrl-F5 או Ctrl-R (במחשב מק: ⌘-R).
  • גוגל כרום: ללחוץ על צירוף המקשים Ctrl-Shift-R (במחשב מק: ⌘-Shift-R).
  • אינטרנט אקספלורר / אדג': להחזיק את המקש Ctrl בעת לחיצה על רענן (Refresh) או ללחוץ על צירוף המקשים Ctrl-F5.
  • אופרה: ללחוץ על Ctrl-F5.
/*
replace all the internal links in an article to their chosen language interwiki, when such exists.
it uses 2 windows global variables, with defaults: 
translatelinks_target: chosen language code. defaults to 'en'. 
translatelinks_prompt: text for the menu item to activate the replacement
translatelinks_autoreplace: if set to true, no menu will be created, and the substitution will happen automatically when opening a new page

if you want to use it in any wiki project in any language, call it from [[meta:Special:MyPage/global.js]]
*/

mw.loader.using( ['mediawiki.api', 'mediawiki.util'] ).then ( function() {
$( function() {
	var links = {},
		linkar = [],
		targetLang = window.translatelinks_target || 'en',
		prompt =     window.translatelinks_prompt || 'החלף קישורי שפה',
		autoreplace = window.translatelinks_autoreplace,
		interactive = window.translatelinks_interactive,
		api = new mw.Api();
	
	function getLinks( ) {
		mw.util.$content.find('a').filter(
			function() { 
				return /^\/wiki\//.test( $(this).attr('href') );
			} 
		).each( function() { 
			var $this = $( this ),
				addr = decodeURIComponent( $this.attr( 'href' ).replace(/^\/wiki\//, '') );
			links[addr]  = links[addr] || (function() { linkar.push( addr ); return []; } )() ; 
			links[addr].push( $( this ) );
		} );
	}
	
	function getLangLinks() {
		if ( interactive ) targetLang = window.prompt( interactive, targetLang );
		if ( ! targetLang ) return;
		var params = { redirects: true, prop: 'langlinks', lllang: targetLang, lllimit: 50, llprop: 'url' };
		console.log( 'linkar.length is: ' + linkar.length );
		while ( linkar.length ) {
				local = linkar.splice( 0, 50 );
				params.titles = local.join( "|" );
				console.log( params.titles );
				api.get( params ).done( procesResults /* function( data ) { console.log( data ); } */ );
		}
	}
	
	
	
	function procesResults( data ) {
		if ( data && data.query && data.query.pages ) {
			var pages = data.query.pages,
				redirects = data.query.redirects || [],
				langlink,
				title,
				lntitle,
				redititle,
				processlink = function( a ) {
					a.attr( {
						'href':  langlink.url,
						'title': langlink['*']
					} )
					.addClass( 'interwiki-substituted' ); 
				},
				redirFilter = function( redi ) { 
					return redi.to.replace( / /g, '_' ) == title; 
				},
				appendRedir = function( redi ) {
							redititle = redi.from.replace( / /g, '_' );
							lntitle = lntitle.concat( links[redititle] || [] );
						}; 
							
			for ( var pageid in pages ) {
				var page = pages[pageid];
				
				if ( ! page.langlinks || page.langlinks.length < 1 )
					continue;
				langlink = page.langlinks[0];
				
				redititle = null;
				title = page.title.replace( / /g, '_' );
				lntitle = links[title] || [];
				redirects.filter( redirFilter ).map( appendRedir );
				console.log( 'title is: ' + title + ' found ' + ( lntitle && lntitle.length || '0' ) + ( redititle && ( ' redirects to: ' + redititle ) || '' ) );	
				lntitle.map( processlink );
			}
		}
	}
	
	function doit() {
		mw.util.addCSS('.interwiki-substituted { padding: 2px; border: 1px solid green; }');
		getLinks();
		getLangLinks();
	}
	
	if ( autoreplace === true && ! interactive) 
		$(doit);
	else
		$( mw.util.addPortletLink( 'p-cactions', '#', prompt ) ).click( doit );
} );
} );