3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 use MediaWiki\Linker\LinkRenderer
;
22 use MediaWiki\MediaWikiServices
;
25 * Sub class of HTMLForm that provides the form section of SpecialUpload
27 class UploadForm
extends HTMLForm
{
29 protected $mForReUpload;
30 protected $mSessionKey;
31 protected $mHideIgnoreWarning;
32 protected $mDestWarningAck;
37 protected $mTextAfterSummary;
39 protected $mSourceIds;
41 protected $mMaxFileSize = [];
43 protected $mMaxUploadSize = [];
45 public function __construct( array $options = [], IContextSource
$context = null,
46 LinkRenderer
$linkRenderer = null
48 if ( $context instanceof IContextSource
) {
49 $this->setContext( $context );
52 if ( !$linkRenderer ) {
53 $linkRenderer = MediaWikiServices
::getInstance()->getLinkRenderer();
56 $this->mWatch
= !empty( $options['watch'] );
57 $this->mForReUpload
= !empty( $options['forreupload'] );
58 $this->mSessionKey
= $options['sessionkey'] ??
'';
59 $this->mHideIgnoreWarning
= !empty( $options['hideignorewarning'] );
60 $this->mDestWarningAck
= !empty( $options['destwarningack'] );
61 $this->mDestFile
= $options['destfile'] ??
'';
63 $this->mComment
= $options['description'] ??
'';
65 $this->mTextTop
= $options['texttop'] ??
'';
67 $this->mTextAfterSummary
= $options['textaftersummary'] ??
'';
69 $sourceDescriptor = $this->getSourceSection();
70 $descriptor = $sourceDescriptor
71 +
$this->getDescriptionSection()
72 +
$this->getOptionsSection();
74 Hooks
::run( 'UploadFormInitDescriptor', [ &$descriptor ] );
75 parent
::__construct( $descriptor, $context, 'upload' );
77 # Add a link to edit MediaWiki:Licenses
78 if ( $this->getUser()->isAllowed( 'editinterface' ) ) {
79 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
80 $licensesLink = $linkRenderer->makeKnownLink(
81 $this->msg( 'licenses' )->inContentLanguage()->getTitle(),
82 $this->msg( 'licenses-edit' )->text(),
84 [ 'action' => 'edit' ]
86 $editLicenses = '<p class="mw-upload-editlicenses">' . $licensesLink . '</p>';
87 $this->addFooterText( $editLicenses, 'description' );
90 # Set some form properties
91 $this->setSubmitText( $this->msg( 'uploadbtn' )->text() );
92 $this->setSubmitName( 'wpUpload' );
93 # Used message keys: 'accesskey-upload', 'tooltip-upload'
94 $this->setSubmitTooltip( 'upload' );
95 $this->setId( 'mw-upload-form' );
97 # Build a list of IDs for javascript insertion
98 $this->mSourceIds
= [];
99 foreach ( $sourceDescriptor as $field ) {
100 if ( !empty( $field['id'] ) ) {
101 $this->mSourceIds
[] = $field['id'];
107 * Get the descriptor of the fieldset that contains the file source
108 * selection. The section is 'source'
110 * @return array Descriptor array
112 protected function getSourceSection() {
113 if ( $this->mSessionKey
) {
117 'default' => $this->mSessionKey
,
121 'default' => 'Stash',
126 $canUploadByUrl = UploadFromUrl
::isEnabled()
127 && ( UploadFromUrl
::isAllowed( $this->getUser() ) === true )
128 && $this->getConfig()->get( 'CopyUploadsFromSpecialUpload' );
129 $radio = $canUploadByUrl;
130 $selectedSourceType = strtolower( $this->getRequest()->getText( 'wpSourceType', 'File' ) );
133 if ( $this->mTextTop
) {
134 $descriptor['UploadFormTextTop'] = [
136 'section' => 'source',
137 'default' => $this->mTextTop
,
142 $this->mMaxUploadSize
['file'] = min(
143 UploadBase
::getMaxUploadSize( 'file' ),
144 UploadBase
::getMaxPhpUploadSize()
147 $help = $this->msg( 'upload-maxfilesize',
148 $this->getContext()->getLanguage()->formatSize( $this->mMaxUploadSize
['file'] )
151 // If the user can also upload by URL, there are 2 different file size limits.
152 // This extra message helps stress which limit corresponds to what.
153 if ( $canUploadByUrl ) {
154 $help .= $this->msg( 'word-separator' )->escaped();
155 $help .= $this->msg( 'upload_source_file' )->parse();
158 $descriptor['UploadFile'] = [
159 'class' => UploadSourceField
::class,
160 'section' => 'source',
162 'id' => 'wpUploadFile',
163 'radio-id' => 'wpSourceTypeFile',
164 'label-message' => 'sourcefilename',
165 'upload-type' => 'File',
168 'checked' => $selectedSourceType == 'file',
171 if ( $canUploadByUrl ) {
172 $this->mMaxUploadSize
['url'] = UploadBase
::getMaxUploadSize( 'url' );
173 $descriptor['UploadFileURL'] = [
174 'class' => UploadSourceField
::class,
175 'section' => 'source',
176 'id' => 'wpUploadFileURL',
177 'radio-id' => 'wpSourceTypeurl',
178 'label-message' => 'sourceurl',
179 'upload-type' => 'url',
181 'help' => $this->msg( 'upload-maxfilesize',
182 $this->getContext()->getLanguage()->formatSize( $this->mMaxUploadSize
['url'] )
184 $this->msg( 'word-separator' )->escaped() .
185 $this->msg( 'upload_source_url' )->parse(),
186 'checked' => $selectedSourceType == 'url',
189 Hooks
::run( 'UploadFormSourceDescriptors', [ &$descriptor, &$radio, $selectedSourceType ] );
191 $descriptor['Extensions'] = [
193 'section' => 'source',
194 'default' => $this->getExtensionsMessage(),
202 * Get the messages indicating which extensions are preferred and prohibitted.
204 * @return string HTML string containing the message
206 protected function getExtensionsMessage() {
207 # Print a list of allowed file extensions, if so configured. We ignore
208 # MIME type here, it's incomprehensible to most people and too long.
209 $config = $this->getConfig();
211 if ( $config->get( 'CheckFileExtensions' ) ) {
212 $fileExtensions = array_unique( $config->get( 'FileExtensions' ) );
213 if ( $config->get( 'StrictFileExtensions' ) ) {
214 # Everything not permitted is banned
216 '<div id="mw-upload-permitted">' .
217 $this->msg( 'upload-permitted' )
218 ->params( $this->getLanguage()->commaList( $fileExtensions ) )
219 ->numParams( count( $fileExtensions ) )
223 # We have to list both preferred and prohibited
224 $fileBlacklist = array_unique( $config->get( 'FileBlacklist' ) );
226 '<div id="mw-upload-preferred">' .
227 $this->msg( 'upload-preferred' )
228 ->params( $this->getLanguage()->commaList( $fileExtensions ) )
229 ->numParams( count( $fileExtensions ) )
232 '<div id="mw-upload-prohibited">' .
233 $this->msg( 'upload-prohibited' )
234 ->params( $this->getLanguage()->commaList( $fileBlacklist ) )
235 ->numParams( count( $fileBlacklist ) )
240 # Everything is permitted.
241 $extensionsList = '';
244 return $extensionsList;
248 * Get the descriptor of the fieldset that contains the file description
249 * input. The section is 'description'
251 * @return array Descriptor array
253 protected function getDescriptionSection() {
254 $config = $this->getConfig();
255 if ( $this->mSessionKey
) {
256 $stash = RepoGroup
::singleton()->getLocalRepo()->getUploadStash( $this->getUser() );
258 $file = $stash->getFile( $this->mSessionKey
);
259 } catch ( Exception
$e ) {
265 $mto = $file->transform( [ 'width' => 120 ] );
267 $this->addHeaderText(
268 '<div class="thumb t' . $wgContLang->alignEnd() . '">' .
269 Html
::element( 'img', [
270 'src' => $mto->getUrl(),
271 'class' => 'thumbimage',
272 ] ) . '</div>', 'description' );
280 'section' => 'description',
281 'id' => 'wpDestFile',
282 'label-message' => 'destfilename',
284 'default' => $this->mDestFile
,
285 # @todo FIXME: Hack to work around poor handling of the 'default' option in HTMLForm
286 'nodata' => strval( $this->mDestFile
) !== '',
288 'UploadDescription' => [
289 'type' => 'textarea',
290 'section' => 'description',
291 'id' => 'wpUploadDescription',
292 'label-message' => $this->mForReUpload
293 ?
'filereuploadsummary'
294 : 'fileuploadsummary',
295 'default' => $this->mComment
,
300 if ( $this->mTextAfterSummary
) {
301 $descriptor['UploadFormTextAfterSummary'] = [
303 'section' => 'description',
304 'default' => $this->mTextAfterSummary
,
311 'type' => 'edittools',
312 'section' => 'description',
313 'message' => 'edittools-upload',
317 if ( $this->mForReUpload
) {
318 $descriptor['DestFile']['readonly'] = true;
320 $descriptor['License'] = [
322 'class' => Licenses
::class,
323 'section' => 'description',
325 'label-message' => 'license',
329 if ( $config->get( 'UseCopyrightUpload' ) ) {
330 $descriptor['UploadCopyStatus'] = [
332 'section' => 'description',
333 'id' => 'wpUploadCopyStatus',
334 'label-message' => 'filestatus',
336 $descriptor['UploadSource'] = [
338 'section' => 'description',
339 'id' => 'wpUploadSource',
340 'label-message' => 'filesource',
348 * Get the descriptor of the fieldset that contains the upload options,
349 * such as "watch this file". The section is 'options'
351 * @return array Descriptor array
353 protected function getOptionsSection() {
354 $user = $this->getUser();
355 if ( $user->isLoggedIn() ) {
359 'id' => 'wpWatchthis',
360 'label-message' => 'watchthisupload',
361 'section' => 'options',
362 'default' => $this->mWatch
,
366 if ( !$this->mHideIgnoreWarning
) {
367 $descriptor['IgnoreWarning'] = [
369 'id' => 'wpIgnoreWarning',
370 'label-message' => 'ignorewarnings',
371 'section' => 'options',
375 $descriptor['DestFileWarningAck'] = [
377 'id' => 'wpDestFileWarningAck',
378 'default' => $this->mDestWarningAck ?
'1' : '',
381 if ( $this->mForReUpload
) {
382 $descriptor['ForReUpload'] = [
384 'id' => 'wpForReUpload',
393 * Add the upload JS and show the form.
395 public function show() {
396 $this->addUploadJS();
401 * Add upload JS to the OutputPage
403 protected function addUploadJS() {
404 $config = $this->getConfig();
406 $this->mMaxUploadSize
['*'] = UploadBase
::getMaxUploadSize();
409 'wgAjaxUploadDestCheck' => $config->get( 'AjaxUploadDestCheck' ),
410 'wgAjaxLicensePreview' => $config->get( 'AjaxLicensePreview' ),
411 'wgUploadAutoFill' => !$this->mForReUpload
&&
412 // If we received mDestFile from the request, don't autofill
413 // the wpDestFile textbox
414 $this->mDestFile
=== '',
415 'wgUploadSourceIds' => $this->mSourceIds
,
416 'wgCheckFileExtensions' => $config->get( 'CheckFileExtensions' ),
417 'wgStrictFileExtensions' => $config->get( 'StrictFileExtensions' ),
418 'wgFileExtensions' => array_values( array_unique( $config->get( 'FileExtensions' ) ) ),
419 'wgCapitalizeUploads' => MWNamespace
::isCapitalized( NS_FILE
),
420 'wgMaxUploadSize' => $this->mMaxUploadSize
,
421 'wgFileCanRotate' => SpecialUpload
::rotationEnabled(),
424 $out = $this->getOutput();
425 $out->addJsConfigVars( $scriptVars );
428 'mediawiki.special.upload', // Extras for thumbnail and license preview.
433 * Empty function; submission is handled elsewhere.
437 function trySubmit() {