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 // @todo 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() ) {
116 # When $wgBetterDirectionality is enabled, NS_FILE is in the user language,
117 # but this section (the actual wikitext) should be in page content language
118 $pageLang = $this->mTitle
->getPageLanguage();
119 $wgOut->addHTML( Xml
::openElement( 'div', array( 'id' => 'mw-imagepage-content',
120 'lang' => $pageLang->getCode(), 'dir' => $pageLang->getDir(),
121 'class' => 'mw-content-'.$pageLang->getDir() ) ) );
123 $wgOut->addHTML( Xml
::closeElement( 'div' ) );
125 # Just need to set the right headers
126 $wgOut->setArticleFlag( true );
127 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
128 $this->viewUpdates();
131 # Show shared description, if needed
132 if ( $this->mExtraDescription
) {
133 $fol = wfMessage( 'shareddescriptionfollows' );
134 if ( !$fol->isDisabled() ) {
135 $wgOut->addWikiText( $fol->plain() );
137 $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription
. "</div>\n" );
140 $this->closeShowImage();
141 $this->imageHistory();
142 // TODO: Cleanup the following
144 $wgOut->addHTML( Xml
::element( 'h2',
145 array( 'id' => 'filelinks' ),
146 wfMsg( 'imagelinks' ) ) . "\n" );
148 # @todo FIXME: For some freaky reason, we can't redirect to foreign images.
149 # Yet we return metadata about the target. Definitely an issue in the FileRepo
152 # Allow extensions to add something after the image links
154 wfRunHooks( 'ImagePageAfterImageLinks', array( $this, &$html ) );
156 $wgOut->addHTML( $html );
160 $wgOut->addHTML( Xml
::element( 'h2', array( 'id' => 'metadata' ), wfMsg( 'metadata' ) ) . "\n" );
161 $wgOut->addWikiText( $this->makeMetadataTable( $formattedMetadata ) );
162 $wgOut->addModules( array( 'mediawiki.action.view.metadata' ) );
165 $css = $this->repo
->getDescriptionStylesheetUrl();
167 $wgOut->addStyle( $css );
171 public function getRedirectTarget() {
173 if ( $this->img
->isLocal() ) {
174 return parent
::getRedirectTarget();
176 // Foreign image page
177 $from = $this->img
->getRedirected();
178 $to = $this->img
->getName();
179 if ( $from == $to ) {
182 return $this->mRedirectTarget
= Title
::makeTitle( NS_FILE
, $to );
185 public function followRedirect() {
187 if ( $this->img
->isLocal() ) {
188 return parent
::followRedirect();
190 $from = $this->img
->getRedirected();
191 $to = $this->img
->getName();
192 if ( $from == $to ) {
195 return Title
::makeTitle( NS_FILE
, $to );
198 public function isRedirect( $text = false ) {
200 if ( $this->img
->isLocal() ) {
201 return parent
::isRedirect( $text );
204 return (bool)$this->img
->getRedirected();
207 public function isLocal() {
209 return $this->img
->isLocal();
212 public function getFile() {
217 public function getDisplayedFile() {
219 return $this->displayImg
;
222 public function getDuplicates() {
224 if ( !is_null( $this->dupes
) ) {
227 $hash = $this->img
->getSha1();
229 return $this->dupes
= array();
231 $dupes = RepoGroup
::singleton()->findBySha1( $hash );
232 // Remove duplicates with self and non matching file sizes
233 $self = $this->img
->getRepoName() . ':' . $this->img
->getName();
234 $size = $this->img
->getSize();
235 foreach ( $dupes as $index => $file ) {
236 $key = $file->getRepoName() . ':' . $file->getName();
237 if ( $key == $self ) {
238 unset( $dupes[$index] );
240 if ( $file->getSize() != $size ) {
241 unset( $dupes[$index] );
244 return $this->dupes
= $dupes;
250 * @param $metadata Boolean: whether or not to show the metadata link
253 protected function showTOC( $metadata ) {
255 '<li><a href="#file">' . wfMsgHtml( 'file-anchor-link' ) . '</a></li>',
256 '<li><a href="#filehistory">' . wfMsgHtml( 'filehist' ) . '</a></li>',
257 '<li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>',
260 $r[] = '<li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>';
263 wfRunHooks( 'ImagePageShowTOC', array( $this, &$r ) );
265 return '<ul id="filetoc">' . implode( "\n", $r ) . '</ul>';
269 * Make a table with metadata to be shown in the output page.
271 * @todo FIXME: Bad interface, see note on MediaHandler::formatMetadata().
273 * @param $metadata Array: the array containing the EXIF data
274 * @return String The metadata table. This is treated as Wikitext (!)
276 protected function makeMetadataTable( $metadata ) {
277 $r = "<div class=\"mw-imagepage-section-metadata\">";
278 $r .= wfMsgNoTrans( 'metadata-help' );
279 $r .= "<table id=\"mw_metadata\" class=\"mw_metadata\">\n";
280 foreach ( $metadata as $type => $stuff ) {
281 foreach ( $stuff as $v ) {
282 # @todo FIXME: Why is this using escapeId for a class?!
283 $class = Sanitizer
::escapeId( $v['id'] );
284 if ( $type == 'collapsed' ) {
285 $class .= ' collapsable';
287 $r .= "<tr class=\"$class\">\n";
288 $r .= "<th>{$v['name']}</th>\n";
289 $r .= "<td>{$v['value']}</td>\n</tr>";
292 $r .= "</table>\n</div>\n";
297 * Overloading Article's getContent method.
299 * Omit noarticletext if sharedupload; text will be fetched from the
300 * shared upload server if possible.
302 public function getContent() {
304 if ( $this->img
&& !$this->img
->isLocal() && 0 == $this->getID() ) {
307 return parent
::getContent();
310 protected function openShowImage() {
311 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
312 $wgLang, $wgEnableUploads;
316 $sizeSel = intval( $wgUser->getOption( 'imagesize' ) );
317 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
318 $sizeSel = User
::getDefaultOption( 'imagesize' );
320 // The user offset might still be incorrect, specially if
321 // $wgImageLimits got changed (see bug #8858).
322 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
323 // Default to the first offset in $wgImageLimits
327 $max = $wgImageLimits[$sizeSel];
329 $maxHeight = $max[1];
330 $sk = $wgUser->getSkin();
331 $dirmark = wfUILang()->getDirMark();
333 if ( $this->displayImg
->exists() ) {
335 $page = $wgRequest->getIntOrNull( 'page' );
336 if ( is_null( $page ) ) {
340 $params = array( 'page' => $page );
342 $width_orig = $this->displayImg
->getWidth( $page );
343 $width = $width_orig;
344 $height_orig = $this->displayImg
->getHeight( $page );
345 $height = $height_orig;
347 $longDesc = wfMsg( 'parentheses', $this->displayImg
->getLongDesc() );
349 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this, &$wgOut ) );
351 if ( $this->displayImg
->allowInlineDisplay() ) {
354 # "Download high res version" link below the image
355 # $msgsize = wfMsgHtml( 'file-info-size', $width_orig, $height_orig, $sk->formatSize( $this->displayImg->getSize() ), $mime );
356 # We'll show a thumbnail of this image
357 if ( $width > $maxWidth ||
$height > $maxHeight ) {
358 # Calculate the thumbnail size.
359 # First case, the limiting factor is the width, not the height.
360 if ( $width / $height >= $maxWidth / $maxHeight ) {
361 $height = round( $height * $maxWidth / $width );
363 # Note that $height <= $maxHeight now.
365 $newwidth = floor( $width * $maxHeight / $height );
366 $height = round( $height * $newwidth / $width );
368 # Note that $height <= $maxHeight now, but might not be identical
369 # because of rounding.
371 $msgbig = wfMsgHtml( 'show-big-image' );
372 $otherSizes = array();
373 foreach ( $wgImageLimits as $size ) {
374 if ( $size[0] < $width_orig && $size[1] < $height_orig &&
375 $size[0] != $width && $size[1] != $height ) {
376 $otherSizes[] = $this->makeSizeLink( $params, $size[0], $size[1] );
379 $msgsmall = wfMessage( 'show-big-image-preview' )->
380 rawParams( $this->makeSizeLink( $params, $width, $height ) )->
382 wfMessage( 'show-big-image-other' )->
383 rawParams( $wgLang->pipeList( $otherSizes ) )->parse();
385 # Image is small enough to show full size on image page
386 $msgsmall = wfMsgExt( 'file-nohires', array( 'parseinline' ) );
389 $params['width'] = $width;
390 $params['height'] = $height;
391 $thumbnail = $this->displayImg
->transform( $params );
394 $anchorclose = '<br />' . $msgsmall;
396 $isMulti = $this->displayImg
->isMultipage() && $this->displayImg
->pageCount() > 1;
398 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
403 'alt' => $this->displayImg
->getTitle()->getPrefixedText(),
406 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
407 $thumbnail->toHtml( $options ) .
408 $anchorclose . "</div>\n" );
412 $count = $this->displayImg
->pageCount();
415 $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
420 array( 'page' => $page - 1 ),
421 array( 'known', 'noclasses' )
423 $thumb1 = $sk->makeThumbLinkObj( $this->mTitle
, $this->displayImg
, $link, $label, 'none',
424 array( 'page' => $page - 1 ) );
429 if ( $page < $count ) {
430 $label = wfMsg( 'imgmultipagenext' );
435 array( 'page' => $page +
1 ),
436 array( 'known', 'noclasses' )
438 $thumb2 = $sk->makeThumbLinkObj( $this->mTitle
, $this->displayImg
, $link, $label, 'none',
439 array( 'page' => $page +
1 ) );
447 'name' => 'pageselector',
448 'action' => $wgScript,
449 'onchange' => 'document.pageselector.submit();',
452 for ( $i = 1; $i <= $count; $i++
) {
453 $options[] = Xml
::option( $wgLang->formatNum( $i ), $i, $i == $page );
455 $select = Xml
::tags( 'select',
456 array( 'id' => 'pageselector', 'name' => 'page' ),
457 implode( "\n", $options ) );
460 '</td><td><div class="multipageimagenavbox">' .
461 Xml
::openElement( 'form', $formParams ) .
462 Html
::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
463 wfMsgExt( 'imgmultigoto', array( 'parseinline', 'replaceafter' ), $select ) .
464 Xml
::submitButton( wfMsg( 'imgmultigo' ) ) .
465 Xml
::closeElement( 'form' ) .
466 "<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>"
470 # if direct link is allowed but it's not a renderable image, show an icon.
471 if ( $this->displayImg
->isSafeFile() ) {
472 $icon = $this->displayImg
->iconThumb();
474 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
475 $icon->toHtml( array( 'file-link' => true ) ) .
483 $filename = wfEscapeWikiText( $this->displayImg
->getName() );
484 $linktext = $filename;
485 if ( isset( $msgbig ) ) {
486 $linktext = wfEscapeWikiText( $msgbig );
488 $medialink = "[[Media:$filename|$linktext]]";
490 if ( !$this->displayImg
->isSafeFile() ) {
491 $warning = wfMsgNoTrans( 'mediawarning' );
492 $wgOut->addWikiText( <<<EOT
493 <div class="fullMedia"><span class="dangerousLink">{$medialink}</span>$dirmark <span class="fileInfo">$longDesc</span></div>
494 <div class="mediaWarning">$warning</div>
498 $wgOut->addWikiText( <<<EOT
499 <div class="fullMedia">{$medialink}{$dirmark} <span class="fileInfo">$longDesc</span>
506 if ( !$this->displayImg
->isLocal() ) {
507 $this->printSharedImageText();
510 # Image does not exist
511 if ( $wgEnableUploads && $wgUser->isAllowed( 'upload' ) ) {
512 // Only show an upload link if the user can upload
513 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
515 'filepage-nofile-link',
516 $uploadTitle->getFullURL( array( 'wpDestFile' => $this->img
->getName() ) )
519 $nofile = 'filepage-nofile';
521 // Note, if there is an image description page, but
522 // no image, then this setRobotPolicy is overriden
523 // by Article::View().
524 $wgOut->setRobotPolicy( 'noindex,nofollow' );
525 $wgOut->wrapWikiMsg( "<div id='mw-imagepage-nofile' class='plainlinks'>\n$1\n</div>", $nofile );
526 if ( !$this->getID() ) {
527 // If there is no image, no shared image, and no description page,
528 // output a 404, to be consistent with articles.
529 $wgRequest->response()->header( 'HTTP/1.1 404 Not Found' );
535 * Creates an thumbnail of specified size and returns an HTML link to it
536 * @param array $params Scaler parameters
540 private function makeSizeLink( $params, $width, $height ) {
541 $params['width'] = $width;
542 $params['height'] = $height;
543 $thumbnail = $this->displayImg
->transform( $params );
544 if ( $thumbnail && !$thumbnail->isError() ) {
545 return Html
::rawElement( 'a', array(
546 'href' => $thumbnail->getUrl(),
547 'class' => 'mw-thumbnail-link'
548 ), wfMessage( 'show-big-image-size' )->numParams(
549 $thumbnail->getWidth(), $thumbnail->getHeight()
557 * Show a notice that the file is from a shared repository
559 protected function printSharedImageText() {
564 $descUrl = $this->img
->getDescriptionUrl();
565 $descText = $this->img
->getDescriptionText();
567 /* Add canonical to head if there is no local page for this shared file */
568 if( $descUrl && $this->getID() == 0 ) {
569 $wgOut->addLink( array( 'rel' => 'canonical', 'href' => $descUrl ) );
572 $wrap = "<div class=\"sharedUploadNotice\">\n$1\n</div>\n";
573 $repo = $this->img
->getRepo()->getDisplayName();
575 if ( $descUrl && $descText && wfMsgNoTrans( 'sharedupload-desc-here' ) !== '-' ) {
576 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-here', $repo, $descUrl ) );
577 } elseif ( $descUrl && wfMsgNoTrans( 'sharedupload-desc-there' ) !== '-' ) {
578 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-there', $repo, $descUrl ) );
580 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload', $repo ), ''/*BACKCOMPAT*/ );
584 $this->mExtraDescription
= $descText;
588 public function getUploadUrl() {
590 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
591 return $uploadTitle->getFullURL( array(
592 'wpDestFile' => $this->img
->getName(),
598 * Print out the various links at the bottom of the image page, e.g. reupload,
599 * external editing (and instructions link) etc.
601 protected function uploadLinksBox() {
602 global $wgUser, $wgOut, $wgEnableUploads, $wgUseExternalEditor;
604 if ( !$wgEnableUploads ) {
609 if ( !$this->img
->isLocal() ) {
613 $sk = $wgUser->getSkin();
615 $wgOut->addHTML( "<br /><ul>\n" );
617 # "Upload a new version of this file" link
618 if ( UploadBase
::userCanReUpload( $wgUser, $this->img
->name
) ) {
619 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
620 $wgOut->addHTML( "<li id=\"mw-imagepage-reupload-link\"><div class=\"plainlinks\">{$ulink}</div></li>\n" );
623 # External editing link
624 if ( $wgUseExternalEditor ) {
627 wfMsgHtml( 'edit-externally' ),
631 'externaledit' => 'true',
634 array( 'known', 'noclasses' )
637 '<li id="mw-imagepage-edit-external">' . $elink . ' <small>' .
638 wfMsgExt( 'edit-externally-help', array( 'parseinline' ) ) .
643 $wgOut->addHTML( "</ul>\n" );
646 protected function closeShowImage() { } # For overloading
649 * If the page we've just displayed is in the "Image" namespace,
650 * we follow it with an upload history of the image and its usage.
652 protected function imageHistory() {
656 $pager = new ImageHistoryPseudoPager( $this );
657 $wgOut->addHTML( $pager->getBody() );
658 $wgOut->preventClickjacking( $pager->getPreventClickjacking() );
660 $this->img
->resetHistory(); // free db resources
662 # Exist check because we don't want to show this on pages where an image
663 # doesn't exist along with the noimage message, that would suck. -ævar
664 if ( $this->img
->exists() ) {
665 $this->uploadLinksBox();
669 protected function queryImageLinks( $target, $limit ) {
670 $dbr = wfGetDB( DB_SLAVE
);
673 array( 'imagelinks', 'page' ),
674 array( 'page_namespace', 'page_title', 'page_is_redirect', 'il_to' ),
675 array( 'il_to' => $target, 'il_from = page_id' ),
677 array( 'LIMIT' => $limit +
1 )
681 protected function imageLinks() {
682 global $wgUser, $wgOut, $wgLang;
686 $res = $this->queryImageLinks( $this->mTitle
->getDbKey(), $limit +
1);
688 $redirects = array();
689 foreach ( $res as $row ) {
690 if ( $row->page_is_redirect
) {
691 $redirects[$row->page_title
] = array();
695 $count = count( $rows );
697 $hasMore = $count > $limit;
698 if ( !$hasMore && count( $redirects ) ) {
699 $res = $this->queryImageLinks( array_keys( $redirects ),
700 $limit - count( $rows ) +
1 );
701 foreach ( $res as $row ) {
702 $redirects[$row->il_to
][] = $row;
705 $hasMore = ( $res->numRows() +
count( $rows ) ) > $limit;
710 Html
::rawElement( 'div',
711 array( 'id' => 'mw-imagepage-nolinkstoimage' ), "\n$1\n" ),
717 $wgOut->addHTML( "<div id='mw-imagepage-section-linkstoimage'>\n" );
719 $wgOut->addWikiMsg( 'linkstoimage', $count );
721 // More links than the limit. Add a link to [[Special:Whatlinkshere]]
722 $wgOut->addWikiMsg( 'linkstoimage-more',
723 $wgLang->formatNum( $limit ),
724 $this->mTitle
->getPrefixedDBkey()
729 Html
::openElement( 'ul',
730 array( 'class' => 'mw-imagepage-linkstoimage' ) ) . "\n"
732 $sk = $wgUser->getSkin();
735 // Sort the list by namespace:title
736 usort( $rows, array( $this, 'compare' ) );
738 // Create links for every element
740 foreach( $rows as $element ) {
742 if ( $currentCount > $limit ) {
746 $link = $sk->linkKnown( Title
::makeTitle( $element->page_namespace
, $element->page_title
) );
747 if ( !isset( $redirects[$element->page_title
] ) ) {
750 $ul = "<ul class='mw-imagepage-redirectstofile'>\n";
751 foreach ( $redirects[$element->page_title
] as $row ) {
753 if ( $currentCount > $limit ) {
757 $link2 = $sk->linkKnown( Title
::makeTitle( $row->page_namespace
, $row->page_title
) );
758 $ul .= Html
::rawElement(
760 array( 'id' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace
),
765 $liContents = wfMessage( 'linkstoimage-redirect' )->rawParams(
766 $link, $ul )->parse();
768 $wgOut->addHTML( Html
::rawElement(
770 array( 'id' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace
),
776 $wgOut->addHTML( Html
::closeElement( 'ul' ) . "\n" );
779 // Add a links to [[Special:Whatlinkshere]]
780 if ( $count > $limit ) {
781 $wgOut->addWikiMsg( 'morelinkstoimage', $this->mTitle
->getPrefixedDBkey() );
783 $wgOut->addHTML( Html
::closeElement( 'div' ) . "\n" );
786 protected function imageDupes() {
787 global $wgOut, $wgUser, $wgLang;
791 $dupes = $this->getDuplicates();
792 if ( count( $dupes ) == 0 ) {
796 $wgOut->addHTML( "<div id='mw-imagepage-section-duplicates'>\n" );
797 $wgOut->addWikiMsg( 'duplicatesoffile',
798 $wgLang->formatNum( count( $dupes ) ), $this->mTitle
->getDBkey()
800 $wgOut->addHTML( "<ul class='mw-imagepage-duplicates'>\n" );
802 $sk = $wgUser->getSkin();
803 foreach ( $dupes as $file ) {
805 if ( $file->isLocal() ) {
811 array( 'known', 'noclasses' )
814 $link = $sk->makeExternalLink( $file->getDescriptionUrl(),
815 $file->getTitle()->getPrefixedText() );
816 $fromSrc = wfMsg( 'shared-repo-from', $file->getRepo()->getDisplayName() );
818 $wgOut->addHTML( "<li>{$link} {$fromSrc}</li>\n" );
820 $wgOut->addHTML( "</ul></div>\n" );
824 * Delete the file, or an earlier version of it
826 public function delete() {
827 global $wgUploadMaintenance;
828 if ( $wgUploadMaintenance && $this->mTitle
&& $this->mTitle
->getNamespace() == NS_FILE
) {
830 $wgOut->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n", array( 'filedelete-maintenance' ) );
835 if ( !$this->img
->exists() ||
!$this->img
->isLocal() ||
$this->img
->getRedirected() ) {
836 // Standard article deletion
840 $deleter = new FileDeleteForm( $this->img
);
845 * Revert the file to an earlier version
847 public function revert() {
849 $reverter = new FileRevertForm( $this->img
);
850 $reverter->execute();
854 * Override handling of action=purge
856 public function doPurge() {
858 if ( $this->img
->exists() ) {
859 wfDebug( 'ImagePage::doPurge purging ' . $this->img
->getName() . "\n" );
860 $update = new HTMLCacheUpdate( $this->mTitle
, 'imagelinks' );
862 $this->img
->upgradeRow();
863 $this->img
->purgeCache();
865 wfDebug( 'ImagePage::doPurge no image for ' . $this->img
->getName() . "; limiting purge to cache only\n" );
866 // even if the file supposedly doesn't exist, force any cached information
867 // to be updated (in case the cached information is wrong)
868 $this->img
->purgeCache();
874 * Display an error with a wikitext description
876 function showError( $description ) {
878 $wgOut->setPageTitle( wfMsg( 'internalerror' ) );
879 $wgOut->setRobotPolicy( 'noindex,nofollow' );
880 $wgOut->setArticleRelated( false );
881 $wgOut->enableClientCache( false );
882 $wgOut->addWikiText( $description );
886 * Callback for usort() to do link sorts by (namespace, title)
887 * Function copied from Title::compare()
889 * @param $a object page to compare with
890 * @param $b object page to compare with
891 * @return Integer: result of string comparison, or namespace comparison
893 protected function compare( $a, $b ) {
894 if ( $a->page_namespace
== $b->page_namespace
) {
895 return strcmp( $a->page_title
, $b->page_title
);
897 return $a->page_namespace
- $b->page_namespace
;
903 * Builds the image revision log shown on image pages
907 class ImageHistoryList
{
922 protected $imagePage;
929 protected $repo, $showThumb;
930 protected $preventClickjacking = false;
933 * @param ImagePage $imagePage
935 public function __construct( $imagePage ) {
936 global $wgUser, $wgShowArchiveThumbnails;
937 $this->skin
= $wgUser->getSkin();
938 $this->current
= $imagePage->getFile();
939 $this->img
= $imagePage->getDisplayedFile();
940 $this->title
= $imagePage->getTitle();
941 $this->imagePage
= $imagePage;
942 $this->showThumb
= $wgShowArchiveThumbnails && $this->img
->canRender();
945 public function getImagePage() {
946 return $this->imagePage
;
949 public function getSkin() {
953 public function getFile() {
957 public function beginImageHistoryList( $navLinks = '' ) {
958 global $wgOut, $wgUser;
959 return Xml
::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) ) . "\n"
960 . "<div id=\"mw-imagepage-section-filehistory\">\n"
961 . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
963 . Xml
::openElement( 'table', array( 'class' => 'wikitable filehistory' ) ) . "\n"
965 . ( $this->current
->isLocal() && ( $wgUser->isAllowedAny( 'delete', 'deletedhistory' ) ) ?
'<td></td>' : '' )
966 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
967 . ( $this->showThumb ?
'<th>' . wfMsgHtml( 'filehist-thumb' ) . '</th>' : '' )
968 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
969 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
970 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
974 public function endImageHistoryList( $navLinks = '' ) {
975 return "</table>\n$navLinks\n</div>\n";
983 public function imageHistoryLine( $iscur, $file ) {
984 global $wgUser, $wgLang;
986 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
987 $img = $iscur ?
$file->getName() : $file->getArchiveName();
988 $user = $file->getUser( 'id' );
989 $usertext = $file->getUser( 'text' );
990 $description = $file->getDescription();
992 $local = $this->current
->isLocal();
993 $row = $selected = '';
996 if ( $local && ( $wgUser->isAllowedAny( 'delete', 'deletedhistory' ) ) ) {
998 # Link to remove from history
999 if ( $wgUser->isAllowed( 'delete' ) ) {
1000 $q = array( 'action' => 'delete' );
1002 $q['oldimage'] = $img;
1004 $row .= $this->skin
->link(
1006 wfMsgHtml( $iscur ?
'filehist-deleteall' : 'filehist-deleteone' ),
1007 array(), $q, array( 'known' )
1010 # Link to hide content. Don't show useless link to people who cannot hide revisions.
1011 $canHide = $wgUser->isAllowed( 'deleterevision' );
1012 if ( $canHide ||
( $wgUser->isAllowed( 'deletedhistory' ) && $file->getVisibility() ) ) {
1013 if ( $wgUser->isAllowed( 'delete' ) ) {
1016 // If file is top revision or locked from this user, don't link
1017 if ( $iscur ||
!$file->userCan( File
::DELETED_RESTRICTED
) ) {
1018 $del = $this->skin
->revDeleteLinkDisabled( $canHide );
1020 list( $ts, $name ) = explode( '!', $img, 2 );
1022 'type' => 'oldimage',
1023 'target' => $this->title
->getPrefixedText(),
1026 $del = $this->skin
->revDeleteLink( $query,
1027 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1034 // Reversion link/current indicator
1037 $row .= wfMsgHtml( 'filehist-current' );
1038 } elseif ( $local && $wgUser->isLoggedIn() && $this->title
->userCan( 'edit' ) ) {
1039 if ( $file->isDeleted( File
::DELETED_FILE
) ) {
1040 $row .= wfMsgHtml( 'filehist-revert' );
1042 $row .= $this->skin
->link(
1044 wfMsgHtml( 'filehist-revert' ),
1047 'action' => 'revert',
1049 'wpEditToken' => $wgUser->editToken( $img )
1051 array( 'known', 'noclasses' )
1057 // Date/time and image link
1058 if ( $file->getTimestamp() === $this->img
->getTimestamp() ) {
1059 $selected = "class='filehistory-selected'";
1061 $row .= "<td $selected style='white-space: nowrap;'>";
1062 if ( !$file->userCan( File
::DELETED_FILE
) ) {
1063 # Don't link to unviewable files
1064 $row .= '<span class="history-deleted">' . $wgLang->timeAndDate( $timestamp, true ) . '</span>';
1065 } elseif ( $file->isDeleted( File
::DELETED_FILE
) ) {
1067 $this->preventClickjacking();
1068 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
1069 # Make a link to review the image
1070 $url = $this->skin
->link(
1072 $wgLang->timeAndDate( $timestamp, true ),
1075 'target' => $this->title
->getPrefixedText(),
1077 'token' => $wgUser->editToken( $img )
1079 array( 'known', 'noclasses' )
1082 $url = $wgLang->timeAndDate( $timestamp, true );
1084 $row .= '<span class="history-deleted">' . $url . '</span>';
1086 $url = $iscur ?
$this->current
->getUrl() : $this->current
->getArchiveUrl( $img );
1087 $row .= Xml
::element( 'a', array( 'href' => $url ), $wgLang->timeAndDate( $timestamp, true ) );
1092 if ( $this->showThumb
) {
1093 $row .= '<td>' . $this->getThumbForLine( $file ) . '</td>';
1096 // Image dimensions + size
1098 $row .= htmlspecialchars( $file->getDimensionsString() );
1099 $row .= ' <span style="white-space: nowrap;">(' . $this->skin
->formatSize( $file->getSize() ) . ')</span>';
1104 // Hide deleted usernames
1105 if ( $file->isDeleted( File
::DELETED_USER
) ) {
1106 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1109 $row .= $this->skin
->userLink( $user, $usertext ) . ' <span style="white-space: nowrap;">' .
1110 $this->skin
->userToolLinks( $user, $usertext ) . '</span>';
1112 $row .= htmlspecialchars( $usertext );
1115 $row .= '</td><td>';
1117 // Don't show deleted descriptions
1118 if ( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1119 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span>';
1121 $row .= $this->skin
->commentBlock( $description, $this->title
);
1126 wfRunHooks( 'ImagePageFileHistoryLine', array( $this, $file, &$row, &$rowClass ) );
1127 $classAttr = $rowClass ?
" class='$rowClass'" : '';
1129 return "<tr{$classAttr}>{$row}</tr>\n";
1136 protected function getThumbForLine( $file ) {
1139 if ( $file->allowInlineDisplay() && $file->userCan( File
::DELETED_FILE
) && !$file->isDeleted( File
::DELETED_FILE
) ) {
1144 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
1146 $thumbnail = $file->transform( $params );
1148 'alt' => wfMsg( 'filehist-thumbtext',
1149 $wgLang->timeAndDate( $timestamp, true ),
1150 $wgLang->date( $timestamp, true ),
1151 $wgLang->time( $timestamp, true ) ),
1152 'file-link' => true,
1155 if ( !$thumbnail ) {
1156 return wfMsgHtml( 'filehist-nothumb' );
1159 return $thumbnail->toHtml( $options );
1161 return wfMsgHtml( 'filehist-nothumb' );
1165 protected function preventClickjacking( $enable = true ) {
1166 $this->preventClickjacking
= $enable;
1169 public function getPreventClickjacking() {
1170 return $this->preventClickjacking
;
1174 class ImageHistoryPseudoPager
extends ReverseChronologicalPager
{
1175 protected $preventClickjacking = false;
1188 * @param ImagePage $imagePage
1190 function __construct( $imagePage ) {
1191 parent
::__construct();
1192 $this->mImagePage
= $imagePage;
1193 $this->mTitle
= clone ( $imagePage->getTitle() );
1194 $this->mTitle
->setFragment( '#filehistory' );
1196 $this->mHist
= array();
1197 $this->mRange
= array( 0, 0 ); // display range
1200 function getTitle() {
1201 return $this->mTitle
;
1204 function getQueryInfo() {
1208 function getIndexField() {
1212 function formatRow( $row ) {
1216 function getBody() {
1219 if ( count( $this->mHist
) ) {
1220 $list = new ImageHistoryList( $this->mImagePage
);
1221 # Generate prev/next links
1222 $navLink = $this->getNavigationBar();
1223 $s = $list->beginImageHistoryList( $navLink );
1224 // Skip rows there just for paging links
1225 for ( $i = $this->mRange
[0]; $i <= $this->mRange
[1]; $i++
) {
1226 $file = $this->mHist
[$i];
1227 $s .= $list->imageHistoryLine( !$file->isOld(), $file );
1229 $s .= $list->endImageHistoryList( $navLink );
1231 if ( $list->getPreventClickjacking() ) {
1232 $this->preventClickjacking();
1238 function doQuery() {
1239 if ( $this->mQueryDone
) {
1242 $this->mImg
= $this->mImagePage
->getFile(); // ensure loading
1243 if ( !$this->mImg
->exists() ) {
1246 $queryLimit = $this->mLimit +
1; // limit plus extra row
1247 if ( $this->mIsBackwards
) {
1248 // Fetch the file history
1249 $this->mHist
= $this->mImg
->getHistory( $queryLimit, null, $this->mOffset
, false );
1250 // The current rev may not meet the offset/limit
1251 $numRows = count( $this->mHist
);
1252 if ( $numRows <= $this->mLimit
&& $this->mImg
->getTimestamp() > $this->mOffset
) {
1253 $this->mHist
= array_merge( array( $this->mImg
), $this->mHist
);
1256 // The current rev may not meet the offset
1257 if ( !$this->mOffset ||
$this->mImg
->getTimestamp() < $this->mOffset
) {
1258 $this->mHist
[] = $this->mImg
;
1260 // Old image versions (fetch extra row for nav links)
1261 $oiLimit = count( $this->mHist
) ?
$this->mLimit
: $this->mLimit +
1;
1262 // Fetch the file history
1263 $this->mHist
= array_merge( $this->mHist
,
1264 $this->mImg
->getHistory( $oiLimit, $this->mOffset
, null, false ) );
1266 $numRows = count( $this->mHist
); // Total number of query results
1268 # Index value of top item in the list
1269 $firstIndex = $this->mIsBackwards ?
1270 $this->mHist
[$numRows - 1]->getTimestamp() : $this->mHist
[0]->getTimestamp();
1271 # Discard the extra result row if there is one
1272 if ( $numRows > $this->mLimit
&& $numRows > 1 ) {
1273 if ( $this->mIsBackwards
) {
1274 # Index value of item past the index
1275 $this->mPastTheEndIndex
= $this->mHist
[0]->getTimestamp();
1276 # Index value of bottom item in the list
1277 $lastIndex = $this->mHist
[1]->getTimestamp();
1279 $this->mRange
= array( 1, $numRows - 1 );
1281 # Index value of item past the index
1282 $this->mPastTheEndIndex
= $this->mHist
[$numRows - 1]->getTimestamp();
1283 # Index value of bottom item in the list
1284 $lastIndex = $this->mHist
[$numRows - 2]->getTimestamp();
1286 $this->mRange
= array( 0, $numRows - 2 );
1289 # Setting indexes to an empty string means that they will be
1290 # omitted if they would otherwise appear in URLs. It just so
1291 # happens that this is the right thing to do in the standard
1292 # UI, in all the relevant cases.
1293 $this->mPastTheEndIndex
= '';
1294 # Index value of bottom item in the list
1295 $lastIndex = $this->mIsBackwards ?
1296 $this->mHist
[0]->getTimestamp() : $this->mHist
[$numRows - 1]->getTimestamp();
1298 $this->mRange
= array( 0, $numRows - 1 );
1303 $this->mPastTheEndIndex
= '';
1305 if ( $this->mIsBackwards
) {
1306 $this->mIsFirst
= ( $numRows < $queryLimit );
1307 $this->mIsLast
= ( $this->mOffset
== '' );
1308 $this->mLastShown
= $firstIndex;
1309 $this->mFirstShown
= $lastIndex;
1311 $this->mIsFirst
= ( $this->mOffset
== '' );
1312 $this->mIsLast
= ( $numRows < $queryLimit );
1313 $this->mLastShown
= $lastIndex;
1314 $this->mFirstShown
= $firstIndex;
1316 $this->mQueryDone
= true;
1319 protected function preventClickjacking( $enable = true ) {
1320 $this->preventClickjacking
= $enable;
1323 public function getPreventClickjacking() {
1324 return $this->preventClickjacking
;