Kasutaja:Wkentaur/backlinks.js

Allikas: Vikipeedia

Märkus: Võimalik, et pärast avaldamist tuleb muudatuste nägemiseks brauseri puhver tühjendada.

  • Firefox / Safari: Hoia all Shift-klahvi ja klõpsa Laadi uuesti või vajuta kas Ctrl-F5 või Ctrl-R (Macis ⌘-R).
  • Google Chrome: Vajuta Ctrl-Shift-R (Macis ⌘-Shift-R).
  • Internet Explorer / Edge: Hoia all Ctrl-klahvi ja klõpsa Värskenda või vajuta Ctrl-F5.
  • Opera: Vajuta Ctrl-F5.
// Kuvab artiklile linkivad lehed menüüs

(function($,mw) {
	var txt = {};

	var install = function() {
		if(mw.config.get("wgNamespaceNumber") === 0) {
			mw.loader.using( ['mediawiki.Title', 'mediawiki.api'], function() {
				$( document ).ready( function() {
					page = mw.config.get("wgTitle");
					mw.util.addPortletLink( 'p-tb', '//et.wikipedia.org/wiki/Eri:Lingid_siia/'+page, '// Lingid siia //' );
					
					getBacklinks( page ).done( function( backlinks, pageTitles ) {
                		for (var i = 0; i < backlinks.length; i++) {
                    		backlink = backlinks[i];
                    		mw.util.addPortletLink("p-tb", "//et.wikipedia.org/wiki/"+backlink, backlink, '', backlink);
                		}
					} ).fail( error );
			
				} );
			} );
		}
	}
 		
	/*
	 * Return the 'canonical title' of a page
	 */
	var getCanonicalTitle = function( title ) {
		try {
			title = new mw.Title( title ).getPrefixedText();
		} catch ( ex ) {
			// mw.Title seems to be buggy, and some valid titles are rejected
			// FIXME: This may cause false negatives	
		}
		return title;
	};

	/*
	 * Display an error message
	 */
	var error = function( errorDescription ) {
		var errorBox = $( '<div></div>' ).addClass( 'disamassist-box disamassist-errorbox' );
		errorBox.text( txt.error.replace( '$1', errorDescription ) );
		errorBox.append( createButton( txt.dismissError, function() {
			errorBox.fadeOut();
		} ).addClass( 'disamassist-errorbutton' ) );
		var uiIsInPlace = ui && $.contains( document.documentElement, ui.display[0] );
		var nextElement = uiIsInPlace ? ui.display : $( '#mw-content-text' );
		nextElement.before( errorBox );
		errorBox.hide().fadeIn();
	}
		
	/*
	 * Fetch the incoming links to a page. Returns a jQuery promise
	 * (success - array of titles of pages that contain links to the target page and
	 * array of "canonical titles" of possible destinations of the backlinks (either
	 * the target page or redirects to the target page), failure - error description)
	 * page: Target page
	 */
	var getBacklinks = function( page ) {
		var dfd = new $.Deferred();
		var api = new mw.Api();
		api.get( {
			'action': 'query',
			'list': 'backlinks',
			'bltitle': page,
			'bllimit': 15,
			'blnamespace': 0
		} ).done( function( data ) {
			// There might be duplicate entries in some corner cases. We don't care,
			// since we are going to check later, anyway
			var backlinks = [];
			var linkTitles = [getCanonicalTitle( page )];
			$.each( data.query.backlinks, function() {
				backlinks.push( this.title );
				if ( this.redirlinks ) {
					linkTitles.push( this.title );
					$.each( this.redirlinks, function() {
						backlinks.push( this.title );
					} );
				}
			} );
			dfd.resolve( backlinks, linkTitles );
		} ).fail( function( code, data ) {
			dfd.reject( txt.getBacklinksError.replace( '$1', code ) );
		} );
		return dfd.promise();
	};		
 
install();

} )(jQuery,mediaWiki);