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