3 const config = require( './config.json' );
6 * @classdesc Upload to another MediaWiki site.
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 `target` or not - if the first argument is
12 * an object, we assume you want the default, and treat it as apiconfig
15 * @class mw.ForeignUpload
19 * @description Used to represent an upload in progress on the frontend.
20 * @param {string} [target] Used to set up the target
21 * wiki. If not remote, this class behaves identically to mw.Upload (unless further subclassed)
22 * Use the same names as set in $wgForeignFileRepos for this. Also,
23 * make sure there is an entry in the $wgForeignUploadTargets array for this name.
24 * @param {Object} [apiconfig] Passed to the constructor of {@link mw.ForeignApi} or {@link mw.Api}, as needed.
26 function ForeignUpload( target, apiconfig ) {
27 const validTargets = config.ForeignUploadTargets;
29 if ( typeof target === 'object' ) {
30 // target probably wasn't passed in, it must
36 // * Use the given `target` first;
37 // * If not given, fall back to default (first) ForeignUploadTarget;
38 // * If none is configured, fall back to local uploads.
39 this.target = target || validTargets[ 0 ] || 'local';
41 // Now we have several different options.
42 // If the local wiki is the target, then we can skip a bunch of steps
43 // and just return an mw.Api object, because we don't need any special
44 // configuration for that.
45 // However, if the target is a remote wiki, we must check the API
46 // to confirm that the target is one that this site is configured to
48 if ( validTargets.length === 0 ) {
49 this.apiPromise = $.Deferred().reject( 'upload-dialog-disabled' );
50 } else if ( this.target === 'local' ) {
51 // If local uploads were requested, but they are disabled, fail.
52 if ( !config.EnableUploads ) {
53 this.apiPromise = $.Deferred().reject( 'uploaddisabledtext' );
55 // We'll ignore the CORS and centralauth stuff if the target is
57 this.apiPromise = $.Deferred().resolve( new mw.Api( apiconfig ) );
60 const api = new mw.Api();
61 this.apiPromise = api.get( {
64 friprop: [ 'name', 'scriptDirUrl', 'canUpload' ]
65 } ).then( ( data ) => {
66 const repos = data.query.repos;
68 // First pass - try to find the passed-in target and check
69 // that it's configured for uploads.
70 for ( const i in repos ) {
71 const repo = repos[ i ];
73 // Skip repos that are not our target, or if they
74 // are the target, cannot be uploaded to.
75 if ( repo.name === this.target && repo.canUpload === '' ) {
76 return new mw.ForeignApi(
77 repo.scriptDirUrl + '/api.php',
83 return $.Deferred().reject( 'upload-foreign-cant-upload' );
87 // Build the upload object without an API - this class overrides the
88 // actual API call methods to wait for the apiPromise to resolve
90 mw.Upload.call( this, null );
93 OO.inheritClass( ForeignUpload, mw.Upload );
96 * Used to specify the target repository of the upload.
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.
102 * Most wikis use "shared" to refer to Wikimedia Commons, we assume that
103 * in this class and in the messages linked to it.
105 * Defaults to the first available foreign upload target,
106 * or to local uploads if no foreign target is configured.
108 * @name mw.ForeignUpload.target
115 ForeignUpload.prototype.getApi = function () {
116 return this.apiPromise;
120 * Override from mw.Upload to make sure the API info is found and allowed
124 ForeignUpload.prototype.upload = function () {
125 return this.apiPromise.then( ( api ) => {
127 return mw.Upload.prototype.upload.call( this );
132 * Override from mw.Upload to make sure the API info is found and allowed
136 ForeignUpload.prototype.uploadToStash = function () {
137 return this.apiPromise.then( ( api ) => {
139 return mw.Upload.prototype.uploadToStash.call( this );
143 mw.ForeignUpload = ForeignUpload;