3 if ( !defined( 'MEDIAWIKI' ) )
7 * Special handling for image description pages
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;
20 function __construct( $title ) {
21 parent
::__construct( $title );
30 public function setFile( $file ) {
31 $this->displayImg
= $file;
33 $this->fileLoaded
= true;
36 protected function loadFile() {
37 if ( $this->fileLoaded
) {
40 $this->fileLoaded
= true;
42 $this->displayImg
= $this->img
= false;
43 wfRunHooks( 'ImagePageFindFile', array( $this, &$this->img
, &$this->displayImg
) );
45 $this->img
= wfFindFile( $this->mTitle
);
47 $this->img
= wfLocalFile( $this->mTitle
);
50 if ( !$this->displayImg
) {
51 $this->displayImg
= $this->img
;
53 $this->repo
= $this->img
->getRepo();
57 * Handler for action=render
58 * Include body text only; none of the image extras
60 public function render() {
62 $wgOut->setArticleBodyOnly( true );
66 public function view() {
67 global $wgOut, $wgShowEXIF, $wgRequest, $wgUser;
69 $diff = $wgRequest->getVal( 'diff' );
70 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
72 if ( $this->mTitle
->getNamespace() != NS_FILE ||
( isset( $diff ) && $diffOnly ) ) {
73 return parent
::view();
78 if ( $this->mTitle
->getNamespace() == NS_FILE
&& $this->img
->getRedirected() ) {
79 if ( $this->mTitle
->getDBkey() == $this->img
->getName() ||
isset( $diff ) ) {
80 // mTitle is the same as the redirect target so ask Article
81 // to perform the redirect for us.
82 $wgRequest->setVal( 'diffonly', 'true' );
83 return parent
::view();
85 // mTitle is not the same as the redirect target so it is
86 // probably the redirect page itself. Fake the redirect symbol
87 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
88 $wgOut->addHTML( $this->viewRedirect( Title
::makeTitle( NS_FILE
, $this->img
->getName() ),
89 /* $appendSubtitle */ true, /* $forceKnown */ true ) );
95 $this->showRedirectedFromHeader();
97 if ( $wgShowEXIF && $this->displayImg
->exists() ) {
98 // FIXME: bad interface, see note on MediaHandler::formatMetadata().
99 $formattedMetadata = $this->displayImg
->formatMetadata();
100 $showmeta = $formattedMetadata !== false;
105 if ( !$diff && $this->displayImg
->exists() )
106 $wgOut->addHTML( $this->showTOC( $showmeta ) );
109 $this->openShowImage();
111 # No need to display noarticletext, we use our own message, output in openShowImage()
112 if ( $this->getID() ) {
115 # Just need to set the right headers
116 $wgOut->setArticleFlag( true );
117 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
118 $this->viewUpdates();
121 # Show shared description, if needed
122 if ( $this->mExtraDescription
) {
123 $fol = wfMsgNoTrans( 'shareddescriptionfollows' );
124 if ( $fol != '-' && !wfEmptyMsg( 'shareddescriptionfollows', $fol ) ) {
125 $wgOut->addWikiText( $fol );
127 $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription
. "</div>\n" );
130 $this->closeShowImage();
131 $this->imageHistory();
132 // TODO: Cleanup the following
134 $wgOut->addHTML( Xml
::element( 'h2',
135 array( 'id' => 'filelinks' ),
136 wfMsg( 'imagelinks' ) ) . "\n" );
138 # TODO! FIXME! For some freaky reason, we can't redirect to foreign images.
139 # Yet we return metadata about the target. Definitely an issue in the FileRepo
140 $this->imageRedirects();
143 # Allow extensions to add something after the image links
145 wfRunHooks( 'ImagePageAfterImageLinks', array( $this, &$html ) );
147 $wgOut->addHTML( $html );
150 $wgOut->addHTML( Xml
::element( 'h2', array( 'id' => 'metadata' ), wfMsg( 'metadata' ) ) . "\n" );
151 $wgOut->addWikiText( $this->makeMetadataTable( $formattedMetadata ) );
152 $wgOut->addModules( array( 'mediawiki.legacy.metadata' ) );
155 $css = $this->repo
->getDescriptionStylesheetUrl();
157 $wgOut->addStyle( $css );
161 public function getRedirectTarget() {
163 if ( $this->img
->isLocal() ) {
164 return parent
::getRedirectTarget();
166 // Foreign image page
167 $from = $this->img
->getRedirected();
168 $to = $this->img
->getName();
169 if ( $from == $to ) {
172 return $this->mRedirectTarget
= Title
::makeTitle( NS_FILE
, $to );
174 public function followRedirect() {
176 if ( $this->img
->isLocal() ) {
177 return parent
::followRedirect();
179 $from = $this->img
->getRedirected();
180 $to = $this->img
->getName();
181 if ( $from == $to ) {
184 return Title
::makeTitle( NS_FILE
, $to );
186 public function isRedirect( $text = false ) {
188 if ( $this->img
->isLocal() )
189 return parent
::isRedirect( $text );
191 return (bool)$this->img
->getRedirected();
194 public function isLocal() {
196 return $this->img
->isLocal();
199 public function getFile() {
204 public function getDisplayedFile() {
206 return $this->displayImg
;
209 public function getDuplicates() {
211 if ( !is_null( $this->dupes
) ) {
214 if ( !( $hash = $this->img
->getSha1() ) ) {
215 return $this->dupes
= array();
217 $dupes = RepoGroup
::singleton()->findBySha1( $hash );
218 // Remove duplicates with self and non matching file sizes
219 $self = $this->img
->getRepoName() . ':' . $this->img
->getName();
220 $size = $this->img
->getSize();
221 foreach ( $dupes as $index => $file ) {
222 $key = $file->getRepoName() . ':' . $file->getName();
224 unset( $dupes[$index] );
225 if ( $file->getSize() != $size )
226 unset( $dupes[$index] );
228 return $this->dupes
= $dupes;
236 * @param $metadata Boolean: whether or not to show the metadata link
239 protected function showTOC( $metadata ) {
241 '<li><a href="#file">' . wfMsgHtml( 'file-anchor-link' ) . '</a></li>',
242 '<li><a href="#filehistory">' . wfMsgHtml( 'filehist' ) . '</a></li>',
243 '<li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>',
246 $r[] = '<li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>';
249 wfRunHooks( 'ImagePageShowTOC', array( $this, &$r ) );
251 return '<ul id="filetoc">' . implode( "\n", $r ) . '</ul>';
255 * Make a table with metadata to be shown in the output page.
257 * FIXME: bad interface, see note on MediaHandler::formatMetadata().
259 * @param $metadata Array: the array containing the EXIF data
262 protected function makeMetadataTable( $metadata ) {
263 $r = "<div class=\"mw-imagepage-section-metadata\">";
264 $r .= wfMsgNoTrans( 'metadata-help' );
265 $r .= "<table id=\"mw_metadata\" class=\"mw_metadata\">\n";
266 foreach ( $metadata as $type => $stuff ) {
267 foreach ( $stuff as $v ) {
268 # FIXME, why is this using escapeId for a class?!
269 $class = Sanitizer
::escapeId( $v['id'] );
270 if ( $type == 'collapsed' ) {
271 $class .= ' collapsable';
273 $r .= "<tr class=\"$class\">\n";
274 $r .= "<th>{$v['name']}</th>\n";
275 $r .= "<td>{$v['value']}</td>\n</tr>";
278 $r .= "</table>\n</div>\n";
283 * Overloading Article's getContent method.
285 * Omit noarticletext if sharedupload; text will be fetched from the
286 * shared upload server if possible.
288 public function getContent() {
290 if ( $this->img
&& !$this->img
->isLocal() && 0 == $this->getID() ) {
293 return Article
::getContent();
296 protected function openShowImage() {
297 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
298 $wgLang, $wgContLang, $wgEnableUploads;
302 $full_url = $this->displayImg
->getURL();
303 $sizeSel = intval( $wgUser->getOption( 'imagesize' ) );
304 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
305 $sizeSel = User
::getDefaultOption( 'imagesize' );
307 // The user offset might still be incorrect, specially if
308 // $wgImageLimits got changed (see bug #8858).
309 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
310 // Default to the first offset in $wgImageLimits
314 $max = $wgImageLimits[$sizeSel];
316 $maxHeight = $max[1];
317 $sk = $wgUser->getSkin();
318 $dirmark = $wgContLang->getDirMark();
320 if ( $this->displayImg
->exists() ) {
322 $page = $wgRequest->getIntOrNull( 'page' );
323 if ( is_null( $page ) ) {
327 $params = array( 'page' => $page );
329 $width_orig = $this->displayImg
->getWidth( $page );
330 $width = $width_orig;
331 $height_orig = $this->displayImg
->getHeight( $page );
332 $height = $height_orig;
334 $longDesc = $this->displayImg
->getLongDesc();
336 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this, &$wgOut ) );
338 if ( $this->displayImg
->allowInlineDisplay() ) {
341 # "Download high res version" link below the image
342 # $msgsize = wfMsgHtml('file-info-size', $width_orig, $height_orig, $sk->formatSize( $this->displayImg->getSize() ), $mime );
343 # We'll show a thumbnail of this image
344 if ( $width > $maxWidth ||
$height > $maxHeight ) {
345 # Calculate the thumbnail size.
346 # First case, the limiting factor is the width, not the height.
347 if ( $width / $height >= $maxWidth / $maxHeight ) {
348 $height = round( $height * $maxWidth / $width );
350 # Note that $height <= $maxHeight now.
352 $newwidth = floor( $width * $maxHeight / $height );
353 $height = round( $height * $newwidth / $width );
355 # Note that $height <= $maxHeight now, but might not be identical
356 # because of rounding.
358 $msgbig = wfMsgHtml( 'show-big-image' );
359 $msgsmall = wfMsgExt( 'show-big-image-thumb', 'parseinline',
360 $wgLang->formatNum( $width ),
361 $wgLang->formatNum( $height )
364 # Image is small enough to show full size on image page
365 $msgsmall = wfMsgExt( 'file-nohires', array( 'parseinline' ) );
368 $params['width'] = $width;
369 $thumbnail = $this->displayImg
->transform( $params );
373 if ( !$this->displayImg
->mustRender() ) {
374 $anchorclose = "<br />" . $msgsmall;
377 $isMulti = $this->displayImg
->isMultipage() && $this->displayImg
->pageCount() > 1;
379 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
384 'alt' => $this->displayImg
->getTitle()->getPrefixedText(),
387 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
388 $thumbnail->toHtml( $options ) .
389 $anchorclose . "</div>\n" );
393 $count = $this->displayImg
->pageCount();
396 $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
401 array( 'page' => $page - 1 ),
402 array( 'known', 'noclasses' )
404 $thumb1 = $sk->makeThumbLinkObj( $this->mTitle
, $this->displayImg
, $link, $label, 'none',
405 array( 'page' => $page - 1 ) );
410 if ( $page < $count ) {
411 $label = wfMsg( 'imgmultipagenext' );
416 array( 'page' => $page +
1 ),
417 array( 'known', 'noclasses' )
419 $thumb2 = $sk->makeThumbLinkObj( $this->mTitle
, $this->displayImg
, $link, $label, 'none',
420 array( 'page' => $page +
1 ) );
428 'name' => 'pageselector',
429 'action' => $wgScript,
430 'onchange' => 'document.pageselector.submit();',
433 for ( $i = 1; $i <= $count; $i++
) {
434 $options[] = Xml
::option( $wgLang->formatNum( $i ), $i, $i == $page );
436 $select = Xml
::tags( 'select',
437 array( 'id' => 'pageselector', 'name' => 'page' ),
438 implode( "\n", $options ) );
441 '</td><td><div class="multipageimagenavbox">' .
442 Xml
::openElement( 'form', $formParams ) .
443 Html
::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) .
444 wfMsgExt( 'imgmultigoto', array( 'parseinline', 'replaceafter' ), $select ) .
445 Xml
::submitButton( wfMsg( 'imgmultigo' ) ) .
446 Xml
::closeElement( 'form' ) .
447 "<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>"
451 # if direct link is allowed but it's not a renderable image, show an icon.
452 if ( $this->displayImg
->isSafeFile() ) {
453 $icon = $this->displayImg
->iconThumb();
455 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
456 $icon->toHtml( array( 'file-link' => true ) ) .
465 $filename = wfEscapeWikiText( $this->displayImg
->getName() );
466 $linktext = $filename;
467 if ( isset( $msgbig ) ) {
468 $linktext = wfEscapeWikiText( $msgbig );
470 $medialink = "[[Media:$filename|$linktext]]";
472 if ( !$this->displayImg
->isSafeFile() ) {
473 $warning = wfMsgNoTrans( 'mediawarning' );
474 $wgOut->addWikiText( <<<EOT
475 <div class="fullMedia"><span class="dangerousLink">{$medialink}</span>$dirmark <span class="fileInfo">$longDesc</span></div>
476 <div class="mediaWarning">$warning</div>
480 $wgOut->addWikiText( <<<EOT
481 <div class="fullMedia">{$medialink}{$dirmark} <span class="fileInfo">$longDesc</span>
488 if ( !$this->displayImg
->isLocal() ) {
489 $this->printSharedImageText();
492 # Image does not exist
493 if ( $wgEnableUploads && $wgUser->isAllowed( 'upload' ) ) {
494 // Only show an upload link if the user can upload
495 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
497 'filepage-nofile-link',
498 $uploadTitle->getFullUrl( array( 'wpDestFile' => $this->img
->getName() ) )
503 $nofile = 'filepage-nofile';
505 $wgOut->setRobotPolicy( 'noindex,nofollow' );
506 $wgOut->wrapWikiMsg( "<div id='mw-imagepage-nofile' class='plainlinks'>\n$1\n</div>", $nofile );
511 * Show a notice that the file is from a shared repository
513 protected function printSharedImageText() {
518 $descUrl = $this->img
->getDescriptionUrl();
519 $descText = $this->img
->getDescriptionText();
521 /* Add canonical to head if there is no local page for this shared file */
522 if( $descUrl && $this->getID() == 0 ) {
523 $wgOut->addLink( array( 'rel' => 'canonical', 'href' => $descUrl ) );
526 $wrap = "<div class=\"sharedUploadNotice\">\n$1\n</div>\n";
527 $repo = $this->img
->getRepo()->getDisplayName();
529 if ( $descUrl && $descText && wfMsgNoTrans( 'sharedupload-desc-here' ) !== '-' ) {
530 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-here', $repo, $descUrl ) );
531 } elseif ( $descUrl && wfMsgNoTrans( 'sharedupload-desc-there' ) !== '-' ) {
532 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-there', $repo, $descUrl ) );
534 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload', $repo ), ''/*BACKCOMPAT*/ );
538 $this->mExtraDescription
= $descText;
542 public function getUploadUrl() {
544 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
545 return $uploadTitle->getFullUrl( array(
546 'wpDestFile' => $this->img
->getName(),
552 * Print out the various links at the bottom of the image page, e.g. reupload,
553 * external editing (and instructions link) etc.
555 protected function uploadLinksBox() {
556 global $wgUser, $wgOut, $wgEnableUploads, $wgUseExternalEditor;
558 if ( !$wgEnableUploads ) { return; }
561 if ( !$this->img
->isLocal() )
564 $sk = $wgUser->getSkin();
566 $wgOut->addHTML( "<br /><ul>\n" );
568 # "Upload a new version of this file" link
569 if ( UploadBase
::userCanReUpload( $wgUser, $this->img
->name
) ) {
570 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
571 $wgOut->addHTML( "<li id=\"mw-imagepage-reupload-link\"><div class=\"plainlinks\">{$ulink}</div></li>\n" );
574 # External editing link
575 if ( $wgUseExternalEditor ) {
578 wfMsgHtml( 'edit-externally' ),
582 'externaledit' => 'true',
585 array( 'known', 'noclasses' )
587 $wgOut->addHTML( '<li id="mw-imagepage-edit-external">' . $elink . ' <small>' . wfMsgExt( 'edit-externally-help', array( 'parseinline' ) ) . "</small></li>\n" );
590 $wgOut->addHTML( "</ul>\n" );
593 protected function closeShowImage() { } # For overloading
596 * If the page we've just displayed is in the "Image" namespace,
597 * we follow it with an upload history of the image and its usage.
599 protected function imageHistory() {
603 $pager = new ImageHistoryPseudoPager( $this );
604 $wgOut->addHTML( $pager->getBody() );
606 $this->img
->resetHistory(); // free db resources
608 # Exist check because we don't want to show this on pages where an image
609 # doesn't exist along with the noimage message, that would suck. -ævar
610 if ( $this->img
->exists() ) {
611 $this->uploadLinksBox();
615 protected function imageLinks() {
616 global $wgUser, $wgOut, $wgLang;
620 $dbr = wfGetDB( DB_SLAVE
);
623 array( 'imagelinks', 'page' ),
624 array( 'page_namespace', 'page_title' ),
625 array( 'il_to' => $this->mTitle
->getDBkey(), 'il_from = page_id' ),
627 array( 'LIMIT' => $limit +
1 )
629 $count = $dbr->numRows( $res );
631 $wgOut->wrapWikiMsg( Html
::rawElement( 'div', array ( 'id' => 'mw-imagepage-nolinkstoimage' ), "\n$1\n" ), 'nolinkstoimage' );
635 $wgOut->addHTML( "<div id='mw-imagepage-section-linkstoimage'>\n" );
636 if ( $count <= $limit - 1 ) {
637 $wgOut->addWikiMsg( 'linkstoimage', $count );
639 // More links than the limit. Add a link to [[Special:Whatlinkshere]]
640 $wgOut->addWikiMsg( 'linkstoimage-more',
641 $wgLang->formatNum( $limit ),
642 $this->mTitle
->getPrefixedDBkey()
646 $wgOut->addHTML( Html
::openElement( 'ul', array( 'class' => 'mw-imagepage-linkstoimage' ) ) . "\n" );
647 $sk = $wgUser->getSkin();
650 foreach ( $res as $s ) {
652 if ( $count <= $limit ) {
653 // We have not yet reached the extra one that tells us there is more to fetch
658 // Sort the list by namespace:title
659 usort ( $elements, array( $this, 'compare' ) );
661 // Create links for every element
662 foreach( $elements as $element ) {
663 $link = $sk->linkKnown( Title
::makeTitle( $element->page_namespace
, $element->page_title
) );
664 $wgOut->addHTML( Html
::rawElement(
666 array( 'id' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace
),
672 $wgOut->addHTML( Html
::closeElement( 'ul' ) . "\n" );
675 // Add a links to [[Special:Whatlinkshere]]
676 if ( $count > $limit ) {
677 $wgOut->addWikiMsg( 'morelinkstoimage', $this->mTitle
->getPrefixedDBkey() );
679 $wgOut->addHTML( Html
::closeElement( 'div' ) . "\n" );
682 protected function imageRedirects() {
683 global $wgUser, $wgOut, $wgLang;
685 $redirects = $this->getTitle()->getRedirectsHere( NS_FILE
);
686 if ( count( $redirects ) == 0 ) return;
688 $wgOut->addHTML( "<div id='mw-imagepage-section-redirectstofile'>\n" );
689 $wgOut->addWikiMsg( 'redirectstofile',
690 $wgLang->formatNum( count( $redirects ) )
692 $wgOut->addHTML( "<ul class='mw-imagepage-redirectstofile'>\n" );
694 $sk = $wgUser->getSkin();
695 foreach ( $redirects as $title ) {
700 array( 'redirect' => 'no' ),
701 array( 'known', 'noclasses' )
703 $wgOut->addHTML( "<li>{$link}</li>\n" );
705 $wgOut->addHTML( "</ul></div>\n" );
709 protected function imageDupes() {
710 global $wgOut, $wgUser, $wgLang;
714 $dupes = $this->getDuplicates();
715 if ( count( $dupes ) == 0 ) return;
717 $wgOut->addHTML( "<div id='mw-imagepage-section-duplicates'>\n" );
718 $wgOut->addWikiMsg( 'duplicatesoffile',
719 $wgLang->formatNum( count( $dupes ) ), $this->mTitle
->getDBkey()
721 $wgOut->addHTML( "<ul class='mw-imagepage-duplicates'>\n" );
723 $sk = $wgUser->getSkin();
724 foreach ( $dupes as $file ) {
726 if ( $file->isLocal() ) {
732 array( 'known', 'noclasses' )
735 $link = $sk->makeExternalLink( $file->getDescriptionUrl(),
736 $file->getTitle()->getPrefixedText() );
737 $fromSrc = wfMsg( 'shared-repo-from', $file->getRepo()->getDisplayName() );
739 $wgOut->addHTML( "<li>{$link} {$fromSrc}</li>\n" );
741 $wgOut->addHTML( "</ul></div>\n" );
745 * Delete the file, or an earlier version of it
747 public function delete() {
748 global $wgUploadMaintenance;
749 if ( $wgUploadMaintenance && $this->mTitle
&& $this->mTitle
->getNamespace() == NS_FILE
) {
751 $wgOut->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n", array( 'filedelete-maintenance' ) );
756 if ( !$this->img
->exists() ||
!$this->img
->isLocal() ||
$this->img
->getRedirected() ) {
757 // Standard article deletion
761 $deleter = new FileDeleteForm( $this->img
);
766 * Revert the file to an earlier version
768 public function revert() {
770 $reverter = new FileRevertForm( $this->img
);
771 $reverter->execute();
775 * Override handling of action=purge
777 public function doPurge() {
779 if ( $this->img
->exists() ) {
780 wfDebug( "ImagePage::doPurge purging " . $this->img
->getName() . "\n" );
781 $update = new HTMLCacheUpdate( $this->mTitle
, 'imagelinks' );
783 $this->img
->upgradeRow();
784 $this->img
->purgeCache();
786 wfDebug( "ImagePage::doPurge no image for " . $this->img
->getName() . "; limiting purge to cache only\n" );
787 // even if the file supposedly doesn't exist, force any cached information
788 // to be updated (in case the cached information is wrong)
789 $this->img
->purgeCache();
795 * Display an error with a wikitext description
797 function showError( $description ) {
799 $wgOut->setPageTitle( wfMsg( "internalerror" ) );
800 $wgOut->setRobotPolicy( "noindex,nofollow" );
801 $wgOut->setArticleRelated( false );
802 $wgOut->enableClientCache( false );
803 $wgOut->addWikiText( $description );
808 * Callback for usort() to do link sorts by (namespace, title)
809 * Function copied from Title::compare()
811 * @param $a object page to compare with
812 * @param $b object page to compare with
813 * @return Integer: result of string comparison, or namespace comparison
815 protected function compare( $a, $b ) {
816 if ( $a->page_namespace
== $b->page_namespace
) {
817 return strcmp( $a->page_title
, $b->page_title
);
819 return $a->page_namespace
- $b->page_namespace
;
825 * Builds the image revision log shown on image pages
829 class ImageHistoryList
{
831 protected $imagePage, $img, $skin, $title, $repo, $showThumb;
833 public function __construct( $imagePage ) {
834 global $wgUser, $wgShowArchiveThumbnails;
835 $this->skin
= $wgUser->getSkin();
836 $this->current
= $imagePage->getFile();
837 $this->img
= $imagePage->getDisplayedFile();
838 $this->title
= $imagePage->getTitle();
839 $this->imagePage
= $imagePage;
840 $this->showThumb
= $wgShowArchiveThumbnails && $this->img
->canRender();
843 public function getImagePage() {
844 return $this->imagePage
;
847 public function getSkin() {
851 public function getFile() {
855 public function beginImageHistoryList( $navLinks = '' ) {
856 global $wgOut, $wgUser;
857 return Xml
::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) ) . "\n"
858 . "<div id=\"mw-imagepage-section-filehistory\">\n"
859 . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
861 . Xml
::openElement( 'table', array( 'class' => 'wikitable filehistory' ) ) . "\n"
863 . ( $this->current
->isLocal() && ( $wgUser->isAllowed( 'delete' ) ||
$wgUser->isAllowed( 'deletedhistory' ) ) ?
'<td></td>' : '' )
864 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
865 . ( $this->showThumb ?
'<th>' . wfMsgHtml( 'filehist-thumb' ) . '</th>' : '' )
866 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
867 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
868 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
872 public function endImageHistoryList( $navLinks = '' ) {
873 return "</table>\n$navLinks\n</div>\n";
876 public function imageHistoryLine( $iscur, $file ) {
877 global $wgUser, $wgLang;
879 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
880 $img = $iscur ?
$file->getName() : $file->getArchiveName();
881 $user = $file->getUser( 'id' );
882 $usertext = $file->getUser( 'text' );
883 $description = $file->getDescription();
885 $local = $this->current
->isLocal();
886 $row = $selected = '';
889 if ( $local && ( $wgUser->isAllowed( 'delete' ) ||
$wgUser->isAllowed( 'deletedhistory' ) ) ) {
891 # Link to remove from history
892 if ( $wgUser->isAllowed( 'delete' ) ) {
893 $q = array( 'action' => 'delete' );
895 $q['oldimage'] = $img;
896 $row .= $this->skin
->link(
898 wfMsgHtml( $iscur ?
'filehist-deleteall' : 'filehist-deleteone' ),
899 array(), $q, array( 'known' )
902 # Link to hide content. Don't show useless link to people who cannot hide revisions.
903 $canHide = $wgUser->isAllowed( 'deleterevision' );
904 if ( $canHide ||
( $wgUser->isAllowed( 'deletedhistory' ) && $file->getVisibility() ) ) {
905 if ( $wgUser->isAllowed( 'delete' ) ) {
908 // If file is top revision or locked from this user, don't link
909 if ( $iscur ||
!$file->userCan( File
::DELETED_RESTRICTED
) ) {
910 $del = $this->skin
->revDeleteLinkDisabled( $canHide );
912 list( $ts, $name ) = explode( '!', $img, 2 );
914 'type' => 'oldimage',
915 'target' => $this->title
->getPrefixedText(),
918 $del = $this->skin
->revDeleteLink( $query,
919 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
926 // Reversion link/current indicator
929 $row .= wfMsgHtml( 'filehist-current' );
930 } elseif ( $local && $wgUser->isLoggedIn() && $this->title
->userCan( 'edit' ) ) {
931 if ( $file->isDeleted( File
::DELETED_FILE
) ) {
932 $row .= wfMsgHtml( 'filehist-revert' );
934 $row .= $this->skin
->link(
936 wfMsgHtml( 'filehist-revert' ),
939 'action' => 'revert',
941 'wpEditToken' => $wgUser->editToken( $img )
943 array( 'known', 'noclasses' )
949 // Date/time and image link
950 if ( $file->getTimestamp() === $this->img
->getTimestamp() ) {
951 $selected = "class='filehistory-selected'";
953 $row .= "<td $selected style='white-space: nowrap;'>";
954 if ( !$file->userCan( File
::DELETED_FILE
) ) {
955 # Don't link to unviewable files
956 $row .= '<span class="history-deleted">' . $wgLang->timeAndDate( $timestamp, true ) . '</span>';
957 } elseif ( $file->isDeleted( File
::DELETED_FILE
) ) {
958 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
959 # Make a link to review the image
960 $url = $this->skin
->link(
962 $wgLang->timeAndDate( $timestamp, true ),
965 'target' => $this->title
->getPrefixedText(),
967 'token' => $wgUser->editToken( $img )
969 array( 'known', 'noclasses' )
971 $row .= '<span class="history-deleted">' . $url . '</span>';
973 $url = $iscur ?
$this->current
->getUrl() : $this->current
->getArchiveUrl( $img );
974 $row .= Xml
::element( 'a', array( 'href' => $url ), $wgLang->timeAndDate( $timestamp, true ) );
979 if ( $this->showThumb
) {
980 $row .= '<td>' . $this->getThumbForLine( $file ) . '</td>';
983 // Image dimensions + size
985 $row .= htmlspecialchars( $file->getDimensionsString() );
986 $row .= " <span style='white-space: nowrap;'>(" . $this->skin
->formatSize( $file->getSize() ) . ')</span>';
992 // Hide deleted usernames
993 if ( $file->isDeleted( File
::DELETED_USER
) ) {
994 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
996 $row .= $this->skin
->userLink( $user, $usertext ) . " <span style='white-space: nowrap;'>" .
997 $this->skin
->userToolLinks( $user, $usertext ) . "</span>";
1000 $row .= htmlspecialchars( $usertext );
1002 $row .= '</td><td>';
1004 // Don't show deleted descriptions
1005 if ( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1006 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span>';
1008 $row .= $this->skin
->commentBlock( $description, $this->title
);
1013 wfRunHooks( 'ImagePageFileHistoryLine', array( $this, $file, &$row, &$rowClass ) );
1014 $classAttr = $rowClass ?
" class='$rowClass'" : "";
1016 return "<tr{$classAttr}>{$row}</tr>\n";
1019 protected function getThumbForLine( $file ) {
1022 if ( $file->allowInlineDisplay() && $file->userCan( File
::DELETED_FILE
) && !$file->isDeleted( File
::DELETED_FILE
) ) {
1027 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
1029 $thumbnail = $file->transform( $params );
1031 'alt' => wfMsg( 'filehist-thumbtext',
1032 $wgLang->timeAndDate( $timestamp, true ),
1033 $wgLang->date( $timestamp, true ),
1034 $wgLang->time( $timestamp, true ) ),
1035 'file-link' => true,
1038 if ( !$thumbnail ) return wfMsgHtml( 'filehist-nothumb' );
1040 return $thumbnail->toHtml( $options );
1042 return wfMsgHtml( 'filehist-nothumb' );
1047 class ImageHistoryPseudoPager
extends ReverseChronologicalPager
{
1048 function __construct( $imagePage ) {
1049 parent
::__construct();
1050 $this->mImagePage
= $imagePage;
1051 $this->mTitle
= clone ( $imagePage->getTitle() );
1052 $this->mTitle
->setFragment( '#filehistory' );
1054 $this->mHist
= array();
1055 $this->mRange
= array( 0, 0 ); // display range
1058 function getTitle() {
1059 return $this->mTitle
;
1062 function getQueryInfo() {
1066 function getIndexField() {
1070 function formatRow( $row ) {
1074 function getBody() {
1077 if ( count( $this->mHist
) ) {
1078 $list = new ImageHistoryList( $this->mImagePage
);
1079 # Generate prev/next links
1080 $navLink = $this->getNavigationBar();
1081 $s = $list->beginImageHistoryList( $navLink );
1082 // Skip rows there just for paging links
1083 for ( $i = $this->mRange
[0]; $i <= $this->mRange
[1]; $i++
) {
1084 $file = $this->mHist
[$i];
1085 $s .= $list->imageHistoryLine( !$file->isOld(), $file );
1087 $s .= $list->endImageHistoryList( $navLink );
1092 function doQuery() {
1093 if ( $this->mQueryDone
) return;
1094 $this->mImg
= $this->mImagePage
->getFile(); // ensure loading
1095 if ( !$this->mImg
->exists() ) {
1098 $queryLimit = $this->mLimit +
1; // limit plus extra row
1099 if ( $this->mIsBackwards
) {
1100 // Fetch the file history
1101 $this->mHist
= $this->mImg
->getHistory( $queryLimit, null, $this->mOffset
, false );
1102 // The current rev may not meet the offset/limit
1103 $numRows = count( $this->mHist
);
1104 if ( $numRows <= $this->mLimit
&& $this->mImg
->getTimestamp() > $this->mOffset
) {
1105 $this->mHist
= array_merge( array( $this->mImg
), $this->mHist
);
1108 // The current rev may not meet the offset
1109 if ( !$this->mOffset ||
$this->mImg
->getTimestamp() < $this->mOffset
) {
1110 $this->mHist
[] = $this->mImg
;
1112 // Old image versions (fetch extra row for nav links)
1113 $oiLimit = count( $this->mHist
) ?
$this->mLimit
: $this->mLimit +
1;
1114 // Fetch the file history
1115 $this->mHist
= array_merge( $this->mHist
,
1116 $this->mImg
->getHistory( $oiLimit, $this->mOffset
, null, false ) );
1118 $numRows = count( $this->mHist
); // Total number of query results
1120 # Index value of top item in the list
1121 $firstIndex = $this->mIsBackwards ?
1122 $this->mHist
[$numRows - 1]->getTimestamp() : $this->mHist
[0]->getTimestamp();
1123 # Discard the extra result row if there is one
1124 if ( $numRows > $this->mLimit
&& $numRows > 1 ) {
1125 if ( $this->mIsBackwards
) {
1126 # Index value of item past the index
1127 $this->mPastTheEndIndex
= $this->mHist
[0]->getTimestamp();
1128 # Index value of bottom item in the list
1129 $lastIndex = $this->mHist
[1]->getTimestamp();
1131 $this->mRange
= array( 1, $numRows - 1 );
1133 # Index value of item past the index
1134 $this->mPastTheEndIndex
= $this->mHist
[$numRows - 1]->getTimestamp();
1135 # Index value of bottom item in the list
1136 $lastIndex = $this->mHist
[$numRows - 2]->getTimestamp();
1138 $this->mRange
= array( 0, $numRows - 2 );
1141 # Setting indexes to an empty string means that they will be
1142 # omitted if they would otherwise appear in URLs. It just so
1143 # happens that this is the right thing to do in the standard
1144 # UI, in all the relevant cases.
1145 $this->mPastTheEndIndex
= '';
1146 # Index value of bottom item in the list
1147 $lastIndex = $this->mIsBackwards ?
1148 $this->mHist
[0]->getTimestamp() : $this->mHist
[$numRows - 1]->getTimestamp();
1150 $this->mRange
= array( 0, $numRows - 1 );
1155 $this->mPastTheEndIndex
= '';
1157 if ( $this->mIsBackwards
) {
1158 $this->mIsFirst
= ( $numRows < $queryLimit );
1159 $this->mIsLast
= ( $this->mOffset
== '' );
1160 $this->mLastShown
= $firstIndex;
1161 $this->mFirstShown
= $lastIndex;
1163 $this->mIsFirst
= ( $this->mOffset
== '' );
1164 $this->mIsLast
= ( $numRows < $queryLimit );
1165 $this->mLastShown
= $lastIndex;
1166 $this->mFirstShown
= $firstIndex;
1168 $this->mQueryDone
= true;