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() ) ) );
163 $out->addHTML( Xml
::closeElement( 'div' ) );
165 # Just need to set the right headers
166 $out->setArticleFlag( true );
167 $out->setPageTitle( $this->getTitle()->getPrefixedText() );
168 $this->mPage
->doViewUpdates( $this->getContext()->getUser() );
171 # Show shared description, if needed
172 if ( $this->mExtraDescription
) {
173 $fol = wfMessage( 'shareddescriptionfollows' );
174 if ( !$fol->isDisabled() ) {
175 $out->addWikiText( $fol->plain() );
177 $out->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription
. "</div>\n" );
180 $this->closeShowImage();
181 $this->imageHistory();
182 // TODO: Cleanup the following
184 $out->addHTML( Xml
::element( 'h2',
185 array( 'id' => 'filelinks' ),
186 wfMsg( 'imagelinks' ) ) . "\n" );
188 # @todo FIXME: For some freaky reason, we can't redirect to foreign images.
189 # Yet we return metadata about the target. Definitely an issue in the FileRepo
192 # Allow extensions to add something after the image links
194 wfRunHooks( 'ImagePageAfterImageLinks', array( $this, &$html ) );
196 $out->addHTML( $html );
200 $out->addHTML( Xml
::element( 'h2', array( 'id' => 'metadata' ), wfMsg( 'metadata' ) ) . "\n" );
201 $out->addWikiText( $this->makeMetadataTable( $formattedMetadata ) );
202 $out->addModules( array( 'mediawiki.action.view.metadata' ) );
205 // Add remote Filepage.css
206 if( !$this->repo
->isLocal() ) {
207 $css = $this->repo
->getDescriptionStylesheetUrl();
209 $out->addStyle( $css );
212 // always show the local local Filepage.css, bug 29277
213 $out->addModuleStyles( 'filepage' );
219 public function getDisplayedFile() {
221 return $this->displayImg
;
227 * @param $metadata Boolean: whether or not to show the metadata link
230 protected function showTOC( $metadata ) {
232 '<li><a href="#file">' . wfMsgHtml( 'file-anchor-link' ) . '</a></li>',
233 '<li><a href="#filehistory">' . wfMsgHtml( 'filehist' ) . '</a></li>',
234 '<li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>',
237 $r[] = '<li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>';
240 wfRunHooks( 'ImagePageShowTOC', array( $this, &$r ) );
242 return '<ul id="filetoc">' . implode( "\n", $r ) . '</ul>';
246 * Make a table with metadata to be shown in the output page.
248 * @todo FIXME: Bad interface, see note on MediaHandler::formatMetadata().
250 * @param $metadata Array: the array containing the EXIF data
251 * @return String The metadata table. This is treated as Wikitext (!)
253 protected function makeMetadataTable( $metadata ) {
254 $r = "<div class=\"mw-imagepage-section-metadata\">";
255 $r .= wfMsgNoTrans( 'metadata-help' );
256 $r .= "<table id=\"mw_metadata\" class=\"mw_metadata\">\n";
257 foreach ( $metadata as $type => $stuff ) {
258 foreach ( $stuff as $v ) {
259 # @todo FIXME: Why is this using escapeId for a class?!
260 $class = Sanitizer
::escapeId( $v['id'] );
261 if ( $type == 'collapsed' ) {
262 $class .= ' collapsable';
264 $r .= "<tr class=\"$class\">\n";
265 $r .= "<th>{$v['name']}</th>\n";
266 $r .= "<td>{$v['value']}</td>\n</tr>";
269 $r .= "</table>\n</div>\n";
274 * Overloading Article's getContentObject method.
276 * Omit noarticletext if sharedupload; text will be fetched from the
277 * shared upload server if possible.
280 public function getContentObject() {
282 if ( $this->mPage
->getFile() && !$this->mPage
->getFile()->isLocal() && 0 == $this->getID() ) {
285 return parent
::getContentObject();
288 protected function openShowImage() {
289 global $wgImageLimits, $wgEnableUploads, $wgSend404Code;
292 $out = $this->getContext()->getOutput();
293 $user = $this->getContext()->getUser();
294 $lang = $this->getContext()->getLanguage();
295 $dirmark = $lang->getDirMarkEntity();
296 $request = $this->getContext()->getRequest();
298 $sizeSel = intval( $user->getOption( 'imagesize' ) );
299 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
300 $sizeSel = User
::getDefaultOption( 'imagesize' );
302 // The user offset might still be incorrect, specially if
303 // $wgImageLimits got changed (see bug #8858).
304 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
305 // Default to the first offset in $wgImageLimits
309 $max = $wgImageLimits[$sizeSel];
311 $maxHeight = $max[1];
313 if ( $this->displayImg
->exists() ) {
315 $page = $request->getIntOrNull( 'page' );
316 if ( is_null( $page ) ) {
320 $params = array( 'page' => $page );
322 $width_orig = $this->displayImg
->getWidth( $page );
323 $width = $width_orig;
324 $height_orig = $this->displayImg
->getHeight( $page );
325 $height = $height_orig;
327 $longDesc = wfMsg( 'parentheses', $this->displayImg
->getLongDesc() );
329 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this, &$out ) );
331 if ( $this->displayImg
->allowInlineDisplay() ) {
334 # "Download high res version" link below the image
335 # $msgsize = wfMsgHtml( 'file-info-size', $width_orig, $height_orig, Linker::formatSize( $this->displayImg->getSize() ), $mime );
336 # We'll show a thumbnail of this image
337 if ( $width > $maxWidth ||
$height > $maxHeight ) {
338 # Calculate the thumbnail size.
339 # First case, the limiting factor is the width, not the height.
340 if ( $width / $height >= $maxWidth / $maxHeight ) {
341 $height = round( $height * $maxWidth / $width );
343 # Note that $height <= $maxHeight now.
345 $newwidth = floor( $width * $maxHeight / $height );
346 $height = round( $height * $newwidth / $width );
348 # Note that $height <= $maxHeight now, but might not be identical
349 # because of rounding.
351 $msgbig = wfMsgHtml( 'show-big-image' );
352 if ( $this->displayImg
->getRepo()->canTransformVia404() ) {
353 $thumbSizes = $wgImageLimits;
355 # Creating thumb links triggers thumbnail generation.
356 # Just generate the thumb for the current users prefs.
357 $thumbOption = $user->getOption( 'thumbsize' );
358 $thumbSizes = array( isset( $wgImageLimits[$thumbOption] )
359 ?
$wgImageLimits[$thumbOption]
360 : $wgImageLimits[User
::getDefaultOption( 'thumbsize' )] );
362 # Generate thumbnails or thumbnail links as needed...
363 $otherSizes = array();
364 foreach ( $thumbSizes as $size ) {
365 if ( $size[0] < $width_orig && $size[1] < $height_orig
366 && $size[0] != $width && $size[1] != $height )
368 $otherSizes[] = $this->makeSizeLink( $params, $size[0], $size[1] );
371 $msgsmall = wfMessage( 'show-big-image-preview' )->
372 rawParams( $this->makeSizeLink( $params, $width, $height ) )->
374 if ( count( $otherSizes ) ) {
376 Html
::rawElement( 'span', array( 'class' => 'mw-filepage-other-resolutions' ),
377 wfMessage( 'show-big-image-other' )->rawParams( $lang->pipeList( $otherSizes ) )->
378 params( count( $otherSizes ) )->parse()
381 } elseif ( $width == 0 && $height == 0 ){
382 # Some sort of audio file that doesn't have dimensions
383 # Don't output a no hi res message for such a file
386 # Image is small enough to show full size on image page
387 $msgsmall = wfMessage( 'file-nohires' )->parse();
390 $params['width'] = $width;
391 $params['height'] = $height;
392 $thumbnail = $this->displayImg
->transform( $params );
395 $anchorclose = Html
::rawElement( 'div', array( 'class' => 'mw-filepage-resolutioninfo' ), $msgsmall );
397 $isMulti = $this->displayImg
->isMultipage() && $this->displayImg
->pageCount() > 1;
399 $out->addHTML( '<table class="multipageimage"><tr><td>' );
404 'alt' => $this->displayImg
->getTitle()->getPrefixedText(),
407 $out->addHTML( '<div class="fullImageLink" id="file">' .
408 $thumbnail->toHtml( $options ) .
409 $anchorclose . "</div>\n" );
413 $count = $this->displayImg
->pageCount();
416 $label = $out->parse( wfMsg( 'imgmultipageprev' ), false );
417 $link = Linker
::linkKnown(
421 array( 'page' => $page - 1 )
423 $thumb1 = Linker
::makeThumbLinkObj( $this->getTitle(), $this->displayImg
, $link, $label, 'none',
424 array( 'page' => $page - 1 ) );
429 if ( $page < $count ) {
430 $label = wfMsg( 'imgmultipagenext' );
431 $link = Linker
::linkKnown(
435 array( 'page' => $page +
1 )
437 $thumb2 = Linker
::makeThumbLinkObj( $this->getTitle(), $this->displayImg
, $link, $label, 'none',
438 array( 'page' => $page +
1 ) );
446 'name' => 'pageselector',
447 'action' => $wgScript,
448 'onchange' => 'document.pageselector.submit();',
451 for ( $i = 1; $i <= $count; $i++
) {
452 $options[] = Xml
::option( $lang->formatNum( $i ), $i, $i == $page );
454 $select = Xml
::tags( 'select',
455 array( 'id' => 'pageselector', 'name' => 'page' ),
456 implode( "\n", $options ) );
459 '</td><td><div class="multipageimagenavbox">' .
460 Xml
::openElement( 'form', $formParams ) .
461 Html
::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
462 wfMsgExt( 'imgmultigoto', array( 'parseinline', 'replaceafter' ), $select ) .
463 Xml
::submitButton( wfMsg( 'imgmultigo' ) ) .
464 Xml
::closeElement( 'form' ) .
465 "<hr />$thumb1\n$thumb2<br style=\"clear: both\" /></div></td></tr></table>"
469 # if direct link is allowed but it's not a renderable image, show an icon.
470 if ( $this->displayImg
->isSafeFile() ) {
471 $icon = $this->displayImg
->iconThumb();
473 $out->addHTML( '<div class="fullImageLink" id="file">' .
474 $icon->toHtml( array( 'file-link' => true ) ) .
482 $filename = wfEscapeWikiText( $this->displayImg
->getName() );
483 $linktext = $filename;
484 if ( isset( $msgbig ) ) {
485 $linktext = wfEscapeWikiText( $msgbig );
487 $medialink = "[[Media:$filename|$linktext]]";
489 if ( !$this->displayImg
->isSafeFile() ) {
490 $warning = wfMsgNoTrans( 'mediawarning' );
491 // dirmark is needed here to separate the file name, which
492 // most likely ends in Latin characters, from the description,
493 // which may begin with the file type. In RTL environment
494 // this will get messy.
495 // The dirmark, however, must not be immediately adjacent
496 // to the filename, because it can get copied with it.
498 $out->addWikiText( <<<EOT
499 <div class="fullMedia"><span class="dangerousLink">{$medialink}</span> $dirmark<span class="fileInfo">$longDesc</span></div>
500 <div class="mediaWarning">$warning</div>
504 $out->addWikiText( <<<EOT
505 <div class="fullMedia">{$medialink} {$dirmark}<span class="fileInfo">$longDesc</span>
512 if ( !$this->displayImg
->isLocal() ) {
513 $this->printSharedImageText();
516 # Image does not exist
517 if ( !$this->getID() ) {
518 # No article exists either
519 # Show deletion log to be consistent with normal articles
520 LogEventsList
::showLogExtract(
522 array( 'delete', 'move' ),
523 $this->getTitle()->getPrefixedText(),
526 'conds' => array( "log_action != 'revision'" ),
527 'showIfEmpty' => false,
528 'msgKey' => array( 'moveddeleted-notice' )
533 if ( $wgEnableUploads && $user->isAllowed( 'upload' ) ) {
534 // Only show an upload link if the user can upload
535 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
537 'filepage-nofile-link',
538 $uploadTitle->getFullURL( array( 'wpDestFile' => $this->mPage
->getFile()->getName() ) )
541 $nofile = 'filepage-nofile';
543 // Note, if there is an image description page, but
544 // no image, then this setRobotPolicy is overriden
545 // by Article::View().
546 $out->setRobotPolicy( 'noindex,nofollow' );
547 $out->wrapWikiMsg( "<div id='mw-imagepage-nofile' class='plainlinks'>\n$1\n</div>", $nofile );
548 if ( !$this->getID() && $wgSend404Code ) {
549 // If there is no image, no shared image, and no description page,
550 // output a 404, to be consistent with articles.
551 $request->response()->header( 'HTTP/1.1 404 Not Found' );
554 $out->setFileVersion( $this->displayImg
);
558 * Creates an thumbnail of specified size and returns an HTML link to it
559 * @param $params array Scaler parameters
564 private function makeSizeLink( $params, $width, $height ) {
565 $params['width'] = $width;
566 $params['height'] = $height;
567 $thumbnail = $this->displayImg
->transform( $params );
568 if ( $thumbnail && !$thumbnail->isError() ) {
569 return Html
::rawElement( 'a', array(
570 'href' => $thumbnail->getUrl(),
571 'class' => 'mw-thumbnail-link'
572 ), wfMessage( 'show-big-image-size' )->numParams(
573 $thumbnail->getWidth(), $thumbnail->getHeight()
581 * Show a notice that the file is from a shared repository
583 protected function printSharedImageText() {
584 $out = $this->getContext()->getOutput();
587 $descUrl = $this->mPage
->getFile()->getDescriptionUrl();
588 $descText = $this->mPage
->getFile()->getDescriptionText();
590 /* Add canonical to head if there is no local page for this shared file */
591 if( $descUrl && $this->mPage
->getID() == 0 ) {
592 $out->addLink( array( 'rel' => 'canonical', 'href' => $descUrl ) );
595 $wrap = "<div class=\"sharedUploadNotice\">\n$1\n</div>\n";
596 $repo = $this->mPage
->getFile()->getRepo()->getDisplayName();
598 if ( $descUrl && $descText && wfMsgNoTrans( 'sharedupload-desc-here' ) !== '-' ) {
599 $out->wrapWikiMsg( $wrap, array( 'sharedupload-desc-here', $repo, $descUrl ) );
600 } elseif ( $descUrl && wfMsgNoTrans( 'sharedupload-desc-there' ) !== '-' ) {
601 $out->wrapWikiMsg( $wrap, array( 'sharedupload-desc-there', $repo, $descUrl ) );
603 $out->wrapWikiMsg( $wrap, array( 'sharedupload', $repo ), ''/*BACKCOMPAT*/ );
607 $this->mExtraDescription
= $descText;
611 public function getUploadUrl() {
613 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
614 return $uploadTitle->getFullURL( array(
615 'wpDestFile' => $this->mPage
->getFile()->getName(),
621 * Print out the various links at the bottom of the image page, e.g. reupload,
622 * external editing (and instructions link) etc.
624 protected function uploadLinksBox() {
625 global $wgEnableUploads, $wgUseExternalEditor;
627 if ( !$wgEnableUploads ) {
632 if ( !$this->mPage
->getFile()->isLocal() ) {
636 $out = $this->getContext()->getOutput();
637 $out->addHTML( "<br /><ul>\n" );
639 # "Upload a new version of this file" link
640 if ( UploadBase
::userCanReUpload( $this->getContext()->getUser(), $this->mPage
->getFile()->name
) ) {
641 $ulink = Linker
::makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
642 $out->addHTML( "<li id=\"mw-imagepage-reupload-link\"><div class=\"plainlinks\">{$ulink}</div></li>\n" );
645 # External editing link
646 if ( $wgUseExternalEditor ) {
647 $elink = Linker
::linkKnown(
649 wfMsgHtml( 'edit-externally' ),
653 'externaledit' => 'true',
658 '<li id="mw-imagepage-edit-external">' . $elink . ' <small>' .
659 wfMsgExt( 'edit-externally-help', array( 'parseinline' ) ) .
664 $out->addHTML( "</ul>\n" );
667 protected function closeShowImage() { } # For overloading
670 * If the page we've just displayed is in the "Image" namespace,
671 * we follow it with an upload history of the image and its usage.
673 protected function imageHistory() {
675 $out = $this->getContext()->getOutput();
676 $pager = new ImageHistoryPseudoPager( $this );
677 $out->addHTML( $pager->getBody() );
678 $out->preventClickjacking( $pager->getPreventClickjacking() );
680 $this->mPage
->getFile()->resetHistory(); // free db resources
682 # Exist check because we don't want to show this on pages where an image
683 # doesn't exist along with the noimage message, that would suck. -ævar
684 if ( $this->mPage
->getFile()->exists() ) {
685 $this->uploadLinksBox();
692 * @return ResultWrapper
694 protected function queryImageLinks( $target, $limit ) {
695 $dbr = wfGetDB( DB_SLAVE
);
698 array( 'imagelinks', 'page' ),
699 array( 'page_namespace', 'page_title', 'page_is_redirect', 'il_to' ),
700 array( 'il_to' => $target, 'il_from = page_id' ),
702 array( 'LIMIT' => $limit +
1, 'ORDER BY' => 'il_from', )
706 protected function imageLinks() {
709 $out = $this->getContext()->getOutput();
710 $res = $this->queryImageLinks( $this->getTitle()->getDbKey(), $limit +
1);
712 $redirects = array();
713 foreach ( $res as $row ) {
714 if ( $row->page_is_redirect
) {
715 $redirects[$row->page_title
] = array();
719 $count = count( $rows );
721 $hasMore = $count > $limit;
722 if ( !$hasMore && count( $redirects ) ) {
723 $res = $this->queryImageLinks( array_keys( $redirects ),
724 $limit - count( $rows ) +
1 );
725 foreach ( $res as $row ) {
726 $redirects[$row->il_to
][] = $row;
729 $hasMore = ( $res->numRows() +
count( $rows ) ) > $limit;
734 Html
::rawElement( 'div',
735 array( 'id' => 'mw-imagepage-nolinkstoimage' ), "\n$1\n" ),
741 $out->addHTML( "<div id='mw-imagepage-section-linkstoimage'>\n" );
743 $out->addWikiMsg( 'linkstoimage', $count );
745 // More links than the limit. Add a link to [[Special:Whatlinkshere]]
746 $out->addWikiMsg( 'linkstoimage-more',
747 $this->getContext()->getLanguage()->formatNum( $limit ),
748 $this->getTitle()->getPrefixedDBkey()
753 Html
::openElement( 'ul',
754 array( 'class' => 'mw-imagepage-linkstoimage' ) ) . "\n"
758 // Sort the list by namespace:title
759 usort( $rows, array( $this, 'compare' ) );
761 // Create links for every element
763 foreach( $rows as $element ) {
765 if ( $currentCount > $limit ) {
769 $link = Linker
::linkKnown( Title
::makeTitle( $element->page_namespace
, $element->page_title
) );
770 if ( !isset( $redirects[$element->page_title
] ) ) {
773 $ul = "<ul class='mw-imagepage-redirectstofile'>\n";
774 foreach ( $redirects[$element->page_title
] as $row ) {
776 if ( $currentCount > $limit ) {
780 $link2 = Linker
::linkKnown( Title
::makeTitle( $row->page_namespace
, $row->page_title
) );
781 $ul .= Html
::rawElement(
783 array( 'class' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace
),
788 $liContents = wfMessage( 'linkstoimage-redirect' )->rawParams(
789 $link, $ul )->parse();
791 $out->addHTML( Html
::rawElement(
793 array( 'class' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace
),
799 $out->addHTML( Html
::closeElement( 'ul' ) . "\n" );
802 // Add a links to [[Special:Whatlinkshere]]
803 if ( $count > $limit ) {
804 $out->addWikiMsg( 'morelinkstoimage', $this->getTitle()->getPrefixedDBkey() );
806 $out->addHTML( Html
::closeElement( 'div' ) . "\n" );
809 protected function imageDupes() {
811 $out = $this->getContext()->getOutput();
813 $dupes = $this->mPage
->getDuplicates();
814 if ( count( $dupes ) == 0 ) {
818 $out->addHTML( "<div id='mw-imagepage-section-duplicates'>\n" );
819 $out->addWikiMsg( 'duplicatesoffile',
820 $this->getContext()->getLanguage()->formatNum( count( $dupes ) ), $this->getTitle()->getDBkey()
822 $out->addHTML( "<ul class='mw-imagepage-duplicates'>\n" );
827 foreach ( $dupes as $file ) {
829 if ( $file->isLocal() ) {
830 $link = Linker
::linkKnown( $file->getTitle() );
832 $link = Linker
::makeExternalLink( $file->getDescriptionUrl(),
833 $file->getTitle()->getPrefixedText() );
834 $fromSrc = wfMsg( 'shared-repo-from', $file->getRepo()->getDisplayName() );
836 $out->addHTML( "<li>{$link} {$fromSrc}</li>\n" );
838 $out->addHTML( "</ul></div>\n" );
842 * Delete the file, or an earlier version of it
844 public function delete() {
845 $file = $this->mPage
->getFile();
846 if ( !$file->exists() ||
!$file->isLocal() ||
$file->getRedirected() ) {
847 // Standard article deletion
852 $deleter = new FileDeleteForm( $file );
857 * Display an error with a wikitext description
859 * @param $description String
861 function showError( $description ) {
862 $out = $this->getContext()->getOutput();
863 $out->setPageTitle( wfMessage( 'internalerror' ) );
864 $out->setRobotPolicy( 'noindex,nofollow' );
865 $out->setArticleRelated( false );
866 $out->enableClientCache( false );
867 $out->addWikiText( $description );
871 * Callback for usort() to do link sorts by (namespace, title)
872 * Function copied from Title::compare()
874 * @param $a object page to compare with
875 * @param $b object page to compare with
876 * @return Integer: result of string comparison, or namespace comparison
878 protected function compare( $a, $b ) {
879 if ( $a->page_namespace
== $b->page_namespace
) {
880 return strcmp( $a->page_title
, $b->page_title
);
882 return $a->page_namespace
- $b->page_namespace
;
888 * Builds the image revision log shown on image pages
892 class ImageHistoryList
extends ContextSource
{
907 protected $imagePage;
914 protected $repo, $showThumb;
915 protected $preventClickjacking = false;
918 * @param ImagePage $imagePage
920 public function __construct( $imagePage ) {
921 global $wgShowArchiveThumbnails;
922 $this->current
= $imagePage->getFile();
923 $this->img
= $imagePage->getDisplayedFile();
924 $this->title
= $imagePage->getTitle();
925 $this->imagePage
= $imagePage;
926 $this->showThumb
= $wgShowArchiveThumbnails && $this->img
->canRender();
927 $this->setContext( $imagePage->getContext() );
933 public function getImagePage() {
934 return $this->imagePage
;
940 public function getFile() {
945 * @param $navLinks string
948 public function beginImageHistoryList( $navLinks = '' ) {
949 return Xml
::element( 'h2', array( 'id' => 'filehistory' ), $this->msg( 'filehist' )->text() ) . "\n"
950 . "<div id=\"mw-imagepage-section-filehistory\">\n"
951 . $this->msg( 'filehist-help' )->parseAsBlock()
953 . Xml
::openElement( 'table', array( 'class' => 'wikitable filehistory' ) ) . "\n"
955 . ( $this->current
->isLocal() && ( $this->getUser()->isAllowedAny( 'delete', 'deletedhistory' ) ) ?
'<td></td>' : '' )
956 . '<th>' . $this->msg( 'filehist-datetime' )->escaped() . '</th>'
957 . ( $this->showThumb ?
'<th>' . $this->msg( 'filehist-thumb' )->escaped() . '</th>' : '' )
958 . '<th>' . $this->msg( 'filehist-dimensions' )->escaped() . '</th>'
959 . '<th>' . $this->msg( 'filehist-user' )->escaped() . '</th>'
960 . '<th>' . $this->msg( 'filehist-comment' )->escaped() . '</th>'
965 * @param $navLinks string
968 public function endImageHistoryList( $navLinks = '' ) {
969 return "</table>\n$navLinks\n</div>\n";
977 public function imageHistoryLine( $iscur, $file ) {
980 $user = $this->getUser();
981 $lang = $this->getLanguage();
982 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
983 $img = $iscur ?
$file->getName() : $file->getArchiveName();
984 $userId = $file->getUser( 'id' );
985 $userText = $file->getUser( 'text' );
986 $description = $file->getDescription( File
::FOR_THIS_USER
, $user );
988 $local = $this->current
->isLocal();
989 $row = $selected = '';
992 if ( $local && ( $user->isAllowedAny( 'delete', 'deletedhistory' ) ) ) {
994 # Link to remove from history
995 if ( $user->isAllowed( 'delete' ) ) {
996 $q = array( 'action' => 'delete' );
998 $q['oldimage'] = $img;
1000 $row .= Linker
::linkKnown(
1002 $this->msg( $iscur ?
'filehist-deleteall' : 'filehist-deleteone' )->escaped(),
1006 # Link to hide content. Don't show useless link to people who cannot hide revisions.
1007 $canHide = $user->isAllowed( 'deleterevision' );
1008 if ( $canHide ||
( $user->isAllowed( 'deletedhistory' ) && $file->getVisibility() ) ) {
1009 if ( $user->isAllowed( 'delete' ) ) {
1012 // If file is top revision or locked from this user, don't link
1013 if ( $iscur ||
!$file->userCan( File
::DELETED_RESTRICTED
, $user ) ) {
1014 $del = Linker
::revDeleteLinkDisabled( $canHide );
1016 list( $ts, ) = explode( '!', $img, 2 );
1018 'type' => 'oldimage',
1019 'target' => $this->title
->getPrefixedText(),
1022 $del = Linker
::revDeleteLink( $query,
1023 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1030 // Reversion link/current indicator
1033 $row .= $this->msg( 'filehist-current' )->escaped();
1034 } elseif ( $local && $this->title
->quickUserCan( 'edit', $user )
1035 && $this->title
->quickUserCan( 'upload', $user )
1037 if ( $file->isDeleted( File
::DELETED_FILE
) ) {
1038 $row .= $this->msg( 'filehist-revert' )->escaped();
1040 $row .= Linker
::linkKnown(
1042 $this->msg( 'filehist-revert' )->escaped(),
1045 'action' => 'revert',
1047 'wpEditToken' => $user->getEditToken( $img )
1054 // Date/time and image link
1055 if ( $file->getTimestamp() === $this->img
->getTimestamp() ) {
1056 $selected = "class='filehistory-selected'";
1058 $row .= "<td $selected style='white-space: nowrap;'>";
1059 if ( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
1060 # Don't link to unviewable files
1061 $row .= '<span class="history-deleted">' . $lang->userTimeAndDate( $timestamp, $user ) . '</span>';
1062 } elseif ( $file->isDeleted( File
::DELETED_FILE
) ) {
1064 $this->preventClickjacking();
1065 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
1066 # Make a link to review the image
1067 $url = Linker
::linkKnown(
1069 $lang->userTimeAndDate( $timestamp, $user ),
1072 'target' => $this->title
->getPrefixedText(),
1074 'token' => $user->getEditToken( $img )
1078 $url = $lang->userTimeAndDate( $timestamp, $user );
1080 $row .= '<span class="history-deleted">' . $url . '</span>';
1082 $url = $iscur ?
$this->current
->getUrl() : $this->current
->getArchiveUrl( $img );
1083 $row .= Xml
::element( 'a', array( 'href' => $url ), $lang->userTimeAndDate( $timestamp, $user ) );
1088 if ( $this->showThumb
) {
1089 $row .= '<td>' . $this->getThumbForLine( $file ) . '</td>';
1092 // Image dimensions + size
1094 $row .= htmlspecialchars( $file->getDimensionsString() );
1095 $row .= $this->msg( 'word-separator' )->plain();
1096 $row .= '<span style="white-space: nowrap;">';
1097 $row .= $this->msg( 'parentheses' )->rawParams( Linker
::formatSize( $file->getSize() ) )->plain();
1103 // Hide deleted usernames
1104 if ( $file->isDeleted( File
::DELETED_USER
) ) {
1105 $row .= '<span class="history-deleted">' . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
1108 $row .= Linker
::userLink( $userId, $userText );
1109 $row .= $this->msg( 'word-separator' )->plain();
1110 $row .= '<span style="white-space: nowrap;">';
1111 $row .= Linker
::userToolLinks( $userId, $userText );
1114 $row .= htmlspecialchars( $userText );
1119 // Don't show deleted descriptions
1120 if ( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1121 $row .= '<td><span class="history-deleted">' . $this->msg( 'rev-deleted-comment' )->escaped() . '</span></td>';
1123 $row .= '<td dir="' . $wgContLang->getDir() . '">' . Linker
::formatComment( $description, $this->title
) . '</td>';
1127 wfRunHooks( 'ImagePageFileHistoryLine', array( $this, $file, &$row, &$rowClass ) );
1128 $classAttr = $rowClass ?
" class='$rowClass'" : '';
1130 return "<tr{$classAttr}>{$row}</tr>\n";
1137 protected function getThumbForLine( $file ) {
1138 $lang = $this->getLanguage();
1139 $user = $this->getUser();
1140 if ( $file->allowInlineDisplay() && $file->userCan( File
::DELETED_FILE
,$user )
1141 && !$file->isDeleted( File
::DELETED_FILE
) )
1147 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
1149 $thumbnail = $file->transform( $params );
1151 'alt' => $this->msg( 'filehist-thumbtext',
1152 $lang->userTimeAndDate( $timestamp, $user ),
1153 $lang->userDate( $timestamp, $user ),
1154 $lang->userTime( $timestamp, $user ) )->text(),
1155 'file-link' => true,
1158 if ( !$thumbnail ) {
1159 return $this->msg( 'filehist-nothumb' )->escaped();
1162 return $thumbnail->toHtml( $options );
1164 return $this->msg( 'filehist-nothumb' )->escaped();
1169 * @param $enable bool
1171 protected function preventClickjacking( $enable = true ) {
1172 $this->preventClickjacking
= $enable;
1178 public function getPreventClickjacking() {
1179 return $this->preventClickjacking
;
1183 class ImageHistoryPseudoPager
extends ReverseChronologicalPager
{
1184 protected $preventClickjacking = false;
1197 * @param ImagePage $imagePage
1199 function __construct( $imagePage ) {
1200 parent
::__construct();
1201 $this->mImagePage
= $imagePage;
1202 $this->mTitle
= clone ( $imagePage->getTitle() );
1203 $this->mTitle
->setFragment( '#filehistory' );
1205 $this->mHist
= array();
1206 $this->mRange
= array( 0, 0 ); // display range
1212 function getTitle() {
1213 return $this->mTitle
;
1216 function getQueryInfo() {
1223 function getIndexField() {
1228 * @param $row object
1231 function formatRow( $row ) {
1238 function getBody() {
1241 if ( count( $this->mHist
) ) {
1242 $list = new ImageHistoryList( $this->mImagePage
);
1243 # Generate prev/next links
1244 $navLink = $this->getNavigationBar();
1245 $s = $list->beginImageHistoryList( $navLink );
1246 // Skip rows there just for paging links
1247 for ( $i = $this->mRange
[0]; $i <= $this->mRange
[1]; $i++
) {
1248 $file = $this->mHist
[$i];
1249 $s .= $list->imageHistoryLine( !$file->isOld(), $file );
1251 $s .= $list->endImageHistoryList( $navLink );
1253 if ( $list->getPreventClickjacking() ) {
1254 $this->preventClickjacking();
1260 function doQuery() {
1261 if ( $this->mQueryDone
) {
1264 $this->mImg
= $this->mImagePage
->getFile(); // ensure loading
1265 if ( !$this->mImg
->exists() ) {
1268 $queryLimit = $this->mLimit +
1; // limit plus extra row
1269 if ( $this->mIsBackwards
) {
1270 // Fetch the file history
1271 $this->mHist
= $this->mImg
->getHistory( $queryLimit, null, $this->mOffset
, false );
1272 // The current rev may not meet the offset/limit
1273 $numRows = count( $this->mHist
);
1274 if ( $numRows <= $this->mLimit
&& $this->mImg
->getTimestamp() > $this->mOffset
) {
1275 $this->mHist
= array_merge( array( $this->mImg
), $this->mHist
);
1278 // The current rev may not meet the offset
1279 if ( !$this->mOffset ||
$this->mImg
->getTimestamp() < $this->mOffset
) {
1280 $this->mHist
[] = $this->mImg
;
1282 // Old image versions (fetch extra row for nav links)
1283 $oiLimit = count( $this->mHist
) ?
$this->mLimit
: $this->mLimit +
1;
1284 // Fetch the file history
1285 $this->mHist
= array_merge( $this->mHist
,
1286 $this->mImg
->getHistory( $oiLimit, $this->mOffset
, null, false ) );
1288 $numRows = count( $this->mHist
); // Total number of query results
1290 # Index value of top item in the list
1291 $firstIndex = $this->mIsBackwards ?
1292 $this->mHist
[$numRows - 1]->getTimestamp() : $this->mHist
[0]->getTimestamp();
1293 # Discard the extra result row if there is one
1294 if ( $numRows > $this->mLimit
&& $numRows > 1 ) {
1295 if ( $this->mIsBackwards
) {
1296 # Index value of item past the index
1297 $this->mPastTheEndIndex
= $this->mHist
[0]->getTimestamp();
1298 # Index value of bottom item in the list
1299 $lastIndex = $this->mHist
[1]->getTimestamp();
1301 $this->mRange
= array( 1, $numRows - 1 );
1303 # Index value of item past the index
1304 $this->mPastTheEndIndex
= $this->mHist
[$numRows - 1]->getTimestamp();
1305 # Index value of bottom item in the list
1306 $lastIndex = $this->mHist
[$numRows - 2]->getTimestamp();
1308 $this->mRange
= array( 0, $numRows - 2 );
1311 # Setting indexes to an empty string means that they will be
1312 # omitted if they would otherwise appear in URLs. It just so
1313 # happens that this is the right thing to do in the standard
1314 # UI, in all the relevant cases.
1315 $this->mPastTheEndIndex
= '';
1316 # Index value of bottom item in the list
1317 $lastIndex = $this->mIsBackwards ?
1318 $this->mHist
[0]->getTimestamp() : $this->mHist
[$numRows - 1]->getTimestamp();
1320 $this->mRange
= array( 0, $numRows - 1 );
1325 $this->mPastTheEndIndex
= '';
1327 if ( $this->mIsBackwards
) {
1328 $this->mIsFirst
= ( $numRows < $queryLimit );
1329 $this->mIsLast
= ( $this->mOffset
== '' );
1330 $this->mLastShown
= $firstIndex;
1331 $this->mFirstShown
= $lastIndex;
1333 $this->mIsFirst
= ( $this->mOffset
== '' );
1334 $this->mIsLast
= ( $numRows < $queryLimit );
1335 $this->mLastShown
= $lastIndex;
1336 $this->mFirstShown
= $firstIndex;
1338 $this->mQueryDone
= true;
1342 * @param $enable bool
1344 protected function preventClickjacking( $enable = true ) {
1345 $this->preventClickjacking
= $enable;
1351 public function getPreventClickjacking() {
1352 return $this->preventClickjacking
;