MediaWiki
Difference between revisions of "Gadget-LinguaImporter.js"
(Created page with "→* LangImporter * * Ajoute un onglet permettant d'importer automatiquement une langue sur * LinguaLibre depuis un élément Wikidata.: →<nowiki>: /* globals mw, OO, $...") |
|||
Line 1: | Line 1: | ||
/* | /* | ||
− | * | + | * LinguaImporter |
* | * | ||
* Ajoute un onglet permettant d'importer automatiquement une langue sur | * Ajoute un onglet permettant d'importer automatiquement une langue sur | ||
Line 15: | Line 15: | ||
const messages = { | const messages = { | ||
'fr': { | 'fr': { | ||
− | ' | + | 'linguaimporter': 'LinguaImporter', |
− | ' | + | 'linguaimporter-action-import': 'Importer', |
− | ' | + | 'linguaimporter-action-cancel': 'Annuler', |
− | ' | + | 'linguaimporter-field-wd': 'Élément Wikidata', |
− | ' | + | 'linguaimporter-portlet-title': 'Importer une langue', |
− | ' | + | 'linguaimporter-notify-success': 'Langue importée avec succès - [[$1]]', |
}, | }, | ||
'en': { | 'en': { | ||
− | ' | + | 'linguaimporter': 'LinguaImporter', |
− | ' | + | 'linguaimporter-action-import': 'Import', |
− | ' | + | 'linguaimporter-action-cancel': 'Cancel', |
− | ' | + | 'linguaimporter-field-wd': 'Wikidata item', |
− | ' | + | 'linguaimporter-portlet-title': 'Import a language', |
− | ' | + | 'linguaimporter-notify-success': 'Language successfuly imported - [[$1]]', |
} | } | ||
}; | }; | ||
Line 38: | Line 38: | ||
− | // Instanciate | + | // Instanciate LinguaImporter and add it ti MediaWiki's UI |
$( function ( $ ) { | $( function ( $ ) { | ||
− | var | + | var instanceLinguaImporter, |
instanceWindowManager = new OO.ui.WindowManager(), | instanceWindowManager = new OO.ui.WindowManager(), | ||
− | portlet = mw.util.addPortletLink( 'p-cactions', '#', mw.msg( ' | + | portlet = mw.util.addPortletLink( 'p-cactions', '#', mw.msg( 'linguaimporter-portlet-title' ) ); |
$( 'body' ).append( instanceWindowManager.$element ); | $( 'body' ).append( instanceWindowManager.$element ); | ||
$( portlet ).on( 'click', function ( e ) { | $( portlet ).on( 'click', function ( e ) { | ||
e.preventDefault(); | e.preventDefault(); | ||
− | if ( | + | if ( instanceLinguaImporter === undefined ) { |
− | + | instanceLinguaImporter = new LinguaImporter(); | |
− | instanceWindowManager.addWindows( [ | + | instanceWindowManager.addWindows( [ instanceLinguaImporter ] ); |
} | } | ||
− | + | instanceLinguaImporter.open(); | |
} ); | } ); | ||
} ); | } ); | ||
− | var | + | var LinguaImporter = function() { |
// Initialize config | // Initialize config | ||
var config = { size: 'large' }; | var config = { size: 'large' }; | ||
// Parent constructor | // Parent constructor | ||
− | + | LinguaImporter.parent.call( this, config ); | |
// Properties | // Properties | ||
Line 77: | Line 77: | ||
/* Setup */ | /* Setup */ | ||
− | OO.inheritClass( | + | OO.inheritClass( LinguaImporter, OO.ui.ProcessDialog ); |
/* Static Properties */ | /* Static Properties */ | ||
− | + | LinguaImporter.static.name = 'linguaimporter'; | |
− | + | LinguaImporter.static.title = mw.msg( 'linguaimporter' ); | |
− | + | LinguaImporter.static.actions = [ | |
− | { action: 'import', label: mw.msg( ' | + | { action: 'import', label: mw.msg( 'linguaimporter-action-import' ), flags: [ 'primary', 'progressive' ] }, |
− | { action: 'cancel', label: mw.msg( ' | + | { action: 'cancel', label: mw.msg( 'linguaimporter-action-cancel' ), flags: [ 'safe', 'back' ] } |
]; | ]; | ||
Line 96: | Line 96: | ||
* Build the interface displayed inside the ProcessDialog box. | * Build the interface displayed inside the ProcessDialog box. | ||
*/ | */ | ||
− | + | LinguaImporter.prototype.initialize = function () { | |
− | + | LinguaImporter.parent.prototype.initialize.apply( this, arguments ); | |
this.wdInput = new mw.recordWizard.layout.WikidataSearchWidget( { | this.wdInput = new mw.recordWizard.layout.WikidataSearchWidget( { | ||
Line 108: | Line 108: | ||
this.wdInput, { | this.wdInput, { | ||
align: 'top', | align: 'top', | ||
− | label: mw.msg( ' | + | label: mw.msg( 'linguaimporter-field-wd' ), |
} | } | ||
) | ) | ||
Line 125: | Line 125: | ||
* | * | ||
* This method is called within the ProcessDialog when the user clicks | * This method is called within the ProcessDialog when the user clicks | ||
− | * on an action button (the one defined in | + | * on an action button (the one defined in LinguaImporter.static.actions). |
* Here is defined in which order each method of the category moving | * Here is defined in which order each method of the category moving | ||
* process is called. | * process is called. | ||
Line 131: | Line 131: | ||
* @return {OO.ui.Process} Action process. | * @return {OO.ui.Process} Action process. | ||
*/ | */ | ||
− | + | LinguaImporter.prototype.getActionProcess = function ( action ) { | |
var process = new OO.ui.Process(), | var process = new OO.ui.Process(), | ||
WID, wikidataItem; | WID, wikidataItem; | ||
Line 153: | Line 153: | ||
* @return {jQuery.Promise} Promise resolved when window is closed | * @return {jQuery.Promise} Promise resolved when window is closed | ||
*/ | */ | ||
− | + | LinguaImporter.prototype.closeDialog = function () { | |
var dialog = this; | var dialog = this; | ||
Line 167: | Line 167: | ||
* @return {number} Height in px the dialog should be. | * @return {number} Height in px the dialog should be. | ||
*/ | */ | ||
− | + | LinguaImporter.prototype.getBodyHeight = function () { | |
return this.content.$element.outerHeight( true ); | return this.content.$element.outerHeight( true ); | ||
}; | }; | ||
Line 173: | Line 173: | ||
− | + | LinguaImporter.prototype.createLang = function ( wikidataItem ) { | |
var newItem = new mw.recordWizard.wikibase.Item(), | var newItem = new mw.recordWizard.wikibase.Item(), | ||
dialog = this; | dialog = this; | ||
Line 203: | Line 203: | ||
}; | }; | ||
− | + | LinguaImporter.prototype.success = function () { | |
− | mw.notify( mw.message( ' | + | mw.notify( mw.message( 'linguaimporter-notify-success', this.newEntityId ).text() ) |
}; | }; | ||
} ); | } ); |
Revision as of 14:00, 21 September 2018
/*
* LinguaImporter
*
* Ajoute un onglet permettant d'importer automatiquement une langue sur
* LinguaLibre depuis un élément Wikidata.
*/
/* <nowiki> */
/* globals mw, OO, $ */
mw.loader.using( [ 'oojs-ui', 'ext.recordWizard.wikibase', 'ext.recordWizard.layout', 'wikibase.api.RepoApi', 'mediawiki.ForeignApi', 'mediawiki.widgets' ], function() {
'use strict';
// Messages
const messages = {
'fr': {
'linguaimporter': 'LinguaImporter',
'linguaimporter-action-import': 'Importer',
'linguaimporter-action-cancel': 'Annuler',
'linguaimporter-field-wd': 'Élément Wikidata',
'linguaimporter-portlet-title': 'Importer une langue',
'linguaimporter-notify-success': 'Langue importée avec succès - [[$1]]',
},
'en': {
'linguaimporter': 'LinguaImporter',
'linguaimporter-action-import': 'Import',
'linguaimporter-action-cancel': 'Cancel',
'linguaimporter-field-wd': 'Wikidata item',
'linguaimporter-portlet-title': 'Import a language',
'linguaimporter-notify-success': 'Language successfuly imported - [[$1]]',
}
};
mw.messages.set( messages.en );
var lang = mw.config.get( 'wgUserLanguage' );
if ( lang !== 'en' && lang in messages ) {
mw.messages.set( messages[ lang ] );
}
// Instanciate LinguaImporter and add it ti MediaWiki's UI
$( function ( $ ) {
var instanceLinguaImporter,
instanceWindowManager = new OO.ui.WindowManager(),
portlet = mw.util.addPortletLink( 'p-cactions', '#', mw.msg( 'linguaimporter-portlet-title' ) );
$( 'body' ).append( instanceWindowManager.$element );
$( portlet ).on( 'click', function ( e ) {
e.preventDefault();
if ( instanceLinguaImporter === undefined ) {
instanceLinguaImporter = new LinguaImporter();
instanceWindowManager.addWindows( [ instanceLinguaImporter ] );
}
instanceLinguaImporter.open();
} );
} );
var LinguaImporter = function() {
// Initialize config
var config = { size: 'large' };
// Parent constructor
LinguaImporter.parent.call( this, config );
// Properties
this.api = new mw.Api( { timeout: 7000 } );
this.wikidataApi = new mw.ForeignApi( 'https://www.wikidata.org/w/api.php', {
anonymous: true,
parameters: { origin: '*' },
ajax: { timeout: 10000 }
} );
this.content = new OO.ui.PanelLayout( { padded: true, expanded: false } );
this.layout;
this.$body;
};
/* Setup */
OO.inheritClass( LinguaImporter, OO.ui.ProcessDialog );
/* Static Properties */
LinguaImporter.static.name = 'linguaimporter';
LinguaImporter.static.title = mw.msg( 'linguaimporter' );
LinguaImporter.static.actions = [
{ action: 'import', label: mw.msg( 'linguaimporter-action-import' ), flags: [ 'primary', 'progressive' ] },
{ action: 'cancel', label: mw.msg( 'linguaimporter-action-cancel' ), flags: [ 'safe', 'back' ] }
];
/* ProcessDialog-related Methods */
/**
* Build the interface displayed inside the ProcessDialog box.
*/
LinguaImporter.prototype.initialize = function () {
LinguaImporter.parent.prototype.initialize.apply( this, arguments );
this.wdInput = new mw.recordWizard.layout.WikidataSearchWidget( {
$overlay: $( 'body' )
} );
this.layout = new OO.ui.Widget( {
content: [
new OO.ui.FieldLayout(
this.wdInput, {
align: 'top',
label: mw.msg( 'linguaimporter-field-wd' ),
}
)
],
} );
this.content.$element.append( this.layout.$element );
this.$body.append( this.content.$element );
this.updateSize();
};
/**
* Get a process for taking action.
*
* This method is called within the ProcessDialog when the user clicks
* on an action button (the one defined in LinguaImporter.static.actions).
* Here is defined in which order each method of the category moving
* process is called.
* @param {string} action Name of the action button clicked.
* @return {OO.ui.Process} Action process.
*/
LinguaImporter.prototype.getActionProcess = function ( action ) {
var process = new OO.ui.Process(),
WID, wikidataItem;
if ( action === 'import' ) {
WID = this.wdInput.getData();
wikidataItem = new mw.recordWizard.wikibase.Item( WID );
process.next( wikidataItem.getFromApi.bind( wikidataItem, this.wikidataApi ) );
process.next( this.createLang.bind( this, wikidataItem ) );
process.next( this.success.bind( this ) );
}
process.next( this.closeDialog, this );
return process;
};
/**
* Close the window.
*
* @return {jQuery.Promise} Promise resolved when window is closed
*/
LinguaImporter.prototype.closeDialog = function () {
var dialog = this;
var lifecycle = dialog.close();
return lifecycle.closed;
};
/**
* Get the height of the window body.
* Used by the ProcessDialog to set an accurate height to the dialog.
*
* @return {number} Height in px the dialog should be.
*/
LinguaImporter.prototype.getBodyHeight = function () {
return this.content.$element.outerHeight( true );
};
LinguaImporter.prototype.createLang = function ( wikidataItem ) {
var newItem = new mw.recordWizard.wikibase.Item(),
dialog = this;
newItem.labels = wikidataItem.getLabels();
// instance of = language
newItem.addStatement( new mw.recordWizard.wikibase.Statement( 'P2' ).setType( 'wikibase-item' ).setValue( 'Q4' ) );
// wikidata id
newItem.addStatement( new mw.recordWizard.wikibase.Statement( 'P12' ).setType( 'external-id' ).setValue( wikidataItem.getId() ) );
// ISO 639-3
isoStatement = wikidataItem.getStatements( 'P220' )
if ( isoStatement !== null ) {
newItem.addStatement( new mw.recordWizard.wikibase.Statement( 'P13' ).setType( 'external-id' ).setValue( isoStatement[ 0 ].getValue() ) );
}
// Wikimedia language code
wmlangStatement = wikidataItem.getStatements( 'P424' )
if ( wmlangStatement !== null ) {
newItem.addStatement( new mw.recordWizard.wikibase.Statement( 'P17' ).setType( 'external-id' ).setValue( wmlangStatement[ 0 ].getValue() ) );
}
return newItem.createOrUpdate( this.api ).then( function( data ) {
console.log( 'https://lingualibre.fr/wiki/' + data.entity.id );
dialog.newEntityId = data.entity.id;
} );
};
LinguaImporter.prototype.success = function () {
mw.notify( mw.message( 'linguaimporter-notify-success', this.newEntityId ).text() )
};
} );