Jump to content

User:MarkTraceur/commonsUpload.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// <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 + '&centralauthtoken=' + 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 + '&centralauthtoken=' + 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>