Difference between revisions of "MediaWiki:Gadget-minipedia.js"
Jump to navigation
Jump to search
m (migrate CSS cosmetic changes) |
(minor refactor with Promise objects) |
||
| Line 3: | Line 3: | ||
* |
* |
||
* Dependencies: mediawiki.util |
* Dependencies: mediawiki.util |
||
| + | * |
||
| + | * See https://phabricator.wikimedia.org/tag/minipedia/ |
||
* |
* |
||
* @revision 2020-06-27 |
* @revision 2020-06-27 |
||
*/ |
*/ |
||
| − | $.when( |
+ | $.when( $.ready ).then( function () { |
/* |
/* |
||
| Line 14: | Line 16: | ||
* |
* |
||
* // assure that you do not overwrite other-people customizations |
* // assure that you do not overwrite other-people customizations |
||
| − | * window.MiniPedia |
+ | * window.MiniPedia = window.MiniPedia || {}; |
* window.MiniPedia.L10N = window.MiniPedia.L10N || {}; |
* window.MiniPedia.L10N = window.MiniPedia.L10N || {}; |
||
* |
* |
||
| − | * // customize |
+ | * // then customize something |
| − | * window.MiniPedia. |
+ | * window.MiniPedia.editIntro = 'Project:How to create'; |
| − | * window.MiniPedia.L10N.minipedia = |
+ | * window.MiniPedia.L10N.minipedia = 'Otherpedia'; |
* |
* |
||
| − | * For all the strings available in the L10N object |
+ | * For all the strings available in the 'MiniPedia.L10N' object |
* see the L10N_DEFAULTS variable below. |
* see the L10N_DEFAULTS variable below. |
||
*/ |
*/ |
||
| Line 51: | Line 53: | ||
// localization stuff |
// localization stuff |
||
| − | mp.L10N |
+ | mp.L10N = mp.L10N || {}; |
// shortcut |
// shortcut |
||
| Line 62: | Line 64: | ||
} |
} |
||
} |
} |
||
| + | |||
| + | /** |
||
| + | * Lazy shortcut to obtain just the first API result |
||
| + | * |
||
| + | * @param {Object} response API Response |
||
| + | * @return {Object} page object |
||
| + | */ |
||
| + | function justFirstQueryPage( response ) { |
||
| + | |||
| + | // no response no party |
||
| + | if( !response.query || !response.query.pages ) { |
||
| + | throw 'no valid API response'; |
||
| + | } |
||
| + | |||
| + | // the list should contain just one page |
||
| + | var pages = response.query.pages; |
||
| + | for( var id in pages ) { |
||
| + | return pages[ id ]; |
||
| + | } |
||
| + | |||
| + | // no page no party |
||
| + | return false; |
||
| + | }; |
||
/** |
/** |
||
* Check if a page title already exists |
* Check if a page title already exists |
||
* |
* |
||
| − | * @param title |
+ | * @param title Page title |
| + | * @return Promise |
||
| − | * @param callback Callback with one boolean argument telling if the page title exists or not |
||
*/ |
*/ |
||
| − | function pageExists( title |
+ | function pageExists( title ) { |
| − | // require the API stuff |
+ | // require the API stuff at runtime |
| + | // TODO: check if it's called everytime and eventually put dependency in ResourceLoader |
||
| − | mw.loader.using( 'mediawiki.api' ).then( function() { |
||
| + | // this returns the Promise created downstream (false or existing page) |
||
| + | return mw.loader.using( 'mediawiki.api' ).then( function() { |
||
// prepare the API request |
// prepare the API request |
||
| Line 82: | Line 109: | ||
// make the API request |
// make the API request |
||
| + | // resolve the parent Promise with another Promise giving the existing page or false |
||
| − | ( new mw.Api() ).get( request ).done( function ( response ) { |
||
| + | return ( new mw.Api() ).get( request ).then( function ( response ) { |
||
| − | // |
+ | // check if it exists |
| − | + | var page = justFirstQueryPage( response ); |
|
| + | if( page && page.pageid && page.pageid > 0 ) { |
||
| − | throw 'no valid response'; |
||
| + | return page; |
||
} |
} |
||
| + | return false; |
||
| − | // the list should contain just one page |
||
| − | var pages = response.query.pages; |
||
| − | for( var id in pages ) { |
||
| − | |||
| − | // for each page (just one) check if it exists |
||
| − | var page = pages[ id ]; |
||
| − | if( page.pageid && page.pageid > 0 ) { |
||
| − | |||
| − | // found! |
||
| − | callback( page ); |
||
| − | return; |
||
| − | } |
||
| − | } |
||
| − | |||
| − | // does not exist |
||
| − | callback( false ); |
||
} ); |
} ); |
||
| Line 166: | Line 180: | ||
// on the mini toolback click, check if a mini version exists |
// on the mini toolback click, check if a mini version exists |
||
$( miniVersionPortletLink ).click( function( e ) { |
$( miniVersionPortletLink ).click( function( e ) { |
||
| + | |||
| − | |||
| − | // |
+ | // wait for some information |
| + | $.when( |
||
| − | pageExists( miniPageName, function( miniPageExists ) { |
||
| + | // check if the page really exists |
||
| + | pageExists( miniPageName ), |
||
// allow to open OO UI windows |
// allow to open OO UI windows |
||
| − | mw.loader.using( 'oojs-ui-windows' ) |
+ | mw.loader.using( 'oojs-ui-windows' ) |
| + | ).done( function( miniPageExists, loader ) { |
||
| − | // check if the page already exist |
||
| − | if( miniPageExists ) { |
||
| − | + | // check if the page already exist |
|
| + | if( miniPageExists ) { |
||
| − | window.location = miniPageUrl; |
||
| − | } else { |
||
| − | + | // just redirect to the Minipedia version |
|
| + | window.location = miniPageUrl; |
||
| + | } else { |
||
| − | + | // ask if you want to create the page |
|
| − | var messageDialog = new OO.ui.MessageDialog(); |
||
| − | var windowManager = new OO.ui.WindowManager(); |
||
| − | $( 'body' ).append( windowManager.$element ); |
||
| − | windowManager.addWindows( [ messageDialog ] ); |
||
| − | + | // create message dialog window |
|
| + | var messageDialog = new OO.ui.MessageDialog(); |
||
| − | var windowInstance = windowManager.openWindow( messageDialog, { |
||
| + | var windowManager = new OO.ui.WindowManager(); |
||
| − | title: L10N.createMinipediaPageTitle, |
||
| + | $( 'body' ).append( windowManager.$element ); |
||
| − | message: L10N.createMinipediaPageBody, |
||
| + | windowManager.addWindows( [ messageDialog ] ); |
||
| − | } ); |
||
| − | + | // configure and open dialog |
|
| − | + | var windowInstance = windowManager.openWindow( messageDialog, { |
|
| + | title: L10N.createMinipediaPageTitle, |
||
| − | if( data.action === 'accept' ) { |
||
| + | message: L10N.createMinipediaPageBody, |
||
| + | } ); |
||
| + | // check if you accepted the page creation |
||
| − | // go go go! |
||
| + | windowInstance.closed.then( function ( data ) { |
||
| − | goToMinipediaEditPage(); |
||
| + | if( data.action === 'accept' ) { |
||
| − | } |
||
| − | } ); |
||
| − | + | // go go go! |
|
| + | goToMinipediaEditPage(); |
||
| − | // end if |
||
| + | } |
||
| + | } ); |
||
| − | } |
+ | } |
| − | // end |
+ | // end if page exists |
} ); |
} ); |
||
| − | // end |
+ | // end $.when() |
// avoid scrolling to the top |
// avoid scrolling to the top |
||
Revision as of 11:37, 31 July 2020
/**
* Make Minipedia magics
*
* Dependencies: mediawiki.util
*
* See https://phabricator.wikimedia.org/tag/minipedia/
*
* @revision 2020-06-27
*/
$.when( $.ready ).then( function () {
/*
* CONFIGURATION/LOCALIZATION INSTRUCTIONS
*
* Declare somewhere something like this:
*
* // assure that you do not overwrite other-people customizations
* window.MiniPedia = window.MiniPedia || {};
* window.MiniPedia.L10N = window.MiniPedia.L10N || {};
*
* // then customize something
* window.MiniPedia.editIntro = 'Project:How to create';
* window.MiniPedia.L10N.minipedia = 'Otherpedia';
*
* For all the strings available in the 'MiniPedia.L10N' object
* see the L10N_DEFAULTS variable below.
*/
// load localization defaults
var L10N_DEFAULTS = {
minipedia: "Minipedia",
minipediaShort: "Mini",
normalpedia: "Wikipedia Test",
normalpediaShort: "WikipediaTest",
openMinipedia: "Apri Minipedia",
openNormalpedia: "Apri Wikipedia Test",
createMinipediaPageTitle: "Accesso Minipedia",
createMinipediaPageBody: "Sii il primo a creare una versione più ridotta e più accessibile di questa voce, in Minipedia!",
};
// global configuration file
window.MiniPedia = window.MiniPedia || {};
// shortcut for the global configuration file (mp = mini-pedia)
var mp = window.MiniPedia;
// default namespace informations
mp.namespace = mp.namespace || 'Mini';
mp.namespaceNum = mp.namespaceNum || 3002;
// default edit intro page title
mp.editIntro = mp.editIntro || 'Progetto:Minipedia/Creazione voce';
// localization stuff
mp.L10N = mp.L10N || {};
// shortcut
var L10N = mp.L10N;
// load the defaults
for( var key in L10N_DEFAULTS ) {
if( !L10N[ key ] ) {
L10N[ key ] = L10N_DEFAULTS[ key ];
}
}
/**
* Lazy shortcut to obtain just the first API result
*
* @param {Object} response API Response
* @return {Object} page object
*/
function justFirstQueryPage( response ) {
// no response no party
if( !response.query || !response.query.pages ) {
throw 'no valid API response';
}
// the list should contain just one page
var pages = response.query.pages;
for( var id in pages ) {
return pages[ id ];
}
// no page no party
return false;
};
/**
* Check if a page title already exists
*
* @param title Page title
* @return Promise
*/
function pageExists( title ) {
// require the API stuff at runtime
// TODO: check if it's called everytime and eventually put dependency in ResourceLoader
// this returns the Promise created downstream (false or existing page)
return mw.loader.using( 'mediawiki.api' ).then( function() {
// prepare the API request
var request = {
action: 'query',
prop: 'info',
titles: title
};
// make the API request
// resolve the parent Promise with another Promise giving the existing page or false
return ( new mw.Api() ).get( request ).then( function ( response ) {
// check if it exists
var page = justFirstQueryPage( response );
if( page && page.pageid && page.pageid > 0 ) {
return page;
}
return false;
} );
} );
};
/**
* Prepare the wiki
*/
function prepareNormalpedia() {
// normal page title and mini version
var pageName = mw.config.get( 'wgPageName' );
// prepare the Minipedia title object
var miniTitleObject = new mw.Title( pageName, mp.namespaceNum );
// minipedia page title with prefix
var miniPageName = miniTitleObject.getPrefixedText();
// minipedia page URL
var miniPageUrl = miniTitleObject.getUrl();
/**
* Go to the Minipedia page in edit mode
*
* The page in the main namespace will be preloaded.
*/
function goToMinipediaEditPage() {
/**
* Build the query string to edit a page
*
* TODO: eventually add VisualEditor support
*/
var editPageQueryString = {
action: 'edit',
title: miniPageName,
preload: pageName,
editintro: mp.editIntro,
};
// '/index.php'
var wgScript = mw.config.get( 'wgScript' );
// go to the edit page URL
window.location = wgScript + '?' + $.param( editPageQueryString );
};
// add a "Minipedia"
var miniVersionPortletLink = mw.util.addPortletLink(
'p-namespaces',
miniPageUrl,
L10N.minipediaShort,
'ca-minipedia',
L10N.openMinipedia,
'n'
);
// on the mini toolback click, check if a mini version exists
$( miniVersionPortletLink ).click( function( e ) {
// wait for some information
$.when(
// check if the page really exists
pageExists( miniPageName ),
// allow to open OO UI windows
mw.loader.using( 'oojs-ui-windows' )
).done( function( miniPageExists, loader ) {
// check if the page already exist
if( miniPageExists ) {
// just redirect to the Minipedia version
window.location = miniPageUrl;
} 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: L10N.createMinipediaPageTitle,
message: L10N.createMinipediaPageBody,
} );
// check if you accepted the page creation
windowInstance.closed.then( function ( data ) {
if( data.action === 'accept' ) {
// go go go!
goToMinipediaEditPage();
}
} );
}
// end if page exists
} );
// end $.when()
// avoid scrolling to the top
e.preventDefault();
} );
// end $( miniVersionPortletLink ).click
};
// end prepareNormalWiki()
/**
* Prepare the Minipedia namespace
*/
function prepareMinipedia() {
// normal page title and mini version
var pageName = mw.config.get( 'wgTitle' );
// prepare the Normalpedia title object
var normalTitleObject = new mw.Title( pageName );
// minipedia page title with prefix
var normalPageName = normalTitleObject.getPrefixedText();
// minipedia page URL
var normalPageUrl = normalTitleObject.getUrl();
// add a "Minipedia"
var normalVersionPortletLink = mw.util.addPortletLink(
'p-namespaces',
normalPageUrl,
L10N.normalpediaShort,
'ca-normalpedia',
L10N.openNormalpedia
);
};
// end prepareMinipedia()
// work only in the main namespace
var ns = mw.config.get( 'wgNamespaceNumber' );
if( ns === 0 ) {
prepareNormalpedia();
} else if( ns == mp.namespaceNum ) {
prepareMinipedia();
}
// end namespace zero check
} );