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 wfMessage( 'imagelinks' )->text() ) . "\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(
200 array( 'id' => 'metadata' ),
201 wfMessage( 'metadata' )->text() ) . "\n" );
202 $out->addWikiText( $this->makeMetadataTable( $formattedMetadata ) );
203 $out->addModules( array( 'mediawiki.action.view.metadata' ) );
206 // Add remote Filepage.css
207 if( !$this->repo
->isLocal() ) {
208 $css = $this->repo
->getDescriptionStylesheetUrl();
210 $out->addStyle( $css );
213 // always show the local local Filepage.css, bug 29277
214 $out->addModuleStyles( 'filepage' );
220 public function getDisplayedFile() {
222 return $this->displayImg
;
228 * @param $metadata Boolean: whether or not to show the metadata link
231 protected function showTOC( $metadata ) {
233 '<li><a href="#file">' . wfMessage( 'file-anchor-link' )->escaped() . '</a></li>',
234 '<li><a href="#filehistory">' . wfMessage( 'filehist' )->escaped() . '</a></li>',
235 '<li><a href="#filelinks">' . wfMessage( 'imagelinks' )->escaped() . '</a></li>',
238 $r[] = '<li><a href="#metadata">' . wfMessage( 'metadata' )->escaped() . '</a></li>';
241 wfRunHooks( 'ImagePageShowTOC', array( $this, &$r ) );
243 return '<ul id="filetoc">' . implode( "\n", $r ) . '</ul>';
247 * Make a table with metadata to be shown in the output page.
249 * @todo FIXME: Bad interface, see note on MediaHandler::formatMetadata().
251 * @param $metadata Array: the array containing the EXIF data
252 * @return String The metadata table. This is treated as Wikitext (!)
254 protected function makeMetadataTable( $metadata ) {
255 $r = "<div class=\"mw-imagepage-section-metadata\">";
256 $r .= wfMessage( 'metadata-help' )->plain();
257 $r .= "<table id=\"mw_metadata\" class=\"mw_metadata\">\n";
258 foreach ( $metadata as $type => $stuff ) {
259 foreach ( $stuff as $v ) {
260 # @todo FIXME: Why is this using escapeId for a class?!
261 $class = Sanitizer
::escapeId( $v['id'] );
262 if ( $type == 'collapsed' ) {
263 $class .= ' collapsable';
265 $r .= "<tr class=\"$class\">\n";
266 $r .= "<th>{$v['name']}</th>\n";
267 $r .= "<td>{$v['value']}</td>\n</tr>";
270 $r .= "</table>\n</div>\n";
275 * Overloading Article's getContent method.
277 * Omit noarticletext if sharedupload; text will be fetched from the
278 * shared upload server if possible.
281 public function getContent() {
283 if ( $this->mPage
->getFile() && !$this->mPage
->getFile()->isLocal() && 0 == $this->getID() ) {
286 return parent
::getContent();
289 protected function openShowImage() {
290 global $wgImageLimits, $wgEnableUploads, $wgSend404Code;
293 $out = $this->getContext()->getOutput();
294 $user = $this->getContext()->getUser();
295 $lang = $this->getContext()->getLanguage();
296 $dirmark = $lang->getDirMarkEntity();
297 $request = $this->getContext()->getRequest();
299 $sizeSel = intval( $user->getOption( 'imagesize' ) );
300 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
301 $sizeSel = User
::getDefaultOption( 'imagesize' );
303 // The user offset might still be incorrect, specially if
304 // $wgImageLimits got changed (see bug #8858).
305 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
306 // Default to the first offset in $wgImageLimits
310 $max = $wgImageLimits[$sizeSel];
312 $maxHeight = $max[1];
314 if ( $this->displayImg
->exists() ) {
316 $page = $request->getIntOrNull( 'page' );
317 if ( is_null( $page ) ) {
321 $params = array( 'page' => $page );
323 $width_orig = $this->displayImg
->getWidth( $page );
324 $width = $width_orig;
325 $height_orig = $this->displayImg
->getHeight( $page );
326 $height = $height_orig;
328 $longDesc = wfMessage( 'parentheses', $this->displayImg
->getLongDesc() )->text();
330 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this, &$out ) );
332 if ( $this->displayImg
->allowInlineDisplay() ) {
335 # "Download high res version" link below the image
336 # $msgsize = wfMessage( 'file-info-size', $width_orig, $height_orig, Linker::formatSize( $this->displayImg->getSize() ), $mime )->escaped();
337 # We'll show a thumbnail of this image
338 if ( $width > $maxWidth ||
$height > $maxHeight ) {
339 # Calculate the thumbnail size.
340 # First case, the limiting factor is the width, not the height.
341 if ( $width / $height >= $maxWidth / $maxHeight ) {
342 $height = round( $height * $maxWidth / $width );
344 # Note that $height <= $maxHeight now.
346 $newwidth = floor( $width * $maxHeight / $height );
347 $height = round( $height * $newwidth / $width );
349 # Note that $height <= $maxHeight now, but might not be identical
350 # because of rounding.
352 $msgbig = wfMessage( 'show-big-image' )->escaped();
353 if ( $this->displayImg
->getRepo()->canTransformVia404() ) {
354 $thumbSizes = $wgImageLimits;
356 # Creating thumb links triggers thumbnail generation.
357 # Just generate the thumb for the current users prefs.
358 $thumbOption = $user->getOption( 'thumbsize' );
359 $thumbSizes = array( isset( $wgImageLimits[$thumbOption] )
360 ?
$wgImageLimits[$thumbOption]
361 : $wgImageLimits[User
::getDefaultOption( 'thumbsize' )] );
363 # Generate thumbnails or thumbnail links as needed...
364 $otherSizes = array();
365 foreach ( $thumbSizes as $size ) {
366 if ( $size[0] < $width_orig && $size[1] < $height_orig
367 && $size[0] != $width && $size[1] != $height )
369 $otherSizes[] = $this->makeSizeLink( $params, $size[0], $size[1] );
372 $msgsmall = wfMessage( 'show-big-image-preview' )->
373 rawParams( $this->makeSizeLink( $params, $width, $height ) )->
375 if ( count( $otherSizes ) ) {
377 Html
::rawElement( 'span', array( 'class' => 'mw-filepage-other-resolutions' ),
378 wfMessage( 'show-big-image-other' )->rawParams( $lang->pipeList( $otherSizes ) )->
379 params( count( $otherSizes ) )->parse()
382 } elseif ( $width == 0 && $height == 0 ){
383 # Some sort of audio file that doesn't have dimensions
384 # Don't output a no hi res message for such a file
387 # Image is small enough to show full size on image page
388 $msgsmall = wfMessage( 'file-nohires' )->parse();
391 $params['width'] = $width;
392 $params['height'] = $height;
393 $thumbnail = $this->displayImg
->transform( $params );
396 $anchorclose = Html
::rawElement( 'div', array( 'class' => 'mw-filepage-resolutioninfo' ), $msgsmall );
398 $isMulti = $this->displayImg
->isMultipage() && $this->displayImg
->pageCount() > 1;
400 $out->addHTML( '<table class="multipageimage"><tr><td>' );
405 'alt' => $this->displayImg
->getTitle()->getPrefixedText(),
408 $out->addHTML( '<div class="fullImageLink" id="file">' .
409 $thumbnail->toHtml( $options ) .
410 $anchorclose . "</div>\n" );
414 $count = $this->displayImg
->pageCount();
417 $label = $out->parse( wfMessage( 'imgmultipageprev' )->text(), false );
418 $link = Linker
::linkKnown(
422 array( 'page' => $page - 1 )
424 $thumb1 = Linker
::makeThumbLinkObj( $this->getTitle(), $this->displayImg
, $link, $label, 'none',
425 array( 'page' => $page - 1 ) );
430 if ( $page < $count ) {
431 $label = wfMessage( 'imgmultipagenext' )->text();
432 $link = Linker
::linkKnown(
436 array( 'page' => $page +
1 )
438 $thumb2 = Linker
::makeThumbLinkObj( $this->getTitle(), $this->displayImg
, $link, $label, 'none',
439 array( 'page' => $page +
1 ) );
447 'name' => 'pageselector',
448 'action' => $wgScript,
449 'onchange' => 'document.pageselector.submit();',
452 for ( $i = 1; $i <= $count; $i++
) {
453 $options[] = Xml
::option( $lang->formatNum( $i ), $i, $i == $page );
455 $select = Xml
::tags( 'select',
456 array( 'id' => 'pageselector', 'name' => 'page' ),
457 implode( "\n", $options ) );
460 '</td><td><div class="multipageimagenavbox">' .
461 Xml
::openElement( 'form', $formParams ) .
462 Html
::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
463 wfMessage( 'imgmultigoto' )->rawParams( $select )->parse() .
464 Xml
::submitButton( wfMessage( 'imgmultigo' )->text() ) .
465 Xml
::closeElement( 'form' ) .
466 "<hr />$thumb1\n$thumb2<br style=\"clear: both\" /></div></td></tr></table>"
470 # if direct link is allowed but it's not a renderable image, show an icon.
471 if ( $this->displayImg
->isSafeFile() ) {
472 $icon = $this->displayImg
->iconThumb();
474 $out->addHTML( '<div class="fullImageLink" id="file">' .
475 $icon->toHtml( array( 'file-link' => true ) ) .
483 $filename = wfEscapeWikiText( $this->displayImg
->getName() );
484 $linktext = $filename;
485 if ( isset( $msgbig ) ) {
486 $linktext = wfEscapeWikiText( $msgbig );
488 $medialink = "[[Media:$filename|$linktext]]";
490 if ( !$this->displayImg
->isSafeFile() ) {
491 $warning = wfMessage( 'mediawarning' )->plain();
492 // dirmark is needed here to separate the file name, which
493 // most likely ends in Latin characters, from the description,
494 // which may begin with the file type. In RTL environment
495 // this will get messy.
496 // The dirmark, however, must not be immediately adjacent
497 // to the filename, because it can get copied with it.
499 $out->addWikiText( <<<EOT
500 <div class="fullMedia"><span class="dangerousLink">{$medialink}</span> $dirmark<span class="fileInfo">$longDesc</span></div>
501 <div class="mediaWarning">$warning</div>
505 $out->addWikiText( <<<EOT
506 <div class="fullMedia">{$medialink} {$dirmark}<span class="fileInfo">$longDesc</span>
513 if ( !$this->displayImg
->isLocal() ) {
514 $this->printSharedImageText();
517 # Image does not exist
518 if ( !$this->getID() ) {
519 # No article exists either
520 # Show deletion log to be consistent with normal articles
521 LogEventsList
::showLogExtract(
523 array( 'delete', 'move' ),
524 $this->getTitle()->getPrefixedText(),
527 'conds' => array( "log_action != 'revision'" ),
528 'showIfEmpty' => false,
529 'msgKey' => array( 'moveddeleted-notice' )
534 if ( $wgEnableUploads && $user->isAllowed( 'upload' ) ) {
535 // Only show an upload link if the user can upload
536 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
538 'filepage-nofile-link',
539 $uploadTitle->getFullURL( array( 'wpDestFile' => $this->mPage
->getFile()->getName() ) )
542 $nofile = 'filepage-nofile';
544 // Note, if there is an image description page, but
545 // no image, then this setRobotPolicy is overriden
546 // by Article::View().
547 $out->setRobotPolicy( 'noindex,nofollow' );
548 $out->wrapWikiMsg( "<div id='mw-imagepage-nofile' class='plainlinks'>\n$1\n</div>", $nofile );
549 if ( !$this->getID() && $wgSend404Code ) {
550 // If there is no image, no shared image, and no description page,
551 // output a 404, to be consistent with articles.
552 $request->response()->header( 'HTTP/1.1 404 Not Found' );
555 $out->setFileVersion( $this->displayImg
);
559 * Creates an thumbnail of specified size and returns an HTML link to it
560 * @param $params array Scaler parameters
565 private function makeSizeLink( $params, $width, $height ) {
566 $params['width'] = $width;
567 $params['height'] = $height;
568 $thumbnail = $this->displayImg
->transform( $params );
569 if ( $thumbnail && !$thumbnail->isError() ) {
570 return Html
::rawElement( 'a', array(
571 'href' => $thumbnail->getUrl(),
572 'class' => 'mw-thumbnail-link'
573 ), wfMessage( 'show-big-image-size' )->numParams(
574 $thumbnail->getWidth(), $thumbnail->getHeight()
582 * Show a notice that the file is from a shared repository
584 protected function printSharedImageText() {
585 $out = $this->getContext()->getOutput();
588 $descUrl = $this->mPage
->getFile()->getDescriptionUrl();
589 $descText = $this->mPage
->getFile()->getDescriptionText();
591 /* Add canonical to head if there is no local page for this shared file */
592 if( $descUrl && $this->mPage
->getID() == 0 ) {
593 $out->addLink( array( 'rel' => 'canonical', 'href' => $descUrl ) );
596 $wrap = "<div class=\"sharedUploadNotice\">\n$1\n</div>\n";
597 $repo = $this->mPage
->getFile()->getRepo()->getDisplayName();
599 if ( $descUrl && $descText && wfMessage( 'sharedupload-desc-here' )->plain() !== '-' ) {
600 $out->wrapWikiMsg( $wrap, array( 'sharedupload-desc-here', $repo, $descUrl ) );
601 } elseif ( $descUrl && wfMessage( 'sharedupload-desc-there' )->plain() !== '-' ) {
602 $out->wrapWikiMsg( $wrap, array( 'sharedupload-desc-there', $repo, $descUrl ) );
604 $out->wrapWikiMsg( $wrap, array( 'sharedupload', $repo ), ''/*BACKCOMPAT*/ );
608 $this->mExtraDescription
= $descText;
612 public function getUploadUrl() {
614 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
615 return $uploadTitle->getFullURL( array(
616 'wpDestFile' => $this->mPage
->getFile()->getName(),
622 * Print out the various links at the bottom of the image page, e.g. reupload,
623 * external editing (and instructions link) etc.
625 protected function uploadLinksBox() {
626 global $wgEnableUploads, $wgUseExternalEditor;
628 if ( !$wgEnableUploads ) {
633 if ( !$this->mPage
->getFile()->isLocal() ) {
637 $out = $this->getContext()->getOutput();
638 $out->addHTML( "<br /><ul>\n" );
640 # "Upload a new version of this file" link
641 if ( UploadBase
::userCanReUpload( $this->getContext()->getUser(), $this->mPage
->getFile()->name
) ) {
642 $ulink = Linker
::makeExternalLink( $this->getUploadUrl(), wfMessage( 'uploadnewversion-linktext' )->text() );
643 $out->addHTML( "<li id=\"mw-imagepage-reupload-link\"><div class=\"plainlinks\">{$ulink}</div></li>\n" );
646 # External editing link
647 if ( $wgUseExternalEditor ) {
648 $elink = Linker
::linkKnown(
650 wfMessage( 'edit-externally' )->escaped(),
654 'externaledit' => 'true',
659 '<li id="mw-imagepage-edit-external">' . $elink . ' <small>' .
660 wfMessage( 'edit-externally-help' )->parse() .
665 $out->addHTML( "</ul>\n" );
668 protected function closeShowImage() { } # For overloading
671 * If the page we've just displayed is in the "Image" namespace,
672 * we follow it with an upload history of the image and its usage.
674 protected function imageHistory() {
676 $out = $this->getContext()->getOutput();
677 $pager = new ImageHistoryPseudoPager( $this );
678 $out->addHTML( $pager->getBody() );
679 $out->preventClickjacking( $pager->getPreventClickjacking() );
681 $this->mPage
->getFile()->resetHistory(); // free db resources
683 # Exist check because we don't want to show this on pages where an image
684 # doesn't exist along with the noimage message, that would suck. -ævar
685 if ( $this->mPage
->getFile()->exists() ) {
686 $this->uploadLinksBox();
693 * @return ResultWrapper
695 protected function queryImageLinks( $target, $limit ) {
696 $dbr = wfGetDB( DB_SLAVE
);
699 array( 'imagelinks', 'page' ),
700 array( 'page_namespace', 'page_title', 'page_is_redirect', 'il_to' ),
701 array( 'il_to' => $target, 'il_from = page_id' ),
703 array( 'LIMIT' => $limit +
1, 'ORDER BY' => 'il_from', )
707 protected function imageLinks() {
710 $out = $this->getContext()->getOutput();
711 $res = $this->queryImageLinks( $this->getTitle()->getDbKey(), $limit +
1);
713 $redirects = array();
714 foreach ( $res as $row ) {
715 if ( $row->page_is_redirect
) {
716 $redirects[$row->page_title
] = array();
720 $count = count( $rows );
722 $hasMore = $count > $limit;
723 if ( !$hasMore && count( $redirects ) ) {
724 $res = $this->queryImageLinks( array_keys( $redirects ),
725 $limit - count( $rows ) +
1 );
726 foreach ( $res as $row ) {
727 $redirects[$row->il_to
][] = $row;
730 $hasMore = ( $res->numRows() +
count( $rows ) ) > $limit;
735 Html
::rawElement( 'div',
736 array( 'id' => 'mw-imagepage-nolinkstoimage' ), "\n$1\n" ),
742 $out->addHTML( "<div id='mw-imagepage-section-linkstoimage'>\n" );
744 $out->addWikiMsg( 'linkstoimage', $count );
746 // More links than the limit. Add a link to [[Special:Whatlinkshere]]
747 $out->addWikiMsg( 'linkstoimage-more',
748 $this->getContext()->getLanguage()->formatNum( $limit ),
749 $this->getTitle()->getPrefixedDBkey()
754 Html
::openElement( 'ul',
755 array( 'class' => 'mw-imagepage-linkstoimage' ) ) . "\n"
759 // Sort the list by namespace:title
760 usort( $rows, array( $this, 'compare' ) );
762 // Create links for every element
764 foreach( $rows as $element ) {
766 if ( $currentCount > $limit ) {
770 $link = Linker
::linkKnown( Title
::makeTitle( $element->page_namespace
, $element->page_title
) );
771 if ( !isset( $redirects[$element->page_title
] ) ) {
774 $ul = "<ul class='mw-imagepage-redirectstofile'>\n";
775 foreach ( $redirects[$element->page_title
] as $row ) {
777 if ( $currentCount > $limit ) {
781 $link2 = Linker
::linkKnown( Title
::makeTitle( $row->page_namespace
, $row->page_title
) );
782 $ul .= Html
::rawElement(
784 array( 'class' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace
),
789 $liContents = wfMessage( 'linkstoimage-redirect' )->rawParams(
790 $link, $ul )->parse();
792 $out->addHTML( Html
::rawElement(
794 array( 'class' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace
),
800 $out->addHTML( Html
::closeElement( 'ul' ) . "\n" );
803 // Add a links to [[Special:Whatlinkshere]]
804 if ( $count > $limit ) {
805 $out->addWikiMsg( 'morelinkstoimage', $this->getTitle()->getPrefixedDBkey() );
807 $out->addHTML( Html
::closeElement( 'div' ) . "\n" );
810 protected function imageDupes() {
812 $out = $this->getContext()->getOutput();
814 $dupes = $this->mPage
->getDuplicates();
815 if ( count( $dupes ) == 0 ) {
819 $out->addHTML( "<div id='mw-imagepage-section-duplicates'>\n" );
820 $out->addWikiMsg( 'duplicatesoffile',
821 $this->getContext()->getLanguage()->formatNum( count( $dupes ) ), $this->getTitle()->getDBkey()
823 $out->addHTML( "<ul class='mw-imagepage-duplicates'>\n" );
828 foreach ( $dupes as $file ) {
830 if ( $file->isLocal() ) {
831 $link = Linker
::linkKnown( $file->getTitle() );
833 $link = Linker
::makeExternalLink( $file->getDescriptionUrl(),
834 $file->getTitle()->getPrefixedText() );
835 $fromSrc = wfMessage( 'shared-repo-from', $file->getRepo()->getDisplayName() )->text();
837 $out->addHTML( "<li>{$link} {$fromSrc}</li>\n" );
839 $out->addHTML( "</ul></div>\n" );
843 * Delete the file, or an earlier version of it
845 public function delete() {
846 $file = $this->mPage
->getFile();
847 if ( !$file->exists() ||
!$file->isLocal() ||
$file->getRedirected() ) {
848 // Standard article deletion
853 $deleter = new FileDeleteForm( $file );
858 * Display an error with a wikitext description
860 * @param $description String
862 function showError( $description ) {
863 $out = $this->getContext()->getOutput();
864 $out->setPageTitle( wfMessage( 'internalerror' ) );
865 $out->setRobotPolicy( 'noindex,nofollow' );
866 $out->setArticleRelated( false );
867 $out->enableClientCache( false );
868 $out->addWikiText( $description );
872 * Callback for usort() to do link sorts by (namespace, title)
873 * Function copied from Title::compare()
875 * @param $a object page to compare with
876 * @param $b object page to compare with
877 * @return Integer: result of string comparison, or namespace comparison
879 protected function compare( $a, $b ) {
880 if ( $a->page_namespace
== $b->page_namespace
) {
881 return strcmp( $a->page_title
, $b->page_title
);
883 return $a->page_namespace
- $b->page_namespace
;
889 * Builds the image revision log shown on image pages
893 class ImageHistoryList
extends ContextSource
{
908 protected $imagePage;
915 protected $repo, $showThumb;
916 protected $preventClickjacking = false;
919 * @param ImagePage $imagePage
921 public function __construct( $imagePage ) {
922 global $wgShowArchiveThumbnails;
923 $this->current
= $imagePage->getFile();
924 $this->img
= $imagePage->getDisplayedFile();
925 $this->title
= $imagePage->getTitle();
926 $this->imagePage
= $imagePage;
927 $this->showThumb
= $wgShowArchiveThumbnails && $this->img
->canRender();
928 $this->setContext( $imagePage->getContext() );
934 public function getImagePage() {
935 return $this->imagePage
;
941 public function getFile() {
946 * @param $navLinks string
949 public function beginImageHistoryList( $navLinks = '' ) {
950 return Xml
::element( 'h2', array( 'id' => 'filehistory' ), $this->msg( 'filehist' )->text() ) . "\n"
951 . "<div id=\"mw-imagepage-section-filehistory\">\n"
952 . $this->msg( 'filehist-help' )->parseAsBlock()
954 . Xml
::openElement( 'table', array( 'class' => 'wikitable filehistory' ) ) . "\n"
956 . ( $this->current
->isLocal() && ( $this->getUser()->isAllowedAny( 'delete', 'deletedhistory' ) ) ?
'<td></td>' : '' )
957 . '<th>' . $this->msg( 'filehist-datetime' )->escaped() . '</th>'
958 . ( $this->showThumb ?
'<th>' . $this->msg( 'filehist-thumb' )->escaped() . '</th>' : '' )
959 . '<th>' . $this->msg( 'filehist-dimensions' )->escaped() . '</th>'
960 . '<th>' . $this->msg( 'filehist-user' )->escaped() . '</th>'
961 . '<th>' . $this->msg( 'filehist-comment' )->escaped() . '</th>'
966 * @param $navLinks string
969 public function endImageHistoryList( $navLinks = '' ) {
970 return "</table>\n$navLinks\n</div>\n";
978 public function imageHistoryLine( $iscur, $file ) {
981 $user = $this->getUser();
982 $lang = $this->getLanguage();
983 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
984 $img = $iscur ?
$file->getName() : $file->getArchiveName();
985 $userId = $file->getUser( 'id' );
986 $userText = $file->getUser( 'text' );
987 $description = $file->getDescription( File
::FOR_THIS_USER
, $user );
989 $local = $this->current
->isLocal();
990 $row = $selected = '';
993 if ( $local && ( $user->isAllowedAny( 'delete', 'deletedhistory' ) ) ) {
995 # Link to remove from history
996 if ( $user->isAllowed( 'delete' ) ) {
997 $q = array( 'action' => 'delete' );
999 $q['oldimage'] = $img;
1001 $row .= Linker
::linkKnown(
1003 $this->msg( $iscur ?
'filehist-deleteall' : 'filehist-deleteone' )->escaped(),
1007 # Link to hide content. Don't show useless link to people who cannot hide revisions.
1008 $canHide = $user->isAllowed( 'deleterevision' );
1009 if ( $canHide ||
( $user->isAllowed( 'deletedhistory' ) && $file->getVisibility() ) ) {
1010 if ( $user->isAllowed( 'delete' ) ) {
1013 // If file is top revision or locked from this user, don't link
1014 if ( $iscur ||
!$file->userCan( File
::DELETED_RESTRICTED
, $user ) ) {
1015 $del = Linker
::revDeleteLinkDisabled( $canHide );
1017 list( $ts, ) = explode( '!', $img, 2 );
1019 'type' => 'oldimage',
1020 'target' => $this->title
->getPrefixedText(),
1023 $del = Linker
::revDeleteLink( $query,
1024 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1031 // Reversion link/current indicator
1034 $row .= $this->msg( 'filehist-current' )->escaped();
1035 } elseif ( $local && $this->title
->quickUserCan( 'edit', $user )
1036 && $this->title
->quickUserCan( 'upload', $user )
1038 if ( $file->isDeleted( File
::DELETED_FILE
) ) {
1039 $row .= $this->msg( 'filehist-revert' )->escaped();
1041 $row .= Linker
::linkKnown(
1043 $this->msg( 'filehist-revert' )->escaped(),
1046 'action' => 'revert',
1048 'wpEditToken' => $user->getEditToken( $img )
1055 // Date/time and image link
1056 if ( $file->getTimestamp() === $this->img
->getTimestamp() ) {
1057 $selected = "class='filehistory-selected'";
1059 $row .= "<td $selected style='white-space: nowrap;'>";
1060 if ( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
1061 # Don't link to unviewable files
1062 $row .= '<span class="history-deleted">' . $lang->userTimeAndDate( $timestamp, $user ) . '</span>';
1063 } elseif ( $file->isDeleted( File
::DELETED_FILE
) ) {
1065 $this->preventClickjacking();
1066 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
1067 # Make a link to review the image
1068 $url = Linker
::linkKnown(
1070 $lang->userTimeAndDate( $timestamp, $user ),
1073 'target' => $this->title
->getPrefixedText(),
1075 'token' => $user->getEditToken( $img )
1079 $url = $lang->userTimeAndDate( $timestamp, $user );
1081 $row .= '<span class="history-deleted">' . $url . '</span>';
1083 $url = $iscur ?
$this->current
->getUrl() : $this->current
->getArchiveUrl( $img );
1084 $row .= Xml
::element( 'a', array( 'href' => $url ), $lang->userTimeAndDate( $timestamp, $user ) );
1089 if ( $this->showThumb
) {
1090 $row .= '<td>' . $this->getThumbForLine( $file ) . '</td>';
1093 // Image dimensions + size
1095 $row .= htmlspecialchars( $file->getDimensionsString() );
1096 $row .= $this->msg( 'word-separator' )->plain();
1097 $row .= '<span style="white-space: nowrap;">';
1098 $row .= $this->msg( 'parentheses' )->rawParams( Linker
::formatSize( $file->getSize() ) )->plain();
1104 // Hide deleted usernames
1105 if ( $file->isDeleted( File
::DELETED_USER
) ) {
1106 $row .= '<span class="history-deleted">' . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
1109 $row .= Linker
::userLink( $userId, $userText );
1110 $row .= $this->msg( 'word-separator' )->plain();
1111 $row .= '<span style="white-space: nowrap;">';
1112 $row .= Linker
::userToolLinks( $userId, $userText );
1115 $row .= htmlspecialchars( $userText );
1120 // Don't show deleted descriptions
1121 if ( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1122 $row .= '<td><span class="history-deleted">' . $this->msg( 'rev-deleted-comment' )->escaped() . '</span></td>';
1124 $row .= '<td dir="' . $wgContLang->getDir() . '">' . Linker
::formatComment( $description, $this->title
) . '</td>';
1128 wfRunHooks( 'ImagePageFileHistoryLine', array( $this, $file, &$row, &$rowClass ) );
1129 $classAttr = $rowClass ?
" class='$rowClass'" : '';
1131 return "<tr{$classAttr}>{$row}</tr>\n";
1138 protected function getThumbForLine( $file ) {
1139 $lang = $this->getLanguage();
1140 $user = $this->getUser();
1141 if ( $file->allowInlineDisplay() && $file->userCan( File
::DELETED_FILE
,$user )
1142 && !$file->isDeleted( File
::DELETED_FILE
) )
1148 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
1150 $thumbnail = $file->transform( $params );
1152 'alt' => $this->msg( 'filehist-thumbtext',
1153 $lang->userTimeAndDate( $timestamp, $user ),
1154 $lang->userDate( $timestamp, $user ),
1155 $lang->userTime( $timestamp, $user ) )->text(),
1156 'file-link' => true,
1159 if ( !$thumbnail ) {
1160 return $this->msg( 'filehist-nothumb' )->escaped();
1163 return $thumbnail->toHtml( $options );
1165 return $this->msg( 'filehist-nothumb' )->escaped();
1170 * @param $enable bool
1172 protected function preventClickjacking( $enable = true ) {
1173 $this->preventClickjacking
= $enable;
1179 public function getPreventClickjacking() {
1180 return $this->preventClickjacking
;
1184 class ImageHistoryPseudoPager
extends ReverseChronologicalPager
{
1185 protected $preventClickjacking = false;
1198 * @param ImagePage $imagePage
1200 function __construct( $imagePage ) {
1201 parent
::__construct();
1202 $this->mImagePage
= $imagePage;
1203 $this->mTitle
= clone ( $imagePage->getTitle() );
1204 $this->mTitle
->setFragment( '#filehistory' );
1206 $this->mHist
= array();
1207 $this->mRange
= array( 0, 0 ); // display range
1213 function getTitle() {
1214 return $this->mTitle
;
1217 function getQueryInfo() {
1224 function getIndexField() {
1229 * @param $row object
1232 function formatRow( $row ) {
1239 function getBody() {
1242 if ( count( $this->mHist
) ) {
1243 $list = new ImageHistoryList( $this->mImagePage
);
1244 # Generate prev/next links
1245 $navLink = $this->getNavigationBar();
1246 $s = $list->beginImageHistoryList( $navLink );
1247 // Skip rows there just for paging links
1248 for ( $i = $this->mRange
[0]; $i <= $this->mRange
[1]; $i++
) {
1249 $file = $this->mHist
[$i];
1250 $s .= $list->imageHistoryLine( !$file->isOld(), $file );
1252 $s .= $list->endImageHistoryList( $navLink );
1254 if ( $list->getPreventClickjacking() ) {
1255 $this->preventClickjacking();
1261 function doQuery() {
1262 if ( $this->mQueryDone
) {
1265 $this->mImg
= $this->mImagePage
->getFile(); // ensure loading
1266 if ( !$this->mImg
->exists() ) {
1269 $queryLimit = $this->mLimit +
1; // limit plus extra row
1270 if ( $this->mIsBackwards
) {
1271 // Fetch the file history
1272 $this->mHist
= $this->mImg
->getHistory( $queryLimit, null, $this->mOffset
, false );
1273 // The current rev may not meet the offset/limit
1274 $numRows = count( $this->mHist
);
1275 if ( $numRows <= $this->mLimit
&& $this->mImg
->getTimestamp() > $this->mOffset
) {
1276 $this->mHist
= array_merge( array( $this->mImg
), $this->mHist
);
1279 // The current rev may not meet the offset
1280 if ( !$this->mOffset ||
$this->mImg
->getTimestamp() < $this->mOffset
) {
1281 $this->mHist
[] = $this->mImg
;
1283 // Old image versions (fetch extra row for nav links)
1284 $oiLimit = count( $this->mHist
) ?
$this->mLimit
: $this->mLimit +
1;
1285 // Fetch the file history
1286 $this->mHist
= array_merge( $this->mHist
,
1287 $this->mImg
->getHistory( $oiLimit, $this->mOffset
, null, false ) );
1289 $numRows = count( $this->mHist
); // Total number of query results
1291 # Index value of top item in the list
1292 $firstIndex = $this->mIsBackwards ?
1293 $this->mHist
[$numRows - 1]->getTimestamp() : $this->mHist
[0]->getTimestamp();
1294 # Discard the extra result row if there is one
1295 if ( $numRows > $this->mLimit
&& $numRows > 1 ) {
1296 if ( $this->mIsBackwards
) {
1297 # Index value of item past the index
1298 $this->mPastTheEndIndex
= $this->mHist
[0]->getTimestamp();
1299 # Index value of bottom item in the list
1300 $lastIndex = $this->mHist
[1]->getTimestamp();
1302 $this->mRange
= array( 1, $numRows - 1 );
1304 # Index value of item past the index
1305 $this->mPastTheEndIndex
= $this->mHist
[$numRows - 1]->getTimestamp();
1306 # Index value of bottom item in the list
1307 $lastIndex = $this->mHist
[$numRows - 2]->getTimestamp();
1309 $this->mRange
= array( 0, $numRows - 2 );
1312 # Setting indexes to an empty string means that they will be
1313 # omitted if they would otherwise appear in URLs. It just so
1314 # happens that this is the right thing to do in the standard
1315 # UI, in all the relevant cases.
1316 $this->mPastTheEndIndex
= '';
1317 # Index value of bottom item in the list
1318 $lastIndex = $this->mIsBackwards ?
1319 $this->mHist
[0]->getTimestamp() : $this->mHist
[$numRows - 1]->getTimestamp();
1321 $this->mRange
= array( 0, $numRows - 1 );
1326 $this->mPastTheEndIndex
= '';
1328 if ( $this->mIsBackwards
) {
1329 $this->mIsFirst
= ( $numRows < $queryLimit );
1330 $this->mIsLast
= ( $this->mOffset
== '' );
1331 $this->mLastShown
= $firstIndex;
1332 $this->mFirstShown
= $lastIndex;
1334 $this->mIsFirst
= ( $this->mOffset
== '' );
1335 $this->mIsLast
= ( $numRows < $queryLimit );
1336 $this->mLastShown
= $lastIndex;
1337 $this->mFirstShown
= $firstIndex;
1339 $this->mQueryDone
= true;
1343 * @param $enable bool
1345 protected function preventClickjacking( $enable = true ) {
1346 $this->preventClickjacking
= $enable;
1352 public function getPreventClickjacking() {
1353 return $this->preventClickjacking
;