Revert r23471, r23474: restore Special:Version to working, non-ugly state
[lhc/web/wiklou.git] / includes / ImagePage.php
index 2e6e94e..e31ca15 100644 (file)
@@ -10,12 +10,22 @@ if( !defined( 'MEDIAWIKI' ) )
 
 /**
  * Special handling for image description pages
+ *
+ * @addtogroup Media
  */
 class ImagePage extends Article {
 
        /* private */ var $img;  // Image object this page is shown for
        var $mExtraDescription = false;
 
+       function __construct( $title ) {
+               parent::__construct( $title );
+               $this->img = wfFindFile( $this->mTitle );
+               if ( !$this->img ) {
+                       $this->img = wfLocalFile( $this->mTitle );
+               }
+       }
+
        /**
         * Handler for action=render
         * Include body text only; none of the image extras
@@ -29,8 +39,6 @@ class ImagePage extends Article {
        function view() {
                global $wgOut, $wgShowEXIF, $wgRequest, $wgUser;
 
-               $this->img = new Image( $this->mTitle );
-
                $diff = $wgRequest->getVal( 'diff' );
                $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
 
@@ -64,7 +72,7 @@ class ImagePage extends Article {
                # Show shared description, if needed
                if ( $this->mExtraDescription ) {
                        $fol = wfMsg( 'shareddescriptionfollows' );
-                       if( $fol != '-' ) {
+                       if( $fol != '-' && !wfEmptyMsg( 'shareddescriptionfollows', $fol ) ) {
                                $wgOut->addWikiText( $fol );
                        }
                        $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription . '</div>' );
@@ -158,7 +166,7 @@ class ImagePage extends Article {
         * shared upload server if possible.
         */
        function getContent() {
-               if( $this->img && $this->img->fromSharedDirectory && 0 == $this->getID() ) {
+               if( $this->img && !$this->img->isLocal() && 0 == $this->getID() ) {
                        return '';
                }
                return Article::getContent();
@@ -166,15 +174,19 @@ class ImagePage extends Article {
 
        function openShowImage() {
                global $wgOut, $wgUser, $wgImageLimits, $wgRequest, $wgLang;
-               global $wgUseImageResize, $wgGenerateThumbnailOnParse;
 
                $full_url  = $this->img->getURL();
-               $anchoropen = '';
-               $anchorclose = '';
+               $linkAttribs = false;
                $sizeSel = intval( $wgUser->getOption( 'imagesize') );
-
                if( !isset( $wgImageLimits[$sizeSel] ) ) {
                        $sizeSel = User::getDefaultOption( 'imagesize' );
+
+                       // The user offset might still be incorrect, specially if
+                       // $wgImageLimits got changed (see bug #8858).
+                       if( !isset( $wgImageLimits[$sizeSel] ) ) {
+                               // Default to the first offset in $wgImageLimits
+                               $sizeSel = 0;
+                       }
                }
                $max = $wgImageLimits[$sizeSel];
                $maxWidth = $max[0];
@@ -184,21 +196,27 @@ class ImagePage extends Article {
                if ( $this->img->exists() ) {
                        # image
                        $page = $wgRequest->getIntOrNull( 'page' );
-                       if ( ! is_null( $page ) ) {
-                               $this->img->selectPage( $page );
-                       } else {
+                       if ( is_null( $page ) ) {
+                               $params = array();
                                $page = 1;
+                       } else {
+                               $params = array( 'page' => $page );
                        }
-                       $width = $this->img->getWidth();
-                       $height = $this->img->getHeight();
+                       $width_orig = $this->img->getWidth();
+                       $width = $width_orig;
+                       $height_orig = $this->img->getHeight();
+                       $height = $height_orig;
+                       $mime = $this->img->getMimeType();
                        $showLink = false;
+                       $linkAttribs = array( 'href' => $full_url );
+
+      wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this , &$wgOut ) )        ;
 
                        if ( $this->img->allowInlineDisplay() and $width and $height) {
                                # image
 
                                # "Download high res version" link below the image
-                               $msg = wfMsgHtml('showbigimage', $width, $height, intval( $this->img->getSize()/1024 ) );
-
+                               $msgsize = wfMsgHtml('file-info-size', $width_orig, $height_orig, $sk->formatSize( $this->img->getSize() ), $mime );
                                # We'll show a thumbnail of this image
                                if ( $width > $maxWidth || $height > $maxHeight ) {
                                        # Calculate the thumbnail size.
@@ -214,61 +232,68 @@ class ImagePage extends Article {
                                                # 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 = "</a><br />";
-                                       if( $this->img->mustRender() ) {
-                                               $showLink = true;
-                                       } else {
-                                               $anchorclose .= "\n$anchoropen{$msg}</a>";
-                                       }
+                                       $msgbig  = wfMsgHtml( 'show-big-image' );
+                                       $msgsmall = wfMsgExt( 'show-big-image-thumb',
+                                               array( 'parseinline' ), $width, $height );
                                } else {
-                                       $url = $this->img->getViewURL();
+                                       # Image is small enough to show full size on image page
+                                       $msgbig = htmlspecialchars( $this->img->getName() );
+                                       $msgsmall = wfMsgExt( 'file-nohires', array( 'parseinline' ) );
+                               }
+
+                               $params['width'] = $width;
+                               $thumbnail = $this->img->transform( $params );
+
+                               $anchorclose = "<br />";
+                               if( $this->img->mustRender() ) {
                                        $showLink = true;
+                               } else {
+                                       $anchorclose .= 
+                                               $msgsmall .
+                                               '<br />' . Xml::tags( 'a', $linkAttribs,  $msgbig ) . ' ' . $msgsize;
                                }
 
                                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( $this->img->getTitle()->getPrefixedText() ).'" />' . $anchorclose . '</div>' );
+                               $imgAttribs = array(
+                                       'border' => 0,
+                                       'alt' => $this->img->getTitle()->getPrefixedText()
+                               );
+
+                               if ( $thumbnail ) {
+                                       $wgOut->addHTML( '<div class="fullImageLink" id="file">' . 
+                                               $thumbnail->toHtml( $imgAttribs, $linkAttribs ) .
+                                               $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' );
+                                               $link = $sk->makeKnownLinkObj( $this->mTitle, $label, 'page='. ($page-1) );
+                                               $thumb1 = $sk->makeThumbLinkObj( $this->mTitle, $this->img, $link, $label, 'none', 
+                                                       array( 'page' => $page - 1 ) );
                                        } 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' );
+                                               $link = $sk->makeKnownLinkObj( $this->mTitle, $label, 'page='. ($page+1) );
+                                               $thumb2 = $sk->makeThumbLinkObj( $this->mTitle, $this->img, $link, $label, 'none', 
+                                                       array( 'page' => $page + 1 ) );
                                        } else {
                                                $thumb2 = '';
                                        }
 
-                                       $select = '<form name="pageselector" action="' . $this->img->getEscapeLocalUrl( '' ) . '" method="GET" onchange="document.pageselector.submit();">' ;
+                                       global $wgScript;
+                                       $select = '<form name="pageselector" action="' . 
+                                               htmlspecialchars( $wgScript ) .
+                                               '" method="get" onchange="document.pageselector.submit();">' .
+                                               Xml::hidden( 'title', $this->getTitle()->getPrefixedDbKey() );
                                        $select .= $wgOut->parse( wfMsg( 'imgmultigotopre' ), false ) .
                                                ' <select id="pageselector" name="page">';
                                        for ( $i=1; $i <= $count; $i++ ) {
@@ -280,7 +305,7 @@ class ImagePage extends Article {
                                                htmlspecialchars( wfMsg( 'imgmultigo' ) ) . '"></form>';
 
                                        $wgOut->addHTML( '</td><td><div class="multipageimagenavbox">' .
-                                          "$select<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>" );
+                                               "$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.
@@ -297,41 +322,42 @@ class ImagePage extends Article {
 
 
                        if ($showLink) {
-                               $filename = wfEscapeWikiText( $this->img->getName() );
-                               // Hacky workaround: for some reason we use the incorrect MIME type
-                               // image/svg for SVG.  This should be fixed internally, but at least
-                               // make the displayed type right.
-                               $mime = $this->img->getMimeType();
+                               // Workaround for incorrect MIME type on SVGs uploaded in previous versions
                                if ($mime == 'image/svg') $mime = 'image/svg+xml';
 
-                               $info = wfMsg( 'fileinfo',
-                                       ceil($this->img->getSize()/1024.0),
-                                       $mime );
+                               $filename = wfEscapeWikiText( $this->img->getName() );
+                               $info = wfMsg( 'file-info', $sk->formatSize( $this->img->getSize() ), $mime );
+                               $infores = '';
+
+                               // Check for MIME type. Other types may have more information in the future.
+                               if (substr($mime,0,9) == 'image/svg' ) {
+                                       $infores = wfMsg('file-svg', $width_orig, $height_orig ) . '<br />';
+                               }
 
                                global $wgContLang;
                                $dirmark = $wgContLang->getDirMark();
                                if (!$this->img->isSafeFile()) {
                                        $warning = wfMsg( 'mediawarning' );
-                                       $wgOut->addWikiText( <<<END
-<div class="fullMedia">
+                                       $wgOut->addWikiText( <<<EOT
+<div class="fullMedia">$infores
 <span class="dangerousLink">[[Media:$filename|$filename]]</span>$dirmark
-<span class="fileInfo"> ($info)</span>
+<span class="fileInfo"> $info</span>
 </div>
 
 <div class="mediaWarning">$warning</div>
-END
+EOT
                                                );
                                } else {
-                                       $wgOut->addWikiText( <<<END
-<div class="fullMedia">
-[[Media:$filename|$filename]]$dirmark <span class="fileInfo"> ($info)</span>
+                                       $wgOut->addWikiText( <<<EOT
+<div class="fullMedia">$infores
+[[Media:$filename|$filename]]$dirmark <span class="fileInfo"> $info</span>
 </div>
-END
+EOT
                                                );
                                }
                        }
 
-                       if($this->img->fromSharedDirectory) {
+                       if(!$this->img->isLocal()) {
                                $this->printSharedImageText();
                        }
                } else {
@@ -345,25 +371,21 @@ END
        }
 
        function printSharedImageText() {
-               global $wgRepositoryBaseUrl, $wgFetchCommonsDescriptions, $wgOut, $wgUser;
-
-               $url = $wgRepositoryBaseUrl . urlencode($this->mTitle->getDBkey());
-               $sharedtext = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml("sharedupload");
-               if ($wgRepositoryBaseUrl && !$wgFetchCommonsDescriptions) {
+               global $wgOut, $wgUser;
 
+               $descUrl = $this->img->getDescriptionUrl();
+               $descText = $this->img->getDescriptionText();
+               $s = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml("sharedupload");
+               if ( $descUrl && !$descText) {
                        $sk = $wgUser->getSkin();
-                       $title = SpecialPage::getTitleFor( 'Upload' );
-                       $link = $sk->makeKnownLinkObj($title, wfMsgHtml('shareduploadwiki-linktext'),
-                       array( 'wpDestFile' => urlencode( $this->img->getName() )));
-                       $sharedtext .= " " . wfMsgWikiHtml('shareduploadwiki', $link);
+                       $link = $sk->makeExternalLink( $descUrl, wfMsg('shareduploadwiki-linktext') );
+                       $s .= " " . wfMsgWikiHtml('shareduploadwiki', $link);
                }
-               $sharedtext .= "</div>";
-               $wgOut->addHTML($sharedtext);
+               $s .= "</div>";
+               $wgOut->addHTML($s);
 
-               if ($wgRepositoryBaseUrl && $wgFetchCommonsDescriptions) {
-                       $text = Http::get($url . '?action=render');
-                       if ($text)
-                               $this->mExtraDescription = $text;
+               if ( $descText ) {
+                       $this->mExtraDescription = $descText;
                }
        }
 
@@ -380,7 +402,7 @@ END
        function uploadLinksBox() {
                global $wgUser, $wgOut;
 
-               if( $this->img->fromSharedDirectory )
+               if( !$this->img->isLocal() )
                        return;
 
                $sk = $wgUser->getSkin();
@@ -388,9 +410,9 @@ END
                $wgOut->addHtml( '<br /><ul>' );
                
                # "Upload a new version of this file" link
-               if( $wgUser->isAllowed( 'reupload' ) ) {
+               if( UploadForm::userCanReUpload($wgUser,$this->img->name) ) {
                        $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
-                       $wgOut->addHtml( "<li><div>{$ulink}</div></li>" );
+                       $wgOut->addHtml( "<li><div class='plainlinks'>{$ulink}</div></li>" );
                }
                
                # External editing link
@@ -419,7 +441,7 @@ END
                $line = $this->img->nextHistoryLine();
 
                if ( $line ) {
-                       $list = new ImageHistoryList( $sk );
+                       $list = new ImageHistoryList( $sk, $this->img );
                        $s = $list->beginImageHistoryList() .
                                $list->imageHistoryLine( true, wfTimestamp(TS_MW, $line->img_timestamp),
                                        $this->mTitle->getDBkey(),  $line->img_user,
@@ -480,6 +502,12 @@ END
        {
                global $wgUser, $wgOut, $wgRequest;
 
+               if ( !$this->img->exists() || !$this->img->isLocal() ) {
+                       # Use standard article deletion
+                       Article::delete();
+                       return;
+               }
+
                $confirm = $wgRequest->wasPosted();
                $reason = $wgRequest->getVal( 'wpReason' );
                $image = $wgRequest->getVal( 'image' );
@@ -492,7 +520,8 @@ END
                        return;
                }
                if ( $wgUser->isBlocked() ) {
-                       return $this->blockedIPpage();
+                       $wgOut->blockedPage();
+                       return;
                }
                if ( wfReadOnly() ) {
                        $wgOut->readOnlyPage();
@@ -507,12 +536,10 @@ END
                        return;
                }
 
-               $this->img  = new Image( $this->mTitle );
-
                # Deleting old images doesn't require confirmation
                if ( !is_null( $oldimage ) || $confirm ) {
                        if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
-                               $this->doDelete( $reason );
+                               $this->doDeleteImage( $reason );
                        } else {
                                $wgOut->showFatalError( wfMsg( 'sessionfailure' ) );
                        }
@@ -531,9 +558,12 @@ END
 
        /*
         * Delete an image.
+        * Called doDeleteImage() not doDelete() so that Article::delete() doesn't 
+        * call back to here.
+        *
         * @param $reason User provided reason for deletion.
         */
-       function doDelete( $reason ) {
+       function doDeleteImage( $reason ) {
                global $wgOut, $wgRequest;
 
                $oldimage = $wgRequest->getVal( 'oldimage' );
@@ -625,62 +655,40 @@ END
                        return;
                }
                if ( $wgUser->isBlocked() ) {
-                       return $this->blockedIPpage();
+                       $wgOut->blockedPage();
+                       return;
                }
                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}";
+               $sourcePath = $this->img->getArchiveVirtualUrl( $oldimage );
+               $comment = wfMsg( "reverted" );
+               $result = $this->img->upload( $sourcePath, $comment, $comment );
 
-               if ( !is_dir( $dest ) ) wfMkdirParents( $dest );
-               if ( !is_dir( $archive ) ) wfMkdirParents( $archive );
-
-               if ( ! is_file( $curfile ) ) {
-                       $wgOut->showFileNotFoundError( htmlspecialchars( $curfile ) );
-                       return;
-               }
-               $oldver = wfTimestampNow() . "!{$name}";
-
-               if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
-                       $wgOut->showFileRenameError( $curfile, "${archive}/{$oldver}" );
-                       return;
-               }
-               if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
-                       $wgOut->showFileCopyError( "${archive}/{$oldimage}", $curfile );
+               if ( WikiError::isError( $result ) ) {
+                       $this->showError( $result );
                        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' ) );
 
-               $descTitle = $img->getTitle();
+               $descTitle = $this->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->upgradeRow();
                        $this->img->purgeCache();
                } else {
                        wfDebug( "ImagePage::doPurge no image\n" );
@@ -688,14 +696,29 @@ END
                parent::doPurge();
        }
 
+       /**
+        * Display an error from a wikitext-formatted WikiError object
+        */
+       function showError( WikiError $error ) {
+               global $wgOut;
+               $wgOut->setPageTitle( wfMsg( "internalerror" ) );
+               $wgOut->setRobotpolicy( "noindex,nofollow" );
+               $wgOut->setArticleRelated( false );
+               $wgOut->enableClientCache( false );
+               $wgOut->addWikiText( $error->getMessage() );
+       }
+
 }
 
 /**
  * @todo document
+ * @addtogroup Media
  */
 class ImageHistoryList {
-       function ImageHistoryList( &$skin ) {
-               $this->skin =& $skin;
+       var $img, $skin;
+       function ImageHistoryList( $skin, $img ) {
+               $this->skin = $skin;
+               $this->img = $img;
        }
 
        function beginImageHistoryList() {
@@ -717,11 +740,12 @@ class ImageHistoryList {
                $del = wfMsgHtml( 'deleteimg' );
                $delall = wfMsgHtml( 'deleteimgcompletely' );
                $cur = wfMsgHtml( 'cur' );
+               $local = $this->img->isLocal();
 
                if ( $iscur ) {
-                       $url = Image::imageUrl( $img );
+                       $url = htmlspecialchars( $this->img->getURL() );
                        $rlink = $cur;
-                       if ( $wgUser->isAllowed('delete') ) {
+                       if ( $local && $wgUser->isAllowed('delete') ) {
                                $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
                                  '&action=delete' );
                                $style = $this->skin->getInternalLinkAttributes( $link, $delall );
@@ -731,8 +755,8 @@ class ImageHistoryList {
                                $dlink = $del;
                        }
                } else {
-                       $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
-                       if( $wgUser->getID() != 0 && $wgTitle->userCan( 'edit' ) ) {
+                       $url = htmlspecialchars( $this->img->getArchiveUrl( $img ) );
+                       if( $local && $wgUser->getID() != 0 && $wgTitle->userCan( 'edit' ) ) {
                                $token = urlencode( $wgUser->editToken( $img ) );
                                $rlink = $this->skin->makeKnownLinkObj( $wgTitle,
                                           wfMsgHtml( 'revertimg' ), 'action=revert&oldimage=' .
@@ -748,8 +772,12 @@ class ImageHistoryList {
                                $dlink = $del;
                        }
                }
-               
-               $userlink = $this->skin->userLink( $user, $usertext ) . $this->skin->userToolLinks( $user, $usertext );
+
+               if ( $local ) {
+                       $userlink = $this->skin->userLink( $user, $usertext ) . $this->skin->userToolLinks( $user, $usertext );
+               } else {
+                       $userlink = htmlspecialchars( $usertext );
+               }
                $nbytes = wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
                        $wgLang->formatNum( $size ) );
                $widthheight = wfMsgHtml( 'widthheight', $width, $height );
@@ -761,7 +789,6 @@ class ImageHistoryList {
                $s .= "</li>\n";
                return $s;
        }
-
 }