Differenze tra le versioni di "MediaWiki:Gadget-minipedia.js"

Da Minipedia.
Jump to navigation Jump to search
(work in progress)
 
(add support to modal and page creation)
Riga 1: Riga 1:
 
/**
 
/**
  +
* Make Minipedia magics
* Add "Purge" content action link.
 
 
*
 
*
 
* Dependencies: mediawiki.util
 
* Dependencies: mediawiki.util
 
*
 
*
  +
* @revision 2020-06-27
* @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 () {
+
$.when( mw.loader.using( 'mediawiki.util' ), $.ready ).then( function () {
   
 
// global configuration file
 
// global configuration file
Riga 17: Riga 16:
 
// default namespace name
 
// default namespace name
 
mp.namespace = mp.namespace || 'Mini';
 
mp.namespace = mp.namespace || 'Mini';
  +
  +
// default edit intro page title
  +
mp.editIntro = mp.editIntro || 'Progetto:Minipedia/Creazione voce';
   
 
// work only in the main namespace
 
// work only in the main namespace
Riga 34: Riga 36:
 
);
 
);
   
  +
// normal page title and mini version
 
var pageName = mw.config.get( 'wgPageName' );
 
var pageName = mw.config.get( 'wgPageName' );
 
var miniPageName = mp.namespace + ':' + pageName;
 
var miniPageName = mp.namespace + ':' + pageName;
Riga 45: Riga 48:
 
function pageExists( title, callback ) {
 
function pageExists( title, callback ) {
   
// prepare the API request
+
// require the API stuff
  +
mw.loader.using( 'mediawiki.api' ).then( function() {
var request = {
 
action: 'query',
 
prop: 'info',
 
titles: title
 
};
 
   
// make the API request
+
// prepare the API request
( new mw.Api() ).get( request ).done( function ( response ) {
+
var request = {
  +
action: 'query',
  +
prop: 'info',
  +
titles: title
  +
};
   
// no response no party
+
// make the API request
  +
( new mw.Api() ).get( request ).done( function ( response ) {
if( !response.query ) {
 
throw 'no response';
 
}
 
   
  +
// no response no party
var page;
 
for( var i in response.query ) {
+
if( !response.query ) {
page = response.query[i];
+
throw 'no response';
  +
}
if( page.pageid && page.pageid > 0 ) {
 
   
  +
// the list should contain just one page
// exists
 
callback( true );
+
var page;
  +
for( var i in response.query ) {
  +
page = response.query[i];
  +
if( page.pageid && page.pageid > 0 ) {
  +
  +
// exists
  +
callback( page );
  +
}
 
}
 
}
}
 
   
// does not exist
+
// does not exist
callback( false );
+
callback( false );
  +
} );
  +
 
} );
 
} );
  +
};
  +
  +
/**
  +
* Go to the Minipedia page in edit mode
  +
*
  +
* @param title Page title without Minipedia namespace
  +
*/
  +
function goToMinipediaEditPageTitle( title ) {
  +
  +
// eventually just inherit this page name
  +
title = title || pageName;
  +
  +
var mpTitle = mp.namespace + ':' + title;
  +
  +
var queryString = {
  +
action: 'edit',
  +
title: mpTitle,
  +
preload: title,
  +
editintro: mp.editIntro,
  +
};
  +
  +
// '/index.php'
  +
var wgScript = mw.config.get( 'wgScript' );
  +
  +
// go to the edit page URL
  +
window.location = wgScript + '?' + $.param( queryString );
  +
};
  +
  +
/**
  +
* Go to the Minipedia page in view mode
  +
*
  +
* @param title Page title without Minipedia namespace
  +
*/
  +
function goToMinipediaPageTitle( title ) {
  +
  +
// eventually just inherit this page name
  +
title = title || pageName;
  +
  +
var miniTitleObject = new mw.Title( title, mp.namespace );
  +
  +
// go to the edit page URL
  +
window.location = miniTitleObject.getUrl();
 
};
 
};
   
Riga 80: Riga 131:
 
// check if the mini version exists
 
// check if the mini version exists
 
