remove unused message
[lhc/web/wiklou.git] / includes / ImagePage.php
index e99e046..7e87090 100644 (file)
@@ -7,7 +7,7 @@
  *
  */
 if( !defined( 'MEDIAWIKI' ) )
-       die();
+       die( 1 );
 
 require_once( 'Image.php' );
 
@@ -17,95 +17,347 @@ require_once( 'Image.php' );
  */
 class ImagePage extends Article {
 
-       /* private */ var $img;  // Image object this page is shown for. Initilaized in openShowImage, not
-                                // available in doDelete etc.
+       /* 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->addSecondaryWikitext( $this->getContent() );
+       }
 
        function view() {
-               if ( Namespace::getImage() == $this->mTitle->getNamespace() ) {
+               global $wgOut, $wgShowEXIF;
+
+               $this->img = new Image( $this->mTitle );
+
+               if( $this->mTitle->getNamespace() == NS_IMAGE  ) {
+                       if ($wgShowEXIF && $this->img->exists()) {
+                               $exif = $this->img->getExifData();
+                               $showmeta = count($exif) ? true : false;
+                       } else {
+                               $exif = false;
+                               $showmeta = false;
+                       }
+
+                       if ($this->img->exists())
+                               $wgOut->addHTML($this->showTOC($showmeta));
+
                        $this->openShowImage();
-               }
 
-               Article::view();
-               
-               # If the article we've just shown is in the "Image" namespace,
-               # follow it with the history list and link list for the image
-               # it describes.
+                       # No need to display noarticletext, we use our own message, output in openShowImage()
+                       if( $this->getID() ) {
+                               Article::view();
+                       } else {
+                               # Just need to set the right headers
+                               $wgOut->setArticleFlag( true );
+                               $wgOut->setRobotpolicy( 'index,follow' );
+                               $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
+                               $this->viewUpdates();
+                       }
+
+                       # 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>' );
+                       }
 
-               if ( Namespace::getImage() == $this->mTitle->getNamespace() ) {
                        $this->closeShowImage();
                        $this->imageHistory();
                        $this->imageLinks();
+                       if( $exif ) {
+                               global $wgStylePath;
+                               $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\"></script>\n" .
+                                       "<script type=\"text/javascript\">attachMetadataToggle('mw_metadata', '$expand', '$collapse');</script>\n" );
+                       }
+               } else {
+                       Article::view();
                }
        }
 
-       function openShowImage()
-       {
-               global $wgOut, $wgUser, $wgImageLimits, $wgRequest, 
-                      $wgUseImageResize, $wgRepositoryBaseUrl;
-               $this->img  = Image::newFromTitle( $this->mTitle );
-               $full_url  = $this->img->getViewURL();
-               $anchoropen = '';
-               $anchorclose = '';
-               if ( $wgUseImageResize ) {
-                       if( $wgUser->getOption( 'imagesize' ) == '' ) {
-                               $sizeSel = User::getDefaultOption( 'imagesize' );
-                       } else {
-                               $sizeSel = IntVal( $wgUser->getOption( 'imagesize' ) );
+       /**
+        * Create the TOC
+        *
+        * @access private
+        *
+        * @param bool $metadata Whether or not to show the metadata link
+        * @return string
+        */
+       function showTOC( $metadata ) {
+               global $wgLang;
+               $r = '<ul id="filetoc">
+                       <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>' .
+                       ($metadata ? '<li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>' : '') . '
+               </ul>';
+               return $r;
+       }
+
+       /**
+        * Make a table with metadata to be shown in the output page.
+        *
+        * @access private
+        *
+        * @param array $exif The array containing the EXIF data
+        * @return string
+        */
+       function makeMetadataTable( $exif ) {
+               $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 );
+                       $msg = wfMsg( "exif-$tag" );
+                       $class = "exif-$tag";
+                       if( !in_array( $tag, $visibleFields ) ) {
+                               $class .= ' collapsable';
                        }
-                       if( !isset( $wgImageLimits[$sizeSel] ) ) {
-                               $sizeSel = User::getDefaultOption( 'imagesize' );
+                       $r .= "|- class=\"$class\"\n";
+                       $r .= "!| $msg\n";
+                       $r .= "|| $v\n";
+               }
+               $r .= '|}';
+               return $r;
+       }
+
+       /**
+        * Get a list of EXIF metadata items which should be displayed when
+        * the metadata table is collapsed.
+        *
+        * @return array of strings
+        * @access private
+        */
+       function visibleMetadataFields() {
+               $fields = array();
+               $lines = explode( "\n", wfMsgForContent( 'metadata-fields' ) );
+               foreach( $lines as $line ) {
+                       if( preg_match( '/^\\*\s*(.*?)\s*$/', $line, $matches ) ) {
+                               $fields[] = $matches[1];
                        }
-                       $max = $wgImageLimits[$sizeSel];
-                       $maxWidth = $max[0];
-                       $maxHeight = $max[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();
+       }
+
+       function openShowImage() {
+               global $wgOut, $wgUser, $wgImageLimits, $wgRequest;
+               global $wgUseImageResize, $wgGenerateThumbnailOnParse;
+
+               $full_url  = $this->img->getURL();
+               $anchoropen = '';
+               $anchorclose = '';
+
+               if( $wgUser->getOption( 'imagesize' ) == '' ) {
+                       $sizeSel = User::getDefaultOption( 'imagesize' );
+               } else {
+                       $sizeSel = intval( $wgUser->getOption( 'imagesize' ) );
+               }
+               if( !isset( $wgImageLimits[$sizeSel] ) ) {
+                       $sizeSel = User::getDefaultOption( 'imagesize' );
+               }
+               $max = $wgImageLimits[$sizeSel];
+               $maxWidth = $max[0];
+               $maxHeight = $max[1];
+               $sk = $wgUser->getSkin();
 
                if ( $this->img->exists() ) {
+                       # image
+                       $width = $this->img->getWidth();
+                       $height = $this->img->getHeight();
+                       $showLink = false;
 
-                       $sk = $wgUser->getSkin();
-                       
-                       if ( $this->img->getType() != '' ) {
+                       if ( $this->img->allowInlineDisplay() and $width and $height) {
                                # image
-                               $width = $this->img->getWidth();
-                               $height = $this->img->getHeight();
-                               $msg = wfMsg('showbigimage', $width, $height, intval( $this->img->getSize()/1024 ) );
-                               if ( $width > $maxWidth && $wgUseImageResize ) {
-                                       $height = floor( $height * $maxWidth / $width );
-                                       $width  = $maxWidth;
-                               } 
-                               if ( $height > $maxHeight && $wgUseImageResize ) {
-                                       $width = floor( $width * $maxHeight / $height );
-                                       $height = $maxHeight;
-                               }
-                               if ( $width != $this->img->getWidth() || $height != $this->img->getHeight() ) {
-                                       $url = $this->img->createThumb( $width );
+
+                               # "Download high res version" link below the image
+                               $msg = wfMsgHtml('showbigimage', $width, $height, intval( $this->img->getSize()/1024 ) );
+
+                               # 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, -1, $wgGenerateThumbnailOnParse );
+                                               if ( $thumbnail == null ) {
+                                                       $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 = $this->img->getViewURL();
+                                       }
                                        $anchoropen  = "<a href=\"{$full_url}\">";
-                                       $anchorclose = "<br>{$msg}</a>";
+                                       $anchorclose = "</a><br />";
+                                       if( $this->img->mustRender() ) {
+                                               $showLink = true;
+                                       } else {
+                                               $anchorclose .= "\n$anchoropen{$msg}</a>";
+                                       }
                                } else {
-                                       $url = $full_url;
+                                       $url = $this->img->getViewURL();
+                                       $showLink = true;
                                }
-                               $s = '<div class="fullImageLink">' . $anchoropen .
+                               $wgOut->addHTML( '<div class="fullImageLink" id="file">' . $anchoropen .
                                     "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
-                                    htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>';
+                                    htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>' );
                        } else {
-                               $s = "<div class=\"fullMedia\">" . $sk->makeMediaLink( $this->img->getName(),'' ) . '</div>';
+                               #if direct link is allowed but it's not a renderable image, show an icon.
+                               if ($this->img->isSafeFile()) {
+                                       $icon= $this->img->iconThumb();
+
+                                       $wgOut->addHTML( '<div class="fullImageLink" id="file"><a href="' . $full_url . '">' .
+                                       $icon->toHtml() .
+                                       '</a></div>' );
+                               }
+
+                               $showLink = true;
                        }
-                       $wgOut->addHTML( $s );
-                       if($this->img->fromSharedDirectory) {
-                               $sharedtext="<div class=\"sharedUploadNotice\">" . wfMsg("sharedupload");
-                               if($wgRepositoryBaseUrl) {
-                                       $sharedtext .= " ". wfMsg("shareduploadwiki",$wgRepositoryBaseUrl . urlencode($this->mTitle->getDBkey()));
+
+
+                       if ($showLink) {
+                               $filename = wfEscapeWikiText( $this->img->getName() );
+                               $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>$dirmark
+<span class="fileInfo"> ($info)</span>
+</div>
+
+<div class="mediaWarning">$warning</div>
+END
+                                               );
+                               } else {
+                                       $wgOut->addWikiText( <<<END
+<div class="fullMedia">
+[[Media:$filename|$filename]]$dirmark <span class="fileInfo"> ($info)</span>
+</div>
+END
+                                               );
                                }
-                               $sharedtext.="</div>";
-                               $wgOut->addWikiText($sharedtext);
                        }
+
+                       if($this->img->fromSharedDirectory) {
+                               $this->printSharedImageText();
+                       }
+               } else {
+                       # Image does not exist
+
+                       $title = Title::makeTitle( NS_SPECIAL, 'Upload' );
+                       $link = $sk->makeKnownLinkObj($title, wfMsgHtml('noimage-linktext'),
+                               'wpDestFile=' . urlencode( $this->img->getName() ) );
+                       $wgOut->addHTML( wfMsgWikiHtml( 'noimage', $link ) );
                }
        }
-       
+
+       function printSharedImageText() {
+               global $wgRepositoryBaseUrl, $wgFetchCommonsDescriptions, $wgOut, $wgUser;
+
+               $url = $wgRepositoryBaseUrl . urlencode($this->mTitle->getDBkey());
+               $sharedtext = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml("sharedupload");
+               if ($wgRepositoryBaseUrl && !$wgFetchCommonsDescriptions) {
+
+                       $sk = $wgUser->getSkin();
+                       $title = Title::makeTitle( NS_SPECIAL, 'Upload' );
+                       $link = $sk->makeKnownLinkObj($title, wfMsgHtml('shareduploadwiki-linktext'),
+                       array( 'wpDestFile' => urlencode( $this->img->getName() )));
+                       $sharedtext .= " " . wfMsgWikiHtml('shareduploadwiki', $link);
+               }
+               $sharedtext .= "</div>";
+               $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);
+                       if ($text)
+                               $this->mExtraDescription = $text;
+               }
+       }
+
+       function getUploadUrl() {
+               global $wgServer;
+               $uploadTitle = Title::makeTitle( NS_SPECIAL, 'Upload' );
+               return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img->getName() ) );
+       }
+
+       /**
+        * 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 )
+                       return;
+
+               $sk = $wgUser->getSkin();
+               
+               $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()
        {
                # For overloading
+
        }
 
        /**
@@ -114,43 +366,54 @@ class ImagePage extends Article {
         */
        function imageHistory()
        {
-               global $wgUser, $wgOut;
+               global $wgUser, $wgOut, $wgUseExternalEditor;
 
                $sk = $wgUser->getSkin();
 
                $line = $this->img->nextHistoryLine();
 
                if ( $line ) {
-                       $list =& new ImageHistoryList( $sk );
+                       $list = new ImageHistoryList( $sk );
                        $s = $list->beginImageHistoryList() .
-                               $list->imageHistoryLine( true, $line->img_timestamp,
+                               $list->imageHistoryLine( true, wfTimestamp(TS_MW, $line->img_timestamp),
                                        $this->mTitle->getDBkey(),  $line->img_user,
-                                       $line->img_user_text, $line->img_size, $line->img_description );
+                                       $line->img_user_text, $line->img_size, $line->img_description,
+                                       $line->img_width, $line->img_height
+                               );
 
                        while ( $line = $this->img->nextHistoryLine() ) {
                                $s .= $list->imageHistoryLine( false, $line->img_timestamp,
-                               $line->oi_archive_name, $line->img_user,
-                               $line->img_user_text, $line->img_size, $line->img_description );
+                                       $line->oi_archive_name, $line->img_user,
+                                       $line->img_user_text, $line->img_size, $line->img_description,
+                                       $line->img_width, $line->img_height
+                               );
                        }
                        $s .= $list->endImageHistoryList();
                } else { $s=''; }
                $wgOut->addHTML( $s );
