User:MarkTraceur/commonsUpload.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | Documentation for this user script can be added at User:MarkTraceur/commonsUpload. |
// <nowiki>
( function ( mw, $ ) {
mw.commonsUploadPromise = mw.loader.using( [ 'mediawiki.Upload', 'oojs' ] ).then( function () {
var CUP;
/**
* @class mw.CommonsUpload
* @extends mw.Upload
*
* Used to represent an upload in progress on the frontend.
* Subclassed to upload to Commons, with all of the specifics to that
* environment.
*
* @TODO license support
*
* @constructor
* @param {Object} [apiconfig] Passed to the constructor of mw.Api.
*/
function CommonsUpload( apiconfig ) {
var origurl, oldupload, oldajax,
upload = this;
apiconfig = apiconfig || {};
apiconfig.ajax = apiconfig.ajax || {};
// CAUTION currently set to target beta, change if moving to production
apiconfig.ajax.url = apiconfig.ajax.url || 'https://commons.wikimedia.org/w/api.php?origin=' + encodeURIComponent( document.location.protocol + '//' + document.location.host );
origurl = apiconfig.ajax.url;
mw.Upload.call( this, apiconfig );
oldupload = this.api.upload;
// Core is stupid
this.api.upload = function ( file, data ) {
return upload.getCAToken().then( function ( token ) {
upload.api.defaults.ajax.url = origurl + '¢ralauthtoken=' + token;
return oldupload.call( upload.api, file, data );
} );
};
oldajax = this.api.ajax;
this.api.ajax = function ( p, o ) {
return upload.getCAToken().then( function ( token ) {
upload.api.defaults.ajax.url = origurl + '¢ralauthtoken=' + token;
return oldajax.call( upload.api, p, o );
} );
};
this.date = undefined;
this.descriptions = [];
this.categories = [];
}
OO.inheritClass( CommonsUpload, mw.Upload );
CUP = CommonsUpload.prototype;
CUP.getCAToken = function () {
var localApi = new mw.Api();
return localApi.get( {
action: 'centralauthtoken'
} ).then( function ( result ) {
return result.centralauthtoken.centralauthtoken;
} );
};
/**
* Add categories to the upload.
* @param {string[]} categories Array of categories to which this upload will be added.
*/
CUP.addCategories = function ( categories ) {
var upload = this;
$.each( categories, function ( i, category ) {
upload.categories.push( category );
} );
};
/**
* Add a description to the upload.
* @param {string} language The language code for the description's language. Must have a template on the target wiki to work properly.
* @param {string} description The description of the file.
*/
CUP.addDescription = function ( language, description ) {
this.descriptions.push( {
language: language,
text: description
} );
};
/**
* Set the date of creation for the upload.
* @param {Date} date
*/
CUP.setDate = function ( date ) {
this.date = date;
};
/**
* Get the text of the file page, to be created on upload. Brings together
* several different pieces of information to create useful text.
* @return {string}
*/
CUP.getText = function () {
return [
'{{',
this.getTemplateName(),
'|description=',
this.getDescriptions(),
'|date=',
this.getDate(),
'|source=',
this.getUser(),
'|author=',
this.getUser(),
'}}\n\n',
this.getLicense(),
'\n\n',
this.getCategories()
].join( '' );
};
/**
* Gets the wikitext for the creation date of this upload.
* @return {string}
*/
CUP.getDate = function () {
if ( !this.date ) {
return '';
}
return this.date.toString();
};
/**
* Gets the name of the template to use for creating the file metadata.
* Override in subclasses for other templates.
* @return {string}
*/
CUP.getTemplateName = function () {
return 'Information';
};
/**
* Fetches the wikitext for any descriptions that have been added
* to the upload.
* @return {string}
*/
CUP.getDescriptions = function () {
var desctext = '';
$.each( this.descriptions, function ( i, desc ) {
desctext += '{{' + desc.language + '|' + desc.text + '}}';
} );
return desctext;
};
/**
* Fetches the wikitext for the categories to which the upload will
* be added.
* @return {string}
*/
CUP.getCategories = function () {
var cattext = '';
$.each( this.categories, function ( i, cat ) {
cattext += '[[' + 'Category:' + cat + ']]';
} );
return cattext;
};
/**
* Gets the wikitext for the license of the upload. Abstract for now.
* @abstract
* @return {string}
*/
CUP.getLicense = function () {
return '';
};
/**
* Get the username.
* @return {string}
*/
CUP.getUser = function () {
return mw.user.getName();
};
mw.CommonsUpload = CommonsUpload;
return $.Deferred().resolve();
} );
}( mediaWiki, jQuery ) );
// </nowiki>