MediaWiki REL1_31
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
43 protected $mMaxUploadSize = [];
44
45 public function __construct( array $options = [], IContextSource $context = null,
47 ) {
48 if ( $context instanceof IContextSource ) {
49 $this->setContext( $context );
50 }
51
52 if ( !$linkRenderer ) {
53 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
54 }
55
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'] : '';
62
63 $this->mComment = isset( $options['description'] ) ?
64 $options['description'] : '';
65
66 $this->mTextTop = isset( $options['texttop'] )
67 ? $options['texttop'] : '';
68
69 $this->mTextAfterSummary = isset( $options['textaftersummary'] )
70 ? $options['textaftersummary'] : '';
71
72 $sourceDescriptor = $this->getSourceSection();
73 $descriptor = $sourceDescriptor
74 + $this->getDescriptionSection()
75 + $this->getOptionsSection();
76
77 Hooks::run( 'UploadFormInitDescriptor', [ &$descriptor ] );
78 parent::__construct( $descriptor, $context, 'upload' );
79
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(),
86 [],
87 [ 'action' => 'edit' ]
88 );
89 $editLicenses = '<p class="mw-upload-editlicenses">' . $licensesLink . '</p>';
90 $this->addFooterText( $editLicenses, 'description' );
91 }
92
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' );
99
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'];
105 }
106 }
107 }
108
115 protected function getSourceSection() {
116 if ( $this->mSessionKey ) {
117 return [
118 'SessionKey' => [
119 'type' => 'hidden',
120 'default' => $this->mSessionKey,
121 ],
122 'SourceType' => [
123 'type' => 'hidden',
124 'default' => 'Stash',
125 ],
126 ];
127 }
128
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' ) );
134
135 $descriptor = [];
136 if ( $this->mTextTop ) {
137 $descriptor['UploadFormTextTop'] = [
138 'type' => 'info',
139 'section' => 'source',
140 'default' => $this->mTextTop,
141 'raw' => true,
142 ];
143 }
144
145 $this->mMaxUploadSize['file'] = min(
148 );
149
150 $help = $this->msg( 'upload-maxfilesize',
151 $this->getContext()->getLanguage()->formatSize( $this->mMaxUploadSize['file'] )
152 )->parse();
153
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();
159 }
160
161 $descriptor['UploadFile'] = [
162 'class' => UploadSourceField::class,
163 'section' => 'source',
164 'type' => 'file',
165 'id' => 'wpUploadFile',
166 'radio-id' => 'wpSourceTypeFile',
167 'label-message' => 'sourcefilename',
168 'upload-type' => 'File',
169 'radio' => &$radio,
170 'help' => $help,
171 'checked' => $selectedSourceType == 'file',
172 ];
173
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',
183 'radio' => &$radio,
184 'help' => $this->msg( 'upload-maxfilesize',
185 $this->getContext()->getLanguage()->formatSize( $this->mMaxUploadSize['url'] )
186 )->parse() .
187 $this->msg( 'word-separator' )->escaped() .
188 $this->msg( 'upload_source_url' )->parse(),
189 'checked' => $selectedSourceType == 'url',
190 ];
191 }
192 Hooks::run( 'UploadFormSourceDescriptors', [ &$descriptor, &$radio, $selectedSourceType ] );
193
194 $descriptor['Extensions'] = [
195 'type' => 'info',
196 'section' => 'source',
197 'default' => $this->getExtensionsMessage(),
198 'raw' => true,
199 ];
200
201 return $descriptor;
202 }
203
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();
213
214 if ( $config->get( 'CheckFileExtensions' ) ) {
215 $fileExtensions = array_unique( $config->get( 'FileExtensions' ) );
216 if ( $config->get( 'StrictFileExtensions' ) ) {
217 # Everything not permitted is banned
218 $extensionsList =
219 '<div id="mw-upload-permitted">' .
220 $this->msg( 'upload-permitted' )
221 ->params( $this->getLanguage()->commaList( $fileExtensions ) )
222 ->numParams( count( $fileExtensions ) )
223 ->parseAsBlock() .
224 "</div>\n";
225 } else {
226 # We have to list both preferred and prohibited
227 $fileBlacklist = array_unique( $config->get( 'FileBlacklist' ) );
228 $extensionsList =
229 '<div id="mw-upload-preferred">' .
230 $this->msg( 'upload-preferred' )
231 ->params( $this->getLanguage()->commaList( $fileExtensions ) )
232 ->numParams( count( $fileExtensions ) )
233 ->parseAsBlock() .
234 "</div>\n" .
235 '<div id="mw-upload-prohibited">' .
236 $this->msg( 'upload-prohibited' )
237 ->params( $this->getLanguage()->commaList( $fileBlacklist ) )
238 ->numParams( count( $fileBlacklist ) )
239 ->parseAsBlock() .
240 "</div>\n";
241 }
242 } else {
243 # Everything is permitted.
244 $extensionsList = '';
245 }
246
247 return $extensionsList;
248 }
249
256 protected function getDescriptionSection() {
257 $config = $this->getConfig();
258 if ( $this->mSessionKey ) {
259 $stash = RepoGroup::singleton()->getLocalRepo()->getUploadStash( $this->getUser() );
260 try {
261 $file = $stash->getFile( $this->mSessionKey );
262 } catch ( Exception $e ) {
263 $file = null;
264 }
265 if ( $file ) {
267
268 $mto = $file->transform( [ 'width' => 120 ] );
269 if ( $mto ) {
270 $this->addHeaderText(
271 '<div class="thumb t' . $wgContLang->alignEnd() . '">' .
272 Html::element( 'img', [
273 'src' => $mto->getUrl(),
274 'class' => 'thumbimage',
275 ] ) . '</div>', 'description' );
276 }
277 }
278 }
279
280 $descriptor = [
281 'DestFile' => [
282 'type' => 'text',
283 'section' => 'description',
284 'id' => 'wpDestFile',
285 'label-message' => 'destfilename',
286 'size' => 60,
287 'default' => $this->mDestFile,
288 # @todo FIXME: Hack to work around poor handling of the 'default' option in HTMLForm
289 'nodata' => strval( $this->mDestFile ) !== '',
290 ],
291 'UploadDescription' => [
292 'type' => 'textarea',
293 'section' => 'description',
294 'id' => 'wpUploadDescription',
295 'label-message' => $this->mForReUpload
296 ? 'filereuploadsummary'
297 : 'fileuploadsummary',
298 'default' => $this->mComment,
299 'cols' => 80,
300 'rows' => 8,
301 ]
302 ];
303 if ( $this->mTextAfterSummary ) {
304 $descriptor['UploadFormTextAfterSummary'] = [
305 'type' => 'info',
306 'section' => 'description',
307 'default' => $this->mTextAfterSummary,
308 'raw' => true,
309 ];
310 }
311
312 $descriptor += [
313 'EditTools' => [
314 'type' => 'edittools',
315 'section' => 'description',
316 'message' => 'edittools-upload',
317 ]
318 ];
319
320 if ( $this->mForReUpload ) {
321 $descriptor['DestFile']['readonly'] = true;
322 } else {
323 $descriptor['License'] = [
324 'type' => 'select',
325 'class' => Licenses::class,
326 'section' => 'description',
327 'id' => 'wpLicense',
328 'label-message' => 'license',
329 ];
330 }
331
332 if ( $config->get( 'UseCopyrightUpload' ) ) {
333 $descriptor['UploadCopyStatus'] = [
334 'type' => 'text',
335 'section' => 'description',
336 'id' => 'wpUploadCopyStatus',
337 'label-message' => 'filestatus',
338 ];
339 $descriptor['UploadSource'] = [
340 'type' => 'text',
341 'section' => 'description',
342 'id' => 'wpUploadSource',
343 'label-message' => 'filesource',
344 ];
345 }
346
347 return $descriptor;
348 }
349
356 protected function getOptionsSection() {
357 $user = $this->getUser();
358 if ( $user->isLoggedIn() ) {
359 $descriptor = [
360 'Watchthis' => [
361 'type' => 'check',
362 'id' => 'wpWatchthis',
363 'label-message' => 'watchthisupload',
364 'section' => 'options',
365 'default' => $this->mWatch,
366 ]
367 ];
368 }
369 if ( !$this->mHideIgnoreWarning ) {
370 $descriptor['IgnoreWarning'] = [
371 'type' => 'check',
372 'id' => 'wpIgnoreWarning',
373 'label-message' => 'ignorewarnings',
374 'section' => 'options',
375 ];
376 }
377
378 $descriptor['DestFileWarningAck'] = [
379 'type' => 'hidden',
380 'id' => 'wpDestFileWarningAck',
381 'default' => $this->mDestWarningAck ? '1' : '',
382 ];
383
384 if ( $this->mForReUpload ) {
385 $descriptor['ForReUpload'] = [
386 'type' => 'hidden',
387 'id' => 'wpForReUpload',
388 'default' => '1',
389 ];
390 }
391
392 return $descriptor;
393 }
394
398 public function show() {
399 $this->addUploadJS();
400 parent::show();
401 }
402
406 protected function addUploadJS() {
407 $config = $this->getConfig();
408
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();
413
414 $scriptVars = [
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(),
428 ];
429
430 $out = $this->getOutput();
431 $out->addJsConfigVars( $scriptVars );
432
433 $out->addModules( [
434 'mediawiki.special.upload', // Extras for thumbnail and license preview.
435 ] );
436 }
437
443 function trySubmit() {
444 return false;
445 }
446}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
msg( $key)
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.
Definition HTMLForm.php:130
setSubmitName( $name)
setId( $id)
addFooterText( $msg, $section=null)
Add footer text, inside the form.
Definition HTMLForm.php:816
addHeaderText( $msg, $section=null)
Add HTML to the header, inside the form.
Definition HTMLForm.php:761
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.
static singleton()
Get a RepoGroup instance.
Definition RepoGroup.php:59
static rotationEnabled()
Should we rotate images in the preview on Special:Upload.
static getMaxUploadSize( $forType=null)
Get the MediaWiki maximum uploaded file size for given type of upload, based on $wgMaxUploadSize.
static getMaxPhpUploadSize()
Get the PHP maximum uploaded file size, based on ini settings.
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.
static isAllowed( $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.
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the local content language as $wgContLang
Definition design.txt:57
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add text
Definition design.txt:18
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
const NS_FILE
Definition Defines.php:80
the array() calling protocol came about after MediaWiki 1.4rc1.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition hooks.txt:2001
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition hooks.txt:2006
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition hooks.txt:864
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing after in associative array form before processing starts Return false to skip default processing and return $ret $linkRenderer
Definition hooks.txt:2056
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition hooks.txt:247
returning false will NOT prevent logging $e
Definition hooks.txt:2176
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
Interface for objects which can provide a MediaWiki context on request.
$help
Definition mcc.php:32