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()->getPageViewLanguage();
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 wfMessage( 'imagelinks' )->text() ) . "\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(
202 array( 'id' => 'metadata' ),
203 wfMessage( 'metadata' )->text() ) . "\n" );
204 $out->addWikiText( $this->makeMetadataTable( $formattedMetadata ) );
205 $out->addModules( array( 'mediawiki.action.view.metadata' ) );
208 // Add remote Filepage.css
209 if( !$this->repo
->isLocal() ) {
210 $css = $this->repo
->getDescriptionStylesheetUrl();
212 $out->addStyle( $css );
215 // always show the local local Filepage.css, bug 29277
216 $out->addModuleStyles( 'filepage' );
222 public function getDisplayedFile() {
224 return $this->displayImg
;
230 * @param $metadata Boolean: whether or not to show the metadata link
233 protected function showTOC( $metadata ) {
235 '<li><a href="#file">' . wfMessage( 'file-anchor-link' )->escaped() . '</a></li>',
236 '<li><a href="#filehistory">' . wfMessage( 'filehist' )->escaped() . '</a></li>',
237 '<li><a href="#filelinks">' . wfMessage( 'imagelinks' )->escaped() . '</a></li>',
240 $r[] = '<li><a href="#metadata">' . wfMessage( 'metadata' )->escaped() . '</a></li>';
243 wfRunHooks( 'ImagePageShowTOC', array( $this, &$r ) );
245 return '<ul id="filetoc">' . implode( "\n", $r ) . '</ul>';
249 * Make a table with metadata to be shown in the output page.
251 * @todo FIXME: Bad interface, see note on MediaHandler::formatMetadata().
253 * @param $metadata Array: the array containing the EXIF data
254 * @return String The metadata table. This is treated as Wikitext (!)
256 protected function makeMetadataTable( $metadata ) {
257 $r = "<div class=\"mw-imagepage-section-metadata\">";
258 $r .= wfMessage( 'metadata-help' )->plain();
259 $r .= "<table id=\"mw_metadata\" class=\"mw_metadata\">\n";
260 foreach ( $metadata as $type => $stuff ) {
261 foreach ( $stuff as $v ) {
262 # @todo FIXME: Why is this using escapeId for a class?!
263 $class = Sanitizer
::escapeId( $v['id'] );
264 if ( $type == 'collapsed' ) {
265 $class .= ' collapsable';
267 $r .= "<tr class=\"$class\">\n";
268 $r .= "<th>{$v['name']}</th>\n";
269 $r .= "<td>{$v['value']}</td>\n</tr>";
272 $r .= "</table>\n</div>\n";
277 * Overloading Article's getContentObject method.
279 * Omit noarticletext if sharedupload; text will be fetched from the
280 * shared upload server if possible.
283 public function getContentObject() {
285 if ( $this->mPage
->getFile() && !$this->mPage
->getFile()->isLocal() && 0 == $this->getID() ) {
288 return parent
::getContentObject();
291 protected function openShowImage() {
292 global $wgImageLimits, $wgEnableUploads, $wgSend404Code;
295 $out = $this->getContext()->getOutput();
296 $user = $this->getContext()->getUser();
297 $lang = $this->getContext()->getLanguage();
298 $dirmark = $lang->getDirMarkEntity();
299 $request = $this->getContext()->getRequest();
301 $sizeSel = intval( $user->getOption( 'imagesize' ) );
302 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
303 $sizeSel = User
::getDefaultOption( 'imagesize' );
305 // The user offset might still be incorrect, specially if
306 // $wgImageLimits got changed (see bug #8858).
307 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
308 // Default to the first offset in $wgImageLimits
312 $max = $wgImageLimits[$sizeSel];
314 $maxHeight = $max[1];
316 if ( $this->displayImg
->exists() ) {
318 $page = $request->getIntOrNull( 'page' );
319 if ( is_null( $page ) ) {
323 $params = array( 'page' => $page );
325 $width_orig = $this->displayImg
->getWidth( $page );
326 $width = $width_orig;
327 $height_orig = $this->displayImg
->getHeight( $page );
328 $height = $height_orig;
330 $filename = wfEscapeWikiText( $this->displayImg
->getName() );
331 $linktext = $filename;
333 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this, &$out ) );
335 if ( $this->displayImg
->allowInlineDisplay() ) {
338 # "Download high res version" link below the image
339 # $msgsize = wfMessage( 'file-info-size', $width_orig, $height_orig, Linker::formatSize( $this->displayImg->getSize() ), $mime )->escaped();
340 # We'll show a thumbnail of this image
341 if ( $width > $maxWidth ||
$height > $maxHeight ) {
342 # Calculate the thumbnail size.
343 # First case, the limiting factor is the width, not the height.
344 if ( $width / $height >= $maxWidth / $maxHeight ) { // FIXME: Possible divison by 0. bug 36911
345 $height = round( $height * $maxWidth / $width ); // FIXME: Possible divison by 0. bug 36911
347 # Note that $height <= $maxHeight now.
349 $newwidth = floor( $width * $maxHeight / $height ); // FIXME: Possible divison by 0. bug 36911
350 $height = round( $height * $newwidth / $width ); // FIXME: Possible divison by 0. bug 36911
352 # Note that $height <= $maxHeight now, but might not be identical
353 # because of rounding.
355 $linktext = wfMessage( 'show-big-image' )->escaped();
356 if ( $this->displayImg
->getRepo()->canTransformVia404() ) {
357 $thumbSizes = $wgImageLimits;
359 # Creating thumb links triggers thumbnail generation.
360 # Just generate the thumb for the current users prefs.
361 $thumbOption = $user->getOption( 'thumbsize' );
362 $thumbSizes = array( isset( $wgImageLimits[$thumbOption] )
363 ?
$wgImageLimits[$thumbOption]
364 : $wgImageLimits[User
::getDefaultOption( 'thumbsize' )] );
366 # Generate thumbnails or thumbnail links as needed...
367 $otherSizes = array();
368 foreach ( $thumbSizes as $size ) {
369 if ( $size[0] < $width_orig && $size[1] < $height_orig
370 && $size[0] != $width && $size[1] != $height )
372 $otherSizes[] = $this->makeSizeLink( $params, $size[0], $size[1] );
375 $msgsmall = wfMessage( 'show-big-image-preview' )->
376 rawParams( $this->makeSizeLink( $params, $width, $height ) )->
378 if ( count( $otherSizes ) ) {
380 Html
::rawElement( 'span', array( 'class' => 'mw-filepage-other-resolutions' ),
381 wfMessage( 'show-big-image-other' )->rawParams( $lang->pipeList( $otherSizes ) )->
382 params( count( $otherSizes ) )->parse()
385 } elseif ( $width == 0 && $height == 0 ) {
386 # Some sort of audio file that doesn't have dimensions
387 # Don't output a no hi res message for such a file
389 } elseif ( $this->displayImg
->isVectorized() ) {
390 # For vectorized images, full size is just the frame size
393 # Image is small enough to show full size on image page
394 $msgsmall = wfMessage( 'file-nohires' )->parse();
397 $params['width'] = $width;
398 $params['height'] = $height;
399 $thumbnail = $this->displayImg
->transform( $params );
401 $anchorclose = Html
::rawElement( 'div', array( 'class' => 'mw-filepage-resolutioninfo' ), $msgsmall );
403 $isMulti = $this->displayImg
->isMultipage() && $this->displayImg
->pageCount() > 1;
405 $out->addHTML( '<table class="multipageimage"><tr><td>' );
410 'alt' => $this->displayImg
->getTitle()->getPrefixedText(),
413 $out->addHTML( '<div class="fullImageLink" id="file">' .
414 $thumbnail->toHtml( $options ) .
415 $anchorclose . "</div>\n" );
419 $count = $this->displayImg
->pageCount();
422 $label = $out->parse( wfMessage( 'imgmultipageprev' )->text(), false );
423 $link = Linker
::linkKnown(
427 array( 'page' => $page - 1 )
429 $thumb1 = Linker
::makeThumbLinkObj( $this->getTitle(), $this->displayImg
, $link, $label, 'none',
430 array( 'page' => $page - 1 ) );
435 if ( $page < $count ) {
436 $label = wfMessage( 'imgmultipagenext' )->text();
437 $link = Linker
::linkKnown(
441 array( 'page' => $page +
1 )
443 $thumb2 = Linker
::makeThumbLinkObj( $this->getTitle(), $this->displayImg
, $link, $label, 'none',
444 array( 'page' => $page +
1 ) );
452 'name' => 'pageselector',
453 'action' => $wgScript,
454 'onchange' => 'document.pageselector.submit();',
457 for ( $i = 1; $i <= $count; $i++
) {
458 $options[] = Xml
::option( $lang->formatNum( $i ), $i, $i == $page );
460 $select = Xml
::tags( 'select',
461 array( 'id' => 'pageselector', 'name' => 'page' ),
462 implode( "\n", $options ) );
465 '</td><td><div class="multipageimagenavbox">' .
466 Xml
::openElement( 'form', $formParams ) .
467 Html
::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
468 wfMessage( 'imgmultigoto' )->rawParams( $select )->parse() .
469 Xml
::submitButton( wfMessage( 'imgmultigo' )->text() ) .
470 Xml
::closeElement( 'form' ) .
471 "<hr />$thumb1\n$thumb2<br style=\"clear: both\" /></div></td></tr></table>"
474 } elseif ( $this->displayImg
->isSafeFile() ) {
475 # if direct link is allowed but it's not a renderable image, show an icon.
476 $icon = $this->displayImg
->iconThumb();
478 $out->addHTML( '<div class="fullImageLink" id="file">' .
479 $icon->toHtml( array( 'file-link' => true ) ) .
483 $longDesc = wfMessage( 'parentheses', $this->displayImg
->getLongDesc() )->text();
485 $medialink = "[[Media:$filename|$linktext]]";
487 if ( !$this->displayImg
->isSafeFile() ) {
488 $warning = wfMessage( 'mediawarning' )->plain();
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>
509 // Add cannot animate thumbnail warning
510 if ( !$this->displayImg
->canAnimateThumbIfAppropriate() ) {
511 // Include the extension so wiki admins can
512 // customize it on a per file-type basis
513 // (aka say things like use format X instead).
514 // additionally have a specific message for
515 // file-no-thumb-animation-gif
516 $ext = $this->displayImg
->getExtension();
517 $noAnimMesg = wfMessageFallback(
518 'file-no-thumb-animation-' . $ext,
519 'file-no-thumb-animation'
522 $out->addWikiText( <<<EOT
523 <div class="mw-noanimatethumb">{$noAnimMesg}</div>
528 if ( !$this->displayImg
->isLocal() ) {
529 $this->printSharedImageText();
532 # Image does not exist
533 if ( !$this->getID() ) {
534 # No article exists either
535 # Show deletion log to be consistent with normal articles
536 LogEventsList
::showLogExtract(
538 array( 'delete', 'move' ),
539 $this->getTitle()->getPrefixedText(),
542 'conds' => array( "log_action != 'revision'" ),
543 'showIfEmpty' => false,
544 'msgKey' => array( 'moveddeleted-notice' )
549 if ( $wgEnableUploads && $user->isAllowed( 'upload' ) ) {
550 // Only show an upload link if the user can upload
551 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
553 'filepage-nofile-link',
554 $uploadTitle->getFullURL( array( 'wpDestFile' => $this->mPage
->getFile()->getName() ) )
557 $nofile = 'filepage-nofile';
559 // Note, if there is an image description page, but
560 // no image, then this setRobotPolicy is overriden
561 // by Article::View().
562 $out->setRobotPolicy( 'noindex,nofollow' );
563 $out->wrapWikiMsg( "<div id='mw-imagepage-nofile' class='plainlinks'>\n$1\n</div>", $nofile );
564 if ( !$this->getID() && $wgSend404Code ) {
565 // If there is no image, no shared image, and no description page,
566 // output a 404, to be consistent with articles.
567 $request->response()->header( 'HTTP/1.1 404 Not Found' );
570 $out->setFileVersion( $this->displayImg
);
574 * Creates an thumbnail of specified size and returns an HTML link to it
575 * @param $params array Scaler parameters
580 private function makeSizeLink( $params, $width, $height ) {
581 $params['width'] = $width;
582 $params['height'] = $height;
583 $thumbnail = $this->displayImg
->transform( $params );
584 if ( $thumbnail && !$thumbnail->isError() ) {
585 return Html
::rawElement( 'a', array(
586 'href' => $thumbnail->getUrl(),
587 'class' => 'mw-thumbnail-link'
588 ), wfMessage( 'show-big-image-size' )->numParams(
589 $thumbnail->getWidth(), $thumbnail->getHeight()
597 * Show a notice that the file is from a shared repository
599 protected function printSharedImageText() {
600 $out = $this->getContext()->getOutput();
603 $descUrl = $this->mPage
->getFile()->getDescriptionUrl();
604 $descText = $this->mPage
->getFile()->getDescriptionText();
606 /* Add canonical to head if there is no local page for this shared file */
607 if( $descUrl && $this->mPage
->getID() == 0 ) {
608 $out->setCanonicalUrl( $descUrl );
611 $wrap = "<div class=\"sharedUploadNotice\">\n$1\n</div>\n";
612 $repo = $this->mPage
->getFile()->getRepo()->getDisplayName();
614 if ( $descUrl && $descText && wfMessage( 'sharedupload-desc-here' )->plain() !== '-' ) {
615 $out->wrapWikiMsg( $wrap, array( 'sharedupload-desc-here', $repo, $descUrl ) );
616 } elseif ( $descUrl && wfMessage( 'sharedupload-desc-there' )->plain() !== '-' ) {
617 $out->wrapWikiMsg( $wrap, array( 'sharedupload-desc-there', $repo, $descUrl ) );
619 $out->wrapWikiMsg( $wrap, array( 'sharedupload', $repo ), ''/*BACKCOMPAT*/ );
623 $this->mExtraDescription
= $descText;
627 public function getUploadUrl() {
629 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
630 return $uploadTitle->getFullURL( array(
631 'wpDestFile' => $this->mPage
->getFile()->getName(),
637 * Print out the various links at the bottom of the image page, e.g. reupload,
638 * external editing (and instructions link) etc.
640 protected function uploadLinksBox() {
641 global $wgEnableUploads, $wgUseExternalEditor;
643 if ( !$wgEnableUploads ) {
648 if ( !$this->mPage
->getFile()->isLocal() ) {
652 $out = $this->getContext()->getOutput();
653 $out->addHTML( "<ul>\n" );
655 # "Upload a new version of this file" link
656 $canUpload = $this->getTitle()->userCan( 'upload', $this->getContext()->getUser() );
657 if ( $canUpload && UploadBase
::userCanReUpload( $this->getContext()->getUser(), $this->mPage
->getFile()->name
) ) {
658 $ulink = Linker
::makeExternalLink( $this->getUploadUrl(), wfMessage( 'uploadnewversion-linktext' )->text() );
659 $out->addHTML( "<li id=\"mw-imagepage-reupload-link\"><div class=\"plainlinks\">{$ulink}</div></li>\n" );
661 $out->addHTML( "<li id=\"mw-imagepage-upload-disallowed\">" . $this->getContext()->msg( 'upload-disallowed-here' )->escaped() . "</li>\n" );
664 # External editing link
665 if ( $wgUseExternalEditor ) {
666 $elink = Linker
::linkKnown(
668 wfMessage( 'edit-externally' )->escaped(),
672 'externaledit' => 'true',
677 '<li id="mw-imagepage-edit-external">' . $elink . ' <small>' .
678 wfMessage( 'edit-externally-help' )->parse() .
683 $out->addHTML( "</ul>\n" );
686 protected function closeShowImage() { } # For overloading
689 * If the page we've just displayed is in the "Image" namespace,
690 * we follow it with an upload history of the image and its usage.
692 protected function imageHistory() {
694 $out = $this->getContext()->getOutput();
695 $pager = new ImageHistoryPseudoPager( $this );
696 $out->addHTML( $pager->getBody() );
697 $out->preventClickjacking( $pager->getPreventClickjacking() );
699 $this->mPage
->getFile()->resetHistory(); // free db resources
701 # Exist check because we don't want to show this on pages where an image
702 # doesn't exist along with the noimage message, that would suck. -ævar
703 if ( $this->mPage
->getFile()->exists() ) {
704 $this->uploadLinksBox();
711 * @return ResultWrapper
713 protected function queryImageLinks( $target, $limit ) {
714 $dbr = wfGetDB( DB_SLAVE
);
717 array( 'imagelinks', 'page' ),
718 array( 'page_namespace', 'page_title', 'page_is_redirect', 'il_to' ),
719 array( 'il_to' => $target, 'il_from = page_id' ),
721 array( 'LIMIT' => $limit +
1, 'ORDER BY' => 'il_from', )
725 protected function imageLinks() {
728 $out = $this->getContext()->getOutput();
729 $res = $this->queryImageLinks( $this->getTitle()->getDbKey(), $limit +
1);
731 $redirects = array();
732 foreach ( $res as $row ) {
733 if ( $row->page_is_redirect
) {
734 $redirects[$row->page_title
] = array();
738 $count = count( $rows );
740 $hasMore = $count > $limit;
741 if ( !$hasMore && count( $redirects ) ) {
742 $res = $this->queryImageLinks( array_keys( $redirects ),
743 $limit - count( $rows ) +
1 );
744 foreach ( $res as $row ) {
745 $redirects[$row->il_to
][] = $row;
748 $hasMore = ( $res->numRows() +
count( $rows ) ) > $limit;
753 Html
::rawElement( 'div',
754 array( 'id' => 'mw-imagepage-nolinkstoimage' ), "\n$1\n" ),
760 $out->addHTML( "<div id='mw-imagepage-section-linkstoimage'>\n" );
762 $out->addWikiMsg( 'linkstoimage', $count );
764 // More links than the limit. Add a link to [[Special:Whatlinkshere]]
765 $out->addWikiMsg( 'linkstoimage-more',
766 $this->getContext()->getLanguage()->formatNum( $limit ),
767 $this->getTitle()->getPrefixedDBkey()
772 Html
::openElement( 'ul',
773 array( 'class' => 'mw-imagepage-linkstoimage' ) ) . "\n"
777 // Sort the list by namespace:title
778 usort( $rows, array( $this, 'compare' ) );
780 // Create links for every element
782 foreach( $rows as $element ) {
784 if ( $currentCount > $limit ) {
788 $link = Linker
::linkKnown( Title
::makeTitle( $element->page_namespace
, $element->page_title
) );
789 if ( !isset( $redirects[$element->page_title
] ) ) {
792 } elseif ( count( $redirects[$element->page_title
] ) === 0 ) {
793 # Redirect without usages
794 $liContents = wfMessage( 'linkstoimage-redirect' )->rawParams( $link, '' )->parse();
796 # Redirect with usages
798 foreach ( $redirects[$element->page_title
] as $row ) {
800 if ( $currentCount > $limit ) {
804 $link2 = Linker
::linkKnown( Title
::makeTitle( $row->page_namespace
, $row->page_title
) );
805 $li .= Html
::rawElement(
807 array( 'class' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace
),
812 $ul = Html
::rawElement(
814 array( 'class' => 'mw-imagepage-redirectstofile'),
817 $liContents = wfMessage( 'linkstoimage-redirect' )->rawParams(
818 $link, $ul )->parse();
820 $out->addHTML( Html
::rawElement(
822 array( 'class' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace
),
828 $out->addHTML( Html
::closeElement( 'ul' ) . "\n" );
831 // Add a links to [[Special:Whatlinkshere]]
832 if ( $count > $limit ) {
833 $out->addWikiMsg( 'morelinkstoimage', $this->getTitle()->getPrefixedDBkey() );
835 $out->addHTML( Html
::closeElement( 'div' ) . "\n" );
838 protected function imageDupes() {
840 $out = $this->getContext()->getOutput();
842 $dupes = $this->mPage
->getDuplicates();
843 if ( count( $dupes ) == 0 ) {
847 $out->addHTML( "<div id='mw-imagepage-section-duplicates'>\n" );
848 $out->addWikiMsg( 'duplicatesoffile',
849 $this->getContext()->getLanguage()->formatNum( count( $dupes ) ), $this->getTitle()->getDBkey()
851 $out->addHTML( "<ul class='mw-imagepage-duplicates'>\n" );
856 foreach ( $dupes as $file ) {
858 if ( $file->isLocal() ) {
859 $link = Linker
::linkKnown( $file->getTitle() );
861 $link = Linker
::makeExternalLink( $file->getDescriptionUrl(),
862 $file->getTitle()->getPrefixedText() );
863 $fromSrc = wfMessage( 'shared-repo-from', $file->getRepo()->getDisplayName() )->text();
865 $out->addHTML( "<li>{$link} {$fromSrc}</li>\n" );
867 $out->addHTML( "</ul></div>\n" );
871 * Delete the file, or an earlier version of it
873 public function delete() {
874 $file = $this->mPage
->getFile();
875 if ( !$file->exists() ||
!$file->isLocal() ||
$file->getRedirected() ) {
876 // Standard article deletion
881 $deleter = new FileDeleteForm( $file );
886 * Display an error with a wikitext description
888 * @param $description String
890 function showError( $description ) {
891 $out = $this->getContext()->getOutput();
892 $out->setPageTitle( wfMessage( 'internalerror' ) );
893 $out->setRobotPolicy( 'noindex,nofollow' );
894 $out->setArticleRelated( false );
895 $out->enableClientCache( false );
896 $out->addWikiText( $description );
900 * Callback for usort() to do link sorts by (namespace, title)
901 * Function copied from Title::compare()
903 * @param $a object page to compare with
904 * @param $b object page to compare with
905 * @return Integer: result of string comparison, or namespace comparison
907 protected function compare( $a, $b ) {
908 if ( $a->page_namespace
== $b->page_namespace
) {
909 return strcmp( $a->page_title
, $b->page_title
);
911 return $a->page_namespace
- $b->page_namespace
;
917 * Builds the image revision log shown on image pages
921 class ImageHistoryList
extends ContextSource
{
936 protected $imagePage;
943 protected $repo, $showThumb;
944 protected $preventClickjacking = false;
947 * @param ImagePage $imagePage
949 public function __construct( $imagePage ) {
950 global $wgShowArchiveThumbnails;
951 $this->current
= $imagePage->getFile();
952 $this->img
= $imagePage->getDisplayedFile();
953 $this->title
= $imagePage->getTitle();
954 $this->imagePage
= $imagePage;
955 $this->showThumb
= $wgShowArchiveThumbnails && $this->img
->canRender();
956 $this->setContext( $imagePage->getContext() );
962 public function getImagePage() {
963 return $this->imagePage
;
969 public function getFile() {
974 * @param $navLinks string
977 public function beginImageHistoryList( $navLinks = '' ) {
978 return Xml
::element( 'h2', array( 'id' => 'filehistory' ), $this->msg( 'filehist' )->text() ) . "\n"
979 . "<div id=\"mw-imagepage-section-filehistory\">\n"
980 . $this->msg( 'filehist-help' )->parseAsBlock()
982 . Xml
::openElement( 'table', array( 'class' => 'wikitable filehistory' ) ) . "\n"
984 . ( $this->current
->isLocal() && ( $this->getUser()->isAllowedAny( 'delete', 'deletedhistory' ) ) ?
'<td></td>' : '' )
985 . '<th>' . $this->msg( 'filehist-datetime' )->escaped() . '</th>'
986 . ( $this->showThumb ?
'<th>' . $this->msg( 'filehist-thumb' )->escaped() . '</th>' : '' )
987 . '<th>' . $this->msg( 'filehist-dimensions' )->escaped() . '</th>'
988 . '<th>' . $this->msg( 'filehist-user' )->escaped() . '</th>'
989 . '<th>' . $this->msg( 'filehist-comment' )->escaped() . '</th>'
994 * @param $navLinks string
997 public function endImageHistoryList( $navLinks = '' ) {
998 return "</table>\n$navLinks\n</div>\n";
1006 public function imageHistoryLine( $iscur, $file ) {
1009 $user = $this->getUser();
1010 $lang = $this->getLanguage();
1011 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
1012 $img = $iscur ?
$file->getName() : $file->getArchiveName();
1013 $userId = $file->getUser( 'id' );
1014 $userText = $file->getUser( 'text' );
1015 $description = $file->getDescription( File
::FOR_THIS_USER
, $user );
1017 $local = $this->current
->isLocal();
1018 $row = $selected = '';
1021 if ( $local && ( $user->isAllowedAny( 'delete', 'deletedhistory' ) ) ) {
1023 # Link to remove from history
1024 if ( $user->isAllowed( 'delete' ) ) {
1025 $q = array( 'action' => 'delete' );
1027 $q['oldimage'] = $img;
1029 $row .= Linker
::linkKnown(
1031 $this->msg( $iscur ?
'filehist-deleteall' : 'filehist-deleteone' )->escaped(),
1035 # Link to hide content. Don't show useless link to people who cannot hide revisions.
1036 $canHide = $user->isAllowed( 'deleterevision' );
1037 if ( $canHide ||
( $user->isAllowed( 'deletedhistory' ) && $file->getVisibility() ) ) {
1038 if ( $user->isAllowed( 'delete' ) ) {
1041 // If file is top revision or locked from this user, don't link
1042 if ( $iscur ||
!$file->userCan( File
::DELETED_RESTRICTED
, $user ) ) {
1043 $del = Linker
::revDeleteLinkDisabled( $canHide );
1045 list( $ts, ) = explode( '!', $img, 2 );
1047 'type' => 'oldimage',
1048 'target' => $this->title
->getPrefixedText(),
1051 $del = Linker
::revDeleteLink( $query,
1052 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1059 // Reversion link/current indicator
1062 $row .= $this->msg( 'filehist-current' )->escaped();
1063 } elseif ( $local && $this->title
->quickUserCan( 'edit', $user )
1064 && $this->title
->quickUserCan( 'upload', $user )
1066 if ( $file->isDeleted( File
::DELETED_FILE
) ) {
1067 $row .= $this->msg( 'filehist-revert' )->escaped();
1069 $row .= Linker
::linkKnown(
1071 $this->msg( 'filehist-revert' )->escaped(),
1074 'action' => 'revert',
1076 'wpEditToken' => $user->getEditToken( $img )
1083 // Date/time and image link
1084 if ( $file->getTimestamp() === $this->img
->getTimestamp() ) {
1085 $selected = "class='filehistory-selected'";
1087 $row .= "<td $selected style='white-space: nowrap;'>";
1088 if ( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
1089 # Don't link to unviewable files
1090 $row .= '<span class="history-deleted">' . $lang->userTimeAndDate( $timestamp, $user ) . '</span>';
1091 } elseif ( $file->isDeleted( File
::DELETED_FILE
) ) {
1093 $this->preventClickjacking();
1094 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
1095 # Make a link to review the image
1096 $url = Linker
::linkKnown(
1098 $lang->userTimeAndDate( $timestamp, $user ),
1101 'target' => $this->title
->getPrefixedText(),
1103 'token' => $user->getEditToken( $img )
1107 $url = $lang->userTimeAndDate( $timestamp, $user );
1109 $row .= '<span class="history-deleted">' . $url . '</span>';
1111 $url = $iscur ?
$this->current
->getUrl() : $this->current
->getArchiveUrl( $img );
1112 $row .= Xml
::element( 'a', array( 'href' => $url ), $lang->userTimeAndDate( $timestamp, $user ) );
1117 if ( $this->showThumb
) {
1118 $row .= '<td>' . $this->getThumbForLine( $file ) . '</td>';
1121 // Image dimensions + size
1123 $row .= htmlspecialchars( $file->getDimensionsString() );
1124 $row .= $this->msg( 'word-separator' )->plain();
1125 $row .= '<span style="white-space: nowrap;">';
1126 $row .= $this->msg( 'parentheses' )->rawParams( Linker
::formatSize( $file->getSize() ) )->plain();
1132 // Hide deleted usernames
1133 if ( $file->isDeleted( File
::DELETED_USER
) ) {
1134 $row .= '<span class="history-deleted">' . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
1137 $row .= Linker
::userLink( $userId, $userText );
1138 $row .= $this->msg( 'word-separator' )->plain();
1139 $row .= '<span style="white-space: nowrap;">';
1140 $row .= Linker
::userToolLinks( $userId, $userText );
1143 $row .= htmlspecialchars( $userText );
1148 // Don't show deleted descriptions
1149 if ( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1150 $row .= '<td><span class="history-deleted">' . $this->msg( 'rev-deleted-comment' )->escaped() . '</span></td>';
1152 $row .= '<td dir="' . $wgContLang->getDir() . '">' . Linker
::formatComment( $description, $this->title
) . '</td>';
1156 wfRunHooks( 'ImagePageFileHistoryLine', array( $this, $file, &$row, &$rowClass ) );
1157 $classAttr = $rowClass ?
" class='$rowClass'" : '';
1159 return "<tr{$classAttr}>{$row}</tr>\n";
1166 protected function getThumbForLine( $file ) {
1167 $lang = $this->getLanguage();
1168 $user = $this->getUser();
1169 if ( $file->allowInlineDisplay() && $file->userCan( File
::DELETED_FILE
, $user )
1170 && !$file->isDeleted( File
::DELETED_FILE
) )
1176 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
1178 $thumbnail = $file->transform( $params );
1180 'alt' => $this->msg( 'filehist-thumbtext',
1181 $lang->userTimeAndDate( $timestamp, $user ),
1182 $lang->userDate( $timestamp, $user ),
1183 $lang->userTime( $timestamp, $user ) )->text(),
1184 'file-link' => true,
1187 if ( !$thumbnail ) {
1188 return $this->msg( 'filehist-nothumb' )->escaped();
1191 return $thumbnail->toHtml( $options );
1193 return $this->msg( 'filehist-nothumb' )->escaped();
1198 * @param $enable bool
1200 protected function preventClickjacking( $enable = true ) {
1201 $this->preventClickjacking
= $enable;
1207 public function getPreventClickjacking() {
1208 return $this->preventClickjacking
;
1212 class ImageHistoryPseudoPager
extends ReverseChronologicalPager
{
1213 protected $preventClickjacking = false;
1226 * @param ImagePage $imagePage
1228 function __construct( $imagePage ) {
1229 parent
::__construct( $imagePage->getContext() );
1230 $this->mImagePage
= $imagePage;
1231 $this->mTitle
= clone ( $imagePage->getTitle() );
1232 $this->mTitle
->setFragment( '#filehistory' );
1234 $this->mHist
= array();
1235 $this->mRange
= array( 0, 0 ); // display range
1241 function getTitle() {
1242 return $this->mTitle
;
1245 function getQueryInfo() {
1252 function getIndexField() {
1257 * @param $row object
1260 function formatRow( $row ) {
1267 function getBody() {
1270 if ( count( $this->mHist
) ) {
1271 $list = new ImageHistoryList( $this->mImagePage
);
1272 # Generate prev/next links
1273 $navLink = $this->getNavigationBar();
1274 $s = $list->beginImageHistoryList( $navLink );
1275 // Skip rows there just for paging links
1276 for ( $i = $this->mRange
[0]; $i <= $this->mRange
[1]; $i++
) {
1277 $file = $this->mHist
[$i];
1278 $s .= $list->imageHistoryLine( !$file->isOld(), $file );
1280 $s .= $list->endImageHistoryList( $navLink );
1282 if ( $list->getPreventClickjacking() ) {
1283 $this->preventClickjacking();
1289 function doQuery() {
1290 if ( $this->mQueryDone
) {
1293 $this->mImg
= $this->mImagePage
->getFile(); // ensure loading
1294 if ( !$this->mImg
->exists() ) {
1297 $queryLimit = $this->mLimit +
1; // limit plus extra row
1298 if ( $this->mIsBackwards
) {
1299 // Fetch the file history
1300 $this->mHist
= $this->mImg
->getHistory( $queryLimit, null, $this->mOffset
, false );
1301 // The current rev may not meet the offset/limit
1302 $numRows = count( $this->mHist
);
1303 if ( $numRows <= $this->mLimit
&& $this->mImg
->getTimestamp() > $this->mOffset
) {
1304 $this->mHist
= array_merge( array( $this->mImg
), $this->mHist
);
1307 // The current rev may not meet the offset
1308 if ( !$this->mOffset ||
$this->mImg
->getTimestamp() < $this->mOffset
) {
1309 $this->mHist
[] = $this->mImg
;
1311 // Old image versions (fetch extra row for nav links)
1312 $oiLimit = count( $this->mHist
) ?
$this->mLimit
: $this->mLimit +
1;
1313 // Fetch the file history
1314 $this->mHist
= array_merge( $this->mHist
,
1315 $this->mImg
->getHistory( $oiLimit, $this->mOffset
, null, false ) );
1317 $numRows = count( $this->mHist
); // Total number of query results
1319 # Index value of top item in the list
1320 $firstIndex = $this->mIsBackwards ?
1321 $this->mHist
[$numRows - 1]->getTimestamp() : $this->mHist
[0]->getTimestamp();
1322 # Discard the extra result row if there is one
1323 if ( $numRows > $this->mLimit
&& $numRows > 1 ) {
1324 if ( $this->mIsBackwards
) {
1325 # Index value of item past the index
1326 $this->mPastTheEndIndex
= $this->mHist
[0]->getTimestamp();
1327 # Index value of bottom item in the list
1328 $lastIndex = $this->mHist
[1]->getTimestamp();
1330 $this->mRange
= array( 1, $numRows - 1 );
1332 # Index value of item past the index
1333 $this->mPastTheEndIndex
= $this->mHist
[$numRows - 1]->getTimestamp();
1334 # Index value of bottom item in the list
1335 $lastIndex = $this->mHist
[$numRows - 2]->getTimestamp();
1337 $this->mRange
= array( 0, $numRows - 2 );
1340 # Setting indexes to an empty string means that they will be
1341 # omitted if they would otherwise appear in URLs. It just so
1342 # happens that this is the right thing to do in the standard
1343 # UI, in all the relevant cases.
1344 $this->mPastTheEndIndex
= '';
1345 # Index value of bottom item in the list
1346 $lastIndex = $this->mIsBackwards ?
1347 $this->mHist
[0]->getTimestamp() : $this->mHist
[$numRows - 1]->getTimestamp();
1349 $this->mRange
= array( 0, $numRows - 1 );
1354 $this->mPastTheEndIndex
= '';
1356 if ( $this->mIsBackwards
) {
1357 $this->mIsFirst
= ( $numRows < $queryLimit );
1358 $this->mIsLast
= ( $this->mOffset
== '' );
1359 $this->mLastShown
= $firstIndex;
1360 $this->mFirstShown
= $lastIndex;
1362 $this->mIsFirst
= ( $this->mOffset
== '' );
1363 $this->mIsLast
= ( $numRows < $queryLimit );
1364 $this->mLastShown
= $lastIndex;
1365 $this->mFirstShown
= $firstIndex;
1367 $this->mQueryDone
= true;
1371 * @param $enable bool
1373 protected function preventClickjacking( $enable = true ) {
1374 $this->preventClickjacking
= $enable;
1380 public function getPreventClickjacking() {
1381 return $this->preventClickjacking
;