MarkTraceur has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/231408

Change subject: Add (unused) library for uploading to Commons
......................................................................

Add (unused) library for uploading to Commons

No license support yet, but this is the library we will be using to
expand our upload tool repertoire on other wikis. It has CORS and
CentralAuth support for cross-wiki uploading to Commons, and will also
work on Commons itself.

Change-Id: I7042648303ba1edb45a1caee34ed73b4d63b59aa
---
M UploadWizardHooks.php
A resources/lib/mw.CommonsUpload.js
2 files changed, 238 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/UploadWizard 
refs/changes/08/231408/1

diff --git a/UploadWizardHooks.php b/UploadWizardHooks.php
index 51fea10..6058ebd 100644
--- a/UploadWizardHooks.php
+++ b/UploadWizardHooks.php
@@ -722,6 +722,17 @@
                                'mwe-upwiz-file-all-failed',
                        ),
                ),
+
+               'mediawiki.CommonsUpload' => array(
+                       'scripts' => array(
+                               'resources/lib/mw.CommonsUpload.js',
+                       ),
+
+                       'dependencies' => array(
+                               'oojs',
+                               'mediawiki.Upload',
+                       ),
+               ),
        );
 
        /**
diff --git a/resources/lib/mw.CommonsUpload.js 
b/resources/lib/mw.CommonsUpload.js
new file mode 100644
index 0000000..693382e
--- /dev/null
+++ b/resources/lib/mw.CommonsUpload.js
@@ -0,0 +1,227 @@
+( function ( mw, $, OO ) {
+       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, particularly optional CORS and CentralAuth support, and
+        * the {{information}} template that Commons uses to create structured
+        * metadata on file pages.
+        *
+        * Note that the apiconfig parameter may be modified if the upload is 
not
+        * happening on commons.wikimedia.org.
+        *
+        * @TODO license support
+        *
+        * @constructor
+        * @param {Object} [apiconfig] Passed to the constructor of mw.Api.
+        */
+       function CommonsUpload( apiconfig ) {
+               var origurl, oldupload, oldajax, needCors,
+                       origin, target,
+                       upload = this;
+
+               // We'll ignore the CORS and centralauth stuff if we're on 
Commons already
+               needCors = document.location.host !== this.targetHost;
+
+               apiconfig = apiconfig || {};
+               apiconfig.ajax = apiconfig.ajax || {};
+
+               if ( needCors ) {
+                       target = document.location.protocol + '//' + 
this.targetHost;
+                       origin = document.location.protocol + '//' + 
document.location.host;
+
+                       // CAUTION currently set to target beta, change if 
moving to production
+                       apiconfig.ajax.url = apiconfig.ajax.url || ( target + 
'/w/api.php?origin=' + encodeURIComponent( origin ) );
+
+                       origurl = apiconfig.ajax.url;
+               }
+
+               mw.Upload.call( this, apiconfig );
+
+               if ( needCors ) {
+                       // Override some core API methods so we can get a 
CentralAuth
+                       // token if we need to do CORS.
+                       oldupload = this.api.upload;
+
+                       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;
+
+       /**
+        * @property targetHost
+        * Used to specify the target repository of the upload.
+        *
+        * You could override this to point at something that isn't Commons,
+        * but be sure it has the correct templates and is CORS and CentralAuth
+        * ready.
+        */
+       CUP.targetHost = 'commons.wikimedia.org';
+
+       /**
+        * Used to fetch a centralauth token from the local API so we
+        * can perform a cross-site request later.
+        * @return {string}
+        */
+       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;
+}( mediaWiki, jQuery, OO ) );

-- 
To view, visit https://gerrit.wikimedia.org/r/231408
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7042648303ba1edb45a1caee34ed73b4d63b59aa
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/UploadWizard
Gerrit-Branch: master
Gerrit-Owner: MarkTraceur <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to