* (bug 3643) Fix image page display of large images with resizing disabled
[lhc/web/wiklou.git] / includes / ImagePage.php
index ec39f31..1ab1c36 100644 (file)
@@ -18,7 +18,14 @@ require_once( 'Image.php' );
 class ImagePage extends Article {
 
        /* private */ var $img;  // Image object this page is shown for
-       
+       var $mExtraDescription = false;
+
+       function render() {
+               global $wgOut;
+               $wgOut->setArticleBodyOnly(true);
+               $wgOut->addWikitext($this->getContent(true));
+       }
+
        function view() {
                global $wgUseExternalEditor, $wgOut, $wgShowEXIF;
 
@@ -39,7 +46,7 @@ class ImagePage extends Article {
                        $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() ) {
                                Article::view();
@@ -51,7 +58,14 @@ class ImagePage extends Article {
                                $wgOut->addMetaTags();
                                $this->viewUpdates();
                        }
-                       
+
+                       if ($this->mExtraDescription) {
+                               $fol = wfMsg('shareddescriptionfollows');
+                               if ($fol != '-')
+                                       $wgOut->addWikiText(wfMsg('shareddescriptionfollows'));
+                               $wgOut->addHTML($this->mExtraDescription);
+                       }
+
                        $this->closeShowImage();
                        $this->imageHistory();
                        $this->imageLinks();
@@ -72,9 +86,9 @@ class ImagePage extends Article {
                global $wgLang;
                $r = '<ul id="filetoc">
                        <li><a href="#file">' . $wgLang->getNsText( NS_IMAGE ) . '</a></li>' .
-                       ($metadata ? '<li><a href="#metadata">' . wfMsg( 'metadata' ) . '</a></li>' : '') . '
-                       <li><a href="#filehistory">' . wfMsg( 'imghistory' ) . '</a></li>
-                       <li><a href="#filelinks">' . wfMsg( 'imagelinks' ) . '</a></li>
+                       ($metadata ? '<li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>' : '') . '
+                       <li><a href="#filehistory">' . wfMsgHtml( 'imghistory' ) . '</a></li>
+                       <li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>
                </ul>';
                return $r;
        }
@@ -83,17 +97,19 @@ class ImagePage extends Article {
         * 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 = "{| class=metadata align=right width=250px\n";
-               $r .= '|+ id=metadata | '. htmlspecialchars( wfMsg( 'metadata' ) ) . "\n";
+               $r .= '|+ id=metadata | '. wfMsg( 'metadata' )  . "\n";
                foreach( $exif as $k => $v ) {
                        $tag = strtolower( $k );
-                       $r .= "! class=$tag |" . wfMsg( "exif-$tag" ) . "\n";
-                       $r .= "| class=$tag |" . htmlspecialchars( $v ) . "\n";
+                       $msg = wfMsg( "exif-$tag" );
+                       
+                       $r .= "! class=$tag | $msg\n";
+                       $r .= "| class=$tag | $v\n";
                        $r .= "|-\n";
                }
                return substr($r, 0, -3) . '|}';
@@ -101,7 +117,7 @@ class ImagePage extends Article {
 
        /**
         * Overloading Article's getContent method.
-        * Omit noarticletext if sharedupload 
+        * Omit noarticletext if sharedupload
         *
         * @param $noredir If true, do not follow redirects
         */
@@ -115,17 +131,17 @@ class ImagePage extends Article {
 
        function openShowImage()
        {
-               global $wgOut, $wgUser, $wgImageLimits, $wgRequest, 
-                      $wgUseImageResize, $wgRepositoryBaseUrl, 
-                      $wgUseExternalEditor, $wgServer;
-               $full_url  = $this->img->getViewURL();
+               global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
+                      $wgUseImageResize, $wgRepositoryBaseUrl,
+                      $wgUseExternalEditor, $wgServer, $wgFetchCommonsDescriptions;
+               $full_url  = $this->img->getURL();
                $anchoropen = '';
                $anchorclose = '';
 
                if( $wgUser->getOption( 'imagesize' ) == '' ) {
                        $sizeSel = User::getDefaultOption( 'imagesize' );
                } else {
-                       $sizeSel = IntVal( $wgUser->getOption( 'imagesize' ) );
+                       $sizeSel = intval( $wgUser->getOption( 'imagesize' ) );
                }
                if( !isset( $wgImageLimits[$sizeSel] ) ) {
                        $sizeSel = User::getDefaultOption( 'imagesize' );
@@ -140,39 +156,43 @@ class ImagePage extends Article {
                        $width = $this->img->getWidth();
                        $height = $this->img->getHeight();
                        $showLink = false;
-                       
-                       if ( $this->img->allowInlineDisplay() and $width and $height) { 
+
+                       if ( $this->img->allowInlineDisplay() and $width and $height) {
                                # image
-                               
+
                                # "Download high res version" link below the image
-                               $msg = wfMsg('showbigimage', $width, $height, intval( $this->img->getSize()/1024 ) );
+                               $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 ( !$this->img->mustRender() 
-                                  && ( $width != $this->img->getWidth() || $height != $this->img->getHeight() ) ) {
+                               if ( $width != $this->img->getWidth() || $height != $this->img->getHeight() ) {
                                        if( $wgUseImageResize ) {
                                                $thumbnail = $this->img->getThumbnail( $width );
                                                if ( $thumbnail == null ) {
-                                                       $url = $full_url;
+                                                       $url = $this->img->getViewURL();
                                                } else {
-                                                       $url = $thumbnail->getUrl();
+                                                       $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 = $full_url;
+                                               $url = $this->img->getViewURL();
                                        }
                                        $anchoropen  = "<a href=\"{$full_url}\">";
-                                       $anchorclose = "</a><br />\n$anchoropen{$msg}</a>";
+                                       $anchorclose = "</a><br />";
+                                       if( $this->img->mustRender() ) {
+                                               $showLink = true;
+                                       } else {
+                                               $anchorclose .= "\n$anchoropen{$msg}</a>";
+                                       }
                                } else {
-                                       $url = $full_url;
-                                       $showLink = $this->img->mustRender();
+                                       $url = $this->img->getViewURL();
+                                       $showLink = true;
                                }
                                $wgOut->addHTML( '<div class="fullImageLink" id="file">' . $anchoropen .
                                     "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
@@ -181,63 +201,82 @@ class ImagePage extends Article {
                                #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;
                        }
-                       
-                       
+
+
                        if ($showLink) {
-                               $s= $sk->makeMediaLink( $this->img->getName(), '', '', true );
-                               $info= wfMsg( 'fileinfo', ceil($this->img->getSize()/1024.0), $this->img->getMimeType() );
-                       
+                               $filename = wfEscapeWikiText( $this->img->getName() );
+                               $info = wfMsg( 'fileinfo',
+                                       ceil($this->img->getSize()/1024.0),
+                                       $this->img->getMimeType() );
+       
                                if (!$this->img->isSafeFile()) {
-                                       $wgOut->addHTML("<div class=\"fullMedia\">");
-                                       $wgOut->addHTML("<span class=\"dangerousLink\">");
-                                       $wgOut->addHTML($s);
-                                       $wgOut->addHTML("</span>");
-                                       
-                                       $wgOut->addHTML("<span class=\"fileInfo\"> (");
-                                       $wgOut->addWikiText( $info, false );
-                                       $wgOut->addHTML(")</span>");
-                                       $wgOut->addHTML("</div>");
-                                       
-                                       #this should be formated a little nicer. Is CSS sufficient?
-                                       $wgOut->addHTML("<div class=\"mediaWarning\">");
-                                       $wgOut->addWikiText( wfMsg( 'mediawarning' ) );
-                                       $wgOut->addHTML('</div>');
-                                        
+                                       $warning = wfMsg( 'mediawarning' );
+                                       $wgOut->addWikiText( <<<END
+<div class="fullMedia">
+<span class="dangerousLink">[[Media:$filename|$filename]]</span>
+<span class="fileInfo"> ($info)</span>
+</div>
+
+<div class="mediaWarning">$warning</div>
+END
+                                               );
                                } else {
-                                       $wgOut->addHTML("<div class=\"fullMedia\">");
-                                       $wgOut->addHTML($s);
-                                       
-                                       $wgOut->addHTML("<span class=\"fileInfo\"> (");
-                                       $wgOut->addWikiText( $info, false );
-                                       $wgOut->addHTML(")</span>");
-                                       
-                                       $wgOut->addHTML("</div>");
+                                       $wgOut->addWikiText( <<<END
+<div class="fullMedia">
+[[Media:$filename|$filename]] <span class="fileInfo"> ($info)</span>
+</div>
+END
+                                               );
                                }
                        }
-                       
+
                        if($this->img->fromSharedDirectory) {
-                               $sharedtext="<div class=\"sharedUploadNotice\">" . wfMsg("sharedupload");
-                               if($wgRepositoryBaseUrl) {
-                                       $sharedtext .= " ". wfMsg("shareduploadwiki",$wgRepositoryBaseUrl . urlencode($this->mTitle->getDBkey()));
-                               }
-                               $sharedtext.="</div>";
-                               $wgOut->addWikiText($sharedtext);
+                               $this->printSharedImageText();
                        }
-                       
                } else {
                        # Image does not exist
-                       $wgOut->addWikiText( wfMsg( 'noimage', $this->getUploadUrl() ) );
+
+                       $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' );
@@ -247,21 +286,27 @@ class ImagePage extends Article {
 
        function uploadLinksBox()
        {
-               global $wgUser,$wgOut;
+               global $wgUser, $wgOut;
+
+               if ($this->img->fromSharedDirectory)
+                       return;
+
                $sk = $wgUser->getSkin();
-               $wgOut->addHTML( '<br /><ul><li>' );
-               $wgOut->addWikiText( '<div>'. wfMsg( 'uploadnewversion', $this->getUploadUrl() ) .'</div>' );
-               $wgOut->addHTML( '</li><li>' );
+               $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>' );
        }
-       
+
        function closeShowImage()
        {
                # For overloading
-                       
+
        }
 
        /**
@@ -279,14 +324,18 @@ class ImagePage extends Article {
                if ( $line ) {
                        $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=''; }
@@ -309,10 +358,10 @@ class ImagePage extends Article {
                $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
+                 $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 ) ) {
@@ -337,13 +386,16 @@ class ImagePage extends Article {
                $confirm = $wgRequest->getBool( 'wpConfirmB' );
                $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;
@@ -356,9 +408,9 @@ class ImagePage extends Article {
                        $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
                        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 ) ) {
@@ -368,7 +420,7 @@ class ImagePage extends Article {
                        }
                        return;
                }
-               
+
                if ( !is_null( $image ) ) {
                        $q = '&image=' . urlencode( $image );
                } else if ( !is_null( $oldimage ) ) {
@@ -387,7 +439,7 @@ class ImagePage extends Article {
 
                $reason = $wgRequest->getVal( 'wpReason' );
                $oldimage = $wgRequest->getVal( 'oldimage' );
-               
+
                $dbw =& wfGetDB( DB_MASTER );
 
                if ( !is_null( $oldimage ) ) {
@@ -399,7 +451,7 @@ class ImagePage extends Article {
                                $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
                                return;
                        }
-                       
+
                        # Invalidate description page cache
                        $this->mTitle->invalidateCache();
 
@@ -418,7 +470,7 @@ class ImagePage extends Article {
                        $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}";
@@ -428,14 +480,14 @@ class ImagePage extends Article {
                                return;
                        }
                        $dbw->delete( 'image', array( 'img_name' => $image ) );
-                       $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_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();
@@ -443,7 +495,7 @@ class ImagePage extends Article {
                                $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.
@@ -468,7 +520,7 @@ class ImagePage extends Article {
 
                $loglink = '[[Special:Log/delete|' . wfMsg( 'deletionlog' ) . ']]';
                $text = wfMsg( 'deletedtext', $deleted, $loglink );
-               
+
                $wgOut->addWikiText( $text );
 
                $wgOut->returnToMain( false, $this->mTitle->getPrefixedText() );
@@ -480,7 +532,7 @@ class ImagePage extends Article {
 
                $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.
@@ -526,10 +578,13 @@ class ImagePage extends Article {
                        $wgOut->sysopRequired();
                        return;
                }
+               if ( $wgUser->isBlocked() ) {
+                       return $this->blockedIPpage();
+               }
                if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
                        $wgOut->errorpage( 'internalerror', 'sessionfailure' );
                        return;
-               }               
+               }
                $name = substr( $oldimage, 15 );
 
                $dest = wfImageDir( $name );
@@ -541,7 +596,7 @@ class ImagePage extends Article {
                        return;
                }
                $oldver = wfTimestampNow() . "!{$name}";
-               
+
                $dbr =& wfGetDB( DB_SLAVE );
                $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage )  );
 
@@ -564,6 +619,13 @@ class ImagePage extends Article {
                $descTitle = $img->getTitle();
                $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
        }
+
+       function blockedIPpage() {
+               require_once( 'EditPage.php' );
+               $edit = new EditPage( $this );
+               return $edit->blockedIPpage();
+       }
+
 }
 
 /**
@@ -574,7 +636,7 @@ class ImageHistoryList {
        function ImageHistoryList( &$skin ) {
                $this->skin =& $skin;
        }
-       
+
        function beginImageHistoryList() {
                $s = "\n<h2 id=\"filehistory\">" . wfMsg( 'imghistory' ) . "</h2>\n" .
                  "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
@@ -586,7 +648,7 @@ class ImageHistoryList {
                return $s;
        }
 
-       function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
+       function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description, $width, $height ) {
                global $wgUser, $wgLang, $wgContLang, $wgTitle;
 
                $datetime = $wgLang->timeanddate( $timestamp, true );
@@ -632,10 +694,10 @@ class ImageHistoryList {
                                $usertext );
                }
                $nbytes = wfMsg( 'nbytes', $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})";
 
                $s .= $this->skin->commentBlock( $description, $wgTitle );
                $s .= "</li>\n";