* Made special page names case-insensitive and localisable. Care has been taken to...
[lhc/web/wiklou.git] / includes / ImagePage.php
index f596da4..86a1500 100644 (file)
@@ -7,9 +7,7 @@
  *
  */
 if( !defined( 'MEDIAWIKI' ) )
-       die();
-
-require_once( 'Image.php' );
+       die( 1 );
 
 /**
  * Special handling for image description pages
@@ -20,14 +18,18 @@ class ImagePage extends Article {
        /* private */ var $img;  // Image object this page is shown for
        var $mExtraDescription = false;
 
+       /**
+        * Handler for action=render
+        * Include body text only; none of the image extras
+        */
        function render() {
                global $wgOut;
-               $wgOut->setArticleBodyOnly(true);
-               $wgOut->addWikitext($this->getContent(true));
+               $wgOut->setArticleBodyOnly( true );
+               $wgOut->addSecondaryWikitext( $this->getContent() );
        }
 
        function view() {
-               global $wgUseExternalEditor, $wgOut, $wgShowEXIF;
+               global $wgOut, $wgShowEXIF;
 
                $this->img = new Image( $this->mTitle );
 
@@ -44,8 +46,6 @@ class ImagePage extends Article {
                                $wgOut->addHTML($this->showTOC($showmeta));
 
                        $this->openShowImage();
-                       if ($exif)
-                               $wgOut->addWikiText($this->makeMetadataTable($exif));
 
                        # No need to display noarticletext, we use our own message, output in openShowImage()
                        if( $this->getID() ) {
@@ -55,20 +55,31 @@ class ImagePage extends Article {
                                $wgOut->setArticleFlag( true );
                                $wgOut->setRobotpolicy( 'index,follow' );
                                $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
-                               $wgOut->addMetaTags();
                                $this->viewUpdates();
                        }
 
-                       if ($this->mExtraDescription) {
-                               $fol = wfMsg('shareddescriptionfollows');
-                               if ($fol != '-')
-                                       $wgOut->addWikiText(wfMsg('shareddescriptionfollows'));
-                               $wgOut->addHTML($this->mExtraDescription);
+                       # Show shared description, if needed
+                       if( $this->mExtraDescription ) {
+                               $fol = wfMsg( 'shareddescriptionfollows' );
+                               if( $fol != '-' ) {
+                                       $wgOut->addWikiText( $fol );
+                               }
+                               $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription . '</div>' );
                        }
 
                        $this->closeShowImage();
                        $this->imageHistory();
                        $this->imageLinks();
+                       if( $exif ) {
+                               global $wgStylePath, $wgStyleVersion;
+                               $expand = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-expand' ) ) );
+                               $collapse = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-collapse' ) ) );
+                               $wgOut->addHTML( "<h2 id=\"metadata\">" . wfMsgHtml( 'metadata' ) . "</h2>\n" );
+                               $wgOut->addWikiText( $this->makeMetadataTable( $exif ) );
+                               $wgOut->addHTML(
+                                       "<script type=\"text/javascript\" src=\"$wgStylePath/common/metadata.js?wgStyleVersion\"></script>\n" .
+                                       "<script type=\"text/javascript\">attachMetadataToggle('mw_metadata', '$expand', '$collapse');</script>\n" );
+                       }
                } else {
                        Article::view();
                }
@@ -85,10 +96,10 @@ class ImagePage extends Article {
        function showTOC( $metadata ) {
                global $wgLang;
                $r = '<ul id="filetoc">
-                       <li><a href="#file">' . $wgLang->getNsText( NS_IMAGE ) . '</a></li>' .
-                       ($metadata ? '<li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>' : '') . '
+                       <li><a href="#file">' . $wgLang->getNsText( NS_IMAGE ) . '</a></li>
                        <li><a href="#filehistory">' . wfMsgHtml( 'imghistory' ) . '</a></li>
-                       <li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>
+                       <li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>' .
+                       ($metadata ? '<li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>' : '') . '
                </ul>';
                return $r;
        }
@@ -102,36 +113,59 @@ class ImagePage extends Article {
         * @return string
         */
        function makeMetadataTable( $exif ) {
-               $r = "{| class=metadata align=right width=250px\n";
-               $r .= '|+ id=metadata | '. htmlspecialchars( wfMsgHtml( 'metadata' ) ) . "\n";
+               $r = wfMsg( 'metadata-help' ) . "\n\n";
+               $r .= "{| id=mw_metadata class=mw_metadata\n";
+               $visibleFields = $this->visibleMetadataFields();
                foreach( $exif as $k => $v ) {
                        $tag = strtolower( $k );
-                       $r .= "! class=$tag |" . wfMsg( "exif-$tag" ) . "\n";
-                       $r .= "| class=$tag |" . htmlspecialchars( $v ) . "\n";
-                       $r .= "|-\n";
+                       $msg = wfMsg( "exif-$tag" );
+                       $class = "exif-$tag";
+                       if( !in_array( $tag, $visibleFields ) ) {
+                               $class .= ' collapsable';
+                       }
+                       $r .= "|- class=\"$class\"\n";
+                       $r .= "!| $msg\n";
+                       $r .= "|| $v\n";
                }
-               return substr($r, 0, -3) . '|}';
+               $r .= '|}';
+               return $r;
        }
 
        /**
-        * Overloading Article's getContent method.
-        * Omit noarticletext if sharedupload
+        * Get a list of EXIF metadata items which should be displayed when
+        * the metadata table is collapsed.
         *
-        * @param $noredir If true, do not follow redirects
+        * @return array of strings
+        * @access private
         */
-       function getContent( $noredir )
-       {
-               if ( $this->img && $this->img->fromSharedDirectory && 0 == $this->getID() ) {
+       function visibleMetadataFields() {
+               $fields = array();
+               $lines = explode( "\n", wfMsgForContent( 'metadata-fields' ) );
+               foreach( $lines as $line ) {
+                       if( preg_match( '/^\\*\s*(.*?)\s*$/', $line, $matches ) ) {
+                               $fields[] = $matches[1];
+                       }
+               }
+               return $fields;
+       }
+
+       /**
+        * Overloading Article's getContent method.
+        * 
+        * Omit noarticletext if sharedupload; text will be fetched from the
+        * shared upload server if possible.
+        */
+       function getContent() {
+               if( $this->img && $this->img->fromSharedDirectory && 0 == $this->getID() ) {
                        return '';
                }
-               return Article::getContent( $noredir );
+               return Article::getContent();
        }
 
-       function openShowImage()
-       {
-               global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
-                      $wgUseImageResize, $wgRepositoryBaseUrl,
-                      $wgUseExternalEditor, $wgServer, $wgFetchCommonsDescriptions;
+       function openShowImage() {
+               global $wgOut, $wgUser, $wgImageLimits, $wgRequest, $wgLang;
+               global $wgUseImageResize, $wgGenerateThumbnailOnParse;
+
                $full_url  = $this->img->getURL();
                $anchoropen = '';
                $anchorclose = '';
@@ -151,6 +185,12 @@ class ImagePage extends Article {
 
                if ( $this->img->exists() ) {
                        # image
+                       $page = $wgRequest->getIntOrNull( 'page' );
+                       if ( ! is_null( $page ) ) {
+                               $this->img->selectPage( $page );
+                       } else {
+                               $page = 1;
+                       }
                        $width = $this->img->getWidth();
                        $height = $this->img->getHeight();
                        $showLink = false;
@@ -160,26 +200,34 @@ class ImagePage extends Article {
 
                                # "Download high res version" link below the image
                                $msg = wfMsgHtml('showbigimage', $width, $height, intval( $this->img->getSize()/1024 ) );
-                               if ( $width > $maxWidth ) {
-                                       $height = floor( $height * $maxWidth / $width );
-                                       $width  = $maxWidth;
-                               }
-                               if ( $height > $maxHeight ) {
-                                       $width = floor( $width * $maxHeight / $height );
-                                       $height = $maxHeight;
-                               }
-                               if ( $width != $this->img->getWidth() || $height != $this->img->getHeight() ) {
+
+                               # We'll show a thumbnail of this image
+                               if ( $width > $maxWidth || $height > $maxHeight ) {
+                                       # Calculate the thumbnail size.
+                                       # First case, the limiting factor is the width, not the height.
+                                       if ( $width / $height >= $maxWidth / $maxHeight ) {
+                                               $height = round( $height * $maxWidth / $width);
+                                               $width = $maxWidth;
+                                               # Note that $height <= $maxHeight now.
+                                       } else {
+                                               $newwidth = floor( $width * $maxHeight / $height);
+                                               $height = round( $height * $newwidth / $width );
+                                               $width = $newwidth;
+                                               # Note that $height <= $maxHeight now, but might not be identical
+                                               # because of rounding.
+                                       }
+
                                        if( $wgUseImageResize ) {
-                                               $thumbnail = $this->img->getThumbnail( $width );
+                                               $thumbnail = $this->img->getThumbnail( $width, -1, $wgGenerateThumbnailOnParse );
                                                if ( $thumbnail == null ) {
-                                                       $url = $img->getViewURL();
+                                                       $url = $this->img->getViewURL();
                                                } else {
                                                        $url = $thumbnail->getURL();
                                                }
                                        } else {
                                                # No resize ability? Show the full image, but scale
                                                # it down in the browser so it fits on the page.
-                                               $url = $img->getViewURL();
+                                               $url = $this->img->getViewURL();
                                        }
                                        $anchoropen  = "<a href=\"{$full_url}\">";
                                        $anchorclose = "</a><br />";
@@ -192,9 +240,50 @@ class ImagePage extends Article {
                                        $url = $this->img->getViewURL();
                                        $showLink = true;
                                }
+
+                               if ( $this->img->isMultipage() ) {
+                                       $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
+                               }
+
                                $wgOut->addHTML( '<div class="fullImageLink" id="file">' . $anchoropen .
                                     "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
                                     htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>' );
+
+                               if ( $this->img->isMultipage() ) {
+                                       $count = $this->img->pageCount();
+
+                                       if ( $page > 1 ) {
+                                               $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
+                                               $link = $sk->makeLinkObj( $this->mTitle, $label, 'page='. ($page-1) );
+                                               $this->img->selectPage( $page - 1 );
+                                               $thumb1 = $sk->makeThumbLinkObj( $this->img, $link, $label, 'none' );
+                                       } else {
+                                               $thumb1 = '';
+                                       }
+
+                                       if ( $page < $count ) {
+                                               $label = wfMsg( 'imgmultipagenext' );
+                                               $this->img->selectPage( $page + 1 );
+                                               $link = $sk->makeLinkObj( $this->mTitle, $label, 'page='. ($page+1) );
+                                               $thumb2 = $sk->makeThumbLinkObj( $this->img, $link, $label, 'none' );
+                                       } else {
+                                               $thumb2 = '';
+                                       }
+
+                                       $select = '<form name="pageselector" action="' . $this->img->getEscapeLocalUrl( '' ) . '" method="GET" onchange="document.pageselector.submit();">' ;
+                                       $select .= $wgOut->parse( wfMsg( 'imgmultigotopre' ), false ) .
+                                               ' <select id="pageselector" name="page">';
+                                       for ( $i=1; $i <= $count; $i++ ) {
+                                               $select .= Xml::option( $wgLang->formatNum( $i ), $i,
+                                                       $i == $page );
+                                       }
+                                       $select .= '</select>' . $wgOut->parse( wfMsg( 'imgmultigotopost' ), false ) .
+                                               '<input type="submit" value="' .
+                                               htmlspecialchars( wfMsg( 'imgmultigo' ) ) . '"></form>';
+
+                                       $wgOut->addHTML( '</td><td><div class="multipageimagenavbox">' .
+                                          "$select<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>" );
+                               }
                        } else {
                                #if direct link is allowed but it's not a renderable image, show an icon.
                                if ($this->img->isSafeFile()) {
@@ -204,7 +293,7 @@ class ImagePage extends Article {
                                        $icon->toHtml() .
                                        '</a></div>' );
                                }
-                               
+
                                $showLink = true;
                        }
 
@@ -214,12 +303,14 @@ class ImagePage extends Article {
                                $info = wfMsg( 'fileinfo',
                                        ceil($this->img->getSize()/1024.0),
                                        $this->img->getMimeType() );
-       
+
+                               global $wgContLang;
+                               $dirmark = $wgContLang->getDirMark();
                                if (!$this->img->isSafeFile()) {
                                        $warning = wfMsg( 'mediawarning' );
                                        $wgOut->addWikiText( <<<END
 <div class="fullMedia">
-<span class="dangerousLink">[[Media:$filename|$filename]]</span>
+<span class="dangerousLink">[[Media:$filename|$filename]]</span>$dirmark
 <span class="fileInfo"> ($info)</span>
 </div>
 
@@ -229,7 +320,7 @@ END
                                } else {
                                        $wgOut->addWikiText( <<<END
 <div class="fullMedia">
-[[Media:$filename|$filename]] <span class="fileInfo"> ($info)</span>
+[[Media:$filename|$filename]]$dirmark <span class="fileInfo"> ($info)</span>
 </div>
 END
                                                );
@@ -242,7 +333,7 @@ END
                } else {
                        # Image does not exist
 
-                       $title = Title::makeTitle( NS_SPECIAL, 'Upload' );
+                       $title = SpecialPage::getTitleFor( 'Upload' );
                        $link = $sk->makeKnownLinkObj($title, wfMsgHtml('noimage-linktext'),
                                'wpDestFile=' . urlencode( $this->img->getName() ) );
                        $wgOut->addHTML( wfMsgWikiHtml( 'noimage', $link ) );
@@ -257,7 +348,7 @@ END
                if ($wgRepositoryBaseUrl && !$wgFetchCommonsDescriptions) {
 
                        $sk = $wgUser->getSkin();
-                       $title = Title::makeTitle( NS_SPECIAL, 'Upload' );
+                       $title = SpecialPage::getTitleFor( 'Upload' );
                        $link = $sk->makeKnownLinkObj($title, wfMsgHtml('shareduploadwiki-linktext'),
                        array( 'wpDestFile' => urlencode( $this->img->getName() )));
                        $sharedtext .= " " . wfMsgWikiHtml('shareduploadwiki', $link);
@@ -266,10 +357,7 @@ END
                $wgOut->addHTML($sharedtext);
 
                if ($wgRepositoryBaseUrl && $wgFetchCommonsDescriptions) {
-                       require_once("HttpFunctions.php");
-                       $ur = ini_set('allow_url_fopen', true);
-                       $text = wfGetHTTP($url . '?action=render');
-                       ini_set('allow_url_fopen', $ur);
+                       $text = Http::get($url . '?action=render');
                        if ($text)
                                $this->mExtraDescription = $text;
                }
@@ -277,28 +365,35 @@ END
 
        function getUploadUrl() {
                global $wgServer;
-               $uploadTitle = Title::makeTitle( NS_SPECIAL, 'Upload' );
+               $uploadTitle = SpecialPage::getTitleFor( 'Upload' );
                return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img->getName() ) );
        }
 
-
-       function uploadLinksBox()
-       {
+       /**
+        * Print out the various links at the bottom of the image page, e.g. reupload,
+        * external editing (and instructions link) etc.
+        */
+       function uploadLinksBox() {
                global $wgUser, $wgOut;
 
-               if ($this->img->fromSharedDirectory)
+               if( $this->img->fromSharedDirectory )
                        return;
 
                $sk = $wgUser->getSkin();
-               $wgOut->addHTML( '<br /><ul>' );
-               if( $wgUser->isAllowed( 'reupload' ) ) {        
-                       $wgOut->addWikiText( "<li>\n<div>". wfMsg( 'uploadnewversion', $this->getUploadUrl() ) ."</div>\n</li>\n" );
-               }
-               $wgOut->addHTML( '<li>' );
-               $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle,
-                       wfMsg( 'edit-externally' ), "action=edit&externaledit=true&mode=file" ) );
-               $wgOut->addWikiText( '<div>' .  wfMsg('edit-externally-help') . '</div>' );
-               $wgOut->addHTML( '</li></ul>' );
+               
+               $wgOut->addHtml( '<br /><ul>' );
+               
+               # "Upload a new version of this file" link
+               if( $wgUser->isAllowed( 'reupload' ) ) {
+                       $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
+                       $wgOut->addHtml( "<li><div>{$ulink}</div></li>" );
+               }
+               
+               # External editing link
+               $elink = $sk->makeKnownLinkObj( $this->mTitle, wfMsg( 'edit-externally' ), 'action=edit&externaledit=true&mode=file' );
+               $wgOut->addHtml( '<li>' . $elink . '<div>' . wfMsgWikiHtml( 'edit-externally-help' ) . '</div></li>' );
+               
+               $wgOut->addHtml( '</ul>' );
        }
 
        function closeShowImage()
@@ -320,7 +415,7 @@ END
                $line = $this->img->nextHistoryLine();
 
                if ( $line ) {
-                       $list =& new ImageHistoryList( $sk );
+                       $list = new ImageHistoryList( $sk );
                        $s = $list->beginImageHistoryList() .
                                $list->imageHistoryLine( true, wfTimestamp(TS_MW, $line->img_timestamp),
                                        $this->mTitle->getDBkey(),  $line->img_user,
@@ -381,14 +476,15 @@ END
        {
                global $wgUser, $wgOut, $wgRequest;
 
-               $confirm = $wgRequest->getBool( 'wpConfirmB' );
+               $confirm = $wgRequest->wasPosted();
+               $reason = $wgRequest->getVal( 'wpReason' );
                $image = $wgRequest->getVal( 'image' );
                $oldimage = $wgRequest->getVal( 'oldimage' );
 
                # Only sysops can delete images. Previously ordinary users could delete
                # old revisions, but this is no longer the case.
                if ( !$wgUser->isAllowed('delete') ) {
-                       $wgOut->sysopRequired();
+                       $wgOut->permissionRequired( 'delete' );
                        return;
                }
                if ( $wgUser->isBlocked() ) {
@@ -403,7 +499,7 @@ END
                $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
                if ( ( !is_null( $image ) )
                  && ( '' == trim( $image ) ) ) {
-                       $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
+                       $wgOut->showFatalError( wfMsg( 'cannotdelete' ) );
                        return;
                }
 
@@ -412,9 +508,9 @@ END
                # Deleting old images doesn't require confirmation
                if ( !is_null( $oldimage ) || $confirm ) {
                        if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
-                               $this->doDelete();
+                               $this->doDelete( $reason );
                        } else {
-                               $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
+                               $wgOut->showFatalError( wfMsg( 'sessionfailure' ) );
                        }
                        return;
                }
@@ -429,88 +525,48 @@ END
                return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
        }
 
-       function doDelete()
-       {
-               global $wgOut, $wgUser, $wgContLang, $wgRequest;
-               global $wgUseSquid, $wgInternalServer, $wgPostCommitUpdateList;
+       /*
+        * Delete an image.
+        * @param $reason User provided reason for deletion.
+        */
+       function doDelete( $reason ) {
+               global $wgOut, $wgRequest, $wgUseSquid;
+               global $wgPostCommitUpdateList;
+
                $fname = 'ImagePage::doDelete';
 
-               $reason = $wgRequest->getVal( 'wpReason' );
                $oldimage = $wgRequest->getVal( 'oldimage' );
 
                $dbw =& wfGetDB( DB_MASTER );
 
                if ( !is_null( $oldimage ) ) {
                        if ( strlen( $oldimage ) < 16 ) {
-                               $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
+                               $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
                                return;
                        }
                        if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
-                               $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
+                               $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
                                return;
                        }
-
-                       # Invalidate description page cache
-                       $this->mTitle->invalidateCache();
-
-                       # Squid purging
-                       if ( $wgUseSquid ) {
-                               $urlArr = Array(
-                                       $wgInternalServer.wfImageArchiveUrl( $oldimage ),
-                                       $wgInternalServer.$this->mTitle->getFullURL()
-                               );
-                               wfPurgeSquidServers($urlArr);
+                       if ( !$this->doDeleteOldImage( $oldimage ) ) {
+                               return;
                        }
-                       $this->doDeleteOldImage( $oldimage );
-                       $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
                        $deleted = $oldimage;
                } else {
-                       $image = $this->mTitle->getDBkey();
-                       $dest = wfImageDir( $image );
-                       $archive = wfImageDir( $image );
-
-                       # Delete the image file if it exists; due to sync problems
-                       # or manual trimming sometimes the file will be missing.
-                       $targetFile = "{$dest}/{$image}";
-                       if( file_exists( $targetFile ) && ! @unlink( $targetFile ) ) {
+                       $ok = $this->img->delete( $reason );
+                       if( !$ok ) {
                                # If the deletion operation actually failed, bug out:
-                               $wgOut->fileDeleteError( $targetFile );
+                               $wgOut->showFileDeleteError( $this->img->getName() );
                                return;
                        }
-                       $dbw->delete( 'image', array( 'img_name' => $image ) );
-                       $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
-
-                       # Purge archive URLs from the squid
-                       $urlArr = Array();
-                       while ( $s = $dbw->fetchObject( $res ) ) {
-                               $this->doDeleteOldImage( $s->oi_archive_name );
-                               $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name );
-                       }
-
-                       # And also the HTML of all pages using this image
-                       $linksTo = $this->img->getLinksTo();
-                       if ( $wgUseSquid ) {
-                               $u = SquidUpdate::newFromTitles( $linksTo, $urlArr );
-                               array_push( $wgPostCommitUpdateList, $u );
-                       }
-
-                       $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
-
+                       
                        # Image itself is now gone, and database is cleaned.
                        # Now we remove the image description page.
-
+       
                        $article = new Article( $this->mTitle );
                        $article->doDeleteArticle( $reason ); # ignore errors
 
-                       # Invalidate parser cache and client cache for pages using this image
-                       # This is left until relatively late to reduce lock time
-                       Title::touchArray( $linksTo );
-
-                       /* Delete thumbnails and refresh image metadata cache */
-                       $this->img->purgeCache();
-
-
-                       $deleted = $image;
+                       $deleted = $this->img->getName();
                }
 
                $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
@@ -524,43 +580,36 @@ END
                $wgOut->returnToMain( false, $this->mTitle->getPrefixedText() );
        }
 
+       /**
+        * @return success
+        */
        function doDeleteOldImage( $oldimage )
        {
                global $wgOut;
 
-               $name = substr( $oldimage, 15 );
-               $archive = wfImageArchiveDir( $name );
-
-               # Delete the image if it exists. Sometimes the file will be missing
-               # due to manual intervention or weird sync problems; treat that
-               # condition gracefully and continue to delete the database entry.
-               # Also some records may end up with an empty oi_archive_name field
-               # if the original file was missing when a new upload was made;
-               # don't try to delete the directory then!
-               #
-               $targetFile = "{$archive}/{$oldimage}";
-               if( $oldimage != '' && file_exists( $targetFile ) && !@unlink( $targetFile ) ) {
+               $ok = $this->img->deleteOld( $oldimage, '' );
+               if( !$ok ) {
                        # If we actually have a file and can't delete it, throw an error.
-                       $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
+                       # Something went awry...
+                       $wgOut->showFileDeleteError( "$oldimage" );
                } else {
                        # Log the deletion
                        $log = new LogPage( 'delete' );
                        $log->addEntry( 'delete', $this->mTitle, wfMsg('deletedrevision',$oldimage) );
                }
+               return $ok;
        }
 
-       function revert()
-       {
+       function revert() {
                global $wgOut, $wgRequest, $wgUser;
-               global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
 
                $oldimage = $wgRequest->getText( 'oldimage' );
                if ( strlen( $oldimage ) < 16 ) {
-                       $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
+                       $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
                        return;
                }
                if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
-                       $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
+                       $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
                        return;
                }
 
@@ -569,18 +618,18 @@ END
                        return;
                }
                if( $wgUser->isAnon() ) {
-                       $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
+                       $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' );
                        return;
                }
                if ( ! $this->mTitle->userCanEdit() ) {
-                       $wgOut->sysopRequired();
+                       $wgOut->readOnlyPage( $this->getContent(), true );
                        return;
                }
                if ( $wgUser->isBlocked() ) {
                        return $this->blockedIPpage();
                }
                if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
-                       $wgOut->errorpage( 'internalerror', 'sessionfailure' );
+                       $wgOut->showErrorPage( 'internalerror', 'sessionfailure' );
                        return;
                }
                $name = substr( $oldimage, 15 );
@@ -589,8 +638,11 @@ END
                $archive = wfImageArchiveDir( $name );
                $curfile = "{$dest}/{$name}";
 
+               if ( !is_dir( $dest ) ) wfMkdirParents( $dest );
+               if ( !is_dir( $archive ) ) wfMkdirParents( $archive );
+
                if ( ! is_file( $curfile ) ) {
-                       $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
+                       $wgOut->showFileNotFoundError( htmlspecialchars( $curfile ) );
                        return;
                }
                $oldver = wfTimestampNow() . "!{$name}";
@@ -599,11 +651,12 @@ END
                $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage )  );
 
                if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
-                       $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
+                       $wgOut->showFileRenameError( $curfile, "${archive}/{$oldver}" );
                        return;
                }
                if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
-                       $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
+                       $wgOut->showFileCopyError( "${archive}/{$oldimage}", $curfile );
+                       return;
                }
 
                # Record upload and update metadata cache
@@ -619,10 +672,25 @@ END
        }
 
        function blockedIPpage() {
-               require_once( 'EditPage.php' );
                $edit = new EditPage( $this );
                return $edit->blockedIPpage();
        }
+       
+       /**
+        * Override handling of action=purge
+        */
+       function doPurge() {
+               $this->img = new Image( $this->mTitle );
+               if( $this->img->exists() ) {
+                       wfDebug( "ImagePage::doPurge purging " . $this->img->getName() . "\n" );
+                       $update = new HTMLCacheUpdate( $this->mTitle, 'imagelinks' );
+                       $update->doUpdate();
+                       $this->img->purgeCache();
+               } else {
+                       wfDebug( "ImagePage::doPurge no image\n" );
+               }
+               parent::doPurge();
+       }
 
 }
 
@@ -647,7 +715,7 @@ class ImageHistoryList {
        }
 
        function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description, $width, $height ) {
-               global $wgUser, $wgLang, $wgContLang, $wgTitle;
+               global $wgUser, $wgLang, $wgTitle, $wgContLang;
 
                $datetime = $wgLang->timeanddate( $timestamp, true );
                $del = wfMsg( 'deleteimg' );
@@ -684,14 +752,10 @@ class ImageHistoryList {
                                $dlink = $del;
                        }
                }
-               if ( 0 == $user ) {
-                       $userlink = $usertext;
-               } else {
-                       $userlink = $this->skin->makeLinkObj(
-                               Title::makeTitle( NS_USER, $usertext ),
-                               $usertext );
-               }
-               $nbytes = wfMsg( 'nbytes', $size );
+               
+               $userlink = $this->skin->userLink( $user, $usertext ) . $this->skin->userToolLinks( $user, $usertext );
+               $nbytes = wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
+                       $wgLang->formatNum( $size ) );
                $widthheight = wfMsg( 'widthheight', $width, $height );
                $style = $this->skin->getInternalLinkAttributes( $url, $datetime );