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 ) {
538 $params['width'] = $width;
539 $params['height'] = $height;
540 $thumbnail = $this->displayImg
->transform( $params );
541 if ( $thumbnail && !$thumbnail->isError() ) {
542 return Html
::rawElement( 'a', array(
543 'href' => $thumbnail->getUrl(),
544 'class' => 'mw-thumbnail-link'
545 ), wfMessage( 'show-big-image-size' )->numParams(
546 $thumbnail->getWidth(), $thumbnail->getHeight()
554 * Show a notice that the file is from a shared repository
556 protected function printSharedImageText() {
561 $descUrl = $this->img
->getDescriptionUrl();
562 $descText = $this->img
->getDescriptionText();
564 /* Add canonical to head if there is no local page for this shared file */
565 if( $descUrl && $this->getID() == 0 ) {
566 $wgOut->addLink( array( 'rel' => 'canonical', 'href' => $descUrl ) );
569 $wrap = "<div class=\"sharedUploadNotice\">\n$1\n</div>\n";
570 $repo = $this->img
->getRepo()->getDisplayName();
572 if ( $descUrl && $descText && wfMsgNoTrans( 'sharedupload-desc-here' ) !== '-' ) {
573 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-here', $repo, $descUrl ) );
574 } elseif ( $descUrl && wfMsgNoTrans( 'sharedupload-desc-there' ) !== '-' ) {
575 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-there', $repo, $descUrl ) );
577 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload', $repo ), ''/*BACKCOMPAT*/ );
581 $this->mExtraDescription
= $descText;
585 public function getUploadUrl() {
587 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
588 return $uploadTitle->getFullURL( array(
589 'wpDestFile' => $this->img
->getName(),
595 * Print out the various links at the bottom of the image page, e.g. reupload,
596 * external editing (and instructions link) etc.
598 protected function uploadLinksBox() {
599 global $wgUser, $wgOut, $wgEnableUploads, $wgUseExternalEditor;
601 if ( !$wgEnableUploads ) {
606 if ( !$this->img
->isLocal() ) {
610 $sk = $wgUser->getSkin();
612 $wgOut->addHTML( "<br /><ul>\n" );
614 # "Upload a new version of this file" link
615 if ( UploadBase
::userCanReUpload( $wgUser, $this->img
->name
) ) {
616 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
617 $wgOut->addHTML( "<li id=\"mw-imagepage-reupload-link\"><div class=\"plainlinks\">{$ulink}</div></li>\n" );
620 # External editing link
621 if ( $wgUseExternalEditor ) {
624 wfMsgHtml( 'edit-externally' ),
628 'externaledit' => 'true',
631 array( 'known', 'noclasses' )
634 '<li id="mw-imagepage-edit-external">' . $elink . ' <small>' .
635 wfMsgExt( 'edit-externally-help', array( 'parseinline' ) ) .
640 $wgOut->addHTML( "</ul>\n" );
643 protected function closeShowImage() { } # For overloading
646 * If the page we've just displayed is in the "Image" namespace,
647 * we follow it with an upload history of the image and its usage.
649 protected function imageHistory() {
653 $pager = new ImageHistoryPseudoPager( $this );
654 $wgOut->addHTML( $pager->getBody() );
655 $wgOut->preventClickjacking( $pager->getPreventClickjacking() );
657 $this->img
->resetHistory(); // free db resources
659 # Exist check because we don't want to show this on pages where an image
660 # doesn't exist along with the noimage message, that would suck. -ævar
661 if ( $this->img
->exists() ) {
662 $this->uploadLinksBox();
666 protected function imageLinks() {
667 global $wgUser, $wgOut, $wgLang;
671 $dbr = wfGetDB( DB_SLAVE
);
674 array( 'imagelinks', 'page' ),
675 array( 'page_namespace', 'page_title' ),
676 array( 'il_to' => $this->mTitle
->getDBkey(), 'il_from = page_id' ),
678 array( 'LIMIT' => $limit +
1 )
680 $count = $dbr->numRows( $res );
683 Html
::rawElement( 'div',
684 array( 'id' => 'mw-imagepage-nolinkstoimage' ), "\n$1\n" ),
690 $wgOut->addHTML( "<div id='mw-imagepage-section-linkstoimage'>\n" );
691 if ( $count <= $limit - 1 ) {
692 $wgOut->addWikiMsg( 'linkstoimage', $count );
694 // More links than the limit. Add a link to [[Special:Whatlinkshere]]
695 $wgOut->addWikiMsg( 'linkstoimage-more',
696 $wgLang->formatNum( $limit ),
697 $this->mTitle
->getPrefixedDBkey()
702 Html
::openElement( 'ul',
703 array( 'class' => 'mw-imagepage-linkstoimage' ) ) . "\n"
705 $sk = $wgUser->getSkin();
708 foreach ( $res as $s ) {
710 if ( $count <= $limit ) {
711 // We have not yet reached the extra one that tells us there is more to fetch
716 // Sort the list by namespace:title
717 usort( $elements, array( $this, 'compare' ) );
719 // Create links for every element
720 foreach( $elements as $element ) {
721 $link = $sk->linkKnown( Title
::makeTitle( $element->page_namespace
, $element->page_title
) );
722 $wgOut->addHTML( Html
::rawElement(
724 array( 'id' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace
),
730 $wgOut->addHTML( Html
::closeElement( 'ul' ) . "\n" );
733 // Add a links to [[Special:Whatlinkshere]]
734 if ( $count > $limit ) {
735 $wgOut->addWikiMsg( 'morelinkstoimage', $this->mTitle
->getPrefixedDBkey() );
737 $wgOut->addHTML( Html
::closeElement( 'div' ) . "\n" );
740 protected function imageRedirects() {
741 global $wgUser, $wgOut, $wgLang;
743 $redirects = $this->getTitle()->getRedirectsHere( NS_FILE
);
744 if ( count( $redirects ) == 0 ) {
748 $wgOut->addHTML( "<div id='mw-imagepage-section-redirectstofile'>\n" );
749 $wgOut->addWikiMsg( 'redirectstofile',
750 $wgLang->formatNum( count( $redirects ) )
752 $wgOut->addHTML( "<ul class='mw-imagepage-redirectstofile'>\n" );
754 $sk = $wgUser->getSkin();
755 foreach ( $redirects as $title ) {
760 array( 'redirect' => 'no' ),
761 array( 'known', 'noclasses' )
763 $wgOut->addHTML( "<li>{$link}</li>\n" );
765 $wgOut->addHTML( "</ul></div>\n" );
768 protected function imageDupes() {
769 global $wgOut, $wgUser, $wgLang;
773 $dupes = $this->getDuplicates();
774 if ( count( $dupes ) == 0 ) {
778 $wgOut->addHTML( "<div id='mw-imagepage-section-duplicates'>\n" );
779 $wgOut->addWikiMsg( 'duplicatesoffile',
780 $wgLang->formatNum( count( $dupes ) ), $this->mTitle
->getDBkey()
782 $wgOut->addHTML( "<ul class='mw-imagepage-duplicates'>\n" );
784 $sk = $wgUser->getSkin();
785 foreach ( $dupes as $file ) {
787 if ( $file->isLocal() ) {
793 array( 'known', 'noclasses' )
796 $link = $sk->makeExternalLink( $file->getDescriptionUrl(),
797 $file->getTitle()->getPrefixedText() );
798 $fromSrc = wfMsg( 'shared-repo-from', $file->getRepo()->getDisplayName() );
800 $wgOut->addHTML( "<li>{$link} {$fromSrc}</li>\n" );
802 $wgOut->addHTML( "</ul></div>\n" );
806 * Delete the file, or an earlier version of it
808 public function delete() {
809 global $wgUploadMaintenance;
810 if ( $wgUploadMaintenance && $this->mTitle
&& $this->mTitle
->getNamespace() == NS_FILE
) {
812 $wgOut->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n", array( 'filedelete-maintenance' ) );
817 if ( !$this->img
->exists() ||
!$this->img
->isLocal() ||
$this->img
->getRedirected() ) {
818 // Standard article deletion
822 $deleter = new FileDeleteForm( $this->img
);
827 * Revert the file to an earlier version
829 public function revert() {
831 $reverter = new FileRevertForm( $this->img
);
832 $reverter->execute();
836 * Override handling of action=purge
838 public function doPurge() {
840 if ( $this->img
->exists() ) {
841 wfDebug( 'ImagePage::doPurge purging ' . $this->img
->getName() . "\n" );
842 $update = new HTMLCacheUpdate( $this->mTitle
, 'imagelinks' );
844 $this->img
->upgradeRow();
845 $this->img
->purgeCache();
847 wfDebug( 'ImagePage::doPurge no image for ' . $this->img
->getName() . "; limiting purge to cache only\n" );
848 // even if the file supposedly doesn't exist, force any cached information
849 // to be updated (in case the cached information is wrong)
850 $this->img
->purgeCache();
856 * Display an error with a wikitext description
858 function showError( $description ) {
860 $wgOut->setPageTitle( wfMsg( 'internalerror' ) );
861 $wgOut->setRobotPolicy( 'noindex,nofollow' );
862 $wgOut->setArticleRelated( false );
863 $wgOut->enableClientCache( false );
864 $wgOut->addWikiText( $description );
868 * Callback for usort() to do link sorts by (namespace, title)
869 * Function copied from Title::compare()
871 * @param $a object page to compare with
872 * @param $b object page to compare with
873 * @return Integer: result of string comparison, or namespace comparison
875 protected function compare( $a, $b ) {
876 if ( $a->page_namespace
== $b->page_namespace
) {
877 return strcmp( $a->page_title
, $b->page_title
);
879 return $a->page_namespace
- $b->page_namespace
;
885 * Builds the image revision log shown on image pages
889 class ImageHistoryList
{
904 protected $imagePage;
911 protected $repo, $showThumb;
912 protected $preventClickjacking = false;
915 * @param ImagePage $imagePage
917 public function __construct( $imagePage ) {
918 global $wgUser, $wgShowArchiveThumbnails;
919 $this->skin
= $wgUser->getSkin();
920 $this->current
= $imagePage->getFile();
921 $this->img
= $imagePage->getDisplayedFile();
922 $this->title
= $imagePage->getTitle();
923 $this->imagePage
= $imagePage;
924 $this->showThumb
= $wgShowArchiveThumbnails && $this->img
->canRender();
927 public function getImagePage() {
928 return $this->imagePage
;
931 public function getSkin() {
935 public function getFile() {
939 public function beginImageHistoryList( $navLinks = '' ) {
940 global $wgOut, $wgUser;
941 return Xml
::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) ) . "\n"
942 . "<div id=\"mw-imagepage-section-filehistory\">\n"
943 . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
945 . Xml
::openElement( 'table', array( 'class' => 'wikitable filehistory' ) ) . "\n"
947 . ( $this->current
->isLocal() && ( $wgUser->isAllowedAny( 'delete', 'deletedhistory' ) ) ?
'<td></td>' : '' )
948 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
949 . ( $this->showThumb ?
'<th>' . wfMsgHtml( 'filehist-thumb' ) . '</th>' : '' )
950 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
951 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
952 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
956 public function endImageHistoryList( $navLinks = '' ) {
957 return "</table>\n$navLinks\n</div>\n";
965 public function imageHistoryLine( $iscur, $file ) {
966 global $wgUser, $wgLang;
968 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
969 $img = $iscur ?
$file->getName() : $file->getArchiveName();
970 $user = $file->getUser( 'id' );
971 $usertext = $file->getUser( 'text' );
972 $description = $file->getDescription();
974 $local = $this->current
->isLocal();
975 $row = $selected = '';
978 if ( $local && ( $wgUser->isAllowedAny( 'delete', 'deletedhistory' ) ) ) {
980 # Link to remove from history
981 if ( $wgUser->isAllowed( 'delete' ) ) {
982 $q = array( 'action' => 'delete' );
984 $q['oldimage'] = $img;
986 $row .= $this->skin
->link(
988 wfMsgHtml( $iscur ?
'filehist-deleteall' : 'filehist-deleteone' ),
989 array(), $q, array( 'known' )
992 # Link to hide content. Don't show useless link to people who cannot hide revisions.
993 $canHide = $wgUser->isAllowed( 'deleterevision' );
994 if ( $canHide ||
( $wgUser->isAllowed( 'deletedhistory' ) && $file->getVisibility() ) ) {
995 if ( $wgUser->isAllowed( 'delete' ) ) {
998 // If file is top revision or locked from this user, don't link
999 if ( $iscur ||
!$file->userCan( File
::DELETED_RESTRICTED
) ) {
1000 $del = $this->skin
->revDeleteLinkDisabled( $canHide );
1002 list( $ts, $name ) = explode( '!', $img, 2 );
1004 'type' => 'oldimage',
1005 'target' => $this->title
->getPrefixedText(),
1008 $del = $this->skin
->revDeleteLink( $query,
1009 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1016 // Reversion link/current indicator
1019 $row .= wfMsgHtml( 'filehist-current' );
1020 } elseif ( $local && $wgUser->isLoggedIn() && $this->title
->userCan( 'edit' ) ) {
1021 if ( $file->isDeleted( File
::DELETED_FILE
) ) {
1022 $row .= wfMsgHtml( 'filehist-revert' );
1024 $row .= $this->skin
->link(
1026 wfMsgHtml( 'filehist-revert' ),
1029 'action' => 'revert',
1031 'wpEditToken' => $wgUser->editToken( $img )
1033 array( 'known', 'noclasses' )
1039 // Date/time and image link
1040 if ( $file->getTimestamp() === $this->img
->getTimestamp() ) {
1041 $selected = "class='filehistory-selected'";
1043 $row .= "<td $selected style='white-space: nowrap;'>";
1044 if ( !$file->userCan( File
::DELETED_FILE
) ) {
1045 # Don't link to unviewable files
1046 $row .= '<span class="history-deleted">' . $wgLang->timeAndDate( $timestamp, true ) . '</span>';
1047 } elseif ( $file->isDeleted( File
::DELETED_FILE
) ) {
1049 $this->preventClickjacking();
1050 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
1051 # Make a link to review the image
1052 $url = $this->skin
->link(
1054 $wgLang->timeAndDate( $timestamp, true ),
1057 'target' => $this->title
->getPrefixedText(),
1059 'token' => $wgUser->editToken( $img )
1061 array( 'known', 'noclasses' )
1064 $url = $wgLang->timeAndDate( $timestamp, true );
1066 $row .= '<span class="history-deleted">' . $url . '</span>';
1068 $url = $iscur ?
$this->current
->getUrl() : $this->current
->getArchiveUrl( $img );
1069 $row .= Xml
::element( 'a', array( 'href' => $url ), $wgLang->timeAndDate( $timestamp, true ) );
1074 if ( $this->showThumb
) {
1075 $row .= '<td>' . $this->getThumbForLine( $file ) . '</td>';
1078 // Image dimensions + size
1080 $row .= htmlspecialchars( $file->getDimensionsString() );
1081 $row .= ' <span style="white-space: nowrap;">(' . $this->skin
->formatSize( $file->getSize() ) . ')</span>';
1086 // Hide deleted usernames
1087 if ( $file->isDeleted( File
::DELETED_USER
) ) {
1088 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1091 $row .= $this->skin
->userLink( $user, $usertext ) . ' <span style="white-space: nowrap;">' .
1092 $this->skin
->userToolLinks( $user, $usertext ) . '</span>';
1094 $row .= htmlspecialchars( $usertext );
1097 $row .= '</td><td>';
1099 // Don't show deleted descriptions
1100 if ( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1101 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span>';
1103 $row .= $this->skin
->commentBlock( $description, $this->title
);
1108 wfRunHooks( 'ImagePageFileHistoryLine', array( $this, $file, &$row, &$rowClass ) );
1109 $classAttr = $rowClass ?
" class='$rowClass'" : '';
1111 return "<tr{$classAttr}>{$row}</tr>\n";
1118 protected function getThumbForLine( $file ) {
1121 if ( $file->allowInlineDisplay() && $file->userCan( File
::DELETED_FILE
) && !$file->isDeleted( File
::DELETED_FILE
) ) {
1126 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
1128 $thumbnail = $file->transform( $params );
1130 'alt' => wfMsg( 'filehist-thumbtext',
1131 $wgLang->timeAndDate( $timestamp, true ),
1132 $wgLang->date( $timestamp, true ),
1133 $wgLang->time( $timestamp, true ) ),
1134 'file-link' => true,
1137 if ( !$thumbnail ) {
1138 return wfMsgHtml( 'filehist-nothumb' );
1141 return $thumbnail->toHtml( $options );
1143 return wfMsgHtml( 'filehist-nothumb' );
1147 protected function preventClickjacking( $enable = true ) {
1148 $this->preventClickjacking
= $enable;
1151 public function getPreventClickjacking() {
1152 return $this->preventClickjacking
;
1156 class ImageHistoryPseudoPager
extends ReverseChronologicalPager
{
1157 protected $preventClickjacking = false;
1170 * @param ImagePage $imagePage
1172 function __construct( $imagePage ) {
1173 parent
::__construct();
1174 $this->mImagePage
= $imagePage;
1175 $this->mTitle
= clone ( $imagePage->getTitle() );
1176 $this->mTitle
->setFragment( '#filehistory' );
1178 $this->mHist
= array();
1179 $this->mRange
= array( 0, 0 ); // display range
1182 function getTitle() {
1183 return $this->mTitle
;
1186 function getQueryInfo() {
1190 function getIndexField() {
1194 function formatRow( $row ) {
1198 function getBody() {
1201 if ( count( $this->mHist
) ) {
1202 $list = new ImageHistoryList( $this->mImagePage
);
1203 # Generate prev/next links
1204 $navLink = $this->getNavigationBar();
1205 $s = $list->beginImageHistoryList( $navLink );
1206 // Skip rows there just for paging links
1207 for ( $i = $this->mRange
[0]; $i <= $this->mRange
[1]; $i++
) {
1208 $file = $this->mHist
[$i];
1209 $s .= $list->imageHistoryLine( !$file->isOld(), $file );
1211 $s .= $list->endImageHistoryList( $navLink );
1213 if ( $list->getPreventClickjacking() ) {
1214 $this->preventClickjacking();
1220 function doQuery() {
1221 if ( $this->mQueryDone
) {
1224 $this->mImg
= $this->mImagePage
->getFile(); // ensure loading
1225 if ( !$this->mImg
->exists() ) {
1228 $queryLimit = $this->mLimit +
1; // limit plus extra row
1229 if ( $this->mIsBackwards
) {
1230 // Fetch the file history
1231 $this->mHist
= $this->mImg
->getHistory( $queryLimit, null, $this->mOffset
, false );
1232 // The current rev may not meet the offset/limit
1233 $numRows = count( $this->mHist
);
1234 if ( $numRows <= $this->mLimit
&& $this->mImg
->getTimestamp() > $this->mOffset
) {
1235 $this->mHist
= array_merge( array( $this->mImg
), $this->mHist
);
1238 // The current rev may not meet the offset
1239 if ( !$this->mOffset ||
$this->mImg
->getTimestamp() < $this->mOffset
) {
1240 $this->mHist
[] = $this->mImg
;
1242 // Old image versions (fetch extra row for nav links)
1243 $oiLimit = count( $this->mHist
) ?
$this->mLimit
: $this->mLimit +
1;
1244 // Fetch the file history
1245 $this->mHist
= array_merge( $this->mHist
,
1246 $this->mImg
->getHistory( $oiLimit, $this->mOffset
, null, false ) );
1248 $numRows = count( $this->mHist
); // Total number of query results
1250 # Index value of top item in the list
1251 $firstIndex = $this->mIsBackwards ?
1252 $this->mHist
[$numRows - 1]->getTimestamp() : $this->mHist
[0]->getTimestamp();
1253 # Discard the extra result row if there is one
1254 if ( $numRows > $this->mLimit
&& $numRows > 1 ) {
1255 if ( $this->mIsBackwards
) {
1256 # Index value of item past the index
1257 $this->mPastTheEndIndex
= $this->mHist
[0]->getTimestamp();
1258 # Index value of bottom item in the list
1259 $lastIndex = $this->mHist
[1]->getTimestamp();
1261 $this->mRange
= array( 1, $numRows - 1 );
1263 # Index value of item past the index
1264 $this->mPastTheEndIndex
= $this->mHist
[$numRows - 1]->getTimestamp();
1265 # Index value of bottom item in the list
1266 $lastIndex = $this->mHist
[$numRows - 2]->getTimestamp();
1268 $this->mRange
= array( 0, $numRows - 2 );
1271 # Setting indexes to an empty string means that they will be
1272 # omitted if they would otherwise appear in URLs. It just so
1273 # happens that this is the right thing to do in the standard
1274 # UI, in all the relevant cases.
1275 $this->mPastTheEndIndex
= '';
1276 # Index value of bottom item in the list
1277 $lastIndex = $this->mIsBackwards ?
1278 $this->mHist
[0]->getTimestamp() : $this->mHist
[$numRows - 1]->getTimestamp();
1280 $this->mRange
= array( 0, $numRows - 1 );
1285 $this->mPastTheEndIndex
= '';
1287 if ( $this->mIsBackwards
) {
1288 $this->mIsFirst
= ( $numRows < $queryLimit );
1289 $this->mIsLast
= ( $this->mOffset
== '' );
1290 $this->mLastShown
= $firstIndex;
1291 $this->mFirstShown
= $lastIndex;
1293 $this->mIsFirst
= ( $this->mOffset
== '' );
1294 $this->mIsLast
= ( $numRows < $queryLimit );
1295 $this->mLastShown
= $lastIndex;
1296 $this->mFirstShown
= $firstIndex;
1298 $this->mQueryDone
= true;
1301 protected function preventClickjacking( $enable = true ) {
1302 $this->preventClickjacking
= $enable;
1305 public function getPreventClickjacking() {
1306 return $this->preventClickjacking
;