38c910606c00bfb59d6c5b3c273a1e589e17921b
1 ( function ( mw
, OO
, $ ) {
3 * @class mw.ForeignUpload
6 * Used to represent an upload in progress on the frontend.
8 * Subclassed to upload to a foreign API, with no other goodies. Use
9 * this for a generic foreign image repository on your wiki farm.
11 * Note you can provide the {@link #target target} or not - if the first argument is
12 * an object, we assume you want the default, and treat it as apiconfig
16 * @param {string} [target] Used to set up the target
17 * wiki. If not remote, this class behaves identically to mw.Upload (unless further subclassed)
18 * Use the same names as set in $wgForeignFileRepos for this. Also,
19 * make sure there is an entry in the $wgForeignUploadTargets array
20 * set to "true" for this name.
21 * @param {Object} [apiconfig] Passed to the constructor of mw.ForeignApi or mw.Api, as needed.
23 function ForeignUpload( target
, apiconfig
) {
24 var api
, upload
= this;
26 if ( typeof target
=== 'object' ) {
27 // target probably wasn't passed in, it must
35 // Now we have several different options.
36 // If the local wiki is the target, then we can skip a bunch of steps
37 // and just return an mw.Api object, because we don't need any special
38 // configuration for that.
39 // However, if the target is a remote wiki, we must check the API
40 // to confirm that the target is one that this site is configured to
42 if ( this.target
=== 'local' ) {
43 // We'll ignore the CORS and centralauth stuff if the target is
45 this.apiPromise
= $.Deferred().resolve( new mw
.Api( apiconfig
) );
48 this.apiPromise
= api
.get( {
51 friprop
: [ 'name', 'scriptDirUrl', 'canUpload' ]
52 } ).then( function ( data
) {
54 repos
= data
.query
.repos
;
56 // First pass - try to find the passed-in target and check
57 // that it's configured for uploads.
61 // Skip repos that are not our target, or if they
62 // are the target, cannot be uploaded to.
63 if ( repo
.name
=== upload
.target
&& repo
.canUpload
) {
64 return new mw
.ForeignApi(
65 repo
.scriptDirUrl
+ '/api.php',
71 // Second pass - none of the configured repos were our
72 // passed-in target, just look for the first one that would
77 if ( repo
.canUpload
) {
78 return new mw
.ForeignApi(
79 repo
.scriptDirUrl
+ '/api.php',
85 // No luck finding the correct foreign repo, default to local.
86 return $.Deferred().resolve( new mw
.Api( apiconfig
) );
90 // Build the upload object without an API - this class overrides the
91 // actual API call methods to wait for the apiPromise to resolve
93 mw
.Upload
.call( this, null );
96 OO
.inheritClass( ForeignUpload
, mw
.Upload
);
99 * @property {string} target
100 * Used to specify the target repository of the upload.
102 * If you set this to something that isn't 'local', you must be sure to
103 * add that target to $wgForeignUploadTargets in LocalSettings, and the
104 * repository must be set up to use CORS and CentralAuth.
106 * Most wikis use "shared" to refer to Wikimedia Commons, we assume that
107 * in this class and in the messages linked to it.
111 * Override from mw.Upload to make sure the API info is found and allowed
113 ForeignUpload
.prototype.upload = function () {
115 return this.apiPromise
.then( function ( api
) {
117 return mw
.Upload
.prototype.upload
.call( upload
);
122 * Override from mw.Upload to make sure the API info is found and allowed
124 ForeignUpload
.prototype.uploadToStash = function () {
126 return this.apiPromise
.then( function ( api
) {
128 return mw
.Upload
.prototype.uploadToStash
.call( upload
);
132 mw
.ForeignUpload
= ForeignUpload
;
133 }( mediaWiki
, OO
, jQuery
) );