3 if ( !defined( 'MEDIAWIKI' ) )
7 * Special handling for image description pages
11 class ImagePage
extends Article
{
13 /* private */ var $img; // Image object
14 /* private */ var $displayImg;
15 /* private */ var $repo;
16 /* private */ var $fileLoaded;
17 var $mExtraDescription = false;
20 function __construct( $title ) {
21 parent
::__construct( $title );
26 public function setFile( $file ) {
27 $this->displayImg
= $file;
29 $this->fileLoaded
= true;
32 protected function loadFile() {
33 if ( $this->fileLoaded
) {
36 $this->fileLoaded
= true;
38 $this->displayImg
= $this->img
= false;
39 wfRunHooks( 'ImagePageFindFile', array( $this, &$this->img
, &$this->displayImg
) );
41 $this->img
= wfFindFile( $this->mTitle
);
43 $this->img
= wfLocalFile( $this->mTitle
);
46 if ( !$this->displayImg
) {
47 $this->displayImg
= $this->img
;
49 $this->repo
= $this->img
->getRepo();
53 * Handler for action=render
54 * Include body text only; none of the image extras
56 public function render() {
58 $wgOut->setArticleBodyOnly( true );
62 public function view() {
63 global $wgOut, $wgShowEXIF, $wgRequest, $wgUser;
65 $diff = $wgRequest->getVal( 'diff' );
66 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
68 if ( $this->mTitle
->getNamespace() != NS_FILE ||
( isset( $diff ) && $diffOnly ) ) {
69 return Article
::view();
74 if ( $this->mTitle
->getNamespace() == NS_FILE
&& $this->img
->getRedirected() ) {
75 if ( $this->mTitle
->getDBkey() == $this->img
->getName() ||
isset( $diff ) ) {
76 // mTitle is the same as the redirect target so ask Article
77 // to perform the redirect for us.
78 $wgRequest->setVal( 'diffonly', 'true' );
79 return Article
::view();
81 // mTitle is not the same as the redirect target so it is
82 // probably the redirect page itself. Fake the redirect symbol
83 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
84 $wgOut->addHTML( $this->viewRedirect( Title
::makeTitle( NS_FILE
, $this->img
->getName() ),
85 /* $appendSubtitle */ true, /* $forceKnown */ true ) );
91 $this->showRedirectedFromHeader();
93 if ( $wgShowEXIF && $this->displayImg
->exists() ) {
94 // FIXME: bad interface, see note on MediaHandler::formatMetadata().
95 $formattedMetadata = $this->displayImg
->formatMetadata();
96 $showmeta = $formattedMetadata !== false;
101 if ( !$diff && $this->displayImg
->exists() )
102 $wgOut->addHTML( $this->showTOC( $showmeta ) );
105 $this->openShowImage();
107 # No need to display noarticletext, we use our own message, output in openShowImage()
108 if ( $this->getID() ) {
111 # Just need to set the right headers
112 $wgOut->setArticleFlag( true );
113 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
114 $this->viewUpdates();
117 # Show shared description, if needed
118 if ( $this->mExtraDescription
) {
119 $fol = wfMsgNoTrans( 'shareddescriptionfollows' );
120 if ( $fol != '-' && !wfEmptyMsg( 'shareddescriptionfollows', $fol ) ) {
121 $wgOut->addWikiText( $fol );
123 $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription
. "</div>\n" );
126 $this->closeShowImage();
127 $this->imageHistory();
128 // TODO: Cleanup the following
130 $wgOut->addHTML( Xml
::element( 'h2',
131 array( 'id' => 'filelinks' ),
132 wfMsg( 'imagelinks' ) ) . "\n" );
134 # TODO! FIXME! For some freaky reason, we can't redirect to foreign images.
135 # Yet we return metadata about the target. Definitely an issue in the FileRepo
136 $this->imageRedirects();
139 # Allow extensions to add something after the image links
141 wfRunHooks( 'ImagePageAfterImageLinks', array( $this, &$html ) );
143 $wgOut->addHTML( $html );
146 global $wgStylePath, $wgStyleVersion;
147 $expand = htmlspecialchars( Xml
::escapeJsString( wfMsg( 'metadata-expand' ) ) );
148 $collapse = htmlspecialchars( Xml
::escapeJsString( wfMsg( 'metadata-collapse' ) ) );
149 $wgOut->addHTML( Xml
::element( 'h2', array( 'id' => 'metadata' ), wfMsg( 'metadata' ) ) . "\n" );
150 $wgOut->addWikiText( $this->makeMetadataTable( $formattedMetadata ) );
151 $wgOut->addScriptFile( 'metadata.js' );
153 "<script type=\"text/javascript\">attachMetadataToggle('mw_metadata', '$expand', '$collapse');</script>\n" );
156 $css = $this->repo
->getDescriptionStylesheetUrl();
158 $wgOut->addStyle( $css );
162 public function getRedirectTarget() {
164 if ( $this->img
->isLocal() ) {
165 return parent
::getRedirectTarget();
167 // Foreign image page
168 $from = $this->img
->getRedirected();
169 $to = $this->img
->getName();
170 if ( $from == $to ) {
173 return $this->mRedirectTarget
= Title
::makeTitle( NS_FILE
, $to );
175 public function followRedirect() {
177 if ( $this->img
->isLocal() ) {
178 return parent
::followRedirect();
180 $from = $this->img
->getRedirected();
181 $to = $this->img
->getName();
182 if ( $from == $to ) {
185 return Title
::makeTitle( NS_FILE
, $to );
187 public function isRedirect( $text = false ) {
189 if ( $this->img
->isLocal() )
190 return parent
::isRedirect( $text );
192 return (bool)$this->img
->getRedirected();
195 public function isLocal() {
197 return $this->img
->isLocal();
200 public function getFile() {
205 public function getDisplayedFile() {
207 return $this->displayImg
;
210 public function getDuplicates() {
212 if ( !is_null( $this->dupes
) ) {
215 if ( !( $hash = $this->img
->getSha1() ) ) {
216 return $this->dupes
= array();
218 $dupes = RepoGroup
::singleton()->findBySha1( $hash );
219 // Remove duplicates with self and non matching file sizes
220 $self = $this->img
->getRepoName() . ':' . $this->img
->getName();
221 $size = $this->img
->getSize();
222 foreach ( $dupes as $index => $file ) {
223 $key = $file->getRepoName() . ':' . $file->getName();
225 unset( $dupes[$index] );
226 if ( $file->getSize() != $size )
227 unset( $dupes[$index] );
229 return $this->dupes
= $dupes;
237 * @param $metadata Boolean: whether or not to show the metadata link
240 protected function showTOC( $metadata ) {
242 '<li><a href="#file">' . wfMsgHtml( 'file-anchor-link' ) . '</a></li>',
243 '<li><a href="#filehistory">' . wfMsgHtml( 'filehist' ) . '</a></li>',
244 '<li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>',
247 $r[] = '<li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>';
250 wfRunHooks( 'ImagePageShowTOC', array( $this, &$r ) );
252 return '<ul id="filetoc">' . implode( "\n", $r ) . '</ul>';
256 * Make a table with metadata to be shown in the output page.
258 * FIXME: bad interface, see note on MediaHandler::formatMetadata().
260 * @param $metadata Array: the array containing the EXIF data
263 protected function makeMetadataTable( $metadata ) {
264 $r = "<div class=\"mw-imagepage-section-metadata\">";
265 $r .= wfMsgNoTrans( 'metadata-help' );
266 $r .= "<table id=\"mw_metadata\" class=\"mw_metadata\">\n";
267 foreach ( $metadata as $type => $stuff ) {
268 foreach ( $stuff as $v ) {
269 # FIXME, why is this using escapeId for a class?!
270 $class = Sanitizer
::escapeId( $v['id'] );
271 if ( $type == 'collapsed' ) {
272 $class .= ' collapsable';
274 $r .= "<tr class=\"$class\">\n";
275 $r .= "<th>{$v['name']}</th>\n";
276 $r .= "<td>{$v['value']}</td>\n</tr>";
279 $r .= "</table>\n</div>\n";
284 * Overloading Article's getContent method.
286 * Omit noarticletext if sharedupload; text will be fetched from the
287 * shared upload server if possible.
289 public function getContent() {
291 if ( $this->img
&& !$this->img
->isLocal() && 0 == $this->getID() ) {
294 return Article
::getContent();
297 protected function openShowImage() {
298 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
299 $wgLang, $wgContLang, $wgEnableUploads;
303 $full_url = $this->displayImg
->getURL();
304 $linkAttribs = false;
305 $sizeSel = intval( $wgUser->getOption( 'imagesize' ) );
306 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
307 $sizeSel = User
::getDefaultOption( 'imagesize' );
309 // The user offset might still be incorrect, specially if
310 // $wgImageLimits got changed (see bug #8858).
311 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
312 // Default to the first offset in $wgImageLimits
316 $max = $wgImageLimits[$sizeSel];
318 $maxHeight = $max[1];
319 $sk = $wgUser->getSkin();
320 $dirmark = $wgContLang->getDirMark();
322 if ( $this->displayImg
->exists() ) {
324 $page = $wgRequest->getIntOrNull( 'page' );
325 if ( is_null( $page ) ) {
329 $params = array( 'page' => $page );
331 $width_orig = $this->displayImg
->getWidth();
332 $width = $width_orig;
333 $height_orig = $this->displayImg
->getHeight();
334 $height = $height_orig;
335 $mime = $this->displayImg
->getMimeType();
337 $linkAttribs = array( 'href' => $full_url );
338 $longDesc = $this->displayImg
->getLongDesc();
340 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this, &$wgOut ) );
342 if ( $this->displayImg
->allowInlineDisplay() ) {
345 # "Download high res version" link below the image
346 # $msgsize = wfMsgHtml('file-info-size', $width_orig, $height_orig, $sk->formatSize( $this->displayImg->getSize() ), $mime );
347 # We'll show a thumbnail of this image
348 if ( $width > $maxWidth ||
$height > $maxHeight ) {
349 # Calculate the thumbnail size.
350 # First case, the limiting factor is the width, not the height.
351 if ( $width / $height >= $maxWidth / $maxHeight ) {
352 $height = round( $height * $maxWidth / $width );
354 # Note that $height <= $maxHeight now.
356 $newwidth = floor( $width * $maxHeight / $height );
357 $height = round( $height * $newwidth / $width );
359 # Note that $height <= $maxHeight now, but might not be identical
360 # because of rounding.
362 $msgbig = wfMsgHtml( 'show-big-image' );
363 $msgsmall = wfMsgExt( 'show-big-image-thumb', 'parseinline',
364 $wgLang->formatNum( $width ),
365 $wgLang->formatNum( $height )
368 # Image is small enough to show full size on image page
369 $msgbig = htmlspecialchars( $this->displayImg
->getName() );
370 $msgsmall = wfMsgExt( 'file-nohires', array( 'parseinline' ) );
373 $params['width'] = $width;
374 $thumbnail = $this->displayImg
->transform( $params );
376 $anchorclose = "<br />";
377 if ( $this->displayImg
->mustRender() ) {
382 '<br />' . Xml
::tags( 'a', $linkAttribs, $msgbig ) . "$dirmark " . $longDesc;
385 $isMulti = $this->displayImg
->isMultipage() && $this->displayImg
->pageCount() > 1;
387 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
392 'alt' => $this->displayImg
->getTitle()->getPrefixedText(),
395 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
396 $thumbnail->toHtml( $options ) .
397 $anchorclose . "</div>\n" );
401 $count = $this->displayImg
->pageCount();
404 $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
409 array( 'page' => $page - 1 ),
410 array( 'known', 'noclasses' )
412 $thumb1 = $sk->makeThumbLinkObj( $this->mTitle
, $this->displayImg
, $link, $label, 'none',
413 array( 'page' => $page - 1 ) );
418 if ( $page < $count ) {
419 $label = wfMsg( 'imgmultipagenext' );
424 array( 'page' => $page +
1 ),
425 array( 'known', 'noclasses' )
427 $thumb2 = $sk->makeThumbLinkObj( $this->mTitle
, $this->displayImg
, $link, $label, 'none',
428 array( 'page' => $page +
1 ) );
436 'name' => 'pageselector',
437 'action' => $wgScript,
438 'onchange' => 'document.pageselector.submit();',
442 for ( $i = 1; $i <= $count; $i++
) {
443 $options[] = Xml
::option( $wgLang->formatNum( $i ), $i, $i == $page );
445 $select = Xml
::tags( 'select',
446 array( 'id' => 'pageselector', 'name' => 'page' ),
447 implode( "\n", $options ) );
450 '</td><td><div class="multipageimagenavbox">' .
451 Xml
::openElement( 'form', $formParams ) .
452 Xml
::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) .
453 wfMsgExt( 'imgmultigoto', array( 'parseinline', 'replaceafter' ), $select ) .
454 Xml
::submitButton( wfMsg( 'imgmultigo' ) ) .
455 Xml
::closeElement( 'form' ) .
456 "<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>"
460 # if direct link is allowed but it's not a renderable image, show an icon.
461 if ( $this->displayImg
->isSafeFile() ) {
462 $icon = $this->displayImg
->iconThumb();
464 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
465 $icon->toHtml( array( 'file-link' => true ) ) .
474 $filename = wfEscapeWikiText( $this->displayImg
->getName() );
475 $medialink = "[[Media:$filename|$filename]]";
477 if ( !$this->displayImg
->isSafeFile() ) {
478 $warning = wfMsgNoTrans( 'mediawarning' );
479 $wgOut->addWikiText( <<<EOT
480 <div class="fullMedia">
481 <span class="dangerousLink">{$medialink}</span>$dirmark
482 <span class="fileInfo">$longDesc</span>
484 <div class="mediaWarning">$warning</div>
488 $wgOut->addWikiText( <<<EOT
489 <div class="fullMedia">
490 {$medialink}{$dirmark}
491 <span class="fileInfo">$longDesc</span>
498 if ( !$this->displayImg
->isLocal() ) {
499 $this->printSharedImageText();
502 # Image does not exist
503 if ( $wgEnableUploads && $wgUser->isAllowed( 'upload' ) ) {
504 // Only show an upload link if the user can upload
505 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
507 'filepage-nofile-link',
508 $uploadTitle->getFullUrl( array( 'wpDestFile' => $this->img
->getName() ) )
513 $nofile = 'filepage-nofile';
515 $wgOut->setRobotPolicy( 'noindex,nofollow' );
516 $wgOut->wrapWikiMsg( "<div id='mw-imagepage-nofile' class='plainlinks'>\n$1\n</div>", $nofile );
521 * Show a notice that the file is from a shared repository
523 protected function printSharedImageText() {
528 $descUrl = $this->img
->getDescriptionUrl();
529 $descText = $this->img
->getDescriptionText();
531 $wrap = "<div class=\"sharedUploadNotice\">\n$1\n</div>\n";
532 $repo = $this->img
->getRepo()->getDisplayName();
535 if ( $descUrl && $descText && wfMsgNoTrans( 'sharedupload-desc-here' ) !== '-' ) {
536 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-here', $repo, $descUrl ) );
537 } elseif ( $descUrl && wfMsgNoTrans( 'sharedupload-desc-there' ) !== '-' ) {
538 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-there', $repo, $descUrl ) );
540 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload', $repo ), ''/*BACKCOMPAT*/ );
544 $this->mExtraDescription
= $descText;
548 public function getUploadUrl() {
550 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
551 return $uploadTitle->getFullUrl( array(
552 'wpDestFile' => $this->img
->getName(),
558 * Print out the various links at the bottom of the image page, e.g. reupload,
559 * external editing (and instructions link) etc.
561 protected function uploadLinksBox() {
562 global $wgUser, $wgOut, $wgEnableUploads, $wgUseExternalEditor;
564 if ( !$wgEnableUploads ) { return; }
567 if ( !$this->img
->isLocal() )
570 $sk = $wgUser->getSkin();
572 $wgOut->addHTML( "<br /><ul>\n" );
574 # "Upload a new version of this file" link
575 if ( UploadBase
::userCanReUpload( $wgUser, $this->img
->name
) ) {
576 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
577 $wgOut->addHTML( "<li id=\"mw-imagepage-reupload-link\"><div class=\"plainlinks\">{$ulink}</div></li>\n" );
580 # External editing link
581 if ( $wgUseExternalEditor ) {
584 wfMsgHtml( 'edit-externally' ),
588 'externaledit' => 'true',
591 array( 'known', 'noclasses' )
593 $wgOut->addHTML( '<li id="mw-imagepage-edit-external">' . $elink . ' <small>' . wfMsgExt( 'edit-externally-help', array( 'parseinline' ) ) . "</small></li>\n" );
596 $wgOut->addHTML( "</ul>\n" );
599 protected function closeShowImage() { } # For overloading
602 * If the page we've just displayed is in the "Image" namespace,
603 * we follow it with an upload history of the image and its usage.
605 protected function imageHistory() {
609 $pager = new ImageHistoryPseudoPager( $this );
610 $wgOut->addHTML( $pager->getBody() );
612 $this->img
->resetHistory(); // free db resources
614 # Exist check because we don't want to show this on pages where an image
615 # doesn't exist along with the noimage message, that would suck. -ævar
616 if ( $this->img
->exists() ) {
617 $this->uploadLinksBox();
621 protected function imageLinks() {
622 global $wgUser, $wgOut, $wgLang;
626 $dbr = wfGetDB( DB_SLAVE
);
629 array( 'imagelinks', 'page' ),
630 array( 'page_namespace', 'page_title' ),
631 array( 'il_to' => $this->mTitle
->getDBkey(), 'il_from = page_id' ),
633 array( 'LIMIT' => $limit +
1 )
635 $count = $dbr->numRows( $res );
637 $wgOut->wrapWikiMsg( Html
::rawElement( 'div', array ( 'id' => 'mw-imagepage-nolinkstoimage' ), "\n$1\n" ), 'nolinkstoimage' );
641 $wgOut->addHTML( "<div id='mw-imagepage-section-linkstoimage'>\n" );
642 if ( $count <= $limit - 1 ) {
643 $wgOut->addWikiMsg( 'linkstoimage', $count );
645 // More links than the limit. Add a link to [[Special:Whatlinkshere]]
646 $wgOut->addWikiMsg( 'linkstoimage-more',
647 $wgLang->formatNum( $limit ),
648 $this->mTitle
->getPrefixedDBkey()
652 $wgOut->addHTML( Html
::openElement( 'ul', array( 'class' => 'mw-imagepage-linkstoimage' ) ) . "\n" );
653 $sk = $wgUser->getSkin();
656 while ( $s = $res->fetchObject() ) {
658 if ( $count <= $limit ) {
659 // We have not yet reached the extra one that tells us there is more to fetch
664 // Sort the list by namespace:title
665 usort ( $elements, array( $this, 'compare' ) );
667 // Create links for every element
668 foreach( $elements as $element ) {
669 $link = $sk->linkKnown( Title
::makeTitle( $element->page_namespace
, $element->page_title
) );
670 $wgOut->addHTML( Html
::rawElement(
672 array( 'id' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace
),
678 $wgOut->addHTML( Html
::closeElement( 'ul' ) . "\n" );
681 // Add a links to [[Special:Whatlinkshere]]
682 if ( $count > $limit ) {
683 $wgOut->addWikiMsg( 'morelinkstoimage', $this->mTitle
->getPrefixedDBkey() );
685 $wgOut->addHTML( Html
::closeElement( 'div' ) . "\n" );
688 protected function imageRedirects() {
689 global $wgUser, $wgOut, $wgLang;
691 $redirects = $this->getTitle()->getRedirectsHere( NS_FILE
);
692 if ( count( $redirects ) == 0 ) return;
694 $wgOut->addHTML( "<div id='mw-imagepage-section-redirectstofile'>\n" );
695 $wgOut->addWikiMsg( 'redirectstofile',
696 $wgLang->formatNum( count( $redirects ) )
698 $wgOut->addHTML( "<ul class='mw-imagepage-redirectstofile'>\n" );
700 $sk = $wgUser->getSkin();
701 foreach ( $redirects as $title ) {
706 array( 'redirect' => 'no' ),
707 array( 'known', 'noclasses' )
709 $wgOut->addHTML( "<li>{$link}</li>\n" );
711 $wgOut->addHTML( "</ul></div>\n" );
715 protected function imageDupes() {
716 global $wgOut, $wgUser, $wgLang;
720 $dupes = $this->getDuplicates();
721 if ( count( $dupes ) == 0 ) return;
723 $wgOut->addHTML( "<div id='mw-imagepage-section-duplicates'>\n" );
724 $wgOut->addWikiMsg( 'duplicatesoffile',
725 $wgLang->formatNum( count( $dupes ) ), $this->mTitle
->getDBkey()
727 $wgOut->addHTML( "<ul class='mw-imagepage-duplicates'>\n" );
729 $sk = $wgUser->getSkin();
730 foreach ( $dupes as $file ) {
732 if ( $file->isLocal() ) {
738 array( 'known', 'noclasses' )
741 $link = $sk->makeExternalLink( $file->getDescriptionUrl(),
742 $file->getTitle()->getPrefixedText() );
743 $fromSrc = wfMsg( 'shared-repo-from', $file->getRepo()->getDisplayName() );
745 $wgOut->addHTML( "<li>{$link} {$fromSrc}</li>\n" );
747 $wgOut->addHTML( "</ul></div>\n" );
751 * Delete the file, or an earlier version of it
753 public function delete() {
754 global $wgUploadMaintenance;
755 if ( $wgUploadMaintenance && $this->mTitle
&& $this->mTitle
->getNamespace() == NS_FILE
) {
757 $wgOut->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n", array( 'filedelete-maintenance' ) );
762 if ( !$this->img
->exists() ||
!$this->img
->isLocal() ||
$this->img
->getRedirected() ) {
763 // Standard article deletion
767 $deleter = new FileDeleteForm( $this->img
);
772 * Revert the file to an earlier version
774 public function revert() {
776 $reverter = new FileRevertForm( $this->img
);
777 $reverter->execute();
781 * Override handling of action=purge
783 public function doPurge() {
785 if ( $this->img
->exists() ) {
786 wfDebug( "ImagePage::doPurge purging " . $this->img
->getName() . "\n" );
787 $update = new HTMLCacheUpdate( $this->mTitle
, 'imagelinks' );
789 $this->img
->upgradeRow();
790 $this->img
->purgeCache();
792 wfDebug( "ImagePage::doPurge no image for " . $this->img
->getName() . "; limiting purge to cache only\n" );
793 // even if the file supposedly doesn't exist, force any cached information
794 // to be updated (in case the cached information is wrong)
795 $this->img
->purgeCache();
801 * Display an error with a wikitext description
803 function showError( $description ) {
805 $wgOut->setPageTitle( wfMsg( "internalerror" ) );
806 $wgOut->setRobotPolicy( "noindex,nofollow" );
807 $wgOut->setArticleRelated( false );
808 $wgOut->enableClientCache( false );
809 $wgOut->addWikiText( $description );
814 * Callback for usort() to do link sorts by (namespace, title)
815 * Function copied from Title::compare()
817 * @param $a object page to compare with
818 * @param $b object page to compare with
819 * @return Integer: result of string comparison, or namespace comparison
821 protected function compare( $a, $b ) {
822 if ( $a->page_namespace
== $b->page_namespace
) {
823 return strcmp( $a->page_title
, $b->page_title
);
825 return $a->page_namespace
- $b->page_namespace
;
831 * Builds the image revision log shown on image pages
835 class ImageHistoryList
{
837 protected $imagePage, $img, $skin, $title, $repo, $showThumb;
839 public function __construct( $imagePage ) {
840 global $wgUser, $wgShowArchiveThumbnails;
841 $this->skin
= $wgUser->getSkin();
842 $this->current
= $imagePage->getFile();
843 $this->img
= $imagePage->getDisplayedFile();
844 $this->title
= $imagePage->getTitle();
845 $this->imagePage
= $imagePage;
846 $this->showThumb
= $wgShowArchiveThumbnails && $this->img
->canRender();
849 public function getImagePage() {
850 return $this->imagePage
;
853 public function getSkin() {
857 public function getFile() {
861 public function beginImageHistoryList( $navLinks = '' ) {
862 global $wgOut, $wgUser;
863 return Xml
::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) ) . "\n"
864 . "<div id=\"mw-imagepage-section-filehistory\">\n"
865 . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
867 . Xml
::openElement( 'table', array( 'class' => 'wikitable filehistory' ) ) . "\n"
869 . ( $this->current
->isLocal() && ( $wgUser->isAllowed( 'delete' ) ||
$wgUser->isAllowed( 'deletedhistory' ) ) ?
'<td></td>' : '' )
870 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
871 . ( $this->showThumb ?
'<th>' . wfMsgHtml( 'filehist-thumb' ) . '</th>' : '' )
872 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
873 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
874 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
878 public function endImageHistoryList( $navLinks = '' ) {
879 return "</table>\n$navLinks\n</div>\n";
882 public function imageHistoryLine( $iscur, $file ) {
883 global $wgUser, $wgLang, $wgContLang;
885 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
886 $img = $iscur ?
$file->getName() : $file->getArchiveName();
887 $user = $file->getUser( 'id' );
888 $usertext = $file->getUser( 'text' );
889 $description = $file->getDescription();
891 $local = $this->current
->isLocal();
892 $row = $css = $selected = '';
895 if ( $local && ( $wgUser->isAllowed( 'delete' ) ||
$wgUser->isAllowed( 'deletedhistory' ) ) ) {
897 # Link to remove from history
898 if ( $wgUser->isAllowed( 'delete' ) ) {
899 $q = array( 'action' => 'delete' );
901 $q['oldimage'] = $img;
902 $row .= $this->skin
->link(
904 wfMsgHtml( $iscur ?
'filehist-deleteall' : 'filehist-deleteone' ),
905 array(), $q, array( 'known' )
908 # Link to hide content. Don't show useless link to people who cannot hide revisions.
909 $canHide = $wgUser->isAllowed( 'deleterevision' );
910 if ( $canHide ||
( $wgUser->isAllowed( 'deletedhistory' ) && $file->getVisibility() ) ) {
911 if ( $wgUser->isAllowed( 'delete' ) ) {
914 // If file is top revision or locked from this user, don't link
915 if ( $iscur ||
!$file->userCan( File
::DELETED_RESTRICTED
) ) {
916 $del = $this->skin
->revDeleteLinkDisabled( $canHide );
918 list( $ts, $name ) = explode( '!', $img, 2 );
920 'type' => 'oldimage',
921 'target' => $this->title
->getPrefixedText(),
924 $del = $this->skin
->revDeleteLink( $query,
925 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
932 // Reversion link/current indicator
935 $row .= wfMsgHtml( 'filehist-current' );
936 } elseif ( $local && $wgUser->isLoggedIn() && $this->title
->userCan( 'edit' ) ) {
937 if ( $file->isDeleted( File
::DELETED_FILE
) ) {
938 $row .= wfMsgHtml( 'filehist-revert' );
940 $row .= $this->skin
->link(
942 wfMsgHtml( 'filehist-revert' ),
945 'action' => 'revert',
947 'wpEditToken' => $wgUser->editToken( $img )
949 array( 'known', 'noclasses' )
955 // Date/time and image link
956 if ( $file->getTimestamp() === $this->img
->getTimestamp() ) {
957 $selected = "class='filehistory-selected'";
959 $row .= "<td $selected style='white-space: nowrap;'>";
960 if ( !$file->userCan( File
::DELETED_FILE
) ) {
961 # Don't link to unviewable files
962 $row .= '<span class="history-deleted">' . $wgLang->timeAndDate( $timestamp, true ) . '</span>';
963 } elseif ( $file->isDeleted( File
::DELETED_FILE
) ) {
964 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
965 # Make a link to review the image
966 $url = $this->skin
->link(
968 $wgLang->timeAndDate( $timestamp, true ),
971 'target' => $this->title
->getPrefixedText(),
973 'token' => $wgUser->editToken( $img )
975 array( 'known', 'noclasses' )
977 $row .= '<span class="history-deleted">' . $url . '</span>';
979 $url = $iscur ?
$this->current
->getUrl() : $this->current
->getArchiveUrl( $img );
980 $row .= Xml
::element( 'a', array( 'href' => $url ), $wgLang->timeAndDate( $timestamp, true ) );
985 if ( $this->showThumb
) {
986 $row .= '<td>' . $this->getThumbForLine( $file ) . '</td>';
989 // Image dimensions + size
991 $row .= htmlspecialchars( $file->getDimensionsString() );
992 $row .= " <span style='white-space: nowrap;'>(" . $this->skin
->formatSize( $file->getSize() ) . ')</span>';
998 // Hide deleted usernames
999 if ( $file->isDeleted( File
::DELETED_USER
) ) {
1000 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1002 $row .= $this->skin
->userLink( $user, $usertext ) . " <span style='white-space: nowrap;'>" .
1003 $this->skin
->userToolLinks( $user, $usertext ) . "</span>";
1006 $row .= htmlspecialchars( $usertext );
1008 $row .= '</td><td>';
1010 // Don't show deleted descriptions
1011 if ( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1012 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span>';
1014 $row .= $this->skin
->commentBlock( $description, $this->title
);
1018 wfRunHooks( 'ImagePageFileHistoryLine', array( $this, $file, &$row, &$rowClass ) );
1019 $classAttr = $rowClass ?
" class='$rowClass'" : "";
1021 return "<tr{$classAttr}>{$row}</tr>\n";
1024 protected function getThumbForLine( $file ) {
1027 if ( $file->allowInlineDisplay() && $file->userCan( File
::DELETED_FILE
) && !$file->isDeleted( File
::DELETED_FILE
) ) {
1032 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
1034 $thumbnail = $file->transform( $params );
1036 'alt' => wfMsg( 'filehist-thumbtext',
1037 $wgLang->timeAndDate( $timestamp, true ),
1038 $wgLang->date( $timestamp, true ),
1039 $wgLang->time( $timestamp, true ) ),
1040 'file-link' => true,
1043 if ( !$thumbnail ) return wfMsgHtml( 'filehist-nothumb' );
1045 return $thumbnail->toHtml( $options );
1047 return wfMsgHtml( 'filehist-nothumb' );
1052 class ImageHistoryPseudoPager
extends ReverseChronologicalPager
{
1053 function __construct( $imagePage ) {
1054 parent
::__construct();
1055 $this->mImagePage
= $imagePage;
1056 $this->mTitle
= clone ( $imagePage->getTitle() );
1057 $this->mTitle
->setFragment( '#filehistory' );
1059 $this->mHist
= array();
1060 $this->mRange
= array( 0, 0 ); // display range
1063 function getTitle() {
1064 return $this->mTitle
;
1067 function getQueryInfo() {
1071 function getIndexField() {
1075 function formatRow( $row ) {
1079 function getBody() {
1082 if ( count( $this->mHist
) ) {
1083 $list = new ImageHistoryList( $this->mImagePage
);
1084 # Generate prev/next links
1085 $navLink = $this->getNavigationBar();
1086 $s = $list->beginImageHistoryList( $navLink );
1087 // Skip rows there just for paging links
1088 for ( $i = $this->mRange
[0]; $i <= $this->mRange
[1]; $i++
) {
1089 $file = $this->mHist
[$i];
1090 $s .= $list->imageHistoryLine( !$file->isOld(), $file );
1092 $s .= $list->endImageHistoryList( $navLink );
1097 function doQuery() {
1098 if ( $this->mQueryDone
) return;
1099 $this->mImg
= $this->mImagePage
->getFile(); // ensure loading
1100 if ( !$this->mImg
->exists() ) {
1103 $queryLimit = $this->mLimit +
1; // limit plus extra row
1104 if ( $this->mIsBackwards
) {
1105 // Fetch the file history
1106 $this->mHist
= $this->mImg
->getHistory( $queryLimit, null, $this->mOffset
, false );
1107 // The current rev may not meet the offset/limit
1108 $numRows = count( $this->mHist
);
1109 if ( $numRows <= $this->mLimit
&& $this->mImg
->getTimestamp() > $this->mOffset
) {
1110 $this->mHist
= array_merge( array( $this->mImg
), $this->mHist
);
1113 // The current rev may not meet the offset
1114 if ( !$this->mOffset ||
$this->mImg
->getTimestamp() < $this->mOffset
) {
1115 $this->mHist
[] = $this->mImg
;
1117 // Old image versions (fetch extra row for nav links)
1118 $oiLimit = count( $this->mHist
) ?
$this->mLimit
: $this->mLimit +
1;
1119 // Fetch the file history
1120 $this->mHist
= array_merge( $this->mHist
,
1121 $this->mImg
->getHistory( $oiLimit, $this->mOffset
, null, false ) );
1123 $numRows = count( $this->mHist
); // Total number of query results
1125 # Index value of top item in the list
1126 $firstIndex = $this->mIsBackwards ?
1127 $this->mHist
[$numRows - 1]->getTimestamp() : $this->mHist
[0]->getTimestamp();
1128 # Discard the extra result row if there is one
1129 if ( $numRows > $this->mLimit
&& $numRows > 1 ) {
1130 if ( $this->mIsBackwards
) {
1131 # Index value of item past the index
1132 $this->mPastTheEndIndex
= $this->mHist
[0]->getTimestamp();
1133 # Index value of bottom item in the list
1134 $lastIndex = $this->mHist
[1]->getTimestamp();
1136 $this->mRange
= array( 1, $numRows - 1 );
1138 # Index value of item past the index
1139 $this->mPastTheEndIndex
= $this->mHist
[$numRows - 1]->getTimestamp();
1140 # Index value of bottom item in the list
1141 $lastIndex = $this->mHist
[$numRows - 2]->getTimestamp();
1143 $this->mRange
= array( 0, $numRows - 2 );
1146 # Setting indexes to an empty string means that they will be
1147 # omitted if they would otherwise appear in URLs. It just so
1148 # happens that this is the right thing to do in the standard
1149 # UI, in all the relevant cases.
1150 $this->mPastTheEndIndex
= '';
1151 # Index value of bottom item in the list
1152 $lastIndex = $this->mIsBackwards ?
1153 $this->mHist
[0]->getTimestamp() : $this->mHist
[$numRows - 1]->getTimestamp();
1155 $this->mRange
= array( 0, $numRows - 1 );
1160 $this->mPastTheEndIndex
= '';
1162 if ( $this->mIsBackwards
) {
1163 $this->mIsFirst
= ( $numRows < $queryLimit );
1164 $this->mIsLast
= ( $this->mOffset
== '' );
1165 $this->mLastShown
= $firstIndex;
1166 $this->mFirstShown
= $lastIndex;
1168 $this->mIsFirst
= ( $this->mOffset
== '' );
1169 $this->mIsLast
= ( $numRows < $queryLimit );
1170 $this->mLastShown
= $lastIndex;
1171 $this->mFirstShown
= $firstIndex;
1173 $this->mQueryDone
= true;