From 16c2df094f4c5a5959f5c5f363cb6ce4492b282f Mon Sep 17 00:00:00 2001 From: Ike Hecht Date: Wed, 17 Oct 2018 09:49:48 -0400 Subject: [PATCH] Add per-user group max upload configuration --- extension.json | 4 +++- res/ext.SimpleBatchUpload.js | 19 +++++++++++++++---- src/SimpleBatchUpload.php | 23 +++++++++++++---------- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/extension.json b/extension.json index b297409..3118e00 100644 --- a/extension.json +++ b/extension.json @@ -17,7 +17,9 @@ ] }, "config": { - "SimpleBatchUploadMaxFilesPerBatch": 0 + "SimpleBatchUploadMaxFilesPerBatch": { + "*": 1000 + } }, "callback": "SimpleBatchUpload\\SimpleBatchUpload::initCallback", "manifest_version": 1, diff --git a/res/ext.SimpleBatchUpload.js b/res/ext.SimpleBatchUpload.js index 91fd4fb..674f060 100644 --- a/res/ext.SimpleBatchUpload.js +++ b/res/ext.SimpleBatchUpload.js @@ -27,6 +27,19 @@ 'use strict'; $( function () { + + var filesLimitPerBatchConfig = mw.config.get( 'simpleBatchUploadMaxFilesPerBatch' ), + userGroups = mw.config.get( 'wgUserGroups' ), + userUploadLimit = 0; + + if ( filesLimitPerBatchConfig ) { + $.each( filesLimitPerBatchConfig, function ( role, limit ) { + if ( userGroups.indexOf( role ) !== -1 && ( limit > userUploadLimit ) ) { + userUploadLimit = limit; + } + } ); + } + $( 'span.fileupload-container' ).each( function () { var container = this; @@ -40,14 +53,12 @@ dropZone: $( '.fileupload-dropzone', container ), progressInterval: 100, - add: function ( e, data ) { var that = this; - var filesLimitPerBatch = mw.config.get( 'simpleBatchUploadMaxFilesPerBatch' ); - if ( filesLimitPerBatch && data.originalFiles.length > filesLimitPerBatch ) { - alert( mw.msg( 'simplebatchupload-max-files-alert', filesLimitPerBatch ) ); + if ( data.originalFiles.length > userUploadLimit ) { + window.alert( mw.msg( 'simplebatchupload-max-files-alert', userUploadLimit ) ); return false; } diff --git a/src/SimpleBatchUpload.php b/src/SimpleBatchUpload.php index cc94372..4f538ee 100644 --- a/src/SimpleBatchUpload.php +++ b/src/SimpleBatchUpload.php @@ -30,9 +30,9 @@ class SimpleBatchUpload { /** - * @var integer Max files could be uploaded per batch + * @var array Max files could be uploaded per batch */ - protected $maxFilesPerBatch; + protected $maxFilesPerBatchConfig; public static function initCallback() { @@ -134,11 +134,11 @@ protected function getBasePathsForNonComposerModules() { } /** - * @param array $vars + * @param array &$vars * @param \OutputPage $out */ public function onMakeGlobalVariablesScript( &$vars, $out ) { - $vars['simpleBatchUploadMaxFilesPerBatch'] = $this->getMaxFilesPerBatch(); + $vars['simpleBatchUploadMaxFilesPerBatch'] = $this->getMaxFilesPerBatchConfig(); } public function onSetupAfterCache() { @@ -149,18 +149,21 @@ public function onSetupAfterCache() { } /** - * @return integer + * @return array */ - public function getMaxFilesPerBatch() { + public function getMaxFilesPerBatchConfig() { global $wgSimpleBatchUploadMaxFilesPerBatch; - return $this->maxFilesPerBatch ? $this->maxFilesPerBatch : $wgSimpleBatchUploadMaxFilesPerBatch; + if ( $this->maxFilesPerBatchConfig === null ) { + $this->maxFilesPerBatchConfig = $wgSimpleBatchUploadMaxFilesPerBatch; + } + return $this->maxFilesPerBatchConfig; } /** - * @param integer $maxFilesPerBatch + * @param $maxFilesPerBatchConfig */ - public function setMaxFilesPerBatch( $maxFilesPerBatch ) { - $this->maxFilesPerBatch = $maxFilesPerBatch; + public function setMaxFilesPerBatchConfig( $maxFilesPerBatchConfig ) { + $this->maxFilesPerBatchConfig = $maxFilesPerBatchConfig; } }