* Fix multipage selector drop-down for DjVu images to work when title
[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 class ImagePage extends Article {
15
16 /* private */ var $img; // Image object this page is shown for
17 var $mExtraDescription = false;
18
19 /**
20 * Handler for action=render
21 * Include body text only; none of the image extras
22 */
23 function render() {
24 global $wgOut;
25 $wgOut->setArticleBodyOnly( true );
26 $wgOut->addSecondaryWikitext( $this->getContent() );
27 }
28
29 function view() {
30 global $wgOut, $wgShowEXIF, $wgRequest, $wgUser;
31
32 $this->img = new Image( $this->mTitle );
33
34 $diff = $wgRequest->getVal( 'diff' );
35 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
36
37 if ( $this->mTitle->getNamespace() != NS_IMAGE || ( isset( $diff ) && $diffOnly ) )
38 return Article::view();
39
40 if ($wgShowEXIF && $this->img->exists()) {
41 $exif = $this->img->getExifData();
42 $showmeta = count($exif) ? true : false;
43 } else {
44 $exif = false;
45 $showmeta = false;
46 }
47
48 if ($this->img->exists())
49 $wgOut->addHTML($this->showTOC($showmeta));
50
51 $this->openShowImage();
52
53 # No need to display noarticletext, we use our own message, output in openShowImage()
54 if ( $this->getID() ) {
55 Article::view();
56 } else {
57 # Just need to set the right headers
58 $wgOut->setArticleFlag( true );
59 $wgOut->setRobotpolicy( 'index,follow' );
60 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
61 $this->viewUpdates();
62 }
63
64 # Show shared description, if needed
65 if ( $this->mExtraDescription ) {
66 $fol = wfMsg( 'shareddescriptionfollows' );
67 if( $fol != '-' ) {
68 $wgOut->addWikiText( $fol );
69 }
70 $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription . '</div>' );
71 }
72
73 $this->closeShowImage();
74 $this->imageHistory();
75 $this->imageLinks();
76
77 if ( $exif ) {
78 global $wgStylePath, $wgStyleVersion;
79 $expand = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-expand' ) ) );
80 $collapse = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-collapse' ) ) );
81 $wgOut->addHTML( Xml::element( 'h2', array( 'id' => 'metadata' ), wfMsg( 'metadata' ) ). "\n" );
82 $wgOut->addWikiText( $this->makeMetadataTable( $exif ) );
83 $wgOut->addHTML(
84 "<script type=\"text/javascript\" src=\"$wgStylePath/common/metadata.js?$wgStyleVersion\"></script>\n" .
85 "<script type=\"text/javascript\">attachMetadataToggle('mw_metadata', '$expand', '$collapse');</script>\n" );
86 }
87 }
88
89 /**
90 * Create the TOC
91 *
92 * @access private
93 *
94 * @param bool $metadata Whether or not to show the metadata link
95 * @return string
96 */
97 function showTOC( $metadata ) {
98 global $wgLang;
99 $r = '<ul id="filetoc">
100 <li><a href="#file">' . $wgLang->getNsText( NS_IMAGE ) . '</a></li>
101 <li><a href="#filehistory">' . wfMsgHtml( 'imghistory' ) . '</a></li>
102 <li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>' .
103 ($metadata ? '<li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>' : '') . '
104 </ul>';
105 return $r;
106 }
107
108 /**
109 * Make a table with metadata to be shown in the output page.
110 *
111 * @access private
112 *
113 * @param array $exif The array containing the EXIF data
114 * @return string
115 */
116 function makeMetadataTable( $exif ) {
117 $r = wfMsg( 'metadata-help' ) . "\n\n";
118 $r .= "{| id=mw_metadata class=mw_metadata\n";
119 $visibleFields = $this->visibleMetadataFields();
120 foreach( $exif as $k => $v ) {
121 $tag = strtolower( $k );
122 $msg = wfMsg( "exif-$tag" );
123 $class = "exif-$tag";
124 if( !in_array( $tag, $visibleFields ) ) {
125 $class .= ' collapsable';
126 }
127 $r .= "|- class=\"$class\"\n";
128 $r .= "!| $msg\n";
129 $r .= "|| $v\n";
130 }
131 $r .= '|}';
132 return $r;
133 }
134
135 /**
136 * Get a list of EXIF metadata items which should be displayed when
137 * the metadata table is collapsed.
138 *
139 * @return array of strings
140 * @access private
141 */
142 function visibleMetadataFields() {
143 $fields = array();
144 $lines = explode( "\n", wfMsgForContent( 'metadata-fields' ) );
145 foreach( $lines as $line ) {
146 $matches = array();
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, $wgLang;
169 global $wgUseImageResize, $wgGenerateThumbnailOnParse;
170
171 $full_url = $this->img->getURL();
172 $anchoropen = '';
173 $anchorclose = '';
174 $sizeSel = intval( $wgUser->getOption( 'imagesize') );
175
176 if( !isset( $wgImageLimits[$sizeSel] ) ) {
177 $sizeSel = User::getDefaultOption( 'imagesize' );
178 }
179 $max = $wgImageLimits[$sizeSel];
180 $maxWidth = $max[0];
181 $maxHeight = $max[1];
182 $sk = $wgUser->getSkin();
183
184 if ( $this->img->exists() ) {
185 # image
186 $page = $wgRequest->getIntOrNull( 'page' );
187 if ( ! is_null( $page ) ) {
188 $this->img->selectPage( $page );
189 } else {
190 $page = 1;
191 }
192 $width = $this->img->getWidth();
193 $height = $this->img->getHeight();
194 $showLink = false;
195
196 if ( $this->img->allowInlineDisplay() and $width and $height) {
197 # image
198
199 # "Download high res version" link below the image
200 $msg = wfMsgHtml('showbigimage', $width, $height, intval( $this->img->getSize()/1024 ) );
201
202 # We'll show a thumbnail of this image
203 if ( $width > $maxWidth || $height > $maxHeight ) {
204 # Calculate the thumbnail size.
205 # First case, the limiting factor is the width, not the height.
206 if ( $width / $height >= $maxWidth / $maxHeight ) {
207 $height = round( $height * $maxWidth / $width);
208 $width = $maxWidth;
209 # Note that $height <= $maxHeight now.
210 } else {
211 $newwidth = floor( $width * $maxHeight / $height);
212 $height = round( $height * $newwidth / $width );
213 $width = $newwidth;
214 # Note that $height <= $maxHeight now, but might not be identical
215 # because of rounding.
216 }
217
218 if( $wgUseImageResize ) {
219 $thumbnail = $this->img->getThumbnail( $width, -1, $wgGenerateThumbnailOnParse );
220 if ( $thumbnail == null ) {
221 $url = $this->img->getViewURL();
222 } else {
223 $url = $thumbnail->getURL();
224 }
225 } else {
226 # No resize ability? Show the full image, but scale
227 # it down in the browser so it fits on the page.
228 $url = $this->img->getViewURL();
229 }
230 $anchoropen = "<a href=\"{$full_url}\">";
231 $anchorclose = "</a><br />";
232 if( $this->img->mustRender() ) {
233 $showLink = true;
234 } else {
235 $anchorclose .= "\n$anchoropen{$msg}</a>";
236 }
237 } else {
238 $url = $this->img->getViewURL();
239 $showLink = true;
240 }
241
242 if ( $this->img->isMultipage() ) {
243 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
244 }
245
246 $wgOut->addHTML( '<div class="fullImageLink" id="file">' . $anchoropen .
247 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
248 htmlspecialchars( $this->img->getTitle()->getPrefixedText() ).'" />' . $anchorclose . '</div>' );
249
250 if ( $this->img->isMultipage() ) {
251 $count = $this->img->pageCount();
252
253 if ( $page > 1 ) {
254 $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
255 $link = $sk->makeLinkObj( $this->mTitle, $label, 'page='. ($page-1) );
256 $this->img->selectPage( $page - 1 );
257 $thumb1 = $sk->makeThumbLinkObj( $this->img, $link, $label, 'none' );
258 } else {
259 $thumb1 = '';
260 }
261
262 if ( $page < $count ) {
263 $label = wfMsg( 'imgmultipagenext' );
264 $this->img->selectPage( $page + 1 );
265 $link = $sk->makeLinkObj( $this->mTitle, $label, 'page='. ($page+1) );
266 $thumb2 = $sk->makeThumbLinkObj( $this->img, $link, $label, 'none' );
267 } else {
268 $thumb2 = '';
269 }
270
271 global $wgScript;
272 $select = '<form name="pageselector" action="' .
273 htmlspecialchars( $wgScript ) .
274 '" method="get" onchange="document.pageselector.submit();">' .
275 Xml::hidden( 'title', $this->getTitle()->getPrefixedDbKey() );
276 $select .= $wgOut->parse( wfMsg( 'imgmultigotopre' ), false ) .
277 ' <select id="pageselector" name="page">';
278 for ( $i=1; $i <= $count; $i++ ) {
279 $select .= Xml::option( $wgLang->formatNum( $i ), $i,
280 $i == $page );
281 }
282 $select .= '</select>' . $wgOut->parse( wfMsg( 'imgmultigotopost' ), false ) .
283 '<input type="submit" value="' .
284 htmlspecialchars( wfMsg( 'imgmultigo' ) ) . '"></form>';
285
286 $wgOut->addHTML( '</td><td><div class="multipageimagenavbox">' .
287 "$select<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>" );
288 }
289 } else {
290 #if direct link is allowed but it's not a renderable image, show an icon.
291 if ($this->img->isSafeFile()) {
292 $icon= $this->img->iconThumb();
293
294 $wgOut->addHTML( '<div class="fullImageLink" id="file"><a href="' . $full_url . '">' .
295 $icon->toHtml() .
296 '</a></div>' );
297 }
298
299 $showLink = true;
300 }
301
302
303 if ($showLink) {
304 $filename = wfEscapeWikiText( $this->img->getName() );
305 // Hacky workaround: for some reason we use the incorrect MIME type
306 // image/svg for SVG. This should be fixed internally, but at least
307 // make the displayed type right.
308 $mime = $this->img->getMimeType();
309 if ($mime == 'image/svg') $mime = 'image/svg+xml';
310
311 $info = wfMsg( 'fileinfo',
312 ceil($this->img->getSize()/1024.0),
313 $mime );
314
315 global $wgContLang;
316 $dirmark = $wgContLang->getDirMark();
317 if (!$this->img->isSafeFile()) {
318 $warning = wfMsg( 'mediawarning' );
319 $wgOut->addWikiText( <<<END
320 <div class="fullMedia">
321 <span class="dangerousLink">[[Media:$filename|$filename]]</span>$dirmark
322 <span class="fileInfo"> ($info)</span>
323 </div>
324
325 <div class="mediaWarning">$warning</div>
326 END
327 );
328 } else {
329 $wgOut->addWikiText( <<<END
330 <div class="fullMedia">
331 [[Media:$filename|$filename]]$dirmark <span class="fileInfo"> ($info)</span>
332 </div>
333 END
334 );
335 }
336 }
337
338 if($this->img->fromSharedDirectory) {
339 $this->printSharedImageText();
340 }
341 } else {
342 # Image does not exist
343
344 $title = SpecialPage::getTitleFor( 'Upload' );
345 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('noimage-linktext'),
346 'wpDestFile=' . urlencode( $this->img->getName() ) );
347 $wgOut->addHTML( wfMsgWikiHtml( 'noimage', $link ) );
348 }
349 }
350
351 function printSharedImageText() {
352 global $wgRepositoryBaseUrl, $wgFetchCommonsDescriptions, $wgOut, $wgUser;
353
354 $url = $wgRepositoryBaseUrl . urlencode($this->mTitle->getDBkey());
355 $sharedtext = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml("sharedupload");
356 if ($wgRepositoryBaseUrl && !$wgFetchCommonsDescriptions) {
357
358 $sk = $wgUser->getSkin();
359 $title = SpecialPage::getTitleFor( 'Upload' );
360 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('shareduploadwiki-linktext'),
361 array( 'wpDestFile' => urlencode( $this->img->getName() )));
362 $sharedtext .= " " . wfMsgWikiHtml('shareduploadwiki', $link);
363 }
364 $sharedtext .= "</div>";
365 $wgOut->addHTML($sharedtext);
366
367 if ($wgRepositoryBaseUrl && $wgFetchCommonsDescriptions) {
368 $text = Http::get($url . '?action=render');
369 if ($text)
370 $this->mExtraDescription = $text;
371 }
372 }
373
374 function getUploadUrl() {
375 global $wgServer;
376 $uploadTitle = SpecialPage::getTitleFor( '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, wfMsgHtml( '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( Xml::element( 'h2', array( 'id' => 'filelinks' ), wfMsg( 'imagelinks' ) ) . "\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;
542
543 $oldimage = $wgRequest->getVal( 'oldimage' );
544
545 if ( !is_null( $oldimage ) ) {
546 if ( strlen( $oldimage ) < 16 ) {
547 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
548 return;
549 }
550 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
551 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
552 return;
553 }
554 if ( !$this->doDeleteOldImage( $oldimage ) ) {
555 return;
556 }
557 $deleted = $oldimage;
558 } else {
559 $ok = $this->img->delete( $reason );
560 if( !$ok ) {
561 # If the deletion operation actually failed, bug out:
562 $wgOut->showFileDeleteError( $this->img->getName() );
563 return;
564 }
565
566 # Image itself is now gone, and database is cleaned.
567 # Now we remove the image description page.
568
569 $article = new Article( $this->mTitle );
570 $article->doDeleteArticle( $reason ); # ignore errors
571
572 $deleted = $this->img->getName();
573 }
574
575 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
576 $wgOut->setRobotpolicy( 'noindex,nofollow' );
577
578 $loglink = '[[Special:Log/delete|' . wfMsg( 'deletionlog' ) . ']]';
579 $text = wfMsg( 'deletedtext', $deleted, $loglink );
580
581 $wgOut->addWikiText( $text );
582
583 $wgOut->returnToMain( false, $this->mTitle->getPrefixedText() );
584 }
585
586 /**
587 * @return success
588 */
589 function doDeleteOldImage( $oldimage )
590 {
591 global $wgOut;
592
593 $ok = $this->img->deleteOld( $oldimage, '' );
594 if( !$ok ) {
595 # If we actually have a file and can't delete it, throw an error.
596 # Something went awry...
597 $wgOut->showFileDeleteError( "$oldimage" );
598 } else {
599 # Log the deletion
600 $log = new LogPage( 'delete' );
601 $log->addEntry( 'delete', $this->mTitle, wfMsg('deletedrevision',$oldimage) );
602 }
603 return $ok;
604 }
605
606 function revert() {
607 global $wgOut, $wgRequest, $wgUser;
608
609 $oldimage = $wgRequest->getText( 'oldimage' );
610 if ( strlen( $oldimage ) < 16 ) {
611 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
612 return;
613 }
614 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
615 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
616 return;
617 }
618
619 if ( wfReadOnly() ) {
620 $wgOut->readOnlyPage();
621 return;
622 }
623 if( $wgUser->isAnon() ) {
624 $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' );
625 return;
626 }
627 if ( ! $this->mTitle->userCan( 'edit' ) ) {
628 $wgOut->readOnlyPage( $this->getContent(), true );
629 return;
630 }
631 if ( $wgUser->isBlocked() ) {
632 return $this->blockedIPpage();
633 }
634 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
635 $wgOut->showErrorPage( 'internalerror', 'sessionfailure' );
636 return;
637 }
638 $name = substr( $oldimage, 15 );
639
640 $dest = wfImageDir( $name );
641 $archive = wfImageArchiveDir( $name );
642 $curfile = "{$dest}/{$name}";
643
644 if ( !is_dir( $dest ) ) wfMkdirParents( $dest );
645 if ( !is_dir( $archive ) ) wfMkdirParents( $archive );
646
647 if ( ! is_file( $curfile ) ) {
648 $wgOut->showFileNotFoundError( htmlspecialchars( $curfile ) );
649 return;
650 }
651 $oldver = wfTimestampNow() . "!{$name}";
652
653 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
654 $wgOut->showFileRenameError( $curfile, "${archive}/{$oldver}" );
655 return;
656 }
657 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
658 $wgOut->showFileCopyError( "${archive}/{$oldimage}", $curfile );
659 return;
660 }
661
662 # Record upload and update metadata cache
663 $img = Image::newFromName( $name );
664 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
665
666 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
667 $wgOut->setRobotpolicy( 'noindex,nofollow' );
668 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
669
670 $descTitle = $img->getTitle();
671 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
672 }
673
674 function blockedIPpage() {
675 $edit = new EditPage( $this );
676 return $edit->blockedIPpage();
677 }
678
679 /**
680 * Override handling of action=purge
681 */
682 function doPurge() {
683 $this->img = new Image( $this->mTitle );
684 if( $this->img->exists() ) {
685 wfDebug( "ImagePage::doPurge purging " . $this->img->getName() . "\n" );
686 $update = new HTMLCacheUpdate( $this->mTitle, 'imagelinks' );
687 $update->doUpdate();
688 $this->img->purgeCache();
689 } else {
690 wfDebug( "ImagePage::doPurge no image\n" );
691 }
692 parent::doPurge();
693 }
694
695 }
696
697 /**
698 * @todo document
699 */
700 class ImageHistoryList {
701 function ImageHistoryList( &$skin ) {
702 $this->skin =& $skin;
703 }
704
705 function beginImageHistoryList() {
706 $s = "\n" .
707 Xml::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'imghistory' ) ) .
708 "\n<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
709 return $s;
710 }
711
712 function endImageHistoryList() {
713 $s = "</ul>\n";
714 return $s;
715 }
716
717 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description, $width, $height ) {
718 global $wgUser, $wgLang, $wgTitle, $wgContLang;
719
720 $datetime = $wgLang->timeanddate( $timestamp, true );
721 $del = wfMsgHtml( 'deleteimg' );
722 $delall = wfMsgHtml( 'deleteimgcompletely' );
723 $cur = wfMsgHtml( 'cur' );
724
725 if ( $iscur ) {
726 $url = Image::imageUrl( $img );
727 $rlink = $cur;
728 if ( $wgUser->isAllowed('delete') ) {
729 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
730 '&action=delete' );
731 $style = $this->skin->getInternalLinkAttributes( $link, $delall );
732
733 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
734 } else {
735 $dlink = $del;
736 }
737 } else {
738 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
739 if( $wgUser->getID() != 0 && $wgTitle->userCan( 'edit' ) ) {
740 $token = urlencode( $wgUser->editToken( $img ) );
741 $rlink = $this->skin->makeKnownLinkObj( $wgTitle,
742 wfMsgHtml( 'revertimg' ), 'action=revert&oldimage=' .
743 urlencode( $img ) . "&wpEditToken=$token" );
744 $dlink = $this->skin->makeKnownLinkObj( $wgTitle,
745 $del, 'action=delete&oldimage=' . urlencode( $img ) .
746 "&wpEditToken=$token" );
747 } else {
748 # Having live active links for non-logged in users
749 # means that bots and spiders crawling our site can
750 # inadvertently change content. Baaaad idea.
751 $rlink = wfMsgHtml( 'revertimg' );
752 $dlink = $del;
753 }
754 }
755
756 $userlink = $this->skin->userLink( $user, $usertext ) . $this->skin->userToolLinks( $user, $usertext );
757 $nbytes = wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
758 $wgLang->formatNum( $size ) );
759 $widthheight = wfMsgHtml( 'widthheight', $width, $height );
760 $style = $this->skin->getInternalLinkAttributes( $url, $datetime );
761
762 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a> . . {$userlink} . . {$widthheight} ({$nbytes})";
763
764 $s .= $this->skin->commentBlock( $description, $wgTitle );
765 $s .= "</li>\n";
766 return $s;
767 }
768
769 }
770
771
772 ?>