pageExists( miniPageName, function( miniPageExists ) {
 
pageExists( miniPageName, function( miniPageExists ) {
  +
if( miniPageExists ) {
 
// do something
+
// allow to open OO UI windows
  +
mw.loader.using( 'oojs-ui-windows' ).then( function() {
} else {
 
  +
// do something else
 
  +
// check if the page already exist
}
 
  +
if( miniPageExists ) {
  +
  +
// just redirect to the Minipedia version
  +
goToMinipediaPageTitle();
  +
} else {
  +
  +
// ask if you want to create the page
  +
  +
// create message dialog window
  +
var messageDialog = new OO.ui.MessageDialog();
  +
var windowManager = new OO.ui.WindowManager();
  +
$( 'body' ).append( windowManager.$element );
  +
windowManager.addWindows( [ messageDialog ] );
  +
  +
// configure and open dialog
  +
var windowInstance = windowManager.openWindow( messageDialog, {
  +
title: "Accesso Minipedia",
  +
message: "Sii il primo a creare una versione più ridotta e più accessibile di questa voce, in Minipedia!"
  +
} );
  +
  +
// check if you accepted the page creation
  +
windowInstance.closed.then( function ( data ) {
  +
if( data.action === 'accept' ) {
  +
  +
// go go go!
  +
goToMinipediaEditPageTitle();
  +
}
  +
} );
  +
}
  +
  +
} );
 
} );
 
} );
 
} );
 
} );

Versione delle 01:17, 27 giu 2020

/**
 * Make Minipedia magics
 *
 * Dependencies: mediawiki.util
 *
 * @revision 2020-06-27
 */
$.when( mw.loader.using( 'mediawiki.util' ), $.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';

	// default edit intro page title
	mp.editIntro = mp.editIntro || 'Progetto:Minipedia/Creazione voce';

	// 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'
	);

	// normal page title and mini version
	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 ) {

		// require the API stuff
		mw.loader.using( 'mediawiki.api' ).then( function() {

			// 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';
				}

				// the list should contain just one page
				var page;
				for( var i in response.query ) {
					page = response.query[i];
					if( page.pageid && page.pageid > 0 ) {

						// exists
						callback( page );
					}
				}

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

	/**
	 * Go to the Minipedia page in edit mode
	 *
	 * @param title Page title without Minipedia namespace
	 */
	function goToMinipediaEditPageTitle( title ) {

		// eventually just inherit this page name
		title = title || pageName;

		var mpTitle = mp.namespace + ':' + title;

		var queryString = {
			action: 'edit',
			title: mpTitle,
			preload: title,
			editintro: mp.editIntro,			
		};

		// '/index.php'
		var wgScript = mw.config.get( 'wgScript' );

		// go to the edit page URL
		window.location = wgScript + '?' + $.param( queryString );
	};

	/**
	 * Go to the Minipedia page in view mode
	 *
	 * @param title Page title without Minipedia namespace
	 */
	function goToMinipediaPageTitle( title ) {

		// eventually just inherit this page name
		title = title || pageName;

		var miniTitleObject = new mw.Title( title,  mp.namespace );

		// go to the edit page URL
		window.location = miniTitleObject.getUrl();
	};

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

		// check if the mini version exists
		pageExists( miniPageName, function( miniPageExists ) {

			// allow to open OO UI windows
			mw.loader.using( 'oojs-ui-windows' ).then( function() {

				// check if the page already exist
				if( miniPageExists ) {

					// just redirect to the Minipedia version
					goToMinipediaPageTitle();
				} else {

					// ask if you want to create the page

					// create message dialog window
					var messageDialog = new OO.ui.MessageDialog();
					var windowManager = new OO.ui.WindowManager();
					$( 'body' ).append( windowManager.$element );
					windowManager.addWindows( [ messageDialog ] );

					// configure and open dialog
					var windowInstance = windowManager.openWindow( messageDialog, {
						title: "Accesso Minipedia",
						message: "Sii il primo a creare una versione più ridotta e più accessibile di questa voce, in Minipedia!"
					} );

					// check if you accepted the page creation
					windowInstance.closed.then( function ( data ) {
						if( data.action === 'accept' ) {

							// go go go!
							goToMinipediaEditPageTitle();
						}
					} );
				}

			} );
		} );
	} );
} );