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 ) );
114 $this->openShowImage();
117 # No need to display noarticletext, we use our own message, output in openShowImage()
118 if ( $this->getID() ) {
121 # Just need to set the right headers
122 $wgOut->setArticleFlag( true );
123 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
124 $this->viewUpdates();
127 # Show shared description, if needed
128 if ( $this->mExtraDescription
) {
129 $fol = wfMessage( 'shareddescriptionfollows' );
130 if ( !$fol->isDisabled() ) {
131 $wgOut->addWikiText( $fol->plain() );
133 $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription
. "</div>\n" );
136 $this->closeShowImage();
137 $this->imageHistory();
138 // TODO: Cleanup the following
140 $wgOut->addHTML( Xml
::element( 'h2',
141 array( 'id' => 'filelinks' ),
142 wfMsg( 'imagelinks' ) ) . "\n" );
144 # TODO! FIXME! For some freaky reason, we can't redirect to foreign images.
145 # Yet we return metadata about the target. Definitely an issue in the FileRepo
146 $this->imageRedirects();
149 # Allow extensions to add something after the image links
151 wfRunHooks( 'ImagePageAfterImageLinks', array( $this, &$html ) );
153 $wgOut->addHTML( $html );
157 $wgOut->addHTML( Xml
::element( 'h2', array( 'id' => 'metadata' ), wfMsg( 'metadata' ) ) . "\n" );
158 $wgOut->addWikiText( $this->makeMetadataTable( $formattedMetadata ) );
159 $wgOut->addModules( array( 'mediawiki.legacy.metadata' ) );
162 $css = $this->repo
->getDescriptionStylesheetUrl();
164 $wgOut->addStyle( $css );
168 public function getRedirectTarget() {
170 if ( $this->img
->isLocal() ) {
171 return parent
::getRedirectTarget();
173 // Foreign image page
174 $from = $this->img
->getRedirected();
175 $to = $this->img
->getName();
176 if ( $from == $to ) {
179 return $this->mRedirectTarget
= Title
::makeTitle( NS_FILE
, $to );
182 public function followRedirect() {
184 if ( $this->img
->isLocal() ) {
185 return parent
::followRedirect();
187 $from = $this->img
->getRedirected();
188 $to = $this->img
->getName();
189 if ( $from == $to ) {
192 return Title
::makeTitle( NS_FILE
, $to );
195 public function isRedirect( $text = false ) {
197 if ( $this->img
->isLocal() ) {
198 return parent
::isRedirect( $text );
201 return (bool)$this->img
->getRedirected();
204 public function isLocal() {
206 return $this->img
->isLocal();
209 public function getFile() {
214 public function getDisplayedFile() {
216 return $this->displayImg
;
219 public function getDuplicates() {
221 if ( !is_null( $this->dupes
) ) {
224 $hash = $this->img
->getSha1();
226 return $this->dupes
= array();
228 $dupes = RepoGroup
::singleton()->findBySha1( $hash );
229 // Remove duplicates with self and non matching file sizes
230 $self = $this->img
->getRepoName() . ':' . $this->img
->getName();
231 $size = $this->img
->getSize();
232 foreach ( $dupes as $index => $file ) {
233 $key = $file->getRepoName() . ':' . $file->getName();
234 if ( $key == $self ) {
235 unset( $dupes[$index] );
237 if ( $file->getSize() != $size ) {
238 unset( $dupes[$index] );
241 return $this->dupes
= $dupes;
247 * @param $metadata Boolean: whether or not to show the metadata link
250 protected function showTOC( $metadata ) {
252 '<li><a href="#file">' . wfMsgHtml( 'file-anchor-link' ) . '</a></li>',
253 '<li><a href="#filehistory">' . wfMsgHtml( 'filehist' ) . '</a></li>',
254 '<li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>',
257 $r[] = '<li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>';
260 wfRunHooks( 'ImagePageShowTOC', array( $this, &$r ) );
262 return '<ul id="filetoc">' . implode( "\n", $r ) . '</ul>';
266 * Make a table with metadata to be shown in the output page.
268 * FIXME: bad interface, see note on MediaHandler::formatMetadata().
270 * @param $metadata Array: the array containing the EXIF data
273 protected function makeMetadataTable( $metadata ) {
274 $r = "<div class=\"mw-imagepage-section-metadata\">";
275 $r .= wfMsgNoTrans( 'metadata-help' );
276 $r .= "<table id=\"mw_metadata\" class=\"mw_metadata\">\n";
277 foreach ( $metadata as $type => $stuff ) {
278 foreach ( $stuff as $v ) {
279 # FIXME, why is this using escapeId for a class?!
280 $class = Sanitizer
::escapeId( $v['id'] );
281 if ( $type == 'collapsed' ) {
282 $class .= ' collapsable';
284 $r .= "<tr class=\"$class\">\n";
285 $r .= "<th>{$v['name']}</th>\n";
286 $r .= "<td>{$v['value']}</td>\n</tr>";
289 $r .= "</table>\n</div>\n";
294 * Overloading Article's getContent method.
296 * Omit noarticletext if sharedupload; text will be fetched from the
297 * shared upload server if possible.
299 public function getContent() {
301 if ( $this->img
&& !$this->img
->isLocal() && 0 == $this->getID() ) {
304 return parent
::getContent();
307 protected function openShowImage() {
308 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
309 $wgLang, $wgContLang, $wgEnableUploads;
313 $sizeSel = intval( $wgUser->getOption( 'imagesize' ) );
314 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
315 $sizeSel = User
::getDefaultOption( 'imagesize' );
317 // The user offset might still be incorrect, specially if
318 // $wgImageLimits got changed (see bug #8858).
319 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
320 // Default to the first offset in $wgImageLimits
324 $max = $wgImageLimits[$sizeSel];
326 $maxHeight = $max[1];
327 $sk = $wgUser->getSkin();
328 $dirmark = $wgContLang->getDirMark();
330 if ( $this->displayImg
->exists() ) {
332 $page = $wgRequest->getIntOrNull( 'page' );
333 if ( is_null( $page ) ) {
337 $params = array( 'page' => $page );
339 $width_orig = $this->displayImg
->getWidth( $page );
340 $width = $width_orig;
341 $height_orig = $this->displayImg
->getHeight( $page );
342 $height = $height_orig;
344 $longDesc = wfMsg( 'parentheses', $this->displayImg
->getLongDesc() );
346 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this, &$wgOut ) );
348 if ( $this->displayImg
->allowInlineDisplay() ) {
351 # "Download high res version" link below the image
352 # $msgsize = wfMsgHtml( 'file-info-size', $width_orig, $height_orig, $sk->formatSize( $this->displayImg->getSize() ), $mime );
353 # We'll show a thumbnail of this image
354 if ( $width > $maxWidth ||
$height > $maxHeight ) {
355 # Calculate the thumbnail size.
356 # First case, the limiting factor is the width, not the height.
357 if ( $width / $height >= $maxWidth / $maxHeight ) {
358 $height = round( $height * $maxWidth / $width );
360 # Note that $height <= $maxHeight now.
362 $newwidth = floor( $width * $maxHeight / $height );
363 $height = round( $height * $newwidth / $width );
365 # Note that $height <= $maxHeight now, but might not be identical
366 # because of rounding.
368 $msgbig = wfMsgHtml( 'show-big-image' );
369 $otherSizes = array();
370 foreach ( $wgImageLimits as $size ) {
371 if ( $size[0] < $width_orig && $size[1] < $height_orig &&
372 $size[0] != $width && $size[1] != $height ) {
373 $otherSizes[] = $this->makeSizeLink( $params, $size[0], $size[1] );
376 $msgsmall = wfMessage( 'show-big-image-preview' )->
377 rawParams( $this->makeSizeLink( $params, $width, $height ) )->
379 wfMessage( 'show-big-image-other' )->
380 rawParams( $wgLang->pipeList( $otherSizes ) )->parse();
382 # Image is small enough to show full size on image page
383 $msgsmall = wfMsgExt( 'file-nohires', array( 'parseinline' ) );
386 $params['width'] = $width;
387 $params['height'] = $height;
388 $thumbnail = $this->displayImg
->transform( $params );
391 $anchorclose = '<br />' . $msgsmall;
393 $isMulti = $this->displayImg
->isMultipage() && $this->displayImg
->pageCount() > 1;
395 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
400 'alt' => $this->displayImg
->getTitle()->getPrefixedText(),
403 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
404 $thumbnail->toHtml( $options ) .
405 $anchorclose . "</div>\n" );
409 $count = $this->displayImg
->pageCount();
412 $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
417 array( 'page' => $page - 1 ),
418 array( 'known', 'noclasses' )
420 $thumb1 = $sk->makeThumbLinkObj( $this->mTitle
, $this->displayImg
, $link, $label, 'none',
421 array( 'page' => $page - 1 ) );
426 if ( $page < $count ) {
427 $label = wfMsg( 'imgmultipagenext' );
432 array( 'page' => $page +
1 ),
433 array( 'known', 'noclasses' )
435 $thumb2 = $sk->makeThumbLinkObj( $this->mTitle
, $this->displayImg
, $link, $label, 'none',
436 array( 'page' => $page +
1 ) );
444 'name' => 'pageselector',
445 'action' => $wgScript,
446 'onchange' => 'document.pageselector.submit();',
449 for ( $i = 1; $i <= $count; $i++
) {
450 $options[] = Xml
::option( $wgLang->formatNum( $i ), $i, $i == $page );
452 $select = Xml
::tags( 'select',
453 array( 'id' => 'pageselector', 'name' => 'page' ),
454 implode( "\n", $options ) );
457 '</td><td><div class="multipageimagenavbox">' .
458 Xml
::openElement( 'form', $formParams ) .
459 Html
::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
460 wfMsgExt( 'imgmultigoto', array( 'parseinline', 'replaceafter' ), $select ) .
461 Xml
::submitButton( wfMsg( 'imgmultigo' ) ) .
462 Xml
::closeElement( 'form' ) .
463 "<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>"
467 # if direct link is allowed but it's not a renderable image, show an icon.
468 if ( $this->displayImg
->isSafeFile() ) {
469 $icon = $this->displayImg
->iconThumb();
471 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
472 $icon->toHtml( array( 'file-link' => true ) ) .
480 $filename = wfEscapeWikiText( $this->displayImg
->getName() );
481 $linktext = $filename;
482 if ( isset( $msgbig ) ) {
483 $linktext = wfEscapeWikiText( $msgbig );
485 $medialink = "[[Media:$filename|$linktext]]";
487 if ( !$this->displayImg
->isSafeFile() ) {
488 $warning = wfMsgNoTrans( 'mediawarning' );
489 $wgOut->addWikiText( <<<EOT
490 <div class="fullMedia"><span class="dangerousLink">{$medialink}</span>$dirmark <span class="fileInfo">$longDesc</span></div>
491 <div class="mediaWarning">$warning</div>
495 $wgOut->addWikiText( <<<EOT
496 <div class="fullMedia">{$medialink}{$dirmark} <span class="fileInfo">$longDesc</span>
503 if ( !$this->displayImg
->isLocal() ) {
504 $this->printSharedImageText();
507 # Image does not exist
508 if ( $wgEnableUploads && $wgUser->isAllowed( 'upload' ) ) {
509 // Only show an upload link if the user can upload
510 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
512 'filepage-nofile-link',
513 $uploadTitle->getFullURL( array( 'wpDestFile' => $this->img
->getName() ) )
516 $nofile = 'filepage-nofile';
518 // Note, if there is an image description page, but
519 // no image, then this setRobotPolicy is overriden
520 // by Article::View().
521 $wgOut->setRobotPolicy( 'noindex,nofollow' );
522 $wgOut->wrapWikiMsg( "<div id='mw-imagepage-nofile' class='plainlinks'>\n$1\n</div>", $nofile );
523 if ( !$this->getID() ) {
524 // If there is no image, no shared image, and no description page,
525 // output a 404, to be consistent with articles.
526 $wgRequest->response()->header( 'HTTP/1.1 404 Not Found' );
532 * Creates an thumbnail of specified size and returns an HTML link to it
533 * @param array $params Scaler parameters
537 private function makeSizeLink( $params, $width, $height ) {
540 $params['width'] = $width;
541 $params['height'] = $height;
542 $thumbnail = $this->displayImg
->transform( $params );
543 if ( $thumbnail && !$thumbnail->isError() ) {
544 return Html
::rawElement( 'a', array(
545 'href' => $thumbnail->getUrl(),
546 'class' => 'mw-thumbnail-link'
547 ), wfMessage( 'show-big-image-size' )->numParams(
548 $thumbnail->getWidth(), $thumbnail->getHeight()
556 * Show a notice that the file is from a shared repository
558 protected function printSharedImageText() {
563 $descUrl = $this->img
->getDescriptionUrl();
564 $descText = $this->img
->getDescriptionText();
566 /* Add canonical to head if there is no local page for this shared file */
567 if( $descUrl && $this->getID() == 0 ) {
568 $wgOut->addLink( array( 'rel' => 'canonical', 'href' => $descUrl ) );
571 $wrap = "<div class=\"sharedUploadNotice\">\n$1\n</div>\n";
572 $repo = $this->img
->getRepo()->getDisplayName();
574 if ( $descUrl && $descText && wfMsgNoTrans( 'sharedupload-desc-here' ) !== '-' ) {
575 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-here', $repo, $descUrl ) );
576 } elseif ( $descUrl && wfMsgNoTrans( 'sharedupload-desc-there' ) !== '-' ) {
577 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-there', $repo, $descUrl ) );
579 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload', $repo ), ''/*BACKCOMPAT*/ );
583 $this->mExtraDescription
= $descText;
587 public function getUploadUrl() {
589 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
590 return $uploadTitle->getFullURL( array(
591 'wpDestFile' => $this->img
->getName(),
597 * Print out the various links at the bottom of the image page, e.g. reupload,
598 * external editing (and instructions link) etc.
600 protected function uploadLinksBox() {
601 global $wgUser, $wgOut, $wgEnableUploads, $wgUseExternalEditor;
603 if ( !$wgEnableUploads ) {
608 if ( !$this->img
->isLocal() ) {
612 $sk = $wgUser->getSkin();
614 $wgOut->addHTML( "<br /><ul>\n" );
616 # "Upload a new version of this file" link
617 if ( UploadBase
::userCanReUpload( $wgUser, $this->img
->name
) ) {
618 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
619 $wgOut->addHTML( "<li id=\"mw-imagepage-reupload-link\"><div class=\"plainlinks\">{$ulink}</div></li>\n" );
622 # External editing link
623 if ( $wgUseExternalEditor ) {
626 wfMsgHtml( 'edit-externally' ),
630 'externaledit' => 'true',
633 array( 'known', 'noclasses' )
636 '<li id="mw-imagepage-edit-external">' . $elink . ' <small>' .
637 wfMsgExt( 'edit-externally-help', array( 'parseinline' ) ) .
642 $wgOut->addHTML( "</ul>\n" );
645 protected function closeShowImage() { } # For overloading
648 * If the page we've just displayed is in the "Image" namespace,
649 * we follow it with an upload history of the image and its usage.
651 protected function imageHistory() {
655 $pager = new ImageHistoryPseudoPager( $this );
656 $wgOut->addHTML( $pager->getBody() );
657 $wgOut->preventClickjacking( $pager->getPreventClickjacking() );
659 $this->img
->resetHistory(); // free db resources
661 # Exist check because we don't want to show this on pages where an image
662 # doesn't exist along with the noimage message, that would suck. -ævar
663 if ( $this->img
->exists() ) {
664 $this->uploadLinksBox();
668 protected function imageLinks() {
669 global $wgUser, $wgOut, $wgLang;
673 $dbr = wfGetDB( DB_SLAVE
);
676 array( 'imagelinks', 'page' ),
677 array( 'page_namespace', 'page_title' ),
678 array( 'il_to' => $this->mTitle
->getDBkey(), 'il_from = page_id' ),
680 array( 'LIMIT' => $limit +
1 )
682 $count = $dbr->numRows( $res );
685 Html
::rawElement( 'div',
686 array( 'id' => 'mw-imagepage-nolinkstoimage' ), "\n$1\n" ),
692 $wgOut->addHTML( "<div id='mw-imagepage-section-linkstoimage'>\n" );
693 if ( $count <= $limit - 1 ) {
694 $wgOut->addWikiMsg( 'linkstoimage', $count );
696 // More links than the limit. Add a link to [[Special:Whatlinkshere]]
697 $wgOut->addWikiMsg( 'linkstoimage-more',
698 $wgLang->formatNum( $limit ),
699 $this->mTitle
->getPrefixedDBkey()
704 Html
::openElement( 'ul',
705 array( 'class' => 'mw-imagepage-linkstoimage' ) ) . "\n"
707 $sk = $wgUser->getSkin();
710 foreach ( $res as $s ) {
712 if ( $count <= $limit ) {
713 // We have not yet reached the extra one that tells us there is more to fetch
718 // Sort the list by namespace:title
719 usort( $elements, array( $this, 'compare' ) );
721 // Create links for every element
722 foreach( $elements as $element ) {
723 $link = $sk->linkKnown( Title
::makeTitle( $element->page_namespace
, $element->page_title
) );
724 $wgOut->addHTML( Html
::rawElement(
726 array( 'id' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace
),
732 $wgOut->addHTML( Html
::closeElement( 'ul' ) . "\n" );
735 // Add a links to [[Special:Whatlinkshere]]
736 if ( $count > $limit ) {
737 $wgOut->addWikiMsg( 'morelinkstoimage', $this->mTitle
->getPrefixedDBkey() );
739 $wgOut->addHTML( Html
::closeElement( 'div' ) . "\n" );
742 protected function imageRedirects() {
743 global $wgUser, $wgOut, $wgLang;
745 $redirects = $this->getTitle()->getRedirectsHere( NS_FILE
);
746 if ( count( $redirects ) == 0 ) {
750 $wgOut->addHTML( "<div id='mw-imagepage-section-redirectstofile'>\n" );
751 $wgOut->addWikiMsg( 'redirectstofile',
752 $wgLang->formatNum( count( $redirects ) )
754 $wgOut->addHTML( "<ul class='mw-imagepage-redirectstofile'>\n" );
756 $sk = $wgUser->getSkin();
757 foreach ( $redirects as $title ) {
762 array( 'redirect' => 'no' ),
763 array( 'known', 'noclasses' )
765 $wgOut->addHTML( "<li>{$link}</li>\n" );
767 $wgOut->addHTML( "</ul></div>\n" );
770 protected function imageDupes() {
771 global $wgOut, $wgUser, $wgLang;
775 $dupes = $this->getDuplicates();
776 if ( count( $dupes ) == 0 ) {
780 $wgOut->addHTML( "<div id='mw-imagepage-section-duplicates'>\n" );
781 $wgOut->addWikiMsg( 'duplicatesoffile',
782 $wgLang->formatNum( count( $dupes ) ), $this->mTitle
->getDBkey()
784 $wgOut->addHTML( "<ul class='mw-imagepage-duplicates'>\n" );
786 $sk = $wgUser->getSkin();
787 foreach ( $dupes as $file ) {
789 if ( $file->isLocal() ) {
795 array( 'known', 'noclasses' )
798 $link = $sk->makeExternalLink( $file->getDescriptionUrl(),
799 $file->getTitle()->getPrefixedText() );
800 $fromSrc = wfMsg( 'shared-repo-from', $file->getRepo()->getDisplayName() );
802 $wgOut->addHTML( "<li>{$link} {$fromSrc}</li>\n" );
804 $wgOut->addHTML( "</ul></div>\n" );
808 * Delete the file, or an earlier version of it
810 public function delete() {
811 global $wgUploadMaintenance;
812 if ( $wgUploadMaintenance && $this->mTitle
&& $this->mTitle
->getNamespace() == NS_FILE
) {
814 $wgOut->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n", array( 'filedelete-maintenance' ) );
819 if ( !$this->img
->exists() ||
!$this->img
->isLocal() ||
$this->img
->getRedirected() ) {
820 // Standard article deletion
824 $deleter = new FileDeleteForm( $this->img
);
829 * Revert the file to an earlier version
831 public function revert() {
833 $reverter = new FileRevertForm( $this->img
);
834 $reverter->execute();
838 * Override handling of action=purge
840 public function doPurge() {
842 if ( $this->img
->exists() ) {
843 wfDebug( 'ImagePage::doPurge purging ' . $this->img
->getName() . "\n" );
844 $update = new HTMLCacheUpdate( $this->mTitle
, 'imagelinks' );
846 $this->img
->upgradeRow();
847 $this->img
->purgeCache();
849 wfDebug( 'ImagePage::doPurge no image for ' . $this->img
->getName() . "; limiting purge to cache only\n" );
850 // even if the file supposedly doesn't exist, force any cached information
851 // to be updated (in case the cached information is wrong)
852 $this->img
->purgeCache();
858 * Display an error with a wikitext description
860 function showError( $description ) {
862 $wgOut->setPageTitle( wfMsg( 'internalerror' ) );
863 $wgOut->setRobotPolicy( 'noindex,nofollow' );
864 $wgOut->setArticleRelated( false );
865 $wgOut->enableClientCache( false );
866 $wgOut->addWikiText( $description );
870 * Callback for usort() to do link sorts by (namespace, title)
871 * Function copied from Title::compare()
873 * @param $a object page to compare with
874 * @param $b object page to compare with
875 * @return Integer: result of string comparison, or namespace comparison
877 protected function compare( $a, $b ) {
878 if ( $a->page_namespace
== $b->page_namespace
) {
879 return strcmp( $a->page_title
, $b->page_title
);
881 return $a->page_namespace
- $b->page_namespace
;
887 * Builds the image revision log shown on image pages
891 class ImageHistoryList
{
906 protected $imagePage;
913 protected $repo, $showThumb;
914 protected $preventClickjacking = false;
917 * @param ImagePage $imagePage
919 public function __construct( $imagePage ) {
920 global $wgUser, $wgShowArchiveThumbnails;
921 $this->skin
= $wgUser->getSkin();
922 $this->current
= $imagePage->getFile();
923 $this->img
= $imagePage->getDisplayedFile();
924 $this->title
= $imagePage->getTitle();
925 $this->imagePage
= $imagePage;
926 $this->showThumb
= $wgShowArchiveThumbnails && $this->img
->canRender();
929 public function getImagePage() {
930 return $this->imagePage
;
933 public function getSkin() {
937 public function getFile() {
941 public function beginImageHistoryList( $navLinks = '' ) {
942 global $wgOut, $wgUser;
943 return Xml
::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) ) . "\n"
944 . "<div id=\"mw-imagepage-section-filehistory\">\n"
945 . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
947 . Xml
::openElement( 'table', array( 'class' => 'wikitable filehistory' ) ) . "\n"
949 . ( $this->current
->isLocal() && ( $wgUser->isAllowedAny( 'delete', 'deletedhistory' ) ) ?
'<td></td>' : '' )
950 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
951 . ( $this->showThumb ?
'<th>' . wfMsgHtml( 'filehist-thumb' ) . '</th>' : '' )
952 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
953 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
954 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
958 public function endImageHistoryList( $navLinks = '' ) {
959 return "</table>\n$navLinks\n</div>\n";
967 public function imageHistoryLine( $iscur, $file ) {
968 global $wgUser, $wgLang;
970 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
971 $img = $iscur ?
$file->getName() : $file->getArchiveName();
972 $user = $file->getUser( 'id' );
973 $usertext = $file->getUser( 'text' );
974 $description = $file->getDescription();
976 $local = $this->current
->isLocal();
977 $row = $selected = '';
980 if ( $local && ( $wgUser->isAllowedAny( 'delete', 'deletedhistory' ) ) ) {
982 # Link to remove from history
983 if ( $wgUser->isAllowed( 'delete' ) ) {
984 $q = array( 'action' => 'delete' );
986 $q['oldimage'] = $img;
988 $row .= $this->skin
->link(
990 wfMsgHtml( $iscur ?
'filehist-deleteall' : 'filehist-deleteone' ),
991 array(), $q, array( 'known' )
994 # Link to hide content. Don't show useless link to people who cannot hide revisions.
995 $canHide = $wgUser->isAllowed( 'deleterevision' );
996 if ( $canHide ||
( $wgUser->isAllowed( 'deletedhistory' ) && $file->getVisibility() ) ) {
997 if ( $wgUser->isAllowed( 'delete' ) ) {
1000 // If file is top revision or locked from this user, don't link
1001 if ( $iscur ||
!$file->userCan( File
::DELETED_RESTRICTED
) ) {
1002 $del = $this->skin
->revDeleteLinkDisabled( $canHide );
1004 list( $ts, $name ) = explode( '!', $img, 2 );
1006 'type' => 'oldimage',
1007 'target' => $this->title
->getPrefixedText(),
1010 $del = $this->skin
->revDeleteLink( $query,
1011 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1018 // Reversion link/current indicator
1021 $row .= wfMsgHtml( 'filehist-current' );
1022 } elseif ( $local && $wgUser->isLoggedIn() && $this->title
->userCan( 'edit' ) ) {
1023 if ( $file->isDeleted( File
::DELETED_FILE
) ) {
1024 $row .= wfMsgHtml( 'filehist-revert' );
1026 $row .= $this->skin
->link(
1028 wfMsgHtml( 'filehist-revert' ),
1031 'action' => 'revert',
1033 'wpEditToken' => $wgUser->editToken( $img )
1035 array( 'known', 'noclasses' )
1041 // Date/time and image link
1042 if ( $file->getTimestamp() === $this->img
->getTimestamp() ) {
1043 $selected = "class='filehistory-selected'";
1045 $row .= "<td $selected style='white-space: nowrap;'>";
1046 if ( !$file->userCan( File
::DELETED_FILE
) ) {
1047 # Don't link to unviewable files
1048 $row .= '<span class="history-deleted">' . $wgLang->timeAndDate( $timestamp, true ) . '</span>';
1049 } elseif ( $file->isDeleted( File
::DELETED_FILE
) ) {
1050 $this->preventClickjacking();
1051 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
1052 # Make a link to review the image
1053 $url = $this->skin
->link(
1055 $wgLang->timeAndDate( $timestamp, true ),
1058 'target' => $this->title
->getPrefixedText(),
1060 'token' => $wgUser->editToken( $img )
1062 array( 'known', 'noclasses' )
1064 $row .= '<span class="history-deleted">' . $url . '</span>';
1066 $url = $iscur ?
$this->current
->getUrl() : $this->current
->getArchiveUrl( $img );
1067 $row .= Xml
::element( 'a', array( 'href' => $url ), $wgLang->timeAndDate( $timestamp, true ) );
1072 if ( $this->showThumb
) {
1073 $row .= '<td>' . $this->getThumbForLine( $file ) . '</td>';
1076 // Image dimensions + size
1078 $row .= htmlspecialchars( $file->getDimensionsString() );
1079 $row .= ' <span style="white-space: nowrap;">(' . $this->skin
->formatSize( $file->getSize() ) . ')</span>';
1085 // Hide deleted usernames
1086 if ( $file->isDeleted( File
::DELETED_USER
) ) {
1087 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1089 $row .= $this->skin
->userLink( $user, $usertext ) . ' <span style="white-space: nowrap;">' .
1090 $this->skin
->userToolLinks( $user, $usertext ) . '</span>';
1093 $row .= htmlspecialchars( $usertext );
1095 $row .= '</td><td>';
1097 // Don't show deleted descriptions
1098 if ( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1099 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span>';
1101 $row .= $this->skin
->commentBlock( $description, $this->title
);
1106 wfRunHooks( 'ImagePageFileHistoryLine', array( $this, $file, &$row, &$rowClass ) );
1107 $classAttr = $rowClass ?
" class='$rowClass'" : '';
1109 return "<tr{$classAttr}>{$row}</tr>\n";
1116 protected function getThumbForLine( $file ) {
1119 if ( $file->allowInlineDisplay() && $file->userCan( File
::DELETED_FILE
) && !$file->isDeleted( File
::DELETED_FILE
) ) {
1124 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
1126 $thumbnail = $file->transform( $params );
1128 'alt' => wfMsg( 'filehist-thumbtext',
1129 $wgLang->timeAndDate( $timestamp, true ),
1130 $wgLang->date( $timestamp, true ),
1131 $wgLang->time( $timestamp, true ) ),
1132 'file-link' => true,
1135 if ( !$thumbnail ) {
1136 return wfMsgHtml( 'filehist-nothumb' );
1139 return $thumbnail->toHtml( $options );
1141 return wfMsgHtml( 'filehist-nothumb' );
1145 protected function preventClickjacking( $enable = true ) {
1146 $this->preventClickjacking
= $enable;
1149 public function getPreventClickjacking() {
1150 return $this->preventClickjacking
;
1154 class ImageHistoryPseudoPager
extends ReverseChronologicalPager
{
1155 protected $preventClickjacking = false;
1168 * @param ImagePage $imagePage
1170 function __construct( $imagePage ) {
1171 parent
::__construct();
1172 $this->mImagePage
= $imagePage;
1173 $this->mTitle
= clone ( $imagePage->getTitle() );
1174 $this->mTitle
->setFragment( '#filehistory' );
1176 $this->mHist
= array();
1177 $this->mRange
= array( 0, 0 ); // display range
1180 function getTitle() {
1181 return $this->mTitle
;
1184 function getQueryInfo() {
1188 function getIndexField() {
1192 function formatRow( $row ) {
1196 function getBody() {
1199 if ( count( $this->mHist
) ) {
1200 $list = new ImageHistoryList( $this->mImagePage
);
1201 # Generate prev/next links
1202 $navLink = $this->getNavigationBar();
1203 $s = $list->beginImageHistoryList( $navLink );
1204 // Skip rows there just for paging links
1205 for ( $i = $this->mRange
[0]; $i <= $this->mRange
[1]; $i++
) {
1206 $file = $this->mHist
[$i];
1207 $s .= $list->imageHistoryLine( !$file->isOld(), $file );
1209 $s .= $list->endImageHistoryList( $navLink );
1211 if ( $list->getPreventClickjacking() ) {
1212 $this->preventClickjacking();
1218 function doQuery() {
1219 if ( $this->mQueryDone
) {
1222 $this->mImg
= $this->mImagePage
->getFile(); // ensure loading
1223 if ( !$this->mImg
->exists() ) {
1226 $queryLimit = $this->mLimit +
1; // limit plus extra row
1227 if ( $this->mIsBackwards
) {
1228 // Fetch the file history
1229 $this->mHist
= $this->mImg
->getHistory( $queryLimit, null, $this->mOffset
, false );
1230 // The current rev may not meet the offset/limit
1231 $numRows = count( $this->mHist
);
1232 if ( $numRows <= $this->mLimit
&& $this->mImg
->getTimestamp() > $this->mOffset
) {
1233 $this->mHist
= array_merge( array( $this->mImg
), $this->mHist
);
1236 // The current rev may not meet the offset
1237 if ( !$this->mOffset ||
$this->mImg
->getTimestamp() < $this->mOffset
) {
1238 $this->mHist
[] = $this->mImg
;
1240 // Old image versions (fetch extra row for nav links)
1241 $oiLimit = count( $this->mHist
) ?
$this->mLimit
: $this->mLimit +
1;
1242 // Fetch the file history
1243 $this->mHist
= array_merge( $this->mHist
,
1244 $this->mImg
->getHistory( $oiLimit, $this->mOffset
, null, false ) );
1246 $numRows = count( $this->mHist
); // Total number of query results
1248 # Index value of top item in the list
1249 $firstIndex = $this->mIsBackwards ?
1250 $this->mHist
[$numRows - 1]->getTimestamp() : $this->mHist
[0]->getTimestamp();
1251 # Discard the extra result row if there is one
1252 if ( $numRows > $this->mLimit
&& $numRows > 1 ) {
1253 if ( $this->mIsBackwards
) {
1254 # Index value of item past the index
1255 $this->mPastTheEndIndex
= $this->mHist
[0]->getTimestamp();
1256 # Index value of bottom item in the list
1257 $lastIndex = $this->mHist
[1]->getTimestamp();
1259 $this->mRange
= array( 1, $numRows - 1 );
1261 # Index value of item past the index
1262 $this->mPastTheEndIndex
= $this->mHist
[$numRows - 1]->getTimestamp();
1263 # Index value of bottom item in the list
1264 $lastIndex = $this->mHist
[$numRows - 2]->getTimestamp();
1266 $this->mRange
= array( 0, $numRows - 2 );
1269 # Setting indexes to an empty string means that they will be
1270 # omitted if they would otherwise appear in URLs. It just so
1271 # happens that this is the right thing to do in the standard
1272 # UI, in all the relevant cases.
1273 $this->mPastTheEndIndex
= '';
1274 # Index value of bottom item in the list
1275 $lastIndex = $this->mIsBackwards ?
1276 $this->mHist
[0]->getTimestamp() : $this->mHist
[$numRows - 1]->getTimestamp();
1278 $this->mRange
= array( 0, $numRows - 1 );
1283 $this->mPastTheEndIndex
= '';
1285 if ( $this->mIsBackwards
) {
1286 $this->mIsFirst
= ( $numRows < $queryLimit );
1287 $this->mIsLast
= ( $this->mOffset
== '' );
1288 $this->mLastShown
= $firstIndex;
1289 $this->mFirstShown
= $lastIndex;
1291 $this->mIsFirst
= ( $this->mOffset
== '' );
1292 $this->mIsLast
= ( $numRows < $queryLimit );
1293 $this->mLastShown
= $lastIndex;
1294 $this->mFirstShown
= $firstIndex;
1296 $this->mQueryDone
= true;
1299 protected function preventClickjacking( $enable = true ) {
1300 $this->preventClickjacking
= $enable;
1303 public function getPreventClickjacking() {
1304 return $this->preventClickjacking
;