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
= isset( $options['sessionkey'] ) ?
$options['sessionkey'] : '';
59 $this->mHideIgnoreWarning
= !empty( $options['hideignorewarning'] );
60 $this->mDestWarningAck
= !empty( $options['destwarningack'] );
61 $this->mDestFile
= isset( $options['destfile'] ) ?
$options['destfile'] : '';
63 $this->mComment
= isset( $options['description'] ) ?
64 $options['description'] : '';
66 $this->mTextTop
= isset( $options['texttop'] )
67 ?
$options['texttop'] : '';
69 $this->mTextAfterSummary
= isset( $options['textaftersummary'] )
70 ?
$options['textaftersummary'] : '';
72 $sourceDescriptor = $this->getSourceSection();
73 $descriptor = $sourceDescriptor
74 +
$this->getDescriptionSection()
75 +
$this->getOptionsSection();
77 Hooks
::run( 'UploadFormInitDescriptor', [ &$descriptor ] );
78 parent
::__construct( $descriptor, $context, 'upload' );
80 # Add a link to edit MediaWiki:Licenses
81 if ( $this->getUser()->isAllowed( 'editinterface' ) ) {
82 $this->getOutput()->addModuleStyles( 'mediawiki.special.upload.styles' );
83 $licensesLink = $linkRenderer->makeKnownLink(
84 $this->msg( 'licenses' )->inContentLanguage()->getTitle(),
85 $this->msg( 'licenses-edit' )->text(),
87 [ 'action' => 'edit' ]
89 $editLicenses = '<p class="mw-upload-editlicenses">' . $licensesLink . '</p>';
90 $this->addFooterText( $editLicenses, 'description' );
93 # Set some form properties
94 $this->setSubmitText( $this->msg( 'uploadbtn' )->text() );
95 $this->setSubmitName( 'wpUpload' );
96 # Used message keys: 'accesskey-upload', 'tooltip-upload'
97 $this->setSubmitTooltip( 'upload' );
98 $this->setId( 'mw-upload-form' );
100 # Build a list of IDs for javascript insertion
101 $this->mSourceIds
= [];
102 foreach ( $sourceDescriptor as $field ) {
103 if ( !empty( $field['id'] ) ) {
104 $this->mSourceIds
[] = $field['id'];
110 * Get the descriptor of the fieldset that contains the file source
111 * selection. The section is 'source'
113 * @return array Descriptor array
115 protected function getSourceSection() {
116 if ( $this->mSessionKey
) {
120 'default' => $this->mSessionKey
,
124 'default' => 'Stash',
129 $canUploadByUrl = UploadFromUrl
::isEnabled()
130 && ( UploadFromUrl
::isAllowed( $this->getUser() ) === true )
131 && $this->getConfig()->get( 'CopyUploadsFromSpecialUpload' );
132 $radio = $canUploadByUrl;
133 $selectedSourceType = strtolower( $this->getRequest()->getText( 'wpSourceType', 'File' ) );
136 if ( $this->mTextTop
) {
137 $descriptor['UploadFormTextTop'] = [
139 'section' => 'source',
140 'default' => $this->mTextTop
,
145 $this->mMaxUploadSize
['file'] = min(
146 UploadBase
::getMaxUploadSize( 'file' ),
147 UploadBase
::getMaxPhpUploadSize()
150 $help = $this->msg( 'upload-maxfilesize',
151 $this->getContext()->getLanguage()->formatSize( $this->mMaxUploadSize
['file'] )
154 // If the user can also upload by URL, there are 2 different file size limits.
155 // This extra message helps stress which limit corresponds to what.
156 if ( $canUploadByUrl ) {
157 $help .= $this->msg( 'word-separator' )->escaped();
158 $help .= $this->msg( 'upload_source_file' )->parse();
161 $descriptor['UploadFile'] = [
162 'class' => UploadSourceField
::class,
163 'section' => 'source',
165 'id' => 'wpUploadFile',
166 'radio-id' => 'wpSourceTypeFile',
167 'label-message' => 'sourcefilename',
168 'upload-type' => 'File',
171 'checked' => $selectedSourceType == 'file',
174 if ( $canUploadByUrl ) {
175 $this->mMaxUploadSize
['url'] = UploadBase
::getMaxUploadSize( 'url' );
176 $descriptor['UploadFileURL'] = [
177 'class' => UploadSourceField
::class,
178 'section' => 'source',
179 'id' => 'wpUploadFileURL',
180 'radio-id' => 'wpSourceTypeurl',
181 'label-message' => 'sourceurl',
182 'upload-type' => 'url',
184 'help' => $this->msg( 'upload-maxfilesize',
185 $this->getContext()->getLanguage()->formatSize( $this->mMaxUploadSize
['url'] )
187 $this->msg( 'word-separator' )->escaped() .
188 $this->msg( 'upload_source_url' )->parse(),
189 'checked' => $selectedSourceType == 'url',
192 Hooks
::run( 'UploadFormSourceDescriptors', [ &$descriptor, &$radio, $selectedSourceType ] );
194 $descriptor['Extensions'] = [
196 'section' => 'source',
197 'default' => $this->getExtensionsMessage(),
205 * Get the messages indicating which extensions are preferred and prohibitted.
207 * @return string HTML string containing the message
209 protected function getExtensionsMessage() {
210 # Print a list of allowed file extensions, if so configured. We ignore
211 # MIME type here, it's incomprehensible to most people and too long.
212 $config = $this->getConfig();
214 if ( $config->get( 'CheckFileExtensions' ) ) {
215 $fileExtensions = array_unique( $config->get( 'FileExtensions' ) );
216 if ( $config->get( 'StrictFileExtensions' ) ) {
217 # Everything not permitted is banned
219 '<div id="mw-upload-permitted">' .
220 $this->msg( 'upload-permitted' )
221 ->params( $this->getLanguage()->commaList( $fileExtensions ) )
222 ->numParams( count( $fileExtensions ) )
226 # We have to list both preferred and prohibited
227 $fileBlacklist = array_unique( $config->get( 'FileBlacklist' ) );
229 '<div id="mw-upload-preferred">' .
230 $this->msg( 'upload-preferred' )
231 ->params( $this->getLanguage()->commaList( $fileExtensions ) )
232 ->numParams( count( $fileExtensions ) )
235 '<div id="mw-upload-prohibited">' .
236 $this->msg( 'upload-prohibited' )
237 ->params( $this->getLanguage()->commaList( $fileBlacklist ) )
238 ->numParams( count( $fileBlacklist ) )
243 # Everything is permitted.
244 $extensionsList = '';
247 return $extensionsList;
251 * Get the descriptor of the fieldset that contains the file description
252 * input. The section is 'description'
254 * @return array Descriptor array
256 protected function getDescriptionSection() {
257 $config = $this->getConfig();
258 if ( $this->mSessionKey
) {
259 $stash = RepoGroup
::singleton()->getLocalRepo()->getUploadStash( $this->getUser() );
261 $file = $stash->getFile( $this->mSessionKey
);
262 } catch ( Exception
$e ) {
268 $mto = $file->transform( [ 'width' => 120 ] );
270 $this->addHeaderText(
271 '<div class="thumb t' . $wgContLang->alignEnd() . '">' .
272 Html
::element( 'img', [
273 'src' => $mto->getUrl(),
274 'class' => 'thumbimage',
275 ] ) . '</div>', 'description' );
283 'section' => 'description',
284 'id' => 'wpDestFile',
285 'label-message' => 'destfilename',
287 'default' => $this->mDestFile
,
288 # @todo FIXME: Hack to work around poor handling of the 'default' option in HTMLForm
289 'nodata' => strval( $this->mDestFile
) !== '',
291 'UploadDescription' => [
292 'type' => 'textarea',
293 'section' => 'description',
294 'id' => 'wpUploadDescription',
295 'label-message' => $this->mForReUpload
296 ?
'filereuploadsummary'
297 : 'fileuploadsummary',
298 'default' => $this->mComment
,
303 if ( $this->mTextAfterSummary
) {
304 $descriptor['UploadFormTextAfterSummary'] = [
306 'section' => 'description',
307 'default' => $this->mTextAfterSummary
,
314 'type' => 'edittools',
315 'section' => 'description',
316 'message' => 'edittools-upload',
320 if ( $this->mForReUpload
) {
321 $descriptor['DestFile']['readonly'] = true;
323 $descriptor['License'] = [
325 'class' => Licenses
::class,
326 'section' => 'description',
328 'label-message' => 'license',
332 if ( $config->get( 'UseCopyrightUpload' ) ) {
333 $descriptor['UploadCopyStatus'] = [
335 'section' => 'description',
336 'id' => 'wpUploadCopyStatus',
337 'label-message' => 'filestatus',
339 $descriptor['UploadSource'] = [
341 'section' => 'description',
342 'id' => 'wpUploadSource',
343 'label-message' => 'filesource',
351 * Get the descriptor of the fieldset that contains the upload options,
352 * such as "watch this file". The section is 'options'
354 * @return array Descriptor array
356 protected function getOptionsSection() {
357 $user = $this->getUser();
358 if ( $user->isLoggedIn() ) {
362 'id' => 'wpWatchthis',
363 'label-message' => 'watchthisupload',
364 'section' => 'options',
365 'default' => $this->mWatch
,
369 if ( !$this->mHideIgnoreWarning
) {
370 $descriptor['IgnoreWarning'] = [
372 'id' => 'wpIgnoreWarning',
373 'label-message' => 'ignorewarnings',
374 'section' => 'options',
378 $descriptor['DestFileWarningAck'] = [
380 'id' => 'wpDestFileWarningAck',
381 'default' => $this->mDestWarningAck ?
'1' : '',
384 if ( $this->mForReUpload
) {
385 $descriptor['ForReUpload'] = [
387 'id' => 'wpForReUpload',
396 * Add the upload JS and show the form.
398 public function show() {
399 $this->addUploadJS();
404 * Add upload JS to the OutputPage
406 protected function addUploadJS() {
407 $config = $this->getConfig();
409 $useAjaxDestCheck = $config->get( 'UseAjax' ) && $config->get( 'AjaxUploadDestCheck' );
410 $useAjaxLicensePreview = $config->get( 'UseAjax' ) &&
411 $config->get( 'AjaxLicensePreview' ) && $config->get( 'EnableAPI' );
412 $this->mMaxUploadSize
['*'] = UploadBase
::getMaxUploadSize();
415 'wgAjaxUploadDestCheck' => $useAjaxDestCheck,
416 'wgAjaxLicensePreview' => $useAjaxLicensePreview,
417 'wgUploadAutoFill' => !$this->mForReUpload
&&
418 // If we received mDestFile from the request, don't autofill
419 // the wpDestFile textbox
420 $this->mDestFile
=== '',
421 'wgUploadSourceIds' => $this->mSourceIds
,
422 'wgCheckFileExtensions' => $config->get( 'CheckFileExtensions' ),
423 'wgStrictFileExtensions' => $config->get( 'StrictFileExtensions' ),
424 'wgFileExtensions' => array_values( array_unique( $config->get( 'FileExtensions' ) ) ),
425 'wgCapitalizeUploads' => MWNamespace
::isCapitalized( NS_FILE
),
426 'wgMaxUploadSize' => $this->mMaxUploadSize
,
427 'wgFileCanRotate' => SpecialUpload
::rotationEnabled(),
430 $out = $this->getOutput();
431 $out->addJsConfigVars( $scriptVars );
434 'mediawiki.special.upload', // Extras for thumbnail and license preview.
439 * Empty function; submission is handled elsewhere.
443 function trySubmit() {