+
+               # Exist check because we don't want to show this on pages where an image
+               # doesn't exist along with the noimage message, that would suck. -ævar
+               if( $wgUseExternalEditor && $this->img->exists() ) {
+                       $this->uploadLinksBox();
+               }
+
        }
 
        function imageLinks()
        {
                global $wgUser, $wgOut;
 
-               $wgOut->addHTML( '<h2>' . wfMsg( 'imagelinks' ) . "</h2>\n" );
+               $wgOut->addHTML( '<h2 id="filelinks">' . wfMsg( 'imagelinks' ) . "</h2>\n" );
 
                $dbr =& wfGetDB( DB_SLAVE );
                $page = $dbr->tableName( 'page' );
                $imagelinks = $dbr->tableName( 'imagelinks' );
-               
+
                $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
-                 $dbr->addQuotes( $this->mTitle->getDBkey() ) . " AND il_from=page_id"
-                 . " LIMIT 500"; # quickie emergency brake
-               $res = $dbr->query( $sql, DB_SLAVE, "Article::imageLinks" );
+                 $dbr->addQuotes( $this->mTitle->getDBkey() ) . " AND il_from=page_id";
+               $sql = $dbr->limitResult($sql, 500, 0);
+               $res = $dbr->query( $sql, "ImagePage::imageLinks" );
 
                if ( 0 == $dbr->numRows( $res ) ) {
                        $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
@@ -171,16 +434,20 @@ class ImagePage extends Article {
        {
                global $wgUser, $wgOut, $wgRequest;
 
-               $confirm = $wgRequest->getBool( 'wpConfirm' );
+               $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 
+
+               # 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();
                        return;
                }
+               if ( $wgUser->isBlocked() ) {
+                       return $this->blockedIPpage();
+               }
                if ( wfReadOnly() ) {
                        $wgOut->readOnlyPage();
                        return;
@@ -190,16 +457,22 @@ class ImagePage extends Article {
                $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
                if ( ( !is_null( $image ) )
                  && ( '' == trim( $image ) ) ) {
-                       $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
+                       $wgOut->showFatalError( wfMsg( 'cannotdelete' ) );
                        return;
                }
-               
+
+               $this->img  = new Image( $this->mTitle );
+
                # Deleting old images doesn't require confirmation
                if ( !is_null( $oldimage ) || $confirm ) {
-                       $this->doDelete();
+                       if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
+                               $this->doDelete( $reason );
+                       } else {
+                               $wgOut->showFatalError( wfMsg( 'sessionfailure' ) );
+                       }
                        return;
                }
-               
+
                if ( !is_null( $image ) ) {
                        $q = '&image=' . urlencode( $image );
                } else if ( !is_null( $oldimage ) ) {
@@ -210,132 +483,91 @@ class ImagePage extends Article {
                return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
        }
 
-       function doDelete()
-       {
-               global $wgOut, $wgUser, $wgContLang, $wgRequest;
-               global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
+       /*
+        * 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' );
-               $image = $wgRequest->getVal( 'image' );
                $oldimage = $wgRequest->getVal( 'oldimage' );
-               
+
                $dbw =& wfGetDB( DB_MASTER );
 
                if ( !is_null( $oldimage ) ) {
-                       # Squid purging
-                       if ( $wgUseSquid ) {
-                               $urlArr = Array(
-                                       $wgInternalServer.wfImageArchiveUrl( $oldimage )
-                               );
-                               wfPurgeSquidServers($urlArr);
+                       if ( strlen( $oldimage ) < 16 ) {
+                               $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
+                               return;
+                       }
+                       if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
+                               $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
+                               return;
+                       }
+                       if ( !$this->doDeleteOldImage( $oldimage ) ) {
+                               return;
                        }
-                       $this->doDeleteOldImage( $oldimage );
-                       $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
                        $deleted = $oldimage;
                } else {
-                       if ( is_null ( $image ) ) {
-                               $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 ) );
-                                               
-                       # Squid purging
-                       if ( $wgUseSquid ) {
-                               $urlArr = Array(
-                                       $wgInternalServer . Image::wfImageUrl( $image )
-                               );
-                               wfPurgeSquidServers($urlArr);
-                       }
-                       
-
-                       $urlArr = Array();
-                       while ( $s = $dbw->fetchObject( $res ) ) {
-                               $this->doDeleteOldImage( $s->oi_archive_name );
-                               $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name );
-                       }       
-                       
-                       # Squid purging, part II
-                       if ( $wgUseSquid ) {
-                               /* this needs to be done after LinksUpdate */
-                               $u = new SquidUpdate( $urlArr );
-                               array_push( $wgDeferredUpdateList, $u );
-                       }
                        
-                       $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
-
                        # Image itself is now gone, and database is cleaned.
                        # Now we remove the image description page.
-
-                       $nt = Title::newFromText( $wgContLang->getNsText( Namespace::getImage() ) . ":" . $image );
-                       $article = new Article( $nt );
+       
+                       $article = new Article( $this->mTitle );
                        $article->doDeleteArticle( $reason ); # ignore errors
 
-                       $deleted = $image;
+                       $deleted = $this->img->getName();
                }
 
                $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
                $wgOut->setRobotpolicy( 'noindex,nofollow' );
 
-               $sk = $wgUser->getSkin();
-               $loglink = $sk->makeKnownLink( $wgContLang->getNsText(
-                 Namespace::getWikipedia() ) .
-                 ':' . wfMsg( 'dellogpage' ), wfMsg( 'deletionlog' ) );
-
+               $loglink = '[[Special:Log/delete|' . wfMsg( 'deletionlog' ) . ']]';
                $text = wfMsg( 'deletedtext', $deleted, $loglink );
 
-               $wgOut->addHTML( '<p>' . $text . "</p>\n" );
-               $wgOut->returnToMain( false );
+               $wgOut->addWikiText( $text );
+
+               $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()
-       {
-               global $wgOut, $wgRequest;
-               global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
+       function revert() {
+               global $wgOut, $wgRequest, $wgUser;
 
                $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;
                }
 
@@ -343,57 +575,94 @@ class ImagePage extends Article {
                        $wgOut->readOnlyPage();
                        return;
                }
+               if( $wgUser->isAnon() ) {
+                       $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' );
+                       return;
+               }
                if ( ! $this->mTitle->userCanEdit() ) {
                        $wgOut->sysopRequired();
                        return;
                }
+               if ( $wgUser->isBlocked() ) {
+                       return $this->blockedIPpage();
+               }
+               if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
+                       $wgOut->showErrorPage( 'internalerror', 'sessionfailure' );
+                       return;
+               }
                $name = substr( $oldimage, 15 );
 
                $dest = wfImageDir( $name );
                $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}";
-               
+
                $dbr =& wfGetDB( DB_SLAVE );
-               $size = $dbr->selectField( 'oldimage', 'oi_size', 'oi_archive_name=\'' .
-                 $dbr->strencode( $oldimage ) . "'" );
+               $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 );
-               }
-               wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
-               # Squid purging
-               if ( $wgUseSquid ) {
-                       $urlArr = Array(
-                               $wgInternalServer.wfImageArchiveUrl( $name ),
-                               $wgInternalServer . Image::wfImageUrl( $name )
-                       );
-                       wfPurgeSquidServers($urlArr);
+                       $wgOut->showFileCopyError( "${archive}/{$oldimage}", $curfile );
+                       return;
                }
 
+               # Record upload and update metadata cache
+               $img = Image::newFromName( $name );
+               $img->recordUpload( $oldver, wfMsg( "reverted" ) );
+
                $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
                $wgOut->setRobotpolicy( 'noindex,nofollow' );
                $wgOut->addHTML( wfMsg( 'imagereverted' ) );
-               $wgOut->returnToMain( false );
+
+               $descTitle = $img->getTitle();
+               $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
+       }
+
+       function blockedIPpage() {
+               $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();
        }
