* (bug 2780) Fix thumbnail generation with GD for new image schema
[lhc/web/wiklou.git] / includes / ImagePage.php
index 4c40fcb..d8c0b8c 100644 (file)
@@ -18,18 +18,37 @@ 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 ;
+               global $wgUseExternalEditor, $wgOut, $wgShowEXIF;
 
-               $this->img  = new Image( $this->mTitle );
+               $this->img = new Image( $this->mTitle );
 
                if( $this->mTitle->getNamespace() == NS_IMAGE  ) {
-                       if ( $this->img->exists() ) $this->showEXIFdata();
+                       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();
-                       
+                       if ($exif)
+                               $wgOut->addWikiText($this->makeMetadataTable($exif));
+
                        # No need to display noarticletext, we use our own message, output in openShowImage()
-                       if ( $this->getID() ) {
+                       if( $this->getID() ) {
                                Article::view();
                        } else {
                                # Just need to set the right headers
@@ -39,13 +58,14 @@ class ImagePage extends Article {
                                $wgOut->addMetaTags();
                                $this->viewUpdates();
                        }
-                       
-                       if ( $this->img->exists() ) {
-                               $this->uploadNewVersionLink();
-                               if ( $wgUseExternalEditor && $this->img->exists() ) {
-                                       $this->externalEditorLink();
-                               }
+
+                       if ($this->mExtraDescription) {
+                               $fol = wfMsg('shareddescriptionfollows');
+                               if ($fol != '-')
+                                       $wgOut->addWikiText(wfMsg('shareddescriptionfollows'));
+                               $wgOut->addHTML($this->mExtraDescription);
                        }
+
                        $this->closeShowImage();
                        $this->imageHistory();
                        $this->imageLinks();
@@ -53,36 +73,65 @@ class ImagePage extends Article {
                        Article::view();
                }
        }
-       
-       function showEXIFdata()
-               {
-               global $wgOut , $wgShowEXIF ;
-               if ( ! $wgShowEXIF ) return ;
-
-               # Get the EXIF data
-               $exif = $this->img->getExifData () ;
-               if ( count ( $exif ) == 0 ) return ; # No EXIF data available
-               if ( count ( $exif ) == 1 && isset ( $exif["EXIF"] ) && $exif["EXIF"] == "NO" ) return ; # This image does not have EXIF data
-               
-               # Create the table
-               $r = "<table border='1' cellspacing='0' cellpadding='0' align='right'>" ;
-               $r .= "<caption>EXIF data</caption>" ;
-               foreach ( $exif AS $k => $v ) {
-                       $r .= "<tr>" ;
-                       $v = str_replace ( "\0" , "" , $v ) ;
-                       $r .= "<th><small>" . htmlspecialchars ( $k ) . "</small></th>" ;
-                       $r .= "<td><small>" . htmlspecialchars ( $v ) . "</small></td>" ;
-                       $r .= "</tr>" ;
-                       }
-               $r .= "</table>" ;
-               $wgOut->addHTML ( $r ) ;
+
+       /**
+        * 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>' .
+                       ($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>
+               </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 = "{| class=metadata align=right width=250px\n";
+               $r .= '|+ id=metadata | '. htmlspecialchars( wfMsg( 'metadata' ) ) . "\n";
+               foreach( $exif as $k => $v ) {
+                       $tag = strtolower( $k );
+                       $r .= "! class=$tag |" . wfMsg( "exif-$tag" ) . "\n";
+                       $r .= "| class=$tag |" . htmlspecialchars( $v ) . "\n";
+                       $r .= "|-\n";
+               }
+               return substr($r, 0, -3) . '|}';
+       }
+
+       /**
+        * Overloading Article's getContent method.
+        * Omit noarticletext if sharedupload
+        *
+        * @param $noredir If true, do not follow redirects
+        */
+       function getContent( $noredir )
+       {
+               if ( $this->img && $this->img->fromSharedDirectory && 0 == $this->getID() ) {
+                       return '';
                }
+               return Article::getContent( $noredir );
+       }
 
        function openShowImage()
        {
-               global $wgOut, $wgUser, $wgImageLimits, $wgRequest, 
-                      $wgUseImageResize, $wgRepositoryBaseUrl, 
-                      $wgUseExternalEditor, $wgServer;
+               global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
+                      $wgUseImageResize, $wgRepositoryBaseUrl,
+                      $wgUseExternalEditor, $wgServer, $wgFetchCommonsDescriptions;
                $full_url  = $this->img->getViewURL();
                $anchoropen = '';
                $anchorclose = '';
@@ -101,23 +150,33 @@ class ImagePage extends Article {
                $sk = $wgUser->getSkin();
 
                if ( $this->img->exists() ) {
-                       if ( $this->img->getType() != '' ) {
+                       # image
+                       $width = $this->img->getWidth();
+                       $height = $this->img->getHeight();
+                       $showLink = false;
+
+                       if ( $this->img->allowInlineDisplay() and $width and $height) {
                                # image
-                               $width = $this->img->getWidth();
-                               $height = $this->img->getHeight();
+
+                               # "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 ( $width != $this->img->getWidth() || $height != $this->img->getHeight() ) {
+                               if ( !$this->img->mustRender()
+                                  && ( $width != $this->img->getWidth() || $height != $this->img->getHeight() ) ) {
                                        if( $wgUseImageResize ) {
                                                $thumbnail = $this->img->getThumbnail( $width );
-                                               $url = $thumbnail->getUrl();
+                                               if ( $thumbnail == null ) {
+                                                       $url = $full_url;
+                                               } 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.
@@ -127,29 +186,86 @@ class ImagePage extends Article {
                                        $anchorclose = "</a><br />\n$anchoropen{$msg}</a>";
                                } else {
                                        $url = $full_url;
+                                       $showLink = $this->img->mustRender();
                                }
-                               $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) {
+                               $s= $sk->makeMediaLink( $this->img->getName(), '', '', true );
+                               $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>');
+
+                               } else {
+                                       $wgOut->addHTML("<div class=\"fullMedia\">");
+                                       $wgOut->addHTML($s);
+
+                                       $wgOut->addHTML("<span class=\"fileInfo\"> (");
+                                       $wgOut->addWikiText( $info, false );
+                                       $wgOut->addHTML(")</span>");
+
+                                       $wgOut->addHTML("</div>");
                                }
-                               $sharedtext.="</div>";
-                               $wgOut->addWikiText($sharedtext);
                        }
-                       
+
+                       if($this->img->fromSharedDirectory) {
+                               $this->printSharedImageText();
+                       }
                } else {
                        # Image does not exist
                        $wgOut->addWikiText( wfMsg( 'noimage', $this->getUploadUrl() ) );
                }
        }
-       
+
+       function printSharedImageText() {
+               global $wgRepositoryBaseUrl, $wgFetchCommonsDescriptions, $wgOut;
+
+               $url = $wgRepositoryBaseUrl . urlencode($this->mTitle->getDBkey());
+               $sharedtext = "<div class='sharedUploadNotice'>" . wfMsg("sharedupload");
+               if ($wgRepositoryBaseUrl && !$wgFetchCommonsDescriptions) {
+                       $sharedtext .= " " . wfMsg("shareduploadwiki", $url);
+               }
+               $sharedtext .= "</div>";
+               $wgOut->addWikiText($sharedtext);
+
+               if ($wgRepositoryBaseUrl && $wgFetchCommonsDescriptions) {
+                       $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' );
@@ -157,28 +273,27 @@ class ImagePage extends Article {
        }
 
 
-       function uploadNewVersionLink() {
-               global $wgOut;
-               $wgOut->addWikiText( wfMsg( 'uploadnewversion', $this->getUploadUrl() ) );
-       }
-
-       function externalEditorLink()
+       function uploadLinksBox()
        {
-               global $wgUser,$wgOut;
+               global $wgUser, $wgOut;
+
+               if ($this->img->fromSharedDirectory)
+                       return;
+
                $sk = $wgUser->getSkin();
-               $wgOut->addHTML( '<div class="editExternally">' );
+               $wgOut->addHTML( '<br /><ul><li>' );
+               $wgOut->addWikiText( '<div>'. wfMsg( 'uploadnewversion', $this->getUploadUrl() ) .'</div>' );
+               $wgOut->addHTML( '</li><li>' );
                $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle,
-                       wfMsg( 'edit-externally' ),
-                       "action=edit&externaledit=true&mode=file" ) );
-               $wgOut->addWikiText( '<div class="editExternallyHelp">' .
-                       wfMsg('edit-externally-help') . '</div>' );
-               $wgOut->addHTML( '</div><br clear="both" />' );
+                       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
-                       
+
        }
 
        /**
@@ -187,7 +302,7 @@ class ImagePage extends Article {
         */
        function imageHistory()
        {
-               global $wgUser, $wgOut;
+               global $wgUser, $wgOut, $wgUseExternalEditor;
 
                $sk = $wgUser->getSkin();
 
@@ -208,18 +323,25 @@ class ImagePage extends Article {
                        $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
@@ -247,13 +369,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;
@@ -266,9 +391,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 ) ) {
@@ -278,7 +403,7 @@ class ImagePage extends Article {
                        }
                        return;
                }
-               
+
                if ( !is_null( $image ) ) {
                        $q = '&image=' . urlencode( $image );
                } else if ( !is_null( $oldimage ) ) {
@@ -297,7 +422,7 @@ class ImagePage extends Article {
 
                $reason = $wgRequest->getVal( 'wpReason' );
                $oldimage = $wgRequest->getVal( 'oldimage' );
-               
+
                $dbw =& wfGetDB( DB_MASTER );
 
                if ( !is_null( $oldimage ) ) {
@@ -309,7 +434,7 @@ class ImagePage extends Article {
                                $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
                                return;
                        }
-                       
+
                        # Invalidate description page cache
                        $this->mTitle->invalidateCache();
 
@@ -328,7 +453,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}";
@@ -338,14 +463,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();
@@ -353,7 +478,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.
@@ -376,14 +501,11 @@ class ImagePage extends Article {
                $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
                $wgOut->setRobotpolicy( 'noindex,nofollow' );
 
-               $sk = $wgUser->getSkin();
-               $loglink = $sk->makeKnownLinkObj(
-                       Title::makeTitle( NS_SPECIAL, 'Log/delete' ),
-                       wfMsg( 'deletionlog' ) );
-
+               $loglink = '[[Special:Log/delete|' . wfMsg( 'deletionlog' ) . ']]';
                $text = wfMsg( 'deletedtext', $deleted, $loglink );
 
-               $wgOut->addHTML( '<p>' . $text . "</p>\n" );
+               $wgOut->addWikiText( $text );
+
                $wgOut->returnToMain( false, $this->mTitle->getPrefixedText() );
        }
 
@@ -393,7 +515,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.
@@ -439,10 +561,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 );
@@ -454,7 +579,7 @@ class ImagePage extends Article {
                        return;
                }
                $oldver = wfTimestampNow() . "!{$name}";
-               
+
                $dbr =& wfGetDB( DB_SLAVE );
                $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage )  );
 
@@ -477,6 +602,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();
+       }
+
 }
 
 /**
@@ -487,9 +619,9 @@ 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;
        }