Quick fixes for mw.ForeignUpload
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.ForeignUpload.js
index a367ee0..38c9106 100644 (file)
@@ -8,12 +8,12 @@
         * Subclassed to upload to a foreign API, with no other goodies. Use
         * this for a generic foreign image repository on your wiki farm.
         *
-        * Note you can provide the {@link #targetHost targetHost} or not - if the first argument is
+        * Note you can provide the {@link #target target} or not - if the first argument is
         * an object, we assume you want the default, and treat it as apiconfig
         * instead.
         *
         * @constructor
-        * @param {string} [target="local"] Used to set up the target
+        * @param {string} [target] Used to set up the target
         *     wiki. If not remote, this class behaves identically to mw.Upload (unless further subclassed)
         *     Use the same names as set in $wgForeignFileRepos for this. Also,
         *     make sure there is an entry in the $wgForeignUploadTargets array
@@ -30,9 +30,7 @@
                        target = undefined;
                }
 
-               // Resolve defaults etc. - if target isn't passed in, we use
-               // the default.
-               this.target = target || this.target;
+               this.target = target;
 
                // Now we have several different options.
                // If the local wiki is the target, then we can skip a bunch of steps
                // However, if the target is a remote wiki, we must check the API
                // to confirm that the target is one that this site is configured to
                // support.
-               if ( this.target !== 'local' ) {
+               if ( this.target === 'local' ) {
+                       // We'll ignore the CORS and centralauth stuff if the target is
+                       // the local wiki.
+                       this.apiPromise = $.Deferred().resolve( new mw.Api( apiconfig ) );
+               } else {
                        api = new mw.Api();
                        this.apiPromise = api.get( {
                                action: 'query',
                                var i, repo,
                                        repos = data.query.repos;
 
+                               // First pass - try to find the passed-in target and check
+                               // that it's configured for uploads.
                                for ( i in repos ) {
                                        repo = repos[ i ];
 
-                                       if ( repo.name === upload.target ) {
-                                               // This is our target repo.
-                                               if ( !repo.canUpload ) {
-                                                       // But it's not configured correctly.
-                                                       return $.Deferred().reject( 'repo-cannot-upload' );
-                                               }
+                                       // Skip repos that are not our target, or if they
+                                       // are the target, cannot be uploaded to.
+                                       if ( repo.name === upload.target && repo.canUpload ) {
+                                               return new mw.ForeignApi(
+                                                       repo.scriptDirUrl + '/api.php',
+                                                       apiconfig
+                                               );
+                                       }
+                               }
 
+                               // Second pass - none of the configured repos were our
+                               // passed-in target, just look for the first one that would
+                               // work.
+                               for ( i in repos ) {
+                                       repo = repos[ i ];
+
+                                       if ( repo.canUpload ) {
                                                return new mw.ForeignApi(
                                                        repo.scriptDirUrl + '/api.php',
                                                        apiconfig
                                                );
                                        }
                                }
+
+                               // No luck finding the correct foreign repo, default to local.
+                               return $.Deferred().resolve( new mw.Api( apiconfig ) );
                        } );
-               } else {
-                       // We'll ignore the CORS and centralauth stuff if the target is
-                       // the local wiki.
-                       this.apiPromise = $.Deferred().resolve( new mw.Api( apiconfig ) );
                }
 
                // Build the upload object without an API - this class overrides the
        OO.inheritClass( ForeignUpload, mw.Upload );
 
        /**
-        * @property targetHost
+        * @property {string} target
         * Used to specify the target repository of the upload.
         *
         * If you set this to something that isn't 'local', you must be sure to
         * add that target to $wgForeignUploadTargets in LocalSettings, and the
         * repository must be set up to use CORS and CentralAuth.
+        *
+        * Most wikis use "shared" to refer to Wikimedia Commons, we assume that
+        * in this class and in the messages linked to it.
         */
-       ForeignUpload.prototype.target = 'local';
 
        /**
         * Override from mw.Upload to make sure the API info is found and allowed