3 * Special handling for file description pages.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 * Class for viewing MediaWiki file description pages
28 class ImagePage
extends Article
{
40 var $mExtraDescription = false;
44 * @return WikiFilePage
46 protected function newPage( Title
$title ) {
47 // Overload mPage with a file-specific page
48 return new WikiFilePage( $title );
52 * Constructor from a page id
53 * @param $id Int article ID to load
54 * @return ImagePage|null
56 public static function newFromID( $id ) {
57 $t = Title
::newFromID( $id );
58 # @todo FIXME: Doesn't inherit right
59 return $t == null ?
null : new self( $t );
60 # return $t == null ? null : new static( $t ); // PHP 5.3
67 public function setFile( $file ) {
68 $this->mPage
->setFile( $file );
69 $this->displayImg
= $file;
70 $this->fileLoaded
= true;
73 protected function loadFile() {
74 if ( $this->fileLoaded
) {
77 $this->fileLoaded
= true;
79 $this->displayImg
= $img = false;
80 wfRunHooks( 'ImagePageFindFile', array( $this, &$img, &$this->displayImg
) );
81 if ( !$img ) { // not set by hook?
82 $img = wfFindFile( $this->getTitle() );
84 $img = wfLocalFile( $this->getTitle() );
87 $this->mPage
->setFile( $img );
88 if ( !$this->displayImg
) { // not set by hook?
89 $this->displayImg
= $img;
91 $this->repo
= $img->getRepo();
95 * Handler for action=render
96 * Include body text only; none of the image extras
98 public function render() {
99 $this->getContext()->getOutput()->setArticleBodyOnly( true );
103 public function view() {
106 $out = $this->getContext()->getOutput();
107 $request = $this->getContext()->getRequest();
108 $diff = $request->getVal( 'diff' );
109 $diffOnly = $request->getBool( 'diffonly', $this->getContext()->getUser()->getOption( 'diffonly' ) );
111 if ( $this->getTitle()->getNamespace() != NS_FILE ||
( isset( $diff ) && $diffOnly ) ) {
118 if ( $this->getTitle()->getNamespace() == NS_FILE
&& $this->mPage
->getFile()->getRedirected() ) {
119 if ( $this->getTitle()->getDBkey() == $this->mPage
->getFile()->getName() ||
isset( $diff ) ) {
120 // mTitle is the same as the redirect target so ask Article
121 // to perform the redirect for us.
122 $request->setVal( 'diffonly', 'true' );
126 // mTitle is not the same as the redirect target so it is
127 // probably the redirect page itself. Fake the redirect symbol
128 $out->setPageTitle( $this->getTitle()->getPrefixedText() );
129 $out->addHTML( $this->viewRedirect( Title
::makeTitle( NS_FILE
, $this->mPage
->getFile()->getName() ),
130 /* $appendSubtitle */ true, /* $forceKnown */ true ) );
131 $this->mPage
->doViewUpdates( $this->getContext()->getUser() );
136 if ( $wgShowEXIF && $this->displayImg
->exists() ) {
137 // @todo FIXME: Bad interface, see note on MediaHandler::formatMetadata().
138 $formattedMetadata = $this->displayImg
->formatMetadata();
139 $showmeta = $formattedMetadata !== false;
144 if ( !$diff && $this->displayImg
->exists() ) {
145 $out->addHTML( $this->showTOC( $showmeta ) );
149 $this->openShowImage();
152 # No need to display noarticletext, we use our own message, output in openShowImage()
153 if ( $this->mPage
->getID() ) {
154 # NS_FILE is in the user language, but this section (the actual wikitext)
155 # should be in page content language
156 $pageLang = $this->getTitle()->getPageLanguage();
157 $out->addHTML( Xml
::openElement( 'div', array( 'id' => 'mw-imagepage-content',
158 'lang' => $pageLang->getHtmlCode(), 'dir' => $pageLang->getDir(),
159 'class' => 'mw-content-'.$pageLang->getDir() ) ) );
161 $out->addHTML( Xml
::closeElement( 'div' ) );
163 # Just need to set the right headers
164 $out->setArticleFlag( true );
165 $out->setPageTitle( $this->getTitle()->getPrefixedText() );
166 $this->mPage
->doViewUpdates( $this->getContext()->getUser() );
169 # Show shared description, if needed
170 if ( $this->mExtraDescription
) {
171 $fol = wfMessage( 'shareddescriptionfollows' );
172 if ( !$fol->isDisabled() ) {
173 $out->addWikiText( $fol->plain() );
175 $out->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription
. "</div>\n" );
178 $this->closeShowImage();
179 $this->imageHistory();
180 // TODO: Cleanup the following
182 $out->addHTML( Xml
::element( 'h2',
183 array( 'id' => 'filelinks' ),
184 wfMsg( 'imagelinks' ) ) . "\n" );
186 # @todo FIXME: For some freaky reason, we can't redirect to foreign images.
187 # Yet we return metadata about the target. Definitely an issue in the FileRepo
190 # Allow extensions to add something after the image links
192 wfRunHooks( 'ImagePageAfterImageLinks', array( $this, &$html ) );
194 $out->addHTML( $html );
198 $out->addHTML( Xml
::element( 'h2', array( 'id' => 'metadata' ), wfMsg( 'metadata' ) ) . "\n" );
199 $out->addWikiText( $this->makeMetadataTable( $formattedMetadata ) );
200 $out->addModules( array( 'mediawiki.action.view.metadata' ) );
203 // Add remote Filepage.css
204 if( !$this->repo
->isLocal() ) {
205 $css = $this->repo
->getDescriptionStylesheetUrl();
207 $out->addStyle( $css );
210 // always show the local local Filepage.css, bug 29277
211 $out->addModuleStyles( 'filepage' );
217 public function getDisplayedFile() {
219 return $this->displayImg
;
225 * @param $metadata Boolean: whether or not to show the metadata link
228 protected function showTOC( $metadata ) {
230 '<li><a href="#file">' . wfMsgHtml( 'file-anchor-link' ) . '</a></li>',
231 '<li><a href="#filehistory">' . wfMsgHtml( 'filehist' ) . '</a></li>',
232 '<li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>',
235 $r[] = '<li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>';
238 wfRunHooks( 'ImagePageShowTOC', array( $this, &$r ) );
240 return '<ul id="filetoc">' . implode( "\n", $r ) . '</ul>';
244 * Make a table with metadata to be shown in the output page.
246 * @todo FIXME: Bad interface, see note on MediaHandler::formatMetadata().
248 * @param $metadata Array: the array containing the EXIF data
249 * @return String The metadata table. This is treated as Wikitext (!)
251 protected function makeMetadataTable( $metadata ) {
252 $r = "<div class=\"mw-imagepage-section-metadata\">";
253 $r .= wfMsgNoTrans( 'metadata-help' );
254 $r .= "<table id=\"mw_metadata\" class=\"mw_metadata\">\n";
255 foreach ( $metadata as $type => $stuff ) {
256 foreach ( $stuff as $v ) {
257 # @todo FIXME: Why is this using escapeId for a class?!
258 $class = Sanitizer
::escapeId( $v['id'] );
259 if ( $type == 'collapsed' ) {
260 $class .= ' collapsable';
262 $r .= "<tr class=\"$class\">\n";
263 $r .= "<th>{$v['name']}</th>\n";
264 $r .= "<td>{$v['value']}</td>\n</tr>";
267 $r .= "</table>\n</div>\n";
272 * Overloading Article's getContent method.
274 * Omit noarticletext if sharedupload; text will be fetched from the
275 * shared upload server if possible.
278 public function getContent() {
280 if ( $this->mPage
->getFile() && !$this->mPage
->getFile()->isLocal() && 0 == $this->getID() ) {
283 return parent
::getContent();
286 protected function openShowImage() {
287 global $wgImageLimits, $wgEnableUploads, $wgSend404Code;
290 $out = $this->getContext()->getOutput();
291 $user = $this->getContext()->getUser();
292 $lang = $this->getContext()->getLanguage();
293 $dirmark = $lang->getDirMarkEntity();
294 $request = $this->getContext()->getRequest();
296 $sizeSel = intval( $user->getOption( 'imagesize' ) );
297 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
298 $sizeSel = User
::getDefaultOption( 'imagesize' );
300 // The user offset might still be incorrect, specially if
301 // $wgImageLimits got changed (see bug #8858).
302 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
303 // Default to the first offset in $wgImageLimits
307 $max = $wgImageLimits[$sizeSel];
309 $maxHeight = $max[1];
311 if ( $this->displayImg
->exists() ) {
313 $page = $request->getIntOrNull( 'page' );
314 if ( is_null( $page ) ) {
318 $params = array( 'page' => $page );
320 $width_orig = $this->displayImg
->getWidth( $page );
321 $width = $width_orig;
322 $height_orig = $this->displayImg
->getHeight( $page );
323 $height = $height_orig;
325 $longDesc = wfMsg( 'parentheses', $this->displayImg
->getLongDesc() );
327 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this, &$out ) );
329 if ( $this->displayImg
->allowInlineDisplay() ) {
332 # "Download high res version" link below the image
333 # $msgsize = wfMsgHtml( 'file-info-size', $width_orig, $height_orig, Linker::formatSize( $this->displayImg->getSize() ), $mime );
334 # We'll show a thumbnail of this image
335 if ( $width > $maxWidth ||
$height > $maxHeight ) {
336 # Calculate the thumbnail size.
337 # First case, the limiting factor is the width, not the height.
338 if ( $width / $height >= $maxWidth / $maxHeight ) {
339 $height = round( $height * $maxWidth / $width );
341 # Note that $height <= $maxHeight now.
343 $newwidth = floor( $width * $maxHeight / $height );
344 $height = round( $height * $newwidth / $width );
346 # Note that $height <= $maxHeight now, but might not be identical
347 # because of rounding.
349 $msgbig = wfMsgHtml( 'show-big-image' );
350 if ( $this->displayImg
->getRepo()->canTransformVia404() ) {
351 $thumbSizes = $wgImageLimits;
353 # Creating thumb links triggers thumbnail generation.
354 # Just generate the thumb for the current users prefs.
355 $thumbOption = $user->getOption( 'thumbsize' );
356 $thumbSizes = array( isset( $wgImageLimits[$thumbOption] )
357 ?
$wgImageLimits[$thumbOption]
358 : $wgImageLimits[User
::getDefaultOption( 'thumbsize' )] );
360 # Generate thumbnails or thumbnail links as needed...
361 $otherSizes = array();
362 foreach ( $thumbSizes as $size ) {
363 if ( $size[0] < $width_orig && $size[1] < $height_orig
364 && $size[0] != $width && $size[1] != $height )
366 $otherSizes[] = $this->makeSizeLink( $params, $size[0], $size[1] );
369 $msgsmall = wfMessage( 'show-big-image-preview' )->
370 rawParams( $this->makeSizeLink( $params, $width, $height ) )->
372 if ( count( $otherSizes ) ) {
374 Html
::rawElement( 'span', array( 'class' => 'mw-filepage-other-resolutions' ),
375 wfMessage( 'show-big-image-other' )->rawParams( $lang->pipeList( $otherSizes ) )->
376 params( count( $otherSizes ) )->parse()
379 } elseif ( $width == 0 && $height == 0 ){
380 # Some sort of audio file that doesn't have dimensions
381 # Don't output a no hi res message for such a file
384 # Image is small enough to show full size on image page
385 $msgsmall = wfMessage( 'file-nohires' )->parse();
388 $params['width'] = $width;
389 $params['height'] = $height;
390 $thumbnail = $this->displayImg
->transform( $params );
393 $anchorclose = Html
::rawElement( 'div', array( 'class' => 'mw-filepage-resolutioninfo' ), $msgsmall );
395 $isMulti = $this->displayImg
->isMultipage() && $this->displayImg
->pageCount() > 1;
397 $out->addHTML( '<table class="multipageimage"><tr><td>' );
402 'alt' => $this->displayImg
->getTitle()->getPrefixedText(),
405 $out->addHTML( '<div class="fullImageLink" id="file">' .
406 $thumbnail->toHtml( $options ) .
407 $anchorclose . "</div>\n" );
411 $count = $this->displayImg
->pageCount();
414 $label = $out->parse( wfMsg( 'imgmultipageprev' ), false );
415 $link = Linker
::linkKnown(
419 array( 'page' => $page - 1 )
421 $thumb1 = Linker
::makeThumbLinkObj( $this->getTitle(), $this->displayImg
, $link, $label, 'none',
422 array( 'page' => $page - 1 ) );
427 if ( $page < $count ) {
428 $label = wfMsg( 'imgmultipagenext' );
429 $link = Linker
::linkKnown(
433 array( 'page' => $page +
1 )
435 $thumb2 = Linker
::makeThumbLinkObj( $this->getTitle(), $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( $lang->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 style=\"clear: both\" /></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 $out->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 // dirmark is needed here to separate the file name, which
490 // most likely ends in Latin characters, from the description,
491 // which may begin with the file type. In RTL environment
492 // this will get messy.
493 // The dirmark, however, must not be immediately adjacent
494 // to the filename, because it can get copied with it.
496 $out->addWikiText( <<<EOT
497 <div class="fullMedia"><span class="dangerousLink">{$medialink}</span> $dirmark<span class="fileInfo">$longDesc</span></div>
498 <div class="mediaWarning">$warning</div>
502 $out->addWikiText( <<<EOT
503 <div class="fullMedia">{$medialink} {$dirmark}<span class="fileInfo">$longDesc</span>
510 if ( !$this->displayImg
->isLocal() ) {
511 $this->printSharedImageText();
514 # Image does not exist
515 if ( !$this->getID() ) {
516 # No article exists either
517 # Show deletion log to be consistent with normal articles
518 LogEventsList
::showLogExtract(
520 array( 'delete', 'move' ),
521 $this->getTitle()->getPrefixedText(),
524 'conds' => array( "log_action != 'revision'" ),
525 'showIfEmpty' => false,
526 'msgKey' => array( 'moveddeleted-notice' )
531 if ( $wgEnableUploads && $user->isAllowed( 'upload' ) ) {
532 // Only show an upload link if the user can upload
533 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
535 'filepage-nofile-link',
536 $uploadTitle->getFullURL( array( 'wpDestFile' => $this->mPage
->getFile()->getName() ) )
539 $nofile = 'filepage-nofile';
541 // Note, if there is an image description page, but
542 // no image, then this setRobotPolicy is overriden
543 // by Article::View().
544 $out->setRobotPolicy( 'noindex,nofollow' );
545 $out->wrapWikiMsg( "<div id='mw-imagepage-nofile' class='plainlinks'>\n$1\n</div>", $nofile );
546 if ( !$this->getID() && $wgSend404Code ) {
547 // If there is no image, no shared image, and no description page,
548 // output a 404, to be consistent with articles.
549 $request->response()->header( 'HTTP/1.1 404 Not Found' );
552 $out->setFileVersion( $this->displayImg
);
556 * Creates an thumbnail of specified size and returns an HTML link to it
557 * @param $params array Scaler parameters
562 private function makeSizeLink( $params, $width, $height ) {
563 $params['width'] = $width;
564 $params['height'] = $height;
565 $thumbnail = $this->displayImg
->transform( $params );
566 if ( $thumbnail && !$thumbnail->isError() ) {
567 return Html
::rawElement( 'a', array(
568 'href' => $thumbnail->getUrl(),
569 'class' => 'mw-thumbnail-link'
570 ), wfMessage( 'show-big-image-size' )->numParams(
571 $thumbnail->getWidth(), $thumbnail->getHeight()
579 * Show a notice that the file is from a shared repository
581 protected function printSharedImageText() {
582 $out = $this->getContext()->getOutput();
585 $descUrl = $this->mPage
->getFile()->getDescriptionUrl();
586 $descText = $this->mPage
->getFile()->getDescriptionText();
588 /* Add canonical to head if there is no local page for this shared file */
589 if( $descUrl && $this->mPage
->getID() == 0 ) {
590 $out->addLink( array( 'rel' => 'canonical', 'href' => $descUrl ) );
593 $wrap = "<div class=\"sharedUploadNotice\">\n$1\n</div>\n";
594 $repo = $this->mPage
->getFile()->getRepo()->getDisplayName();
596 if ( $descUrl && $descText && wfMsgNoTrans( 'sharedupload-desc-here' ) !== '-' ) {
597 $out->wrapWikiMsg( $wrap, array( 'sharedupload-desc-here', $repo, $descUrl ) );
598 } elseif ( $descUrl && wfMsgNoTrans( 'sharedupload-desc-there' ) !== '-' ) {
599 $out->wrapWikiMsg( $wrap, array( 'sharedupload-desc-there', $repo, $descUrl ) );
601 $out->wrapWikiMsg( $wrap, array( 'sharedupload', $repo ), ''/*BACKCOMPAT*/ );
605 $this->mExtraDescription
= $descText;
609 public function getUploadUrl() {
611 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
612 return $uploadTitle->getFullURL( array(
613 'wpDestFile' => $this->mPage
->getFile()->getName(),
619 * Print out the various links at the bottom of the image page, e.g. reupload,
620 * external editing (and instructions link) etc.
622 protected function uploadLinksBox() {
623 global $wgEnableUploads, $wgUseExternalEditor;
625 if ( !$wgEnableUploads ) {
630 if ( !$this->mPage
->getFile()->isLocal() ) {
634 $out = $this->getContext()->getOutput();
635 $out->addHTML( "<br /><ul>\n" );
637 # "Upload a new version of this file" link
638 if ( UploadBase
::userCanReUpload( $this->getContext()->getUser(), $this->mPage
->getFile()->name
) ) {
639 $ulink = Linker
::makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
640 $out->addHTML( "<li id=\"mw-imagepage-reupload-link\"><div class=\"plainlinks\">{$ulink}</div></li>\n" );
643 # External editing link
644 if ( $wgUseExternalEditor ) {
645 $elink = Linker
::linkKnown(
647 wfMsgHtml( 'edit-externally' ),
651 'externaledit' => 'true',
656 '<li id="mw-imagepage-edit-external">' . $elink . ' <small>' .
657 wfMsgExt( 'edit-externally-help', array( 'parseinline' ) ) .
662 $out->addHTML( "</ul>\n" );
665 protected function closeShowImage() { } # For overloading
668 * If the page we've just displayed is in the "Image" namespace,
669 * we follow it with an upload history of the image and its usage.
671 protected function imageHistory() {
673 $out = $this->getContext()->getOutput();
674 $pager = new ImageHistoryPseudoPager( $this );
675 $out->addHTML( $pager->getBody() );
676 $out->preventClickjacking( $pager->getPreventClickjacking() );
678 $this->mPage
->getFile()->resetHistory(); // free db resources
680 # Exist check because we don't want to show this on pages where an image
681 # doesn't exist along with the noimage message, that would suck. -ævar
682 if ( $this->mPage
->getFile()->exists() ) {
683 $this->uploadLinksBox();
690 * @return ResultWrapper
692 protected function queryImageLinks( $target, $limit ) {
693 $dbr = wfGetDB( DB_SLAVE
);
696 array( 'imagelinks', 'page' ),
697 array( 'page_namespace', 'page_title', 'page_is_redirect', 'il_to' ),
698 array( 'il_to' => $target, 'il_from = page_id' ),
700 array( 'LIMIT' => $limit +
1, 'ORDER BY' => 'il_from', )
704 protected function imageLinks() {
707 $out = $this->getContext()->getOutput();
708 $res = $this->queryImageLinks( $this->getTitle()->getDbKey(), $limit +
1);
710 $redirects = array();
711 foreach ( $res as $row ) {
712 if ( $row->page_is_redirect
) {
713 $redirects[$row->page_title
] = array();
717 $count = count( $rows );
719 $hasMore = $count > $limit;
720 if ( !$hasMore && count( $redirects ) ) {
721 $res = $this->queryImageLinks( array_keys( $redirects ),
722 $limit - count( $rows ) +
1 );
723 foreach ( $res as $row ) {
724 $redirects[$row->il_to
][] = $row;
727 $hasMore = ( $res->numRows() +
count( $rows ) ) > $limit;
732 Html
::rawElement( 'div',
733 array( 'id' => 'mw-imagepage-nolinkstoimage' ), "\n$1\n" ),
739 $out->addHTML( "<div id='mw-imagepage-section-linkstoimage'>\n" );
741 $out->addWikiMsg( 'linkstoimage', $count );
743 // More links than the limit. Add a link to [[Special:Whatlinkshere]]
744 $out->addWikiMsg( 'linkstoimage-more',
745 $this->getContext()->getLanguage()->formatNum( $limit ),
746 $this->getTitle()->getPrefixedDBkey()
751 Html
::openElement( 'ul',
752 array( 'class' => 'mw-imagepage-linkstoimage' ) ) . "\n"
756 // Sort the list by namespace:title
757 usort( $rows, array( $this, 'compare' ) );
759 // Create links for every element
761 foreach( $rows as $element ) {
763 if ( $currentCount > $limit ) {
767 $link = Linker
::linkKnown( Title
::makeTitle( $element->page_namespace
, $element->page_title
) );
768 if ( !isset( $redirects[$element->page_title
] ) ) {
771 $ul = "<ul class='mw-imagepage-redirectstofile'>\n";
772 foreach ( $redirects[$element->page_title
] as $row ) {
774 if ( $currentCount > $limit ) {
778 $link2 = Linker
::linkKnown( Title
::makeTitle( $row->page_namespace
, $row->page_title
) );
779 $ul .= Html
::rawElement(
781 array( 'id' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace
),
786 $liContents = wfMessage( 'linkstoimage-redirect' )->rawParams(
787 $link, $ul )->parse();
789 $out->addHTML( Html
::rawElement(
791 array( 'id' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace
),
797 $out->addHTML( Html
::closeElement( 'ul' ) . "\n" );
800 // Add a links to [[Special:Whatlinkshere]]
801 if ( $count > $limit ) {
802 $out->addWikiMsg( 'morelinkstoimage', $this->getTitle()->getPrefixedDBkey() );
804 $out->addHTML( Html
::closeElement( 'div' ) . "\n" );
807 protected function imageDupes() {
809 $out = $this->getContext()->getOutput();
811 $dupes = $this->mPage
->getDuplicates();
812 if ( count( $dupes ) == 0 ) {
816 $out->addHTML( "<div id='mw-imagepage-section-duplicates'>\n" );
817 $out->addWikiMsg( 'duplicatesoffile',
818 $this->getContext()->getLanguage()->formatNum( count( $dupes ) ), $this->getTitle()->getDBkey()
820 $out->addHTML( "<ul class='mw-imagepage-duplicates'>\n" );
825 foreach ( $dupes as $file ) {
827 if ( $file->isLocal() ) {
828 $link = Linker
::linkKnown( $file->getTitle() );
830 $link = Linker
::makeExternalLink( $file->getDescriptionUrl(),
831 $file->getTitle()->getPrefixedText() );
832 $fromSrc = wfMsg( 'shared-repo-from', $file->getRepo()->getDisplayName() );
834 $out->addHTML( "<li>{$link} {$fromSrc}</li>\n" );
836 $out->addHTML( "</ul></div>\n" );
840 * Delete the file, or an earlier version of it
842 public function delete() {
843 $file = $this->mPage
->getFile();
844 if ( !$file->exists() ||
!$file->isLocal() ||
$file->getRedirected() ) {
845 // Standard article deletion
850 $deleter = new FileDeleteForm( $file );
855 * Display an error with a wikitext description
857 * @param $description String
859 function showError( $description ) {
860 $out = $this->getContext()->getOutput();
861 $out->setPageTitle( wfMessage( 'internalerror' ) );
862 $out->setRobotPolicy( 'noindex,nofollow' );
863 $out->setArticleRelated( false );
864 $out->enableClientCache( false );
865 $out->addWikiText( $description );
869 * Callback for usort() to do link sorts by (namespace, title)
870 * Function copied from Title::compare()
872 * @param $a object page to compare with
873 * @param $b object page to compare with
874 * @return Integer: result of string comparison, or namespace comparison
876 protected function compare( $a, $b ) {
877 if ( $a->page_namespace
== $b->page_namespace
) {
878 return strcmp( $a->page_title
, $b->page_title
);
880 return $a->page_namespace
- $b->page_namespace
;
886 * Builds the image revision log shown on image pages
890 class ImageHistoryList
extends ContextSource
{
905 protected $imagePage;
912 protected $repo, $showThumb;
913 protected $preventClickjacking = false;
916 * @param ImagePage $imagePage
918 public function __construct( $imagePage ) {
919 global $wgShowArchiveThumbnails;
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();
925 $this->setContext( $imagePage->getContext() );
931 public function getImagePage() {
932 return $this->imagePage
;
938 public function getFile() {
943 * @param $navLinks string
946 public function beginImageHistoryList( $navLinks = '' ) {
947 return Xml
::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) ) . "\n"
948 . "<div id=\"mw-imagepage-section-filehistory\">\n"
949 . $this->getOutput()->parse( wfMsgNoTrans( 'filehist-help' ) )
951 . Xml
::openElement( 'table', array( 'class' => 'wikitable filehistory' ) ) . "\n"
953 . ( $this->current
->isLocal() && ( $this->getUser()->isAllowedAny( 'delete', 'deletedhistory' ) ) ?
'<td></td>' : '' )
954 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
955 . ( $this->showThumb ?
'<th>' . wfMsgHtml( 'filehist-thumb' ) . '</th>' : '' )
956 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
957 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
958 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
963 * @param $navLinks string
966 public function endImageHistoryList( $navLinks = '' ) {
967 return "</table>\n$navLinks\n</div>\n";
975 public function imageHistoryLine( $iscur, $file ) {
978 $user = $this->getUser();
979 $lang = $this->getLanguage();
980 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
981 $img = $iscur ?
$file->getName() : $file->getArchiveName();
982 $userId = $file->getUser( 'id' );
983 $userText = $file->getUser( 'text' );
984 $description = $file->getDescription( File
::FOR_THIS_USER
, $user );
986 $local = $this->current
->isLocal();
987 $row = $selected = '';
990 if ( $local && ( $user->isAllowedAny( 'delete', 'deletedhistory' ) ) ) {
992 # Link to remove from history
993 if ( $user->isAllowed( 'delete' ) ) {
994 $q = array( 'action' => 'delete' );
996 $q['oldimage'] = $img;
998 $row .= Linker
::linkKnown(
1000 wfMsgHtml( $iscur ?
'filehist-deleteall' : 'filehist-deleteone' ),
1004 # Link to hide content. Don't show useless link to people who cannot hide revisions.
1005 $canHide = $user->isAllowed( 'deleterevision' );
1006 if ( $canHide ||
( $user->isAllowed( 'deletedhistory' ) && $file->getVisibility() ) ) {
1007 if ( $user->isAllowed( 'delete' ) ) {
1010 // If file is top revision or locked from this user, don't link
1011 if ( $iscur ||
!$file->userCan( File
::DELETED_RESTRICTED
) ) {
1012 $del = Linker
::revDeleteLinkDisabled( $canHide );
1014 list( $ts, ) = explode( '!', $img, 2 );
1016 'type' => 'oldimage',
1017 'target' => $this->title
->getPrefixedText(),
1020 $del = Linker
::revDeleteLink( $query,
1021 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1028 // Reversion link/current indicator
1031 $row .= wfMsgHtml( 'filehist-current' );
1032 } elseif ( $local && $this->title
->quickUserCan( 'edit' )
1033 && $this->title
->quickUserCan( 'upload' )
1035 if ( $file->isDeleted( File
::DELETED_FILE
) ) {
1036 $row .= wfMsgHtml( 'filehist-revert' );
1038 $row .= Linker
::linkKnown(
1040 wfMsgHtml( 'filehist-revert' ),
1043 'action' => 'revert',
1045 'wpEditToken' => $user->getEditToken( $img )
1052 // Date/time and image link
1053 if ( $file->getTimestamp() === $this->img
->getTimestamp() ) {
1054 $selected = "class='filehistory-selected'";
1056 $row .= "<td $selected style='white-space: nowrap;'>";
1057 if ( !$file->userCan( File
::DELETED_FILE
) ) {
1058 # Don't link to unviewable files
1059 $row .= '<span class="history-deleted">' . $lang->timeanddate( $timestamp, true ) . '</span>';
1060 } elseif ( $file->isDeleted( File
::DELETED_FILE
) ) {
1062 $this->preventClickjacking();
1063 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
1064 # Make a link to review the image
1065 $url = Linker
::linkKnown(
1067 $lang->timeanddate( $timestamp, true ),
1070 'target' => $this->title
->getPrefixedText(),
1072 'token' => $user->getEditToken( $img )
1076 $url = $lang->timeanddate( $timestamp, true );
1078 $row .= '<span class="history-deleted">' . $url . '</span>';
1080 $url = $iscur ?
$this->current
->getUrl() : $this->current
->getArchiveUrl( $img );
1081 $row .= Xml
::element( 'a', array( 'href' => $url ), $lang->timeanddate( $timestamp, true ) );
1086 if ( $this->showThumb
) {
1087 $row .= '<td>' . $this->getThumbForLine( $file ) . '</td>';
1090 // Image dimensions + size
1092 $row .= htmlspecialchars( $file->getDimensionsString() );
1093 $row .= $this->getContext()->msg( 'word-separator' )->plain();
1094 $row .= '<span style="white-space: nowrap;">';
1095 $row .= $this->getContext()->msg( 'parentheses' )->rawParams( Linker
::formatSize( $file->getSize() ) )->plain();
1101 // Hide deleted usernames
1102 if ( $file->isDeleted( File
::DELETED_USER
) ) {
1103 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1106 $row .= Linker
::userLink( $userId, $userText );
1107 $row .= $this->getContext()->msg( 'word-separator' )->plain();
1108 $row .= '<span style="white-space: nowrap;">';
1109 $row .= Linker
::userToolLinks( $userId, $userText );
1112 $row .= htmlspecialchars( $userText );
1117 // Don't show deleted descriptions
1118 if ( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1119 $row .= '<td><span class="history-deleted">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span></td>';
1121 $row .= '<td dir="' . $wgContLang->getDir() . '">' . Linker
::formatComment( $description, $this->title
) . '</td>';
1125 wfRunHooks( 'ImagePageFileHistoryLine', array( $this, $file, &$row, &$rowClass ) );
1126 $classAttr = $rowClass ?
" class='$rowClass'" : '';
1128 return "<tr{$classAttr}>{$row}</tr>\n";
1135 protected function getThumbForLine( $file ) {
1136 $lang = $this->getLanguage();
1137 if ( $file->allowInlineDisplay() && $file->userCan( File
::DELETED_FILE
) && !$file->isDeleted( File
::DELETED_FILE
) ) {
1142 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
1144 $thumbnail = $file->transform( $params );
1146 'alt' => wfMsg( 'filehist-thumbtext',
1147 $lang->timeanddate( $timestamp, true ),
1148 $lang->date( $timestamp, true ),
1149 $lang->time( $timestamp, true ) ),
1150 'file-link' => true,
1153 if ( !$thumbnail ) {
1154 return wfMsgHtml( 'filehist-nothumb' );
1157 return $thumbnail->toHtml( $options );
1159 return wfMsgHtml( 'filehist-nothumb' );
1164 * @param $enable bool
1166 protected function preventClickjacking( $enable = true ) {
1167 $this->preventClickjacking
= $enable;
1173 public function getPreventClickjacking() {
1174 return $this->preventClickjacking
;
1178 class ImageHistoryPseudoPager
extends ReverseChronologicalPager
{
1179 protected $preventClickjacking = false;
1192 * @param ImagePage $imagePage
1194 function __construct( $imagePage ) {
1195 parent
::__construct();
1196 $this->mImagePage
= $imagePage;
1197 $this->mTitle
= clone ( $imagePage->getTitle() );
1198 $this->mTitle
->setFragment( '#filehistory' );
1200 $this->mHist
= array();
1201 $this->mRange
= array( 0, 0 ); // display range
1207 function getTitle() {
1208 return $this->mTitle
;
1211 function getQueryInfo() {
1218 function getIndexField() {
1223 * @param $row object
1226 function formatRow( $row ) {
1233 function getBody() {
1236 if ( count( $this->mHist
) ) {
1237 $list = new ImageHistoryList( $this->mImagePage
);
1238 # Generate prev/next links
1239 $navLink = $this->getNavigationBar();
1240 $s = $list->beginImageHistoryList( $navLink );
1241 // Skip rows there just for paging links
1242 for ( $i = $this->mRange
[0]; $i <= $this->mRange
[1]; $i++
) {
1243 $file = $this->mHist
[$i];
1244 $s .= $list->imageHistoryLine( !$file->isOld(), $file );
1246 $s .= $list->endImageHistoryList( $navLink );
1248 if ( $list->getPreventClickjacking() ) {
1249 $this->preventClickjacking();
1255 function doQuery() {
1256 if ( $this->mQueryDone
) {
1259 $this->mImg
= $this->mImagePage
->getFile(); // ensure loading
1260 if ( !$this->mImg
->exists() ) {
1263 $queryLimit = $this->mLimit +
1; // limit plus extra row
1264 if ( $this->mIsBackwards
) {
1265 // Fetch the file history
1266 $this->mHist
= $this->mImg
->getHistory( $queryLimit, null, $this->mOffset
, false );
1267 // The current rev may not meet the offset/limit
1268 $numRows = count( $this->mHist
);
1269 if ( $numRows <= $this->mLimit
&& $this->mImg
->getTimestamp() > $this->mOffset
) {
1270 $this->mHist
= array_merge( array( $this->mImg
), $this->mHist
);
1273 // The current rev may not meet the offset
1274 if ( !$this->mOffset ||
$this->mImg
->getTimestamp() < $this->mOffset
) {
1275 $this->mHist
[] = $this->mImg
;
1277 // Old image versions (fetch extra row for nav links)
1278 $oiLimit = count( $this->mHist
) ?
$this->mLimit
: $this->mLimit +
1;
1279 // Fetch the file history
1280 $this->mHist
= array_merge( $this->mHist
,
1281 $this->mImg
->getHistory( $oiLimit, $this->mOffset
, null, false ) );
1283 $numRows = count( $this->mHist
); // Total number of query results
1285 # Index value of top item in the list
1286 $firstIndex = $this->mIsBackwards ?
1287 $this->mHist
[$numRows - 1]->getTimestamp() : $this->mHist
[0]->getTimestamp();
1288 # Discard the extra result row if there is one
1289 if ( $numRows > $this->mLimit
&& $numRows > 1 ) {
1290 if ( $this->mIsBackwards
) {
1291 # Index value of item past the index
1292 $this->mPastTheEndIndex
= $this->mHist
[0]->getTimestamp();
1293 # Index value of bottom item in the list
1294 $lastIndex = $this->mHist
[1]->getTimestamp();
1296 $this->mRange
= array( 1, $numRows - 1 );
1298 # Index value of item past the index
1299 $this->mPastTheEndIndex
= $this->mHist
[$numRows - 1]->getTimestamp();
1300 # Index value of bottom item in the list
1301 $lastIndex = $this->mHist
[$numRows - 2]->getTimestamp();
1303 $this->mRange
= array( 0, $numRows - 2 );
1306 # Setting indexes to an empty string means that they will be
1307 # omitted if they would otherwise appear in URLs. It just so
1308 # happens that this is the right thing to do in the standard
1309 # UI, in all the relevant cases.
1310 $this->mPastTheEndIndex
= '';
1311 # Index value of bottom item in the list
1312 $lastIndex = $this->mIsBackwards ?
1313 $this->mHist
[0]->getTimestamp() : $this->mHist
[$numRows - 1]->getTimestamp();
1315 $this->mRange
= array( 0, $numRows - 1 );
1320 $this->mPastTheEndIndex
= '';
1322 if ( $this->mIsBackwards
) {
1323 $this->mIsFirst
= ( $numRows < $queryLimit );
1324 $this->mIsLast
= ( $this->mOffset
== '' );
1325 $this->mLastShown
= $firstIndex;
1326 $this->mFirstShown
= $lastIndex;
1328 $this->mIsFirst
= ( $this->mOffset
== '' );
1329 $this->mIsLast
= ( $numRows < $queryLimit );
1330 $this->mLastShown
= $lastIndex;
1331 $this->mFirstShown
= $firstIndex;
1333 $this->mQueryDone
= true;
1337 * @param $enable bool
1339 protected function preventClickjacking( $enable = true ) {
1340 $this->preventClickjacking
= $enable;
1346 public function getPreventClickjacking() {
1347 return $this->preventClickjacking
;