resources: Strip '$' and 'mw' from file closures
[lhc/web/wiklou.git] / resources / src / mediawiki.ForeignUpload.js
1 ( function () {
2 /**
3 * Used to represent an upload in progress on the frontend.
4 *
5 * Subclassed to upload to a foreign API, with no other goodies. Use
6 * this for a generic foreign image repository on your wiki farm.
7 *
8 * Note you can provide the {@link #target target} or not - if the first argument is
9 * an object, we assume you want the default, and treat it as apiconfig
10 * instead.
11 *
12 * @class mw.ForeignUpload
13 * @extends mw.Upload
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 for this name.
20 * @param {Object} [apiconfig] Passed to the constructor of mw.ForeignApi or mw.Api, as needed.
21 */
22 function ForeignUpload( target, apiconfig ) {
23 var api,
24 validTargets = mw.config.get( 'wgForeignUploadTargets' ),
25 upload = this;
26
27 if ( typeof target === 'object' ) {
28 // target probably wasn't passed in, it must
29 // be apiconfig
30 apiconfig = target;
31 target = undefined;
32 }
33
34 // * Use the given `target` first;
35 // * If not given, fall back to default (first) ForeignUploadTarget;
36 // * If none is configured, fall back to local uploads.
37 this.target = target || validTargets[ 0 ] || 'local';
38
39 // Now we have several different options.
40 // If the local wiki is the target, then we can skip a bunch of steps
41 // and just return an mw.Api object, because we don't need any special
42 // configuration for that.
43 // However, if the target is a remote wiki, we must check the API
44 // to confirm that the target is one that this site is configured to
45 // support.
46 if ( validTargets.length === 0 ) {
47 this.apiPromise = $.Deferred().reject( 'upload-dialog-disabled' );
48 } else if ( this.target === 'local' ) {
49 // If local uploads were requested, but they are disabled, fail.
50 if ( !mw.config.get( 'wgEnableUploads' ) ) {
51 this.apiPromise = $.Deferred().reject( 'uploaddisabledtext' );
52 } else {
53 // We'll ignore the CORS and centralauth stuff if the target is
54 // the local wiki.
55 this.apiPromise = $.Deferred().resolve( new mw.Api( apiconfig ) );
56 }
57 } else {
58 api = new mw.Api();
59 this.apiPromise = api.get( {
60 action: 'query',
61 meta: 'filerepoinfo',
62 friprop: [ 'name', 'scriptDirUrl', 'canUpload' ]
63 } ).then( function ( data ) {
64 var i, repo,
65 repos = data.query.repos;
66
67 // First pass - try to find the passed-in target and check
68 // that it's configured for uploads.
69 for ( i in repos ) {
70 repo = repos[ i ];
71
72 // Skip repos that are not our target, or if they
73 // are the target, cannot be uploaded to.
74 if ( repo.name === upload.target && repo.canUpload === '' ) {
75 return new mw.ForeignApi(
76 repo.scriptDirUrl + '/api.php',
77 apiconfig
78 );
79 }
80 }
81
82 return $.Deferred().reject( 'upload-foreign-cant-upload' );
83 } );
84 }
85
86 // Build the upload object without an API - this class overrides the
87 // actual API call methods to wait for the apiPromise to resolve
88 // before continuing.
89 mw.Upload.call( this, null );
90 }
91
92 OO.inheritClass( ForeignUpload, mw.Upload );
93
94 /**
95 * @property {string} target
96 * Used to specify the target repository of the upload.
97 *
98 * If you set this to something that isn't 'local', you must be sure to
99 * add that target to $wgForeignUploadTargets in LocalSettings, and the
100 * repository must be set up to use CORS and CentralAuth.
101 *
102 * Most wikis use "shared" to refer to Wikimedia Commons, we assume that
103 * in this class and in the messages linked to it.
104 *
105 * Defaults to the first available foreign upload target,
106 * or to local uploads if no foreign target is configured.
107 */
108
109 /**
110 * @inheritdoc
111 */
112 ForeignUpload.prototype.getApi = function () {
113 return this.apiPromise;
114 };
115
116 /**
117 * Override from mw.Upload to make sure the API info is found and allowed
118 *
119 * @inheritdoc
120 */
121 ForeignUpload.prototype.upload = function () {
122 var upload = this;
123 return this.apiPromise.then( function ( api ) {
124 upload.api = api;
125 return mw.Upload.prototype.upload.call( upload );
126 } );
127 };
128
129 /**
130 * Override from mw.Upload to make sure the API info is found and allowed
131 *
132 * @inheritdoc
133 */
134 ForeignUpload.prototype.uploadToStash = function () {
135 var upload = this;
136 return this.apiPromise.then( function ( api ) {
137 upload.api = api;
138 return mw.Upload.prototype.uploadToStash.call( upload );
139 } );
140 };
141
142 mw.ForeignUpload = ForeignUpload;
143 }() );