Passed
Pull Request — master (#17)
by
unknown
01:30
created

getBasePathsForComposerModules()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * File containing the SimpleBatchUpload class
4
 *
5
 * @copyright (C) 2016 - 2017, Stephan Gambke
6
 * @license   GNU General Public License, version 2 (or any later version)
7
 *
8
 * This software is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 * This software is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18
 *
19
 * @file
20
 * @ingroup SimpleBatchUpload
21
 */
22
23
namespace SimpleBatchUpload;
24
25
/**
26
 * Class ExtensionManager
27
 *
28
 * @package SimpleBatchUpload
29
 */
30
class SimpleBatchUpload {
31
32
	/**
33
	 * @var integer Max files could be uploaded per batch
34
	 */
35
	protected $maxFilesPerBatch;
36
37
	const MAX_FILES_UNLIMITED = 0;
38
39
	public static function initCallback() {
40
41
			$configuration = ( new self() )->getConfiguration();
42
43
			foreach ( $configuration as $varname => $value ) {
44
				$GLOBALS[ $varname ] = array_replace_recursive( $GLOBALS[ $varname ], $value );
45
			}
46
47
	}
48
49
	/**
50
	 * @return array
51
	 */
52
	public function getConfiguration() {
53
54
		$configuration = [];
55
56
		$configuration[ 'wgExtensionMessagesFiles' ][ 'SimpleBatchUploadAlias' ] = __DIR__ . '/SimpleBatchUpload.alias.php';
57
		$configuration[ 'wgExtensionMessagesFiles' ][ 'SimpleBatchUploadMagic' ] = __DIR__ . '/SimpleBatchUpload.magic.php';
58
59
		$configuration[ 'wgSpecialPages' ][ 'BatchUpload' ] = '\SimpleBatchUpload\SpecialBatchUpload';
60
61
		$configuration[ 'wgHooks' ][ 'ParserFirstCallInit' ][ 'ext.simplebatchupload' ] = [ $this, 'registerParserFunction' ];
62
		$configuration[ 'wgHooks' ][ 'MakeGlobalVariablesScript' ][ 'ext.simplebatchupload' ] = [ $this, 'onMakeGlobalVariablesScript' ];
63
64
		$configuration[ 'wgResourceModules' ] = $this->getUploadSupportModuleDefinition() + $this->getUploadModuleDefinition();
65
66
		return $configuration;
67
68
	}
69
70
	/**
71
	 * @param \Parser $parser
0 ignored issues
show
Bug introduced by
The type Parser was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
72
	 *
73
	 * @return bool
74
	 * @throws \MWException
75
	 */
76
	public function registerParserFunction( &$parser ) {
77
		$parser->setFunctionHook( 'batchupload', [ new UploadButtonRenderer(), 'renderParserFunction' ], SFH_OBJECT_ARGS );
0 ignored issues
show
Bug introduced by
The constant SimpleBatchUpload\SFH_OBJECT_ARGS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
78
		return true;
79
	}
80
81
82
	/**
83
	 * @return array
84
	 */
85
	protected function getUploadSupportModuleDefinition() {
86
87
		return [ 'ext.SimpleBatchUpload.jquery-file-upload' =>
88
89
			$this->getBasePathsForComposerModules() +
90
91
			[
92
				'scripts' => [ '/vendor/blueimp/jquery-file-upload/js/jquery.fileupload.js' ],
93
				'styles' => [ '/vendor/blueimp/jquery-file-upload/css/jquery.fileupload.css' ],
94
				'position' => 'top',
95
				'dependencies' => [ 'jquery.ui.widget' ],
96
			],
97
		];
98
99
	}
100
101
	/**
102
	 * @return array
103
	 */
104
	protected function getUploadModuleDefinition() {
105
106
		return [ 'ext.SimpleBatchUpload' =>
107
108
			$this->getBasePathsForNonComposerModules() +
109
110
			[
111
				'scripts' => [ 'res/ext.SimpleBatchUpload.js' ],
112
				'styles' => [ 'res/ext.SimpleBatchUpload.css' ],
113
				'position' => 'top',
114
				'dependencies' => [ 'ext.SimpleBatchUpload.jquery-file-upload', 'mediawiki.Title', 'mediawiki.api.edit', 'mediawiki.jqueryMsg' ],
115
				'messages' => [ 'simplebatchupload-comment', 'simplebatchupload-max-files-alert' ],
116
			],
117
		];
118
	}
119
120
	/**
121
	 * @return string[]
122
	 */
123
	protected function getBasePathsForNonComposerModules() {
124
		return [
125
			'localBasePath' => dirname( __DIR__ ),
126
			'remoteBasePath' => $GLOBALS[ 'wgExtensionAssetsPath' ] . '/SimpleBatchUpload',
127
		];
128
	}
129
130
	/**
131
	 * @return string[]
132
	 */
133
	protected function getBasePathsForComposerModules() {
134
135
		if ( file_exists( dirname( __DIR__ ) . '/vendor' ) ) {
136
			return $this->getBasePathsForNonComposerModules();
137
		}
138
139
		return [
140
			'localBasePath' => $GLOBALS[ 'IP' ],
141
			'remoteBasePath' => $GLOBALS[ 'wgScriptPath' ],
142
		];
143
	}
144
145
	/**
146
	 * @param \User $user
0 ignored issues
show
Bug introduced by
The type User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
147
	 *
148
	 * @return int
149
	 */
150
	protected function detectMaxAllowedUploadsPerBatch( $user ) {
151
		global $wgSimpleBatchUploadMaxFilesPerBatch;
152
		$userGroups = $user->getEffectiveGroups();
153
		$maxLimit = self::MAX_FILES_UNLIMITED;
154
		foreach ( $wgSimpleBatchUploadMaxFilesPerBatch as $group => $limit ) {
155
			if ( in_array( $group, $userGroups ) && ( $limit > $maxLimit || $limit == self::MAX_FILES_UNLIMITED ) ) {
156
				$maxLimit = $limit;
157
			}
158
		}
159
		return $maxLimit;
160
	}
161
162
	/**
163
	 * @param array &$vars
164
	 * @param \OutputPage $out
0 ignored issues
show
Bug introduced by
The type OutputPage was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
165
	 */
166
	public function onMakeGlobalVariablesScript( &$vars, $out ) {
167
		$vars['simpleBatchUploadMaxFilesPerBatch'] = $this->getMaxFilesPerBatch( $out->getUser() );
168
	}
169
170
	/**
171
	 * @param \User $user
172
	 *
173
	 * @return int
174
	 */
175
	public function getMaxFilesPerBatch( $user ) {
176
		return $this->maxFilesPerBatch ? $this->maxFilesPerBatch : $this->detectMaxAllowedUploadsPerBatch( $user );
177
	}
178
179
	/**
180
	 * @param integer $maxFilesPerBatch
181
	 */
182
	public function setMaxFilesPerBatch( $maxFilesPerBatch ) {
183
		$this->maxFilesPerBatch = $maxFilesPerBatch;
184
	}
185
186
}
187