4 * Special handling for image description pages
8 class ImagePage
extends Article
{
13 /* private */ var $img;
17 /* private */ var $displayImg;
18 /* private */ var $repo;
19 /* private */ var $fileLoaded;
21 var $mExtraDescription = false;
24 function __construct( $title ) {
25 parent
::__construct( $title );
34 public function setFile( $file ) {
35 $this->displayImg
= $file;
37 $this->fileLoaded
= true;
40 protected function loadFile() {
41 if ( $this->fileLoaded
) {
44 $this->fileLoaded
= true;
46 $this->displayImg
= $this->img
= false;
47 wfRunHooks( 'ImagePageFindFile', array( $this, &$this->img
, &$this->displayImg
) );
49 $this->img
= wfFindFile( $this->mTitle
);
51 $this->img
= wfLocalFile( $this->mTitle
);
54 if ( !$this->displayImg
) {
55 $this->displayImg
= $this->img
;
57 $this->repo
= $this->img
->getRepo();
61 * Handler for action=render
62 * Include body text only; none of the image extras
64 public function render() {
66 $wgOut->setArticleBodyOnly( true );
70 public function view() {
71 global $wgOut, $wgShowEXIF, $wgRequest, $wgUser;
73 $diff = $wgRequest->getVal( 'diff' );
74 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
76 if ( $this->mTitle
->getNamespace() != NS_FILE ||
( isset( $diff ) && $diffOnly ) ) {
77 return parent
::view();
82 if ( $this->mTitle
->getNamespace() == NS_FILE
&& $this->img
->getRedirected() ) {
83 if ( $this->mTitle
->getDBkey() == $this->img
->getName() ||
isset( $diff ) ) {
84 // mTitle is the same as the redirect target so ask Article
85 // to perform the redirect for us.
86 $wgRequest->setVal( 'diffonly', 'true' );
87 return parent
::view();
89 // mTitle is not the same as the redirect target so it is
90 // probably the redirect page itself. Fake the redirect symbol
91 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
92 $wgOut->addHTML( $this->viewRedirect( Title
::makeTitle( NS_FILE
, $this->img
->getName() ),
93 /* $appendSubtitle */ true, /* $forceKnown */ true ) );
99 $this->showRedirectedFromHeader();
101 if ( $wgShowEXIF && $this->displayImg
->exists() ) {
102 // FIXME: bad interface, see note on MediaHandler::formatMetadata().
103 $formattedMetadata = $this->displayImg
->formatMetadata();
104 $showmeta = $formattedMetadata !== false;
109 if ( !$diff && $this->displayImg
->exists() )
110 $wgOut->addHTML( $this->showTOC( $showmeta ) );
113 $this->openShowImage();
115 # No need to display noarticletext, we use our own message, output in openShowImage()
116 if ( $this->getID() ) {
119 # Just need to set the right headers
120 $wgOut->setArticleFlag( true );
121 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
122 $this->viewUpdates();
125 # Show shared description, if needed
126 if ( $this->mExtraDescription
) {
127 $fol = wfMessage( 'shareddescriptionfollows' );
128 if ( !$fol->isDisabled() ) {
129 $wgOut->addWikiText( $fol->plain() );
131 $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription
. "</div>\n" );
134 $this->closeShowImage();
135 $this->imageHistory();
136 // TODO: Cleanup the following
138 $wgOut->addHTML( Xml
::element( 'h2',
139 array( 'id' => 'filelinks' ),
140 wfMsg( 'imagelinks' ) ) . "\n" );
142 # TODO! FIXME! For some freaky reason, we can't redirect to foreign images.
143 # Yet we return metadata about the target. Definitely an issue in the FileRepo
144 $this->imageRedirects();
147 # Allow extensions to add something after the image links
149 wfRunHooks( 'ImagePageAfterImageLinks', array( $this, &$html ) );
151 $wgOut->addHTML( $html );
154 $wgOut->addHTML( Xml
::element( 'h2', array( 'id' => 'metadata' ), wfMsg( 'metadata' ) ) . "\n" );
155 $wgOut->addWikiText( $this->makeMetadataTable( $formattedMetadata ) );
156 $wgOut->addModules( array( 'mediawiki.legacy.metadata' ) );
159 $css = $this->repo
->getDescriptionStylesheetUrl();
161 $wgOut->addStyle( $css );
165 public function getRedirectTarget() {
167 if ( $this->img
->isLocal() ) {
168 return parent
::getRedirectTarget();
170 // Foreign image page
171 $from = $this->img
->getRedirected();
172 $to = $this->img
->getName();
173 if ( $from == $to ) {
176 return $this->mRedirectTarget
= Title
::makeTitle( NS_FILE
, $to );
179 public function followRedirect() {
181 if ( $this->img
->isLocal() ) {
182 return parent
::followRedirect();
184 $from = $this->img
->getRedirected();
185 $to = $this->img
->getName();
186 if ( $from == $to ) {
189 return Title
::makeTitle( NS_FILE
, $to );
192 public function isRedirect( $text = false ) {
194 if ( $this->img
->isLocal() )
195 return parent
::isRedirect( $text );
197 return (bool)$this->img
->getRedirected();
200 public function isLocal() {
202 return $this->img
->isLocal();
205 public function getFile() {
210 public function getDisplayedFile() {
212 return $this->displayImg
;
215 public function getDuplicates() {
217 if ( !is_null( $this->dupes
) ) {
220 if ( !( $hash = $this->img
->getSha1() ) ) {
221 return $this->dupes
= array();
223 $dupes = RepoGroup
::singleton()->findBySha1( $hash );
224 // Remove duplicates with self and non matching file sizes
225 $self = $this->img
->getRepoName() . ':' . $this->img
->getName();
226 $size = $this->img
->getSize();
227 foreach ( $dupes as $index => $file ) {
228 $key = $file->getRepoName() . ':' . $file->getName();
230 unset( $dupes[$index] );
231 if ( $file->getSize() != $size )
232 unset( $dupes[$index] );
234 return $this->dupes
= $dupes;
241 * @param $metadata Boolean: whether or not to show the metadata link
244 protected function showTOC( $metadata ) {
246 '<li><a href="#file">' . wfMsgHtml( 'file-anchor-link' ) . '</a></li>',
247 '<li><a href="#filehistory">' . wfMsgHtml( 'filehist' ) . '</a></li>',
248 '<li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>',
251 $r[] = '<li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>';
254 wfRunHooks( 'ImagePageShowTOC', array( $this, &$r ) );
256 return '<ul id="filetoc">' . implode( "\n", $r ) . '</ul>';
260 * Make a table with metadata to be shown in the output page.
262 * FIXME: bad interface, see note on MediaHandler::formatMetadata().
264 * @param $metadata Array: the array containing the EXIF data
267 protected function makeMetadataTable( $metadata ) {
268 $r = "<div class=\"mw-imagepage-section-metadata\">";
269 $r .= wfMsgNoTrans( 'metadata-help' );
270 $r .= "<table id=\"mw_metadata\" class=\"mw_metadata\">\n";
271 foreach ( $metadata as $type => $stuff ) {
272 foreach ( $stuff as $v ) {
273 # FIXME, why is this using escapeId for a class?!
274 $class = Sanitizer
::escapeId( $v['id'] );
275 if ( $type == 'collapsed' ) {
276 $class .= ' collapsable';
278 $r .= "<tr class=\"$class\">\n";
279 $r .= "<th>{$v['name']}</th>\n";
280 $r .= "<td>{$v['value']}</td>\n</tr>";
283 $r .= "</table>\n</div>\n";
288 * Overloading Article's getContent method.
290 * Omit noarticletext if sharedupload; text will be fetched from the
291 * shared upload server if possible.
293 public function getContent() {
295 if ( $this->img
&& !$this->img
->isLocal() && 0 == $this->getID() ) {
298 return parent
::getContent();
301 protected function openShowImage() {
302 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
303 $wgLang, $wgContLang, $wgEnableUploads;
307 $sizeSel = intval( $wgUser->getOption( 'imagesize' ) );
308 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
309 $sizeSel = User
::getDefaultOption( 'imagesize' );
311 // The user offset might still be incorrect, specially if
312 // $wgImageLimits got changed (see bug #8858).
313 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
314 // Default to the first offset in $wgImageLimits
318 $max = $wgImageLimits[$sizeSel];
320 $maxHeight = $max[1];
321 $sk = $wgUser->getSkin();
322 $dirmark = $wgContLang->getDirMark();
324 if ( $this->displayImg
->exists() ) {
326 $page = $wgRequest->getIntOrNull( 'page' );
327 if ( is_null( $page ) ) {
331 $params = array( 'page' => $page );
333 $width_orig = $this->displayImg
->getWidth( $page );
334 $width = $width_orig;
335 $height_orig = $this->displayImg
->getHeight( $page );
336 $height = $height_orig;
338 $longDesc = wfMsg( 'parentheses', $this->displayImg
->getLongDesc() );
340 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this, &$wgOut ) );
342 if ( $this->displayImg
->allowInlineDisplay() ) {
345 # "Download high res version" link below the image
346 # $msgsize = wfMsgHtml('file-info-size', $width_orig, $height_orig, $sk->formatSize( $this->displayImg->getSize() ), $mime );
347 # We'll show a thumbnail of this image
348 if ( $width > $maxWidth ||
$height > $maxHeight ) {
349 # Calculate the thumbnail size.
350 # First case, the limiting factor is the width, not the height.
351 if ( $width / $height >= $maxWidth / $maxHeight ) {
352 $height = round( $height * $maxWidth / $width );
354 # Note that $height <= $maxHeight now.
356 $newwidth = floor( $width * $maxHeight / $height );
357 $height = round( $height * $newwidth / $width );
359 # Note that $height <= $maxHeight now, but might not be identical
360 # because of rounding.
362 $msgbig = wfMsgHtml( 'show-big-image' );
363 $msgsmall = wfMsgExt( 'show-big-image-thumb', 'parseinline',
364 $wgLang->formatNum( $width ),
365 $wgLang->formatNum( $height )
368 # Image is small enough to show full size on image page
369 $msgsmall = wfMsgExt( 'file-nohires', array( 'parseinline' ) );
372 $params['width'] = $width;
373 $thumbnail = $this->displayImg
->transform( $params );
376 $anchorclose = "<br />" . $msgsmall;
378 $isMulti = $this->displayImg
->isMultipage() && $this->displayImg
->pageCount() > 1;
380 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
385 'alt' => $this->displayImg
->getTitle()->getPrefixedText(),
388 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
389 $thumbnail->toHtml( $options ) .
390 $anchorclose . "</div>\n" );
394 $count = $this->displayImg
->pageCount();
397 $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
402 array( 'page' => $page - 1 ),
403 array( 'known', 'noclasses' )
405 $thumb1 = $sk->makeThumbLinkObj( $this->mTitle
, $this->displayImg
, $link, $label, 'none',
406 array( 'page' => $page - 1 ) );
411 if ( $page < $count ) {
412 $label = wfMsg( 'imgmultipagenext' );
417 array( 'page' => $page +
1 ),
418 array( 'known', 'noclasses' )
420 $thumb2 = $sk->makeThumbLinkObj( $this->mTitle
, $this->displayImg
, $link, $label, 'none',
421 array( 'page' => $page +
1 ) );
429 'name' => 'pageselector',
430 'action' => $wgScript,
431 'onchange' => 'document.pageselector.submit();',
434 for ( $i = 1; $i <= $count; $i++
) {
435 $options[] = Xml
::option( $wgLang->formatNum( $i ), $i, $i == $page );
437 $select = Xml
::tags( 'select',
438 array( 'id' => 'pageselector', 'name' => 'page' ),
439 implode( "\n", $options ) );
442 '</td><td><div class="multipageimagenavbox">' .
443 Xml
::openElement( 'form', $formParams ) .
444 Html
::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) .
445 wfMsgExt( 'imgmultigoto', array( 'parseinline', 'replaceafter' ), $select ) .
446 Xml
::submitButton( wfMsg( 'imgmultigo' ) ) .
447 Xml
::closeElement( 'form' ) .
448 "<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>"
452 # if direct link is allowed but it's not a renderable image, show an icon.
453 if ( $this->displayImg
->isSafeFile() ) {
454 $icon = $this->displayImg
->iconThumb();
456 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
457 $icon->toHtml( array( 'file-link' => true ) ) .
466 $filename = wfEscapeWikiText( $this->displayImg
->getName() );
467 $linktext = $filename;
468 if ( isset( $msgbig ) ) {
469 $linktext = wfEscapeWikiText( $msgbig );
471 $medialink = "[[Media:$filename|$linktext]]";
473 if ( !$this->displayImg
->isSafeFile() ) {
474 $warning = wfMsgNoTrans( 'mediawarning' );
475 $wgOut->addWikiText( <<<EOT
476 <div class="fullMedia"><span class="dangerousLink">{$medialink}</span>$dirmark <span class="fileInfo">$longDesc</span></div>
477 <div class="mediaWarning">$warning</div>
481 $wgOut->addWikiText( <<<EOT
482 <div class="fullMedia">{$medialink}{$dirmark} <span class="fileInfo">$longDesc</span>
489 if ( !$this->displayImg
->isLocal() ) {
490 $this->printSharedImageText();
493 # Image does not exist
494 if ( $wgEnableUploads && $wgUser->isAllowed( 'upload' ) ) {
495 // Only show an upload link if the user can upload
496 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
498 'filepage-nofile-link',
499 $uploadTitle->getFullUrl( array( 'wpDestFile' => $this->img
->getName() ) )
504 $nofile = 'filepage-nofile';
506 // Note, if there is an image description page, but
507 // no image, then this setRobotPolicy is overriden
508 // by Article::View().
509 $wgOut->setRobotPolicy( 'noindex,nofollow' );
510 $wgOut->wrapWikiMsg( "<div id='mw-imagepage-nofile' class='plainlinks'>\n$1\n</div>", $nofile );
511 if ( !$this->getID() ) {
512 // If there is no image, no shared image, and no description page,
513 // output a 404, to be consistent with articles.
514 $wgRequest->response()->header( "HTTP/1.x 404 Not Found" );
520 * Show a notice that the file is from a shared repository
522 protected function printSharedImageText() {
527 $descUrl = $this->img
->getDescriptionUrl();
528 $descText = $this->img
->getDescriptionText();
530 /* Add canonical to head if there is no local page for this shared file */
531 if( $descUrl && $this->getID() == 0 ) {
532 $wgOut->addLink( array( 'rel' => 'canonical', 'href' => $descUrl ) );
535 $wrap = "<div class=\"sharedUploadNotice\">\n$1\n</div>\n";
536 $repo = $this->img
->getRepo()->getDisplayName();
538 if ( $descUrl && $descText && wfMsgNoTrans( 'sharedupload-desc-here' ) !== '-' ) {
539 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-here', $repo, $descUrl ) );
540 } elseif ( $descUrl && wfMsgNoTrans( 'sharedupload-desc-there' ) !== '-' ) {
541 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-there', $repo, $descUrl ) );
543 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload', $repo ), ''/*BACKCOMPAT*/ );
547 $this->mExtraDescription
= $descText;
551 public function getUploadUrl() {
553 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
554 return $uploadTitle->getFullUrl( array(
555 'wpDestFile' => $this->img
->getName(),
561 * Print out the various links at the bottom of the image page, e.g. reupload,
562 * external editing (and instructions link) etc.
564 protected function uploadLinksBox() {
565 global $wgUser, $wgOut, $wgEnableUploads, $wgUseExternalEditor;
567 if ( !$wgEnableUploads ) {
572 if ( !$this->img
->isLocal() )
575 $sk = $wgUser->getSkin();
577 $wgOut->addHTML( "<br /><ul>\n" );
579 # "Upload a new version of this file" link
580 if ( UploadBase
::userCanReUpload( $wgUser, $this->img
->name
) ) {
581 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
582 $wgOut->addHTML( "<li id=\"mw-imagepage-reupload-link\"><div class=\"plainlinks\">{$ulink}</div></li>\n" );
585 # External editing link
586 if ( $wgUseExternalEditor ) {
589 wfMsgHtml( 'edit-externally' ),
593 'externaledit' => 'true',
596 array( 'known', 'noclasses' )
598 $wgOut->addHTML( '<li id="mw-imagepage-edit-external">' . $elink . ' <small>' . wfMsgExt( 'edit-externally-help', array( 'parseinline' ) ) . "</small></li>\n" );
601 $wgOut->addHTML( "</ul>\n" );
604 protected function closeShowImage() { } # For overloading
607 * If the page we've just displayed is in the "Image" namespace,
608 * we follow it with an upload history of the image and its usage.
610 protected function imageHistory() {
614 $pager = new ImageHistoryPseudoPager( $this );
615 $wgOut->addHTML( $pager->getBody() );
616 $wgOut->preventClickjacking( $pager->getPreventClickjacking() );
618 $this->img
->resetHistory(); // free db resources
620 # Exist check because we don't want to show this on pages where an image
621 # doesn't exist along with the noimage message, that would suck. -ævar
622 if ( $this->img
->exists() ) {
623 $this->uploadLinksBox();
627 protected function imageLinks() {
628 global $wgUser, $wgOut, $wgLang;
632 $dbr = wfGetDB( DB_SLAVE
);
635 array( 'imagelinks', 'page' ),
636 array( 'page_namespace', 'page_title' ),
637 array( 'il_to' => $this->mTitle
->getDBkey(), 'il_from = page_id' ),
639 array( 'LIMIT' => $limit +
1 )
641 $count = $dbr->numRows( $res );
643 $wgOut->wrapWikiMsg( Html
::rawElement( 'div', array ( 'id' => 'mw-imagepage-nolinkstoimage' ), "\n$1\n" ), 'nolinkstoimage' );
647 $wgOut->addHTML( "<div id='mw-imagepage-section-linkstoimage'>\n" );
648 if ( $count <= $limit - 1 ) {
649 $wgOut->addWikiMsg( 'linkstoimage', $count );
651 // More links than the limit. Add a link to [[Special:Whatlinkshere]]
652 $wgOut->addWikiMsg( 'linkstoimage-more',
653 $wgLang->formatNum( $limit ),
654 $this->mTitle
->getPrefixedDBkey()
658 $wgOut->addHTML( Html
::openElement( 'ul', array( 'class' => 'mw-imagepage-linkstoimage' ) ) . "\n" );
659 $sk = $wgUser->getSkin();
662 foreach ( $res as $s ) {
664 if ( $count <= $limit ) {
665 // We have not yet reached the extra one that tells us there is more to fetch
670 // Sort the list by namespace:title
671 usort ( $elements, array( $this, 'compare' ) );
673 // Create links for every element
674 foreach( $elements as $element ) {
675 $link = $sk->linkKnown( Title
::makeTitle( $element->page_namespace
, $element->page_title
) );
676 $wgOut->addHTML( Html
::rawElement(
678 array( 'id' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace
),
684 $wgOut->addHTML( Html
::closeElement( 'ul' ) . "\n" );
687 // Add a links to [[Special:Whatlinkshere]]
688 if ( $count > $limit ) {
689 $wgOut->addWikiMsg( 'morelinkstoimage', $this->mTitle
->getPrefixedDBkey() );
691 $wgOut->addHTML( Html
::closeElement( 'div' ) . "\n" );
694 protected function imageRedirects() {
695 global $wgUser, $wgOut, $wgLang;
697 $redirects = $this->getTitle()->getRedirectsHere( NS_FILE
);
698 if ( count( $redirects ) == 0 ) return;
700 $wgOut->addHTML( "<div id='mw-imagepage-section-redirectstofile'>\n" );
701 $wgOut->addWikiMsg( 'redirectstofile',
702 $wgLang->formatNum( count( $redirects ) )
704 $wgOut->addHTML( "<ul class='mw-imagepage-redirectstofile'>\n" );
706 $sk = $wgUser->getSkin();
707 foreach ( $redirects as $title ) {
712 array( 'redirect' => 'no' ),
713 array( 'known', 'noclasses' )
715 $wgOut->addHTML( "<li>{$link}</li>\n" );
717 $wgOut->addHTML( "</ul></div>\n" );
721 protected function imageDupes() {
722 global $wgOut, $wgUser, $wgLang;
726 $dupes = $this->getDuplicates();
727 if ( count( $dupes ) == 0 ) {
731 $wgOut->addHTML( "<div id='mw-imagepage-section-duplicates'>\n" );
732 $wgOut->addWikiMsg( 'duplicatesoffile',
733 $wgLang->formatNum( count( $dupes ) ), $this->mTitle
->getDBkey()
735 $wgOut->addHTML( "<ul class='mw-imagepage-duplicates'>\n" );
737 $sk = $wgUser->getSkin();
738 foreach ( $dupes as $file ) {
740 if ( $file->isLocal() ) {
746 array( 'known', 'noclasses' )
749 $link = $sk->makeExternalLink( $file->getDescriptionUrl(),
750 $file->getTitle()->getPrefixedText() );
751 $fromSrc = wfMsg( 'shared-repo-from', $file->getRepo()->getDisplayName() );
753 $wgOut->addHTML( "<li>{$link} {$fromSrc}</li>\n" );
755 $wgOut->addHTML( "</ul></div>\n" );
759 * Delete the file, or an earlier version of it
761 public function delete() {
762 global $wgUploadMaintenance;
763 if ( $wgUploadMaintenance && $this->mTitle
&& $this->mTitle
->getNamespace() == NS_FILE
) {
765 $wgOut->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n", array( 'filedelete-maintenance' ) );
770 if ( !$this->img
->exists() ||
!$this->img
->isLocal() ||
$this->img
->getRedirected() ) {
771 // Standard article deletion
775 $deleter = new FileDeleteForm( $this->img
);
780 * Revert the file to an earlier version
782 public function revert() {
784 $reverter = new FileRevertForm( $this->img
);
785 $reverter->execute();
789 * Override handling of action=purge
791 public function doPurge() {
793 if ( $this->img
->exists() ) {
794 wfDebug( "ImagePage::doPurge purging " . $this->img
->getName() . "\n" );
795 $update = new HTMLCacheUpdate( $this->mTitle
, 'imagelinks' );
797 $this->img
->upgradeRow();
798 $this->img
->purgeCache();
800 wfDebug( "ImagePage::doPurge no image for " . $this->img
->getName() . "; limiting purge to cache only\n" );
801 // even if the file supposedly doesn't exist, force any cached information
802 // to be updated (in case the cached information is wrong)
803 $this->img
->purgeCache();
809 * Display an error with a wikitext description
811 function showError( $description ) {
813 $wgOut->setPageTitle( wfMsg( "internalerror" ) );
814 $wgOut->setRobotPolicy( "noindex,nofollow" );
815 $wgOut->setArticleRelated( false );
816 $wgOut->enableClientCache( false );
817 $wgOut->addWikiText( $description );
821 * Callback for usort() to do link sorts by (namespace, title)
822 * Function copied from Title::compare()
824 * @param $a object page to compare with
825 * @param $b object page to compare with
826 * @return Integer: result of string comparison, or namespace comparison
828 protected function compare( $a, $b ) {
829 if ( $a->page_namespace
== $b->page_namespace
) {
830 return strcmp( $a->page_title
, $b->page_title
);
832 return $a->page_namespace
- $b->page_namespace
;
838 * Builds the image revision log shown on image pages
842 class ImageHistoryList
{
857 protected $imagePage;
864 protected $repo, $showThumb;
865 protected $preventClickjacking = false;
868 * @param ImagePage $imagePage
870 public function __construct( $imagePage ) {
871 global $wgUser, $wgShowArchiveThumbnails;
872 $this->skin
= $wgUser->getSkin();
873 $this->current
= $imagePage->getFile();
874 $this->img
= $imagePage->getDisplayedFile();
875 $this->title
= $imagePage->getTitle();
876 $this->imagePage
= $imagePage;
877 $this->showThumb
= $wgShowArchiveThumbnails && $this->img
->canRender();
880 public function getImagePage() {
881 return $this->imagePage
;
884 public function getSkin() {
888 public function getFile() {
892 public function beginImageHistoryList( $navLinks = '' ) {
893 global $wgOut, $wgUser;
894 return Xml
::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) ) . "\n"
895 . "<div id=\"mw-imagepage-section-filehistory\">\n"
896 . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
898 . Xml
::openElement( 'table', array( 'class' => 'wikitable filehistory' ) ) . "\n"
900 . ( $this->current
->isLocal() && ( $wgUser->isAllowed( 'delete' ) ||
$wgUser->isAllowed( 'deletedhistory' ) ) ?
'<td></td>' : '' )
901 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
902 . ( $this->showThumb ?
'<th>' . wfMsgHtml( 'filehist-thumb' ) . '</th>' : '' )
903 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
904 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
905 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
909 public function endImageHistoryList( $navLinks = '' ) {
910 return "</table>\n$navLinks\n</div>\n";
918 public function imageHistoryLine( $iscur, $file ) {
919 global $wgUser, $wgLang;
921 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
922 $img = $iscur ?
$file->getName() : $file->getArchiveName();
923 $user = $file->getUser( 'id' );
924 $usertext = $file->getUser( 'text' );
925 $description = $file->getDescription();
927 $local = $this->current
->isLocal();
928 $row = $selected = '';
931 if ( $local && ( $wgUser->isAllowed( 'delete' ) ||
$wgUser->isAllowed( 'deletedhistory' ) ) ) {
933 # Link to remove from history
934 if ( $wgUser->isAllowed( 'delete' ) ) {
935 $q = array( 'action' => 'delete' );
937 $q['oldimage'] = $img;
938 $row .= $this->skin
->link(
940 wfMsgHtml( $iscur ?
'filehist-deleteall' : 'filehist-deleteone' ),
941 array(), $q, array( 'known' )
944 # Link to hide content. Don't show useless link to people who cannot hide revisions.
945 $canHide = $wgUser->isAllowed( 'deleterevision' );
946 if ( $canHide ||
( $wgUser->isAllowed( 'deletedhistory' ) && $file->getVisibility() ) ) {
947 if ( $wgUser->isAllowed( 'delete' ) ) {
950 // If file is top revision or locked from this user, don't link
951 if ( $iscur ||
!$file->userCan( File
::DELETED_RESTRICTED
) ) {
952 $del = $this->skin
->revDeleteLinkDisabled( $canHide );
954 list( $ts, $name ) = explode( '!', $img, 2 );
956 'type' => 'oldimage',
957 'target' => $this->title
->getPrefixedText(),
960 $del = $this->skin
->revDeleteLink( $query,
961 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
968 // Reversion link/current indicator
971 $row .= wfMsgHtml( 'filehist-current' );
972 } elseif ( $local && $wgUser->isLoggedIn() && $this->title
->userCan( 'edit' ) ) {
973 if ( $file->isDeleted( File
::DELETED_FILE
) ) {
974 $row .= wfMsgHtml( 'filehist-revert' );
976 $row .= $this->skin
->link(
978 wfMsgHtml( 'filehist-revert' ),
981 'action' => 'revert',
983 'wpEditToken' => $wgUser->editToken( $img )
985 array( 'known', 'noclasses' )
991 // Date/time and image link
992 if ( $file->getTimestamp() === $this->img
->getTimestamp() ) {
993 $selected = "class='filehistory-selected'";
995 $row .= "<td $selected style='white-space: nowrap;'>";
996 if ( !$file->userCan( File
::DELETED_FILE
) ) {
997 # Don't link to unviewable files
998 $row .= '<span class="history-deleted">' . $wgLang->timeAndDate( $timestamp, true ) . '</span>';
999 } elseif ( $file->isDeleted( File
::DELETED_FILE
) ) {
1000 $this->preventClickjacking();
1001 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
1002 # Make a link to review the image
1003 $url = $this->skin
->link(
1005 $wgLang->timeAndDate( $timestamp, true ),
1008 'target' => $this->title
->getPrefixedText(),
1010 'token' => $wgUser->editToken( $img )
1012 array( 'known', 'noclasses' )
1014 $row .= '<span class="history-deleted">' . $url . '</span>';
1016 $url = $iscur ?
$this->current
->getUrl() : $this->current
->getArchiveUrl( $img );
1017 $row .= Xml
::element( 'a', array( 'href' => $url ), $wgLang->timeAndDate( $timestamp, true ) );
1022 if ( $this->showThumb
) {
1023 $row .= '<td>' . $this->getThumbForLine( $file ) . '</td>';
1026 // Image dimensions + size
1028 $row .= htmlspecialchars( $file->getDimensionsString() );
1029 $row .= " <span style='white-space: nowrap;'>(" . $this->skin
->formatSize( $file->getSize() ) . ')</span>';
1035 // Hide deleted usernames
1036 if ( $file->isDeleted( File
::DELETED_USER
) ) {
1037 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1039 $row .= $this->skin
->userLink( $user, $usertext ) . " <span style='white-space: nowrap;'>" .
1040 $this->skin
->userToolLinks( $user, $usertext ) . "</span>";
1043 $row .= htmlspecialchars( $usertext );
1045 $row .= '</td><td>';
1047 // Don't show deleted descriptions
1048 if ( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1049 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span>';
1051 $row .= $this->skin
->commentBlock( $description, $this->title
);
1056 wfRunHooks( 'ImagePageFileHistoryLine', array( $this, $file, &$row, &$rowClass ) );
1057 $classAttr = $rowClass ?
" class='$rowClass'" : "";
1059 return "<tr{$classAttr}>{$row}</tr>\n";
1066 protected function getThumbForLine( $file ) {
1069 if ( $file->allowInlineDisplay() && $file->userCan( File
::DELETED_FILE
) && !$file->isDeleted( File
::DELETED_FILE
) ) {
1074 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
1076 $thumbnail = $file->transform( $params );
1078 'alt' => wfMsg( 'filehist-thumbtext',
1079 $wgLang->timeAndDate( $timestamp, true ),
1080 $wgLang->date( $timestamp, true ),
1081 $wgLang->time( $timestamp, true ) ),
1082 'file-link' => true,
1085 if ( !$thumbnail ) return wfMsgHtml( 'filehist-nothumb' );
1087 return $thumbnail->toHtml( $options );
1089 return wfMsgHtml( 'filehist-nothumb' );
1093 protected function preventClickjacking( $enable = true ) {
1094 $this->preventClickjacking
= $enable;
1097 public function getPreventClickjacking() {
1098 return $this->preventClickjacking
;
1102 class ImageHistoryPseudoPager
extends ReverseChronologicalPager
{
1103 protected $preventClickjacking = false;
1116 * @param ImagePage $imagePage
1118 function __construct( $imagePage ) {
1119 parent
::__construct();
1120 $this->mImagePage
= $imagePage;
1121 $this->mTitle
= clone ( $imagePage->getTitle() );
1122 $this->mTitle
->setFragment( '#filehistory' );
1124 $this->mHist
= array();
1125 $this->mRange
= array( 0, 0 ); // display range
1128 function getTitle() {
1129 return $this->mTitle
;
1132 function getQueryInfo() {
1136 function getIndexField() {
1140 function formatRow( $row ) {
1144 function getBody() {
1147 if ( count( $this->mHist
) ) {
1148 $list = new ImageHistoryList( $this->mImagePage
);
1149 # Generate prev/next links
1150 $navLink = $this->getNavigationBar();
1151 $s = $list->beginImageHistoryList( $navLink );
1152 // Skip rows there just for paging links
1153 for ( $i = $this->mRange
[0]; $i <= $this->mRange
[1]; $i++
) {
1154 $file = $this->mHist
[$i];
1155 $s .= $list->imageHistoryLine( !$file->isOld(), $file );
1157 $s .= $list->endImageHistoryList( $navLink );
1159 if ( $list->getPreventClickjacking() ) {
1160 $this->preventClickjacking();
1166 function doQuery() {
1167 if ( $this->mQueryDone
) return;
1168 $this->mImg
= $this->mImagePage
->getFile(); // ensure loading
1169 if ( !$this->mImg
->exists() ) {
1172 $queryLimit = $this->mLimit +
1; // limit plus extra row
1173 if ( $this->mIsBackwards
) {
1174 // Fetch the file history
1175 $this->mHist
= $this->mImg
->getHistory( $queryLimit, null, $this->mOffset
, false );
1176 // The current rev may not meet the offset/limit
1177 $numRows = count( $this->mHist
);
1178 if ( $numRows <= $this->mLimit
&& $this->mImg
->getTimestamp() > $this->mOffset
) {
1179 $this->mHist
= array_merge( array( $this->mImg
), $this->mHist
);
1182 // The current rev may not meet the offset
1183 if ( !$this->mOffset ||
$this->mImg
->getTimestamp() < $this->mOffset
) {
1184 $this->mHist
[] = $this->mImg
;
1186 // Old image versions (fetch extra row for nav links)
1187 $oiLimit = count( $this->mHist
) ?
$this->mLimit
: $this->mLimit +
1;
1188 // Fetch the file history
1189 $this->mHist
= array_merge( $this->mHist
,
1190 $this->mImg
->getHistory( $oiLimit, $this->mOffset
, null, false ) );
1192 $numRows = count( $this->mHist
); // Total number of query results
1194 # Index value of top item in the list
1195 $firstIndex = $this->mIsBackwards ?
1196 $this->mHist
[$numRows - 1]->getTimestamp() : $this->mHist
[0]->getTimestamp();
1197 # Discard the extra result row if there is one
1198 if ( $numRows > $this->mLimit
&& $numRows > 1 ) {
1199 if ( $this->mIsBackwards
) {
1200 # Index value of item past the index
1201 $this->mPastTheEndIndex
= $this->mHist
[0]->getTimestamp();
1202 # Index value of bottom item in the list
1203 $lastIndex = $this->mHist
[1]->getTimestamp();
1205 $this->mRange
= array( 1, $numRows - 1 );
1207 # Index value of item past the index
1208 $this->mPastTheEndIndex
= $this->mHist
[$numRows - 1]->getTimestamp();
1209 # Index value of bottom item in the list
1210 $lastIndex = $this->mHist
[$numRows - 2]->getTimestamp();
1212 $this->mRange
= array( 0, $numRows - 2 );
1215 # Setting indexes to an empty string means that they will be
1216 # omitted if they would otherwise appear in URLs. It just so
1217 # happens that this is the right thing to do in the standard
1218 # UI, in all the relevant cases.
1219 $this->mPastTheEndIndex
= '';
1220 # Index value of bottom item in the list
1221 $lastIndex = $this->mIsBackwards ?
1222 $this->mHist
[0]->getTimestamp() : $this->mHist
[$numRows - 1]->getTimestamp();
1224 $this->mRange
= array( 0, $numRows - 1 );
1229 $this->mPastTheEndIndex
= '';
1231 if ( $this->mIsBackwards
) {
1232 $this->mIsFirst
= ( $numRows < $queryLimit );
1233 $this->mIsLast
= ( $this->mOffset
== '' );
1234 $this->mLastShown
= $firstIndex;
1235 $this->mFirstShown
= $lastIndex;
1237 $this->mIsFirst
= ( $this->mOffset
== '' );
1238 $this->mIsLast
= ( $numRows < $queryLimit );
1239 $this->mLastShown
= $lastIndex;
1240 $this->mFirstShown
= $firstIndex;
1242 $this->mQueryDone
= true;
1245 protected function preventClickjacking( $enable = true ) {
1246 $this->preventClickjacking
= $enable;
1249 public function getPreventClickjacking() {
1250 return $this->preventClickjacking
;