4 * Special handling for image description pages
8 class ImagePage
extends Article
{
18 var $mExtraDescription = false;
21 function __construct( $title ) {
22 parent
::__construct( $title );
31 public function setFile( $file ) {
32 $this->displayImg
= $file;
34 $this->fileLoaded
= true;
37 protected function loadFile() {
38 if ( $this->fileLoaded
) {
41 $this->fileLoaded
= true;
43 $this->displayImg
= $this->img
= false;
44 wfRunHooks( 'ImagePageFindFile', array( $this, &$this->img
, &$this->displayImg
) );
46 $this->img
= wfFindFile( $this->mTitle
);
48 $this->img
= wfLocalFile( $this->mTitle
);
51 if ( !$this->displayImg
) {
52 $this->displayImg
= $this->img
;
54 $this->repo
= $this->img
->getRepo();
58 * Handler for action=render
59 * Include body text only; none of the image extras
61 public function render() {
63 $wgOut->setArticleBodyOnly( true );
67 public function view() {
68 global $wgOut, $wgShowEXIF, $wgRequest, $wgUser;
70 $diff = $wgRequest->getVal( 'diff' );
71 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
73 if ( $this->mTitle
->getNamespace() != NS_FILE ||
( isset( $diff ) && $diffOnly ) ) {
74 return parent
::view();
79 if ( $this->mTitle
->getNamespace() == NS_FILE
&& $this->img
->getRedirected() ) {
80 if ( $this->mTitle
->getDBkey() == $this->img
->getName() ||
isset( $diff ) ) {
81 // mTitle is the same as the redirect target so ask Article
82 // to perform the redirect for us.
83 $wgRequest->setVal( 'diffonly', 'true' );
84 return parent
::view();
86 // mTitle is not the same as the redirect target so it is
87 // probably the redirect page itself. Fake the redirect symbol
88 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
89 $wgOut->addHTML( $this->viewRedirect( Title
::makeTitle( NS_FILE
, $this->img
->getName() ),
90 /* $appendSubtitle */ true, /* $forceKnown */ true ) );
96 $this->showRedirectedFromHeader();
98 if ( $wgShowEXIF && $this->displayImg
->exists() ) {
99 // FIXME: bad interface, see note on MediaHandler::formatMetadata().
100 $formattedMetadata = $this->displayImg
->formatMetadata();
101 $showmeta = $formattedMetadata !== false;
106 if ( !$diff && $this->displayImg
->exists() ) {
107 $wgOut->addHTML( $this->showTOC( $showmeta ) );
111 $this->openShowImage();
114 # No need to display noarticletext, we use our own message, output in openShowImage()
115 if ( $this->getID() ) {
118 # Just need to set the right headers
119 $wgOut->setArticleFlag( true );
120 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
121 $this->viewUpdates();
124 # Show shared description, if needed
125 if ( $this->mExtraDescription
) {
126 $fol = wfMessage( 'shareddescriptionfollows' );
127 if ( !$fol->isDisabled() ) {
128 $wgOut->addWikiText( $fol->plain() );
130 $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription
. "</div>\n" );
133 $this->closeShowImage();
134 $this->imageHistory();
135 // TODO: Cleanup the following
137 $wgOut->addHTML( Xml
::element( 'h2',
138 array( 'id' => 'filelinks' ),
139 wfMsg( 'imagelinks' ) ) . "\n" );
141 # TODO! FIXME! For some freaky reason, we can't redirect to foreign images.
142 # Yet we return metadata about the target. Definitely an issue in the FileRepo
143 $this->imageRedirects();
146 # Allow extensions to add something after the image links
148 wfRunHooks( 'ImagePageAfterImageLinks', array( $this, &$html ) );
150 $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.action.view.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 );
198 return (bool)$this->img
->getRedirected();
201 public function isLocal() {
203 return $this->img
->isLocal();
206 public function getFile() {
211 public function getDisplayedFile() {
213 return $this->displayImg
;
216 public function getDuplicates() {
218 if ( !is_null( $this->dupes
) ) {
221 $hash = $this->img
->getSha1();
223 return $this->dupes
= array();
225 $dupes = RepoGroup
::singleton()->findBySha1( $hash );
226 // Remove duplicates with self and non matching file sizes
227 $self = $this->img
->getRepoName() . ':' . $this->img
->getName();
228 $size = $this->img
->getSize();
229 foreach ( $dupes as $index => $file ) {
230 $key = $file->getRepoName() . ':' . $file->getName();
231 if ( $key == $self ) {
232 unset( $dupes[$index] );
234 if ( $file->getSize() != $size ) {
235 unset( $dupes[$index] );
238 return $this->dupes
= $dupes;
244 * @param $metadata Boolean: whether or not to show the metadata link
247 protected function showTOC( $metadata ) {
249 '<li><a href="#file">' . wfMsgHtml( 'file-anchor-link' ) . '</a></li>',
250 '<li><a href="#filehistory">' . wfMsgHtml( 'filehist' ) . '</a></li>',
251 '<li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>',
254 $r[] = '<li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>';
257 wfRunHooks( 'ImagePageShowTOC', array( $this, &$r ) );
259 return '<ul id="filetoc">' . implode( "\n", $r ) . '</ul>';
263 * Make a table with metadata to be shown in the output page.
265 * FIXME: bad interface, see note on MediaHandler::formatMetadata().
267 * @param $metadata Array: the array containing the EXIF data
268 * @return String The metadata table. This is treated as Wikitext (!)
270 protected function makeMetadataTable( $metadata ) {
271 $r = "<div class=\"mw-imagepage-section-metadata\">";
272 $r .= wfMsgNoTrans( 'metadata-help' );
273 $r .= "<table id=\"mw_metadata\" class=\"mw_metadata\">\n";
274 foreach ( $metadata as $type => $stuff ) {
275 foreach ( $stuff as $v ) {
276 # FIXME, why is this using escapeId for a class?!
277 $class = Sanitizer
::escapeId( $v['id'] );
278 if ( $type == 'collapsed' ) {
279 $class .= ' collapsable';
281 $r .= "<tr class=\"$class\">\n";
282 $r .= "<th>{$v['name']}</th>\n";
283 $r .= "<td>{$v['value']}</td>\n</tr>";
286 $r .= "</table>\n</div>\n";
291 * Overloading Article's getContent method.
293 * Omit noarticletext if sharedupload; text will be fetched from the
294 * shared upload server if possible.
296 public function getContent() {
298 if ( $this->img
&& !$this->img
->isLocal() && 0 == $this->getID() ) {
301 return parent
::getContent();
304 protected function openShowImage() {
305 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
306 $wgLang, $wgContLang, $wgEnableUploads;
310 $sizeSel = intval( $wgUser->getOption( 'imagesize' ) );
311 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
312 $sizeSel = User
::getDefaultOption( 'imagesize' );
314 // The user offset might still be incorrect, specially if
315 // $wgImageLimits got changed (see bug #8858).
316 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
317 // Default to the first offset in $wgImageLimits
321 $max = $wgImageLimits[$sizeSel];
323 $maxHeight = $max[1];
324 $sk = $wgUser->getSkin();
325 $dirmark = $wgContLang->getDirMark();
327 if ( $this->displayImg
->exists() ) {
329 $page = $wgRequest->getIntOrNull( 'page' );
330 if ( is_null( $page ) ) {
334 $params = array( 'page' => $page );
336 $width_orig = $this->displayImg
->getWidth( $page );
337 $width = $width_orig;
338 $height_orig = $this->displayImg
->getHeight( $page );
339 $height = $height_orig;
341 $longDesc = wfMsg( 'parentheses', $this->displayImg
->getLongDesc() );
343 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this, &$wgOut ) );
345 if ( $this->displayImg
->allowInlineDisplay() ) {
348 # "Download high res version" link below the image
349 # $msgsize = wfMsgHtml( 'file-info-size', $width_orig, $height_orig, $sk->formatSize( $this->displayImg->getSize() ), $mime );
350 # We'll show a thumbnail of this image
351 if ( $width > $maxWidth ||
$height > $maxHeight ) {
352 # Calculate the thumbnail size.
353 # First case, the limiting factor is the width, not the height.
354 if ( $width / $height >= $maxWidth / $maxHeight ) {
355 $height = round( $height * $maxWidth / $width );
357 # Note that $height <= $maxHeight now.
359 $newwidth = floor( $width * $maxHeight / $height );
360 $height = round( $height * $newwidth / $width );
362 # Note that $height <= $maxHeight now, but might not be identical
363 # because of rounding.
365 $msgbig = wfMsgHtml( 'show-big-image' );
366 $otherSizes = array();
367 foreach ( $wgImageLimits as $size ) {
368 if ( $size[0] < $width_orig && $size[1] < $height_orig &&
369 $size[0] != $width && $size[1] != $height ) {
370 $otherSizes[] = $this->makeSizeLink( $params, $size[0], $size[1] );
373 $msgsmall = wfMessage( 'show-big-image-preview' )->
374 rawParams( $this->makeSizeLink( $params, $width, $height ) )->
376 wfMessage( 'show-big-image-other' )->
377 rawParams( $wgLang->pipeList( $otherSizes ) )->parse();
379 # Image is small enough to show full size on image page
380 $msgsmall = wfMsgExt( 'file-nohires', array( 'parseinline' ) );
383 $params['width'] = $width;
384 $params['height'] = $height;
385 $thumbnail = $this->displayImg
->transform( $params );
388 $anchorclose = '<br />' . $msgsmall;
390 $isMulti = $this->displayImg
->isMultipage() && $this->displayImg
->pageCount() > 1;
392 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
397 'alt' => $this->displayImg
->getTitle()->getPrefixedText(),
400 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
401 $thumbnail->toHtml( $options ) .
402 $anchorclose . "</div>\n" );
406 $count = $this->displayImg
->pageCount();
409 $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
414 array( 'page' => $page - 1 ),
415 array( 'known', 'noclasses' )
417 $thumb1 = $sk->makeThumbLinkObj( $this->mTitle
, $this->displayImg
, $link, $label, 'none',
418 array( 'page' => $page - 1 ) );
423 if ( $page < $count ) {
424 $label = wfMsg( 'imgmultipagenext' );
429 array( 'page' => $page +
1 ),
430 array( 'known', 'noclasses' )
432 $thumb2 = $sk->makeThumbLinkObj( $this->mTitle
, $this->displayImg
, $link, $label, 'none',
433 array( 'page' => $page +
1 ) );
441 'name' => 'pageselector',
442 'action' => $wgScript,
443 'onchange' => 'document.pageselector.submit();',
446 for ( $i = 1; $i <= $count; $i++
) {
447 $options[] = Xml
::option( $wgLang->formatNum( $i ), $i, $i == $page );
449 $select = Xml
::tags( 'select',
450 array( 'id' => 'pageselector', 'name' => 'page' ),
451 implode( "\n", $options ) );
454 '</td><td><div class="multipageimagenavbox">' .
455 Xml
::openElement( 'form', $formParams ) .
456 Html
::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
457 wfMsgExt( 'imgmultigoto', array( 'parseinline', 'replaceafter' ), $select ) .
458 Xml
::submitButton( wfMsg( 'imgmultigo' ) ) .
459 Xml
::closeElement( 'form' ) .
460 "<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>"
464 # if direct link is allowed but it's not a renderable image, show an icon.
465 if ( $this->displayImg
->isSafeFile() ) {
466 $icon = $this->displayImg
->iconThumb();
468 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
469 $icon->toHtml( array( 'file-link' => true ) ) .
477 $filename = wfEscapeWikiText( $this->displayImg
->getName() );
478 $linktext = $filename;
479 if ( isset( $msgbig ) ) {
480 $linktext = wfEscapeWikiText( $msgbig );
482 $medialink = "[[Media:$filename|$linktext]]";
484 if ( !$this->displayImg
->isSafeFile() ) {
485 $warning = wfMsgNoTrans( 'mediawarning' );
486 $wgOut->addWikiText( <<<EOT
487 <div class="fullMedia"><span class="dangerousLink">{$medialink}</span>$dirmark <span class="fileInfo">$longDesc</span></div>
488 <div class="mediaWarning">$warning</div>
492 $wgOut->addWikiText( <<<EOT
493 <div class="fullMedia">{$medialink}{$dirmark} <span class="fileInfo">$longDesc</span>
500 if ( !$this->displayImg
->isLocal() ) {
501 $this->printSharedImageText();
504 # Image does not exist
505 if ( $wgEnableUploads && $wgUser->isAllowed( 'upload' ) ) {
506 // Only show an upload link if the user can upload
507 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
509 'filepage-nofile-link',
510 $uploadTitle->getFullURL( array( 'wpDestFile' => $this->img
->getName() ) )
513 $nofile = 'filepage-nofile';
515 // Note, if there is an image description page, but
516 // no image, then this setRobotPolicy is overriden
517 // by Article::View().
518 $wgOut->setRobotPolicy( 'noindex,nofollow' );
519 $wgOut->wrapWikiMsg( "<div id='mw-imagepage-nofile' class='plainlinks'>\n$1\n</div>", $nofile );
520 if ( !$this->getID() ) {
521 // If there is no image, no shared image, and no description page,
522 // output a 404, to be consistent with articles.
523 $wgRequest->response()->header( 'HTTP/1.1 404 Not Found' );
529 * Creates an thumbnail of specified size and returns an HTML link to it
530 * @param array $params Scaler parameters
534 private function makeSizeLink( $params, $width, $height ) {
535 $params['width'] = $width;
536 $params['height'] = $height;
537 $thumbnail = $this->displayImg
->transform( $params );
538 if ( $thumbnail && !$thumbnail->isError() ) {
539 return Html
::rawElement( 'a', array(
540 'href' => $thumbnail->getUrl(),
541 'class' => 'mw-thumbnail-link'
542 ), wfMessage( 'show-big-image-size' )->numParams(
543 $thumbnail->getWidth(), $thumbnail->getHeight()
551 * Show a notice that the file is from a shared repository
553 protected function printSharedImageText() {
558 $descUrl = $this->img
->getDescriptionUrl();
559 $descText = $this->img
->getDescriptionText();
561 /* Add canonical to head if there is no local page for this shared file */
562 if( $descUrl && $this->getID() == 0 ) {
563 $wgOut->addLink( array( 'rel' => 'canonical', 'href' => $descUrl ) );
566 $wrap = "<div class=\"sharedUploadNotice\">\n$1\n</div>\n";
567 $repo = $this->img
->getRepo()->getDisplayName();
569 if ( $descUrl && $descText && wfMsgNoTrans( 'sharedupload-desc-here' ) !== '-' ) {
570 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-here', $repo, $descUrl ) );
571 } elseif ( $descUrl && wfMsgNoTrans( 'sharedupload-desc-there' ) !== '-' ) {
572 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-there', $repo, $descUrl ) );
574 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload', $repo ), ''/*BACKCOMPAT*/ );
578 $this->mExtraDescription
= $descText;
582 public function getUploadUrl() {
584 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
585 return $uploadTitle->getFullURL( array(
586 'wpDestFile' => $this->img
->getName(),
592 * Print out the various links at the bottom of the image page, e.g. reupload,
593 * external editing (and instructions link) etc.
595 protected function uploadLinksBox() {
596 global $wgUser, $wgOut, $wgEnableUploads, $wgUseExternalEditor;
598 if ( !$wgEnableUploads ) {
603 if ( !$this->img
->isLocal() ) {
607 $sk = $wgUser->getSkin();
609 $wgOut->addHTML( "<br /><ul>\n" );
611 # "Upload a new version of this file" link
612 if ( UploadBase
::userCanReUpload( $wgUser, $this->img
->name
) ) {
613 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
614 $wgOut->addHTML( "<li id=\"mw-imagepage-reupload-link\"><div class=\"plainlinks\">{$ulink}</div></li>\n" );
617 # External editing link
618 if ( $wgUseExternalEditor ) {
621 wfMsgHtml( 'edit-externally' ),
625 'externaledit' => 'true',
628 array( 'known', 'noclasses' )
631 '<li id="mw-imagepage-edit-external">' . $elink . ' <small>' .
632 wfMsgExt( 'edit-externally-help', array( 'parseinline' ) ) .
637 $wgOut->addHTML( "</ul>\n" );
640 protected function closeShowImage() { } # For overloading
643 * If the page we've just displayed is in the "Image" namespace,
644 * we follow it with an upload history of the image and its usage.
646 protected function imageHistory() {
650 $pager = new ImageHistoryPseudoPager( $this );
651 $wgOut->addHTML( $pager->getBody() );
652 $wgOut->preventClickjacking( $pager->getPreventClickjacking() );
654 $this->img
->resetHistory(); // free db resources
656 # Exist check because we don't want to show this on pages where an image
657 # doesn't exist along with the noimage message, that would suck. -ævar
658 if ( $this->img
->exists() ) {
659 $this->uploadLinksBox();
663 protected function imageLinks() {
664 global $wgUser, $wgOut, $wgLang;
668 $dbr = wfGetDB( DB_SLAVE
);
671 array( 'imagelinks', 'page' ),
672 array( 'page_namespace', 'page_title' ),
673 array( 'il_to' => $this->mTitle
->getDBkey(), 'il_from = page_id' ),
675 array( 'LIMIT' => $limit +
1 )
677 $count = $dbr->numRows( $res );
680 Html
::rawElement( 'div',
681 array( 'id' => 'mw-imagepage-nolinkstoimage' ), "\n$1\n" ),
687 $wgOut->addHTML( "<div id='mw-imagepage-section-linkstoimage'>\n" );
688 if ( $count <= $limit - 1 ) {
689 $wgOut->addWikiMsg( 'linkstoimage', $count );
691 // More links than the limit. Add a link to [[Special:Whatlinkshere]]
692 $wgOut->addWikiMsg( 'linkstoimage-more',
693 $wgLang->formatNum( $limit ),
694 $this->mTitle
->getPrefixedDBkey()
699 Html
::openElement( 'ul',
700 array( 'class' => 'mw-imagepage-linkstoimage' ) ) . "\n"
702 $sk = $wgUser->getSkin();
705 foreach ( $res as $s ) {
707 if ( $count <= $limit ) {
708 // We have not yet reached the extra one that tells us there is more to fetch
713 // Sort the list by namespace:title
714 usort( $elements, array( $this, 'compare' ) );
716 // Create links for every element
717 foreach( $elements as $element ) {
718 $link = $sk->linkKnown( Title
::makeTitle( $element->page_namespace
, $element->page_title
) );
719 $wgOut->addHTML( Html
::rawElement(
721 array( 'id' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace
),
727 $wgOut->addHTML( Html
::closeElement( 'ul' ) . "\n" );
730 // Add a links to [[Special:Whatlinkshere]]
731 if ( $count > $limit ) {
732 $wgOut->addWikiMsg( 'morelinkstoimage', $this->mTitle
->getPrefixedDBkey() );
734 $wgOut->addHTML( Html
::closeElement( 'div' ) . "\n" );
737 protected function imageRedirects() {
738 global $wgUser, $wgOut, $wgLang;
740 $redirects = $this->getTitle()->getRedirectsHere( NS_FILE
);
741 if ( count( $redirects ) == 0 ) {
745 $wgOut->addHTML( "<div id='mw-imagepage-section-redirectstofile'>\n" );
746 $wgOut->addWikiMsg( 'redirectstofile',
747 $wgLang->formatNum( count( $redirects ) )
749 $wgOut->addHTML( "<ul class='mw-imagepage-redirectstofile'>\n" );
751 $sk = $wgUser->getSkin();
752 foreach ( $redirects as $title ) {
757 array( 'redirect' => 'no' ),
758 array( 'known', 'noclasses' )
760 $wgOut->addHTML( "<li>{$link}</li>\n" );
762 $wgOut->addHTML( "</ul></div>\n" );
765 protected function imageDupes() {
766 global $wgOut, $wgUser, $wgLang;
770 $dupes = $this->getDuplicates();
771 if ( count( $dupes ) == 0 ) {
775 $wgOut->addHTML( "<div id='mw-imagepage-section-duplicates'>\n" );
776 $wgOut->addWikiMsg( 'duplicatesoffile',
777 $wgLang->formatNum( count( $dupes ) ), $this->mTitle
->getDBkey()
779 $wgOut->addHTML( "<ul class='mw-imagepage-duplicates'>\n" );
781 $sk = $wgUser->getSkin();
782 foreach ( $dupes as $file ) {
784 if ( $file->isLocal() ) {
790 array( 'known', 'noclasses' )
793 $link = $sk->makeExternalLink( $file->getDescriptionUrl(),
794 $file->getTitle()->getPrefixedText() );
795 $fromSrc = wfMsg( 'shared-repo-from', $file->getRepo()->getDisplayName() );
797 $wgOut->addHTML( "<li>{$link} {$fromSrc}</li>\n" );
799 $wgOut->addHTML( "</ul></div>\n" );
803 * Delete the file, or an earlier version of it
805 public function delete() {
806 global $wgUploadMaintenance;
807 if ( $wgUploadMaintenance && $this->mTitle
&& $this->mTitle
->getNamespace() == NS_FILE
) {
809 $wgOut->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n", array( 'filedelete-maintenance' ) );
814 if ( !$this->img
->exists() ||
!$this->img
->isLocal() ||
$this->img
->getRedirected() ) {
815 // Standard article deletion
819 $deleter = new FileDeleteForm( $this->img
);
824 * Revert the file to an earlier version
826 public function revert() {
828 $reverter = new FileRevertForm( $this->img
);
829 $reverter->execute();
833 * Override handling of action=purge
835 public function doPurge() {
837 if ( $this->img
->exists() ) {
838 wfDebug( 'ImagePage::doPurge purging ' . $this->img
->getName() . "\n" );
839 $update = new HTMLCacheUpdate( $this->mTitle
, 'imagelinks' );
841 $this->img
->upgradeRow();
842 $this->img
->purgeCache();
844 wfDebug( 'ImagePage::doPurge no image for ' . $this->img
->getName() . "; limiting purge to cache only\n" );
845 // even if the file supposedly doesn't exist, force any cached information
846 // to be updated (in case the cached information is wrong)
847 $this->img
->purgeCache();
853 * Display an error with a wikitext description
855 function showError( $description ) {
857 $wgOut->setPageTitle( wfMsg( 'internalerror' ) );
858 $wgOut->setRobotPolicy( 'noindex,nofollow' );
859 $wgOut->setArticleRelated( false );
860 $wgOut->enableClientCache( false );
861 $wgOut->addWikiText( $description );
865 * Callback for usort() to do link sorts by (namespace, title)
866 * Function copied from Title::compare()
868 * @param $a object page to compare with
869 * @param $b object page to compare with
870 * @return Integer: result of string comparison, or namespace comparison
872 protected function compare( $a, $b ) {
873 if ( $a->page_namespace
== $b->page_namespace
) {
874 return strcmp( $a->page_title
, $b->page_title
);
876 return $a->page_namespace
- $b->page_namespace
;
882 * Builds the image revision log shown on image pages
886 class ImageHistoryList
{
901 protected $imagePage;
908 protected $repo, $showThumb;
909 protected $preventClickjacking = false;
912 * @param ImagePage $imagePage
914 public function __construct( $imagePage ) {
915 global $wgUser, $wgShowArchiveThumbnails;
916 $this->skin
= $wgUser->getSkin();
917 $this->current
= $imagePage->getFile();
918 $this->img
= $imagePage->getDisplayedFile();
919 $this->title
= $imagePage->getTitle();
920 $this->imagePage
= $imagePage;
921 $this->showThumb
= $wgShowArchiveThumbnails && $this->img
->canRender();
924 public function getImagePage() {
925 return $this->imagePage
;
928 public function getSkin() {
932 public function getFile() {
936 public function beginImageHistoryList( $navLinks = '' ) {
937 global $wgOut, $wgUser;
938 return Xml
::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) ) . "\n"
939 . "<div id=\"mw-imagepage-section-filehistory\">\n"
940 . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
942 . Xml
::openElement( 'table', array( 'class' => 'wikitable filehistory' ) ) . "\n"
944 . ( $this->current
->isLocal() && ( $wgUser->isAllowedAny( 'delete', 'deletedhistory' ) ) ?
'<td></td>' : '' )
945 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
946 . ( $this->showThumb ?
'<th>' . wfMsgHtml( 'filehist-thumb' ) . '</th>' : '' )
947 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
948 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
949 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
953 public function endImageHistoryList( $navLinks = '' ) {
954 return "</table>\n$navLinks\n</div>\n";
962 public function imageHistoryLine( $iscur, $file ) {
963 global $wgUser, $wgLang;
965 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
966 $img = $iscur ?
$file->getName() : $file->getArchiveName();
967 $user = $file->getUser( 'id' );
968 $usertext = $file->getUser( 'text' );
969 $description = $file->getDescription();
971 $local = $this->current
->isLocal();
972 $row = $selected = '';
975 if ( $local && ( $wgUser->isAllowedAny( 'delete', 'deletedhistory' ) ) ) {
977 # Link to remove from history
978 if ( $wgUser->isAllowed( 'delete' ) ) {
979 $q = array( 'action' => 'delete' );
981 $q['oldimage'] = $img;
983 $row .= $this->skin
->link(
985 wfMsgHtml( $iscur ?
'filehist-deleteall' : 'filehist-deleteone' ),
986 array(), $q, array( 'known' )
989 # Link to hide content. Don't show useless link to people who cannot hide revisions.
990 $canHide = $wgUser->isAllowed( 'deleterevision' );
991 if ( $canHide ||
( $wgUser->isAllowed( 'deletedhistory' ) && $file->getVisibility() ) ) {
992 if ( $wgUser->isAllowed( 'delete' ) ) {
995 // If file is top revision or locked from this user, don't link
996 if ( $iscur ||
!$file->userCan( File
::DELETED_RESTRICTED
) ) {
997 $del = $this->skin
->revDeleteLinkDisabled( $canHide );
999 list( $ts, $name ) = explode( '!', $img, 2 );
1001 'type' => 'oldimage',
1002 'target' => $this->title
->getPrefixedText(),
1005 $del = $this->skin
->revDeleteLink( $query,
1006 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1013 // Reversion link/current indicator
1016 $row .= wfMsgHtml( 'filehist-current' );
1017 } elseif ( $local && $wgUser->isLoggedIn() && $this->title
->userCan( 'edit' ) ) {
1018 if ( $file->isDeleted( File
::DELETED_FILE
) ) {
1019 $row .= wfMsgHtml( 'filehist-revert' );
1021 $row .= $this->skin
->link(
1023 wfMsgHtml( 'filehist-revert' ),
1026 'action' => 'revert',
1028 'wpEditToken' => $wgUser->editToken( $img )
1030 array( 'known', 'noclasses' )
1036 // Date/time and image link
1037 if ( $file->getTimestamp() === $this->img
->getTimestamp() ) {
1038 $selected = "class='filehistory-selected'";
1040 $row .= "<td $selected style='white-space: nowrap;'>";
1041 if ( !$file->userCan( File
::DELETED_FILE
) ) {
1042 # Don't link to unviewable files
1043 $row .= '<span class="history-deleted">' . $wgLang->timeAndDate( $timestamp, true ) . '</span>';
1044 } elseif ( $file->isDeleted( File
::DELETED_FILE
) ) {
1046 $this->preventClickjacking();
1047 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
1048 # Make a link to review the image
1049 $url = $this->skin
->link(
1051 $wgLang->timeAndDate( $timestamp, true ),
1054 'target' => $this->title
->getPrefixedText(),
1056 'token' => $wgUser->editToken( $img )
1058 array( 'known', 'noclasses' )
1061 $url = $wgLang->timeAndDate( $timestamp, true );
1063 $row .= '<span class="history-deleted">' . $url . '</span>';
1065 $url = $iscur ?
$this->current
->getUrl() : $this->current
->getArchiveUrl( $img );
1066 $row .= Xml
::element( 'a', array( 'href' => $url ), $wgLang->timeAndDate( $timestamp, true ) );
1071 if ( $this->showThumb
) {
1072 $row .= '<td>' . $this->getThumbForLine( $file ) . '</td>';
1075 // Image dimensions + size
1077 $row .= htmlspecialchars( $file->getDimensionsString() );
1078 $row .= ' <span style="white-space: nowrap;">(' . $this->skin
->formatSize( $file->getSize() ) . ')</span>';
1083 // Hide deleted usernames
1084 if ( $file->isDeleted( File
::DELETED_USER
) ) {
1085 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1088 $row .= $this->skin
->userLink( $user, $usertext ) . ' <span style="white-space: nowrap;">' .
1089 $this->skin
->userToolLinks( $user, $usertext ) . '</span>';
1091 $row .= htmlspecialchars( $usertext );
1094 $row .= '</td><td>';
1096 // Don't show deleted descriptions
1097 if ( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1098 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span>';
1100 $row .= $this->skin
->commentBlock( $description, $this->title
, false, false );
1105 wfRunHooks( 'ImagePageFileHistoryLine', array( $this, $file, &$row, &$rowClass ) );
1106 $classAttr = $rowClass ?
" class='$rowClass'" : '';
1108 return "<tr{$classAttr}>{$row}</tr>\n";
1115 protected function getThumbForLine( $file ) {
1118 if ( $file->allowInlineDisplay() && $file->userCan( File
::DELETED_FILE
) && !$file->isDeleted( File
::DELETED_FILE
) ) {
1123 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
1125 $thumbnail = $file->transform( $params );
1127 'alt' => wfMsg( 'filehist-thumbtext',
1128 $wgLang->timeAndDate( $timestamp, true ),
1129 $wgLang->date( $timestamp, true ),
1130 $wgLang->time( $timestamp, true ) ),
1131 'file-link' => true,
1134 if ( !$thumbnail ) {
1135 return wfMsgHtml( 'filehist-nothumb' );
1138 return $thumbnail->toHtml( $options );
1140 return wfMsgHtml( 'filehist-nothumb' );
1144 protected function preventClickjacking( $enable = true ) {
1145 $this->preventClickjacking
= $enable;
1148 public function getPreventClickjacking() {
1149 return $this->preventClickjacking
;
1153 class ImageHistoryPseudoPager
extends ReverseChronologicalPager
{
1154 protected $preventClickjacking = false;
1167 * @param ImagePage $imagePage
1169 function __construct( $imagePage ) {
1170 parent
::__construct();
1171 $this->mImagePage
= $imagePage;
1172 $this->mTitle
= clone ( $imagePage->getTitle() );
1173 $this->mTitle
->setFragment( '#filehistory' );
1175 $this->mHist
= array();
1176 $this->mRange
= array( 0, 0 ); // display range
1179 function getTitle() {
1180 return $this->mTitle
;
1183 function getQueryInfo() {
1187 function getIndexField() {
1191 function formatRow( $row ) {
1195 function getBody() {
1198 if ( count( $this->mHist
) ) {
1199 $list = new ImageHistoryList( $this->mImagePage
);
1200 # Generate prev/next links
1201 $navLink = $this->getNavigationBar();
1202 $s = $list->beginImageHistoryList( $navLink );
1203 // Skip rows there just for paging links
1204 for ( $i = $this->mRange
[0]; $i <= $this->mRange
[1]; $i++
) {
1205 $file = $this->mHist
[$i];
1206 $s .= $list->imageHistoryLine( !$file->isOld(), $file );
1208 $s .= $list->endImageHistoryList( $navLink );
1210 if ( $list->getPreventClickjacking() ) {
1211 $this->preventClickjacking();
1217 function doQuery() {
1218 if ( $this->mQueryDone
) {
1221 $this->mImg
= $this->mImagePage
->getFile(); // ensure loading
1222 if ( !$this->mImg
->exists() ) {
1225 $queryLimit = $this->mLimit +
1; // limit plus extra row
1226 if ( $this->mIsBackwards
) {
1227 // Fetch the file history
1228 $this->mHist
= $this->mImg
->getHistory( $queryLimit, null, $this->mOffset
, false );
1229 // The current rev may not meet the offset/limit
1230 $numRows = count( $this->mHist
);
1231 if ( $numRows <= $this->mLimit
&& $this->mImg
->getTimestamp() > $this->mOffset
) {
1232 $this->mHist
= array_merge( array( $this->mImg
), $this->mHist
);
1235 // The current rev may not meet the offset
1236 if ( !$this->mOffset ||
$this->mImg
->getTimestamp() < $this->mOffset
) {
1237 $this->mHist
[] = $this->mImg
;
1239 // Old image versions (fetch extra row for nav links)
1240 $oiLimit = count( $this->mHist
) ?
$this->mLimit
: $this->mLimit +
1;
1241 // Fetch the file history
1242 $this->mHist
= array_merge( $this->mHist
,
1243 $this->mImg
->getHistory( $oiLimit, $this->mOffset
, null, false ) );
1245 $numRows = count( $this->mHist
); // Total number of query results
1247 # Index value of top item in the list
1248 $firstIndex = $this->mIsBackwards ?
1249 $this->mHist
[$numRows - 1]->getTimestamp() : $this->mHist
[0]->getTimestamp();
1250 # Discard the extra result row if there is one
1251 if ( $numRows > $this->mLimit
&& $numRows > 1 ) {
1252 if ( $this->mIsBackwards
) {
1253 # Index value of item past the index
1254 $this->mPastTheEndIndex
= $this->mHist
[0]->getTimestamp();
1255 # Index value of bottom item in the list
1256 $lastIndex = $this->mHist
[1]->getTimestamp();
1258 $this->mRange
= array( 1, $numRows - 1 );
1260 # Index value of item past the index
1261 $this->mPastTheEndIndex
= $this->mHist
[$numRows - 1]->getTimestamp();
1262 # Index value of bottom item in the list
1263 $lastIndex = $this->mHist
[$numRows - 2]->getTimestamp();
1265 $this->mRange
= array( 0, $numRows - 2 );
1268 # Setting indexes to an empty string means that they will be
1269 # omitted if they would otherwise appear in URLs. It just so
1270 # happens that this is the right thing to do in the standard
1271 # UI, in all the relevant cases.
1272 $this->mPastTheEndIndex
= '';
1273 # Index value of bottom item in the list
1274 $lastIndex = $this->mIsBackwards ?
1275 $this->mHist
[0]->getTimestamp() : $this->mHist
[$numRows - 1]->getTimestamp();
1277 $this->mRange
= array( 0, $numRows - 1 );
1282 $this->mPastTheEndIndex
= '';
1284 if ( $this->mIsBackwards
) {
1285 $this->mIsFirst
= ( $numRows < $queryLimit );
1286 $this->mIsLast
= ( $this->mOffset
== '' );
1287 $this->mLastShown
= $firstIndex;
1288 $this->mFirstShown
= $lastIndex;
1290 $this->mIsFirst
= ( $this->mOffset
== '' );
1291 $this->mIsLast
= ( $numRows < $queryLimit );
1292 $this->mLastShown
= $lastIndex;
1293 $this->mFirstShown
= $firstIndex;
1295 $this->mQueryDone
= true;
1298 protected function preventClickjacking( $enable = true ) {
1299 $this->preventClickjacking
= $enable;
1302 public function getPreventClickjacking() {
1303 return $this->preventClickjacking
;