Merge "Reopen db connection after doing SpecialPageCacheUpdates"
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.ForeignStructuredUpload.js
index 768efc3..0c572d4 100644 (file)
@@ -1,8 +1,5 @@
-( function ( mw, OO ) {
+( function ( mw, $, OO ) {
        /**
-        * @class mw.ForeignStructuredUpload
-        * @extends mw.ForeignUpload
-        *
         * Used to represent an upload in progress on the frontend.
         *
         * This subclass will upload to a wiki using a structured metadata
         * **TODO: This currently only supports uploads under CC-BY-SA 4.0,
         * and should really have support for more licenses.**
         *
-        * @inheritdoc
+        * @class mw.ForeignStructuredUpload
+        * @extends mw.ForeignUpload
+        *
+        * @constructor
+        * @param {string} [target]
+        * @param {Object} [apiconfig]
         */
        function ForeignStructuredUpload( target, apiconfig ) {
                this.date = undefined;
                this.descriptions = [];
                this.categories = [];
 
+               // Config for uploads to local wiki.
+               // Can be overridden with foreign wiki config when #loadConfig is called.
+               this.config = mw.config.get( 'wgUploadDialog' );
+
                mw.ForeignUpload.call( this, target, apiconfig );
        }
 
        OO.inheritClass( ForeignStructuredUpload, mw.ForeignUpload );
 
+       /**
+        * Get the configuration for the form and filepage from the foreign wiki, if any, and use it for
+        * this upload.
+        *
+        * @return {jQuery.Promise} Promise returning config object
+        */
+       ForeignStructuredUpload.prototype.loadConfig = function () {
+               var deferred,
+                       upload = this;
+
+               if ( this.configPromise ) {
+                       return this.configPromise;
+               }
+
+               if ( this.target === 'local' ) {
+                       deferred = $.Deferred();
+                       setTimeout( function () {
+                               // Resolve asynchronously, so that it's harder to accidentally write synchronous code that
+                               // will break for cross-wiki uploads
+                               deferred.resolve( upload.config );
+                       } );
+                       this.configPromise = deferred.promise();
+               } else {
+                       this.configPromise = this.apiPromise.then( function ( api ) {
+                               // Get the config from the foreign wiki
+                               return api.get( {
+                                       action: 'query',
+                                       meta: 'siteinfo',
+                                       siprop: 'uploaddialog',
+                                       // For convenient true/false booleans
+                                       formatversion: 2
+                               } ).then( function ( resp ) {
+                                       // Foreign wiki might be running a pre-1.27 MediaWiki, without support for this
+                                       if ( resp.query && resp.query.uploaddialog ) {
+                                               upload.config = resp.query.uploaddialog;
+                                               return upload.config;
+                                       } else {
+                                               return $.Deferred().reject( 'upload-foreign-cant-load-config' );
+                                       }
+                               }, function () {
+                                       return $.Deferred().reject( 'upload-foreign-cant-load-config' );
+                               } );
+                       } );
+               }
+
+               return this.configPromise;
+       };
+
        /**
         * Add categories to the upload.
         *
         * @return {string}
         */
        ForeignStructuredUpload.prototype.getText = function () {
-               return (
-                       '== {{int:filedesc}} ==\n' +
-                       '{{' +
-                       this.getTemplateName() +
-                       '\n|description=' +
-                       this.getDescriptions() +
-                       '\n|date=' +
-                       this.getDate() +
-                       '\n|source=' +
-                       this.getSource() +
-                       '\n|author=' +
-                       this.getUser() +
-                       '\n}}\n\n' +
-                       '== {{int:license-header}} ==\n' +
-                       this.getLicense() +
-                       '\n\n' +
-                       this.getCategories()
-               );
+               return this.config.format.filepage
+                       // Replace "named parameters" with the given information
+                       .replace( '$DESCRIPTION', this.getDescriptions() )
+                       .replace( '$DATE', this.getDate() )
+                       .replace( '$SOURCE', this.getSource() )
+                       .replace( '$AUTHOR', this.getUser() )
+                       .replace( '$LICENSE', this.getLicense() )
+                       .replace( '$CATEGORIES', this.getCategories() );
+       };
+
+       /**
+        * @inheritdoc
+        */
+       ForeignStructuredUpload.prototype.getComment = function () {
+               var
+                       isLocal = this.target === 'local',
+                       comment = typeof this.config.comment === 'string' ?
+                               this.config.comment :
+                               this.config.comment[ isLocal ? 'local' : 'foreign' ];
+               return comment
+                       .replace( '$PAGENAME', mw.config.get( 'wgPageName' ) )
+                       .replace( '$HOST', location.host );
        };
 
        /**
                return this.date.toString();
        };
 
-       /**
-        * Gets the name of the template to use for creating the file metadata.
-        * Override in subclasses for other templates.
-        *
-        * @private
-        * @return {string}
-        */
-       ForeignStructuredUpload.prototype.getTemplateName = function () {
-               return 'Information';
-       };
-
        /**
         * Fetches the wikitext for any descriptions that have been added
         * to the upload.
 
                for ( i = 0; i < this.descriptions.length; i++ ) {
                        desc = this.descriptions[ i ];
-                       templateCalls.push( '{{' + desc.language + '|1=' + desc.text + '}}' );
+                       templateCalls.push(
+                               this.config.format.description
+                                       .replace( '$LANGUAGE', desc.language )
+                                       .replace( '$TEXT', desc.text )
+                       );
                }
 
                return templateCalls.join( '\n' );
                var i, cat, categoryLinks = [];
 
                if ( this.categories.length === 0 ) {
-                       return '{{subst:unc}}';
+                       return this.config.format.uncategorized;
                }
 
                for ( i = 0; i < this.categories.length; i++ ) {
         * @return {string}
         */
        ForeignStructuredUpload.prototype.getLicense = function () {
-               // Make sure this matches the messages for different targets in
-               // mw.ForeignStructuredUpload.BookletLayout.prototype.renderUploadForm
-               return this.target === 'shared' ? '{{self|cc-by-sa-4.0}}' : '';
+               return this.config.format.license;
        };
 
        /**
         * @return {string}
         */
        ForeignStructuredUpload.prototype.getSource = function () {
-               return '{{own}}';
+               return this.config.format.ownwork;
        };
 
        /**
         * @return {string}
         */
        ForeignStructuredUpload.prototype.getUser = function () {
-               var username = mw.config.get( 'wgUserName' );
-               // Do not localise 'User:', we don't know the language of target wiki
-               return '[[User:' + username + '|' + username + ']]';
+               var username, namespace;
+               // Do not localise, we don't know the language of target wiki
+               namespace = 'User';
+               username = mw.config.get( 'wgUserName' );
+               if ( !username ) {
+                       // The user is not logged in locally. However, they might be logged in on the foreign wiki.
+                       // We should record their username there. (If they're not logged in there either, this will
+                       // record the IP address.) It's also possible that the user opened this dialog, got an error
+                       // about not being logged in, logged in in another browser tab, then continued uploading.
+                       username = '{{subst:REVISIONUSER}}';
+               }
+               return '[[' + namespace + ':' + username + '|' + username + ']]';
        };
 
        mw.ForeignStructuredUpload = ForeignStructuredUpload;
-}( mediaWiki, OO ) );
+}( mediaWiki, jQuery, OO ) );