Quick fixes for mw.ForeignUpload
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.ForeignUpload.js
1 ( function ( mw, OO, $ ) {
2 /**
3 * @class mw.ForeignUpload
4 * @extends mw.Upload
5 *
6 * Used to represent an upload in progress on the frontend.
7 *
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.
10 *
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
13 * instead.
14 *
15 * @constructor
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.
22 */
23 function ForeignUpload( target, apiconfig ) {
24 var api, upload = this;
25
26 if ( typeof target === 'object' ) {
27 // target probably wasn't passed in, it must
28 // be apiconfig
29 apiconfig = target;
30 target = undefined;
31 }
32
33 this.target = target;
34
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
41 // support.
42 if ( this.target === 'local' ) {
43 // We'll ignore the CORS and centralauth stuff if the target is
44 // the local wiki.
45 this.apiPromise = $.Deferred().resolve( new mw.Api( apiconfig ) );
46 } else {
47 api = new mw.Api();
48 this.apiPromise = api.get( {
49 action: 'query',
50 meta: 'filerepoinfo',
51 friprop: [ 'name', 'scriptDirUrl', 'canUpload' ]
52 } ).then( function ( data ) {
53 var i, repo,
54 repos = data.query.repos;
55
56 // First pass - try to find the passed-in target and check
57 // that it's configured for uploads.
58 for ( i in repos ) {
59 repo = repos[ i ];
60
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',
66 apiconfig
67 );
68 }
69 }
70
71 // Second pass - none of the configured repos were our
72 // passed-in target, just look for the first one that would
73 // work.
74 for ( i in repos ) {
75 repo = repos[ i ];
76
77 if ( repo.canUpload ) {
78 return new mw.ForeignApi(
79 repo.scriptDirUrl + '/api.php',
80 apiconfig
81 );
82 }
83 }
84
85 // No luck finding the correct foreign repo, default to local.
86 return $.Deferred().resolve( new mw.Api( apiconfig ) );
87 } );
88 }
89
90 // Build the upload object without an API - this class overrides the
91 // actual API call methods to wait for the apiPromise to resolve
92 // before continuing.
93 mw.Upload.call( this, null );
94 }
95
96 OO.inheritClass( ForeignUpload, mw.Upload );
97
98 /**
99 * @property {string} target
100 * Used to specify the target repository of the upload.
101 *
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.
105 *
106 * Most wikis use "shared" to refer to Wikimedia Commons, we assume that
107 * in this class and in the messages linked to it.
108 */
109
110 /**
111 * Override from mw.Upload to make sure the API info is found and allowed
112 */
113 ForeignUpload.prototype.upload = function () {
114 var upload = this;
115 return this.apiPromise.then( function ( api ) {
116 upload.api = api;
117 return mw.Upload.prototype.upload.call( upload );
118 } );
119 };
120
121 /**
122 * Override from mw.Upload to make sure the API info is found and allowed
123 */
124 ForeignUpload.prototype.uploadToStash = function () {
125 var upload = this;
126 return this.apiPromise.then( function ( api ) {
127 upload.api = api;
128 return mw.Upload.prototype.uploadToStash.call( upload );
129 } );
130 };
131
132 mw.ForeignUpload = ForeignUpload;
133 }( mediaWiki, OO, jQuery ) );