From 6d2f491d2178091f83820cdc5c1dad9d76a9ef0c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bartosz=20Dziewo=C5=84ski?= Date: Wed, 7 Oct 2015 21:32:20 +0200 Subject: [PATCH] More quick fixes for mw.ForeignUpload Follow-up to 2814053cb0c27bda0a76b21ffc9eeb31ef9726b1. Maybe it works this time! Change-Id: I2633c87ac8b78d411eaa4622c7ad62e35f4cac78 --- includes/DefaultSettings.php | 5 +- .../ResourceLoaderStartUpModule.php | 1 + ...i.ForeignStructuredUpload.BookletLayout.js | 57 ++++++++++++------- .../mediawiki.ForeignStructuredUpload.js | 4 +- .../src/mediawiki/mediawiki.ForeignUpload.js | 38 ++++++------- 5 files changed, 60 insertions(+), 45 deletions(-) diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 95baa56000..7733c93604 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -526,9 +526,12 @@ $wgForeignFileRepos = array(); $wgUseInstantCommons = false; /** - * Array of foreign file repos (set in $wgForeignFileRepos above) that + * Array of foreign file repo names (set in $wgForeignFileRepos above) that * are allowable upload targets. These wikis must have some method of * authentication (i.e. CentralAuth), and be CORS-enabled for this wiki. + * + * Example: + * $wgForeignUploadTargets = array( 'shared' ); */ $wgForeignUploadTargets = array(); diff --git a/includes/resourceloader/ResourceLoaderStartUpModule.php b/includes/resourceloader/ResourceLoaderStartUpModule.php index eabafbd540..bc9a5b819a 100644 --- a/includes/resourceloader/ResourceLoaderStartUpModule.php +++ b/includes/resourceloader/ResourceLoaderStartUpModule.php @@ -103,6 +103,7 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule { 'wgResourceLoaderStorageEnabled' => $conf->get( 'ResourceLoaderStorageEnabled' ), 'wgResourceLoaderLegacyModules' => self::getLegacyModules(), 'wgForeignUploadTargets' => $conf->get( 'ForeignUploadTargets' ), + 'wgEnableUploads' => $conf->get( 'EnableUploads' ), ); Hooks::run( 'ResourceLoaderGetConfigVars', array( &$vars ) ); diff --git a/resources/src/mediawiki/mediawiki.ForeignStructuredUpload.BookletLayout.js b/resources/src/mediawiki/mediawiki.ForeignStructuredUpload.BookletLayout.js index 825e738699..4e3f771e9a 100644 --- a/resources/src/mediawiki/mediawiki.ForeignStructuredUpload.BookletLayout.js +++ b/resources/src/mediawiki/mediawiki.ForeignStructuredUpload.BookletLayout.js @@ -8,7 +8,7 @@ * var uploadDialog = new mw.Upload.Dialog( { * bookletClass: mw.ForeignStructuredUpload.BookletLayout, * booklet: { - * targetHost: 'localhost:8080' + * target: 'local' * } * } ); * var windowManager = new OO.ui.WindowManager(); @@ -18,7 +18,7 @@ * @class mw.ForeignStructuredUpload.BookletLayout * @uses mw.ForeignStructuredUpload * @extends mw.Upload.BookletLayout - * @cfg {string} [targetHost] Used to set up the target wiki. + * @cfg {string} [target] Used to choose the target repository. * If nothing is passed, the {@link mw.ForeignUpload#property-target default} is used. */ mw.ForeignStructuredUpload.BookletLayout = function ( config ) { @@ -26,7 +26,7 @@ // Parent constructor mw.ForeignStructuredUpload.BookletLayout.parent.call( this, config ); - this.targetHost = config.targetHost; + this.target = config.target; }; /* Setup */ @@ -37,13 +37,13 @@ /** * Returns a {@link mw.ForeignStructuredUpload mw.ForeignStructuredUpload} - * with the {@link #cfg-targetHost targetHost} specified in config. + * with the {@link #cfg-target target} specified in config. * * @protected * @return {mw.Upload} */ mw.ForeignStructuredUpload.BookletLayout.prototype.createUpload = function () { - return new mw.ForeignStructuredUpload( this.targetHost ); + return new mw.ForeignStructuredUpload( this.target ); }; /* Form renderers */ @@ -52,24 +52,39 @@ * @inheritdoc */ mw.ForeignStructuredUpload.BookletLayout.prototype.renderUploadForm = function () { - var fieldset, - targets = mw.config.get( 'wgForeignUploadTargets' ), - // Default to using local, but try to use a configured target. - // TODO allow finer configuration of this somehow? - target = ( targets && targets.length ) ? targets[ 0 ] : 'local', - $ownWorkMessage = $( '

' ).html( - mw.message( 'foreign-structured-upload-form-label-own-work-message-' + target ).parse() - ), - $notOwnWorkMessage = $( '

' ).append( - $( '

' ).html( - mw.message( 'foreign-structured-upload-form-label-not-own-work-message-' + target ).parse() - ), - $( '

' ).html( - mw.message( 'foreign-structured-upload-form-label-not-own-work-local-' + target ).parse() - ) - ), + var fieldset, $ownWorkMessage, $notOwnWorkMessage, + ownWorkMessage, notOwnWorkMessage, notOwnWorkLocal, + validTargets = mw.config.get( 'wgForeignUploadTargets' ), + target = this.target || validTargets[ 0 ] || 'local', layout = this; + // foreign-structured-upload-form-label-own-work-message-local + // foreign-structured-upload-form-label-own-work-message-shared + ownWorkMessage = mw.message( 'foreign-structured-upload-form-label-own-work-message-' + target ); + // foreign-structured-upload-form-label-not-own-work-message-local + // foreign-structured-upload-form-label-not-own-work-message-shared + notOwnWorkMessage = mw.message( 'foreign-structured-upload-form-label-not-own-work-message-' + target ); + // foreign-structured-upload-form-label-not-own-work-local-local + // foreign-structured-upload-form-label-not-own-work-local-shared + notOwnWorkLocal = mw.message( 'foreign-structured-upload-form-label-not-own-work-local-' + target ); + + if ( !ownWorkMessage.exists() ) { + ownWorkMessage = mw.message( 'foreign-structured-upload-form-label-own-work-message-default' ); + } + if ( !notOwnWorkMessage.exists() ) { + notOwnWorkMessage = mw.message( 'foreign-structured-upload-form-label-not-own-work-message-default' ); + } + if ( !notOwnWorkLocal.exists() ) { + notOwnWorkLocal = mw.message( 'foreign-structured-upload-form-label-not-own-work-local-default' ); + } + + $ownWorkMessage = $( '

' ).html( ownWorkMessage.parse() ); + $notOwnWorkMessage = $( '

' ).append( + $( '

' ).html( notOwnWorkMessage.parse() ), + $( '

' ).html( notOwnWorkLocal.parse() ) + ); + $ownWorkMessage.add( $notOwnWorkMessage ).find( 'a' ).attr( 'target', '_blank' ); + this.selectFileWidget = new OO.ui.SelectFileWidget(); this.messageLabel = new OO.ui.LabelWidget( { label: $notOwnWorkMessage diff --git a/resources/src/mediawiki/mediawiki.ForeignStructuredUpload.js b/resources/src/mediawiki/mediawiki.ForeignStructuredUpload.js index 3a70a661f3..d27091653d 100644 --- a/resources/src/mediawiki/mediawiki.ForeignStructuredUpload.js +++ b/resources/src/mediawiki/mediawiki.ForeignStructuredUpload.js @@ -16,12 +16,12 @@ * * @inheritdoc */ - function ForeignStructuredUpload( targetHost, apiconfig ) { + function ForeignStructuredUpload( target, apiconfig ) { this.date = undefined; this.descriptions = []; this.categories = []; - mw.ForeignUpload.call( this, targetHost, apiconfig ); + mw.ForeignUpload.call( this, target, apiconfig ); } OO.inheritClass( ForeignStructuredUpload, mw.ForeignUpload ); diff --git a/resources/src/mediawiki/mediawiki.ForeignUpload.js b/resources/src/mediawiki/mediawiki.ForeignUpload.js index 38c910606c..7b3a751c8a 100644 --- a/resources/src/mediawiki/mediawiki.ForeignUpload.js +++ b/resources/src/mediawiki/mediawiki.ForeignUpload.js @@ -16,12 +16,13 @@ * @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 - * set to "true" for this name. + * make sure there is an entry in the $wgForeignUploadTargets array for this name. * @param {Object} [apiconfig] Passed to the constructor of mw.ForeignApi or mw.Api, as needed. */ function ForeignUpload( target, apiconfig ) { - var api, upload = this; + var api, + validTargets = mw.config.get( 'wgForeignUploadTargets' ), + upload = this; if ( typeof target === 'object' ) { // target probably wasn't passed in, it must @@ -30,7 +31,10 @@ target = undefined; } - this.target = target; + // * Use the given `target` first; + // * If not given, fall back to default (first) ForeignUploadTarget; + // * If none is configured, fall back to local uploads. + this.target = target || validTargets[ 0 ] || 'local'; // Now we have several different options. // If the local wiki is the target, then we can skip a bunch of steps @@ -40,6 +44,10 @@ // to confirm that the target is one that this site is configured to // support. if ( this.target === 'local' ) { + // If local uploads were requested, but they are disabled, fail. + if ( !mw.config.get( 'wgEnableUploads' ) ) { + throw new Error( 'Local uploads are disabled' ); + } // We'll ignore the CORS and centralauth stuff if the target is // the local wiki. this.apiPromise = $.Deferred().resolve( new mw.Api( apiconfig ) ); @@ -60,7 +68,7 @@ // Skip repos that are not our target, or if they // are the target, cannot be uploaded to. - if ( repo.name === upload.target && repo.canUpload ) { + if ( repo.name === upload.target && repo.canUpload === '' ) { return new mw.ForeignApi( repo.scriptDirUrl + '/api.php', apiconfig @@ -68,22 +76,7 @@ } } - // 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 ) ); + throw new Error( 'Can not upload to requested foreign repo' ); } ); } @@ -105,6 +98,9 @@ * * Most wikis use "shared" to refer to Wikimedia Commons, we assume that * in this class and in the messages linked to it. + * + * Defaults to the first available foreign upload target, + * or to local uploads if no foreign target is configured. */ /** -- 2.20.1