0ded578b8446a62522a79a2e1fdb79cbbe1e7eda
[lhc/web/wiklou.git] / includes / ImagePage.php
1 <?php
2 /**
3 */
4
5 /**
6 *
7 */
8 if( !defined( 'MEDIAWIKI' ) )
9 die( 1 );
10
11 /**
12 * Special handling for image description pages
13 *
14 * @addtogroup Media
15 */
16 class ImagePage extends Article {
17
18 /* private */ var $img; // Image object this page is shown for
19 var $mExtraDescription = false;
20
21 function __construct( $title ) {
22 parent::__construct( $title );
23 $this->img = wfFindFile( $this->mTitle );
24 if ( !$this->img ) {
25 $this->img = wfLocalFile( $this->mTitle );
26 }
27 }
28
29 /**
30 * Handler for action=render
31 * Include body text only; none of the image extras
32 */
33 function render() {
34 global $wgOut;
35 $wgOut->setArticleBodyOnly( true );
36 $wgOut->addSecondaryWikitext( $this->getContent() );
37 }
38
39 function view() {
40 global $wgOut, $wgShowEXIF, $wgRequest, $wgUser;
41
42 $diff = $wgRequest->getVal( 'diff' );
43 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
44
45 if ( $this->mTitle->getNamespace() != NS_IMAGE || ( isset( $diff ) && $diffOnly ) )
46 return Article::view();
47
48 if ($wgShowEXIF && $this->img->exists()) {
49 $exif = $this->img->getExifData();
50 $showmeta = count($exif) ? true : false;
51 } else {
52 $exif = false;
53 $showmeta = false;
54 }
55
56 if ($this->img->exists())
57 $wgOut->addHTML($this->showTOC($showmeta));
58
59 $this->openShowImage();
60
61 # No need to display noarticletext, we use our own message, output in openShowImage()
62 if ( $this->getID() ) {
63 Article::view();
64 } else {
65 # Just need to set the right headers
66 $wgOut->setArticleFlag( true );
67 $wgOut->setRobotpolicy( 'index,follow' );
68 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
69 $this->viewUpdates();
70 }
71
72 # Show shared description, if needed
73 if ( $this->mExtraDescription ) {
74 $fol = wfMsg( 'shareddescriptionfollows' );
75 if( $fol != '-' && !wfEmptyMsg( 'shareddescriptionfollows', $fol ) ) {
76 $wgOut->addWikiText( $fol );
77 }
78 $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription . '</div>' );
79 }
80
81 $this->closeShowImage();
82 $this->imageHistory();
83 $this->imageLinks();
84
85 if ( $exif ) {
86 global $wgStylePath, $wgStyleVersion;
87 $expand = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-expand' ) ) );
88 $collapse = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-collapse' ) ) );
89 $wgOut->addHTML( Xml::element( 'h2', array( 'id' => 'metadata' ), wfMsg( 'metadata' ) ). "\n" );
90 $wgOut->addWikiText( $this->makeMetadataTable( $exif ) );
91 $wgOut->addHTML(
92 "<script type=\"text/javascript\" src=\"$wgStylePath/common/metadata.js?$wgStyleVersion\"></script>\n" .
93 "<script type=\"text/javascript\">attachMetadataToggle('mw_metadata', '$expand', '$collapse');</script>\n" );
94 }
95 }
96
97 /**
98 * Create the TOC
99 *
100 * @access private
101 *
102 * @param bool $metadata Whether or not to show the metadata link
103 * @return string
104 */
105 function showTOC( $metadata ) {
106 global $wgLang;
107 $r = '<ul id="filetoc">
108 <li><a href="#file">' . $wgLang->getNsText( NS_IMAGE ) . '</a></li>
109 <li><a href="#filehistory">' . wfMsgHtml( 'imghistory' ) . '</a></li>
110 <li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>' .
111 ($metadata ? ' <li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>' : '') . '
112 </ul>';
113 return $r;
114 }
115
116 /**
117 * Make a table with metadata to be shown in the output page.
118 *
119 * @access private
120 *
121 * @param array $exif The array containing the EXIF data
122 * @return string
123 */
124 function makeMetadataTable( $exif ) {
125 $r = wfMsg( 'metadata-help' ) . "\n\n";
126 $r .= "{| id=mw_metadata class=mw_metadata\n";
127 $visibleFields = $this->visibleMetadataFields();
128 foreach( $exif as $k => $v ) {
129 $tag = strtolower( $k );
130 $msg = wfMsg( "exif-$tag" );
131 $class = "exif-$tag";
132 if( !in_array( $tag, $visibleFields ) ) {
133 $class .= ' collapsable';
134 }
135 $r .= "|- class=\"$class\"\n";
136 $r .= "!| $msg\n";
137 $r .= "|| $v\n";
138 }
139 $r .= '|}';
140 return $r;
141 }
142
143 /**
144 * Get a list of EXIF metadata items which should be displayed when
145 * the metadata table is collapsed.
146 *
147 * @return array of strings
148 * @access private
149 */
150 function visibleMetadataFields() {
151 $fields = array();
152 $lines = explode( "\n", wfMsgForContent( 'metadata-fields' ) );
153 foreach( $lines as $line ) {
154 $matches = array();
155 if( preg_match( '/^\\*\s*(.*?)\s*$/', $line, $matches ) ) {
156 $fields[] = $matches[1];
157 }
158 }
159 return $fields;
160 }
161
162 /**
163 * Overloading Article's getContent method.
164 *
165 * Omit noarticletext if sharedupload; text will be fetched from the
166 * shared upload server if possible.
167 */
168 function getContent() {
169 if( $this->img && !$this->img->isLocal() && 0 == $this->getID() ) {
170 return '';
171 }
172 return Article::getContent();
173 }
174
175 function openShowImage() {
176 global $wgOut, $wgUser, $wgImageLimits, $wgRequest, $wgLang;
177
178 $full_url = $this->img->getURL();
179 $linkAttribs = false;
180 $sizeSel = intval( $wgUser->getOption( 'imagesize') );
181 if( !isset( $wgImageLimits[$sizeSel] ) ) {
182 $sizeSel = User::getDefaultOption( 'imagesize' );
183
184 // The user offset might still be incorrect, specially if
185 // $wgImageLimits got changed (see bug #8858).
186 if( !isset( $wgImageLimits[$sizeSel] ) ) {
187 // Default to the first offset in $wgImageLimits
188 $sizeSel = 0;
189 }
190 }
191 $max = $wgImageLimits[$sizeSel];
192 $maxWidth = $max[0];
193 $maxHeight = $max[1];
194 $sk = $wgUser->getSkin();
195
196 if ( $this->img->exists() ) {
197 # image
198 $page = $wgRequest->getIntOrNull( 'page' );
199 if ( is_null( $page ) ) {
200 $params = array();
201 $page = 1;
202 } else {
203 $params = array( 'page' => $page );
204 }
205 $width_orig = $this->img->getWidth();
206 $width = $width_orig;
207 $height_orig = $this->img->getHeight();
208 $height = $height_orig;
209 $mime = $this->img->getMimeType();
210 $showLink = false;
211 $linkAttribs = array( 'href' => $full_url );
212
213 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this , &$wgOut ) ) ;
214
215 if ( $this->img->allowInlineDisplay() and $width and $height) {
216 # image
217
218 # "Download high res version" link below the image
219 $msgsize = wfMsgHtml('file-info-size', $width_orig, $height_orig, $sk->formatSize( $this->img->getSize() ), $mime );
220 # We'll show a thumbnail of this image
221 if ( $width > $maxWidth || $height > $maxHeight ) {
222 # Calculate the thumbnail size.
223 # First case, the limiting factor is the width, not the height.
224 if ( $width / $height >= $maxWidth / $maxHeight ) {
225 $height = round( $height * $maxWidth / $width);
226 $width = $maxWidth;
227 # Note that $height <= $maxHeight now.
228 } else {
229 $newwidth = floor( $width * $maxHeight / $height);
230 $height = round( $height * $newwidth / $width );
231 $width = $newwidth;
232 # Note that $height <= $maxHeight now, but might not be identical
233 # because of rounding.
234 }
235 $msgbig = wfMsgHtml( 'show-big-image' );
236 $msgsmall = wfMsgExt( 'show-big-image-thumb',
237 array( 'parseinline' ), $width, $height );
238 } else {
239 # Image is small enough to show full size on image page
240 $msgbig = htmlspecialchars( $this->img->getName() );
241 $msgsmall = wfMsgExt( 'file-nohires', array( 'parseinline' ) );
242 }
243
244 $params['width'] = $width;
245 $thumbnail = $this->img->transform( $params );
246
247 $anchorclose = "<br />";
248 if( $this->img->mustRender() ) {
249 $showLink = true;
250 } else {
251 $anchorclose .=
252 $msgsmall .
253 '<br />' . Xml::tags( 'a', $linkAttribs, $msgbig ) . ' ' . $msgsize;
254 }
255
256 if ( $this->img->isMultipage() ) {
257 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
258 }
259
260 $imgAttribs = array(
261 'border' => 0,
262 'alt' => $this->img->getTitle()->getPrefixedText()
263 );
264
265 if ( $thumbnail ) {
266 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
267 $thumbnail->toHtml( $imgAttribs, $linkAttribs ) .
268 $anchorclose . '</div>' );
269 }
270
271 if ( $this->img->isMultipage() ) {
272 $count = $this->img->pageCount();
273
274 if ( $page > 1 ) {
275 $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
276 $link = $sk->makeKnownLinkObj( $this->mTitle, $label, 'page='. ($page-1) );
277 $thumb1 = $sk->makeThumbLinkObj( $this->mTitle, $this->img, $link, $label, 'none',
278 array( 'page' => $page - 1 ) );
279 } else {
280 $thumb1 = '';
281 }
282
283 if ( $page < $count ) {
284 $label = wfMsg( 'imgmultipagenext' );
285 $link = $sk->makeKnownLinkObj( $this->mTitle, $label, 'page='. ($page+1) );
286 $thumb2 = $sk->makeThumbLinkObj( $this->mTitle, $this->img, $link, $label, 'none',
287 array( 'page' => $page + 1 ) );
288 } else {
289 $thumb2 = '';
290 }
291
292 global $wgScript;
293 $select = '<form name="pageselector" action="' .
294 htmlspecialchars( $wgScript ) .
295 '" method="get" onchange="document.pageselector.submit();">' .
296 Xml::hidden( 'title', $this->getTitle()->getPrefixedDbKey() );
297 $select .= $wgOut->parse( wfMsg( 'imgmultigotopre' ), false ) .
298 ' <select id="pageselector" name="page">';
299 for ( $i=1; $i <= $count; $i++ ) {
300 $select .= Xml::option( $wgLang->formatNum( $i ), $i,
301 $i == $page );
302 }
303 $select .= '</select>' . $wgOut->parse( wfMsg( 'imgmultigotopost' ), false ) .
304 '<input type="submit" value="' .
305 htmlspecialchars( wfMsg( 'imgmultigo' ) ) . '"></form>';
306
307 $wgOut->addHTML( '</td><td><div class="multipageimagenavbox">' .
308 "$select<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>" );
309 }
310 } else {
311 #if direct link is allowed but it's not a renderable image, show an icon.
312 if ($this->img->isSafeFile()) {
313 $icon= $this->img->iconThumb();
314
315 $wgOut->addHTML( '<div class="fullImageLink" id="file"><a href="' . $full_url . '">' .
316 $icon->toHtml() .
317 '</a></div>' );
318 }
319
320 $showLink = true;
321 }
322
323
324 if ($showLink) {
325 // Workaround for incorrect MIME type on SVGs uploaded in previous versions
326 if ($mime == 'image/svg') $mime = 'image/svg+xml';
327
328 $filename = wfEscapeWikiText( $this->img->getName() );
329 $info = wfMsg( 'file-info', $sk->formatSize( $this->img->getSize() ), $mime );
330 $infores = '';
331
332 // Check for MIME type. Other types may have more information in the future.
333 if (substr($mime,0,9) == 'image/svg' ) {
334 $infores = wfMsg('file-svg', $width_orig, $height_orig ) . '<br />';
335 }
336
337 global $wgContLang;
338 $dirmark = $wgContLang->getDirMark();
339 if (!$this->img->isSafeFile()) {
340 $warning = wfMsg( 'mediawarning' );
341 $wgOut->addWikiText( <<<EOT
342 <div class="fullMedia">$infores
343 <span class="dangerousLink">[[Media:$filename|$filename]]</span>$dirmark
344 <span class="fileInfo"> $info</span>
345 </div>
346
347 <div class="mediaWarning">$warning</div>
348 EOT
349 );
350 } else {
351 $wgOut->addWikiText( <<<EOT
352 <div class="fullMedia">$infores
353 [[Media:$filename|$filename]]$dirmark <span class="fileInfo"> $info</span>
354 </div>
355 EOT
356 );
357 }
358 }
359
360 if(!$this->img->isLocal()) {
361 $this->printSharedImageText();
362 }
363 } else {
364 # Image does not exist
365
366 $title = SpecialPage::getTitleFor( 'Upload' );
367 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('noimage-linktext'),
368 'wpDestFile=' . urlencode( $this->img->getName() ) );
369 $wgOut->addHTML( wfMsgWikiHtml( 'noimage', $link ) );
370 }
371 }
372
373 function printSharedImageText() {
374 global $wgOut, $wgUser;
375
376 $descUrl = $this->img->getDescriptionUrl();
377 $descText = $this->img->getDescriptionText();
378 $s = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml("sharedupload");
379 if ( $descUrl && !$descText) {
380 $sk = $wgUser->getSkin();
381 $link = $sk->makeExternalLink( $descUrl, wfMsg('shareduploadwiki-linktext') );
382 $s .= " " . wfMsgWikiHtml('shareduploadwiki', $link);
383 }
384 $s .= "</div>";
385 $wgOut->addHTML($s);
386
387 if ( $descText ) {
388 $this->mExtraDescription = $descText;
389 }
390 }
391
392 function getUploadUrl() {
393 global $wgServer;
394 $uploadTitle = SpecialPage::getTitleFor( 'Upload' );
395 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img->getName() ) );
396 }
397
398 /**
399 * Print out the various links at the bottom of the image page, e.g. reupload,
400 * external editing (and instructions link) etc.
401 */
402 function uploadLinksBox() {
403 global $wgUser, $wgOut;
404
405 if( !$this->img->isLocal() )
406 return;
407
408 $sk = $wgUser->getSkin();
409
410 $wgOut->addHtml( '<br /><ul>' );
411
412 # "Upload a new version of this file" link
413 if( UploadForm::userCanReUpload($wgUser,$this->img->name) ) {
414 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
415 $wgOut->addHtml( "<li><div class='plainlinks'>{$ulink}</div></li>" );
416 }
417
418 # External editing link
419 $elink = $sk->makeKnownLinkObj( $this->mTitle, wfMsgHtml( 'edit-externally' ), 'action=edit&externaledit=true&mode=file' );
420 $wgOut->addHtml( '<li>' . $elink . '<div>' . wfMsgWikiHtml( 'edit-externally-help' ) . '</div></li>' );
421
422 $wgOut->addHtml( '</ul>' );
423 }
424
425 function closeShowImage()
426 {
427 # For overloading
428
429 }
430
431 /**
432 * If the page we've just displayed is in the "Image" namespace,
433 * we follow it with an upload history of the image and its usage.
434 */
435 function imageHistory()
436 {
437 global $wgUser, $wgOut, $wgUseExternalEditor;
438
439 $sk = $wgUser->getSkin();
440
441 $line = $this->img->nextHistoryLine();
442
443 if ( $line ) {
444 $list = new ImageHistoryList( $sk, $this->img );
445 $s = $list->beginImageHistoryList() .
446 $list->imageHistoryLine( true, wfTimestamp(TS_MW, $line->img_timestamp),
447 $this->mTitle->getDBkey(), $line->img_user,
448 $line->img_user_text, $line->img_size, $line->img_description,
449 $line->img_width, $line->img_height
450 );
451
452 while ( $line = $this->img->nextHistoryLine() ) {
453 $s .= $list->imageHistoryLine( false, $line->img_timestamp,
454 $line->oi_archive_name, $line->img_user,
455 $line->img_user_text, $line->img_size, $line->img_description,
456 $line->img_width, $line->img_height
457 );
458 }
459 $s .= $list->endImageHistoryList();
460 } else { $s=''; }
461 $wgOut->addHTML( $s );
462
463 # Exist check because we don't want to show this on pages where an image
464 # doesn't exist along with the noimage message, that would suck. -ævar
465 if( $wgUseExternalEditor && $this->img->exists() ) {
466 $this->uploadLinksBox();
467 }
468
469 }
470
471 function imageLinks()
472 {
473 global $wgUser, $wgOut;
474
475 $wgOut->addHTML( Xml::element( 'h2', array( 'id' => 'filelinks' ), wfMsg( 'imagelinks' ) ) . "\n" );
476
477 $dbr = wfGetDB( DB_SLAVE );
478 $page = $dbr->tableName( 'page' );
479 $imagelinks = $dbr->tableName( 'imagelinks' );
480
481 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
482 $dbr->addQuotes( $this->mTitle->getDBkey() ) . " AND il_from=page_id";
483 $sql = $dbr->limitResult($sql, 500, 0);
484 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
485
486 if ( 0 == $dbr->numRows( $res ) ) {
487 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
488 return;
489 }
490 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
491
492 $sk = $wgUser->getSkin();
493 while ( $s = $dbr->fetchObject( $res ) ) {
494 $name = Title::MakeTitle( $s->page_namespace, $s->page_title );
495 $link = $sk->makeKnownLinkObj( $name, "" );
496 $wgOut->addHTML( "<li>{$link}</li>\n" );
497 }
498 $wgOut->addHTML( "</ul>\n" );
499 }
500
501 function delete()
502 {
503 global $wgUser, $wgOut, $wgRequest;
504
505 if ( !$this->img->exists() || !$this->img->isLocal() ) {
506 # Use standard article deletion
507 Article::delete();
508 return;
509 }
510
511 $confirm = $wgRequest->wasPosted();
512 $reason = $wgRequest->getVal( 'wpReason' );
513 $image = $wgRequest->getVal( 'image' );
514 $oldimage = $wgRequest->getVal( 'oldimage' );
515
516 # Only sysops can delete images. Previously ordinary users could delete
517 # old revisions, but this is no longer the case.
518 if ( !$wgUser->isAllowed('delete') ) {
519 $wgOut->permissionRequired( 'delete' );
520 return;
521 }
522 if ( $wgUser->isBlocked() ) {
523 $wgOut->blockedPage();
524 return;
525 }
526 if ( wfReadOnly() ) {
527 $wgOut->readOnlyPage();
528 return;
529 }
530
531 # Better double-check that it hasn't been deleted yet!
532 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
533 if ( ( !is_null( $image ) )
534 && ( '' == trim( $image ) ) ) {
535 $wgOut->showFatalError( wfMsg( 'cannotdelete' ) );
536 return;
537 }
538
539 # Deleting old images doesn't require confirmation
540 if ( !is_null( $oldimage ) || $confirm ) {
541 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
542 $this->doDeleteImage( $reason );
543 } else {
544 $wgOut->showFatalError( wfMsg( 'sessionfailure' ) );
545 }
546 return;
547 }
548
549 if ( !is_null( $image ) ) {
550 $q = '&image=' . urlencode( $image );
551 } else if ( !is_null( $oldimage ) ) {
552 $q = '&oldimage=' . urlencode( $oldimage );
553 } else {
554 $q = '';
555 }
556 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
557 }
558
559 /*
560 * Delete an image.
561 * Called doDeleteImage() not doDelete() so that Article::delete() doesn't
562 * call back to here.
563 *
564 * @param $reason User provided reason for deletion.
565 */
566 function doDeleteImage( $reason ) {
567 global $wgOut, $wgRequest;
568
569 $oldimage = $wgRequest->getVal( 'oldimage' );
570
571 if ( !is_null( $oldimage ) ) {
572 if ( strlen( $oldimage ) < 16 ) {
573 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
574 return;
575 }
576 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
577 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
578 return;
579 }
580 if ( !$this->doDeleteOldImage( $oldimage ) ) {
581 return;
582 }
583 $deleted = $oldimage;
584 } else {
585 $ok = $this->img->delete( $reason );
586 if( !$ok ) {
587 # If the deletion operation actually failed, bug out:
588 $wgOut->showFileDeleteError( $this->img->getName() );
589 return;
590 }
591
592 # Image itself is now gone, and database is cleaned.
593 # Now we remove the image description page.
594
595 $article = new Article( $this->mTitle );
596 $article->doDeleteArticle( $reason ); # ignore errors
597
598 $deleted = $this->img->getName();
599 }
600
601 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
602 $wgOut->setRobotpolicy( 'noindex,nofollow' );
603
604 $loglink = '[[Special:Log/delete|' . wfMsg( 'deletionlog' ) . ']]';
605 $text = wfMsg( 'deletedtext', $deleted, $loglink );
606
607 $wgOut->addWikiText( $text );
608
609 $wgOut->returnToMain( false, $this->mTitle->getPrefixedText() );
610 }
611
612 /**
613 * @return success
614 */
615 function doDeleteOldImage( $oldimage )
616 {
617 global $wgOut;
618
619 $ok = $this->img->deleteOld( $oldimage, '' );
620 if( !$ok ) {
621 # If we actually have a file and can't delete it, throw an error.
622 # Something went awry...
623 $wgOut->showFileDeleteError( "$oldimage" );
624 } else {
625 # Log the deletion
626 $log = new LogPage( 'delete' );
627 $log->addEntry( 'delete', $this->mTitle, wfMsg('deletedrevision',$oldimage) );
628 }
629 return $ok;
630 }
631
632 function revert() {
633 global $wgOut, $wgRequest, $wgUser;
634
635 $oldimage = $wgRequest->getText( 'oldimage' );
636 if ( strlen( $oldimage ) < 16 ) {
637 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
638 return;
639 }
640 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
641 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
642 return;
643 }
644
645 if ( wfReadOnly() ) {
646 $wgOut->readOnlyPage();
647 return;
648 }
649 if( $wgUser->isAnon() ) {
650 $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' );
651 return;
652 }
653 if ( ! $this->mTitle->userCan( 'edit' ) ) {
654 $wgOut->readOnlyPage( $this->getContent(), true );
655 return;
656 }
657 if ( $wgUser->isBlocked() ) {
658 $wgOut->blockedPage();
659 return;
660 }
661 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
662 $wgOut->showErrorPage( 'internalerror', 'sessionfailure' );
663 return;
664 }
665
666 $sourcePath = $this->img->getArchiveVirtualUrl( $oldimage );
667 $comment = wfMsg( "reverted" );
668 $result = $this->img->upload( $sourcePath, $comment, $comment );
669
670 if ( WikiError::isError( $result ) ) {
671 $this->showError( $result );
672 return;
673 }
674
675 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
676 $wgOut->setRobotpolicy( 'noindex,nofollow' );
677 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
678
679 $descTitle = $this->img->getTitle();
680 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
681 }
682
683 /**
684 * Override handling of action=purge
685 */
686 function doPurge() {
687 if( $this->img->exists() ) {
688 wfDebug( "ImagePage::doPurge purging " . $this->img->getName() . "\n" );
689 $update = new HTMLCacheUpdate( $this->mTitle, 'imagelinks' );
690 $update->doUpdate();
691 $this->img->upgradeRow();
692 $this->img->purgeCache();
693 } else {
694 wfDebug( "ImagePage::doPurge no image\n" );
695 }
696 parent::doPurge();
697 }
698
699 /**
700 * Display an error from a wikitext-formatted WikiError object
701 */
702 function showError( WikiError $error ) {
703 global $wgOut;
704 $wgOut->setPageTitle( wfMsg( "internalerror" ) );
705 $wgOut->setRobotpolicy( "noindex,nofollow" );
706 $wgOut->setArticleRelated( false );
707 $wgOut->enableClientCache( false );
708 $wgOut->addWikiText( $error->getMessage() );
709 }
710
711 }
712
713 /**
714 * @todo document
715 * @addtogroup Media
716 */
717 class ImageHistoryList {
718 var $img, $skin;
719 function ImageHistoryList( $skin, $img ) {
720 $this->skin = $skin;
721 $this->img = $img;
722 }
723
724 function beginImageHistoryList() {
725 $s = "\n" .
726 Xml::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'imghistory' ) ) .
727 "\n<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
728 return $s;
729 }
730
731 function endImageHistoryList() {
732 $s = "</ul>\n";
733 return $s;
734 }
735
736 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description, $width, $height ) {
737 global $wgUser, $wgLang, $wgTitle, $wgContLang;
738
739 $datetime = $wgLang->timeanddate( $timestamp, true );
740 $del = wfMsgHtml( 'deleteimg' );
741 $delall = wfMsgHtml( 'deleteimgcompletely' );
742 $cur = wfMsgHtml( 'cur' );
743 $local = $this->img->isLocal();
744
745 if ( $iscur ) {
746 $url = htmlspecialchars( $this->img->getURL() );
747 $rlink = $cur;
748 if ( $local && $wgUser->isAllowed('delete') ) {
749 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
750 '&action=delete' );
751 $style = $this->skin->getInternalLinkAttributes( $link, $delall );
752
753 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
754 } else {
755 $dlink = $del;
756 }
757 } else {
758 $url = htmlspecialchars( $this->img->getArchiveUrl( $img ) );
759 if( $local && $wgUser->getID() != 0 && $wgTitle->userCan( 'edit' ) ) {
760 $token = urlencode( $wgUser->editToken( $img ) );
761 $rlink = $this->skin->makeKnownLinkObj( $wgTitle,
762 wfMsgHtml( 'revertimg' ), 'action=revert&oldimage=' .
763 urlencode( $img ) . "&wpEditToken=$token" );
764 $dlink = $this->skin->makeKnownLinkObj( $wgTitle,
765 $del, 'action=delete&oldimage=' . urlencode( $img ) .
766 "&wpEditToken=$token" );
767 } else {
768 # Having live active links for non-logged in users
769 # means that bots and spiders crawling our site can
770 # inadvertently change content. Baaaad idea.
771 $rlink = wfMsgHtml( 'revertimg' );
772 $dlink = $del;
773 }
774 }
775
776 if ( $local ) {
777 $userlink = $this->skin->userLink( $user, $usertext ) . $this->skin->userToolLinks( $user, $usertext );
778 } else {
779 $userlink = htmlspecialchars( $usertext );
780 }
781 $nbytes = wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
782 $wgLang->formatNum( $size ) );
783 $widthheight = wfMsgHtml( 'widthheight', $width, $height );
784 $style = $this->skin->getInternalLinkAttributes( $url, $datetime );
785
786 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a> . . {$userlink} . . {$widthheight} ({$nbytes})";
787
788 $s .= $this->skin->commentBlock( $description, $wgTitle );
789 $s .= "</li>\n";
790 return $s;
791 }
792 }
793
794
795