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