4 * Special handling for image description pages
8 class ImagePage
extends Article
{
10 /* private */ var $img; // Image object
11 /* private */ var $displayImg;
12 /* private */ var $repo;
13 /* private */ var $fileLoaded;
14 var $mExtraDescription = false;
17 function __construct( $title ) {
18 parent
::__construct( $title );
27 public function setFile( $file ) {
28 $this->displayImg
= $file;
30 $this->fileLoaded
= true;
33 protected function loadFile() {
34 if ( $this->fileLoaded
) {
37 $this->fileLoaded
= true;
39 $this->displayImg
= $this->img
= false;
40 wfRunHooks( 'ImagePageFindFile', array( $this, &$this->img
, &$this->displayImg
) );
42 $this->img
= wfFindFile( $this->mTitle
);
44 $this->img
= wfLocalFile( $this->mTitle
);
47 if ( !$this->displayImg
) {
48 $this->displayImg
= $this->img
;
50 $this->repo
= $this->img
->getRepo();
54 * Handler for action=render
55 * Include body text only; none of the image extras
57 public function render() {
59 $wgOut->setArticleBodyOnly( true );
63 public function view() {
64 global $wgOut, $wgShowEXIF, $wgRequest, $wgUser;
66 $diff = $wgRequest->getVal( 'diff' );
67 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
69 if ( $this->mTitle
->getNamespace() != NS_FILE ||
( isset( $diff ) && $diffOnly ) ) {
70 return parent
::view();
75 if ( $this->mTitle
->getNamespace() == NS_FILE
&& $this->img
->getRedirected() ) {
76 if ( $this->mTitle
->getDBkey() == $this->img
->getName() ||
isset( $diff ) ) {
77 // mTitle is the same as the redirect target so ask Article
78 // to perform the redirect for us.
79 $wgRequest->setVal( 'diffonly', 'true' );
80 return parent
::view();
82 // mTitle is not the same as the redirect target so it is
83 // probably the redirect page itself. Fake the redirect symbol
84 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
85 $wgOut->addHTML( $this->viewRedirect( Title
::makeTitle( NS_FILE
, $this->img
->getName() ),
86 /* $appendSubtitle */ true, /* $forceKnown */ true ) );
92 $this->showRedirectedFromHeader();
94 if ( $wgShowEXIF && $this->displayImg
->exists() ) {
95 // FIXME: bad interface, see note on MediaHandler::formatMetadata().
96 $formattedMetadata = $this->displayImg
->formatMetadata();
97 $showmeta = $formattedMetadata !== false;
102 if ( !$diff && $this->displayImg
->exists() )
103 $wgOut->addHTML( $this->showTOC( $showmeta ) );
106 $this->openShowImage();
108 # No need to display noarticletext, we use our own message, output in openShowImage()
109 if ( $this->getID() ) {
112 # Just need to set the right headers
113 $wgOut->setArticleFlag( true );
114 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
115 $this->viewUpdates();
118 # Show shared description, if needed
119 if ( $this->mExtraDescription
) {
120 $fol = wfMessage( 'shareddescriptionfollows' );
121 if ( !$fol->isDisabled() ) {
122 $wgOut->addWikiText( $fol->plain() );
124 $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription
. "</div>\n" );
127 $this->closeShowImage();
128 $this->imageHistory();
129 // TODO: Cleanup the following
131 $wgOut->addHTML( Xml
::element( 'h2',
132 array( 'id' => 'filelinks' ),
133 wfMsg( 'imagelinks' ) ) . "\n" );
135 # TODO! FIXME! For some freaky reason, we can't redirect to foreign images.
136 # Yet we return metadata about the target. Definitely an issue in the FileRepo
137 $this->imageRedirects();
140 # Allow extensions to add something after the image links
142 wfRunHooks( 'ImagePageAfterImageLinks', array( $this, &$html ) );
144 $wgOut->addHTML( $html );
147 $wgOut->addHTML( Xml
::element( 'h2', array( 'id' => 'metadata' ), wfMsg( 'metadata' ) ) . "\n" );
148 $wgOut->addWikiText( $this->makeMetadataTable( $formattedMetadata ) );
149 $wgOut->addModules( array( 'mediawiki.legacy.metadata' ) );
152 $css = $this->repo
->getDescriptionStylesheetUrl();
154 $wgOut->addStyle( $css );
158 public function getRedirectTarget() {
160 if ( $this->img
->isLocal() ) {
161 return parent
::getRedirectTarget();
163 // Foreign image page
164 $from = $this->img
->getRedirected();
165 $to = $this->img
->getName();
166 if ( $from == $to ) {
169 return $this->mRedirectTarget
= Title
::makeTitle( NS_FILE
, $to );
171 public function followRedirect() {
173 if ( $this->img
->isLocal() ) {
174 return parent
::followRedirect();
176 $from = $this->img
->getRedirected();
177 $to = $this->img
->getName();
178 if ( $from == $to ) {
181 return Title
::makeTitle( NS_FILE
, $to );
183 public function isRedirect( $text = false ) {
185 if ( $this->img
->isLocal() )
186 return parent
::isRedirect( $text );
188 return (bool)$this->img
->getRedirected();
191 public function isLocal() {
193 return $this->img
->isLocal();
196 public function getFile() {
201 public function getDisplayedFile() {
203 return $this->displayImg
;
206 public function getDuplicates() {
208 if ( !is_null( $this->dupes
) ) {
211 if ( !( $hash = $this->img
->getSha1() ) ) {
212 return $this->dupes
= array();
214 $dupes = RepoGroup
::singleton()->findBySha1( $hash );
215 // Remove duplicates with self and non matching file sizes
216 $self = $this->img
->getRepoName() . ':' . $this->img
->getName();
217 $size = $this->img
->getSize();
218 foreach ( $dupes as $index => $file ) {
219 $key = $file->getRepoName() . ':' . $file->getName();
221 unset( $dupes[$index] );
222 if ( $file->getSize() != $size )
223 unset( $dupes[$index] );
225 return $this->dupes
= $dupes;
233 * @param $metadata Boolean: whether or not to show the metadata link
236 protected function showTOC( $metadata ) {
238 '<li><a href="#file">' . wfMsgHtml( 'file-anchor-link' ) . '</a></li>',
239 '<li><a href="#filehistory">' . wfMsgHtml( 'filehist' ) . '</a></li>',
240 '<li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>',
243 $r[] = '<li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>';
246 wfRunHooks( 'ImagePageShowTOC', array( $this, &$r ) );
248 return '<ul id="filetoc">' . implode( "\n", $r ) . '</ul>';
252 * Make a table with metadata to be shown in the output page.
254 * FIXME: bad interface, see note on MediaHandler::formatMetadata().
256 * @param $metadata Array: the array containing the EXIF data
259 protected function makeMetadataTable( $metadata ) {
260 $r = "<div class=\"mw-imagepage-section-metadata\">";
261 $r .= wfMsgNoTrans( 'metadata-help' );
262 $r .= "<table id=\"mw_metadata\" class=\"mw_metadata\">\n";
263 foreach ( $metadata as $type => $stuff ) {
264 foreach ( $stuff as $v ) {
265 # FIXME, why is this using escapeId for a class?!
266 $class = Sanitizer
::escapeId( $v['id'] );
267 if ( $type == 'collapsed' ) {
268 $class .= ' collapsable';
270 $r .= "<tr class=\"$class\">\n";
271 $r .= "<th>{$v['name']}</th>\n";
272 $r .= "<td>{$v['value']}</td>\n</tr>";
275 $r .= "</table>\n</div>\n";
280 * Overloading Article's getContent method.
282 * Omit noarticletext if sharedupload; text will be fetched from the
283 * shared upload server if possible.
285 public function getContent() {
287 if ( $this->img
&& !$this->img
->isLocal() && 0 == $this->getID() ) {
290 return parent
::getContent();
293 protected function openShowImage() {
294 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
295 $wgLang, $wgContLang, $wgEnableUploads;
299 $sizeSel = intval( $wgUser->getOption( 'imagesize' ) );
300 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
301 $sizeSel = User
::getDefaultOption( 'imagesize' );
303 // The user offset might still be incorrect, specially if
304 // $wgImageLimits got changed (see bug #8858).
305 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
306 // Default to the first offset in $wgImageLimits
310 $max = $wgImageLimits[$sizeSel];
312 $maxHeight = $max[1];
313 $sk = $wgUser->getSkin();
314 $dirmark = $wgContLang->getDirMark();
316 if ( $this->displayImg
->exists() ) {
318 $page = $wgRequest->getIntOrNull( 'page' );
319 if ( is_null( $page ) ) {
323 $params = array( 'page' => $page );
325 $width_orig = $this->displayImg
->getWidth( $page );
326 $width = $width_orig;
327 $height_orig = $this->displayImg
->getHeight( $page );
328 $height = $height_orig;
330 $longDesc = wfMsg( 'parentheses', $this->displayImg
->getLongDesc() );
332 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this, &$wgOut ) );
334 if ( $this->displayImg
->allowInlineDisplay() ) {
337 # "Download high res version" link below the image
338 # $msgsize = wfMsgHtml('file-info-size', $width_orig, $height_orig, $sk->formatSize( $this->displayImg->getSize() ), $mime );
339 # We'll show a thumbnail of this image
340 if ( $width > $maxWidth ||
$height > $maxHeight ) {
341 # Calculate the thumbnail size.
342 # First case, the limiting factor is the width, not the height.
343 if ( $width / $height >= $maxWidth / $maxHeight ) {
344 $height = round( $height * $maxWidth / $width );
346 # Note that $height <= $maxHeight now.
348 $newwidth = floor( $width * $maxHeight / $height );
349 $height = round( $height * $newwidth / $width );
351 # Note that $height <= $maxHeight now, but might not be identical
352 # because of rounding.
354 $msgbig = wfMsgHtml( 'show-big-image' );
355 $msgsmall = wfMsgExt( 'show-big-image-thumb', 'parseinline',
356 $wgLang->formatNum( $width ),
357 $wgLang->formatNum( $height )
360 # Image is small enough to show full size on image page
361 $msgsmall = wfMsgExt( 'file-nohires', array( 'parseinline' ) );
364 $params['width'] = $width;
365 $thumbnail = $this->displayImg
->transform( $params );
368 $anchorclose = "<br />" . $msgsmall;
370 $isMulti = $this->displayImg
->isMultipage() && $this->displayImg
->pageCount() > 1;
372 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
377 'alt' => $this->displayImg
->getTitle()->getPrefixedText(),
380 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
381 $thumbnail->toHtml( $options ) .
382 $anchorclose . "</div>\n" );
386 $count = $this->displayImg
->pageCount();
389 $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
394 array( 'page' => $page - 1 ),
395 array( 'known', 'noclasses' )
397 $thumb1 = $sk->makeThumbLinkObj( $this->mTitle
, $this->displayImg
, $link, $label, 'none',
398 array( 'page' => $page - 1 ) );
403 if ( $page < $count ) {
404 $label = wfMsg( 'imgmultipagenext' );
409 array( 'page' => $page +
1 ),
410 array( 'known', 'noclasses' )
412 $thumb2 = $sk->makeThumbLinkObj( $this->mTitle
, $this->displayImg
, $link, $label, 'none',
413 array( 'page' => $page +
1 ) );
421 'name' => 'pageselector',
422 'action' => $wgScript,
423 'onchange' => 'document.pageselector.submit();',
426 for ( $i = 1; $i <= $count; $i++
) {
427 $options[] = Xml
::option( $wgLang->formatNum( $i ), $i, $i == $page );
429 $select = Xml
::tags( 'select',
430 array( 'id' => 'pageselector', 'name' => 'page' ),
431 implode( "\n", $options ) );
434 '</td><td><div class="multipageimagenavbox">' .
435 Xml
::openElement( 'form', $formParams ) .
436 Html
::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) .
437 wfMsgExt( 'imgmultigoto', array( 'parseinline', 'replaceafter' ), $select ) .
438 Xml
::submitButton( wfMsg( 'imgmultigo' ) ) .
439 Xml
::closeElement( 'form' ) .
440 "<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>"
444 # if direct link is allowed but it's not a renderable image, show an icon.
445 if ( $this->displayImg
->isSafeFile() ) {
446 $icon = $this->displayImg
->iconThumb();
448 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
449 $icon->toHtml( array( 'file-link' => true ) ) .
458 $filename = wfEscapeWikiText( $this->displayImg
->getName() );
459 $linktext = $filename;
460 if ( isset( $msgbig ) ) {
461 $linktext = wfEscapeWikiText( $msgbig );
463 $medialink = "[[Media:$filename|$linktext]]";
465 if ( !$this->displayImg
->isSafeFile() ) {
466 $warning = wfMsgNoTrans( 'mediawarning' );
467 $wgOut->addWikiText( <<<EOT
468 <div class="fullMedia"><span class="dangerousLink">{$medialink}</span>$dirmark <span class="fileInfo">$longDesc</span></div>
469 <div class="mediaWarning">$warning</div>
473 $wgOut->addWikiText( <<<EOT
474 <div class="fullMedia">{$medialink}{$dirmark} <span class="fileInfo">$longDesc</span>
481 if ( !$this->displayImg
->isLocal() ) {
482 $this->printSharedImageText();
485 # Image does not exist
486 if ( $wgEnableUploads && $wgUser->isAllowed( 'upload' ) ) {
487 // Only show an upload link if the user can upload
488 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
490 'filepage-nofile-link',
491 $uploadTitle->getFullUrl( array( 'wpDestFile' => $this->img
->getName() ) )
496 $nofile = 'filepage-nofile';
498 // Note, if there is an image description page, but
499 // no image, then this setRobotPolicy is overriden
500 // by Article::View().
501 $wgOut->setRobotPolicy( 'noindex,nofollow' );
502 $wgOut->wrapWikiMsg( "<div id='mw-imagepage-nofile' class='plainlinks'>\n$1\n</div>", $nofile );
503 if ( !$this->getID() ) {
504 // If there is no image, no shared image, and no description page,
505 // output a 404, to be consistent with articles.
506 $wgRequest->response()->header( "HTTP/1.x 404 Not Found" );
512 * Show a notice that the file is from a shared repository
514 protected function printSharedImageText() {
519 $descUrl = $this->img
->getDescriptionUrl();
520 $descText = $this->img
->getDescriptionText();
522 /* Add canonical to head if there is no local page for this shared file */
523 if( $descUrl && $this->getID() == 0 ) {
524 $wgOut->addLink( array( 'rel' => 'canonical', 'href' => $descUrl ) );
527 $wrap = "<div class=\"sharedUploadNotice\">\n$1\n</div>\n";
528 $repo = $this->img
->getRepo()->getDisplayName();
530 if ( $descUrl && $descText && wfMsgNoTrans( 'sharedupload-desc-here' ) !== '-' ) {
531 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-here', $repo, $descUrl ) );
532 } elseif ( $descUrl && wfMsgNoTrans( 'sharedupload-desc-there' ) !== '-' ) {
533 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-there', $repo, $descUrl ) );
535 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload', $repo ), ''/*BACKCOMPAT*/ );
539 $this->mExtraDescription
= $descText;
543 public function getUploadUrl() {
545 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
546 return $uploadTitle->getFullUrl( array(
547 'wpDestFile' => $this->img
->getName(),
553 * Print out the various links at the bottom of the image page, e.g. reupload,
554 * external editing (and instructions link) etc.
556 protected function uploadLinksBox() {
557 global $wgUser, $wgOut, $wgEnableUploads, $wgUseExternalEditor;
559 if ( !$wgEnableUploads ) { return; }
562 if ( !$this->img
->isLocal() )
565 $sk = $wgUser->getSkin();
567 $wgOut->addHTML( "<br /><ul>\n" );
569 # "Upload a new version of this file" link
570 if ( UploadBase
::userCanReUpload( $wgUser, $this->img
->name
) ) {
571 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
572 $wgOut->addHTML( "<li id=\"mw-imagepage-reupload-link\"><div class=\"plainlinks\">{$ulink}</div></li>\n" );
575 # External editing link
576 if ( $wgUseExternalEditor ) {
579 wfMsgHtml( 'edit-externally' ),
583 'externaledit' => 'true',
586 array( 'known', 'noclasses' )
588 $wgOut->addHTML( '<li id="mw-imagepage-edit-external">' . $elink . ' <small>' . wfMsgExt( 'edit-externally-help', array( 'parseinline' ) ) . "</small></li>\n" );
591 $wgOut->addHTML( "</ul>\n" );
594 protected function closeShowImage() { } # For overloading
597 * If the page we've just displayed is in the "Image" namespace,
598 * we follow it with an upload history of the image and its usage.
600 protected function imageHistory() {
604 $pager = new ImageHistoryPseudoPager( $this );
605 $wgOut->addHTML( $pager->getBody() );
606 $wgOut->preventClickjacking( $pager->getPreventClickjacking() );
608 $this->img
->resetHistory(); // free db resources
610 # Exist check because we don't want to show this on pages where an image
611 # doesn't exist along with the noimage message, that would suck. -ævar
612 if ( $this->img
->exists() ) {
613 $this->uploadLinksBox();
617 protected function imageLinks() {
618 global $wgUser, $wgOut, $wgLang;
622 $dbr = wfGetDB( DB_SLAVE
);
625 array( 'imagelinks', 'page' ),
626 array( 'page_namespace', 'page_title' ),
627 array( 'il_to' => $this->mTitle
->getDBkey(), 'il_from = page_id' ),
629 array( 'LIMIT' => $limit +
1 )
631 $count = $dbr->numRows( $res );
633 $wgOut->wrapWikiMsg( Html
::rawElement( 'div', array ( 'id' => 'mw-imagepage-nolinkstoimage' ), "\n$1\n" ), 'nolinkstoimage' );
637 $wgOut->addHTML( "<div id='mw-imagepage-section-linkstoimage'>\n" );
638 if ( $count <= $limit - 1 ) {
639 $wgOut->addWikiMsg( 'linkstoimage', $count );
641 // More links than the limit. Add a link to [[Special:Whatlinkshere]]
642 $wgOut->addWikiMsg( 'linkstoimage-more',
643 $wgLang->formatNum( $limit ),
644 $this->mTitle
->getPrefixedDBkey()
648 $wgOut->addHTML( Html
::openElement( 'ul', array( 'class' => 'mw-imagepage-linkstoimage' ) ) . "\n" );
649 $sk = $wgUser->getSkin();
652 foreach ( $res as $s ) {
654 if ( $count <= $limit ) {
655 // We have not yet reached the extra one that tells us there is more to fetch
660 // Sort the list by namespace:title
661 usort ( $elements, array( $this, 'compare' ) );
663 // Create links for every element
664 foreach( $elements as $element ) {
665 $link = $sk->linkKnown( Title
::makeTitle( $element->page_namespace
, $element->page_title
) );
666 $wgOut->addHTML( Html
::rawElement(
668 array( 'id' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace
),
674 $wgOut->addHTML( Html
::closeElement( 'ul' ) . "\n" );
677 // Add a links to [[Special:Whatlinkshere]]
678 if ( $count > $limit ) {
679 $wgOut->addWikiMsg( 'morelinkstoimage', $this->mTitle
->getPrefixedDBkey() );
681 $wgOut->addHTML( Html
::closeElement( 'div' ) . "\n" );
684 protected function imageRedirects() {
685 global $wgUser, $wgOut, $wgLang;
687 $redirects = $this->getTitle()->getRedirectsHere( NS_FILE
);
688 if ( count( $redirects ) == 0 ) return;
690 $wgOut->addHTML( "<div id='mw-imagepage-section-redirectstofile'>\n" );
691 $wgOut->addWikiMsg( 'redirectstofile',
692 $wgLang->formatNum( count( $redirects ) )
694 $wgOut->addHTML( "<ul class='mw-imagepage-redirectstofile'>\n" );
696 $sk = $wgUser->getSkin();
697 foreach ( $redirects as $title ) {
702 array( 'redirect' => 'no' ),
703 array( 'known', 'noclasses' )
705 $wgOut->addHTML( "<li>{$link}</li>\n" );
707 $wgOut->addHTML( "</ul></div>\n" );
711 protected function imageDupes() {
712 global $wgOut, $wgUser, $wgLang;
716 $dupes = $this->getDuplicates();
717 if ( count( $dupes ) == 0 ) return;
719 $wgOut->addHTML( "<div id='mw-imagepage-section-duplicates'>\n" );
720 $wgOut->addWikiMsg( 'duplicatesoffile',
721 $wgLang->formatNum( count( $dupes ) ), $this->mTitle
->getDBkey()
723 $wgOut->addHTML( "<ul class='mw-imagepage-duplicates'>\n" );
725 $sk = $wgUser->getSkin();
726 foreach ( $dupes as $file ) {
728 if ( $file->isLocal() ) {
734 array( 'known', 'noclasses' )
737 $link = $sk->makeExternalLink( $file->getDescriptionUrl(),
738 $file->getTitle()->getPrefixedText() );
739 $fromSrc = wfMsg( 'shared-repo-from', $file->getRepo()->getDisplayName() );
741 $wgOut->addHTML( "<li>{$link} {$fromSrc}</li>\n" );
743 $wgOut->addHTML( "</ul></div>\n" );
747 * Delete the file, or an earlier version of it
749 public function delete() {
750 global $wgUploadMaintenance;
751 if ( $wgUploadMaintenance && $this->mTitle
&& $this->mTitle
->getNamespace() == NS_FILE
) {
753 $wgOut->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n", array( 'filedelete-maintenance' ) );
758 if ( !$this->img
->exists() ||
!$this->img
->isLocal() ||
$this->img
->getRedirected() ) {
759 // Standard article deletion
763 $deleter = new FileDeleteForm( $this->img
);
768 * Revert the file to an earlier version
770 public function revert() {
772 $reverter = new FileRevertForm( $this->img
);
773 $reverter->execute();
777 * Override handling of action=purge
779 public function doPurge() {
781 if ( $this->img
->exists() ) {
782 wfDebug( "ImagePage::doPurge purging " . $this->img
->getName() . "\n" );
783 $update = new HTMLCacheUpdate( $this->mTitle
, 'imagelinks' );
785 $this->img
->upgradeRow();
786 $this->img
->purgeCache();
788 wfDebug( "ImagePage::doPurge no image for " . $this->img
->getName() . "; limiting purge to cache only\n" );
789 // even if the file supposedly doesn't exist, force any cached information
790 // to be updated (in case the cached information is wrong)
791 $this->img
->purgeCache();
797 * Display an error with a wikitext description
799 function showError( $description ) {
801 $wgOut->setPageTitle( wfMsg( "internalerror" ) );
802 $wgOut->setRobotPolicy( "noindex,nofollow" );
803 $wgOut->setArticleRelated( false );
804 $wgOut->enableClientCache( false );
805 $wgOut->addWikiText( $description );
810 * Callback for usort() to do link sorts by (namespace, title)
811 * Function copied from Title::compare()
813 * @param $a object page to compare with
814 * @param $b object page to compare with
815 * @return Integer: result of string comparison, or namespace comparison
817 protected function compare( $a, $b ) {
818 if ( $a->page_namespace
== $b->page_namespace
) {
819 return strcmp( $a->page_title
, $b->page_title
);
821 return $a->page_namespace
- $b->page_namespace
;
827 * Builds the image revision log shown on image pages
831 class ImageHistoryList
{
833 protected $imagePage, $img, $skin, $title, $repo, $showThumb;
834 protected $preventClickjacking = false;
836 public function __construct( $imagePage ) {
837 global $wgUser, $wgShowArchiveThumbnails;
838 $this->skin
= $wgUser->getSkin();
839 $this->current
= $imagePage->getFile();
840 $this->img
= $imagePage->getDisplayedFile();
841 $this->title
= $imagePage->getTitle();
842 $this->imagePage
= $imagePage;
843 $this->showThumb
= $wgShowArchiveThumbnails && $this->img
->canRender();
846 public function getImagePage() {
847 return $this->imagePage
;
850 public function getSkin() {
854 public function getFile() {
858 public function beginImageHistoryList( $navLinks = '' ) {
859 global $wgOut, $wgUser;
860 return Xml
::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) ) . "\n"
861 . "<div id=\"mw-imagepage-section-filehistory\">\n"
862 . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
864 . Xml
::openElement( 'table', array( 'class' => 'wikitable filehistory' ) ) . "\n"
866 . ( $this->current
->isLocal() && ( $wgUser->isAllowed( 'delete' ) ||
$wgUser->isAllowed( 'deletedhistory' ) ) ?
'<td></td>' : '' )
867 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
868 . ( $this->showThumb ?
'<th>' . wfMsgHtml( 'filehist-thumb' ) . '</th>' : '' )
869 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
870 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
871 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
875 public function endImageHistoryList( $navLinks = '' ) {
876 return "</table>\n$navLinks\n</div>\n";
879 public function imageHistoryLine( $iscur, $file ) {
880 global $wgUser, $wgLang;
882 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
883 $img = $iscur ?
$file->getName() : $file->getArchiveName();
884 $user = $file->getUser( 'id' );
885 $usertext = $file->getUser( 'text' );
886 $description = $file->getDescription();
888 $local = $this->current
->isLocal();
889 $row = $selected = '';
892 if ( $local && ( $wgUser->isAllowed( 'delete' ) ||
$wgUser->isAllowed( 'deletedhistory' ) ) ) {
894 # Link to remove from history
895 if ( $wgUser->isAllowed( 'delete' ) ) {
896 $q = array( 'action' => 'delete' );
898 $q['oldimage'] = $img;
899 $row .= $this->skin
->link(
901 wfMsgHtml( $iscur ?
'filehist-deleteall' : 'filehist-deleteone' ),
902 array(), $q, array( 'known' )
905 # Link to hide content. Don't show useless link to people who cannot hide revisions.
906 $canHide = $wgUser->isAllowed( 'deleterevision' );
907 if ( $canHide ||
( $wgUser->isAllowed( 'deletedhistory' ) && $file->getVisibility() ) ) {
908 if ( $wgUser->isAllowed( 'delete' ) ) {
911 // If file is top revision or locked from this user, don't link
912 if ( $iscur ||
!$file->userCan( File
::DELETED_RESTRICTED
) ) {
913 $del = $this->skin
->revDeleteLinkDisabled( $canHide );
915 list( $ts, $name ) = explode( '!', $img, 2 );
917 'type' => 'oldimage',
918 'target' => $this->title
->getPrefixedText(),
921 $del = $this->skin
->revDeleteLink( $query,
922 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
929 // Reversion link/current indicator
932 $row .= wfMsgHtml( 'filehist-current' );
933 } elseif ( $local && $wgUser->isLoggedIn() && $this->title
->userCan( 'edit' ) ) {
934 if ( $file->isDeleted( File
::DELETED_FILE
) ) {
935 $row .= wfMsgHtml( 'filehist-revert' );
937 $row .= $this->skin
->link(
939 wfMsgHtml( 'filehist-revert' ),
942 'action' => 'revert',
944 'wpEditToken' => $wgUser->editToken( $img )
946 array( 'known', 'noclasses' )
952 // Date/time and image link
953 if ( $file->getTimestamp() === $this->img
->getTimestamp() ) {
954 $selected = "class='filehistory-selected'";
956 $row .= "<td $selected style='white-space: nowrap;'>";
957 if ( !$file->userCan( File
::DELETED_FILE
) ) {
958 # Don't link to unviewable files
959 $row .= '<span class="history-deleted">' . $wgLang->timeAndDate( $timestamp, true ) . '</span>';
960 } elseif ( $file->isDeleted( File
::DELETED_FILE
) ) {
961 $this->preventClickjacking();
962 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
963 # Make a link to review the image
964 $url = $this->skin
->link(
966 $wgLang->timeAndDate( $timestamp, true ),
969 'target' => $this->title
->getPrefixedText(),
971 'token' => $wgUser->editToken( $img )
973 array( 'known', 'noclasses' )
975 $row .= '<span class="history-deleted">' . $url . '</span>';
977 $url = $iscur ?
$this->current
->getUrl() : $this->current
->getArchiveUrl( $img );
978 $row .= Xml
::element( 'a', array( 'href' => $url ), $wgLang->timeAndDate( $timestamp, true ) );
983 if ( $this->showThumb
) {
984 $row .= '<td>' . $this->getThumbForLine( $file ) . '</td>';
987 // Image dimensions + size
989 $row .= htmlspecialchars( $file->getDimensionsString() );
990 $row .= " <span style='white-space: nowrap;'>(" . $this->skin
->formatSize( $file->getSize() ) . ')</span>';
996 // Hide deleted usernames
997 if ( $file->isDeleted( File
::DELETED_USER
) ) {
998 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1000 $row .= $this->skin
->userLink( $user, $usertext ) . " <span style='white-space: nowrap;'>" .
1001 $this->skin
->userToolLinks( $user, $usertext ) . "</span>";
1004 $row .= htmlspecialchars( $usertext );
1006 $row .= '</td><td>';
1008 // Don't show deleted descriptions
1009 if ( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1010 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span>';
1012 $row .= $this->skin
->commentBlock( $description, $this->title
);
1017 wfRunHooks( 'ImagePageFileHistoryLine', array( $this, $file, &$row, &$rowClass ) );
1018 $classAttr = $rowClass ?
" class='$rowClass'" : "";
1020 return "<tr{$classAttr}>{$row}</tr>\n";
1023 protected function getThumbForLine( $file ) {
1026 if ( $file->allowInlineDisplay() && $file->userCan( File
::DELETED_FILE
) && !$file->isDeleted( File
::DELETED_FILE
) ) {
1031 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
1033 $thumbnail = $file->transform( $params );
1035 'alt' => wfMsg( 'filehist-thumbtext',
1036 $wgLang->timeAndDate( $timestamp, true ),
1037 $wgLang->date( $timestamp, true ),
1038 $wgLang->time( $timestamp, true ) ),
1039 'file-link' => true,
1042 if ( !$thumbnail ) return wfMsgHtml( 'filehist-nothumb' );
1044 return $thumbnail->toHtml( $options );
1046 return wfMsgHtml( 'filehist-nothumb' );
1050 protected function preventClickjacking( $enable = true ) {
1051 $this->preventClickjacking
= $enable;
1054 public function getPreventClickjacking() {
1055 return $this->preventClickjacking
;
1059 class ImageHistoryPseudoPager
extends ReverseChronologicalPager
{
1060 protected $preventClickjacking = false;
1062 function __construct( $imagePage ) {
1063 parent
::__construct();
1064 $this->mImagePage
= $imagePage;
1065 $this->mTitle
= clone ( $imagePage->getTitle() );
1066 $this->mTitle
->setFragment( '#filehistory' );
1068 $this->mHist
= array();
1069 $this->mRange
= array( 0, 0 ); // display range
1072 function getTitle() {
1073 return $this->mTitle
;
1076 function getQueryInfo() {
1080 function getIndexField() {
1084 function formatRow( $row ) {
1088 function getBody() {
1091 if ( count( $this->mHist
) ) {
1092 $list = new ImageHistoryList( $this->mImagePage
);
1093 # Generate prev/next links
1094 $navLink = $this->getNavigationBar();
1095 $s = $list->beginImageHistoryList( $navLink );
1096 // Skip rows there just for paging links
1097 for ( $i = $this->mRange
[0]; $i <= $this->mRange
[1]; $i++
) {
1098 $file = $this->mHist
[$i];
1099 $s .= $list->imageHistoryLine( !$file->isOld(), $file );
1101 $s .= $list->endImageHistoryList( $navLink );
1103 if ( $list->getPreventClickjacking() ) {
1104 $this->preventClickjacking();
1110 function doQuery() {
1111 if ( $this->mQueryDone
) return;
1112 $this->mImg
= $this->mImagePage
->getFile(); // ensure loading
1113 if ( !$this->mImg
->exists() ) {
1116 $queryLimit = $this->mLimit +
1; // limit plus extra row
1117 if ( $this->mIsBackwards
) {
1118 // Fetch the file history
1119 $this->mHist
= $this->mImg
->getHistory( $queryLimit, null, $this->mOffset
, false );
1120 // The current rev may not meet the offset/limit
1121 $numRows = count( $this->mHist
);
1122 if ( $numRows <= $this->mLimit
&& $this->mImg
->getTimestamp() > $this->mOffset
) {
1123 $this->mHist
= array_merge( array( $this->mImg
), $this->mHist
);
1126 // The current rev may not meet the offset
1127 if ( !$this->mOffset ||
$this->mImg
->getTimestamp() < $this->mOffset
) {
1128 $this->mHist
[] = $this->mImg
;
1130 // Old image versions (fetch extra row for nav links)
1131 $oiLimit = count( $this->mHist
) ?
$this->mLimit
: $this->mLimit +
1;
1132 // Fetch the file history
1133 $this->mHist
= array_merge( $this->mHist
,
1134 $this->mImg
->getHistory( $oiLimit, $this->mOffset
, null, false ) );
1136 $numRows = count( $this->mHist
); // Total number of query results
1138 # Index value of top item in the list
1139 $firstIndex = $this->mIsBackwards ?
1140 $this->mHist
[$numRows - 1]->getTimestamp() : $this->mHist
[0]->getTimestamp();
1141 # Discard the extra result row if there is one
1142 if ( $numRows > $this->mLimit
&& $numRows > 1 ) {
1143 if ( $this->mIsBackwards
) {
1144 # Index value of item past the index
1145 $this->mPastTheEndIndex
= $this->mHist
[0]->getTimestamp();
1146 # Index value of bottom item in the list
1147 $lastIndex = $this->mHist
[1]->getTimestamp();
1149 $this->mRange
= array( 1, $numRows - 1 );
1151 # Index value of item past the index
1152 $this->mPastTheEndIndex
= $this->mHist
[$numRows - 1]->getTimestamp();
1153 # Index value of bottom item in the list
1154 $lastIndex = $this->mHist
[$numRows - 2]->getTimestamp();
1156 $this->mRange
= array( 0, $numRows - 2 );
1159 # Setting indexes to an empty string means that they will be
1160 # omitted if they would otherwise appear in URLs. It just so
1161 # happens that this is the right thing to do in the standard
1162 # UI, in all the relevant cases.
1163 $this->mPastTheEndIndex
= '';
1164 # Index value of bottom item in the list
1165 $lastIndex = $this->mIsBackwards ?
1166 $this->mHist
[0]->getTimestamp() : $this->mHist
[$numRows - 1]->getTimestamp();
1168 $this->mRange
= array( 0, $numRows - 1 );
1173 $this->mPastTheEndIndex
= '';
1175 if ( $this->mIsBackwards
) {
1176 $this->mIsFirst
= ( $numRows < $queryLimit );
1177 $this->mIsLast
= ( $this->mOffset
== '' );
1178 $this->mLastShown
= $firstIndex;
1179 $this->mFirstShown
= $lastIndex;
1181 $this->mIsFirst
= ( $this->mOffset
== '' );
1182 $this->mIsLast
= ( $numRows < $queryLimit );
1183 $this->mLastShown
= $lastIndex;
1184 $this->mFirstShown
= $firstIndex;
1186 $this->mQueryDone
= true;
1189 protected function preventClickjacking( $enable = true ) {
1190 $this->preventClickjacking
= $enable;
1193 public function getPreventClickjacking() {
1194 return $this->preventClickjacking
;