Preparations for Oracle database abstraction update.
[lhc/web/wiklou.git] / includes / ImagePage.php
index b2cade2..612d2a6 100644 (file)
@@ -22,6 +22,12 @@ class ImagePage extends Article {
                $this->dupes = null;
                $this->repo = null;
        }
+       
+       public function setFile( $file ) {
+               $this->displayImg = $file;
+               $this->img = $file;
+               $this->fileLoaded = true;
+       }
 
        protected function loadFile() {
                if( $this->fileLoaded ) {
@@ -57,7 +63,7 @@ class ImagePage extends Article {
                global $wgOut, $wgShowEXIF, $wgRequest, $wgUser;
                $this->loadFile();
 
-               if( $this->mTitle->getNamespace() == NS_IMAGE && $this->img->getRedirected() ) {
+               if( $this->mTitle->getNamespace() == NS_FILE && $this->img->getRedirected() ) {
                        if( $this->mTitle->getDBkey() == $this->img->getName() ) {
                                // mTitle is the same as the redirect target so ask Article
                                // to perform the redirect for us.
@@ -66,7 +72,7 @@ class ImagePage extends Article {
                                // mTitle is not the same as the redirect target so it is 
                                // probably the redirect page itself. Fake the redirect symbol
                                $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
-                               $wgOut->addHTML( $this->viewRedirect( Title::makeTitle( NS_IMAGE, $this->img->getName() ),
+                               $wgOut->addHTML( $this->viewRedirect( Title::makeTitle( NS_FILE, $this->img->getName() ),
                                        /* $appendSubtitle */ true, /* $forceKnown */ true ) );
                                $this->viewUpdates();
                                return;
@@ -76,7 +82,7 @@ class ImagePage extends Article {
                $diff = $wgRequest->getVal( 'diff' );
                $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
 
-               if( $this->mTitle->getNamespace() != NS_IMAGE || ( isset( $diff ) && $diffOnly ) )
+               if( $this->mTitle->getNamespace() != NS_FILE || ( isset( $diff ) && $diffOnly ) )
                        return Article::view();
 
                if( $wgShowEXIF && $this->displayImg->exists() ) {
@@ -87,10 +93,11 @@ class ImagePage extends Article {
                        $showmeta = false;
                }
 
-               if( $this->displayImg->exists() )
+               if( !$diff && $this->displayImg->exists() )
                        $wgOut->addHTML( $this->showTOC($showmeta) );
 
-               $this->openShowImage();
+               if( !$diff )
+                       $this->openShowImage();
 
                # No need to display noarticletext, we use our own message, output in openShowImage()
                if( $this->getID() ) {
@@ -98,7 +105,6 @@ class ImagePage extends Article {
                } else {
                        # Just need to set the right headers
                        $wgOut->setArticleFlag( true );
-                       $wgOut->setRobotPolicy( 'noindex,nofollow' );
                        $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
                        $this->viewUpdates();
                }
@@ -109,9 +115,7 @@ class ImagePage extends Article {
                        if( $fol != '-' && !wfEmptyMsg( 'shareddescriptionfollows', $fol ) ) {
                                $wgOut->addWikiText( $fol );
                        }
-                       $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription . '</div>' );
-               } else {
-                       $this->checkSharedConflict();
+                       $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription . "</div>\n" );
                }
 
                $this->closeShowImage();
@@ -122,18 +126,16 @@ class ImagePage extends Article {
                        array( 'id' => 'filelinks' ),
                        wfMsg( 'imagelinks' ) ) . "\n" );
                $this->imageDupes();
-               // TODO: We may want to find local images redirecting to a foreign 
-               // file: "The following local files redirect to this file"
-               if( $this->img->isLocal() ) {
-                       $this->imageRedirects();
-               }
+               # TODO! FIXME! For some freaky reason, we can't redirect to foreign images.
+               # Yet we return metadata about the target. Definitely an issue in the FileRepo
+               $this->imageRedirects();
                $this->imageLinks();
 
                if( $showmeta ) {
                        global $wgStylePath, $wgStyleVersion;
-                       $expand = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-expand' ) ) );
-                       $collapse = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-collapse' ) ) );
-                       $wgOut->addHTML( Xml::element( 'h2', array( 'id' => 'metadata' ), wfMsg( 'metadata' ) ). "\n" );
+                       $expand = htmlspecialchars( Xml::escapeJsString( wfMsg( 'metadata-expand' ) ) );
+                       $collapse = htmlspecialchars( Xml::escapeJsString( wfMsg( 'metadata-collapse' ) ) );
+                       $wgOut->addHTML( Xml::element( 'h2', array( 'id' => 'metadata' ), wfMsg( 'metadata' ) ) . "\n" );
                        $wgOut->addWikiText( $this->makeMetadataTable( $formattedMetadata ) );
                        $wgOut->addScriptFile( 'metadata.js' );
                        $wgOut->addHTML(
@@ -152,7 +154,7 @@ class ImagePage extends Article {
                if( $from == $to ) {
                        return null; 
                }
-               return $this->mRedirectTarget = Title::makeTitle( NS_IMAGE, $to );
+               return $this->mRedirectTarget = Title::makeTitle( NS_FILE, $to );
        }
        public function followRedirect() {
                $this->loadFile();
@@ -164,7 +166,7 @@ class ImagePage extends Article {
                if( $from == $to ) {
                        return false; 
                }
-               return Title::makeTitle( NS_IMAGE, $to );       
+               return Title::makeTitle( NS_FILE, $to );        
        }
        public function isRedirect( $text = false ) {
                $this->loadFile();
@@ -222,11 +224,11 @@ class ImagePage extends Article {
        protected function showTOC( $metadata ) {
                global $wgLang;
                $r = '<ul id="filetoc">
-                       <li><a href="#file">' . $wgLang->getNsText( NS_IMAGE ) . '</a></li>
+                       <li><a href="#file">' . $wgLang->getNsText( NS_FILE ) . '</a></li>
                        <li><a href="#filehistory">' . wfMsgHtml( 'filehist' ) . '</a></li>
-                       <li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>' .
-                       ($metadata ? ' <li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>' : '') . '
-               </ul>';
+                       <li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . "</a></li>\n" .
+                       ($metadata ? '                  <li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>' : '') . "
+                       </ul>\n";
                return $r;
        }
 
@@ -239,20 +241,22 @@ class ImagePage extends Article {
         * @return string
         */
        protected function makeMetadataTable( $metadata ) {
-               $r = wfMsg( 'metadata-help' ) . "\n\n";
-               $r .= "{| id=mw_metadata class=mw_metadata\n";
+               $r = "<div class=\"mw-imagepage-section-metadata\">\n";
+               $r .= wfMsg( 'metadata-help' );
+               $r .= "<table id=\"mw_metadata\" class=\"mw_metadata\">\n";
                foreach ( $metadata as $type => $stuff ) {
                        foreach ( $stuff as $v ) {
+                               # FIXME, why is this using escapeId for a class?!
                                $class = Sanitizer::escapeId( $v['id'] );
                                if( $type == 'collapsed' ) {
                                        $class .= ' collapsable';
                                }
-                               $r .= "|- class=\"$class\"\n";
-                               $r .= "!| {$v['name']}\n";
-                               $r .= "|| {$v['value']}\n";
+                               $r .= "<tr class=\"$class\">\n";
+                               $r .= "<th>{$v['name']}</th>\n";
+                               $r .= "<td>{$v['value']}</td>\n</tr>";
                        }
                }
-               $r .= '|}';
+               $r .= "</table>\n</div>\n";
                return $r;
        }
 
@@ -368,7 +372,7 @@ class ImagePage extends Article {
                                        );
                                        $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
                                                $thumbnail->toHtml( $options ) .
-                                               $anchorclose . '</div>' );
+                                               $anchorclose . "</div>\n" );
                                }
 
                                if( $this->displayImg->isMultipage() ) {
@@ -425,7 +429,7 @@ class ImagePage extends Article {
 
                                        $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
                                        $icon->toHtml( array( 'desc-link' => true ) ) .
-                                       '</div>' );
+                                       "</div>\n" );
                                }
 
                                $showLink = true;
@@ -434,22 +438,25 @@ class ImagePage extends Article {
 
                        if($showLink) {
                                $filename = wfEscapeWikiText( $this->displayImg->getName() );
+                               $medialink = $this->displayImg->isMissing() ?
+                                       "'''$filename'''" :
+                                       "[[Media:$filename|$filename]]";
 
                                if( !$this->displayImg->isSafeFile() ) {
                                        $warning = wfMsgNoTrans( 'mediawarning' );
                                        $wgOut->addWikiText( <<<EOT
 <div class="fullMedia">
-<span class="dangerousLink">[[Media:$filename|$filename]]</span>$dirmark
-<span class="fileInfo"> $longDesc</span>
+<span class="dangerousLink">{$medialink}</span>$dirmark
+<span class="fileInfo">$longDesc</span>
 </div>
-
 <div class="mediaWarning">$warning</div>
 EOT
                                                );
                                } else {
                                        $wgOut->addWikiText( <<<EOT
 <div class="fullMedia">
-[[Media:$filename|$filename]]$dirmark <span class="fileInfo"> $longDesc</span>
+{$medialink}{$dirmark}
+<span class="fileInfo">$longDesc</span>
 </div>
 EOT
                                                );
@@ -465,6 +472,7 @@ EOT
                        $title = SpecialPage::getTitleFor( 'Upload' );
                        $link = $sk->makeKnownLinkObj($title, wfMsgHtml('noimage-linktext'),
                                'wpDestFile=' . urlencode( $this->displayImg->getName() ) );
+                       $wgOut->setRobotPolicy( 'noindex,nofollow' );
                        $wgOut->addHTML( wfMsgWikiHtml( 'noimage', $link ) );
                }
        }
@@ -473,83 +481,34 @@ EOT
         * Show a notice that the file is from a shared repository
         */
        protected function printSharedImageText() {
-               global $wgOut, $wgUser;
+               global $wgOut;
 
                $this->loadFile();
 
                $descUrl = $this->img->getDescriptionUrl();
                $descText = $this->img->getDescriptionText();
-               $s = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml( 'sharedupload' );
-               if( $descUrl ) {
-                       $sk = $wgUser->getSkin();
-                       $link = $sk->makeExternalLink( $descUrl, wfMsg( 'shareduploadwiki-linktext' ) );
-                       $msg = ( $descText ) ? 'shareduploadwiki-desc' : 'shareduploadwiki';
-                       $msg = wfMsgExt( $msg, array( 'parseinline', 'replaceafter' ), $link );
-                       if( $msg != '-' ) {
-                               # Show message only if not voided by local sysops
-                               $s .= $msg;
-                       }
-               }
-               $s .= "</div>";
-               $wgOut->addHTML( $s );
 
-               if( $descText ) {
-                       $this->mExtraDescription = $descText;
-               }
-       }
-
-       /*
-        * Check for files with the same name on the foreign repos.
-        */
-       protected function checkSharedConflict() {
-               global $wgOut, $wgUser;
-               
-               $repoGroup = RepoGroup::singleton();
-               if( !$repoGroup->hasForeignRepos() ) {
-                       return;
-               }
-               
-               $this->loadFile();
-               if( !$this->img->isLocal() ) {
-                       return;
-               }
+               $wrap = "<div class=\"sharedUploadNotice\">\n$1\n</div>\n";
+               $repo = $this->img->getRepo()->getDisplayName();
 
-               $this->dupFile = null;
-               $repoGroup->forEachForeignRepo( array( $this, 'checkSharedConflictCallback' ) );
-               
-               if( !$this->dupFile )
-                       return;
-               $dupfile = $this->dupFile;
-               $same = (
-                       ($this->img->getSha1() == $dupfile->getSha1()) &&
-                       ($this->img->getSize() == $dupfile->getSize())
-               );
-
-               $sk = $wgUser->getSkin();
-               $descUrl = $dupfile->getDescriptionUrl();
-               if( $same ) {
-                       $link = $sk->makeExternalLink( $descUrl, wfMsg( 'shareduploadduplicate-linktext' ) );
-                       $wgOut->addHTML( '<div id="shared-image-dup">' . wfMsgWikiHtml( 'shareduploadduplicate', $link ) . '</div>' );
+               $msg = '';
+               if( $descUrl && $descText && wfMsgNoTrans( 'sharedupload-desc-here' ) !== '-'  ) {
+                       $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-here', $repo, $descUrl ) );
+               } elseif ( $descUrl && wfMsgNoTrans( 'sharedupload-desc-there' ) !== '-' ) {
+                       $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-there', $repo, $descUrl ) );
                } else {
-                       $link = $sk->makeExternalLink( $descUrl, wfMsg( 'shareduploadconflict-linktext' ) );
-                       $wgOut->addHTML( '<div id="shared-image-conflict">' . wfMsgWikiHtml( 'shareduploadconflict', $link ) . '</div>' );
+                       $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload', $repo ), ''/*BACKCOMPAT*/ );
                }
-       }
 
-       protected function checkSharedConflictCallback( $repo ) {
-               $this->loadFile();
-               $dupfile = $repo->newFile( $this->img->getTitle() );
-               if( $dupfile && $dupfile->exists() ) {
-                       $this->dupFile = $dupfile;
-                       return $dupfile->exists();
+               if( $descText ) {
+                       $this->mExtraDescription = $descText;
                }
-               return false;
        }
 
        public function getUploadUrl() {
                $this->loadFile();
                $uploadTitle = SpecialPage::getTitleFor( 'Upload' );
-               return $uploadTitle->getFullUrl( 'wpDestFile=' . urlencode( $this->img->getName() ) );
+               return $uploadTitle->getFullUrl( 'wpDestFile=' . urlencode( $this->img->getName() ) . '&wpForReUpload=1' );
        }
 
        /**
@@ -565,23 +524,19 @@ EOT
 
                $sk = $wgUser->getSkin();
 
-               $wgOut->addHTML( '<br /><ul>' );
+               $wgOut->addHTML( "<br /><ul>\n" );
 
                # "Upload a new version of this file" link
                if( UploadForm::userCanReUpload($wgUser,$this->img->name) ) {
                        $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
-                       $wgOut->addHTML( "<li><div class='plainlinks'>{$ulink}</div></li>" );
+                       $wgOut->addHTML( "<li id=\"mw-imagepage-reupload-link\"><div class=\"plainlinks\">{$ulink}</div></li>\n" );
                }
 
-               # Link to Special:FileDuplicateSearch
-               $dupeLink = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'FileDuplicateSearch', $this->mTitle->getDBkey() ), wfMsgHtml( 'imagepage-searchdupe' ) );
-               $wgOut->addHTML( "<li>{$dupeLink}</li>" );
-
                # External editing link
                $elink = $sk->makeKnownLinkObj( $this->mTitle, wfMsgHtml( 'edit-externally' ), 'action=edit&externaledit=true&mode=file' );
-               $wgOut->addHTML( '<li>' . $elink . ' <small>' . wfMsgExt( 'edit-externally-help', array( 'parseinline' ) ) . '</small></li>' );
+               $wgOut->addHTML( '<li id="mw-imagepage-edit-external">' . $elink . ' <small>' . wfMsgExt( 'edit-externally-help', array( 'parseinline' ) ) . "</small></li>\n" );
 
-               $wgOut->addHTML( '</ul>' );
+               $wgOut->addHTML( "</ul>\n" );
        }
 
        protected function closeShowImage() {} # For overloading
@@ -594,20 +549,8 @@ EOT
                global $wgOut, $wgUseExternalEditor;
 
                $this->loadFile();
-               $s = '';
-               if( $this->img->exists() ) {
-                       $list = new ImageHistoryList( $this );
-                       $file = $this->img;
-                       $s = $list->beginImageHistoryList();
-                       $s .= $list->imageHistoryLine( true, $file );
-                       // old image versions
-                       $hist = $this->img->getHistory();
-                       foreach( $hist as $file ) {
-                               $s .= $list->imageHistoryLine( false, $file );
-                       }
-                       $s .= $list->endImageHistoryList();
-               }
-               $wgOut->addHTML( $s );
+               $pager = new ImageHistoryPseudoPager( $this );
+               $wgOut->addHTML( $pager->getBody() );
 
                $this->img->resetHistory(); // free db resources
 
@@ -663,18 +606,19 @@ EOT
                                $wgOut->addHTML( "<li>{$link}</li>\n" );
                        }
                }
-               $wgOut->addHTML( "</ul></div>\n" );
+               $wgOut->addHTML( "</ul>\n" );
                $res->free();
 
                // Add a links to [[Special:Whatlinkshere]]
                if( $count > $limit )
                        $wgOut->addWikiMsg( 'morelinkstoimage', $this->mTitle->getPrefixedDBkey() );
+               $wgOut->addHTML( "</div>\n" );
        }
        
        protected function imageRedirects() {
                global $wgUser, $wgOut, $wgLang;
 
-               $redirects = $this->getTitle()->getRedirectsHere( NS_IMAGE );
+               $redirects = $this->getTitle()->getRedirectsHere( NS_FILE );
                if( count( $redirects ) == 0 ) return;
 
                $wgOut->addHTML( "<div id='mw-imagepage-section-redirectstofile'>\n" );
@@ -702,19 +646,21 @@ EOT
 
                $wgOut->addHTML( "<div id='mw-imagepage-section-duplicates'>\n" );
                $wgOut->addWikiMsg( 'duplicatesoffile',
-                       $wgLang->formatNum( count( $dupes ) )
+                       $wgLang->formatNum( count( $dupes ) ), $this->mTitle->getDBkey()
                );
                $wgOut->addHTML( "<ul class='mw-imagepage-duplicates'>\n" );
 
                $sk = $wgUser->getSkin();
                foreach ( $dupes as $file ) {
+                       $fromSrc = '';
                        if( $file->isLocal() )
                                $link = $sk->makeKnownLinkObj( $file->getTitle(), "" );
                        else {
                                $link = $sk->makeExternalLink( $file->getDescriptionUrl(),
                                        $file->getTitle()->getPrefixedText() );
+                               $fromSrc = wfMsg( 'shared-repo-from', $file->getRepo()->getDisplayName() );
                        }
-                       $wgOut->addHTML( "<li>{$link}</li>\n" );
+                       $wgOut->addHTML( "<li>{$link} {$fromSrc}</li>\n" );
                }
                $wgOut->addHTML( "</ul></div>\n" );
        }
@@ -754,7 +700,10 @@ EOT
                        $this->img->upgradeRow();
                        $this->img->purgeCache();
                } else {
-                       wfDebug( "ImagePage::doPurge no image\n" );
+                       wfDebug( "ImagePage::doPurge no image for " . $this->img->getName() . "; limiting purge to cache only\n" );
+                       // even if the file supposedly doesn't exist, force any cached information
+                       // to be updated (in case the cached information is wrong)
+                       $this->img->purgeCache();
                }
                parent::doPurge();
        }
@@ -803,10 +752,12 @@ class ImageHistoryList {
                return $this->img;
        }
 
-       public function beginImageHistoryList() {
+       public function beginImageHistoryList( $navLinks = '' ) {
                global $wgOut, $wgUser;
-               return Xml::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) )
+               return Xml::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) ) . "\n"
+                       . "<div id=\"mw-imagepage-section-filehistory\">\n"
                        . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
+                       . $navLinks . "\n"
                        . Xml::openElement( 'table', array( 'class' => 'filehistory' ) ) . "\n"
                        . '<tr><td></td>'
                        . ( $this->current->isLocal() && ($wgUser->isAllowed('delete') || $wgUser->isAllowed('deleterevision') ) ? '<td></td>' : '' )
@@ -818,8 +769,8 @@ class ImageHistoryList {
                        . "</tr>\n";
        }
 
-       public function endImageHistoryList() {
-               return "</table>\n";
+       public function endImageHistoryList( $navLinks = '' ) {
+               return "</table>\n$navLinks\n</div>\n";
        }
 
        public function imageHistoryLine( $iscur, $file ) {
@@ -829,9 +780,7 @@ class ImageHistoryList {
                $img = $iscur ? $file->getName() : $file->getArchiveName();
                $user = $file->getUser('id');
                $usertext = $file->getUser('text');
-               $size = $file->getSize();
                $description = $file->getDescription();
-               $dims = $file->getDimensionsString();
                $sha1 = $file->getSha1();
 
                $local = $this->current->isLocal();
@@ -842,14 +791,13 @@ class ImageHistoryList {
                        $row .= '<td>';
                        # Link to remove from history
                        if( $wgUser->isAllowed( 'delete' ) ) {
-                               $q = array();
-                               $q[] = 'action=delete';
+                               $q = array( 'action' => 'delete' );
                                if( !$iscur )
-                                       $q[] = 'oldimage=' . urlencode( $img );
-                               $row .= $this->skin->makeKnownLinkObj(
+                                       $q['oldimage'] = $img;
+                               $row .= $this->skin->link(
                                        $this->title,
                                        wfMsgHtml( $iscur ? 'filehist-deleteall' : 'filehist-deleteone' ),
-                                       implode( '&', $q )
+                                       array(), $q, array( 'known' )
                                );
                        }
                        # Link to hide content
@@ -863,10 +811,12 @@ class ImageHistoryList {
                                        $del = wfMsgHtml( 'rev-delundel' );
                                } else {
                                        // If the file was hidden, link to sha-1
-                                       list($ts,$name) = explode('!',$img,2);
-                                       $del = $this->skin->makeKnownLinkObj( $revdel,  wfMsg( 'rev-delundel' ),
-                                               'target=' . urlencode( $wgTitle->getPrefixedText() ) .
-                                               '&oldimage=' . urlencode( $ts ) );
+                                       list( $ts, $name ) = explode( '!', $img, 2 );
+                                       $del = $this->skin->link( $revdel, wfMsgHtml( 'rev-delundel' ),
+                                               array(),
+                                               array( 'target' => $wgTitle->getPrefixedText(), 'oldimage' => $ts ),
+                                               array( 'known' )
+                                       );
                                        // Bolden oversighted content
                                        if( $file->isDeleted(File::DELETED_RESTRICTED) )
                                                $del = "<strong>$del</strong>";
@@ -903,42 +853,32 @@ class ImageHistoryList {
                if( !$file->userCan(File::DELETED_FILE) ) {
                        # Don't link to unviewable files
                        $row .= '<span class="history-deleted">' . $wgLang->timeAndDate( $timestamp, true ) . '</span>';
-               } else if( $file->isDeleted(File::DELETED_FILE) ) {
+               } elseif( $file->isDeleted(File::DELETED_FILE) ) {
                        $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
                        # Make a link to review the image
                        $url = $this->skin->makeKnownLinkObj( $revdel, $wgLang->timeAndDate( $timestamp, true ),
                                "target=".$wgTitle->getPrefixedText()."&file=$sha1.".$this->current->getExtension() );
                        $row .= '<span class="history-deleted">'.$url.'</span>';
+               } elseif( $file->isMissing() ) {
+                       # Don't link to missing files
+                       $row .= $wgLang->timeAndDate( $timestamp, true );
                } else {
                        $url = $iscur ? $this->current->getUrl() : $this->current->getArchiveUrl( $img );
                        $row .= Xml::element( 'a', array( 'href' => $url ), $wgLang->timeAndDate( $timestamp, true ) );
                }
+               $row .= "</td>";
 
                // Thumbnail
-               if( $file->allowInlineDisplay() && $file->userCan( File::DELETED_FILE ) && !$file->isDeleted( File::DELETED_FILE ) ) {
-                       $params = array(
-                               'width' => '120',
-                               'height' => '120',
-                       );
-                       $thumbnail = $file->transform( $params );
-                       $options = array(
-                               'alt' => wfMsg( 'filehist-thumbtext', $wgLang->timeAndDate( $timestamp, true ) ),
-                               'file-link' => true,
-                       );
-                       $row .= '</td><td>' . $thumbnail->toHtml( $options );
-               } else {
-                       $row .= '</td><td>' . wfMsgHtml( 'filehist-nothumb' );
-               }
-               $row .= "</td><td>";
-
-               // Image dimensions
-               $row .= htmlspecialchars( $dims );
+               $row .= '<td>' . $this->getThumbForLine( $file ) . '</td>';
 
-               // File size
-               $row .= " <span style='white-space: nowrap;'>(" . $this->skin->formatSize( $size ) . ')</span>';
+               // Image dimensions + size
+               $row .= '<td>';
+               $row .= htmlspecialchars( $file->getDimensionsString() );
+               $row .= " <span style='white-space: nowrap;'>(" . $this->skin->formatSize( $file->getSize() ) . ')</span>';
+               $row .= '</td>';
 
                // Uploading user
-               $row .= '</td><td>';
+               $row .= '<td>';
                if( $local ) {
                        // Hide deleted usernames
                        if( $file->isDeleted(File::DELETED_USER) ) {
@@ -965,4 +905,155 @@ class ImageHistoryList {
 
                return "<tr{$classAttr}>{$row}</tr>\n";
        }
+
+       protected function getThumbForLine( $file ) {
+               global $wgLang;
+
+               if( $file->isMissing() ) {
+                       return '<strong class="error">' . wfMsgHtml( 'filehist-missing' ) . '</strong>';
+               } elseif( $file->allowInlineDisplay() && $file->userCan( File::DELETED_FILE ) && !$file->isDeleted( File::DELETED_FILE ) ) {
+                       $params = array(
+                               'width' => '120',
+                               'height' => '120',
+                       );
+                       $timestamp = wfTimestamp(TS_MW, $file->getTimestamp());
+
+                       $thumbnail = $file->transform( $params );
+                       $options = array(
+                               'alt' => wfMsg( 'filehist-thumbtext', $wgLang->timeAndDate( $timestamp, true ) ),
+                               'file-link' => true,
+                       );
+                       
+                       if ( !$thumbnail ) return wfMsgHtml( 'filehist-nothumb' );
+
+                       return $thumbnail->toHtml( $options );
+               } else {
+                       return wfMsgHtml( 'filehist-nothumb' );
+               }
+       }
+}
+
+class ImageHistoryPseudoPager extends ReverseChronologicalPager {
+       function __construct( $imagePage ) {
+               parent::__construct();
+               $this->mImagePage = $imagePage;
+               $this->mTitle = clone( $imagePage->getTitle() );
+               $this->mTitle->setFragment( '#filehistory' );
+               $this->mImg = NULL;
+               $this->mHist = array();
+               $this->mRange = array( 0, 0 ); // display range
+       }
+       
+       function getTitle() {
+               return $this->mTitle;
+       }
+
+       function getQueryInfo() {
+               return false;
+       }
+
+       function getIndexField() {
+               return '';
+       }
+
+       function formatRow( $row ) {
+               return '';
+       }
+       
+       function getBody() {
+               $s = '';
+               $this->doQuery();
+               if( count($this->mHist) ) {
+                       $list = new ImageHistoryList( $this->mImagePage );
+                       # Generate prev/next links
+                       $navLink = $this->getNavigationBar();
+                       $s = $list->beginImageHistoryList($navLink);
+                       // Skip rows there just for paging links
+                       for( $i = $this->mRange[0]; $i <= $this->mRange[1]; $i++ ) {
+                               $file = $this->mHist[$i];
+                               $s .= $list->imageHistoryLine( !$file->isOld(), $file );
+                       }
+                       $s .= $list->endImageHistoryList($navLink);
+               }
+               return $s;
+       }
+
+       function doQuery() {
+               if( $this->mQueryDone ) return;
+               $this->mImg = $this->mImagePage->getFile(); // ensure loading
+               if( !$this->mImg->exists() ) {
+                       return;
+               }
+               $queryLimit = $this->mLimit + 1; // limit plus extra row
+               if( $this->mIsBackwards ) {
+                       // Fetch the file history
+                       $this->mHist = $this->mImg->getHistory($queryLimit,null,$this->mOffset,false);
+                       // The current rev may not meet the offset/limit
+                       $numRows = count($this->mHist);
+                       if( $numRows <= $this->mLimit && $this->mImg->getTimestamp() > $this->mOffset ) {
+                               $this->mHist = array_merge( array($this->mImg), $this->mHist );
+                       }
+               } else {
+                       // The current rev may not meet the offset
+                       if( !$this->mOffset || $this->mImg->getTimestamp() < $this->mOffset ) {
+                               $this->mHist[] = $this->mImg;
+                       }
+                       // Old image versions (fetch extra row for nav links)
+                       $oiLimit = count($this->mHist) ? $this->mLimit : $this->mLimit+1;
+                       // Fetch the file history
+                       $this->mHist = array_merge( $this->mHist,
+                               $this->mImg->getHistory($oiLimit,$this->mOffset,null,false) );
+               }
+               $numRows = count($this->mHist); // Total number of query results
+               if( $numRows ) {
+                       # Index value of top item in the list
+                       $firstIndex = $this->mIsBackwards ?
+                               $this->mHist[$numRows-1]->getTimestamp() : $this->mHist[0]->getTimestamp();
+                       # Discard the extra result row if there is one
+                       if( $numRows > $this->mLimit && $numRows > 1 ) {
+                               if( $this->mIsBackwards ) {
+                                       # Index value of item past the index
+                                       $this->mPastTheEndIndex = $this->mHist[0]->getTimestamp();
+                                       # Index value of bottom item in the list
+                                       $lastIndex = $this->mHist[1]->getTimestamp();
+                                       # Display range
+                                       $this->mRange = array( 1, $numRows-1 );
+                               } else {
+                                       # Index value of item past the index
+                                       $this->mPastTheEndIndex = $this->mHist[$numRows-1]->getTimestamp();
+                                       # Index value of bottom item in the list
+                                       $lastIndex = $this->mHist[$numRows-2]->getTimestamp();
+                                       # Display range
+                                       $this->mRange = array( 0, $numRows-2 );
+                               }
+                       } else {
+                               # Setting indexes to an empty string means that they will be
+                               # omitted if they would otherwise appear in URLs. It just so
+                               # happens that this  is the right thing to do in the standard
+                               # UI, in all the relevant cases.
+                               $this->mPastTheEndIndex = '';
+                               # Index value of bottom item in the list
+                               $lastIndex = $this->mIsBackwards ?
+                                       $this->mHist[0]->getTimestamp() : $this->mHist[$numRows-1]->getTimestamp();
+                               # Display range
+                               $this->mRange = array( 0, $numRows-1 );
+                       }
+               } else {
+                       $firstIndex = '';
+                       $lastIndex = '';
+                       $this->mPastTheEndIndex = '';
+               }
+               if( $this->mIsBackwards ) {
+                       $this->mIsFirst = ( $numRows < $queryLimit );
+                       $this->mIsLast = ( $this->mOffset == '' );
+                       $this->mLastShown = $firstIndex;
+                       $this->mFirstShown = $lastIndex;
+               } else {
+                       $this->mIsFirst = ( $this->mOffset == '' );
+                       $this->mIsLast = ( $numRows < $queryLimit );
+                       $this->mLastShown = $lastIndex;
+                       $this->mFirstShown = $firstIndex;
+               }
+               $this->mQueryDone = true;
+       }
 }