* Fix explicit s-maxage=0 on raw pages; should help with proxy issues in
[lhc/web/wiklou.git] / includes / ImagePage.php
index f3ead55..24b0e48 100644 (file)
@@ -7,7 +7,7 @@
  *
  */
 if( !defined( 'MEDIAWIKI' ) )
-       die();
+       die( -1 );
 
 require_once( 'Image.php' );
 
@@ -20,14 +20,18 @@ class ImagePage extends Article {
        /* private */ var $img;  // Image object this page is shown for
        var $mExtraDescription = false;
 
+       /**
+        * Handler for action=render
+        * Include body text only; none of the image extras
+        */
        function render() {
                global $wgOut;
-               $wgOut->setArticleBodyOnly(true);
-               $wgOut->addWikitext($this->getContent(true));
+               $wgOut->setArticleBodyOnly( true );
+               $wgOut->addSecondaryWikitext( $this->getContent() );
        }
 
        function view() {
-               global $wgUseExternalEditor, $wgOut, $wgShowEXIF;
+               global $wgOut, $wgShowEXIF;
 
                $this->img = new Image( $this->mTitle );
 
@@ -44,8 +48,6 @@ class ImagePage extends Article {
                                $wgOut->addHTML($this->showTOC($showmeta));
 
                        $this->openShowImage();
-                       if ($exif)
-                               $wgOut->addWikiText($this->makeMetadataTable($exif));
 
                        # No need to display noarticletext, we use our own message, output in openShowImage()
                        if( $this->getID() ) {
@@ -55,20 +57,31 @@ class ImagePage extends Article {
                                $wgOut->setArticleFlag( true );
                                $wgOut->setRobotpolicy( 'index,follow' );
                                $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
-                               $wgOut->addMetaTags();
                                $this->viewUpdates();
                        }
 
-                       if ($this->mExtraDescription) {
-                               $fol = wfMsg('shareddescriptionfollows');
-                               if ($fol != '-')
-                                       $wgOut->addWikiText(wfMsg('shareddescriptionfollows'));
-                               $wgOut->addHTML($this->mExtraDescription);
+                       # Show shared description, if needed
+                       if( $this->mExtraDescription ) {
+                               $fol = wfMsg( 'shareddescriptionfollows' );
+                               if( $fol != '-' ) {
+                                       $wgOut->addWikiText( $fol );
+                               }
+                               $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription . '</div>' );
                        }
 
                        $this->closeShowImage();
                        $this->imageHistory();
                        $this->imageLinks();
+                       if( $exif ) {
+                               global $wgStylePath;
+                               $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();
                }
@@ -85,10 +98,10 @@ class ImagePage extends Article {
        function showTOC( $metadata ) {
                global $wgLang;
                $r = '<ul id="filetoc">
-                       <li><a href="#file">' . $wgLang->getNsText( NS_IMAGE ) . '</a></li>' .
-                       ($metadata ? '<li><a href="#metadata">' . wfMsg( 'metadata' ) . '</a></li>' : '') . '
-                       <li><a href="#filehistory">' . wfMsg( 'imghistory' ) . '</a></li>
-                       <li><a href="#filelinks">' . wfMsg( 'imagelinks' ) . '</a></li>
+                       <li><a href="#file">' . $wgLang->getNsText( NS_IMAGE ) . '</a></li>
+                       <li><a href="#filehistory">' . wfMsgHtml( 'imghistory' ) . '</a></li>
+                       <li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>' .
+                       ($metadata ? '<li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>' : '') . '
                </ul>';
                return $r;
        }
@@ -102,44 +115,66 @@ class ImagePage extends Article {
         * @return string
         */
        function makeMetadataTable( $exif ) {
-               $r = "{| class=metadata align=right width=250px\n";
-               $r .= '|+ id=metadata | '. htmlspecialchars( wfMsg( 'metadata' ) ) . "\n";
+               $r = wfMsg( 'metadata-help' ) . "\n\n";
+               $r .= "{| id=mw_metadata class=mw_metadata\n";
+               $visibleFields = $this->visibleMetadataFields();
                foreach( $exif as $k => $v ) {
                        $tag = strtolower( $k );
-                       $r .= "! class=$tag |" . wfMsg( "exif-$tag" ) . "\n";
-                       $r .= "| class=$tag |" . htmlspecialchars( $v ) . "\n";
-                       $r .= "|-\n";
+                       $msg = wfMsg( "exif-$tag" );
+                       $class = "exif-$tag";
+                       if( !in_array( $tag, $visibleFields ) ) {
+                               $class .= ' collapsable';
+                       }
+                       $r .= "|- class=\"$class\"\n";
+                       $r .= "!| $msg\n";
+                       $r .= "|| $v\n";
                }
-               return substr($r, 0, -3) . '|}';
+               $r .= '|}';
+               return $r;
        }
 
        /**
-        * Overloading Article's getContent method.
-        * Omit noarticletext if sharedupload
+        * Get a list of EXIF metadata items which should be displayed when
+        * the metadata table is collapsed.
         *
-        * @param $noredir If true, do not follow redirects
+        * @return array of strings
+        * @access private
         */
-       function getContent( $noredir )
-       {
-               if ( $this->img && $this->img->fromSharedDirectory && 0 == $this->getID() ) {
+       function visibleMetadataFields() {
+               $fields = array();
+               $lines = explode( "\n", wfMsgForContent( 'metadata-fields' ) );
+               foreach( $lines as $line ) {
+                       if( preg_match( '/^\\*\s*(.*?)\s*$/', $line, $matches ) ) {
+                               $fields[] = $matches[1];
+                       }
+               }
+               return $fields;
+       }
+
+       /**
+        * Overloading Article's getContent method.
+        * 
+        * Omit noarticletext if sharedupload; text will be fetched from the
+        * shared upload server if possible.
+        */
+       function getContent() {
+               if( $this->img && $this->img->fromSharedDirectory && 0 == $this->getID() ) {
                        return '';
                }
-               return Article::getContent( $noredir );
+               return Article::getContent();
        }
 
-       function openShowImage()
-       {
-               global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
-                      $wgUseImageResize, $wgRepositoryBaseUrl,
-                      $wgUseExternalEditor, $wgServer, $wgFetchCommonsDescriptions;
-               $full_url  = $this->img->getViewURL();
+       function openShowImage() {
+               global $wgOut, $wgUser, $wgImageLimits, $wgRequest, $wgUseImageResize;
+
+               $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' );
@@ -159,34 +194,46 @@ class ImagePage extends Article {
                                # image
 
                                # "Download high res version" link below the image
-                               $msg = wfMsg('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() ) ) {
+                               $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 );
                                                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=\"" .
@@ -206,34 +253,29 @@ class ImagePage extends Article {
 
 
                        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
+                                               );
                                }
                        }
 
@@ -242,20 +284,29 @@ class ImagePage extends Article {
                        }
                } 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;
+               global $wgRepositoryBaseUrl, $wgFetchCommonsDescriptions, $wgOut, $wgUser;
 
                $url = $wgRepositoryBaseUrl . urlencode($this->mTitle->getDBkey());
-               $sharedtext = "<div class='sharedUploadNotice'>" . wfMsg("sharedupload");
+               $sharedtext = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml("sharedupload");
                if ($wgRepositoryBaseUrl && !$wgFetchCommonsDescriptions) {
-                       $sharedtext .= " " . wfMsg("shareduploadwiki", $url);
+
+                       $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->addWikiText($sharedtext);
+               $wgOut->addHTML($sharedtext);
 
                if ($wgRepositoryBaseUrl && $wgFetchCommonsDescriptions) {
                        require_once("HttpFunctions.php");
@@ -282,9 +333,11 @@ class ImagePage extends Article {
                        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>' );
@@ -314,12 +367,16 @@ class ImagePage extends Article {
                        $s = $list->beginImageHistoryList() .
                                $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=''; }
@@ -367,7 +424,7 @@ class ImagePage extends Article {
        {
                global $wgUser, $wgOut, $wgRequest;
 
-               $confirm = $wgRequest->getBool( 'wpConfirmB' );
+               $confirm = $wgRequest->wasPosted();
                $image = $wgRequest->getVal( 'image' );
                $oldimage = $wgRequest->getVal( 'oldimage' );
 
@@ -415,10 +472,10 @@ class ImagePage extends Article {
                return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
        }
 
-       function doDelete()
-       {
-               global $wgOut, $wgUser, $wgContLang, $wgRequest;
-               global $wgUseSquid, $wgInternalServer, $wgPostCommitUpdateList;
+       function doDelete()     {
+               global $wgOut, $wgUser, $wgRequest, $wgUseSquid, $wgInternalServer;
+               global $wgPostCommitUpdateList;
+
                $fname = 'ImagePage::doDelete';
 
                $reason = $wgRequest->getVal( 'wpReason' );
@@ -441,9 +498,9 @@ class ImagePage extends Article {
 
                        # Squid purging
                        if ( $wgUseSquid ) {
-                               $urlArr = Array(
+                               $urlArr = array(
                                        $wgInternalServer.wfImageArchiveUrl( $oldimage ),
-                                       $wgInternalServer.$this->mTitle->getFullURL()
+                                       $this->mTitle->getInternalURL()
                                );
                                wfPurgeSquidServers($urlArr);
                        }
@@ -535,10 +592,8 @@ class ImagePage extends Article {
                }
        }
 
-       function revert()
-       {
+       function revert() {
                global $wgOut, $wgRequest, $wgUser;
-               global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
 
                $oldimage = $wgRequest->getText( 'oldimage' );
                if ( strlen( $oldimage ) < 16 ) {
@@ -632,8 +687,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' );
@@ -673,15 +728,15 @@ class ImageHistoryList {
                if ( 0 == $user ) {
                        $userlink = $usertext;
                } else {
-                       $userlink = $this->skin->makeLinkObj(
-                               Title::makeTitle( NS_USER, $usertext ),
-                               $usertext );
+                       $userlink = $this->skin->makeLinkObj( Title::makeTitle( NS_USER, $usertext ), $usertext );
+                       $usertalk = $this->skin->makeLinkObj( Title::makeTitle( NS_USER_TALK, $usertext), $wgContLang->getNsText( NS_TALK ) );
+                       $userdata = $userlink . ' (' . $usertalk . ')';
                }
                $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> . . {$userdata} . . {$widthheight} ({$nbytes})";
 
                $s .= $this->skin->commentBlock( $description, $wgTitle );
                $s .= "</li>\n";