9025fc228420bd570355581b83e7f733b5a70eb6
[lhc/web/wiklou.git] / includes / specials / SpecialUpload.php
1 <?php
2 /**
3 * @file
4 * @ingroup SpecialPage
5 */
6
7 /**
8 * implements Special:Upload
9 * @ingroup SpecialPage
10 */
11 class UploadForm extends SpecialPage {
12 /**#@+
13 * @access private
14 */
15 var $mComment, $mLicense, $mIgnoreWarning;
16 var $mCopyrightStatus, $mCopyrightSource, $mReUpload, $mAction, $mUploadClicked;
17 var $mDestWarningAck;
18 var $mLocalFile;
19
20 var $mUpload; // Instance of UploadBase or derivative
21
22 # Placeholders for text injection by hooks (must be HTML)
23 # extensions should take care to _append_ to the present value
24 var $uploadFormTextTop;
25 var $uploadFormTextAfterSummary;
26 var $mTokenOk = false;
27 var $mForReUpload = false;
28 /**#@-*/
29
30 /**
31 * Constructor : initialise object
32 * Get data POSTed through the form and assign them to the object
33 * @param $request Data posted.
34 */
35 function __construct( $request = null ) {
36 parent::__construct( 'Upload', 'upload' );
37 $this->mRequest = $request;
38 }
39
40 protected function initForm() {
41 global $wgRequest, $wgUser;
42
43 if ( is_null( $this->mRequest ) ) {
44 $request = $wgRequest;
45 } else {
46 $request = $this->mRequest;
47 }
48 // Guess the desired name from the filename if not provided
49 $this->mDesiredDestName = $request->getText( 'wpDestFile' );
50 if( !$this->mDesiredDestName )
51 $this->mDesiredDestName = $request->getText( 'wpUploadFile' );
52
53 $this->mForReUpload = $request->getBool( 'wpForReUpload' ); // updating a file
54 $this->mIgnoreWarning = $request->getCheck( 'wpIgnoreWarning' );
55 $this->mComment = $request->getText( 'wpUploadDescription' );
56
57 if( !$request->wasPosted() ) {
58 # GET requests just give the main form; no data except destination
59 # filename and description
60 return;
61 }
62
63 # Placeholders for text injection by hooks (empty per default)
64 $this->uploadFormTextTop = "";
65 $this->uploadFormTextAfterSummary = "";
66
67 $this->mUploadClicked = $request->getCheck( 'wpUpload' );
68
69 $this->mLicense = $request->getText( 'wpLicense' );
70 $this->mCopyrightStatus = $request->getText( 'wpUploadCopyStatus' );
71 $this->mCopyrightSource = $request->getText( 'wpUploadSource' );
72 $this->mWatchthis = $request->getBool( 'wpWatchthis' );
73 $this->mSourceType = $request->getVal( 'wpSourceType', 'file' );
74 $this->mDestWarningAck = $request->getText( 'wpDestFileWarningAck' );
75
76 $this->mReUpload = $request->getCheck( 'wpReUpload' ); // retrying upload
77
78 $this->mAction = $request->getVal( 'action' );
79 $this->mUpload = UploadBase::createFromRequest( $request );
80
81 // If it was posted check for the token (no remote POST'ing with user credentials)
82 $token = $request->getVal( 'wpEditToken' );
83 if( $this->mSourceType == 'file' && $token == null ) {
84 // Skip token check for file uploads as that can't be faked via JS...
85 // Some client-side tools don't expect to need to send wpEditToken
86 // with their submissions, as that's new in 1.16.
87 $this->mTokenOk = true;
88 } else {
89 $this->mTokenOk = $wgUser->matchEditToken( $token );
90 }
91 }
92
93 public function userCanExecute( $user ) {
94 return UploadBase::isEnabled() && parent::userCanExecute( $user );
95 }
96
97 /**
98 * Start doing stuff
99 * @access public
100 */
101 function execute( $par ) {
102 global $wgUser, $wgOut, $wgRequest;
103
104 $this->setHeaders();
105 $this->outputHeader();
106
107 $this->initForm();
108
109 # Check uploading enabled
110 if( !UploadBase::isEnabled() ) {
111 $wgOut->showErrorPage( 'uploaddisabled', 'uploaddisabledtext' );
112 return;
113 }
114
115 # Check permissions
116 global $wgGroupPermissions;
117 if( !$wgUser->isAllowed( 'upload' ) ) {
118 if( !$wgUser->isLoggedIn() && ( $wgGroupPermissions['user']['upload']
119 || $wgGroupPermissions['autoconfirmed']['upload'] ) ) {
120 // Custom message if logged-in users without any special rights can upload
121 $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' );
122 } else {
123 $wgOut->permissionRequired( 'upload' );
124 }
125 return;
126 }
127
128 # Check blocks
129 if( $wgUser->isBlocked() ) {
130 $wgOut->blockedPage();
131 return;
132 }
133
134 if( wfReadOnly() ) {
135 $wgOut->readOnlyPage();
136 return;
137 }
138 //check token if uploading or reUploading
139 if( !$this->mTokenOk && !$this->mReUpload && ($this->mUpload && (
140 'submit' == $this->mAction || $this->mUploadClicked ) ) )
141 {
142 $this->mainUploadForm ( wfMsgExt( 'session_fail_preview', 'parseinline' ) );
143 return ;
144 }
145
146
147 if( $this->mReUpload && $this->mUpload) {
148 // User choose to cancel upload
149 if( !$this->mUpload->unsaveUploadedFile() ) {
150 return;
151 }
152 # Because it is probably checked and shouldn't be
153 $this->mIgnoreWarning = false;
154 $this->mainUploadForm();
155 } elseif( $this->mUpload && (
156 'submit' == $this->mAction ||
157 $this->mUploadClicked
158 ) ) {
159 $this->processUpload();
160 } else {
161 $this->mainUploadForm();
162 }
163
164 if( $this->mUpload )
165 $this->mUpload->cleanupTempFile();
166 }
167
168 /**
169 * Do the upload
170 * Checks are made in SpecialUpload::execute()
171 *
172 * FIXME this should really use the standard Status class (instead of associative array)
173 * FIXME would be nice if we refactored this into the upload api.
174 * (the special upload page is not the only response point that needs clean localized error msgs)
175 *
176 * @access private
177 */
178 function processUpload() {
179 global $wgOut, $wgFileExtensions, $wgLang;
180 $details = $this->internalProcessUpload();
181 switch( $details['status'] ) {
182 case UploadBase::SUCCESS:
183 $wgOut->redirect( $this->mLocalFile->getTitle()->getFullURL() );
184 break;
185
186 case UploadBase::BEFORE_PROCESSING:
187 $this->uploadError( $details['error'] );
188 break;
189 case UploadBase::LARGE_FILE_SERVER:
190 $this->mainUploadForm( wfMsgHtml( 'largefileserver' ) );
191 break;
192
193 case UploadBase::EMPTY_FILE:
194 $this->mainUploadForm( wfMsgHtml( 'emptyfile' ) );
195 break;
196
197 case UploadBase::MIN_LENGTH_PARTNAME:
198 $this->mainUploadForm( wfMsgHtml( 'minlength1' ) );
199 break;
200
201 case UploadBase::ILLEGAL_FILENAME:
202 $this->uploadError( wfMsgExt( 'illegalfilename',
203 'parseinline', $details['filtered'] ) );
204 break;
205
206 case UploadBase::PROTECTED_PAGE:
207 $wgOut->showPermissionsErrorPage( $details['permissionserrors'] );
208 break;
209
210 case UploadBase::OVERWRITE_EXISTING_FILE:
211 $this->uploadError( wfMsgExt( $details['overwrite'],
212 'parseinline' ) );
213 break;
214
215 case UploadBase::FILETYPE_MISSING:
216 $this->uploadError( wfMsgExt( 'filetype-missing', array ( 'parseinline' ) ) );
217 break;
218
219 case UploadBase::FILETYPE_BADTYPE:
220 $finalExt = $details['finalExt'];
221 $this->uploadError(
222 wfMsgExt( 'filetype-banned-type',
223 array( 'parseinline' ),
224 htmlspecialchars( $finalExt ),
225 implode(
226 wfMsgExt( 'comma-separator', array( 'escapenoentities' ) ),
227 $wgFileExtensions
228 ),
229 $wgLang->formatNum( count( $wgFileExtensions ) )
230 )
231 );
232 break;
233
234 case UploadBase::VERIFICATION_ERROR:
235 unset( $details['status'] );
236 $code = array_shift( $details['details'] );
237 $this->uploadError( wfMsgExt( $code, 'parseinline', $details['details'] ) );
238 break;
239
240 case UploadBase::UPLOAD_VERIFICATION_ERROR:
241 $error = $details['error'];
242 $this->uploadError( wfMsgExt( $error, 'parseinline' ) );
243 break;
244
245 case UploadBase::UPLOAD_WARNING:
246 unset( $details['status'] );
247 $this->uploadWarning( $details );
248 break;
249
250 case UploadBase::INTERNAL_ERROR:
251 $status = $details['internal'];
252 $this->showError( $wgOut->parse( $status->getWikiText() ) );
253 break;
254
255 default:
256 throw new MWException( __METHOD__ . ": Unknown value `{$details['status']}`" );
257 }
258 }
259
260 /**
261 * Really do the upload
262 * Checks are made in SpecialUpload::execute()
263 *
264 * @param array $resultDetails contains result-specific dict of additional values
265 *
266 * @access private
267 */
268 function internalProcessUpload() {
269 global $wgUser;
270
271 if( !wfRunHooks( 'UploadForm:BeforeProcessing', array( &$this ) ) )
272 {
273 wfDebug( "Hook 'UploadForm:BeforeProcessing' broke processing the file.\n" );
274 return array( 'status' => UploadBase::BEFORE_PROCESSING );
275 }
276
277 /**
278 * If the image is protected, non-sysop users won't be able
279 * to modify it by uploading a new revision.
280 */
281 $permErrors = $this->mUpload->verifyPermissions( $wgUser );
282 if( $permErrors !== true ) {
283 return array( 'status' => UploadBase::PROTECTED_PAGE, 'permissionserrors' => $permErrors );
284 }
285
286 // Fetch the file if required
287 $status = $this->mUpload->fetchFile();
288 if( !$status->isOK() ) {
289 return array( 'status' => UploadBase::BEFORE_PROCESSING, 'error'=> $status->getWikiText() );
290 }
291
292 // Check whether this is a sane upload
293 $result = $this->mUpload->verifyUpload();
294 if( $result['status'] != UploadBase::OK )
295 return $result;
296
297 $this->mLocalFile = $this->mUpload->getLocalFile();
298
299 if( !$this->mIgnoreWarning ) {
300 $warnings = $this->mUpload->checkWarnings();
301
302 if( count( $warnings ) ) {
303 $warnings['status'] = UploadBase::UPLOAD_WARNING;
304 return $warnings;
305 }
306 }
307
308
309 /**
310 * Try actually saving the thing...
311 * It will show an error form on failure. No it will not.
312 */
313 if( !$this->mForReUpload ) {
314 $pageText = self::getInitialPageText( $this->mComment, $this->mLicense,
315 $this->mCopyrightStatus, $this->mCopyrightSource );
316 } else {
317 $pageText = false;
318 }
319 $status = $this->mUpload->performUpload( $this->mComment, $pageText, $this->mWatchthis, $wgUser );
320
321 if ( !$status->isGood() ) {
322 return array( 'status' => UploadBase::INTERNAL_ERROR, 'internal' => $status );
323 } else {
324 // Success, redirect to description page
325 wfRunHooks( 'SpecialUploadComplete', array( &$this ) );
326 return UploadBase::SUCCESS;
327 }
328 }
329
330 /**
331 * Formats a result of UploadBase::getExistsWarning as HTML
332 * This check is static and can be done pre-upload via AJAX
333 *
334 * @param array $exists The result of UploadBase::getExistsWarning
335 * @return string Empty string if there is no warning or an HTML fragment
336 * consisting of one or more <li> elements if there is a warning.
337 */
338 public static function getExistsWarning( $exists ) {
339 global $wgUser, $wgContLang;
340
341 if ( !$exists )
342 return '';
343
344 $file = $exists['file'];
345 $filename = $file->getTitle()->getPrefixedText();
346 $warning = array();
347
348 $sk = $wgUser->getSkin();
349
350 if( $exists['warning'] == 'exists' ) {
351 // Exact match
352 $warning[] = '<li>' . wfMsgExt( 'fileexists', 'parseinline', $filename ) . '</li>';
353 } elseif( $exists['warning'] == 'page-exists' ) {
354 // Page exists but file does not
355 $warning[] = '<li>' . wfMsgExt( 'filepageexists', 'parseinline', $filename ) . '</li>';
356 } elseif ( $exists['warning'] == 'exists-normalized' ) {
357 $warning[] = '<li>' . wfMsgExt( 'fileexists-extension', 'parseinline', $filename,
358 $exists['normalizedFile']->getTitle()->getPrefixedText() ) . '</li>';
359 } elseif ( $exists['warning'] == 'thumb' ) {
360 // Swapped argument order compared with other messages for backwards compatibility
361 $warning[] = '<li>' . wfMsgExt( 'fileexists-thumbnail-yes', 'parseinline',
362 $exists['thumbFile']->getTitle()->getPrefixedText(), $filename ) . '</li>';
363 } elseif ( $exists['warning'] == 'thumb-name' ) {
364 # Image w/o '180px-' does not exists, but we do not like these filenames
365 $name = $file->getName();
366 $badPart = substr( $name, 0, strpos( $name, '-' ) + 1 );
367 $warning[] = '<li>' . wfMsgExt( 'file-thumbnail-no', 'parseinline', $badPart ) . '</li>';
368 } elseif ( $exists['warning'] == 'bad-prefix' ) {
369 $warning[] = '<li>' . wfMsgExt( 'filename-bad-prefix', 'parseinline', $exists['prefix'] ) . '</li>';
370 } elseif ( $exists['warning'] == 'was-deleted' ) {
371 # If the file existed before and was deleted, warn the user of this
372 $ltitle = SpecialPage::getTitleFor( 'Log' );
373 $llink = $sk->linkKnown(
374 $ltitle,
375 wfMsgHtml( 'deletionlog' ),
376 array(),
377 array(
378 'type' => 'delete',
379 'page' => $filename
380 )
381 );
382 $warning[] = '<li>' . wfMsgWikiHtml( 'filewasdeleted', $llink ) . '</li>';
383 }
384
385 return implode( "\n", $warning );
386 }
387
388 /**
389 * Get a list of warnings
390 *
391 * @param string local filename, e.g. 'file exists', 'non-descriptive filename'
392 * @return array list of warning messages
393 */
394 static function ajaxGetExistsWarning( $filename ) {
395 $file = wfFindFile( $filename );
396 if( !$file ) {
397 // Force local file so we have an object to do further checks against
398 // if there isn't an exact match...
399 $file = wfLocalFile( $filename );
400 }
401 $s = '&nbsp;';
402 if ( $file ) {
403 $exists = UploadBase::getExistsWarning( $file );
404 $warning = self::getExistsWarning( $exists );
405 // FIXME: We probably also want the prefix blacklist and the wasdeleted check here
406 if ( $warning !== '' ) {
407 $s = "<ul>$warning</ul>";
408 }
409 }
410 return $s;
411 }
412
413 /**
414 * Render a preview of a given license for the AJAX preview on upload
415 *
416 * @param string $license
417 * @return string
418 */
419 public static function ajaxGetLicensePreview( $license ) {
420 global $wgParser, $wgUser;
421 $text = '{{' . $license . '}}';
422 $title = Title::makeTitle( NS_FILE, 'Sample.jpg' );
423 $options = ParserOptions::newFromUser( $wgUser );
424
425 // Expand subst: first, then live templates...
426 $text = $wgParser->preSaveTransform( $text, $title, $wgUser, $options );
427 $output = $wgParser->parse( $text, $title, $options );
428
429 return $output->getText();
430 }
431
432 /**
433 * Construct the human readable warning message from an array of duplicate files
434 */
435 public static function getDupeWarning( $dupes ) {
436 if( $dupes ) {
437 global $wgOut;
438 $msg = "<gallery>";
439 foreach( $dupes as $file ) {
440 $title = $file->getTitle();
441 $msg .= $title->getPrefixedText() .
442 "|" . $title->getText() . "\n";
443 }
444 $msg .= "</gallery>";
445 return "<li>" .
446 wfMsgExt( "file-exists-duplicate", array( "parse" ), count( $dupes ) ) .
447 $wgOut->parse( $msg ) .
448 "</li>\n";
449 } else {
450 return '';
451 }
452 }
453
454
455 /**
456 * Remove a temporarily kept file stashed by saveTempUploadedFile().
457 * @access private
458 * @return success
459 */
460 function unsaveUploadedFile() {
461 global $wgOut;
462 $success = $this->mUpload->unsaveUploadedFile();
463 if ( ! $success ) {
464 $wgOut->showFileDeleteError( $this->mUpload->getTempPath() );
465 return false;
466 } else {
467 return true;
468 }
469 }
470
471 /* Interface code starts below this line *
472 * -------------------------------------------------------------- */
473
474
475 /**
476 * @param string $error as HTML
477 * @access private
478 */
479 function uploadError( $error ) {
480 global $wgOut;
481 $wgOut->addHTML( '<h2>' . wfMsgHtml( 'uploadwarning' ) . "</h2>\n" );
482 $wgOut->addHTML( '<span class="error">' . $error . '</span>' );
483 }
484
485 /**
486 * There's something wrong with this file, not enough to reject it
487 * totally but we require manual intervention to save it for real.
488 * Stash it away, then present a form asking to confirm or cancel.
489 *
490 * @param string $warning as HTML
491 * @access private
492 */
493 function uploadWarning( $warnings ) {
494 global $wgOut, $wgUser;
495 global $wgUseCopyrightUpload;
496
497 $this->mSessionKey = $this->mUpload->stashSession();
498
499 if( $this->mSessionKey === false ) {
500 # Couldn't save file; an error has been displayed so let's go.
501 return;
502 }
503
504 $sk = $wgUser->getSkin();
505
506 $wgOut->addHTML( '<h2>' . wfMsgHtml( 'uploadwarning' ) . "</h2>\n" );
507 $wgOut->addHTML( '<ul class="warning">' );
508 foreach( $warnings as $warning => $args ) {
509 $msg = null;
510 if( $warning == 'exists' ) {
511
512 //we should not have produced this warning if the user already acknowledged the destination warning
513 //at any rate I don't see why we should hid this warning if mDestWarningAck has been checked
514 //(it produces an empty warning page when no other warnings are fired)
515 //if ( !$this->mDestWarningAck )
516 $msg = self::getExistsWarning( $args );
517
518 } elseif( $warning == 'duplicate' ) {
519 $msg = $this->getDupeWarning( $args );
520 } elseif( $warning == 'duplicate-archive' ) {
521 $titleText = Title::makeTitle( NS_FILE, $args )->getPrefixedText();
522 $msg = Xml::tags( 'li', null, wfMsgExt( 'file-deleted-duplicate', array( 'parseinline' ), array( $titleText ) ) );
523 } else {
524 if( is_bool( $args ) )
525 $args = array();
526 elseif( !is_array( $args ) )
527 $args = array( $args );
528 $msg = "\t<li>" . wfMsgExt( $warning, 'parseinline', $args ) . "</li>\n";
529 }
530 if( $msg )
531 $wgOut->addHTML( $msg );
532 }
533
534 $titleObj = SpecialPage::getTitleFor( 'Upload' );
535
536 if ( $wgUseCopyrightUpload ) {
537 $copyright = Xml::hidden( 'wpUploadCopyStatus', $this->mCopyrightStatus ) . "\n" .
538 Xml::hidden( 'wpUploadSource', $this->mCopyrightSource ) . "\n";
539 } else {
540 $copyright = '';
541 }
542 $wgOut->addHTML(
543 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL( 'action=submit' ),
544 'enctype' => 'multipart/form-data', 'id' => 'uploadwarning' ) ) . "\n" .
545 Xml::hidden('wpEditToken', $wgUser->editToken(), array("id" => 'wpEditToken')) .
546 Xml::hidden( 'wpIgnoreWarning', '1' ) . "\n" .
547 Xml::hidden( 'wpSourceType', 'stash' ) . "\n" .
548 Xml::hidden( 'wpSessionKey', $this->mSessionKey ) . "\n" .
549 Xml::hidden( 'wpUploadDescription', $this->mComment ) . "\n" .
550 Xml::hidden( 'wpLicense', $this->mLicense ) . "\n" .
551 Xml::hidden( 'wpDestFile', $this->mDesiredDestName ) . "\n" .
552 Xml::hidden( 'wpWatchthis', $this->mWatchthis ) . "\n" .
553 "{$copyright}<br />" .
554 Xml::submitButton( wfMsg( 'ignorewarning' ), array ( 'name' => 'wpUpload', 'id' => 'wpUpload', 'checked' => 'checked' ) ) . ' ' .
555 Xml::submitButton( wfMsg( 'reuploaddesc' ), array ( 'name' => 'wpReUpload', 'id' => 'wpReUpload' ) ) .
556 Xml::closeElement( 'form' ) . "\n"
557 );
558 }
559
560 /**
561 * Displays the main upload form, optionally with a highlighted
562 * error message up at the top.
563 *
564 * @param string $msg as HTML
565 * @access private
566 */
567 function mainUploadForm( $msg='' ) {
568 global $wgOut, $wgUser, $wgLang, $wgMaxUploadSize, $wgEnableFirefogg;
569 global $wgUseCopyrightUpload, $wgUseAjax, $wgAjaxUploadDestCheck, $wgAjaxLicensePreview;
570 global $wgRequest;
571 global $wgStylePath, $wgStyleVersion;
572 global $wgEnableJS2system;
573
574 $useAjaxDestCheck = $wgUseAjax && $wgAjaxUploadDestCheck;
575 $useAjaxLicensePreview = $wgUseAjax && $wgAjaxLicensePreview;
576
577 $adc = wfBoolToStr( $useAjaxDestCheck );
578 $alp = wfBoolToStr( $useAjaxLicensePreview );
579 $uef = wfBoolToStr( $wgEnableFirefogg );
580 $autofill = wfBoolToStr( $this->mDesiredDestName == '' );
581
582
583 $wgOut->addScript( "<script type=\"text/javascript\">
584 wgAjaxUploadDestCheck = {$adc};
585 wgAjaxLicensePreview = {$alp};
586 wgEnableFirefogg = {$uef};
587 wgUploadAutoFill = {$autofill};
588 </script>" );
589
590 if( $wgEnableJS2system ) {
591 //js2version of upload page:
592 $wgOut->addScriptClass( 'uploadPage' );
593 }else{
594 //legacy upload code:
595 $wgOut->addScriptFile( 'upload.js' );
596 $wgOut->addScriptFile( 'edit.js' ); // For <charinsert> support
597 }
598
599 if( !wfRunHooks( 'UploadForm:initial', array( &$this ) ) )
600 {
601 wfDebug( "Hook 'UploadForm:initial' broke output of the upload form" );
602 return false;
603 }
604
605 if( $this->mDesiredDestName != '' ) {
606 $title = Title::makeTitleSafe( NS_FILE, $this->mDesiredDestName );
607 // Show a subtitle link to deleted revisions (to sysops et al only)
608 if( $title instanceof Title && ( $count = $title->isDeleted() ) > 0 && $wgUser->isAllowed( 'deletedhistory' ) ) {
609 $link = wfMsgExt(
610 $wgUser->isAllowed( 'delete' ) ? 'thisisdeleted' : 'viewdeleted',
611 array( 'parse', 'replaceafter' ),
612 $wgUser->getSkin()->linkKnown(
613 SpecialPage::getTitleFor( 'Undelete', $title->getPrefixedText() ),
614 wfMsgExt( 'restorelink', array( 'parsemag', 'escape' ), $count )
615 )
616 );
617 $wgOut->addHTML( "<div id=\"contentSub2\">{$link}</div>" );
618 }
619
620 // Show the relevant lines from deletion log (for still deleted files only)
621 if( $title instanceof Title && $title->isDeletedQuick() && !$title->exists() ) {
622 $this->showDeletionLog( $wgOut, $title->getPrefixedText() );
623 }
624 }
625
626 $cols = intval($wgUser->getOption( 'cols' ));
627
628 if( $wgUser->getOption( 'editwidth' ) ) {
629 $width = " style=\"width:100%\"";
630 } else {
631 $width = '';
632 }
633
634 if ( '' != $msg ) {
635 $sub = wfMsgHtml( 'uploaderror' );
636 $wgOut->addHTML( "<h2>{$sub}</h2>\n" .
637 "<span class='error'>{$msg}</span>\n" );
638 }
639
640 $wgOut->addHTML( '<div id="uploadtext">' );
641 $wgOut->addWikiMsg( 'uploadtext', $this->mDesiredDestName );
642 $wgOut->addHTML( "</div>\n" );
643
644 # Print a list of allowed file extensions, if so configured. We ignore
645 # MIME type here, it's incomprehensible to most people and too long.
646 global $wgCheckFileExtensions, $wgStrictFileExtensions,
647 $wgFileExtensions, $wgFileBlacklist;
648
649 $allowedExtensions = '';
650 if( $wgCheckFileExtensions ) {
651 if( $wgStrictFileExtensions ) {
652 # Everything not permitted is banned
653 $extensionsList =
654 '<div id="mw-upload-permitted">' .
655 wfMsgWikiHtml( 'upload-permitted', $wgLang->commaList( $wgFileExtensions ) ) .
656 "</div>\n";
657 } else {
658 # We have to list both preferred and prohibited
659 $extensionsList =
660 '<div id="mw-upload-preferred">' .
661 wfMsgWikiHtml( 'upload-preferred', $wgLang->commaList( $wgFileExtensions ) ) .
662 "</div>\n" .
663 '<div id="mw-upload-prohibited">' .
664 wfMsgWikiHtml( 'upload-prohibited', $wgLang->commaList( $wgFileExtensions ) ) .
665 "</div>\n";
666 }
667 } else {
668 # Everything is permitted.
669 $extensionsList = '';
670 }
671
672 # Get the maximum file size from php.ini as $wgMaxUploadSize works for uploads from URL via CURL only
673 # See http://www.php.net/manual/en/ini.core.php#ini.upload-max-filesize for possible values of upload_max_filesize
674 $val = wfShorthandToInteger( ini_get( 'upload_max_filesize' ) );
675 $maxUploadSize = '<div id="mw-upload-maxfilesize">' .
676 wfMsgExt( 'upload-maxfilesize', array( 'parseinline', 'escapenoentities' ),
677 $wgLang->formatSize( $val ) ) .
678 "</div>\n";
679 //add a hidden filed for upload by url (uses the $wgMaxUploadSize var)
680 if( UploadFromUrl::isEnabled() ) {
681 $maxUploadSize.='<div id="mw-upload-maxfilesize-url" style="display:none">' .
682 wfMsgExt( 'upload-maxfilesize', array( 'parseinline', 'escapenoentities' ),
683 $wgLang->formatSize( $wgMaxUploadSize ) ) .
684 "</div>\n";
685 }
686
687 $sourcefilename = wfMsgExt( 'sourcefilename', array( 'parseinline', 'escapenoentities' ) );
688 $destfilename = wfMsgExt( 'destfilename', array( 'parseinline', 'escapenoentities' ) );
689
690 $msg = ( $this->mForReUpload ) ? 'filereuploadsummary' : 'fileuploadsummary';
691 $summary = wfMsgExt( $msg, 'parseinline' );
692
693 $licenses = new Licenses();
694 $license = wfMsgExt( 'license', array( 'parseinline' ) );
695 $nolicense = wfMsgHtml( 'nolicense' );
696 $licenseshtml = $licenses->getHtml();
697
698 $ulb = wfMsgHtml( 'uploadbtn' );
699
700
701 $titleObj = SpecialPage::getTitleFor( 'Upload' );
702
703 $encDestName = htmlspecialchars( $this->mDesiredDestName );
704
705 $watchChecked = $this->watchCheck() ? 'checked="checked"' : '';
706 # Re-uploads should not need "file exist already" warnings
707 $warningChecked = ($this->mIgnoreWarning || $this->mForReUpload) ? 'checked="checked"' : '';
708
709 // Prepare form for upload or upload/copy
710 //javascript moved from inline calls to setup:
711 if( UploadFromUrl::isEnabled() && $wgUser->isAllowed( 'upload_by_url' ) ) {
712 if( $wgEnableJS2system ) {
713 $filename_form =
714 Xml::input( 'wpSourceType', false, 'file',
715 array( 'id' => 'wpSourceTypeFile', 'type' => 'radio', 'checked' => 'checked' ) ) .
716 Xml::input( 'wpUploadFile', 60, false,
717 array( 'id' => 'wpUploadFile', 'type' => 'file', 'tabindex' => '1' ) ) .
718 wfMsgHTML( 'upload_source_file' ) . "<br/>" .
719 Xml::input( 'wpSourceType', false, 'Url',
720 array( 'id' => 'wpSourceTypeURL', 'type' => 'radio' ) ) .
721 Xml::input( 'wpUploadFileURL', 60, false,
722 array( 'id' => 'wpUploadFileURL', 'type' => 'text', 'tabindex' => '1' ) ) .
723 wfMsgHtml( 'upload_source_url' ) ;
724 } else {
725 //@@todo deprecate (not needed once $wgEnableJS2system is turned on)
726 $filename_form =
727 "<input type='radio' id='wpSourceTypeFile' name='wpSourceType' value='file' " .
728 "onchange='toggle_element_activation(\"wpUploadFileURL\",\"wpUploadFile\")' checked='checked' />" .
729 "<input tabindex='1' type='file' name='wpUploadFile' id='wpUploadFile' " .
730 " onfocus='" .
731 "toggle_element_activation(\"wpUploadFileURL\",\"wpUploadFile\");" .
732 "toggle_element_check(\"wpSourceTypeFile\",\"wpSourceTypeURL\")' " .
733 "onchange='fillDestFilename(\"wpUploadFile\")' size='60' />" .
734 wfMsgHTML( 'upload_source_file' ) . "<br/>" .
735 "<input type='radio' id='wpSourceTypeURL' name='wpSourceType' value='Url' " .
736 "onchange='toggle_element_activation(\"wpUploadFile\",\"wpUploadFileURL\")' />" .
737 "<input tabindex='1' type='text' name='wpUploadFileURL' id='wpUploadFileURL' " .
738 "onfocus='" .
739 "toggle_element_activation(\"wpUploadFile\",\"wpUploadFileURL\");" .
740 "toggle_element_check(\"wpSourceTypeURL\",\"wpSourceTypeFile\")' " .
741 "onchange='fillDestFilename(\"wpUploadFileURL\")' size='60' />" .
742 wfMsgHtml( 'upload_source_url' ) ;
743
744 }
745 } else {
746 if( $wgEnableJS2system ) {
747 $filename_form =
748 Xml::input( 'wpUploadFile', 60, false,
749 array( 'id' => 'wpUploadFile', 'type' => 'file', 'tabindex' => '1' ) ) .
750 Xml::hidden( 'wpSourceType', 'file');
751 } else {
752 $filename_form =
753 "<input tabindex='1' type='file' name='wpUploadFile' id='wpUploadFile' size='60' ".
754 "onchange='fillDestFilename(\"wpUploadFile\")' />" .
755 "<input type='hidden' name='wpSourceType' value='file' />" ;
756 }
757 }
758 $warningRow = '';
759 $destOnkeyup = '';
760 if( $wgEnableJS2system ) {
761 $warningRow = "<tr><td colspan='2' id='wpDestFile-warning'>&nbsp;</td></tr>";
762 } else {
763 if ( $useAjaxDestCheck ) {
764 $warningRow = "<tr><td colspan='2' id='wpDestFile-warning'>&nbsp;</td></tr>";
765 $destOnkeyup = 'onchange=\'wgUploadWarningObj.checkNow(this.value);\'';
766 }
767 }
768 # Uploading a new version? If so, the name is fixed.
769 $on = $this->mForReUpload ? "readonly='readonly'" : "";
770
771 $encComment = htmlspecialchars( $this->mComment );
772
773 //add the wpEditToken
774 $wgOut->addHTML(
775 Xml::openElement( 'form',
776 array( 'method' => 'post', 'action' => $titleObj->getLocalURL( 'action=submit' ),
777 'enctype' => 'multipart/form-data', 'id' => 'mw-upload-form' ) ) .
778 Xml::hidden( 'wpEditToken', $wgUser->editToken(), array( 'id' => 'wpEditToken' ) ) .
779 Xml::openElement( 'fieldset' ) .
780 Xml::element( 'legend', null, wfMsg( 'upload' ) ) .
781 Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-upload-table' ) ) .
782 "<tr>
783 {$this->uploadFormTextTop}
784 <td class='mw-label'>
785 <label for='wpUploadFile'>{$sourcefilename}</label>
786 </td>
787 <td class='mw-input'>
788 {$filename_form}
789 </td>
790 </tr>
791 <tr>
792 <td></td>
793 <td>
794 {$maxUploadSize}
795 {$extensionsList}
796 </td>
797 </tr>
798 <tr>
799 <td class='mw-label'>
800 <label for='wpDestFile'>{$destfilename}</label>
801 </td>
802 <td class='mw-input'>"
803 );
804 if( $this->mForReUpload ) {
805 $wgOut->addHTML(
806 Xml::hidden( 'wpDestFile', $this->mDesiredDestName, array('id'=>'wpDestFile','tabindex'=>2) ) .
807 "<tt>" .
808 $encDestName .
809 "</tt>"
810 );
811 }
812 else {
813 $wgOut->addHTML(
814 "<input tabindex='2' type='text' name='wpDestFile' id='wpDestFile' size='60'
815 value=\"{$encDestName}\" $destOnkeyup />"
816 );
817 }
818
819
820 $wgOut->addHTML(
821 "</td>
822 </tr>
823 <tr>
824 <td class='mw-label'>
825 <label for='wpUploadDescription'>{$summary}</label>
826 </td>
827 <td class='mw-input'>
828 <textarea tabindex='3' name='wpUploadDescription' id='wpUploadDescription' rows='6'
829 cols='{$cols}'{$width}>$encComment</textarea>
830 {$this->uploadFormTextAfterSummary}
831 </td>
832 </tr>
833 <tr>"
834 );
835
836 # Re-uploads should not need license info
837 if ( !$this->mForReUpload && $licenseshtml != '' ) {
838 global $wgStylePath;
839 $wgOut->addHTML( "
840 <td class='mw-label'>
841 <label for='wpLicense'>$license</label>
842 </td>
843 <td class='mw-input'>
844 <select name='wpLicense' id='wpLicense' tabindex='4'
845 onchange='licenseSelectorCheck()'>
846 <option value=''>$nolicense</option>
847 $licenseshtml
848 </select>
849 </td>
850 </tr>
851 <tr>"
852 );
853 if( $useAjaxLicensePreview ) {
854 $wgOut->addHTML( "
855 <td></td>
856 <td id=\"mw-license-preview\"></td>
857 </tr>
858 <tr>"
859 );
860 }
861 }
862
863 if ( !$this->mForReUpload && $wgUseCopyrightUpload ) {
864 $filestatus = wfMsgExt( 'filestatus', 'escapenoentities' );
865 $copystatus = htmlspecialchars( $this->mCopyrightStatus );
866 $filesource = wfMsgExt( 'filesource', 'escapenoentities' );
867 $uploadsource = htmlspecialchars( $this->mCopyrightSource );
868
869 $wgOut->addHTML( "
870 <td class='mw-label' style='white-space: nowrap;'>
871 <label for='wpUploadCopyStatus'>$filestatus</label></td>
872 <td class='mw-input'>
873 <input tabindex='5' type='text' name='wpUploadCopyStatus' id='wpUploadCopyStatus'
874 value=\"$copystatus\" size='60' />
875 </td>
876 </tr>
877 <tr>
878 <td class='mw-label'>
879 <label for='wpUploadCopyStatus'>$filesource</label>
880 </td>
881 <td class='mw-input'>
882 <input tabindex='6' type='text' name='wpUploadSource' id='wpUploadCopyStatus'
883 value=\"$uploadsource\" size='60' />
884 </td>
885 </tr>
886 <tr>"
887 );
888 }
889
890 $wgOut->addHTML( "
891 <td></td>
892 <td>
893 <input tabindex='7' type='checkbox' name='wpWatchthis' id='wpWatchthis' $watchChecked value='true' />
894 <label for='wpWatchthis'>" . wfMsgHtml( 'watchthisupload' ) . "</label>
895 <input tabindex='8' type='checkbox' name='wpIgnoreWarning' id='wpIgnoreWarning' value='true' $warningChecked />
896 <label for='wpIgnoreWarning'>" . wfMsgHtml( 'ignorewarnings' ) . "</label>
897 </td>
898 </tr>
899 $warningRow
900 <tr>
901 <td></td>
902 <td class='mw-input'>
903 <input tabindex='9' type='submit' name='wpUpload' value=\"{$ulb}\"" .
904 $wgUser->getSkin()->tooltipAndAccesskey( 'upload' ) . " />
905 </td>
906 </tr>
907 <tr>
908 <td></td>
909 <td class='mw-input'>"
910 );
911 $wgOut->addHTML( '<div class="mw-editTools">' );
912 $wgOut->addWikiMsgArray( 'edittools', array(), array( 'content' ) );
913 $wgOut->addHTML( '</div>' );
914 $wgOut->addHTML( "
915 </td>
916 </tr>" .
917 Xml::closeElement( 'table' ) .
918 Xml::hidden( 'wpDestFileWarningAck', '', array( 'id' => 'wpDestFileWarningAck' ) ) .
919 Xml::hidden( 'wpForReUpload', $this->mForReUpload, array( 'id' => 'wpForReUpload' ) ) .
920 Xml::closeElement( 'fieldset' ) .
921 Xml::closeElement( 'form' )
922 );
923 $uploadfooter = wfMsgNoTrans( 'uploadfooter' );
924 if( $uploadfooter != '-' && !wfEmptyMsg( 'uploadfooter', $uploadfooter ) ){
925 $wgOut->addWikiText( '<div id="mw-upload-footer-message">' . $uploadfooter . '</div>' );
926 }
927 }
928
929 /* -------------------------------------------------------------- */
930
931 /**
932 * See if we should check the 'watch this page' checkbox on the form
933 * based on the user's preferences and whether we're being asked
934 * to create a new file or update an existing one.
935 *
936 * In the case where 'watch edits' is off but 'watch creations' is on,
937 * we'll leave the box unchecked.
938 *
939 * Note that the page target can be changed *on the form*, so our check
940 * state can get out of sync.
941 */
942 function watchCheck() {
943 global $wgUser;
944 if( $wgUser->getOption( 'watchdefault' ) ) {
945 // Watch all edits!
946 return true;
947 }
948
949 $local = wfLocalFile( $this->mDesiredDestName );
950 if( $local && $local->exists() ) {
951 // We're uploading a new version of an existing file.
952 // No creation, so don't watch it if we're not already.
953 return $local->getTitle()->userIsWatching();
954 } else {
955 // New page should get watched if that's our option.
956 return $wgUser->getOption( 'watchcreations' );
957 }
958 }
959
960 /**
961 * Check if a user is the last uploader
962 *
963 * @param User $user
964 * @param string $img, image name
965 * @return bool
966 * @deprecated Use UploadBase::userCanReUpload
967 */
968 public static function userCanReUpload( User $user, $img ) {
969 wfDeprecated( __METHOD__ );
970
971 if( $user->isAllowed( 'reupload' ) )
972 return true; // non-conditional
973 if( !$user->isAllowed( 'reupload-own' ) )
974 return false;
975
976 $dbr = wfGetDB( DB_SLAVE );
977 $row = $dbr->selectRow('image',
978 /* SELECT */ 'img_user',
979 /* WHERE */ array( 'img_name' => $img )
980 );
981 if ( !$row )
982 return false;
983
984 return $user->getId() == $row->img_user;
985 }
986
987 /**
988 * Display an error with a wikitext description
989 */
990 function showError( $description ) {
991 global $wgOut;
992 $wgOut->setPageTitle( wfMsg( "upload-file-error" ) );
993 $wgOut->setRobotPolicy( "noindex,nofollow" );
994 $wgOut->setArticleRelated( false );
995 $wgOut->enableClientCache( false );
996 $wgOut->addWikiText( $description );
997 }
998
999 /**
1000 * Get the initial image page text based on a comment and optional file status information
1001 */
1002 static function getInitialPageText( $comment = '', $license = '', $copyStatus = '', $source = '' ) {
1003 global $wgUseCopyrightUpload;
1004 if ( $wgUseCopyrightUpload ) {
1005 $licensetxt = '';
1006 if ( $license != '' ) {
1007 $licensetxt = '== ' . wfMsgForContent( 'license-header' ) . " ==\n" . '{{' . $license . '}}' . "\n";
1008 }
1009 $pageText = '== ' . wfMsgForContent ( 'filedesc' ) . " ==\n" . $comment . "\n" .
1010 '== ' . wfMsgForContent ( 'filestatus' ) . " ==\n" . $copyStatus . "\n" .
1011 "$licensetxt" .
1012 '== ' . wfMsgForContent ( 'filesource' ) . " ==\n" . $source ;
1013 } else {
1014 if ( $license != '' ) {
1015 $filedesc = $comment == '' ? '' : '== ' . wfMsgForContent ( 'filedesc' ) . " ==\n" . $comment . "\n";
1016 $pageText = $filedesc .
1017 '== ' . wfMsgForContent ( 'license-header' ) . " ==\n" . '{{' . $license . '}}' . "\n";
1018 } else {
1019 $pageText = $comment;
1020 }
1021 }
1022 return $pageText;
1023 }
1024
1025 /**
1026 * If there are rows in the deletion log for this file, show them,
1027 * along with a nice little note for the user
1028 *
1029 * @param OutputPage $out
1030 * @param string filename
1031 */
1032 private function showDeletionLog( $out, $filename ) {
1033 global $wgUser;
1034 $loglist = new LogEventsList( $wgUser->getSkin(), $out );
1035 $pager = new LogPager( $loglist, 'delete', false, $filename );
1036 if( $pager->getNumRows() > 0 ) {
1037 $out->addHTML( '<div class="mw-warning-with-logexcerpt">' );
1038 $out->addWikiMsg( 'upload-wasdeleted' );
1039 $out->addHTML(
1040 $loglist->beginLogEventsList() .
1041 $pager->getBody() .
1042 $loglist->endLogEventsList()
1043 );
1044 $out->addHTML( '</div>' );
1045 }
1046 }
1047 }