MediaWiki REL1_35
UploadForm.php
Go to the documentation of this file.
1<?php
23
27class UploadForm extends HTMLForm {
28 protected $mWatch;
29 protected $mForReUpload;
30 protected $mSessionKey;
33 protected $mDestFile;
34
35 protected $mComment;
36 protected $mTextTop;
38
39 protected $mSourceIds;
40
41 protected $mMaxFileSize = [];
42
44 protected $mMaxUploadSize = [];
45
46 public function __construct( array $options = [], IContextSource $context = null,
47 LinkRenderer $linkRenderer = null
48 ) {
49 if ( $context instanceof IContextSource ) {
50 $this->setContext( $context );
51 }
52
53 if ( !$linkRenderer ) {
54 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
55 }
56
57 $this->mWatch = !empty( $options['watch'] );
58 $this->mForReUpload = !empty( $options['forreupload'] );
59 $this->mSessionKey = $options['sessionkey'] ?? '';
60 $this->mHideIgnoreWarning = !empty( $options['hideignorewarning'] );
61 $this->mDestWarningAck = !empty( $options['destwarningack'] );
62 $this->mDestFile = $options['destfile'] ?? '';
63
64 $this->mComment = $options['description'] ?? '';
65
66 $this->mTextTop = $options['texttop'] ?? '';
67
68 $this->mTextAfterSummary = $options['textaftersummary'] ?? '';
69
70 $sourceDescriptor = $this->getSourceSection();
71 $descriptor = $sourceDescriptor
72 + $this->getDescriptionSection()
73 + $this->getOptionsSection();
74
75 $this->getHookRunner()->onUploadFormInitDescriptor( $descriptor );
76 parent::__construct( $descriptor, $context, 'upload' );
77
78 # Add a link to edit MediaWiki:Licenses
79 if ( MediaWikiServices::getInstance()
81 ->userHasRight( $this->getUser(), 'editinterface' )
82 ) {
83 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
84 $licensesLink = $linkRenderer->makeKnownLink(
85 $this->msg( 'licenses' )->inContentLanguage()->getTitle(),
86 $this->msg( 'licenses-edit' )->text(),
87 [],
88 [ 'action' => 'edit' ]
89 );
90 $editLicenses = '<p class="mw-upload-editlicenses">' . $licensesLink . '</p>';
91 $this->addFooterText( $editLicenses, 'description' );
92 }
93
94 # Set some form properties
95 $this->setSubmitText( $this->msg( 'uploadbtn' )->text() );
96 $this->setSubmitName( 'wpUpload' );
97 # Used message keys: 'accesskey-upload', 'tooltip-upload'
98 $this->setSubmitTooltip( 'upload' );
99 $this->setId( 'mw-upload-form' );
100
101 # Build a list of IDs for javascript insertion
102 $this->mSourceIds = [];
103 foreach ( $sourceDescriptor as $field ) {
104 if ( !empty( $field['id'] ) ) {
105 $this->mSourceIds[] = $field['id'];
106 }
107 }
108 }
109
116 protected function getSourceSection() {
117 if ( $this->mSessionKey ) {
118 return [
119 'SessionKey' => [
120 'type' => 'hidden',
121 'default' => $this->mSessionKey,
122 ],
123 'SourceType' => [
124 'type' => 'hidden',
125 'default' => 'Stash',
126 ],
127 ];
128 }
129
130 $canUploadByUrl = UploadFromUrl::isEnabled()
131 && ( UploadFromUrl::isAllowed( $this->getUser() ) === true )
132 && $this->getConfig()->get( 'CopyUploadsFromSpecialUpload' );
133 $radio = $canUploadByUrl;
134 $selectedSourceType = strtolower( $this->getRequest()->getText( 'wpSourceType', 'File' ) );
135
136 $descriptor = [];
137 if ( $this->mTextTop ) {
138 $descriptor['UploadFormTextTop'] = [
139 'type' => 'info',
140 'section' => 'source',
141 'default' => $this->mTextTop,
142 'raw' => true,
143 ];
144 }
145
146 $this->mMaxUploadSize['file'] = min(
147 UploadBase::getMaxUploadSize( 'file' ),
148 UploadBase::getMaxPhpUploadSize()
149 );
150
151 $help = $this->msg( 'upload-maxfilesize',
152 $this->getContext()->getLanguage()->formatSize( $this->mMaxUploadSize['file'] )
153 )->parse();
154
155 // If the user can also upload by URL, there are 2 different file size limits.
156 // This extra message helps stress which limit corresponds to what.
157 if ( $canUploadByUrl ) {
158 $help .= $this->msg( 'word-separator' )->escaped();
159 $help .= $this->msg( 'upload_source_file' )->parse();
160 }
161
162 $descriptor['UploadFile'] = [
163 'class' => UploadSourceField::class,
164 'section' => 'source',
165 'type' => 'file',
166 'id' => 'wpUploadFile',
167 'radio-id' => 'wpSourceTypeFile',
168 'label-message' => 'sourcefilename',
169 'upload-type' => 'File',
170 'radio' => &$radio,
171 'help' => $help,
172 'checked' => $selectedSourceType == 'file',
173 ];
174
175 if ( $canUploadByUrl ) {
176 $this->mMaxUploadSize['url'] = UploadBase::getMaxUploadSize( 'url' );
177 $descriptor['UploadFileURL'] = [
178 'class' => UploadSourceField::class,
179 'section' => 'source',
180 'id' => 'wpUploadFileURL',
181 'radio-id' => 'wpSourceTypeurl',
182 'label-message' => 'sourceurl',
183 'upload-type' => 'url',
184 'radio' => &$radio,
185 'help' => $this->msg( 'upload-maxfilesize',
186 $this->getContext()->getLanguage()->formatSize( $this->mMaxUploadSize['url'] )
187 )->parse() .
188 $this->msg( 'word-separator' )->escaped() .
189 $this->msg( 'upload_source_url' )->parse(),
190 'checked' => $selectedSourceType == 'url',
191 ];
192 }
193 $this->getHookRunner()->onUploadFormSourceDescriptors(
194 $descriptor, $radio, $selectedSourceType );
195
196 $descriptor['Extensions'] = [
197 'type' => 'info',
198 'section' => 'source',
199 'default' => $this->getExtensionsMessage(),
200 'raw' => true,
201 ];
202
203 return $descriptor;
204 }
205
211 protected function getExtensionsMessage() {
212 # Print a list of allowed file extensions, if so configured. We ignore
213 # MIME type here, it's incomprehensible to most people and too long.
214 $config = $this->getConfig();
215
216 if ( $config->get( 'CheckFileExtensions' ) ) {
217 $fileExtensions = array_unique( $config->get( 'FileExtensions' ) );
218 if ( $config->get( 'StrictFileExtensions' ) ) {
219 # Everything not permitted is banned
220 $extensionsList =
221 '<div id="mw-upload-permitted">' .
222 $this->msg( 'upload-permitted' )
223 ->params( $this->getLanguage()->commaList( $fileExtensions ) )
224 ->numParams( count( $fileExtensions ) )
225 ->parseAsBlock() .
226 "</div>\n";
227 } else {
228 # We have to list both preferred and prohibited
229 $fileBlacklist = array_unique( $config->get( 'FileBlacklist' ) );
230 $extensionsList =
231 '<div id="mw-upload-preferred">' .
232 $this->msg( 'upload-preferred' )
233 ->params( $this->getLanguage()->commaList( $fileExtensions ) )
234 ->numParams( count( $fileExtensions ) )
235 ->parseAsBlock() .
236 "</div>\n" .
237 '<div id="mw-upload-prohibited">' .
238 $this->msg( 'upload-prohibited' )
239 ->params( $this->getLanguage()->commaList( $fileBlacklist ) )
240 ->numParams( count( $fileBlacklist ) )
241 ->parseAsBlock() .
242 "</div>\n";
243 }
244 } else {
245 # Everything is permitted.
246 $extensionsList = '';
247 }
248
249 return $extensionsList;
250 }
251
258 protected function getDescriptionSection() {
259 $config = $this->getConfig();
260 if ( $this->mSessionKey ) {
261 $stash = MediaWikiServices::getInstance()->getRepoGroup()
262 ->getLocalRepo()->getUploadStash( $this->getUser() );
263 try {
264 $file = $stash->getFile( $this->mSessionKey );
265 } catch ( Exception $e ) {
266 $file = null;
267 }
268 if ( $file ) {
269 $mto = $file->transform( [ 'width' => 120 ] );
270 if ( $mto ) {
271 $this->addHeaderText(
272 '<div class="thumb t' .
273 MediaWikiServices::getInstance()->getContentLanguage()->alignEnd() . '">' .
274 Html::element( 'img', [
275 'src' => $mto->getUrl(),
276 'class' => 'thumbimage',
277 ] ) . '</div>', 'description' );
278 }
279 }
280 }
281
282 $descriptor = [
283 'DestFile' => [
284 'type' => 'text',
285 'section' => 'description',
286 'id' => 'wpDestFile',
287 'label-message' => 'destfilename',
288 'size' => 60,
289 'default' => $this->mDestFile,
290 # @todo FIXME: Hack to work around poor handling of the 'default' option in HTMLForm
291 'nodata' => strval( $this->mDestFile ) !== '',
292 ],
293 'UploadDescription' => [
294 'type' => 'textarea',
295 'section' => 'description',
296 'id' => 'wpUploadDescription',
297 'label-message' => $this->mForReUpload
298 ? 'filereuploadsummary'
299 : 'fileuploadsummary',
300 'default' => $this->mComment,
301 'cols' => 80,
302 'rows' => 8,
303 ]
304 ];
305 if ( $this->mTextAfterSummary ) {
306 $descriptor['UploadFormTextAfterSummary'] = [
307 'type' => 'info',
308 'section' => 'description',
309 'default' => $this->mTextAfterSummary,
310 'raw' => true,
311 ];
312 }
313
314 $descriptor += [
315 'EditTools' => [
316 'type' => 'edittools',
317 'section' => 'description',
318 'message' => 'edittools-upload',
319 ]
320 ];
321
322 if ( $this->mForReUpload ) {
323 $descriptor['DestFile']['readonly'] = true;
324 } else {
325 $descriptor['License'] = [
326 'type' => 'select',
327 'class' => Licenses::class,
328 'section' => 'description',
329 'id' => 'wpLicense',
330 'label-message' => 'license',
331 ];
332 }
333
334 if ( $config->get( 'UseCopyrightUpload' ) ) {
335 $descriptor['UploadCopyStatus'] = [
336 'type' => 'text',
337 'section' => 'description',
338 'id' => 'wpUploadCopyStatus',
339 'label-message' => 'filestatus',
340 ];
341 $descriptor['UploadSource'] = [
342 'type' => 'text',
343 'section' => 'description',
344 'id' => 'wpUploadSource',
345 'label-message' => 'filesource',
346 ];
347 }
348
349 return $descriptor;
350 }
351
358 protected function getOptionsSection() {
359 $user = $this->getUser();
360 if ( $user->isLoggedIn() ) {
361 $descriptor = [
362 'Watchthis' => [
363 'type' => 'check',
364 'id' => 'wpWatchthis',
365 'label-message' => 'watchthisupload',
366 'section' => 'options',
367 'default' => $this->mWatch,
368 ]
369 ];
370 }
371 if ( !$this->mHideIgnoreWarning ) {
372 $descriptor['IgnoreWarning'] = [
373 'type' => 'check',
374 'id' => 'wpIgnoreWarning',
375 'label-message' => 'ignorewarnings',
376 'section' => 'options',
377 ];
378 }
379
380 $descriptor['DestFileWarningAck'] = [
381 'type' => 'hidden',
382 'id' => 'wpDestFileWarningAck',
383 'default' => $this->mDestWarningAck ? '1' : '',
384 ];
385
386 if ( $this->mForReUpload ) {
387 $descriptor['ForReUpload'] = [
388 'type' => 'hidden',
389 'id' => 'wpForReUpload',
390 'default' => '1',
391 ];
392 }
393
394 return $descriptor;
395 }
396
401 public function show() {
402 $this->addUploadJS();
403 return parent::show();
404 }
405
409 protected function addUploadJS() {
410 $config = $this->getConfig();
411
412 $this->mMaxUploadSize['*'] = UploadBase::getMaxUploadSize();
413
414 $scriptVars = [
415 'wgAjaxUploadDestCheck' => $config->get( 'AjaxUploadDestCheck' ),
416 'wgAjaxLicensePreview' => $config->get( 'AjaxLicensePreview' ),
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' => MediaWikiServices::getInstance()->getNamespaceInfo()->
426 isCapitalized( NS_FILE ),
427 'wgMaxUploadSize' => $this->mMaxUploadSize,
428 'wgFileCanRotate' => SpecialUpload::rotationEnabled(),
429 ];
430
431 $out = $this->getOutput();
432 $out->addJsConfigVars( $scriptVars );
433
434 $out->addModules( [
435 'mediawiki.special.upload', // Extras for thumbnail and license preview.
436 ] );
437 }
438
444 public function trySubmit() {
445 return false;
446 }
447}
getPermissionManager()
getUser()
Stable to override.
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
IContextSource $context
getContext()
Get the base IContextSource object.
setContext(IContextSource $context)
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:135
setSubmitName( $name)
setId( $id)
addFooterText( $msg, $section=null)
Add footer text, inside the form.
Definition HTMLForm.php:875
addHeaderText( $msg, $section=null)
Add HTML to the header, inside the form.
Definition HTMLForm.php:819
getTitle()
Get the title.
setSubmitTooltip( $name)
setSubmitText( $t)
Set the text for the submit button.
Class that generates HTML links for pages.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Sub class of HTMLForm that provides the form section of SpecialUpload.
show()
Add the upload JS and show the form.
trySubmit()
Empty function; submission is handled elsewhere.
__construct(array $options=[], IContextSource $context=null, LinkRenderer $linkRenderer=null)
getDescriptionSection()
Get the descriptor of the fieldset that contains the file description input.
getOptionsSection()
Get the descriptor of the fieldset that contains the upload options, such as "watch this file".
addUploadJS()
Add upload JS to the OutputPage.
getSourceSection()
Get the descriptor of the fieldset that contains the file source selection.
getExtensionsMessage()
Get the messages indicating which extensions are preferred and prohibitted.
array $mMaxUploadSize
static isAllowed(UserIdentity $user)
Checks if the user is allowed to use the upload-by-URL feature.
static isEnabled()
Checks if the upload from URL feature is enabled.
const NS_FILE
Definition Defines.php:76
Interface for objects which can provide a MediaWiki context on request.
$help
Definition mcc.php:32
return true
Definition router.php:92
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42