4 * Special handling for image description pages
8 class ImagePage
extends Article
{
21 var $mExtraDescription = false;
24 function __construct( $title ) {
25 parent
::__construct( $title );
34 public function setFile( $file ) {
35 $this->displayImg
= $file;
37 $this->fileLoaded
= true;
40 protected function loadFile() {
41 if ( $this->fileLoaded
) {
44 $this->fileLoaded
= true;
46 $this->displayImg
= $this->img
= false;
47 wfRunHooks( 'ImagePageFindFile', array( $this, &$this->img
, &$this->displayImg
) );
49 $this->img
= wfFindFile( $this->mTitle
);
51 $this->img
= wfLocalFile( $this->mTitle
);
54 if ( !$this->displayImg
) {
55 $this->displayImg
= $this->img
;
57 $this->repo
= $this->img
->getRepo();
61 * Handler for action=render
62 * Include body text only; none of the image extras
64 public function render() {
66 $wgOut->setArticleBodyOnly( true );
70 public function view() {
71 global $wgOut, $wgShowEXIF, $wgRequest, $wgUser;
73 $diff = $wgRequest->getVal( 'diff' );
74 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
76 if ( $this->mTitle
->getNamespace() != NS_FILE ||
( isset( $diff ) && $diffOnly ) ) {
77 return parent
::view();
82 if ( $this->mTitle
->getNamespace() == NS_FILE
&& $this->img
->getRedirected() ) {
83 if ( $this->mTitle
->getDBkey() == $this->img
->getName() ||
isset( $diff ) ) {
84 // mTitle is the same as the redirect target so ask Article
85 // to perform the redirect for us.
86 $wgRequest->setVal( 'diffonly', 'true' );
87 return parent
::view();
89 // mTitle is not the same as the redirect target so it is
90 // probably the redirect page itself. Fake the redirect symbol
91 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
92 $wgOut->addHTML( $this->viewRedirect( Title
::makeTitle( NS_FILE
, $this->img
->getName() ),
93 /* $appendSubtitle */ true, /* $forceKnown */ true ) );
99 $this->showRedirectedFromHeader();
101 if ( $wgShowEXIF && $this->displayImg
->exists() ) {
102 // @todo FIXME: Bad interface, see note on MediaHandler::formatMetadata().
103 $formattedMetadata = $this->displayImg
->formatMetadata();
104 $showmeta = $formattedMetadata !== false;
109 if ( !$diff && $this->displayImg
->exists() ) {
110 $wgOut->addHTML( $this->showTOC( $showmeta ) );
114 $this->openShowImage();
117 # No need to display noarticletext, we use our own message, output in openShowImage()
118 if ( $this->getID() ) {
119 # When $wgBetterDirectionality is enabled, NS_FILE is in the user language,
120 # but this section (the actual wikitext) should be in page content language
121 $pageLang = $this->mTitle
->getPageLanguage();
122 $wgOut->addHTML( Xml
::openElement( 'div', array( 'id' => 'mw-imagepage-content',
123 'lang' => $pageLang->getCode(), 'dir' => $pageLang->getDir(),
124 'class' => 'mw-content-'.$pageLang->getDir() ) ) );
126 $wgOut->addHTML( Xml
::closeElement( 'div' ) );
128 # Just need to set the right headers
129 $wgOut->setArticleFlag( true );
130 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
131 $this->viewUpdates();
134 # Show shared description, if needed
135 if ( $this->mExtraDescription
) {
136 $fol = wfMessage( 'shareddescriptionfollows' );
137 if ( !$fol->isDisabled() ) {
138 $wgOut->addWikiText( $fol->plain() );
140 $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription
. "</div>\n" );
143 $this->closeShowImage();
144 $this->imageHistory();
145 // TODO: Cleanup the following
147 $wgOut->addHTML( Xml
::element( 'h2',
148 array( 'id' => 'filelinks' ),
149 wfMsg( 'imagelinks' ) ) . "\n" );
151 # @todo FIXME: For some freaky reason, we can't redirect to foreign images.
152 # Yet we return metadata about the target. Definitely an issue in the FileRepo
155 # Allow extensions to add something after the image links
157 wfRunHooks( 'ImagePageAfterImageLinks', array( $this, &$html ) );
159 $wgOut->addHTML( $html );
163 $wgOut->addHTML( Xml
::element( 'h2', array( 'id' => 'metadata' ), wfMsg( 'metadata' ) ) . "\n" );
164 $wgOut->addWikiText( $this->makeMetadataTable( $formattedMetadata ) );
165 $wgOut->addModules( array( 'mediawiki.action.view.metadata' ) );
168 // Add remote Filepage.css
169 if( !$this->repo
->isLocal() ) {
170 $css = $this->repo
->getDescriptionStylesheetUrl();
172 $wgOut->addStyle( $css );
175 // always show the local local Filepage.css, bug 29277
176 $wgOut->addModuleStyles( 'filepage' );
179 public function getRedirectTarget() {
181 if ( $this->img
->isLocal() ) {
182 return parent
::getRedirectTarget();
184 // Foreign image page
185 $from = $this->img
->getRedirected();
186 $to = $this->img
->getName();
187 if ( $from == $to ) {
190 return $this->mRedirectTarget
= Title
::makeTitle( NS_FILE
, $to );
193 public function followRedirect() {
195 if ( $this->img
->isLocal() ) {
196 return parent
::followRedirect();
198 $from = $this->img
->getRedirected();
199 $to = $this->img
->getName();
200 if ( $from == $to ) {
203 return Title
::makeTitle( NS_FILE
, $to );
206 public function isRedirect( $text = false ) {
208 if ( $this->img
->isLocal() ) {
209 return parent
::isRedirect( $text );
212 return (bool)$this->img
->getRedirected();
215 public function isLocal() {
217 return $this->img
->isLocal();
220 public function getFile() {
225 public function getDisplayedFile() {
227 return $this->displayImg
;
230 public function getDuplicates() {
232 if ( !is_null( $this->dupes
) ) {
235 $hash = $this->img
->getSha1();
237 return $this->dupes
= array();
239 $dupes = RepoGroup
::singleton()->findBySha1( $hash );
240 // Remove duplicates with self and non matching file sizes
241 $self = $this->img
->getRepoName() . ':' . $this->img
->getName();
242 $size = $this->img
->getSize();
243 foreach ( $dupes as $index => $file ) {
244 $key = $file->getRepoName() . ':' . $file->getName();
245 if ( $key == $self ) {
246 unset( $dupes[$index] );
248 if ( $file->getSize() != $size ) {
249 unset( $dupes[$index] );
252 return $this->dupes
= $dupes;
258 * @param $metadata Boolean: whether or not to show the metadata link
261 protected function showTOC( $metadata ) {
263 '<li><a href="#file">' . wfMsgHtml( 'file-anchor-link' ) . '</a></li>',
264 '<li><a href="#filehistory">' . wfMsgHtml( 'filehist' ) . '</a></li>',
265 '<li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>',
268 $r[] = '<li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>';
271 wfRunHooks( 'ImagePageShowTOC', array( $this, &$r ) );
273 return '<ul id="filetoc">' . implode( "\n", $r ) . '</ul>';
277 * Make a table with metadata to be shown in the output page.
279 * @todo FIXME: Bad interface, see note on MediaHandler::formatMetadata().
281 * @param $metadata Array: the array containing the EXIF data
282 * @return String The metadata table. This is treated as Wikitext (!)
284 protected function makeMetadataTable( $metadata ) {
285 $r = "<div class=\"mw-imagepage-section-metadata\">";
286 $r .= wfMsgNoTrans( 'metadata-help' );
287 $r .= "<table id=\"mw_metadata\" class=\"mw_metadata\">\n";
288 foreach ( $metadata as $type => $stuff ) {
289 foreach ( $stuff as $v ) {
290 # @todo FIXME: Why is this using escapeId for a class?!
291 $class = Sanitizer
::escapeId( $v['id'] );
292 if ( $type == 'collapsed' ) {
293 $class .= ' collapsable';
295 $r .= "<tr class=\"$class\">\n";
296 $r .= "<th>{$v['name']}</th>\n";
297 $r .= "<td>{$v['value']}</td>\n</tr>";
300 $r .= "</table>\n</div>\n";
305 * Overloading Article's getContent method.
307 * Omit noarticletext if sharedupload; text will be fetched from the
308 * shared upload server if possible.
310 public function getContent() {
312 if ( $this->img
&& !$this->img
->isLocal() && 0 == $this->getID() ) {
315 return parent
::getContent();
318 protected function openShowImage() {
319 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
320 $wgLang, $wgEnableUploads;
324 $sizeSel = intval( $wgUser->getOption( 'imagesize' ) );
325 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
326 $sizeSel = User
::getDefaultOption( 'imagesize' );
328 // The user offset might still be incorrect, specially if
329 // $wgImageLimits got changed (see bug #8858).
330 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
331 // Default to the first offset in $wgImageLimits
335 $max = $wgImageLimits[$sizeSel];
337 $maxHeight = $max[1];
338 $sk = $wgUser->getSkin();
339 $dirmark = wfUILang()->getDirMark();
341 if ( $this->displayImg
->exists() ) {
343 $page = $wgRequest->getIntOrNull( 'page' );
344 if ( is_null( $page ) ) {
348 $params = array( 'page' => $page );
350 $width_orig = $this->displayImg
->getWidth( $page );
351 $width = $width_orig;
352 $height_orig = $this->displayImg
->getHeight( $page );
353 $height = $height_orig;
355 $longDesc = wfMsg( 'parentheses', $this->displayImg
->getLongDesc() );
357 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this, &$wgOut ) );
359 if ( $this->displayImg
->allowInlineDisplay() ) {
362 # "Download high res version" link below the image
363 # $msgsize = wfMsgHtml( 'file-info-size', $width_orig, $height_orig, $sk->formatSize( $this->displayImg->getSize() ), $mime );
364 # We'll show a thumbnail of this image
365 if ( $width > $maxWidth ||
$height > $maxHeight ) {
366 # Calculate the thumbnail size.
367 # First case, the limiting factor is the width, not the height.
368 if ( $width / $height >= $maxWidth / $maxHeight ) {
369 $height = round( $height * $maxWidth / $width );
371 # Note that $height <= $maxHeight now.
373 $newwidth = floor( $width * $maxHeight / $height );
374 $height = round( $height * $newwidth / $width );
376 # Note that $height <= $maxHeight now, but might not be identical
377 # because of rounding.
379 $msgbig = wfMsgHtml( 'show-big-image' );
380 $otherSizes = array();
381 foreach ( $wgImageLimits as $size ) {
382 if ( $size[0] < $width_orig && $size[1] < $height_orig &&
383 $size[0] != $width && $size[1] != $height ) {
384 $otherSizes[] = $this->makeSizeLink( $params, $size[0], $size[1] );
387 $msgsmall = wfMessage( 'show-big-image-preview' )->
388 rawParams( $this->makeSizeLink( $params, $width, $height ) )->
390 wfMessage( 'show-big-image-other' )->
391 rawParams( $wgLang->pipeList( $otherSizes ) )->parse();
393 # Image is small enough to show full size on image page
394 $msgsmall = wfMsgExt( 'file-nohires', array( 'parseinline' ) );
397 $params['width'] = $width;
398 $params['height'] = $height;
399 $thumbnail = $this->displayImg
->transform( $params );
402 $anchorclose = '<br />' . $msgsmall;
404 $isMulti = $this->displayImg
->isMultipage() && $this->displayImg
->pageCount() > 1;
406 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
411 'alt' => $this->displayImg
->getTitle()->getPrefixedText(),
414 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
415 $thumbnail->toHtml( $options ) .
416 $anchorclose . "</div>\n" );
420 $count = $this->displayImg
->pageCount();
423 $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
428 array( 'page' => $page - 1 ),
429 array( 'known', 'noclasses' )
431 $thumb1 = $sk->makeThumbLinkObj( $this->mTitle
, $this->displayImg
, $link, $label, 'none',
432 array( 'page' => $page - 1 ) );
437 if ( $page < $count ) {
438 $label = wfMsg( 'imgmultipagenext' );
443 array( 'page' => $page +
1 ),
444 array( 'known', 'noclasses' )
446 $thumb2 = $sk->makeThumbLinkObj( $this->mTitle
, $this->displayImg
, $link, $label, 'none',
447 array( 'page' => $page +
1 ) );
455 'name' => 'pageselector',
456 'action' => $wgScript,
457 'onchange' => 'document.pageselector.submit();',
460 for ( $i = 1; $i <= $count; $i++
) {
461 $options[] = Xml
::option( $wgLang->formatNum( $i ), $i, $i == $page );
463 $select = Xml
::tags( 'select',
464 array( 'id' => 'pageselector', 'name' => 'page' ),
465 implode( "\n", $options ) );
468 '</td><td><div class="multipageimagenavbox">' .
469 Xml
::openElement( 'form', $formParams ) .
470 Html
::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
471 wfMsgExt( 'imgmultigoto', array( 'parseinline', 'replaceafter' ), $select ) .
472 Xml
::submitButton( wfMsg( 'imgmultigo' ) ) .
473 Xml
::closeElement( 'form' ) .
474 "<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>"
478 # if direct link is allowed but it's not a renderable image, show an icon.
479 if ( $this->displayImg
->isSafeFile() ) {
480 $icon = $this->displayImg
->iconThumb();
482 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
483 $icon->toHtml( array( 'file-link' => true ) ) .
491 $filename = wfEscapeWikiText( $this->displayImg
->getName() );
492 $linktext = $filename;
493 if ( isset( $msgbig ) ) {
494 $linktext = wfEscapeWikiText( $msgbig );
496 $medialink = "[[Media:$filename|$linktext]]";
498 if ( !$this->displayImg
->isSafeFile() ) {
499 $warning = wfMsgNoTrans( 'mediawarning' );
500 $wgOut->addWikiText( <<<EOT
501 <div class="fullMedia"><span class="dangerousLink">{$medialink}</span>$dirmark <span class="fileInfo">$longDesc</span></div>
502 <div class="mediaWarning">$warning</div>
506 $wgOut->addWikiText( <<<EOT
507 <div class="fullMedia">{$medialink}{$dirmark} <span class="fileInfo">$longDesc</span>
514 if ( !$this->displayImg
->isLocal() ) {
515 $this->printSharedImageText();
518 # Image does not exist
519 if ( $wgEnableUploads && $wgUser->isAllowed( 'upload' ) ) {
520 // Only show an upload link if the user can upload
521 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
523 'filepage-nofile-link',
524 $uploadTitle->getFullURL( array( 'wpDestFile' => $this->img
->getName() ) )
527 $nofile = 'filepage-nofile';
529 // Note, if there is an image description page, but
530 // no image, then this setRobotPolicy is overriden
531 // by Article::View().
532 $wgOut->setRobotPolicy( 'noindex,nofollow' );
533 $wgOut->wrapWikiMsg( "<div id='mw-imagepage-nofile' class='plainlinks'>\n$1\n</div>", $nofile );
534 if ( !$this->getID() ) {
535 // If there is no image, no shared image, and no description page,
536 // output a 404, to be consistent with articles.
537 $wgRequest->response()->header( 'HTTP/1.1 404 Not Found' );
543 * Creates an thumbnail of specified size and returns an HTML link to it
544 * @param array $params Scaler parameters
548 private function makeSizeLink( $params, $width, $height ) {
549 $params['width'] = $width;
550 $params['height'] = $height;
551 $thumbnail = $this->displayImg
->transform( $params );
552 if ( $thumbnail && !$thumbnail->isError() ) {
553 return Html
::rawElement( 'a', array(
554 'href' => $thumbnail->getUrl(),
555 'class' => 'mw-thumbnail-link'
556 ), wfMessage( 'show-big-image-size' )->numParams(
557 $thumbnail->getWidth(), $thumbnail->getHeight()
565 * Show a notice that the file is from a shared repository
567 protected function printSharedImageText() {
572 $descUrl = $this->img
->getDescriptionUrl();
573 $descText = $this->img
->getDescriptionText();
575 /* Add canonical to head if there is no local page for this shared file */
576 if( $descUrl && $this->getID() == 0 ) {
577 $wgOut->addLink( array( 'rel' => 'canonical', 'href' => $descUrl ) );
580 $wrap = "<div class=\"sharedUploadNotice\">\n$1\n</div>\n";
581 $repo = $this->img
->getRepo()->getDisplayName();
583 if ( $descUrl && $descText && wfMsgNoTrans( 'sharedupload-desc-here' ) !== '-' ) {
584 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-here', $repo, $descUrl ) );
585 } elseif ( $descUrl && wfMsgNoTrans( 'sharedupload-desc-there' ) !== '-' ) {
586 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-there', $repo, $descUrl ) );
588 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload', $repo ), ''/*BACKCOMPAT*/ );
592 $this->mExtraDescription
= $descText;
596 public function getUploadUrl() {
598 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
599 return $uploadTitle->getFullURL( array(
600 'wpDestFile' => $this->img
->getName(),
606 * Print out the various links at the bottom of the image page, e.g. reupload,
607 * external editing (and instructions link) etc.
609 protected function uploadLinksBox() {
610 global $wgUser, $wgOut, $wgEnableUploads, $wgUseExternalEditor;
612 if ( !$wgEnableUploads ) {
617 if ( !$this->img
->isLocal() ) {
621 $sk = $wgUser->getSkin();
623 $wgOut->addHTML( "<br /><ul>\n" );
625 # "Upload a new version of this file" link
626 if ( UploadBase
::userCanReUpload( $wgUser, $this->img
->name
) ) {
627 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
628 $wgOut->addHTML( "<li id=\"mw-imagepage-reupload-link\"><div class=\"plainlinks\">{$ulink}</div></li>\n" );
631 # External editing link
632 if ( $wgUseExternalEditor ) {
635 wfMsgHtml( 'edit-externally' ),
639 'externaledit' => 'true',
642 array( 'known', 'noclasses' )
645 '<li id="mw-imagepage-edit-external">' . $elink . ' <small>' .
646 wfMsgExt( 'edit-externally-help', array( 'parseinline' ) ) .
651 $wgOut->addHTML( "</ul>\n" );
654 protected function closeShowImage() { } # For overloading
657 * If the page we've just displayed is in the "Image" namespace,
658 * we follow it with an upload history of the image and its usage.
660 protected function imageHistory() {
664 $pager = new ImageHistoryPseudoPager( $this );
665 $wgOut->addHTML( $pager->getBody() );
666 $wgOut->preventClickjacking( $pager->getPreventClickjacking() );
668 $this->img
->resetHistory(); // free db resources
670 # Exist check because we don't want to show this on pages where an image
671 # doesn't exist along with the noimage message, that would suck. -ævar
672 if ( $this->img
->exists() ) {
673 $this->uploadLinksBox();
677 protected function queryImageLinks( $target, $limit ) {
678 $dbr = wfGetDB( DB_SLAVE
);
681 array( 'imagelinks', 'page' ),
682 array( 'page_namespace', 'page_title', 'page_is_redirect', 'il_to' ),
683 array( 'il_to' => $target, 'il_from = page_id' ),
685 array( 'LIMIT' => $limit +
1 )
689 protected function imageLinks() {
690 global $wgUser, $wgOut, $wgLang;
694 $res = $this->queryImageLinks( $this->mTitle
->getDbKey(), $limit +
1);
696 $redirects = array();
697 foreach ( $res as $row ) {
698 if ( $row->page_is_redirect
) {
699 $redirects[$row->page_title
] = array();
703 $count = count( $rows );
705 $hasMore = $count > $limit;
706 if ( !$hasMore && count( $redirects ) ) {
707 $res = $this->queryImageLinks( array_keys( $redirects ),
708 $limit - count( $rows ) +
1 );
709 foreach ( $res as $row ) {
710 $redirects[$row->il_to
][] = $row;
713 $hasMore = ( $res->numRows() +
count( $rows ) ) > $limit;
718 Html
::rawElement( 'div',
719 array( 'id' => 'mw-imagepage-nolinkstoimage' ), "\n$1\n" ),
725 $wgOut->addHTML( "<div id='mw-imagepage-section-linkstoimage'>\n" );
727 $wgOut->addWikiMsg( 'linkstoimage', $count );
729 // More links than the limit. Add a link to [[Special:Whatlinkshere]]
730 $wgOut->addWikiMsg( 'linkstoimage-more',
731 $wgLang->formatNum( $limit ),
732 $this->mTitle
->getPrefixedDBkey()
737 Html
::openElement( 'ul',
738 array( 'class' => 'mw-imagepage-linkstoimage' ) ) . "\n"
740 $sk = $wgUser->getSkin();
743 // Sort the list by namespace:title
744 usort( $rows, array( $this, 'compare' ) );
746 // Create links for every element
748 foreach( $rows as $element ) {
750 if ( $currentCount > $limit ) {
754 $link = $sk->linkKnown( Title
::makeTitle( $element->page_namespace
, $element->page_title
) );
755 if ( !isset( $redirects[$element->page_title
] ) ) {
758 $ul = "<ul class='mw-imagepage-redirectstofile'>\n";
759 foreach ( $redirects[$element->page_title
] as $row ) {
761 if ( $currentCount > $limit ) {
765 $link2 = $sk->linkKnown( Title
::makeTitle( $row->page_namespace
, $row->page_title
) );
766 $ul .= Html
::rawElement(
768 array( 'id' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace
),
773 $liContents = wfMessage( 'linkstoimage-redirect' )->rawParams(
774 $link, $ul )->parse();
776 $wgOut->addHTML( Html
::rawElement(
778 array( 'id' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace
),
784 $wgOut->addHTML( Html
::closeElement( 'ul' ) . "\n" );
787 // Add a links to [[Special:Whatlinkshere]]
788 if ( $count > $limit ) {
789 $wgOut->addWikiMsg( 'morelinkstoimage', $this->mTitle
->getPrefixedDBkey() );
791 $wgOut->addHTML( Html
::closeElement( 'div' ) . "\n" );
794 protected function imageDupes() {
795 global $wgOut, $wgUser, $wgLang;
799 $dupes = $this->getDuplicates();
800 if ( count( $dupes ) == 0 ) {
804 $wgOut->addHTML( "<div id='mw-imagepage-section-duplicates'>\n" );
805 $wgOut->addWikiMsg( 'duplicatesoffile',
806 $wgLang->formatNum( count( $dupes ) ), $this->mTitle
->getDBkey()
808 $wgOut->addHTML( "<ul class='mw-imagepage-duplicates'>\n" );
810 $sk = $wgUser->getSkin();
811 foreach ( $dupes as $file ) {
813 if ( $file->isLocal() ) {
819 array( 'known', 'noclasses' )
822 $link = $sk->makeExternalLink( $file->getDescriptionUrl(),
823 $file->getTitle()->getPrefixedText() );
824 $fromSrc = wfMsg( 'shared-repo-from', $file->getRepo()->getDisplayName() );
826 $wgOut->addHTML( "<li>{$link} {$fromSrc}</li>\n" );
828 $wgOut->addHTML( "</ul></div>\n" );
832 * Delete the file, or an earlier version of it
834 public function delete() {
835 global $wgUploadMaintenance;
836 if ( $wgUploadMaintenance && $this->mTitle
&& $this->mTitle
->getNamespace() == NS_FILE
) {
838 $wgOut->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n", array( 'filedelete-maintenance' ) );
843 if ( !$this->img
->exists() ||
!$this->img
->isLocal() ||
$this->img
->getRedirected() ) {
844 // Standard article deletion
848 $deleter = new FileDeleteForm( $this->img
);
853 * Revert the file to an earlier version
855 public function revert() {
857 $reverter = new FileRevertForm( $this->img
);
858 $reverter->execute();
862 * Override handling of action=purge
864 public function doPurge() {
866 if ( $this->img
->exists() ) {
867 wfDebug( 'ImagePage::doPurge purging ' . $this->img
->getName() . "\n" );
868 $update = new HTMLCacheUpdate( $this->mTitle
, 'imagelinks' );
870 $this->img
->upgradeRow();
871 $this->img
->purgeCache();
873 wfDebug( 'ImagePage::doPurge no image for ' . $this->img
->getName() . "; limiting purge to cache only\n" );
874 // even if the file supposedly doesn't exist, force any cached information
875 // to be updated (in case the cached information is wrong)
876 $this->img
->purgeCache();
882 * Display an error with a wikitext description
884 function showError( $description ) {
886 $wgOut->setPageTitle( wfMsg( 'internalerror' ) );
887 $wgOut->setRobotPolicy( 'noindex,nofollow' );
888 $wgOut->setArticleRelated( false );
889 $wgOut->enableClientCache( false );
890 $wgOut->addWikiText( $description );
894 * Callback for usort() to do link sorts by (namespace, title)
895 * Function copied from Title::compare()
897 * @param $a object page to compare with
898 * @param $b object page to compare with
899 * @return Integer: result of string comparison, or namespace comparison
901 protected function compare( $a, $b ) {
902 if ( $a->page_namespace
== $b->page_namespace
) {
903 return strcmp( $a->page_title
, $b->page_title
);
905 return $a->page_namespace
- $b->page_namespace
;
911 * Builds the image revision log shown on image pages
915 class ImageHistoryList
{
930 protected $imagePage;
937 protected $repo, $showThumb;
938 protected $preventClickjacking = false;
941 * @param ImagePage $imagePage
943 public function __construct( $imagePage ) {
944 global $wgUser, $wgShowArchiveThumbnails;
945 $this->skin
= $wgUser->getSkin();
946 $this->current
= $imagePage->getFile();
947 $this->img
= $imagePage->getDisplayedFile();
948 $this->title
= $imagePage->getTitle();
949 $this->imagePage
= $imagePage;
950 $this->showThumb
= $wgShowArchiveThumbnails && $this->img
->canRender();
953 public function getImagePage() {
954 return $this->imagePage
;
957 public function getSkin() {
961 public function getFile() {
965 public function beginImageHistoryList( $navLinks = '' ) {
966 global $wgOut, $wgUser;
967 return Xml
::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) ) . "\n"
968 . "<div id=\"mw-imagepage-section-filehistory\">\n"
969 . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
971 . Xml
::openElement( 'table', array( 'class' => 'wikitable filehistory' ) ) . "\n"
973 . ( $this->current
->isLocal() && ( $wgUser->isAllowedAny( 'delete', 'deletedhistory' ) ) ?
'<td></td>' : '' )
974 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
975 . ( $this->showThumb ?
'<th>' . wfMsgHtml( 'filehist-thumb' ) . '</th>' : '' )
976 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
977 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
978 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
982 public function endImageHistoryList( $navLinks = '' ) {
983 return "</table>\n$navLinks\n</div>\n";
991 public function imageHistoryLine( $iscur, $file ) {
992 global $wgUser, $wgLang;
994 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
995 $img = $iscur ?
$file->getName() : $file->getArchiveName();
996 $user = $file->getUser( 'id' );
997 $usertext = $file->getUser( 'text' );
998 $description = $file->getDescription();
1000 $local = $this->current
->isLocal();
1001 $row = $selected = '';
1004 if ( $local && ( $wgUser->isAllowedAny( 'delete', 'deletedhistory' ) ) ) {
1006 # Link to remove from history
1007 if ( $wgUser->isAllowed( 'delete' ) ) {
1008 $q = array( 'action' => 'delete' );
1010 $q['oldimage'] = $img;
1012 $row .= $this->skin
->link(
1014 wfMsgHtml( $iscur ?
'filehist-deleteall' : 'filehist-deleteone' ),
1015 array(), $q, array( 'known' )
1018 # Link to hide content. Don't show useless link to people who cannot hide revisions.
1019 $canHide = $wgUser->isAllowed( 'deleterevision' );
1020 if ( $canHide ||
( $wgUser->isAllowed( 'deletedhistory' ) && $file->getVisibility() ) ) {
1021 if ( $wgUser->isAllowed( 'delete' ) ) {
1024 // If file is top revision or locked from this user, don't link
1025 if ( $iscur ||
!$file->userCan( File
::DELETED_RESTRICTED
) ) {
1026 $del = $this->skin
->revDeleteLinkDisabled( $canHide );
1028 list( $ts, $name ) = explode( '!', $img, 2 );
1030 'type' => 'oldimage',
1031 'target' => $this->title
->getPrefixedText(),
1034 $del = $this->skin
->revDeleteLink( $query,
1035 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1042 // Reversion link/current indicator
1045 $row .= wfMsgHtml( 'filehist-current' );
1046 } elseif ( $local && $wgUser->isLoggedIn() && $this->title
->userCan( 'edit' ) ) {
1047 if ( $file->isDeleted( File
::DELETED_FILE
) ) {
1048 $row .= wfMsgHtml( 'filehist-revert' );
1050 $row .= $this->skin
->link(
1052 wfMsgHtml( 'filehist-revert' ),
1055 'action' => 'revert',
1057 'wpEditToken' => $wgUser->editToken( $img )
1059 array( 'known', 'noclasses' )
1065 // Date/time and image link
1066 if ( $file->getTimestamp() === $this->img
->getTimestamp() ) {
1067 $selected = "class='filehistory-selected'";
1069 $row .= "<td $selected style='white-space: nowrap;'>";
1070 if ( !$file->userCan( File
::DELETED_FILE
) ) {
1071 # Don't link to unviewable files
1072 $row .= '<span class="history-deleted">' . $wgLang->timeAndDate( $timestamp, true ) . '</span>';
1073 } elseif ( $file->isDeleted( File
::DELETED_FILE
) ) {
1075 $this->preventClickjacking();
1076 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
1077 # Make a link to review the image
1078 $url = $this->skin
->link(
1080 $wgLang->timeAndDate( $timestamp, true ),
1083 'target' => $this->title
->getPrefixedText(),
1085 'token' => $wgUser->editToken( $img )
1087 array( 'known', 'noclasses' )
1090 $url = $wgLang->timeAndDate( $timestamp, true );
1092 $row .= '<span class="history-deleted">' . $url . '</span>';
1094 $url = $iscur ?
$this->current
->getUrl() : $this->current
->getArchiveUrl( $img );
1095 $row .= Xml
::element( 'a', array( 'href' => $url ), $wgLang->timeAndDate( $timestamp, true ) );
1100 if ( $this->showThumb
) {
1101 $row .= '<td>' . $this->getThumbForLine( $file ) . '</td>';
1104 // Image dimensions + size
1106 $row .= htmlspecialchars( $file->getDimensionsString() );
1107 $row .= ' <span style="white-space: nowrap;">(' . $this->skin
->formatSize( $file->getSize() ) . ')</span>';
1112 // Hide deleted usernames
1113 if ( $file->isDeleted( File
::DELETED_USER
) ) {
1114 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1117 $row .= $this->skin
->userLink( $user, $usertext ) . ' <span style="white-space: nowrap;">' .
1118 $this->skin
->userToolLinks( $user, $usertext ) . '</span>';
1120 $row .= htmlspecialchars( $usertext );
1123 $row .= '</td><td>';
1125 // Don't show deleted descriptions
1126 if ( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1127 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span>';
1129 $row .= $this->skin
->commentBlock( $description, $this->title
);
1134 wfRunHooks( 'ImagePageFileHistoryLine', array( $this, $file, &$row, &$rowClass ) );
1135 $classAttr = $rowClass ?
" class='$rowClass'" : '';
1137 return "<tr{$classAttr}>{$row}</tr>\n";
1144 protected function getThumbForLine( $file ) {
1147 if ( $file->allowInlineDisplay() && $file->userCan( File
::DELETED_FILE
) && !$file->isDeleted( File
::DELETED_FILE
) ) {
1152 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
1154 $thumbnail = $file->transform( $params );
1156 'alt' => wfMsg( 'filehist-thumbtext',
1157 $wgLang->timeAndDate( $timestamp, true ),
1158 $wgLang->date( $timestamp, true ),
1159 $wgLang->time( $timestamp, true ) ),
1160 'file-link' => true,
1163 if ( !$thumbnail ) {
1164 return wfMsgHtml( 'filehist-nothumb' );
1167 return $thumbnail->toHtml( $options );
1169 return wfMsgHtml( 'filehist-nothumb' );
1173 protected function preventClickjacking( $enable = true ) {
1174 $this->preventClickjacking
= $enable;
1177 public function getPreventClickjacking() {
1178 return $this->preventClickjacking
;
1182 class ImageHistoryPseudoPager
extends ReverseChronologicalPager
{
1183 protected $preventClickjacking = false;
1196 * @param ImagePage $imagePage
1198 function __construct( $imagePage ) {
1199 parent
::__construct();
1200 $this->mImagePage
= $imagePage;
1201 $this->mTitle
= clone ( $imagePage->getTitle() );
1202 $this->mTitle
->setFragment( '#filehistory' );
1204 $this->mHist
= array();
1205 $this->mRange
= array( 0, 0 ); // display range
1208 function getTitle() {
1209 return $this->mTitle
;
1212 function getQueryInfo() {
1216 function getIndexField() {
1220 function formatRow( $row ) {
1224 function getBody() {
1227 if ( count( $this->mHist
) ) {
1228 $list = new ImageHistoryList( $this->mImagePage
);
1229 # Generate prev/next links
1230 $navLink = $this->getNavigationBar();
1231 $s = $list->beginImageHistoryList( $navLink );
1232 // Skip rows there just for paging links
1233 for ( $i = $this->mRange
[0]; $i <= $this->mRange
[1]; $i++
) {
1234 $file = $this->mHist
[$i];
1235 $s .= $list->imageHistoryLine( !$file->isOld(), $file );
1237 $s .= $list->endImageHistoryList( $navLink );
1239 if ( $list->getPreventClickjacking() ) {
1240 $this->preventClickjacking();
1246 function doQuery() {
1247 if ( $this->mQueryDone
) {
1250 $this->mImg
= $this->mImagePage
->getFile(); // ensure loading
1251 if ( !$this->mImg
->exists() ) {
1254 $queryLimit = $this->mLimit +
1; // limit plus extra row
1255 if ( $this->mIsBackwards
) {
1256 // Fetch the file history
1257 $this->mHist
= $this->mImg
->getHistory( $queryLimit, null, $this->mOffset
, false );
1258 // The current rev may not meet the offset/limit
1259 $numRows = count( $this->mHist
);
1260 if ( $numRows <= $this->mLimit
&& $this->mImg
->getTimestamp() > $this->mOffset
) {
1261 $this->mHist
= array_merge( array( $this->mImg
), $this->mHist
);
1264 // The current rev may not meet the offset
1265 if ( !$this->mOffset ||
$this->mImg
->getTimestamp() < $this->mOffset
) {
1266 $this->mHist
[] = $this->mImg
;
1268 // Old image versions (fetch extra row for nav links)
1269 $oiLimit = count( $this->mHist
) ?
$this->mLimit
: $this->mLimit +
1;
1270 // Fetch the file history
1271 $this->mHist
= array_merge( $this->mHist
,
1272 $this->mImg
->getHistory( $oiLimit, $this->mOffset
, null, false ) );
1274 $numRows = count( $this->mHist
); // Total number of query results
1276 # Index value of top item in the list
1277 $firstIndex = $this->mIsBackwards ?
1278 $this->mHist
[$numRows - 1]->getTimestamp() : $this->mHist
[0]->getTimestamp();
1279 # Discard the extra result row if there is one
1280 if ( $numRows > $this->mLimit
&& $numRows > 1 ) {
1281 if ( $this->mIsBackwards
) {
1282 # Index value of item past the index
1283 $this->mPastTheEndIndex
= $this->mHist
[0]->getTimestamp();
1284 # Index value of bottom item in the list
1285 $lastIndex = $this->mHist
[1]->getTimestamp();
1287 $this->mRange
= array( 1, $numRows - 1 );
1289 # Index value of item past the index
1290 $this->mPastTheEndIndex
= $this->mHist
[$numRows - 1]->getTimestamp();
1291 # Index value of bottom item in the list
1292 $lastIndex = $this->mHist
[$numRows - 2]->getTimestamp();
1294 $this->mRange
= array( 0, $numRows - 2 );
1297 # Setting indexes to an empty string means that they will be
1298 # omitted if they would otherwise appear in URLs. It just so
1299 # happens that this is the right thing to do in the standard
1300 # UI, in all the relevant cases.
1301 $this->mPastTheEndIndex
= '';
1302 # Index value of bottom item in the list
1303 $lastIndex = $this->mIsBackwards ?
1304 $this->mHist
[0]->getTimestamp() : $this->mHist
[$numRows - 1]->getTimestamp();
1306 $this->mRange
= array( 0, $numRows - 1 );
1311 $this->mPastTheEndIndex
= '';
1313 if ( $this->mIsBackwards
) {
1314 $this->mIsFirst
= ( $numRows < $queryLimit );
1315 $this->mIsLast
= ( $this->mOffset
== '' );
1316 $this->mLastShown
= $firstIndex;
1317 $this->mFirstShown
= $lastIndex;
1319 $this->mIsFirst
= ( $this->mOffset
== '' );
1320 $this->mIsLast
= ( $numRows < $queryLimit );
1321 $this->mLastShown
= $lastIndex;
1322 $this->mFirstShown
= $firstIndex;
1324 $this->mQueryDone
= true;
1327 protected function preventClickjacking( $enable = true ) {
1328 $this->preventClickjacking
= $enable;
1331 public function getPreventClickjacking() {
1332 return $this->preventClickjacking
;