MediaWiki:Gadget-minipedia.js

Da Minipedia.
Versione del 26 giu 2020 alle 18:23 di Valerio Bozzolan (discussione | contributi) (work in progress)
(diff) ← Versione meno recente | Versione attuale (diff) | Versione più recente → (diff)
Jump to navigation Jump to search

Nota: dopo aver salvato, potrebbe essere necessario pulire la cache del proprio browser per vedere i cambiamenti.

  • Firefox / Safari: tenere premuto il tasto delle maiuscole Shift e fare clic su Ricarica, oppure premere Ctrl-F5 o Ctrl-R (⌘-R su Mac)
  • Google Chrome: premere Ctrl-Shift-R (⌘-Shift-R su un Mac)
  • Internet Explorer: tenere premuto il tasto Ctrl e fare clic su Aggiorna, oppure premere Ctrl-F5
  • Opera: Vai nel Menu → Impostazioni (Opera → Preferenze su un Mac) e poi in Privacy & sicurezza → Pulisci dati del browser → Immagini e file nella cache.
/**
 * Add "Purge" content action link.
 *
 * Dependencies: mediawiki.util
 *
 * @source www.mediawiki.org/wiki/Snippets/Purge_action
 * @revision 2014-05-14
 */
$.when( mw.loader.using( [ 'mediawiki.util', 'mediawiki.api', 'oojs-ui-core' ] ), $.ready ).then( function () {

	// global configuration file
	window.MiniPedia = window.MiniPedia || {};

	// shortcut for the global configuration file (mp = mini-pedia)
	var mp = window.MiniPedia;

	// default namespace name
	mp.namespace = mp.namespace || 'Mini';

	// work only in the main namespace
	var ns = mw.config.get( 'wgNamespaceNumber' );
	if( ns !== 0 ) {
		return;
	}

	// add a "Minipedia"
	var miniVersionPortletLink = mw.util.addPortletLink(
	    'p-cactions',
	    '#',
	    'Mini Version',
	    'ca-minipedia',
	    'Open Minipedia',
	    'n'
	);

	var pageName = mw.config.get( 'wgPageName' );
	var miniPageName = mp.namespace + ':' + pageName;

	/**
	 * Check if a page title already exists
	 *
	 * @param title    Page title
	 * @param callback Callback with one boolean argument telling if the page title exists or not
	 */
	function pageExists( title, callback ) {

		// prepare the API request
		var request = {
			action: 'query',
			prop: 'info',
			titles: title
		};

		// make the API request
		( new mw.Api() ).get( request ).done( function ( response ) {

			// no response no party
			if( !response.query ) {
				throw 'no response';
			}

			var page;
			for( var i in response.query ) {
				page = response.query[i];
				if( page.pageid && page.pageid > 0 ) {

					// exists
					callback( true );
				}
			}

			// does not exist
			callback( false );
		} );
	};

	// on the mini toolback click, check if a mini version exists
	$( miniVersionPortletLink ).click( function() {

		// check if the mini version exists
		pageExists( miniPageName, function( miniPageExists ) {
			if( miniPageExists ) {
				// do something
			} else {
				// do something else
			}
		} );
	} );
} );