3 * Class for viewing MediaWiki file description pages
7 class ImagePage
extends Article
{
19 var $mExtraDescription = false;
23 * @return WikiFilePage
25 protected function newPage( Title
$title ) {
26 // Overload mPage with a file-specific page
27 return new WikiFilePage( $title );
31 * Constructor from a page id
32 * @param $id Int article ID to load
34 public static function newFromID( $id ) {
35 $t = Title
::newFromID( $id );
36 # @todo FIXME: Doesn't inherit right
37 return $t == null ?
null : new self( $t );
38 # return $t == null ? null : new static( $t ); // PHP 5.3
45 public function setFile( $file ) {
46 $this->mPage
->setFile( $file );
47 $this->displayImg
= $file;
48 $this->fileLoaded
= true;
51 protected function loadFile() {
52 if ( $this->fileLoaded
) {
55 $this->fileLoaded
= true;
57 $this->displayImg
= $img = false;
58 wfRunHooks( 'ImagePageFindFile', array( $this, &$img, &$this->displayImg
) );
59 if ( !$img ) { // not set by hook?
60 $img = wfFindFile( $this->getTitle() );
62 $img = wfLocalFile( $this->getTitle() );
65 $this->mPage
->setFile( $img );
66 if ( !$this->displayImg
) { // not set by hook?
67 $this->displayImg
= $img;
69 $this->repo
= $img->getRepo();
73 * Handler for action=render
74 * Include body text only; none of the image extras
76 public function render() {
78 $wgOut->setArticleBodyOnly( true );
82 public function view() {
83 global $wgOut, $wgShowEXIF, $wgRequest, $wgUser;
85 $diff = $wgRequest->getVal( 'diff' );
86 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
88 if ( $this->getTitle()->getNamespace() != NS_FILE ||
( isset( $diff ) && $diffOnly ) ) {
89 return parent
::view();
94 if ( $this->getTitle()->getNamespace() == NS_FILE
&& $this->mPage
->getFile()->getRedirected() ) {
95 if ( $this->getTitle()->getDBkey() == $this->mPage
->getFile()->getName() ||
isset( $diff ) ) {
96 // mTitle is the same as the redirect target so ask Article
97 // to perform the redirect for us.
98 $wgRequest->setVal( 'diffonly', 'true' );
99 return parent
::view();
101 // mTitle is not the same as the redirect target so it is
102 // probably the redirect page itself. Fake the redirect symbol
103 $wgOut->setPageTitle( $this->getTitle()->getPrefixedText() );
104 $wgOut->addHTML( $this->viewRedirect( Title
::makeTitle( NS_FILE
, $this->mPage
->getFile()->getName() ),
105 /* $appendSubtitle */ true, /* $forceKnown */ true ) );
106 $this->mPage
->viewUpdates();
111 $this->showRedirectedFromHeader();
113 if ( $wgShowEXIF && $this->displayImg
->exists() ) {
114 // @todo FIXME: Bad interface, see note on MediaHandler::formatMetadata().
115 $formattedMetadata = $this->displayImg
->formatMetadata();
116 $showmeta = $formattedMetadata !== false;
121 if ( !$diff && $this->displayImg
->exists() ) {
122 $wgOut->addHTML( $this->showTOC( $showmeta ) );
126 $this->openShowImage();
129 # No need to display noarticletext, we use our own message, output in openShowImage()
130 if ( $this->mPage
->getID() ) {
131 # NS_FILE is in the user language, but this section (the actual wikitext)
132 # should be in page content language
133 $pageLang = $this->getTitle()->getPageLanguage();
134 $wgOut->addHTML( Xml
::openElement( 'div', array( 'id' => 'mw-imagepage-content',
135 'lang' => $pageLang->getCode(), 'dir' => $pageLang->getDir(),
136 'class' => 'mw-content-'.$pageLang->getDir() ) ) );
138 $wgOut->addHTML( Xml
::closeElement( 'div' ) );
140 # Just need to set the right headers
141 $wgOut->setArticleFlag( true );
142 $wgOut->setPageTitle( $this->getTitle()->getPrefixedText() );
143 $this->mPage
->viewUpdates();
146 # Show shared description, if needed
147 if ( $this->mExtraDescription
) {
148 $fol = wfMessage( 'shareddescriptionfollows' );
149 if ( !$fol->isDisabled() ) {
150 $wgOut->addWikiText( $fol->plain() );
152 $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription
. "</div>\n" );
155 $this->closeShowImage();
156 $this->imageHistory();
157 // TODO: Cleanup the following
159 $wgOut->addHTML( Xml
::element( 'h2',
160 array( 'id' => 'filelinks' ),
161 wfMsg( 'imagelinks' ) ) . "\n" );
163 # @todo FIXME: For some freaky reason, we can't redirect to foreign images.
164 # Yet we return metadata about the target. Definitely an issue in the FileRepo
167 # Allow extensions to add something after the image links
169 wfRunHooks( 'ImagePageAfterImageLinks', array( $this, &$html ) );
171 $wgOut->addHTML( $html );
175 $wgOut->addHTML( Xml
::element( 'h2', array( 'id' => 'metadata' ), wfMsg( 'metadata' ) ) . "\n" );
176 $wgOut->addWikiText( $this->makeMetadataTable( $formattedMetadata ) );
177 $wgOut->addModules( array( 'mediawiki.action.view.metadata' ) );
180 // Add remote Filepage.css
181 if( !$this->repo
->isLocal() ) {
182 $css = $this->repo
->getDescriptionStylesheetUrl();
184 $wgOut->addStyle( $css );
187 // always show the local local Filepage.css, bug 29277
188 $wgOut->addModuleStyles( 'filepage' );
194 public function getDisplayedFile() {
196 return $this->displayImg
;
202 * @param $metadata Boolean: whether or not to show the metadata link
205 protected function showTOC( $metadata ) {
207 '<li><a href="#file">' . wfMsgHtml( 'file-anchor-link' ) . '</a></li>',
208 '<li><a href="#filehistory">' . wfMsgHtml( 'filehist' ) . '</a></li>',
209 '<li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>',
212 $r[] = '<li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>';
215 wfRunHooks( 'ImagePageShowTOC', array( $this, &$r ) );
217 return '<ul id="filetoc">' . implode( "\n", $r ) . '</ul>';
221 * Make a table with metadata to be shown in the output page.
223 * @todo FIXME: Bad interface, see note on MediaHandler::formatMetadata().
225 * @param $metadata Array: the array containing the EXIF data
226 * @return String The metadata table. This is treated as Wikitext (!)
228 protected function makeMetadataTable( $metadata ) {
229 $r = "<div class=\"mw-imagepage-section-metadata\">";
230 $r .= wfMsgNoTrans( 'metadata-help' );
231 $r .= "<table id=\"mw_metadata\" class=\"mw_metadata\">\n";
232 foreach ( $metadata as $type => $stuff ) {
233 foreach ( $stuff as $v ) {
234 # @todo FIXME: Why is this using escapeId for a class?!
235 $class = Sanitizer
::escapeId( $v['id'] );
236 if ( $type == 'collapsed' ) {
237 $class .= ' collapsable';
239 $r .= "<tr class=\"$class\">\n";
240 $r .= "<th>{$v['name']}</th>\n";
241 $r .= "<td>{$v['value']}</td>\n</tr>";
244 $r .= "</table>\n</div>\n";
249 * Overloading Article's getContent method.
251 * Omit noarticletext if sharedupload; text will be fetched from the
252 * shared upload server if possible.
255 public function getContent() {
257 if ( $this->mPage
->getFile() && !$this->mPage
->getFile()->isLocal() && 0 == $this->getID() ) {
260 return parent
::getContent();
263 protected function openShowImage() {
264 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
265 $wgLang, $wgEnableUploads;
269 $sizeSel = intval( $wgUser->getOption( 'imagesize' ) );
270 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
271 $sizeSel = User
::getDefaultOption( 'imagesize' );
273 // The user offset might still be incorrect, specially if
274 // $wgImageLimits got changed (see bug #8858).
275 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
276 // Default to the first offset in $wgImageLimits
280 $max = $wgImageLimits[$sizeSel];
282 $maxHeight = $max[1];
283 $dirmark = $wgLang->getDirMark();
285 if ( $this->displayImg
->exists() ) {
287 $page = $wgRequest->getIntOrNull( 'page' );
288 if ( is_null( $page ) ) {
292 $params = array( 'page' => $page );
294 $width_orig = $this->displayImg
->getWidth( $page );
295 $width = $width_orig;
296 $height_orig = $this->displayImg
->getHeight( $page );
297 $height = $height_orig;
299 $longDesc = wfMsg( 'parentheses', $this->displayImg
->getLongDesc() );
301 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this, &$wgOut ) );
303 if ( $this->displayImg
->allowInlineDisplay() ) {
306 # "Download high res version" link below the image
307 # $msgsize = wfMsgHtml( 'file-info-size', $width_orig, $height_orig, Linker::formatSize( $this->displayImg->getSize() ), $mime );
308 # We'll show a thumbnail of this image
309 if ( $width > $maxWidth ||
$height > $maxHeight ) {
310 # Calculate the thumbnail size.
311 # First case, the limiting factor is the width, not the height.
312 if ( $width / $height >= $maxWidth / $maxHeight ) {
313 $height = round( $height * $maxWidth / $width );
315 # Note that $height <= $maxHeight now.
317 $newwidth = floor( $width * $maxHeight / $height );
318 $height = round( $height * $newwidth / $width );
320 # Note that $height <= $maxHeight now, but might not be identical
321 # because of rounding.
323 $msgbig = wfMsgHtml( 'show-big-image' );
324 $otherSizes = array();
325 foreach ( $wgImageLimits as $size ) {
326 if ( $size[0] < $width_orig && $size[1] < $height_orig &&
327 $size[0] != $width && $size[1] != $height ) {
328 $otherSizes[] = $this->makeSizeLink( $params, $size[0], $size[1] );
331 $msgsmall = wfMessage( 'show-big-image-preview' )->
332 rawParams( $this->makeSizeLink( $params, $width, $height ) )->
334 Html
::rawElement( 'span', array( 'class' => 'mw-filepage-other-resolutions' ),
335 wfMessage( 'show-big-image-other' )->rawParams( $wgLang->pipeList( $otherSizes ) )->
336 params( count( $otherSizes ) )->parse()
338 } elseif ( $width == 0 && $height == 0 ){
339 # Some sort of audio file that doesn't have dimensions
340 # Don't output a no hi res message for such a file
343 # Image is small enough to show full size on image page
344 $msgsmall = wfMessage( 'file-nohires' )->parse();
347 $params['width'] = $width;
348 $params['height'] = $height;
349 $thumbnail = $this->displayImg
->transform( $params );
352 $anchorclose = Html
::rawElement( 'div', array( 'class' => 'mw-filepage-resolutioninfo' ), $msgsmall );
354 $isMulti = $this->displayImg
->isMultipage() && $this->displayImg
->pageCount() > 1;
356 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
361 'alt' => $this->displayImg
->getTitle()->getPrefixedText(),
364 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
365 $thumbnail->toHtml( $options ) .
366 $anchorclose . "</div>\n" );
370 $count = $this->displayImg
->pageCount();
373 $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
374 $link = Linker
::link(
378 array( 'page' => $page - 1 ),
379 array( 'known', 'noclasses' )
381 $thumb1 = Linker
::makeThumbLinkObj( $this->getTitle(), $this->displayImg
, $link, $label, 'none',
382 array( 'page' => $page - 1 ) );
387 if ( $page < $count ) {
388 $label = wfMsg( 'imgmultipagenext' );
389 $link = Linker
::link(
393 array( 'page' => $page +
1 ),
394 array( 'known', 'noclasses' )
396 $thumb2 = Linker
::makeThumbLinkObj( $this->getTitle(), $this->displayImg
, $link, $label, 'none',
397 array( 'page' => $page +
1 ) );
405 'name' => 'pageselector',
406 'action' => $wgScript,
407 'onchange' => 'document.pageselector.submit();',
410 for ( $i = 1; $i <= $count; $i++
) {
411 $options[] = Xml
::option( $wgLang->formatNum( $i ), $i, $i == $page );
413 $select = Xml
::tags( 'select',
414 array( 'id' => 'pageselector', 'name' => 'page' ),
415 implode( "\n", $options ) );
418 '</td><td><div class="multipageimagenavbox">' .
419 Xml
::openElement( 'form', $formParams ) .
420 Html
::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
421 wfMsgExt( 'imgmultigoto', array( 'parseinline', 'replaceafter' ), $select ) .
422 Xml
::submitButton( wfMsg( 'imgmultigo' ) ) .
423 Xml
::closeElement( 'form' ) .
424 "<hr />$thumb1\n$thumb2<br style=\"clear: both\" /></div></td></tr></table>"
428 # if direct link is allowed but it's not a renderable image, show an icon.
429 if ( $this->displayImg
->isSafeFile() ) {
430 $icon = $this->displayImg
->iconThumb();
432 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
433 $icon->toHtml( array( 'file-link' => true ) ) .
441 $filename = wfEscapeWikiText( $this->displayImg
->getName() );
442 $linktext = $filename;
443 if ( isset( $msgbig ) ) {
444 $linktext = wfEscapeWikiText( $msgbig );
446 $medialink = "[[Media:$filename|$linktext]]";
448 if ( !$this->displayImg
->isSafeFile() ) {
449 $warning = wfMsgNoTrans( 'mediawarning' );
450 $wgOut->addWikiText( <<<EOT
451 <div class="fullMedia"><span class="dangerousLink">{$medialink}</span>$dirmark <span class="fileInfo">$longDesc</span></div>
452 <div class="mediaWarning">$warning</div>
456 $wgOut->addWikiText( <<<EOT
457 <div class="fullMedia">{$medialink}{$dirmark} <span class="fileInfo">$longDesc</span>
464 if ( !$this->displayImg
->isLocal() ) {
465 $this->printSharedImageText();
468 # Image does not exist
469 if ( $wgEnableUploads && $wgUser->isAllowed( 'upload' ) ) {
470 // Only show an upload link if the user can upload
471 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
473 'filepage-nofile-link',
474 $uploadTitle->getFullURL( array( 'wpDestFile' => $this->mPage
->getFile()->getName() ) )
477 $nofile = 'filepage-nofile';
479 // Note, if there is an image description page, but
480 // no image, then this setRobotPolicy is overriden
481 // by Article::View().
482 $wgOut->setRobotPolicy( 'noindex,nofollow' );
483 $wgOut->wrapWikiMsg( "<div id='mw-imagepage-nofile' class='plainlinks'>\n$1\n</div>", $nofile );
484 if ( !$this->getID() ) {
485 // If there is no image, no shared image, and no description page,
486 // output a 404, to be consistent with articles.
487 $wgRequest->response()->header( 'HTTP/1.1 404 Not Found' );
490 $wgOut->setFileVersion( $this->displayImg
);
494 * Creates an thumbnail of specified size and returns an HTML link to it
495 * @param $params array Scaler parameters
500 private function makeSizeLink( $params, $width, $height ) {
501 $params['width'] = $width;
502 $params['height'] = $height;
503 $thumbnail = $this->displayImg
->transform( $params );
504 if ( $thumbnail && !$thumbnail->isError() ) {
505 return Html
::rawElement( 'a', array(
506 'href' => $thumbnail->getUrl(),
507 'class' => 'mw-thumbnail-link'
508 ), wfMessage( 'show-big-image-size' )->numParams(
509 $thumbnail->getWidth(), $thumbnail->getHeight()
517 * Show a notice that the file is from a shared repository
519 protected function printSharedImageText() {
524 $descUrl = $this->mPage
->getFile()->getDescriptionUrl();
525 $descText = $this->mPage
->getFile()->getDescriptionText();
527 /* Add canonical to head if there is no local page for this shared file */
528 if( $descUrl && $this->mPage
->getID() == 0 ) {
529 $wgOut->addLink( array( 'rel' => 'canonical', 'href' => $descUrl ) );
532 $wrap = "<div class=\"sharedUploadNotice\">\n$1\n</div>\n";
533 $repo = $this->mPage
->getFile()->getRepo()->getDisplayName();
535 if ( $descUrl && $descText && wfMsgNoTrans( 'sharedupload-desc-here' ) !== '-' ) {
536 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-here', $repo, $descUrl ) );
537 } elseif ( $descUrl && wfMsgNoTrans( 'sharedupload-desc-there' ) !== '-' ) {
538 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-there', $repo, $descUrl ) );
540 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload', $repo ), ''/*BACKCOMPAT*/ );
544 $this->mExtraDescription
= $descText;
548 public function getUploadUrl() {
550 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
551 return $uploadTitle->getFullURL( array(
552 'wpDestFile' => $this->mPage
->getFile()->getName(),
558 * Print out the various links at the bottom of the image page, e.g. reupload,
559 * external editing (and instructions link) etc.
561 protected function uploadLinksBox() {
562 global $wgUser, $wgOut, $wgEnableUploads, $wgUseExternalEditor;
564 if ( !$wgEnableUploads ) {
569 if ( !$this->mPage
->getFile()->isLocal() ) {
573 $wgOut->addHTML( "<br /><ul>\n" );
575 # "Upload a new version of this file" link
576 if ( UploadBase
::userCanReUpload( $wgUser, $this->mPage
->getFile()->name
) ) {
577 $ulink = Linker
::makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
578 $wgOut->addHTML( "<li id=\"mw-imagepage-reupload-link\"><div class=\"plainlinks\">{$ulink}</div></li>\n" );
581 # External editing link
582 if ( $wgUseExternalEditor ) {
583 $elink = Linker
::link(
585 wfMsgHtml( 'edit-externally' ),
589 'externaledit' => 'true',
592 array( 'known', 'noclasses' )
595 '<li id="mw-imagepage-edit-external">' . $elink . ' <small>' .
596 wfMsgExt( 'edit-externally-help', array( 'parseinline' ) ) .
601 $wgOut->addHTML( "</ul>\n" );
604 protected function closeShowImage() { } # For overloading
607 * If the page we've just displayed is in the "Image" namespace,
608 * we follow it with an upload history of the image and its usage.
610 protected function imageHistory() {
614 $pager = new ImageHistoryPseudoPager( $this );
615 $wgOut->addHTML( $pager->getBody() );
616 $wgOut->preventClickjacking( $pager->getPreventClickjacking() );
618 $this->mPage
->getFile()->resetHistory(); // free db resources
620 # Exist check because we don't want to show this on pages where an image
621 # doesn't exist along with the noimage message, that would suck. -ævar
622 if ( $this->mPage
->getFile()->exists() ) {
623 $this->uploadLinksBox();
630 * @return ResultWrapper
632 protected function queryImageLinks( $target, $limit ) {
633 $dbr = wfGetDB( DB_SLAVE
);
636 array( 'imagelinks', 'page' ),
637 array( 'page_namespace', 'page_title', 'page_is_redirect', 'il_to' ),
638 array( 'il_to' => $target, 'il_from = page_id' ),
640 array( 'LIMIT' => $limit +
1, 'ORDER BY' => 'il_from', )
644 protected function imageLinks() {
645 global $wgOut, $wgLang;
649 $res = $this->queryImageLinks( $this->getTitle()->getDbKey(), $limit +
1);
651 $redirects = array();
652 foreach ( $res as $row ) {
653 if ( $row->page_is_redirect
) {
654 $redirects[$row->page_title
] = array();
658 $count = count( $rows );
660 $hasMore = $count > $limit;
661 if ( !$hasMore && count( $redirects ) ) {
662 $res = $this->queryImageLinks( array_keys( $redirects ),
663 $limit - count( $rows ) +
1 );
664 foreach ( $res as $row ) {
665 $redirects[$row->il_to
][] = $row;
668 $hasMore = ( $res->numRows() +
count( $rows ) ) > $limit;
673 Html
::rawElement( 'div',
674 array( 'id' => 'mw-imagepage-nolinkstoimage' ), "\n$1\n" ),
680 $wgOut->addHTML( "<div id='mw-imagepage-section-linkstoimage'>\n" );
682 $wgOut->addWikiMsg( 'linkstoimage', $count );
684 // More links than the limit. Add a link to [[Special:Whatlinkshere]]
685 $wgOut->addWikiMsg( 'linkstoimage-more',
686 $wgLang->formatNum( $limit ),
687 $this->getTitle()->getPrefixedDBkey()
692 Html
::openElement( 'ul',
693 array( 'class' => 'mw-imagepage-linkstoimage' ) ) . "\n"
697 // Sort the list by namespace:title
698 usort( $rows, array( $this, 'compare' ) );
700 // Create links for every element
702 foreach( $rows as $element ) {
704 if ( $currentCount > $limit ) {
708 $link = Linker
::linkKnown( Title
::makeTitle( $element->page_namespace
, $element->page_title
) );
709 if ( !isset( $redirects[$element->page_title
] ) ) {
712 $ul = "<ul class='mw-imagepage-redirectstofile'>\n";
713 foreach ( $redirects[$element->page_title
] as $row ) {
715 if ( $currentCount > $limit ) {
719 $link2 = Linker
::linkKnown( Title
::makeTitle( $row->page_namespace
, $row->page_title
) );
720 $ul .= Html
::rawElement(
722 array( 'id' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace
),
727 $liContents = wfMessage( 'linkstoimage-redirect' )->rawParams(
728 $link, $ul )->parse();
730 $wgOut->addHTML( Html
::rawElement(
732 array( 'id' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace
),
738 $wgOut->addHTML( Html
::closeElement( 'ul' ) . "\n" );
741 // Add a links to [[Special:Whatlinkshere]]
742 if ( $count > $limit ) {
743 $wgOut->addWikiMsg( 'morelinkstoimage', $this->getTitle()->getPrefixedDBkey() );
745 $wgOut->addHTML( Html
::closeElement( 'div' ) . "\n" );
748 protected function imageDupes() {
749 global $wgOut, $wgLang;
753 $dupes = $this->mPage
->getDuplicates();
754 if ( count( $dupes ) == 0 ) {
758 $wgOut->addHTML( "<div id='mw-imagepage-section-duplicates'>\n" );
759 $wgOut->addWikiMsg( 'duplicatesoffile',
760 $wgLang->formatNum( count( $dupes ) ), $this->getTitle()->getDBkey()
762 $wgOut->addHTML( "<ul class='mw-imagepage-duplicates'>\n" );
767 foreach ( $dupes as $file ) {
769 if ( $file->isLocal() ) {
770 $link = Linker
::link(
775 array( 'known', 'noclasses' )
778 $link = Linker
::makeExternalLink( $file->getDescriptionUrl(),
779 $file->getTitle()->getPrefixedText() );
780 $fromSrc = wfMsg( 'shared-repo-from', $file->getRepo()->getDisplayName() );
782 $wgOut->addHTML( "<li>{$link} {$fromSrc}</li>\n" );
784 $wgOut->addHTML( "</ul></div>\n" );
788 * Delete the file, or an earlier version of it
790 public function delete() {
791 $file = $this->mPage
->getFile();
792 if ( !$file->exists() ||
!$file->isLocal() ||
$file->getRedirected() ) {
793 // Standard article deletion
798 $deleter = new FileDeleteForm( $file );
803 * Display an error with a wikitext description
805 * @param $description String
807 function showError( $description ) {
809 $wgOut->setPageTitle( wfMessage( 'internalerror' ) );
810 $wgOut->setRobotPolicy( 'noindex,nofollow' );
811 $wgOut->setArticleRelated( false );
812 $wgOut->enableClientCache( false );
813 $wgOut->addWikiText( $description );
817 * Callback for usort() to do link sorts by (namespace, title)
818 * Function copied from Title::compare()
820 * @param $a object page to compare with
821 * @param $b object page to compare with
822 * @return Integer: result of string comparison, or namespace comparison
824 protected function compare( $a, $b ) {
825 if ( $a->page_namespace
== $b->page_namespace
) {
826 return strcmp( $a->page_title
, $b->page_title
);
828 return $a->page_namespace
- $b->page_namespace
;
834 * Builds the image revision log shown on image pages
838 class ImageHistoryList
{
853 protected $imagePage;
860 protected $repo, $showThumb;
861 protected $preventClickjacking = false;
864 * @param ImagePage $imagePage
866 public function __construct( $imagePage ) {
867 global $wgShowArchiveThumbnails;
868 $this->current
= $imagePage->getFile();
869 $this->img
= $imagePage->getDisplayedFile();
870 $this->title
= $imagePage->getTitle();
871 $this->imagePage
= $imagePage;
872 $this->showThumb
= $wgShowArchiveThumbnails && $this->img
->canRender();
878 public function getImagePage() {
879 return $this->imagePage
;
885 public function getFile() {
890 * @param $navLinks string
893 public function beginImageHistoryList( $navLinks = '' ) {
894 global $wgOut, $wgUser;
895 return Xml
::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) ) . "\n"
896 . "<div id=\"mw-imagepage-section-filehistory\">\n"
897 . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
899 . Xml
::openElement( 'table', array( 'class' => 'wikitable filehistory' ) ) . "\n"
901 . ( $this->current
->isLocal() && ( $wgUser->isAllowedAny( 'delete', 'deletedhistory' ) ) ?
'<td></td>' : '' )
902 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
903 . ( $this->showThumb ?
'<th>' . wfMsgHtml( 'filehist-thumb' ) . '</th>' : '' )
904 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
905 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
906 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
911 * @param $navLinks string
914 public function endImageHistoryList( $navLinks = '' ) {
915 return "</table>\n$navLinks\n</div>\n";
923 public function imageHistoryLine( $iscur, $file ) {
924 global $wgUser, $wgLang, $wgContLang;
926 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
927 $img = $iscur ?
$file->getName() : $file->getArchiveName();
928 $user = $file->getUser( 'id' );
929 $usertext = $file->getUser( 'text' );
930 $description = $file->getDescription();
932 $local = $this->current
->isLocal();
933 $row = $selected = '';
936 if ( $local && ( $wgUser->isAllowedAny( 'delete', 'deletedhistory' ) ) ) {
938 # Link to remove from history
939 if ( $wgUser->isAllowed( 'delete' ) ) {
940 $q = array( 'action' => 'delete' );
942 $q['oldimage'] = $img;
944 $row .= Linker
::link(
946 wfMsgHtml( $iscur ?
'filehist-deleteall' : 'filehist-deleteone' ),
947 array(), $q, array( 'known' )
950 # Link to hide content. Don't show useless link to people who cannot hide revisions.
951 $canHide = $wgUser->isAllowed( 'deleterevision' );
952 if ( $canHide ||
( $wgUser->isAllowed( 'deletedhistory' ) && $file->getVisibility() ) ) {
953 if ( $wgUser->isAllowed( 'delete' ) ) {
956 // If file is top revision or locked from this user, don't link
957 if ( $iscur ||
!$file->userCan( File
::DELETED_RESTRICTED
) ) {
958 $del = Linker
::revDeleteLinkDisabled( $canHide );
960 list( $ts, $name ) = explode( '!', $img, 2 );
962 'type' => 'oldimage',
963 'target' => $this->title
->getPrefixedText(),
966 $del = Linker
::revDeleteLink( $query,
967 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
974 // Reversion link/current indicator
977 $row .= wfMsgHtml( 'filehist-current' );
978 } elseif ( $local && $wgUser->isLoggedIn() && $this->title
->userCan( 'edit' ) ) {
979 if ( $file->isDeleted( File
::DELETED_FILE
) ) {
980 $row .= wfMsgHtml( 'filehist-revert' );
982 $row .= Linker
::link(
984 wfMsgHtml( 'filehist-revert' ),
987 'action' => 'revert',
989 'wpEditToken' => $wgUser->getEditToken( $img )
991 array( 'known', 'noclasses' )
997 // Date/time and image link
998 if ( $file->getTimestamp() === $this->img
->getTimestamp() ) {
999 $selected = "class='filehistory-selected'";
1001 $row .= "<td $selected style='white-space: nowrap;'>";
1002 if ( !$file->userCan( File
::DELETED_FILE
) ) {
1003 # Don't link to unviewable files
1004 $row .= '<span class="history-deleted">' . $wgLang->timeAndDate( $timestamp, true ) . '</span>';
1005 } elseif ( $file->isDeleted( File
::DELETED_FILE
) ) {
1007 $this->preventClickjacking();
1008 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
1009 # Make a link to review the image
1010 $url = Linker
::link(
1012 $wgLang->timeAndDate( $timestamp, true ),
1015 'target' => $this->title
->getPrefixedText(),
1017 'token' => $wgUser->getEditToken( $img )
1019 array( 'known', 'noclasses' )
1022 $url = $wgLang->timeAndDate( $timestamp, true );
1024 $row .= '<span class="history-deleted">' . $url . '</span>';
1026 $url = $iscur ?
$this->current
->getUrl() : $this->current
->getArchiveUrl( $img );
1027 $row .= Xml
::element( 'a', array( 'href' => $url ), $wgLang->timeAndDate( $timestamp, true ) );
1032 if ( $this->showThumb
) {
1033 $row .= '<td>' . $this->getThumbForLine( $file ) . '</td>';
1036 // Image dimensions + size
1038 $row .= htmlspecialchars( $file->getDimensionsString() );
1039 $row .= ' <span style="white-space: nowrap;">(' . Linker
::formatSize( $file->getSize() ) . ')</span>';
1044 // Hide deleted usernames
1045 if ( $file->isDeleted( File
::DELETED_USER
) ) {
1046 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1049 $row .= Linker
::userLink( $user, $usertext ) . ' <span style="white-space: nowrap;">' .
1050 Linker
::userToolLinks( $user, $usertext ) . '</span>';
1052 $row .= htmlspecialchars( $usertext );
1057 // Don't show deleted descriptions
1058 if ( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1059 $row .= '<td><span class="history-deleted">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span></td>';
1061 $row .= '<td dir="' . $wgContLang->getDir() . '">' . Linker
::formatComment( $description, $this->title
) . '</td>';
1065 wfRunHooks( 'ImagePageFileHistoryLine', array( $this, $file, &$row, &$rowClass ) );
1066 $classAttr = $rowClass ?
" class='$rowClass'" : '';
1068 return "<tr{$classAttr}>{$row}</tr>\n";
1075 protected function getThumbForLine( $file ) {
1078 if ( $file->allowInlineDisplay() && $file->userCan( File
::DELETED_FILE
) && !$file->isDeleted( File
::DELETED_FILE
) ) {
1083 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
1085 $thumbnail = $file->transform( $params );
1087 'alt' => wfMsg( 'filehist-thumbtext',
1088 $wgLang->timeAndDate( $timestamp, true ),
1089 $wgLang->date( $timestamp, true ),
1090 $wgLang->time( $timestamp, true ) ),
1091 'file-link' => true,
1094 if ( !$thumbnail ) {
1095 return wfMsgHtml( 'filehist-nothumb' );
1098 return $thumbnail->toHtml( $options );
1100 return wfMsgHtml( 'filehist-nothumb' );
1105 * @param $enable bool
1107 protected function preventClickjacking( $enable = true ) {
1108 $this->preventClickjacking
= $enable;
1114 public function getPreventClickjacking() {
1115 return $this->preventClickjacking
;
1119 class ImageHistoryPseudoPager
extends ReverseChronologicalPager
{
1120 protected $preventClickjacking = false;
1133 * @param ImagePage $imagePage
1135 function __construct( $imagePage ) {
1136 parent
::__construct();
1137 $this->mImagePage
= $imagePage;
1138 $this->mTitle
= clone ( $imagePage->getTitle() );
1139 $this->mTitle
->setFragment( '#filehistory' );
1141 $this->mHist
= array();
1142 $this->mRange
= array( 0, 0 ); // display range
1148 function getTitle() {
1149 return $this->mTitle
;
1152 function getQueryInfo() {
1159 function getIndexField() {
1166 function formatRow( $row ) {
1173 function getBody() {
1176 if ( count( $this->mHist
) ) {
1177 $list = new ImageHistoryList( $this->mImagePage
);
1178 # Generate prev/next links
1179 $navLink = $this->getNavigationBar();
1180 $s = $list->beginImageHistoryList( $navLink );
1181 // Skip rows there just for paging links
1182 for ( $i = $this->mRange
[0]; $i <= $this->mRange
[1]; $i++
) {
1183 $file = $this->mHist
[$i];
1184 $s .= $list->imageHistoryLine( !$file->isOld(), $file );
1186 $s .= $list->endImageHistoryList( $navLink );
1188 if ( $list->getPreventClickjacking() ) {
1189 $this->preventClickjacking();
1195 function doQuery() {
1196 if ( $this->mQueryDone
) {
1199 $this->mImg
= $this->mImagePage
->getFile(); // ensure loading
1200 if ( !$this->mImg
->exists() ) {
1203 $queryLimit = $this->mLimit +
1; // limit plus extra row
1204 if ( $this->mIsBackwards
) {
1205 // Fetch the file history
1206 $this->mHist
= $this->mImg
->getHistory( $queryLimit, null, $this->mOffset
, false );
1207 // The current rev may not meet the offset/limit
1208 $numRows = count( $this->mHist
);
1209 if ( $numRows <= $this->mLimit
&& $this->mImg
->getTimestamp() > $this->mOffset
) {
1210 $this->mHist
= array_merge( array( $this->mImg
), $this->mHist
);
1213 // The current rev may not meet the offset
1214 if ( !$this->mOffset ||
$this->mImg
->getTimestamp() < $this->mOffset
) {
1215 $this->mHist
[] = $this->mImg
;
1217 // Old image versions (fetch extra row for nav links)
1218 $oiLimit = count( $this->mHist
) ?
$this->mLimit
: $this->mLimit +
1;
1219 // Fetch the file history
1220 $this->mHist
= array_merge( $this->mHist
,
1221 $this->mImg
->getHistory( $oiLimit, $this->mOffset
, null, false ) );
1223 $numRows = count( $this->mHist
); // Total number of query results
1225 # Index value of top item in the list
1226 $firstIndex = $this->mIsBackwards ?
1227 $this->mHist
[$numRows - 1]->getTimestamp() : $this->mHist
[0]->getTimestamp();
1228 # Discard the extra result row if there is one
1229 if ( $numRows > $this->mLimit
&& $numRows > 1 ) {
1230 if ( $this->mIsBackwards
) {
1231 # Index value of item past the index
1232 $this->mPastTheEndIndex
= $this->mHist
[0]->getTimestamp();
1233 # Index value of bottom item in the list
1234 $lastIndex = $this->mHist
[1]->getTimestamp();
1236 $this->mRange
= array( 1, $numRows - 1 );
1238 # Index value of item past the index
1239 $this->mPastTheEndIndex
= $this->mHist
[$numRows - 1]->getTimestamp();
1240 # Index value of bottom item in the list
1241 $lastIndex = $this->mHist
[$numRows - 2]->getTimestamp();
1243 $this->mRange
= array( 0, $numRows - 2 );
1246 # Setting indexes to an empty string means that they will be
1247 # omitted if they would otherwise appear in URLs. It just so
1248 # happens that this is the right thing to do in the standard
1249 # UI, in all the relevant cases.
1250 $this->mPastTheEndIndex
= '';
1251 # Index value of bottom item in the list
1252 $lastIndex = $this->mIsBackwards ?
1253 $this->mHist
[0]->getTimestamp() : $this->mHist
[$numRows - 1]->getTimestamp();
1255 $this->mRange
= array( 0, $numRows - 1 );
1260 $this->mPastTheEndIndex
= '';
1262 if ( $this->mIsBackwards
) {
1263 $this->mIsFirst
= ( $numRows < $queryLimit );
1264 $this->mIsLast
= ( $this->mOffset
== '' );
1265 $this->mLastShown
= $firstIndex;
1266 $this->mFirstShown
= $lastIndex;
1268 $this->mIsFirst
= ( $this->mOffset
== '' );
1269 $this->mIsLast
= ( $numRows < $queryLimit );
1270 $this->mLastShown
= $lastIndex;
1271 $this->mFirstShown
= $firstIndex;
1273 $this->mQueryDone
= true;
1277 * @param $enable bool
1279 protected function preventClickjacking( $enable = true ) {
1280 $this->preventClickjacking
= $enable;
1286 public function getPreventClickjacking() {
1287 return $this->preventClickjacking
;