MediaWiki:Gadget-wikibase.js
Revisión del 09:11 9 abr 2014 de Bth0 (discusión | contribuciones) (Página creada con «// <nowiki> /** * wikibase.js * =========== * wikibase.js is a libary which provides functions to edit Wikibase entities. It also supports importing site links from wik...»)
// <nowiki> /** * wikibase.js * =========== * wikibase.js is a libary which provides functions to edit Wikibase entities. It also supports importing site links from wikipedias. * If you want to use this libary in a gadget, you have to add to the [[MediaWiki:Gadgets-definition]] page: * * <GadgetName>|wikibase.js|<GadgetName>.js */ mediaWiki.loader.using( 'mediawiki.jqueryMsg', function () { window.jqueryMsg = mediaWiki.jqueryMsg.getMessageFunction(); } ); ( function( mw, $ ) { var debug = mw.util.getParamValue( 'wikibase.js' ); var errorMessages = { 'unknown': { 'en': 'An unknown error had occured.' } }; function errMsg( key ) { /*var msg; if( i18n[ lang ][ key ] ) { msg = errorMessages[ lang ][ key ]; } else if( key in i18n[ 'en' ] ) { msg = errorMessages[ 'en' ][ key ]; } else { msg = '<' + key + '>'; } for ( var i = 1; i < arguments.length; i++ ) { msg = msg.replace( new RegExp( '$' + i, 'g' ), arguments[i] ); } return parse( msg );*/ return '<' + key + '>'; } function parse( key ) { mw.messages.set( key, key ); // register key return jqueryMsg( key ).replace( '\n', '<br>' ); // html encode } /** * Class to make editing Wikibase entities easier. */ function WikibaseTools() { /* properties */ this.id = mediaWiki.config.get( 'wbEntityId' ); this.entities = { ITEM: 'item', PROPERTY: 'property', QUERY: 'query', NONE: false }; switch( this.id ? this.id.substr( 0, 1 ) : false ) { case 'q': this.entityType = this.entities.ITEM; break; case 'p': this.entityType = this.entities.PROPERTY; break; case 'xxx': this.entityType = this.entities.QUERY; break; default: this.entityType = this.entities.NONE; break; } /* functions */ this.createItem = createItem; this.updateItem = updateItem; this.getItem = getItem; this.getSiteLinks = getSiteLinks; this.getInterwikiLinks = getInterwikiLinks; } /** * Create a single new Wikibase entity. * * @param data The serialized object that is used as the data source. A newly created entity will be assigned an 'id'. * @param summary Summary for the edit. Will be prepended by an automatically generated comment. * @param success function called on success * @param error function called on error */ function createItem( data, summary, success, error ) { this.updateItem( data, false, summary, success, error ); } /** * Modify a Wikibase entity with serialised information. * * @param data The serialized object that is used as the data source. A newly created entity will be assigned an 'id'. * @param id The identifier for the entity, including the prefix. If set to false, a new item will be created. * @param summary Summary for the edit. Will be prepended by an automatically generated comment. * @param success function called on success * @param error function called on error */ function updateItem( data, id, summary, success, error ) { var postData = { 'format': 'json', 'action': 'wbeditentity', 'data': $.toJSON( data ), 'summary': summary, 'token': mw.user.tokens.get( 'editToken' ) }; if( id ) { postData['id'] = id; } else { postData['new'] = 'item'; } $.ajax( { type: 'POST', url: mw.util.wikiScript( 'api' ), data: postData, success: function( data ) { if( data.error && data.error.info ) { error( parse( data.error.info ) ); } else { success( data ); } } } ); } /** * Get the data for multiple Wikibase entities * * @param id The IDs of the entities to get the data from * @param success function called on success * @param error function called on error */ function getItem( id, success, error ) { $.ajax( { url: mw.util.wikiScript( 'api' ), data: { 'format': 'json', 'action': 'wbgetentities', 'ids': typeof id === 'string' ? id : id.join( '|' ) }, success: function( data ) { if( data.error && data.error.info ) { error( parse( data.error.info ) ); } else { success( typeof id === 'string' ? data.entities[id] : data.entities ); } } } ); } /** * Get the existings site links of an entity * * @param id The ID of the entity to get the site links from * @param success function called on success * @param error function called on error */ function getSiteLinks( id, success, error ) { this.getItem( id, function( data ) { if( data.sitelinks ) { success( data.sitelinks ); } else { success( {} ); } }, error ); } /** * Return the existings links in a page of a wiki * @param lang the language of the wikipedia ( look for <lang>.wikipedia.org ) * @param title title of the page * @param success function called on success * @param error function called on error */ function getInterwikiLinks( lang, title, success, error ) { $.ajax( { url: '//' + lang + '.wikipedia.org/w/api.php', data: { 'format': 'json', 'action': 'query', 'titles': title, 'prop': 'langlinks', 'lllimit': 400 }, dataType: 'jsonp' } ) .done( function( data ) { if( data.error && data.error.info ) { error( parse( data.error.info ) ); } else if( data.query && data.query.pages ) { for( var id in data.query.pages ) { if( id == -1 ) { error( errMsg( 'unknown' ) ); } else { if( data.query.pages[id].langlinks ) { var langlinks = data.query.pages[id].langlinks; var links = {}; for( var i in langlinks ) { links[langlinks[i]['lang']] = langlinks[i]['*']; } success( links ); } else { success( [] ); } } } } else { error( errMsg( 'unknown' ) ); } } ) .fail( function( jqXHR, textStatus, errorThrown ) { error( errMsg( 'unknown' ) ); } ); } var wikibaseTools = new WikibaseTools(); window.wikibaseTools = wikibaseTools; // global access } )( mediaWiki, jQuery ); // </nowiki>