Localisation updates for core messages from translatewiki.net (2009-02-20 19:30 UTC)
[mediawiki.git] / includes / ImagePage.php
blob9eed236aa1bad92bc7785855438ffc8b2c8f63b9
1 <?php
3 if( !defined( 'MEDIAWIKI' ) )
4 die( 1 );
6 /**
7 * Special handling for image description pages
9 * @ingroup Media
11 class ImagePage extends Article {
13 /* private */ var $img; // Image object
14 /* private */ var $displayImg;
15 /* private */ var $repo;
16 /* private */ var $fileLoaded;
17 var $mExtraDescription = false;
18 var $dupes;
20 function __construct( $title ) {
21 parent::__construct( $title );
22 $this->dupes = null;
23 $this->repo = null;
26 public function setFile( $file ) {
27 $this->displayImg = $file;
28 $this->img = $file;
29 $this->fileLoaded = true;
32 protected function loadFile() {
33 if( $this->fileLoaded ) {
34 return true;
36 $this->fileLoaded = true;
38 $this->displayImg = $this->img = false;
39 wfRunHooks( 'ImagePageFindFile', array( $this, &$this->img, &$this->displayImg ) );
40 if( !$this->img ) {
41 $this->img = wfFindFile( $this->mTitle );
42 if( !$this->img ) {
43 $this->img = wfLocalFile( $this->mTitle );
46 if( !$this->displayImg ) {
47 $this->displayImg = $this->img;
49 $this->repo = $this->img->getRepo();
52 /**
53 * Handler for action=render
54 * Include body text only; none of the image extras
56 public function render() {
57 global $wgOut;
58 $wgOut->setArticleBodyOnly( true );
59 parent::view();
62 public function view() {
63 global $wgOut, $wgShowEXIF, $wgRequest, $wgUser;
64 $this->loadFile();
66 if( $this->mTitle->getNamespace() == NS_FILE && $this->img->getRedirected() ) {
67 if( $this->mTitle->getDBkey() == $this->img->getName() ) {
68 // mTitle is the same as the redirect target so ask Article
69 // to perform the redirect for us.
70 return Article::view();
71 } else {
72 // mTitle is not the same as the redirect target so it is
73 // probably the redirect page itself. Fake the redirect symbol
74 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
75 $wgOut->addHTML( $this->viewRedirect( Title::makeTitle( NS_FILE, $this->img->getName() ),
76 /* $appendSubtitle */ true, /* $forceKnown */ true ) );
77 $this->viewUpdates();
78 return;
82 $diff = $wgRequest->getVal( 'diff' );
83 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
85 if( $this->mTitle->getNamespace() != NS_FILE || ( isset( $diff ) && $diffOnly ) )
86 return Article::view();
88 if( $wgShowEXIF && $this->displayImg->exists() ) {
89 // FIXME: bad interface, see note on MediaHandler::formatMetadata().
90 $formattedMetadata = $this->displayImg->formatMetadata();
91 $showmeta = $formattedMetadata !== false;
92 } else {
93 $showmeta = false;
96 if( !$diff && $this->displayImg->exists() )
97 $wgOut->addHTML( $this->showTOC($showmeta) );
99 if( !$diff )
100 $this->openShowImage();
102 # No need to display noarticletext, we use our own message, output in openShowImage()
103 if( $this->getID() ) {
104 Article::view();
105 } else {
106 # Just need to set the right headers
107 $wgOut->setArticleFlag( true );
108 $wgOut->setRobotPolicy( 'noindex,nofollow' );
109 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
110 $this->viewUpdates();
113 # Show shared description, if needed
114 if( $this->mExtraDescription ) {
115 $fol = wfMsgNoTrans( 'shareddescriptionfollows' );
116 if( $fol != '-' && !wfEmptyMsg( 'shareddescriptionfollows', $fol ) ) {
117 $wgOut->addWikiText( $fol );
119 $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription . '</div>' );
122 $this->closeShowImage();
123 $this->imageHistory();
124 // TODO: Cleanup the following
126 $wgOut->addHTML( Xml::element( 'h2',
127 array( 'id' => 'filelinks' ),
128 wfMsg( 'imagelinks' ) ) . "\n" );
129 $this->imageDupes();
130 $this->imageRedirects();
131 $this->imageLinks();
133 if( $showmeta ) {
134 global $wgStylePath, $wgStyleVersion;
135 $expand = htmlspecialchars( Xml::escapeJsString( wfMsg( 'metadata-expand' ) ) );
136 $collapse = htmlspecialchars( Xml::escapeJsString( wfMsg( 'metadata-collapse' ) ) );
137 $wgOut->addHTML( Xml::element( 'h2', array( 'id' => 'metadata' ), wfMsg( 'metadata' ) ). "\n" );
138 $wgOut->addWikiText( $this->makeMetadataTable( $formattedMetadata ) );
139 $wgOut->addScriptFile( 'metadata.js' );
140 $wgOut->addHTML(
141 "<script type=\"text/javascript\">attachMetadataToggle('mw_metadata', '$expand', '$collapse');</script>\n" );
145 public function getRedirectTarget() {
146 $this->loadFile();
147 if( $this->img->isLocal() ) {
148 return parent::getRedirectTarget();
150 // Foreign image page
151 $from = $this->img->getRedirected();
152 $to = $this->img->getName();
153 if( $from == $to ) {
154 return null;
156 return $this->mRedirectTarget = Title::makeTitle( NS_FILE, $to );
158 public function followRedirect() {
159 $this->loadFile();
160 if( $this->img->isLocal() ) {
161 return parent::followRedirect();
163 $from = $this->img->getRedirected();
164 $to = $this->img->getName();
165 if( $from == $to ) {
166 return false;
168 return Title::makeTitle( NS_FILE, $to );
170 public function isRedirect( $text = false ) {
171 $this->loadFile();
172 if( $this->img->isLocal() )
173 return parent::isRedirect( $text );
175 return (bool)$this->img->getRedirected();
178 public function isLocal() {
179 $this->loadFile();
180 return $this->img->isLocal();
183 public function getFile() {
184 $this->loadFile();
185 return $this->img;
188 public function getDisplayedFile() {
189 $this->loadFile();
190 return $this->displayImg;
193 public function getDuplicates() {
194 $this->loadFile();
195 if( !is_null($this->dupes) ) {
196 return $this->dupes;
198 if( !( $hash = $this->img->getSha1() ) ) {
199 return $this->dupes = array();
201 $dupes = RepoGroup::singleton()->findBySha1( $hash );
202 // Remove duplicates with self and non matching file sizes
203 $self = $this->img->getRepoName().':'.$this->img->getName();
204 $size = $this->img->getSize();
205 foreach ( $dupes as $index => $file ) {
206 $key = $file->getRepoName().':'.$file->getName();
207 if( $key == $self )
208 unset( $dupes[$index] );
209 if( $file->getSize() != $size )
210 unset( $dupes[$index] );
212 return $this->dupes = $dupes;
218 * Create the TOC
220 * @param bool $metadata Whether or not to show the metadata link
221 * @return string
223 protected function showTOC( $metadata ) {
224 global $wgLang;
225 $r = '<ul id="filetoc">
226 <li><a href="#file">' . $wgLang->getNsText( NS_FILE ) . '</a></li>
227 <li><a href="#filehistory">' . wfMsgHtml( 'filehist' ) . '</a></li>
228 <li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>' .
229 ($metadata ? ' <li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>' : '') . '
230 </ul>';
231 return $r;
235 * Make a table with metadata to be shown in the output page.
237 * FIXME: bad interface, see note on MediaHandler::formatMetadata().
239 * @param array $exif The array containing the EXIF data
240 * @return string
242 protected function makeMetadataTable( $metadata ) {
243 $r = wfMsg( 'metadata-help' ) . "\n\n";
244 $r .= "{| id=mw_metadata class=mw_metadata\n";
245 foreach ( $metadata as $type => $stuff ) {
246 foreach ( $stuff as $v ) {
247 # FIXME, why is this using escapeId for a class?!
248 $class = Sanitizer::escapeId( $v['id'] );
249 if( $type == 'collapsed' ) {
250 $class .= ' collapsable';
252 $r .= "|- class=\"$class\"\n";
253 $r .= "!| {$v['name']}\n";
254 $r .= "|| {$v['value']}\n";
257 $r .= '|}';
258 return $r;
262 * Overloading Article's getContent method.
264 * Omit noarticletext if sharedupload; text will be fetched from the
265 * shared upload server if possible.
267 public function getContent() {
268 $this->loadFile();
269 if( $this->img && !$this->img->isLocal() && 0 == $this->getID() ) {
270 return '';
272 return Article::getContent();
275 protected function openShowImage() {
276 global $wgOut, $wgUser, $wgImageLimits, $wgRequest, $wgLang, $wgContLang;
278 $this->loadFile();
280 $full_url = $this->displayImg->getURL();
281 $linkAttribs = false;
282 $sizeSel = intval( $wgUser->getOption( 'imagesize') );
283 if( !isset( $wgImageLimits[$sizeSel] ) ) {
284 $sizeSel = User::getDefaultOption( 'imagesize' );
286 // The user offset might still be incorrect, specially if
287 // $wgImageLimits got changed (see bug #8858).
288 if( !isset( $wgImageLimits[$sizeSel] ) ) {
289 // Default to the first offset in $wgImageLimits
290 $sizeSel = 0;
293 $max = $wgImageLimits[$sizeSel];
294 $maxWidth = $max[0];
295 $maxHeight = $max[1];
296 $sk = $wgUser->getSkin();
297 $dirmark = $wgContLang->getDirMark();
299 if( $this->displayImg->exists() ) {
300 # image
301 $page = $wgRequest->getIntOrNull( 'page' );
302 if( is_null( $page ) ) {
303 $params = array();
304 $page = 1;
305 } else {
306 $params = array( 'page' => $page );
308 $width_orig = $this->displayImg->getWidth();
309 $width = $width_orig;
310 $height_orig = $this->displayImg->getHeight();
311 $height = $height_orig;
312 $mime = $this->displayImg->getMimeType();
313 $showLink = false;
314 $linkAttribs = array( 'href' => $full_url );
315 $longDesc = $this->displayImg->getLongDesc();
317 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this , &$wgOut ) ) ;
319 if( $this->displayImg->allowInlineDisplay() ) {
320 # image
322 # "Download high res version" link below the image
323 #$msgsize = wfMsgHtml('file-info-size', $width_orig, $height_orig, $sk->formatSize( $this->displayImg->getSize() ), $mime );
324 # We'll show a thumbnail of this image
325 if( $width > $maxWidth || $height > $maxHeight ) {
326 # Calculate the thumbnail size.
327 # First case, the limiting factor is the width, not the height.
328 if( $width / $height >= $maxWidth / $maxHeight ) {
329 $height = round( $height * $maxWidth / $width);
330 $width = $maxWidth;
331 # Note that $height <= $maxHeight now.
332 } else {
333 $newwidth = floor( $width * $maxHeight / $height);
334 $height = round( $height * $newwidth / $width );
335 $width = $newwidth;
336 # Note that $height <= $maxHeight now, but might not be identical
337 # because of rounding.
339 $msgbig = wfMsgHtml( 'show-big-image' );
340 $msgsmall = wfMsgExt( 'show-big-image-thumb', 'parseinline',
341 $wgLang->formatNum( $width ),
342 $wgLang->formatNum( $height )
344 } else {
345 # Image is small enough to show full size on image page
346 $msgbig = htmlspecialchars( $this->displayImg->getName() );
347 $msgsmall = wfMsgExt( 'file-nohires', array( 'parseinline' ) );
350 $params['width'] = $width;
351 $thumbnail = $this->displayImg->transform( $params );
353 $anchorclose = "<br />";
354 if( $this->displayImg->mustRender() ) {
355 $showLink = true;
356 } else {
357 $anchorclose .=
358 $msgsmall .
359 '<br />' . Xml::tags( 'a', $linkAttribs, $msgbig ) . "$dirmark " . $longDesc;
362 if( $this->displayImg->isMultipage() ) {
363 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
366 if( $thumbnail ) {
367 $options = array(
368 'alt' => $this->displayImg->getTitle()->getPrefixedText(),
369 'file-link' => true,
371 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
372 $thumbnail->toHtml( $options ) .
373 $anchorclose . '</div>' );
376 if( $this->displayImg->isMultipage() ) {
377 $count = $this->displayImg->pageCount();
379 if( $page > 1 ) {
380 $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
381 $link = $sk->makeKnownLinkObj( $this->mTitle, $label, 'page='. ($page-1) );
382 $thumb1 = $sk->makeThumbLinkObj( $this->mTitle, $this->displayImg, $link, $label, 'none',
383 array( 'page' => $page - 1 ) );
384 } else {
385 $thumb1 = '';
388 if( $page < $count ) {
389 $label = wfMsg( 'imgmultipagenext' );
390 $link = $sk->makeKnownLinkObj( $this->mTitle, $label, 'page='. ($page+1) );
391 $thumb2 = $sk->makeThumbLinkObj( $this->mTitle, $this->displayImg, $link, $label, 'none',
392 array( 'page' => $page + 1 ) );
393 } else {
394 $thumb2 = '';
397 global $wgScript;
399 $formParams = array(
400 'name' => 'pageselector',
401 'action' => $wgScript,
402 'onchange' => 'document.pageselector.submit();',
405 $option = array();
406 for ( $i=1; $i <= $count; $i++ ) {
407 $options[] = Xml::option( $wgLang->formatNum($i), $i, $i == $page );
409 $select = Xml::tags( 'select',
410 array( 'id' => 'pageselector', 'name' => 'page' ),
411 implode( "\n", $options ) );
413 $wgOut->addHTML(
414 '</td><td><div class="multipageimagenavbox">' .
415 Xml::openElement( 'form', $formParams ) .
416 Xml::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) .
417 wfMsgExt( 'imgmultigoto', array( 'parseinline', 'replaceafter' ), $select ) .
418 Xml::submitButton( wfMsg( 'imgmultigo' ) ) .
419 Xml::closeElement( 'form' ) .
420 "<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>"
423 } else {
424 #if direct link is allowed but it's not a renderable image, show an icon.
425 if( $this->displayImg->isSafeFile() ) {
426 $icon= $this->displayImg->iconThumb();
428 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
429 $icon->toHtml( array( 'desc-link' => true ) ) .
430 '</div>' );
433 $showLink = true;
437 if($showLink) {
438 $filename = wfEscapeWikiText( $this->displayImg->getName() );
440 if( !$this->displayImg->isSafeFile() ) {
441 $warning = wfMsgNoTrans( 'mediawarning' );
442 $wgOut->addWikiText( <<<EOT
443 <div class="fullMedia">
444 <span class="dangerousLink">[[Media:$filename|$filename]]</span>$dirmark
445 <span class="fileInfo"> $longDesc</span>
446 </div>
448 <div class="mediaWarning">$warning</div>
451 } else {
452 $wgOut->addWikiText( <<<EOT
453 <div class="fullMedia">
454 [[Media:$filename|$filename]]$dirmark <span class="fileInfo"> $longDesc</span>
455 </div>
461 if( !$this->displayImg->isLocal() ) {
462 $this->printSharedImageText();
464 } else {
465 # Image does not exist
467 $title = SpecialPage::getTitleFor( 'Upload' );
468 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('noimage-linktext'),
469 'wpDestFile=' . urlencode( $this->displayImg->getName() ) );
470 $wgOut->addHTML( wfMsgWikiHtml( 'noimage', $link ) );
475 * Show a notice that the file is from a shared repository
477 protected function printSharedImageText() {
478 global $wgOut, $wgUser;
480 $this->loadFile();
482 $descUrl = $this->img->getDescriptionUrl();
483 $descText = $this->img->getDescriptionText();
484 $msg = '';
485 if( $descUrl ) {
486 $sk = $wgUser->getSkin();
487 $link = $sk->makeExternalLink( $descUrl, wfMsg( 'shareduploadwiki-linktext' ) );
488 $msg = ( $descText ) ? 'shareduploadwiki-desc' : 'shareduploadwiki';
489 $msg = wfMsgExt( $msg, array( 'parseinline', 'replaceafter' ), $link );
490 if( $msg == '-' ) {
491 $msg = '';
494 $s = "<div class='sharedUploadNotice'>";
495 $s .= wfMsgWikiHtml( 'sharedupload', $this->img->getRepo()->getDisplayName(), $msg );
496 $s .= "</div>";
497 $wgOut->addHTML( $s );
499 if( $descText ) {
500 $this->mExtraDescription = $descText;
504 public function getUploadUrl() {
505 $this->loadFile();
506 $uploadTitle = SpecialPage::getTitleFor( 'Upload' );
507 return $uploadTitle->getFullUrl( 'wpDestFile=' . urlencode( $this->img->getName() ) . '&wpForReUpload=1' );
511 * Print out the various links at the bottom of the image page, e.g. reupload,
512 * external editing (and instructions link) etc.
514 protected function uploadLinksBox() {
515 global $wgUser, $wgOut;
517 $this->loadFile();
518 if( !$this->img->isLocal() )
519 return;
521 $sk = $wgUser->getSkin();
523 $wgOut->addHTML( '<br /><ul>' );
525 # "Upload a new version of this file" link
526 if( UploadForm::userCanReUpload($wgUser,$this->img->name) ) {
527 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
528 $wgOut->addHTML( "<li><div class='plainlinks'>{$ulink}</div></li>" );
531 # External editing link
532 $elink = $sk->makeKnownLinkObj( $this->mTitle, wfMsgHtml( 'edit-externally' ), 'action=edit&externaledit=true&mode=file' );
533 $wgOut->addHTML( '<li>' . $elink . ' <small>' . wfMsgExt( 'edit-externally-help', array( 'parseinline' ) ) . '</small></li>' );
535 $wgOut->addHTML( '</ul>' );
538 protected function closeShowImage() {} # For overloading
541 * If the page we've just displayed is in the "Image" namespace,
542 * we follow it with an upload history of the image and its usage.
544 protected function imageHistory() {
545 global $wgOut, $wgUseExternalEditor;
547 $this->loadFile();
548 $pager = new ImageHistoryPseudoPager( $this );
549 $wgOut->addHTML( $pager->getBody() );
551 $this->img->resetHistory(); // free db resources
553 # Exist check because we don't want to show this on pages where an image
554 # doesn't exist along with the noimage message, that would suck. -ævar
555 if( $wgUseExternalEditor && $this->img->exists() ) {
556 $this->uploadLinksBox();
560 protected function imageLinks() {
561 global $wgUser, $wgOut, $wgLang;
563 $limit = 100;
565 $dbr = wfGetDB( DB_SLAVE );
567 $res = $dbr->select(
568 array( 'imagelinks', 'page' ),
569 array( 'page_namespace', 'page_title' ),
570 array( 'il_to' => $this->mTitle->getDBkey(), 'il_from = page_id' ),
571 __METHOD__,
572 array( 'LIMIT' => $limit + 1)
574 $count = $dbr->numRows( $res );
575 if( $count == 0 ) {
576 $wgOut->addHTML( "<div id='mw-imagepage-nolinkstoimage'>\n" );
577 $wgOut->addWikiMsg( 'nolinkstoimage' );
578 $wgOut->addHTML( "</div>\n" );
579 return;
582 $wgOut->addHTML( "<div id='mw-imagepage-section-linkstoimage'>\n" );
583 if( $count <= $limit - 1 ) {
584 $wgOut->addWikiMsg( 'linkstoimage', $count );
585 } else {
586 // More links than the limit. Add a link to [[Special:Whatlinkshere]]
587 $wgOut->addWikiMsg( 'linkstoimage-more',
588 $wgLang->formatNum( $limit ),
589 $this->mTitle->getPrefixedDBkey()
593 $wgOut->addHTML( "<ul class='mw-imagepage-linkstoimage'>\n" );
594 $sk = $wgUser->getSkin();
595 $count = 0;
596 while ( $s = $res->fetchObject() ) {
597 $count++;
598 if( $count <= $limit ) {
599 // We have not yet reached the extra one that tells us there is more to fetch
600 $name = Title::makeTitle( $s->page_namespace, $s->page_title );
601 $link = $sk->makeKnownLinkObj( $name, "" );
602 $wgOut->addHTML( "<li>{$link}</li>\n" );
605 $wgOut->addHTML( "</ul></div>\n" );
606 $res->free();
608 // Add a links to [[Special:Whatlinkshere]]
609 if( $count > $limit )
610 $wgOut->addWikiMsg( 'morelinkstoimage', $this->mTitle->getPrefixedDBkey() );
613 protected function imageRedirects() {
614 global $wgUser, $wgOut, $wgLang;
616 $redirects = $this->getTitle()->getRedirectsHere( NS_FILE );
617 if( count( $redirects ) == 0 ) return;
619 $wgOut->addHTML( "<div id='mw-imagepage-section-redirectstofile'>\n" );
620 $wgOut->addWikiMsg( 'redirectstofile',
621 $wgLang->formatNum( count( $redirects ) )
623 $wgOut->addHTML( "<ul class='mw-imagepage-redirectstofile'>\n" );
625 $sk = $wgUser->getSkin();
626 foreach ( $redirects as $title ) {
627 $link = $sk->makeKnownLinkObj( $title, "", "redirect=no" );
628 $wgOut->addHTML( "<li>{$link}</li>\n" );
630 $wgOut->addHTML( "</ul></div>\n" );
634 protected function imageDupes() {
635 global $wgOut, $wgUser, $wgLang;
637 $this->loadFile();
639 $dupes = $this->getDuplicates();
640 if( count( $dupes ) == 0 ) return;
642 $wgOut->addHTML( "<div id='mw-imagepage-section-duplicates'>\n" );
643 $wgOut->addWikiMsg( 'duplicatesoffile',
644 $wgLang->formatNum( count( $dupes ) ), $this->mTitle->getDBkey()
646 $wgOut->addHTML( "<ul class='mw-imagepage-duplicates'>\n" );
648 $sk = $wgUser->getSkin();
649 foreach ( $dupes as $file ) {
650 $fromSrc = '';
651 if( $file->isLocal() )
652 $link = $sk->makeKnownLinkObj( $file->getTitle(), "" );
653 else {
654 $link = $sk->makeExternalLink( $file->getDescriptionUrl(),
655 $file->getTitle()->getPrefixedText() );
656 $fromSrc = wfMsg( 'shared-repo-from', $file->getRepo()->getDisplayName() );
658 $wgOut->addHTML( "<li>{$link} {$fromSrc}</li>\n" );
660 $wgOut->addHTML( "</ul></div>\n" );
664 * Delete the file, or an earlier version of it
666 public function delete() {
667 $this->loadFile();
668 if( !$this->img->exists() || !$this->img->isLocal() || $this->img->getRedirected() ) {
669 // Standard article deletion
670 Article::delete();
671 return;
673 $deleter = new FileDeleteForm( $this->img );
674 $deleter->execute();
678 * Revert the file to an earlier version
680 public function revert() {
681 $this->loadFile();
682 $reverter = new FileRevertForm( $this->img );
683 $reverter->execute();
687 * Override handling of action=purge
689 public function doPurge() {
690 $this->loadFile();
691 if( $this->img->exists() ) {
692 wfDebug( "ImagePage::doPurge purging " . $this->img->getName() . "\n" );
693 $update = new HTMLCacheUpdate( $this->mTitle, 'imagelinks' );
694 $update->doUpdate();
695 $this->img->upgradeRow();
696 $this->img->purgeCache();
697 } else {
698 wfDebug( "ImagePage::doPurge no image\n" );
700 parent::doPurge();
704 * Display an error with a wikitext description
706 function showError( $description ) {
707 global $wgOut;
708 $wgOut->setPageTitle( wfMsg( "internalerror" ) );
709 $wgOut->setRobotPolicy( "noindex,nofollow" );
710 $wgOut->setArticleRelated( false );
711 $wgOut->enableClientCache( false );
712 $wgOut->addWikiText( $description );
718 * Builds the image revision log shown on image pages
720 * @ingroup Media
722 class ImageHistoryList {
724 protected $imagePage, $img, $skin, $title, $repo;
726 public function __construct( $imagePage ) {
727 global $wgUser;
728 $this->skin = $wgUser->getSkin();
729 $this->current = $imagePage->getFile();
730 $this->img = $imagePage->getDisplayedFile();
731 $this->title = $imagePage->getTitle();
732 $this->imagePage = $imagePage;
735 public function getImagePage() {
736 return $this->imagePage;
739 public function getSkin() {
740 return $this->skin;
743 public function getFile() {
744 return $this->img;
747 public function beginImageHistoryList( $navLinks = '' ) {
748 global $wgOut, $wgUser;
749 return Xml::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) )
750 . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
751 . $navLinks
752 . Xml::openElement( 'table', array( 'class' => 'filehistory' ) ) . "\n"
753 . '<tr><td></td>'
754 . ( $this->current->isLocal() && ($wgUser->isAllowed('delete') || $wgUser->isAllowed('deleterevision') ) ? '<td></td>' : '' )
755 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
756 . '<th>' . wfMsgHtml( 'filehist-thumb' ) . '</th>'
757 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
758 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
759 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
760 . "</tr>\n";
763 public function endImageHistoryList( $navLinks = '' ) {
764 return "</table>\n$navLinks\n";
767 public function imageHistoryLine( $iscur, $file ) {
768 global $wgUser, $wgLang, $wgContLang, $wgTitle;
770 $timestamp = wfTimestamp(TS_MW, $file->getTimestamp());
771 $img = $iscur ? $file->getName() : $file->getArchiveName();
772 $user = $file->getUser('id');
773 $usertext = $file->getUser('text');
774 $size = $file->getSize();
775 $description = $file->getDescription();
776 $dims = $file->getDimensionsString();
777 $sha1 = $file->getSha1();
779 $local = $this->current->isLocal();
780 $row = $css = $selected = '';
782 // Deletion link
783 if( $local && ($wgUser->isAllowed('delete') || $wgUser->isAllowed('deleterevision') ) ) {
784 $row .= '<td>';
785 # Link to remove from history
786 if( $wgUser->isAllowed( 'delete' ) ) {
787 $q = array();
788 $q[] = 'action=delete';
789 if( !$iscur )
790 $q[] = 'oldimage=' . urlencode( $img );
791 $row .= $this->skin->makeKnownLinkObj(
792 $this->title,
793 wfMsgHtml( $iscur ? 'filehist-deleteall' : 'filehist-deleteone' ),
794 implode( '&', $q )
797 # Link to hide content
798 if( $wgUser->isAllowed( 'deleterevision' ) ) {
799 if( $wgUser->isAllowed('delete') ) {
800 $row .= '<br/>';
802 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
803 // If file is top revision or locked from this user, don't link
804 if( $iscur || !$file->userCan(File::DELETED_RESTRICTED) ) {
805 $del = wfMsgHtml( 'rev-delundel' );
806 } else {
807 // If the file was hidden, link to sha-1
808 list($ts,$name) = explode('!',$img,2);
809 $del = $this->skin->makeKnownLinkObj( $revdel, wfMsg( 'rev-delundel' ),
810 'target=' . urlencode( $wgTitle->getPrefixedText() ) .
811 '&oldimage=' . urlencode( $ts ) );
812 // Bolden oversighted content
813 if( $file->isDeleted(File::DELETED_RESTRICTED) )
814 $del = "<strong>$del</strong>";
816 $row .= "<tt style='white-space: nowrap;'><small>$del</small></tt>";
818 $row .= '</td>';
821 // Reversion link/current indicator
822 $row .= '<td>';
823 if( $iscur ) {
824 $row .= wfMsgHtml( 'filehist-current' );
825 } elseif( $local && $wgUser->isLoggedIn() && $this->title->userCan( 'edit' ) ) {
826 if( $file->isDeleted(File::DELETED_FILE) ) {
827 $row .= wfMsgHtml('filehist-revert');
828 } else {
829 $q = array();
830 $q[] = 'action=revert';
831 $q[] = 'oldimage=' . urlencode( $img );
832 $q[] = 'wpEditToken=' . urlencode( $wgUser->editToken( $img ) );
833 $row .= $this->skin->makeKnownLinkObj( $this->title,
834 wfMsgHtml( 'filehist-revert' ),
835 implode( '&', $q ) );
838 $row .= '</td>';
840 // Date/time and image link
841 if( $file->getTimestamp() === $this->img->getTimestamp() ) {
842 $selected = "class='filehistory-selected'";
844 $row .= "<td $selected style='white-space: nowrap;'>";
845 if( !$file->userCan(File::DELETED_FILE) ) {
846 # Don't link to unviewable files
847 $row .= '<span class="history-deleted">' . $wgLang->timeAndDate( $timestamp, true ) . '</span>';
848 } else if( $file->isDeleted(File::DELETED_FILE) ) {
849 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
850 # Make a link to review the image
851 $url = $this->skin->makeKnownLinkObj( $revdel, $wgLang->timeAndDate( $timestamp, true ),
852 "target=".$wgTitle->getPrefixedText()."&file=$sha1.".$this->current->getExtension() );
853 $row .= '<span class="history-deleted">'.$url.'</span>';
854 } else {
855 $url = $iscur ? $this->current->getUrl() : $this->current->getArchiveUrl( $img );
856 $row .= Xml::element( 'a', array( 'href' => $url ), $wgLang->timeAndDate( $timestamp, true ) );
859 // Thumbnail
860 if( $file->allowInlineDisplay() && $file->userCan( File::DELETED_FILE ) && !$file->isDeleted( File::DELETED_FILE ) ) {
861 $params = array(
862 'width' => '120',
863 'height' => '120',
865 $thumbnail = $file->transform( $params );
866 $options = array(
867 'alt' => wfMsg( 'filehist-thumbtext', $wgLang->timeAndDate( $timestamp, true ) ),
868 'file-link' => true,
870 $row .= '</td><td>' . ( $thumbnail ? $thumbnail->toHtml( $options ) :
871 wfMsgHtml( 'filehist-nothumb' ) );
872 } else {
873 $row .= '</td><td>' . wfMsgHtml( 'filehist-nothumb' );
875 $row .= "</td><td>";
877 // Image dimensions
878 $row .= htmlspecialchars( $dims );
880 // File size
881 $row .= " <span style='white-space: nowrap;'>(" . $this->skin->formatSize( $size ) . ')</span>';
883 // Uploading user
884 $row .= '</td><td>';
885 if( $local ) {
886 // Hide deleted usernames
887 if( $file->isDeleted(File::DELETED_USER) ) {
888 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
889 } else {
890 $row .= $this->skin->userLink( $user, $usertext ) . " <span style='white-space: nowrap;'>" .
891 $this->skin->userToolLinks( $user, $usertext ) . "</span>";
893 } else {
894 $row .= htmlspecialchars( $usertext );
896 $row .= '</td><td>';
898 // Don't show deleted descriptions
899 if( $file->isDeleted(File::DELETED_COMMENT) ) {
900 $row .= '<span class="history-deleted">' . wfMsgHtml('rev-deleted-comment') . '</span>';
901 } else {
902 $row .= $this->skin->commentBlock( $description, $this->title );
904 $row .= '</td>';
906 wfRunHooks( 'ImagePageFileHistoryLine', array( $this, $file, &$row, &$rowClass ) );
907 $classAttr = $rowClass ? " class='$rowClass'" : "";
909 return "<tr{$classAttr}>{$row}</tr>\n";
913 class ImageHistoryPseudoPager extends ReverseChronologicalPager {
914 function __construct( $imagePage ) {
915 parent::__construct();
916 $this->mImagePage = $imagePage;
917 $this->mTitle = clone( $imagePage->getTitle() );
918 $this->mTitle->setFragment( '#filehistory' );
919 $this->mImg = NULL;
920 $this->mHist = array();
921 $this->mRange = array( 0, 0 ); // display range
924 function getTitle() {
925 return $this->mTitle;
928 function getQueryInfo() {
929 return false;
932 function getIndexField() {
933 return '';
936 function formatRow( $row ) {
937 return '';
940 function getBody() {
941 $s = '';
942 $this->doQuery();
943 if( count($this->mHist) ) {
944 $list = new ImageHistoryList( $this->mImagePage );
945 # Generate prev/next links
946 $navLink = $this->getNavigationBar();
947 $s = $list->beginImageHistoryList($navLink);
948 // Skip rows there just for paging links
949 for( $i = $this->mRange[0]; $i <= $this->mRange[1]; $i++ ) {
950 $file = $this->mHist[$i];
951 $s .= $list->imageHistoryLine( !$file->isOld(), $file );
953 $s .= $list->endImageHistoryList($navLink);
955 return $s;
958 function doQuery() {
959 if( $this->mQueryDone ) return;
960 $this->mImg = $this->mImagePage->getFile(); // ensure loading
961 if( !$this->mImg->exists() ) {
962 return;
964 $queryLimit = $this->mLimit + 1; // limit plus extra row
965 if( $this->mIsBackwards ) {
966 // Fetch the file history
967 $this->mHist = $this->mImg->getHistory($queryLimit,null,$this->mOffset,false);
968 // The current rev may not meet the offset/limit
969 $numRows = count($this->mHist);
970 if( $numRows <= $this->mLimit && $this->mImg->getTimestamp() > $this->mOffset ) {
971 $this->mHist = array_merge( array($this->mImg), $this->mHist );
973 } else {
974 // The current rev may not meet the offset
975 if( !$this->mOffset || $this->mImg->getTimestamp() < $this->mOffset ) {
976 $this->mHist[] = $this->mImg;
978 // Old image versions (fetch extra row for nav links)
979 $oiLimit = count($this->mHist) ? $this->mLimit : $this->mLimit+1;
980 // Fetch the file history
981 $this->mHist = array_merge( $this->mHist,
982 $this->mImg->getHistory($oiLimit,$this->mOffset,null,false) );
984 $numRows = count($this->mHist); // Total number of query results
985 if( $numRows ) {
986 # Index value of top item in the list
987 $firstIndex = $this->mIsBackwards ?
988 $this->mHist[$numRows-1]->getTimestamp() : $this->mHist[0]->getTimestamp();
989 # Discard the extra result row if there is one
990 if( $numRows > $this->mLimit && $numRows > 1 ) {
991 if( $this->mIsBackwards ) {
992 # Index value of item past the index
993 $this->mPastTheEndIndex = $this->mHist[0]->getTimestamp();
994 # Index value of bottom item in the list
995 $lastIndex = $this->mHist[1]->getTimestamp();
996 # Display range
997 $this->mRange = array( 1, $numRows-1 );
998 } else {
999 # Index value of item past the index
1000 $this->mPastTheEndIndex = $this->mHist[$numRows-1]->getTimestamp();
1001 # Index value of bottom item in the list
1002 $lastIndex = $this->mHist[$numRows-2]->getTimestamp();
1003 # Display range
1004 $this->mRange = array( 0, $numRows-2 );
1006 } else {
1007 # Setting indexes to an empty string means that they will be
1008 # omitted if they would otherwise appear in URLs. It just so
1009 # happens that this is the right thing to do in the standard
1010 # UI, in all the relevant cases.
1011 $this->mPastTheEndIndex = '';
1012 # Index value of bottom item in the list
1013 $lastIndex = $this->mIsBackwards ?
1014 $this->mHist[0]->getTimestamp() : $this->mHist[$numRows-1]->getTimestamp();
1015 # Display range
1016 $this->mRange = array( 0, $numRows-1 );
1018 } else {
1019 $firstIndex = '';
1020 $lastIndex = '';
1021 $this->mPastTheEndIndex = '';
1023 if( $this->mIsBackwards ) {
1024 $this->mIsFirst = ( $numRows < $queryLimit );
1025 $this->mIsLast = ( $this->mOffset == '' );
1026 $this->mLastShown = $firstIndex;
1027 $this->mFirstShown = $lastIndex;
1028 } else {
1029 $this->mIsFirst = ( $this->mOffset == '' );
1030 $this->mIsLast = ( $numRows < $queryLimit );
1031 $this->mLastShown = $lastIndex;
1032 $this->mFirstShown = $firstIndex;
1034 $this->mQueryDone = true;