+
 }
 
+/**
+ * @todo document
+ * @package MediaWiki
+ */
 class ImageHistoryList {
        function ImageHistoryList( &$skin ) {
                $this->skin =& $skin;
        }
-       
+
        function beginImageHistoryList() {
-               $s = "\n<h2>" . wfMsg( 'imghistory' ) . "</h2>\n" .
+               $s = "\n<h2 id=\"filehistory\">" . wfMsg( 'imghistory' ) . "</h2>\n" .
                  "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
                return $s;
        }
@@ -403,8 +672,8 @@ class ImageHistoryList {
                return $s;
        }
 
-       function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
-               global $wgUser, $wgLang, $wgContLang, $wgTitle;
+       function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description, $width, $height ) {
+               global $wgUser, $wgLang, $wgTitle, $wgContLang;
 
                $datetime = $wgLang->timeanddate( $timestamp, true );
                $del = wfMsg( 'deleteimg' );
@@ -412,7 +681,7 @@ class ImageHistoryList {
                $cur = wfMsg( 'cur' );
 
                if ( $iscur ) {
-                       $url = Image::wfImageUrl( $img );
+                       $url = Image::imageUrl( $img );
                        $rlink = $cur;
                        if ( $wgUser->isAllowed('delete') ) {
                                $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
@@ -426,11 +695,13 @@ class ImageHistoryList {
                } else {
                        $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
                        if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
-                               $rlink = $this->skin->makeKnownLink( $wgTitle->getPrefixedText(),
+                               $token = urlencode( $wgUser->editToken( $img ) );
+                               $rlink = $this->skin->makeKnownLinkObj( $wgTitle,
                                           wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
-                                          urlencode( $img ) );
-                               $dlink = $this->skin->makeKnownLink( $wgTitle->getPrefixedText(),
-                                          $del, 'action=delete&oldimage=' . urlencode( $img ) );
+                                          urlencode( $img ) . "&wpEditToken=$token" );
+                               $dlink = $this->skin->makeKnownLinkObj( $wgTitle,
+                                          $del, 'action=delete&oldimage=' . urlencode( $img ) .
+                                          "&wpEditToken=$token" );
                        } else {
                                # Having live active links for non-logged in users
                                # means that bots and spiders crawling our site can
@@ -439,22 +710,16 @@ class ImageHistoryList {
                                $dlink = $del;
                        }
                }
-               if ( 0 == $user ) {
-                       $userlink = $usertext;
-               } else {
-                       $userlink = $this->skin->makeLink( $wgContLang->getNsText( Namespace::getUser() ) .
-                                      ':'.$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 );
 
-               $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
-                 . " . . {$userlink} ({$nbytes})";
+               $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a> . . {$userlink} . . {$widthheight} ({$nbytes})";
 
-               if ( '' != $description && '*' != $description ) {
-                       $sk=$wgUser->getSkin();
-                       $s .= $wgContLang->emphasize(' (' . $sk->formatComment($description,$wgTitle) . ')');
-               }
+               $s .= $this->skin->commentBlock( $description, $wgTitle );
                $s .= "</li>\n";
                return $s;
        }