MediaWiki  master
UploadForm.php
Go to the documentation of this file.
1 <?php
23 
27 class UploadForm extends HTMLForm {
28  protected $mWatch;
29  protected $mForReUpload;
30  protected $mSessionKey;
32  protected $mDestWarningAck;
33  protected $mDestFile;
34 
35  protected $mComment;
36  protected $mTextTop;
37  protected $mTextAfterSummary;
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(
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 }
ContextSource\$context
IContextSource $context
Definition: ContextSource.php:33
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:63
UploadForm\$mMaxUploadSize
array $mMaxUploadSize
Definition: UploadForm.php:44
ContextSource\getContext
getContext()
Get the base IContextSource object.
Definition: ContextSource.php:40
UploadForm\getDescriptionSection
getDescriptionSection()
Get the descriptor of the fieldset that contains the file description input.
Definition: UploadForm.php:258
HTMLForm\setSubmitName
setSubmitName( $name)
Definition: HTMLForm.php:1410
UploadForm\show
show()
Add the upload JS and show the form.
Definition: UploadForm.php:401
UploadForm\getSourceSection
getSourceSection()
Get the descriptor of the fieldset that contains the file source selection.
Definition: UploadForm.php:116
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:146
HTMLForm\setSubmitTooltip
setSubmitTooltip( $name)
Definition: HTMLForm.php:1421
HTMLForm\addHeaderText
addHeaderText( $msg, $section=null)
Add HTML to the header, inside the form.
Definition: HTMLForm.php:810
UploadForm\$mForReUpload
$mForReUpload
Definition: UploadForm.php:29
true
return true
Definition: router.php:90
MediaWiki\Linker\LinkRenderer
Class that generates HTML links for pages.
Definition: LinkRenderer.php:43
UploadBase\getMaxPhpUploadSize
static getMaxPhpUploadSize()
Get the PHP maximum uploaded file size, based on ini settings.
Definition: UploadBase.php:2221
UploadForm
Sub class of HTMLForm that provides the form section of SpecialUpload.
Definition: UploadForm.php:27
NS_FILE
const NS_FILE
Definition: Defines.php:75
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
UploadForm\$mComment
$mComment
Definition: UploadForm.php:35
UploadForm\$mTextAfterSummary
$mTextAfterSummary
Definition: UploadForm.php:37
ContextSource\getRequest
getRequest()
Definition: ContextSource.php:71
ContextSource\getUser
getUser()
Definition: ContextSource.php:120
UploadForm\$mDestFile
$mDestFile
Definition: UploadForm.php:33
UploadForm\trySubmit
trySubmit()
Empty function; submission is handled elsewhere.
Definition: UploadForm.php:444
ContextSource\getLanguage
getLanguage()
Definition: ContextSource.php:128
HTMLForm\setSubmitText
setSubmitText( $t)
Set the text for the submit button.
Definition: HTMLForm.php:1362
UploadForm\getExtensionsMessage
getExtensionsMessage()
Get the messages indicating which extensions are preferred and prohibitted.
Definition: UploadForm.php:211
UploadForm\__construct
__construct(array $options=[], IContextSource $context=null, LinkRenderer $linkRenderer=null)
Definition: UploadForm.php:46
UploadForm\$mHideIgnoreWarning
$mHideIgnoreWarning
Definition: UploadForm.php:31
UploadFromUrl\isEnabled
static isEnabled()
Checks if the upload from URL feature is enabled.
Definition: UploadFromUrl.php:65
getPermissionManager
getPermissionManager()
ContextSource\getOutput
getOutput()
Definition: ContextSource.php:112
ContextSource\setContext
setContext(IContextSource $context)
Definition: ContextSource.php:55
UploadForm\addUploadJS
addUploadJS()
Add upload JS to the OutputPage.
Definition: UploadForm.php:409
ContextSource\msg
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: ContextSource.php:168
UploadForm\$mMaxFileSize
$mMaxFileSize
Definition: UploadForm.php:41
UploadForm\$mWatch
$mWatch
Definition: UploadForm.php:28
UploadForm\$mTextTop
$mTextTop
Definition: UploadForm.php:36
HTMLForm\setId
setId( $id)
Definition: HTMLForm.php:1520
SpecialUpload\rotationEnabled
static rotationEnabled()
Should we rotate images in the preview on Special:Upload.
Definition: SpecialUpload.php:872
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:53
UploadBase\getMaxUploadSize
static getMaxUploadSize( $forType=null)
Get MediaWiki's maximum uploaded file size for given type of upload, based on $wgMaxUploadSize.
Definition: UploadBase.php:2200
UploadForm\$mSourceIds
$mSourceIds
Definition: UploadForm.php:39
UploadForm\$mSessionKey
$mSessionKey
Definition: UploadForm.php:30
HTMLForm\getTitle
getTitle()
Get the title.
Definition: HTMLForm.php:1616
$help
$help
Definition: mcc.php:32
UploadFromUrl\isAllowed
static isAllowed(UserIdentity $user)
Checks if the user is allowed to use the upload-by-URL feature.
Definition: UploadFromUrl.php:50
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:231
UploadForm\getOptionsSection
getOptionsSection()
Get the descriptor of the fieldset that contains the upload options, such as "watch this file".
Definition: UploadForm.php:358
HTMLForm\addFooterText
addFooterText( $msg, $section=null)
Add footer text, inside the form.
Definition: HTMLForm.php:865
UploadForm\$mDestWarningAck
$mDestWarningAck
Definition: UploadForm.php:32
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:133