* Don't display the metadata TOC link for pages where it's not applicable
[lhc/web/wiklou.git] / includes / ImagePage.php
index 7a9209e..4bb4d5b 100644 (file)
@@ -17,37 +17,106 @@ require_once( 'Image.php' );
  */
 class ImagePage extends Article {
 
-       /* private */ var $img;  // Image object this page is shown for. Initilaized in openShowImage, not
-                                // available in doDelete etc.
-
+       /* private */ var $img;  // Image object this page is shown for
+       
        function view() {
-               global $wgUseExternalEditor;
-               if( $this->mTitle->getNamespace() == NS_IMAGE ) {
-                       $this->openShowImage();
-               }
+               global $wgUseExternalEditor, $wgOut, $wgShowEXIF;
 
-               Article::view();
-               if($wgUseExternalEditor) {
-                       $this->externalEditorLink();
-               }
-               
-               # If the article we've just shown is in the "Image" namespace,
-               # follow it with the history list and link list for the image
-               # it describes.
+               $this->img  = new Image( $this->mTitle );
+
+               if( $this->mTitle->getNamespace() == NS_IMAGE  ) {
+                       if ($wgShowEXIF && $this->img->exists()) {
+                               $exif = $this->img->getExifData();
+                               $showmeta = count($exif) ? true : false;
+                       } else {
+                               $exif = false;
+                               $showmeta = false;
+                       }
+
+                       if ($this->img->exists())
+                               $wgOut->addHTML($this->showTOC($showmeta));
 
-               if( $this->mTitle->getNamespace() == NS_IMAGE ) {
+                       $this->openShowImage();
+                       
+                       # No need to display noarticletext, we use our own message, output in openShowImage()
+                       if( $this->getID() ) {
+                               Article::view();
+                       } else {
+                               # Just need to set the right headers
+                               $wgOut->setArticleFlag( true );
+                               $wgOut->setRobotpolicy( 'index,follow' );
+                               $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
+                               $wgOut->addMetaTags();
+                               $this->viewUpdates();
+                       }
+                       
                        $this->closeShowImage();
                        $this->imageHistory();
+                       if ($exif)
+                               $wgOut->addHTML($this->makeMetadataTable($exif));
                        $this->imageLinks();
+               } else {
+                       Article::view();
                }
        }
 
+       /**
+        * Create the TOC
+        *
+        * @access private
+        *
+        * @param bool $metadata Whether or not to show the metadata link
+        * @return string
+        */
+       function showTOC( $metadata ) {
+               global $wgLang;
+               $r = '<ul id="filetoc">
+                       <li><a href="#file">' . $wgLang->getNsText( NS_IMAGE ) . '</a></li>
+                       <li><a href="#filehistory">' . wfMsg( 'imghistory' ) . '</a></li>
+                       <li><a href="#filelinks">' . wfMsg( 'imagelinks' ) . '</a></li>' .
+                       ($metadata ? '<li><a href="#metadata">' . wfMsg( 'metadata' ) . '</a></li>' : '') .
+               '</ul>';
+               return $r;
+       }
+
+       /**
+        * Make a table with metadata to be shown in the output page.
+        *
+        * @access private
+        * 
+        * @param array $exif The array containing the EXIF data
+        * @return string
+        */
+       function makeMetadataTable( $exif ) {
+               # Create the table
+               $r = '<h2 id="metadata">'. htmlspecialchars( wfMsg( 'metadata' ) ) . "</h2>\n";
+               $r .= "<table class='metadata'>\n" ;
+               $n = 0;
+               foreach( $exif as $k => $v ) {
+                       if( $n % 2 == 0 ) {
+                               $r .= '<tr>';
+                       }
+                       $r .= '<th>' . wfMsg( 'exif-' . strtolower( $k ) ) . "</th>\n";
+                       $r .= '<td>' . htmlspecialchars( $v ) . "</td>\n";
+                       if ( $n % 2 == 1 ) {
+                               $r .= "</tr>\n";
+                       } else {
+                               $r .= "<td class='spacer'>&nbsp;</td>\n";
+                       }
+                       $n++;
+               }
+               if ( $n % 2 == 1 ) {
+                       $r .= "<th></th><td></td></tr>\n";
+               }
+
+               return "$r</table>\n";
+       }
+
        function openShowImage()
        {
                global $wgOut, $wgUser, $wgImageLimits, $wgRequest, 
                       $wgUseImageResize, $wgRepositoryBaseUrl, 
-                      $wgUseExternalEditor;
-               $this->img  = Image::newFromTitle( $this->mTitle );
+                      $wgUseExternalEditor, $wgServer;
                $full_url  = $this->img->getViewURL();
                $anchoropen = '';
                $anchorclose = '';
@@ -63,16 +132,14 @@ class ImagePage extends Article {
                $max = $wgImageLimits[$sizeSel];
                $maxWidth = $max[0];
                $maxHeight = $max[1];
-
+               $sk = $wgUser->getSkin();
 
                if ( $this->img->exists() ) {
-
-                       $sk = $wgUser->getSkin();
-                       
-                       if ( $this->img->getType() != '' ) {
+                       if ( $this->img->getType() ) {
                                # 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 );
@@ -85,17 +152,7 @@ class ImagePage extends Article {
                                if ( $width != $this->img->getWidth() || $height != $this->img->getHeight() ) {
                                        if( $wgUseImageResize ) {
                                                $thumbnail = $this->img->getThumbnail( $width );
-
-                                               if (    ( ! $this->img->mustRender() )
-                                                    && ( $thumbnail->getSize() > $this->img->getSize() ) ) {
-                                                       # the thumbnail is bigger thatn the original image.
-                                                       # show the original image instead of the thumb.
-                                                       $url = $full_url;
-                                                       $width = $this->img->getWidth();
-                                                       $height = $this->img->getHeight();
-                                               } 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.
@@ -106,7 +163,7 @@ class ImagePage extends Article {
                                } else {
                                        $url = $full_url;
                                }
-                               $s = '<div class="fullImageLink">' . $anchoropen .
+                               $s = '<div class="fullImageLink" id="file">' . $anchoropen .
                                     "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
                                     htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>';
                        } else {
@@ -122,20 +179,30 @@ class ImagePage extends Article {
                                $wgOut->addWikiText($sharedtext);
                        }
                        
+               } else {
+                       # Image does not exist
+                       $wgOut->addWikiText( wfMsg( 'noimage', $this->getUploadUrl() ) );
                }
        }
        
-       function externalEditorLink()
+       function getUploadUrl() {
+               global $wgServer;
+               $uploadTitle = Title::makeTitle( NS_SPECIAL, 'Upload' );
+               return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img->getName() ) );
+       }
+
+
+       function uploadLinksBox()
        {
                global $wgUser,$wgOut;
                $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="all" />' );
+                       wfMsg( 'edit-externally' ), "action=edit&externaledit=true&mode=file" ) );
+               $wgOut->addWikiText( '<div>' .  wfMsg('edit-externally-help') . '</div>' );
+               $wgOut->addHTML( '</li></ul>' );
        }
        
        function closeShowImage()
@@ -150,7 +217,7 @@ class ImagePage extends Article {
         */
        function imageHistory()
        {
-               global $wgUser, $wgOut;
+               global $wgUser, $wgOut, $wgUseExternalEditor;
 
                $sk = $wgUser->getSkin();
 
@@ -171,13 +238,20 @@ 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' );
@@ -230,6 +304,8 @@ class ImagePage extends Article {
                        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 ) ) {
@@ -253,7 +329,7 @@ class ImagePage extends Article {
        function doDelete()
        {
                global $wgOut, $wgUser, $wgContLang, $wgRequest;
-               global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
+               global $wgUseSquid, $wgInternalServer, $wgPostCommitUpdateList;
                $fname = 'ImagePage::doDelete';
 
                $reason = $wgRequest->getVal( 'wpReason' );
@@ -270,10 +346,15 @@ class ImagePage extends Article {
                                $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
                                return;
                        }
+                       
+                       # Invalidate description page cache
+                       $this->mTitle->invalidateCache();
+
                        # Squid purging
                        if ( $wgUseSquid ) {
                                $urlArr = Array(
-                                       $wgInternalServer.wfImageArchiveUrl( $oldimage )
+                                       $wgInternalServer.wfImageArchiveUrl( $oldimage ),
+                                       $wgInternalServer.$this->mTitle->getFullURL()
                                );
                                wfPurgeSquidServers($urlArr);
                        }
@@ -294,28 +375,20 @@ class ImagePage extends Article {
                                return;
                        }
                        $dbw->delete( 'image', array( 'img_name' => $image ) );
-                       $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
-                                               
-                       # Squid purging
-                       if ( $wgUseSquid ) {
-                               $urlArr = Array(
-                                       $wgInternalServer . Image::wfImageUrl( $image )
-                               );
-                               wfPurgeSquidServers($urlArr);
-                       }
-                       
+                       $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 );
                        }       
-                       
-                       # Squid purging, part II
+
+                       # And also the HTML of all pages using this image
+                       $linksTo = $this->img->getLinksTo();
                        if ( $wgUseSquid ) {
-                               /* this needs to be done after LinksUpdate */
-                               $u = new SquidUpdate( $urlArr );
-                               array_push( $wgDeferredUpdateList, $u );
+                               $u = SquidUpdate::newFromTitles( $linksTo, $urlArr );
+                               array_push( $wgPostCommitUpdateList, $u );
                        }
                        
                        $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
@@ -323,12 +396,16 @@ class ImagePage extends Article {
                        # Image itself is now gone, and database is cleaned.
                        # Now we remove the image description page.
 
-                       $nt = Title::makeTitleSafe( NS_IMAGE, $image );
-                       $article = new Article( $nt );
+                       $article = new Article( $this->mTitle );
                        $article->doDeleteArticle( $reason ); # ignore errors
 
-                       /* refresh image metadata cache */
-                       new Image( $image, true );
+                       # Invalidate parser cache and client cache for pages using this image
+                       # This is left until relatively late to reduce lock time
+                       Title::touchArray( $linksTo );
+
+                       /* Delete thumbnails and refresh image metadata cache */
+                       $this->img->purgeCache();
+
 
                        $deleted = $image;
                }
@@ -338,13 +415,13 @@ class ImagePage extends Article {
 
                $sk = $wgUser->getSkin();
                $loglink = $sk->makeKnownLinkObj(
-                       Title::makeTitle( NS_SPECIAL, 'Delete/log' ),
+                       Title::makeTitle( NS_SPECIAL, 'Log/delete' ),
                        wfMsg( 'deletionlog' ) );
 
                $text = wfMsg( 'deletedtext', $deleted, $loglink );
 
                $wgOut->addHTML( '<p>' . $text . "</p>\n" );
-               $wgOut->returnToMain( false );
+               $wgOut->returnToMain( false, $this->mTitle->getPrefixedText() );
        }
 
        function doDeleteOldImage( $oldimage )
@@ -416,8 +493,7 @@ class ImagePage extends Article {
                $oldver = wfTimestampNow() . "!{$name}";
                
                $dbr =& wfGetDB( DB_SLAVE );
-               $size = $dbr->selectField( 'oldimage', 'oi_size', 'oi_archive_name=\'' .
-                 $dbr->strencode( $oldimage ) . "'" );
+               $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage )  );
 
                if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
                        $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
@@ -426,24 +502,17 @@ class ImagePage extends Article {
                if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
                        $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
                }
-               wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
-
-               /* refresh image metadata cache */
-               new Image( $name, true );
-
-               # Squid purging
-               if ( $wgUseSquid ) {
-                       $urlArr = Array(
-                               $wgInternalServer.wfImageArchiveUrl( $name ),
-                               $wgInternalServer . Image::wfImageUrl( $name )
-                       );
-                       wfPurgeSquidServers($urlArr);
-               }
+
+               # Record upload and update metadata cache
+               $img = Image::newFromName( $name );
+               $img->recordUpload( $oldver, wfMsg( "reverted" ) );
 
                $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
                $wgOut->setRobotpolicy( 'noindex,nofollow' );
                $wgOut->addHTML( wfMsg( 'imagereverted' ) );
-               $wgOut->returnToMain( false );
+
+               $descTitle = $img->getTitle();
+               $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
        }
 }
 
@@ -457,7 +526,7 @@ class ImageHistoryList {
        }
        
        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;
        }
@@ -476,7 +545,7 @@ class ImageHistoryList {
                $cur = wfMsg( 'cur' );
 
                if ( $iscur ) {
-                       $url = Image::wfImageUrl( $img );
+                       $url = Image::imageUrl( $img );
                        $rlink = $cur;
                        if ( $wgUser->isAllowed('delete') ) {
                                $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .