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