MediaWiki  master
UploadForm.php
Go to the documentation of this file.
1 <?php
24 
28 class UploadForm extends HTMLForm {
29  protected $mWatch;
30  protected $mForReUpload;
31  protected $mSessionKey;
33  protected $mDestWarningAck;
34  protected $mDestFile;
35 
36  protected $mComment;
37  protected $mTextTop;
38  protected $mTextAfterSummary;
39 
40  protected $mSourceIds;
41 
42  protected $mMaxFileSize = [];
43 
45  protected $mMaxUploadSize = [];
46 
48  private $localRepo;
49 
52 
54  private $nsInfo;
55 
65  public function __construct(
66  array $options = [],
67  IContextSource $context = null,
68  LinkRenderer $linkRenderer = null,
69  PermissionManager $permissionManager = null,
70  LocalRepo $localRepo = null,
72  NamespaceInfo $nsInfo = null
73  ) {
74  if ( $context instanceof IContextSource ) {
75  $this->setContext( $context );
76  }
77 
78  $services = MediaWikiServices::getInstance();
79  if ( !$linkRenderer ) {
80  $linkRenderer = $services->getLinkRenderer();
81  }
82  if ( !$permissionManager ) {
83  $permissionManager = $services->getPermissionManager();
84  }
85  if ( !$localRepo ) {
86  $localRepo = $services->getRepoGroup()->getLocalRepo();
87  }
88  if ( !$contentLanguage ) {
89  $contentLanguage = $services->getContentLanguage();
90  }
91  if ( !$nsInfo ) {
92  $nsInfo = $services->getNamespaceInfo();
93  }
94  $this->localRepo = $localRepo;
95  $this->contentLanguage = $contentLanguage;
96  $this->nsInfo = $nsInfo;
97 
98  $this->mWatch = !empty( $options['watch'] );
99  $this->mForReUpload = !empty( $options['forreupload'] );
100  $this->mSessionKey = $options['sessionkey'] ?? '';
101  $this->mHideIgnoreWarning = !empty( $options['hideignorewarning'] );
102  $this->mDestWarningAck = !empty( $options['destwarningack'] );
103  $this->mDestFile = $options['destfile'] ?? '';
104 
105  $this->mComment = $options['description'] ?? '';
106 
107  $this->mTextTop = $options['texttop'] ?? '';
108 
109  $this->mTextAfterSummary = $options['textaftersummary'] ?? '';
110 
111  $sourceDescriptor = $this->getSourceSection();
112  $descriptor = $sourceDescriptor
113  + $this->getDescriptionSection()
114  + $this->getOptionsSection();
115 
116  $this->getHookRunner()->onUploadFormInitDescriptor( $descriptor );
117  parent::__construct( $descriptor, $context, 'upload' );
118 
119  # Add a link to edit MediaWiki:Licenses
120  if ( $permissionManager->userHasRight( $this->getUser(), 'editinterface' ) ) {
121  $this->getOutput()->addModuleStyles( 'mediawiki.special' );
122  $licensesLink = $linkRenderer->makeKnownLink(
123  $this->msg( 'licenses' )->inContentLanguage()->getTitle(),
124  $this->msg( 'licenses-edit' )->text(),
125  [],
126  [ 'action' => 'edit' ]
127  );
128  $editLicenses = '<p class="mw-upload-editlicenses">' . $licensesLink . '</p>';
129  $this->addFooterText( $editLicenses, 'description' );
130  }
131 
132  # Set some form properties
133  $this->setSubmitText( $this->msg( 'uploadbtn' )->text() );
134  $this->setSubmitName( 'wpUpload' );
135  # Used message keys: 'accesskey-upload', 'tooltip-upload'
136  $this->setSubmitTooltip( 'upload' );
137  $this->setId( 'mw-upload-form' );
138 
139  # Build a list of IDs for javascript insertion
140  $this->mSourceIds = [];
141  foreach ( $sourceDescriptor as $field ) {
142  if ( !empty( $field['id'] ) ) {
143  $this->mSourceIds[] = $field['id'];
144  }
145  }
146  }
147 
154  protected function getSourceSection() {
155  if ( $this->mSessionKey ) {
156  return [
157  'SessionKey' => [
158  'type' => 'hidden',
159  'default' => $this->mSessionKey,
160  ],
161  'SourceType' => [
162  'type' => 'hidden',
163  'default' => 'Stash',
164  ],
165  ];
166  }
167 
168  $canUploadByUrl = UploadFromUrl::isEnabled()
169  && ( UploadFromUrl::isAllowed( $this->getUser() ) === true )
170  && $this->getConfig()->get( 'CopyUploadsFromSpecialUpload' );
171  $radio = $canUploadByUrl;
172  $selectedSourceType = strtolower( $this->getRequest()->getText( 'wpSourceType', 'File' ) );
173 
174  $descriptor = [];
175  if ( $this->mTextTop ) {
176  $descriptor['UploadFormTextTop'] = [
177  'type' => 'info',
178  'section' => 'source',
179  'default' => $this->mTextTop,
180  'raw' => true,
181  ];
182  }
183 
184  $this->mMaxUploadSize['file'] = min(
187  );
188 
189  $help = $this->msg( 'upload-maxfilesize',
190  $this->getContext()->getLanguage()->formatSize( $this->mMaxUploadSize['file'] )
191  )->parse();
192 
193  // If the user can also upload by URL, there are 2 different file size limits.
194  // This extra message helps stress which limit corresponds to what.
195  if ( $canUploadByUrl ) {
196  $help .= $this->msg( 'word-separator' )->escaped();
197  $help .= $this->msg( 'upload_source_file' )->parse();
198  }
199 
200  $descriptor['UploadFile'] = [
201  'class' => UploadSourceField::class,
202  'section' => 'source',
203  'type' => 'file',
204  'id' => 'wpUploadFile',
205  'radio-id' => 'wpSourceTypeFile',
206  'label-message' => 'sourcefilename',
207  'upload-type' => 'File',
208  'radio' => &$radio,
209  'help' => $help,
210  'checked' => $selectedSourceType == 'file',
211  ];
212 
213  if ( $canUploadByUrl ) {
214  $this->mMaxUploadSize['url'] = UploadBase::getMaxUploadSize( 'url' );
215  $descriptor['UploadFileURL'] = [
216  'class' => UploadSourceField::class,
217  'section' => 'source',
218  'id' => 'wpUploadFileURL',
219  'radio-id' => 'wpSourceTypeurl',
220  'label-message' => 'sourceurl',
221  'upload-type' => 'url',
222  'radio' => &$radio,
223  'help' => $this->msg( 'upload-maxfilesize',
224  $this->getContext()->getLanguage()->formatSize( $this->mMaxUploadSize['url'] )
225  )->parse() .
226  $this->msg( 'word-separator' )->escaped() .
227  $this->msg( 'upload_source_url' )->parse(),
228  'checked' => $selectedSourceType == 'url',
229  ];
230  }
231  $this->getHookRunner()->onUploadFormSourceDescriptors(
232  $descriptor, $radio, $selectedSourceType );
233 
234  $descriptor['Extensions'] = [
235  'type' => 'info',
236  'section' => 'source',
237  'default' => $this->getExtensionsMessage(),
238  'raw' => true,
239  ];
240 
241  return $descriptor;
242  }
243 
249  protected function getExtensionsMessage() {
250  # Print a list of allowed file extensions, if so configured. We ignore
251  # MIME type here, it's incomprehensible to most people and too long.
252  $config = $this->getConfig();
253 
254  if ( $config->get( 'CheckFileExtensions' ) ) {
255  $fileExtensions = array_unique( $config->get( 'FileExtensions' ) );
256  if ( $config->get( 'StrictFileExtensions' ) ) {
257  # Everything not permitted is banned
258  $extensionsList =
259  '<div id="mw-upload-permitted">' .
260  $this->msg( 'upload-permitted' )
261  ->params( $this->getLanguage()->commaList( $fileExtensions ) )
262  ->numParams( count( $fileExtensions ) )
263  ->parseAsBlock() .
264  "</div>\n";
265  } else {
266  # We have to list both preferred and prohibited
267  $fileBlacklist = array_unique( $config->get( 'FileBlacklist' ) );
268  $extensionsList =
269  '<div id="mw-upload-preferred">' .
270  $this->msg( 'upload-preferred' )
271  ->params( $this->getLanguage()->commaList( $fileExtensions ) )
272  ->numParams( count( $fileExtensions ) )
273  ->parseAsBlock() .
274  "</div>\n" .
275  '<div id="mw-upload-prohibited">' .
276  $this->msg( 'upload-prohibited' )
277  ->params( $this->getLanguage()->commaList( $fileBlacklist ) )
278  ->numParams( count( $fileBlacklist ) )
279  ->parseAsBlock() .
280  "</div>\n";
281  }
282  } else {
283  # Everything is permitted.
284  $extensionsList = '';
285  }
286 
287  return $extensionsList;
288  }
289 
296  protected function getDescriptionSection() {
297  $config = $this->getConfig();
298  if ( $this->mSessionKey ) {
299  $stash = $this->localRepo->getUploadStash( $this->getUser() );
300  try {
301  $file = $stash->getFile( $this->mSessionKey );
302  } catch ( Exception $e ) {
303  $file = null;
304  }
305  if ( $file ) {
306  $mto = $file->transform( [ 'width' => 120 ] );
307  if ( $mto ) {
308  $this->addHeaderText(
309  '<div class="thumb t' .
310  $this->contentLanguage->alignEnd() . '">' .
311  Html::element( 'img', [
312  'src' => $mto->getUrl(),
313  'class' => 'thumbimage',
314  ] ) . '</div>', 'description' );
315  }
316  }
317  }
318 
319  $descriptor = [
320  'DestFile' => [
321  'type' => 'text',
322  'section' => 'description',
323  'id' => 'wpDestFile',
324  'label-message' => 'destfilename',
325  'size' => 60,
326  'default' => $this->mDestFile,
327  # @todo FIXME: Hack to work around poor handling of the 'default' option in HTMLForm
328  'nodata' => strval( $this->mDestFile ) !== '',
329  ],
330  'UploadDescription' => [
331  'type' => 'textarea',
332  'section' => 'description',
333  'id' => 'wpUploadDescription',
334  'label-message' => $this->mForReUpload
335  ? 'filereuploadsummary'
336  : 'fileuploadsummary',
337  'default' => $this->mComment,
338  'cols' => 80,
339  'rows' => 8,
340  ]
341  ];
342  if ( $this->mTextAfterSummary ) {
343  $descriptor['UploadFormTextAfterSummary'] = [
344  'type' => 'info',
345  'section' => 'description',
346  'default' => $this->mTextAfterSummary,
347  'raw' => true,
348  ];
349  }
350 
351  $descriptor += [
352  'EditTools' => [
353  'type' => 'edittools',
354  'section' => 'description',
355  'message' => 'edittools-upload',
356  ]
357  ];
358 
359  if ( $this->mForReUpload ) {
360  $descriptor['DestFile']['readonly'] = true;
361  } else {
362  $descriptor['License'] = [
363  'type' => 'select',
364  'class' => Licenses::class,
365  'section' => 'description',
366  'id' => 'wpLicense',
367  'label-message' => 'license',
368  ];
369  }
370 
371  if ( $config->get( 'UseCopyrightUpload' ) ) {
372  $descriptor['UploadCopyStatus'] = [
373  'type' => 'text',
374  'section' => 'description',
375  'id' => 'wpUploadCopyStatus',
376  'label-message' => 'filestatus',
377  ];
378  $descriptor['UploadSource'] = [
379  'type' => 'text',
380  'section' => 'description',
381  'id' => 'wpUploadSource',
382  'label-message' => 'filesource',
383  ];
384  }
385 
386  return $descriptor;
387  }
388 
395  protected function getOptionsSection() {
396  $user = $this->getUser();
397  if ( $user->isLoggedIn() ) {
398  $descriptor = [
399  'Watchthis' => [
400  'type' => 'check',
401  'id' => 'wpWatchthis',
402  'label-message' => 'watchthisupload',
403  'section' => 'options',
404  'default' => $this->mWatch,
405  ]
406  ];
407  }
408  if ( !$this->mHideIgnoreWarning ) {
409  $descriptor['IgnoreWarning'] = [
410  'type' => 'check',
411  'id' => 'wpIgnoreWarning',
412  'label-message' => 'ignorewarnings',
413  'section' => 'options',
414  ];
415  }
416 
417  $descriptor['DestFileWarningAck'] = [
418  'type' => 'hidden',
419  'id' => 'wpDestFileWarningAck',
420  'default' => $this->mDestWarningAck ? '1' : '',
421  ];
422 
423  if ( $this->mForReUpload ) {
424  $descriptor['ForReUpload'] = [
425  'type' => 'hidden',
426  'id' => 'wpForReUpload',
427  'default' => '1',
428  ];
429  }
430 
431  return $descriptor;
432  }
433 
438  public function show() {
439  $this->addUploadJS();
440  return parent::show();
441  }
442 
446  protected function addUploadJS() {
447  $config = $this->getConfig();
448 
449  $this->mMaxUploadSize['*'] = UploadBase::getMaxUploadSize();
450 
451  $scriptVars = [
452  'wgAjaxUploadDestCheck' => $config->get( 'AjaxUploadDestCheck' ),
453  'wgAjaxLicensePreview' => $config->get( 'AjaxLicensePreview' ),
454  'wgUploadAutoFill' => !$this->mForReUpload &&
455  // If we received mDestFile from the request, don't autofill
456  // the wpDestFile textbox
457  $this->mDestFile === '',
458  'wgUploadSourceIds' => $this->mSourceIds,
459  'wgCheckFileExtensions' => $config->get( 'CheckFileExtensions' ),
460  'wgStrictFileExtensions' => $config->get( 'StrictFileExtensions' ),
461  'wgFileExtensions' => array_values( array_unique( $config->get( 'FileExtensions' ) ) ),
462  'wgCapitalizeUploads' => $this->nsInfo->isCapitalized( NS_FILE ),
463  'wgMaxUploadSize' => $this->mMaxUploadSize,
464  'wgFileCanRotate' => SpecialUpload::rotationEnabled(),
465  ];
466 
467  $out = $this->getOutput();
468  $out->addJsConfigVars( $scriptVars );
469 
470  $out->addModules( [
471  'mediawiki.special.upload', // Extras for thumbnail and license preview.
472  ] );
473  }
474 
480  public function trySubmit() {
481  return false;
482  }
483 }
ContextSource\$context
IContextSource $context
Definition: ContextSource.php:37
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:70
UploadForm\$mMaxUploadSize
array $mMaxUploadSize
Definition: UploadForm.php:45
ContextSource\getContext
getContext()
Get the base IContextSource object.
Definition: ContextSource.php:45
UploadForm\getDescriptionSection
getDescriptionSection()
Get the descriptor of the fieldset that contains the file description input.
Definition: UploadForm.php:296
HTMLForm\setSubmitName
setSubmitName( $name)
Definition: HTMLForm.php:1436
UploadForm\show
show()
Add the upload JS and show the form.
Definition: UploadForm.php:438
UploadForm\$nsInfo
NamespaceInfo $nsInfo
Definition: UploadForm.php:54
UploadForm\getSourceSection
getSourceSection()
Get the descriptor of the fieldset that contains the file source selection.
Definition: UploadForm.php:154
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:165
HTMLForm\setSubmitTooltip
setSubmitTooltip( $name)
Definition: HTMLForm.php:1447
HTMLForm\addHeaderText
addHeaderText( $msg, $section=null)
Add HTML to the header, inside the form.
Definition: HTMLForm.php:820
UploadForm\$mForReUpload
$mForReUpload
Definition: UploadForm.php:30
true
return true
Definition: router.php:90
MediaWiki\Linker\LinkRenderer
Class that generates HTML links for pages.
Definition: LinkRenderer.php:41
UploadBase\getMaxPhpUploadSize
static getMaxPhpUploadSize()
Get the PHP maximum uploaded file size, based on ini settings.
Definition: UploadBase.php:2246
UploadForm
Sub class of HTMLForm that provides the form section of SpecialUpload.
Definition: UploadForm.php:28
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:36
UploadForm\$mTextAfterSummary
$mTextAfterSummary
Definition: UploadForm.php:38
ContextSource\getRequest
getRequest()
Definition: ContextSource.php:79
ContextSource\getUser
getUser()
Stable to override.
Definition: ContextSource.php:134
UploadForm\$mDestFile
$mDestFile
Definition: UploadForm.php:34
UploadForm\trySubmit
trySubmit()
Empty function; submission is handled elsewhere.
Definition: UploadForm.php:480
ContextSource\getLanguage
getLanguage()
Definition: ContextSource.php:143
HTMLForm\setSubmitText
setSubmitText( $t)
Set the text for the submit button.
Definition: HTMLForm.php:1388
UploadForm\__construct
__construct(array $options=[], IContextSource $context=null, LinkRenderer $linkRenderer=null, PermissionManager $permissionManager=null, LocalRepo $localRepo=null, Language $contentLanguage=null, NamespaceInfo $nsInfo=null)
Definition: UploadForm.php:65
UploadForm\getExtensionsMessage
getExtensionsMessage()
Get the messages indicating which extensions are preferred and prohibitted.
Definition: UploadForm.php:249
UploadForm\$mHideIgnoreWarning
$mHideIgnoreWarning
Definition: UploadForm.php:32
UploadFromUrl\isEnabled
static isEnabled()
Checks if the upload from URL feature is enabled.
Definition: UploadFromUrl.php:65
ContextSource\getOutput
getOutput()
Definition: ContextSource.php:124
ContextSource\setContext
setContext(IContextSource $context)
Definition: ContextSource.php:61
UploadForm\addUploadJS
addUploadJS()
Add upload JS to the OutputPage.
Definition: UploadForm.php:446
ContextSource\msg
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: ContextSource.php:187
UploadForm\$mMaxFileSize
$mMaxFileSize
Definition: UploadForm.php:42
MediaWiki\Permissions\PermissionManager
A service class for checking permissions To obtain an instance, use MediaWikiServices::getInstance()-...
Definition: PermissionManager.php:50
UploadForm\$localRepo
LocalRepo $localRepo
Definition: UploadForm.php:48
UploadForm\$mWatch
$mWatch
Definition: UploadForm.php:29
UploadForm\$mTextTop
$mTextTop
Definition: UploadForm.php:37
HTMLForm\setId
setId( $id)
Definition: HTMLForm.php:1546
SpecialUpload\rotationEnabled
static rotationEnabled()
Should we rotate images in the preview on Special:Upload.
Definition: SpecialUpload.php:906
UploadForm\$contentLanguage
Language $contentLanguage
Definition: UploadForm.php:51
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:55
UploadBase\getMaxUploadSize
static getMaxUploadSize( $forType=null)
Get MediaWiki's maximum uploaded file size for given type of upload, based on $wgMaxUploadSize.
Definition: UploadBase.php:2225
UploadForm\$mSourceIds
$mSourceIds
Definition: UploadForm.php:40
UploadForm\$mSessionKey
$mSessionKey
Definition: UploadForm.php:31
HTMLForm\getTitle
getTitle()
Definition: HTMLForm.php:1641
$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
NamespaceInfo
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
Definition: NamespaceInfo.php:35
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:234
UploadForm\getOptionsSection
getOptionsSection()
Get the descriptor of the fieldset that contains the upload options, such as "watch this file".
Definition: UploadForm.php:395
Language
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition: Language.php:42
LocalRepo
A repository that stores files in the local filesystem and registers them in the wiki's own database.
Definition: LocalRepo.php:37
HTMLForm\addFooterText
addFooterText( $msg, $section=null)
Add footer text, inside the form.
Definition: HTMLForm.php:876
UploadForm\$mDestWarningAck
$mDestWarningAck
Definition: UploadForm.php:33
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:135