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 );
26 public function setFile( $file ) {
27 $this->displayImg
= $file;
29 $this->fileLoaded
= true;
32 protected function loadFile() {
33 if ( $this->fileLoaded
) {
36 $this->fileLoaded
= true;
38 $this->displayImg
= $this->img
= false;
39 wfRunHooks( 'ImagePageFindFile', array( $this, &$this->img
, &$this->displayImg
) );
41 $this->img
= wfFindFile( $this->mTitle
);
43 $this->img
= wfLocalFile( $this->mTitle
);
46 if ( !$this->displayImg
) {
47 $this->displayImg
= $this->img
;
49 $this->repo
= $this->img
->getRepo();
53 * Handler for action=render
54 * Include body text only; none of the image extras
56 public function render() {
58 $wgOut->setArticleBodyOnly( true );
62 public function view() {
63 global $wgOut, $wgShowEXIF, $wgRequest, $wgUser;
65 $diff = $wgRequest->getVal( 'diff' );
66 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
68 if ( $this->mTitle
->getNamespace() != NS_FILE ||
( isset( $diff ) && $diffOnly ) ) {
69 return parent
::view();
74 if ( $this->mTitle
->getNamespace() == NS_FILE
&& $this->img
->getRedirected() ) {
75 if ( $this->mTitle
->getDBkey() == $this->img
->getName() ||
isset( $diff ) ) {
76 // mTitle is the same as the redirect target so ask Article
77 // to perform the redirect for us.
78 $wgRequest->setVal( 'diffonly', 'true' );
79 return parent
::view();
81 // mTitle is not the same as the redirect target so it is
82 // probably the redirect page itself. Fake the redirect symbol
83 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
84 $wgOut->addHTML( $this->viewRedirect( Title
::makeTitle( NS_FILE
, $this->img
->getName() ),
85 /* $appendSubtitle */ true, /* $forceKnown */ true ) );
91 $this->showRedirectedFromHeader();
93 if ( $wgShowEXIF && $this->displayImg
->exists() ) {
94 // FIXME: bad interface, see note on MediaHandler::formatMetadata().
95 $formattedMetadata = $this->displayImg
->formatMetadata();
96 $showmeta = $formattedMetadata !== false;
101 if ( !$diff && $this->displayImg
->exists() )
102 $wgOut->addHTML( $this->showTOC( $showmeta ) );
105 $this->openShowImage();
107 # No need to display noarticletext, we use our own message, output in openShowImage()
108 if ( $this->getID() ) {
111 # Just need to set the right headers
112 $wgOut->setArticleFlag( true );
113 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
114 $this->viewUpdates();
117 # Show shared description, if needed
118 if ( $this->mExtraDescription
) {
119 $fol = wfMsgNoTrans( 'shareddescriptionfollows' );
120 if ( $fol != '-' && !wfEmptyMsg( 'shareddescriptionfollows', $fol ) ) {
121 $wgOut->addWikiText( $fol );
123 $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription
. "</div>\n" );
126 $this->closeShowImage();
127 $this->imageHistory();
128 // TODO: Cleanup the following
130 $wgOut->addHTML( Xml
::element( 'h2',
131 array( 'id' => 'filelinks' ),
132 wfMsg( 'imagelinks' ) ) . "\n" );
134 # TODO! FIXME! For some freaky reason, we can't redirect to foreign images.
135 # Yet we return metadata about the target. Definitely an issue in the FileRepo
136 $this->imageRedirects();
139 # Allow extensions to add something after the image links
141 wfRunHooks( 'ImagePageAfterImageLinks', array( $this, &$html ) );
143 $wgOut->addHTML( $html );
146 $expand = htmlspecialchars( Xml
::escapeJsString( wfMsg( 'metadata-expand' ) ) );
147 $collapse = htmlspecialchars( Xml
::escapeJsString( wfMsg( 'metadata-collapse' ) ) );
148 $wgOut->addHTML( Xml
::element( 'h2', array( 'id' => 'metadata' ), wfMsg( 'metadata' ) ) . "\n" );
149 $wgOut->addWikiText( $this->makeMetadataTable( $formattedMetadata ) );
150 $wgOut->addModules( array( 'mediawiki.legacy.metadata' ) );
152 "<script type=\"text/javascript\">attachMetadataToggle('mw_metadata', '$expand', '$collapse');</script>\n" );
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;
335 $linkAttribs = array( 'href' => $full_url );
336 $longDesc = $this->displayImg
->getLongDesc();
338 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this, &$wgOut ) );
340 if ( $this->displayImg
->allowInlineDisplay() ) {
343 # "Download high res version" link below the image
344 # $msgsize = wfMsgHtml('file-info-size', $width_orig, $height_orig, $sk->formatSize( $this->displayImg->getSize() ), $mime );
345 # We'll show a thumbnail of this image
346 if ( $width > $maxWidth ||
$height > $maxHeight ) {
347 # Calculate the thumbnail size.
348 # First case, the limiting factor is the width, not the height.
349 if ( $width / $height >= $maxWidth / $maxHeight ) {
350 $height = round( $height * $maxWidth / $width );
352 # Note that $height <= $maxHeight now.
354 $newwidth = floor( $width * $maxHeight / $height );
355 $height = round( $height * $newwidth / $width );
357 # Note that $height <= $maxHeight now, but might not be identical
358 # because of rounding.
360 $msgbig = wfMsgHtml( 'show-big-image' );
361 $msgsmall = wfMsgExt( 'show-big-image-thumb', 'parseinline',
362 $wgLang->formatNum( $width ),
363 $wgLang->formatNum( $height )
366 # Image is small enough to show full size on image page
367 $msgsmall = wfMsgExt( 'file-nohires', array( 'parseinline' ) );
370 $params['width'] = $width;
371 $thumbnail = $this->displayImg
->transform( $params );
374 if ( !$this->displayImg
->mustRender() ) {
375 $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 Xml
::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 $wgOut->setRobotPolicy( 'noindex,nofollow' );
507 $wgOut->wrapWikiMsg( "<div id='mw-imagepage-nofile' class='plainlinks'>\n$1\n</div>", $nofile );
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 $wrap = "<div class=\"sharedUploadNotice\">\n$1\n</div>\n";
523 $repo = $this->img
->getRepo()->getDisplayName();
525 if ( $descUrl && $descText && wfMsgNoTrans( 'sharedupload-desc-here' ) !== '-' ) {
526 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-here', $repo, $descUrl ) );
527 } elseif ( $descUrl && wfMsgNoTrans( 'sharedupload-desc-there' ) !== '-' ) {
528 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-there', $repo, $descUrl ) );
530 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload', $repo ), ''/*BACKCOMPAT*/ );
534 $this->mExtraDescription
= $descText;
538 public function getUploadUrl() {
540 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
541 return $uploadTitle->getFullUrl( array(
542 'wpDestFile' => $this->img
->getName(),
548 * Print out the various links at the bottom of the image page, e.g. reupload,
549 * external editing (and instructions link) etc.
551 protected function uploadLinksBox() {
552 global $wgUser, $wgOut, $wgEnableUploads, $wgUseExternalEditor;
554 if ( !$wgEnableUploads ) { return; }
557 if ( !$this->img
->isLocal() )
560 $sk = $wgUser->getSkin();
562 $wgOut->addHTML( "<br /><ul>\n" );
564 # "Upload a new version of this file" link
565 if ( UploadBase
::userCanReUpload( $wgUser, $this->img
->name
) ) {
566 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
567 $wgOut->addHTML( "<li id=\"mw-imagepage-reupload-link\"><div class=\"plainlinks\">{$ulink}</div></li>\n" );
570 # External editing link
571 if ( $wgUseExternalEditor ) {
574 wfMsgHtml( 'edit-externally' ),
578 'externaledit' => 'true',
581 array( 'known', 'noclasses' )
583 $wgOut->addHTML( '<li id="mw-imagepage-edit-external">' . $elink . ' <small>' . wfMsgExt( 'edit-externally-help', array( 'parseinline' ) ) . "</small></li>\n" );
586 $wgOut->addHTML( "</ul>\n" );
589 protected function closeShowImage() { } # For overloading
592 * If the page we've just displayed is in the "Image" namespace,
593 * we follow it with an upload history of the image and its usage.
595 protected function imageHistory() {
599 $pager = new ImageHistoryPseudoPager( $this );
600 $wgOut->addHTML( $pager->getBody() );
602 $this->img
->resetHistory(); // free db resources
604 # Exist check because we don't want to show this on pages where an image
605 # doesn't exist along with the noimage message, that would suck. -ævar
606 if ( $this->img
->exists() ) {
607 $this->uploadLinksBox();
611 protected function imageLinks() {
612 global $wgUser, $wgOut, $wgLang;
616 $dbr = wfGetDB( DB_SLAVE
);
619 array( 'imagelinks', 'page' ),
620 array( 'page_namespace', 'page_title' ),
621 array( 'il_to' => $this->mTitle
->getDBkey(), 'il_from = page_id' ),
623 array( 'LIMIT' => $limit +
1 )
625 $count = $dbr->numRows( $res );
627 $wgOut->wrapWikiMsg( Html
::rawElement( 'div', array ( 'id' => 'mw-imagepage-nolinkstoimage' ), "\n$1\n" ), 'nolinkstoimage' );
631 $wgOut->addHTML( "<div id='mw-imagepage-section-linkstoimage'>\n" );
632 if ( $count <= $limit - 1 ) {
633 $wgOut->addWikiMsg( 'linkstoimage', $count );
635 // More links than the limit. Add a link to [[Special:Whatlinkshere]]
636 $wgOut->addWikiMsg( 'linkstoimage-more',
637 $wgLang->formatNum( $limit ),
638 $this->mTitle
->getPrefixedDBkey()
642 $wgOut->addHTML( Html
::openElement( 'ul', array( 'class' => 'mw-imagepage-linkstoimage' ) ) . "\n" );
643 $sk = $wgUser->getSkin();
646 while ( $s = $res->fetchObject() ) {
648 if ( $count <= $limit ) {
649 // We have not yet reached the extra one that tells us there is more to fetch
654 // Sort the list by namespace:title
655 usort ( $elements, array( $this, 'compare' ) );
657 // Create links for every element
658 foreach( $elements as $element ) {
659 $link = $sk->linkKnown( Title
::makeTitle( $element->page_namespace
, $element->page_title
) );
660 $wgOut->addHTML( Html
::rawElement(
662 array( 'id' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace
),
668 $wgOut->addHTML( Html
::closeElement( 'ul' ) . "\n" );
671 // Add a links to [[Special:Whatlinkshere]]
672 if ( $count > $limit ) {
673 $wgOut->addWikiMsg( 'morelinkstoimage', $this->mTitle
->getPrefixedDBkey() );
675 $wgOut->addHTML( Html
::closeElement( 'div' ) . "\n" );
678 protected function imageRedirects() {
679 global $wgUser, $wgOut, $wgLang;
681 $redirects = $this->getTitle()->getRedirectsHere( NS_FILE
);
682 if ( count( $redirects ) == 0 ) return;
684 $wgOut->addHTML( "<div id='mw-imagepage-section-redirectstofile'>\n" );
685 $wgOut->addWikiMsg( 'redirectstofile',
686 $wgLang->formatNum( count( $redirects ) )
688 $wgOut->addHTML( "<ul class='mw-imagepage-redirectstofile'>\n" );
690 $sk = $wgUser->getSkin();
691 foreach ( $redirects as $title ) {
696 array( 'redirect' => 'no' ),
697 array( 'known', 'noclasses' )
699 $wgOut->addHTML( "<li>{$link}</li>\n" );
701 $wgOut->addHTML( "</ul></div>\n" );
705 protected function imageDupes() {
706 global $wgOut, $wgUser, $wgLang;
710 $dupes = $this->getDuplicates();
711 if ( count( $dupes ) == 0 ) return;
713 $wgOut->addHTML( "<div id='mw-imagepage-section-duplicates'>\n" );
714 $wgOut->addWikiMsg( 'duplicatesoffile',
715 $wgLang->formatNum( count( $dupes ) ), $this->mTitle
->getDBkey()
717 $wgOut->addHTML( "<ul class='mw-imagepage-duplicates'>\n" );
719 $sk = $wgUser->getSkin();
720 foreach ( $dupes as $file ) {
722 if ( $file->isLocal() ) {
728 array( 'known', 'noclasses' )
731 $link = $sk->makeExternalLink( $file->getDescriptionUrl(),
732 $file->getTitle()->getPrefixedText() );
733 $fromSrc = wfMsg( 'shared-repo-from', $file->getRepo()->getDisplayName() );
735 $wgOut->addHTML( "<li>{$link} {$fromSrc}</li>\n" );
737 $wgOut->addHTML( "</ul></div>\n" );
741 * Delete the file, or an earlier version of it
743 public function delete() {
744 global $wgUploadMaintenance;
745 if ( $wgUploadMaintenance && $this->mTitle
&& $this->mTitle
->getNamespace() == NS_FILE
) {
747 $wgOut->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n", array( 'filedelete-maintenance' ) );
752 if ( !$this->img
->exists() ||
!$this->img
->isLocal() ||
$this->img
->getRedirected() ) {
753 // Standard article deletion
757 $deleter = new FileDeleteForm( $this->img
);
762 * Revert the file to an earlier version
764 public function revert() {
766 $reverter = new FileRevertForm( $this->img
);
767 $reverter->execute();
771 * Override handling of action=purge
773 public function doPurge() {
775 if ( $this->img
->exists() ) {
776 wfDebug( "ImagePage::doPurge purging " . $this->img
->getName() . "\n" );
777 $update = new HTMLCacheUpdate( $this->mTitle
, 'imagelinks' );
779 $this->img
->upgradeRow();
780 $this->img
->purgeCache();
782 wfDebug( "ImagePage::doPurge no image for " . $this->img
->getName() . "; limiting purge to cache only\n" );
783 // even if the file supposedly doesn't exist, force any cached information
784 // to be updated (in case the cached information is wrong)
785 $this->img
->purgeCache();
791 * Display an error with a wikitext description
793 function showError( $description ) {
795 $wgOut->setPageTitle( wfMsg( "internalerror" ) );
796 $wgOut->setRobotPolicy( "noindex,nofollow" );
797 $wgOut->setArticleRelated( false );
798 $wgOut->enableClientCache( false );
799 $wgOut->addWikiText( $description );
804 * Callback for usort() to do link sorts by (namespace, title)
805 * Function copied from Title::compare()
807 * @param $a object page to compare with
808 * @param $b object page to compare with
809 * @return Integer: result of string comparison, or namespace comparison
811 protected function compare( $a, $b ) {
812 if ( $a->page_namespace
== $b->page_namespace
) {
813 return strcmp( $a->page_title
, $b->page_title
);
815 return $a->page_namespace
- $b->page_namespace
;
821 * Builds the image revision log shown on image pages
825 class ImageHistoryList
{
827 protected $imagePage, $img, $skin, $title, $repo, $showThumb;
829 public function __construct( $imagePage ) {
830 global $wgUser, $wgShowArchiveThumbnails;
831 $this->skin
= $wgUser->getSkin();
832 $this->current
= $imagePage->getFile();
833 $this->img
= $imagePage->getDisplayedFile();
834 $this->title
= $imagePage->getTitle();
835 $this->imagePage
= $imagePage;
836 $this->showThumb
= $wgShowArchiveThumbnails && $this->img
->canRender();
839 public function getImagePage() {
840 return $this->imagePage
;
843 public function getSkin() {
847 public function getFile() {
851 public function beginImageHistoryList( $navLinks = '' ) {
852 global $wgOut, $wgUser;
853 return Xml
::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) ) . "\n"
854 . "<div id=\"mw-imagepage-section-filehistory\">\n"
855 . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
857 . Xml
::openElement( 'table', array( 'class' => 'wikitable filehistory' ) ) . "\n"
859 . ( $this->current
->isLocal() && ( $wgUser->isAllowed( 'delete' ) ||
$wgUser->isAllowed( 'deletedhistory' ) ) ?
'<td></td>' : '' )
860 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
861 . ( $this->showThumb ?
'<th>' . wfMsgHtml( 'filehist-thumb' ) . '</th>' : '' )
862 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
863 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
864 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
868 public function endImageHistoryList( $navLinks = '' ) {
869 return "</table>\n$navLinks\n</div>\n";
872 public function imageHistoryLine( $iscur, $file ) {
873 global $wgUser, $wgLang;
875 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
876 $img = $iscur ?
$file->getName() : $file->getArchiveName();
877 $user = $file->getUser( 'id' );
878 $usertext = $file->getUser( 'text' );
879 $description = $file->getDescription();
881 $local = $this->current
->isLocal();
882 $row = $selected = '';
885 if ( $local && ( $wgUser->isAllowed( 'delete' ) ||
$wgUser->isAllowed( 'deletedhistory' ) ) ) {
887 # Link to remove from history
888 if ( $wgUser->isAllowed( 'delete' ) ) {
889 $q = array( 'action' => 'delete' );
891 $q['oldimage'] = $img;
892 $row .= $this->skin
->link(
894 wfMsgHtml( $iscur ?
'filehist-deleteall' : 'filehist-deleteone' ),
895 array(), $q, array( 'known' )
898 # Link to hide content. Don't show useless link to people who cannot hide revisions.
899 $canHide = $wgUser->isAllowed( 'deleterevision' );
900 if ( $canHide ||
( $wgUser->isAllowed( 'deletedhistory' ) && $file->getVisibility() ) ) {
901 if ( $wgUser->isAllowed( 'delete' ) ) {
904 // If file is top revision or locked from this user, don't link
905 if ( $iscur ||
!$file->userCan( File
::DELETED_RESTRICTED
) ) {
906 $del = $this->skin
->revDeleteLinkDisabled( $canHide );
908 list( $ts, $name ) = explode( '!', $img, 2 );
910 'type' => 'oldimage',
911 'target' => $this->title
->getPrefixedText(),
914 $del = $this->skin
->revDeleteLink( $query,
915 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
922 // Reversion link/current indicator
925 $row .= wfMsgHtml( 'filehist-current' );
926 } elseif ( $local && $wgUser->isLoggedIn() && $this->title
->userCan( 'edit' ) ) {
927 if ( $file->isDeleted( File
::DELETED_FILE
) ) {
928 $row .= wfMsgHtml( 'filehist-revert' );
930 $row .= $this->skin
->link(
932 wfMsgHtml( 'filehist-revert' ),
935 'action' => 'revert',
937 'wpEditToken' => $wgUser->editToken( $img )
939 array( 'known', 'noclasses' )
945 // Date/time and image link
946 if ( $file->getTimestamp() === $this->img
->getTimestamp() ) {
947 $selected = "class='filehistory-selected'";
949 $row .= "<td $selected style='white-space: nowrap;'>";
950 if ( !$file->userCan( File
::DELETED_FILE
) ) {
951 # Don't link to unviewable files
952 $row .= '<span class="history-deleted">' . $wgLang->timeAndDate( $timestamp, true ) . '</span>';
953 } elseif ( $file->isDeleted( File
::DELETED_FILE
) ) {
954 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
955 # Make a link to review the image
956 $url = $this->skin
->link(
958 $wgLang->timeAndDate( $timestamp, true ),
961 'target' => $this->title
->getPrefixedText(),
963 'token' => $wgUser->editToken( $img )
965 array( 'known', 'noclasses' )
967 $row .= '<span class="history-deleted">' . $url . '</span>';
969 $url = $iscur ?
$this->current
->getUrl() : $this->current
->getArchiveUrl( $img );
970 $row .= Xml
::element( 'a', array( 'href' => $url ), $wgLang->timeAndDate( $timestamp, true ) );
975 if ( $this->showThumb
) {
976 $row .= '<td>' . $this->getThumbForLine( $file ) . '</td>';
979 // Image dimensions + size
981 $row .= htmlspecialchars( $file->getDimensionsString() );
982 $row .= " <span style='white-space: nowrap;'>(" . $this->skin
->formatSize( $file->getSize() ) . ')</span>';
988 // Hide deleted usernames
989 if ( $file->isDeleted( File
::DELETED_USER
) ) {
990 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
992 $row .= $this->skin
->userLink( $user, $usertext ) . " <span style='white-space: nowrap;'>" .
993 $this->skin
->userToolLinks( $user, $usertext ) . "</span>";
996 $row .= htmlspecialchars( $usertext );
1000 // Don't show deleted descriptions
1001 if ( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1002 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span>';
1004 $row .= $this->skin
->commentBlock( $description, $this->title
);
1008 wfRunHooks( 'ImagePageFileHistoryLine', array( $this, $file, &$row, &$rowClass ) );
1009 $classAttr = $rowClass ?
" class='$rowClass'" : "";
1011 return "<tr{$classAttr}>{$row}</tr>\n";
1014 protected function getThumbForLine( $file ) {
1017 if ( $file->allowInlineDisplay() && $file->userCan( File
::DELETED_FILE
) && !$file->isDeleted( File
::DELETED_FILE
) ) {
1022 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
1024 $thumbnail = $file->transform( $params );
1026 'alt' => wfMsg( 'filehist-thumbtext',
1027 $wgLang->timeAndDate( $timestamp, true ),
1028 $wgLang->date( $timestamp, true ),
1029 $wgLang->time( $timestamp, true ) ),
1030 'file-link' => true,
1033 if ( !$thumbnail ) return wfMsgHtml( 'filehist-nothumb' );
1035 return $thumbnail->toHtml( $options );
1037 return wfMsgHtml( 'filehist-nothumb' );
1042 class ImageHistoryPseudoPager
extends ReverseChronologicalPager
{
1043 function __construct( $imagePage ) {
1044 parent
::__construct();
1045 $this->mImagePage
= $imagePage;
1046 $this->mTitle
= clone ( $imagePage->getTitle() );
1047 $this->mTitle
->setFragment( '#filehistory' );
1049 $this->mHist
= array();
1050 $this->mRange
= array( 0, 0 ); // display range
1053 function getTitle() {
1054 return $this->mTitle
;
1057 function getQueryInfo() {
1061 function getIndexField() {
1065 function formatRow( $row ) {
1069 function getBody() {
1072 if ( count( $this->mHist
) ) {
1073 $list = new ImageHistoryList( $this->mImagePage
);
1074 # Generate prev/next links
1075 $navLink = $this->getNavigationBar();
1076 $s = $list->beginImageHistoryList( $navLink );
1077 // Skip rows there just for paging links
1078 for ( $i = $this->mRange
[0]; $i <= $this->mRange
[1]; $i++
) {
1079 $file = $this->mHist
[$i];
1080 $s .= $list->imageHistoryLine( !$file->isOld(), $file );
1082 $s .= $list->endImageHistoryList( $navLink );
1087 function doQuery() {
1088 if ( $this->mQueryDone
) return;
1089 $this->mImg
= $this->mImagePage
->getFile(); // ensure loading
1090 if ( !$this->mImg
->exists() ) {
1093 $queryLimit = $this->mLimit +
1; // limit plus extra row
1094 if ( $this->mIsBackwards
) {
1095 // Fetch the file history
1096 $this->mHist
= $this->mImg
->getHistory( $queryLimit, null, $this->mOffset
, false );
1097 // The current rev may not meet the offset/limit
1098 $numRows = count( $this->mHist
);
1099 if ( $numRows <= $this->mLimit
&& $this->mImg
->getTimestamp() > $this->mOffset
) {
1100 $this->mHist
= array_merge( array( $this->mImg
), $this->mHist
);
1103 // The current rev may not meet the offset
1104 if ( !$this->mOffset ||
$this->mImg
->getTimestamp() < $this->mOffset
) {
1105 $this->mHist
[] = $this->mImg
;
1107 // Old image versions (fetch extra row for nav links)
1108 $oiLimit = count( $this->mHist
) ?
$this->mLimit
: $this->mLimit +
1;
1109 // Fetch the file history
1110 $this->mHist
= array_merge( $this->mHist
,
1111 $this->mImg
->getHistory( $oiLimit, $this->mOffset
, null, false ) );
1113 $numRows = count( $this->mHist
); // Total number of query results
1115 # Index value of top item in the list
1116 $firstIndex = $this->mIsBackwards ?
1117 $this->mHist
[$numRows - 1]->getTimestamp() : $this->mHist
[0]->getTimestamp();
1118 # Discard the extra result row if there is one
1119 if ( $numRows > $this->mLimit
&& $numRows > 1 ) {
1120 if ( $this->mIsBackwards
) {
1121 # Index value of item past the index
1122 $this->mPastTheEndIndex
= $this->mHist
[0]->getTimestamp();
1123 # Index value of bottom item in the list
1124 $lastIndex = $this->mHist
[1]->getTimestamp();
1126 $this->mRange
= array( 1, $numRows - 1 );
1128 # Index value of item past the index
1129 $this->mPastTheEndIndex
= $this->mHist
[$numRows - 1]->getTimestamp();
1130 # Index value of bottom item in the list
1131 $lastIndex = $this->mHist
[$numRows - 2]->getTimestamp();
1133 $this->mRange
= array( 0, $numRows - 2 );
1136 # Setting indexes to an empty string means that they will be
1137 # omitted if they would otherwise appear in URLs. It just so
1138 # happens that this is the right thing to do in the standard
1139 # UI, in all the relevant cases.
1140 $this->mPastTheEndIndex
= '';
1141 # Index value of bottom item in the list
1142 $lastIndex = $this->mIsBackwards ?
1143 $this->mHist
[0]->getTimestamp() : $this->mHist
[$numRows - 1]->getTimestamp();
1145 $this->mRange
= array( 0, $numRows - 1 );
1150 $this->mPastTheEndIndex
= '';
1152 if ( $this->mIsBackwards
) {
1153 $this->mIsFirst
= ( $numRows < $queryLimit );
1154 $this->mIsLast
= ( $this->mOffset
== '' );
1155 $this->mLastShown
= $firstIndex;
1156 $this->mFirstShown
= $lastIndex;
1158 $this->mIsFirst
= ( $this->mOffset
== '' );
1159 $this->mIsLast
= ( $numRows < $queryLimit );
1160 $this->mLastShown
= $lastIndex;
1161 $this->mFirstShown
= $firstIndex;
1163 $this->mQueryDone
= true;