(bug 973) quickie brake for images with many many uses
[lhc/web/wiklou.git] / includes / ImagePage.php
index 8073cdd..139f024 100644 (file)
@@ -1,9 +1,17 @@
 <?php
-
-/*
-       Special handling for image description pages
-*/
-
+/**
+ * @package MediaWiki
+ */
+
+/**
+ *
+ */
+require_once( 'Image.php' );
+
+/**
+ * Special handling for image description pages
+ * @package MediaWiki
+ */
 class ImagePage extends Article {
 
        /* private */ var $img;  // Image object this page is shown for. Initilaized in openShowImage, not
@@ -29,23 +37,53 @@ class ImagePage extends Article {
 
        function openShowImage()
        {
-               global $wgOut, $wgUser,$wgRequest;
+               global $wgOut, $wgUser, $wgImageLimits, $wgRequest, $wgUseImageResize;
                $this->img  = Image::newFromTitle( $this->mTitle );
-               $url  = $this->img->getUrl();
+               $url  = $this->img->getViewURL();
+               $anchoropen = '';
+               $anchorclose = '';
+               if ( $wgUseImageResize && $wgUser->getOption( 'imagesize' ) != '' ) {
+                       $max = $wgImageLimits[ intval( $wgUser->getOption( 'imagesize' ) ) ];
+                       $maxWidth = $max[0];
+                       $maxHeight = $max[1];
+               }
+
 
                if ( $this->img->exists() ) {
 
                        $sk = $wgUser->getSkin();
                        
-                       if ( $this->img->getType() != "" ) {
+                       if ( $this->img->getType() != '' ) {
                                # image
-                               $s = "<div class=\"fullImage\">" .
-                                    "<img src=\"{$url}\" width=\"" . $this->img->getWidth() . "\" height=\"" . $this->img->getHeight() .
-                                    "\" alt=\"".$wgRequest->getVal( 'image' )."\" /></div>";
+                               $width = $this->img->getWidth();
+                               $height = $this->img->getHeight();
+                               $msg = wfMsg('showbigimage', $width, $height, intval( $this->img->getSize()/1024 ) );
+                               if ( $width > $maxWidth && $wgUseImageResize ) {
+                                       $anchoropen  = "<a href=\"{$url}\">";
+                                       $anchorclose = "<br>{$msg}</a>";
+
+                                       $url = $this->img->createThumb( $maxWidth );
+                                       $height = floor( $height * $maxWidth / $width );
+                                       $width  = $maxWidth;
+                               } 
+                               if ( $height > $maxHeight && $wgUseImageResize ) {
+                                       $anchoropen  = "<a href=\"{$url}\">";
+                                       $anchorclose = "<br>{$msg}</a>";
+
+                                       $width = floor( $width * $maxHeight / $height );
+                                       $height = $maxHeight;
+                                       $url = $this->img->createThumb( $width );
+                               }
+                               $s = "<div class=\"fullImageLink\">" . $anchoropen .
+                                    "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
+                                    htmlspecialchars( $wgRequest->getVal( 'image' ) )."\" />" . $anchorclose . "</div>";
                        } else {
                                $s = "<div class=\"fullMedia\">".$sk->makeMediaLink($this->img->getName(),"")."</div>";
                        }
                        $wgOut->addHTML( $s );
+                       if($this->img->fromSharedDirectory) {
+                               $wgOut->addWikiText("<div class=\"sharedUploadNotice\">".wfMsg("sharedupload")."</div>");
+                       }
                }
        }
        
@@ -54,28 +92,32 @@ class ImagePage extends Article {
                # For overloading
        }
 
-       # If the page we've just displayed is in the "Image" namespace,
-       # we follow it with an upload history of the image and its usage.
-
+       /**
+        * If the page we've just displayed is in the "Image" namespace,
+        * we follow it with an upload history of the image and its usage.
+        */
        function imageHistory()
        {
                global $wgUser, $wgOut;
 
                $sk = $wgUser->getSkin();
-               $s = $sk->beginImageHistoryList();              
 
                $line = $this->img->nextHistoryLine();
 
-               $s .= $sk->imageHistoryLine( true, $line->img_timestamp,
-                 $this->mTitle->getDBkey(),  $line->img_user,
-                 $line->img_user_text, $line->img_size, $line->img_description );
-
-               while ( $line = $this->img->nextHistoryLine() ) {
-                       $s .= $sk->imageHistoryLine( false, $line->img_timestamp,
-                         $line->oi_archive_name, $line->img_user,
-                         $line->img_user_text, $line->img_size, $line->img_description );
-               }
-               $s .= $sk->endImageHistoryList();
+               if ( $line ) {
+                       $list =& new ImageHistoryList( $sk );
+                       $s = $list->beginImageHistoryList() .
+                               $list->imageHistoryLine( true, $line->img_timestamp,
+                                       $this->mTitle->getDBkey(),  $line->img_user,
+                                       $line->img_user_text, $line->img_size, $line->img_description );
+
+                       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 );
+                       }
+                       $s .= $list->endImageHistoryList();
+               } else { $s=''; }
                $wgOut->addHTML( $s );
        }
 
@@ -83,21 +125,22 @@ class ImagePage extends Article {
        {
                global $wgUser, $wgOut;
 
-               $wgOut->addHTML( "<h2>" . wfMsg( "imagelinks" ) . "</h2>\n" );
+               $wgOut->addHTML( '<h2>' . wfMsg( 'imagelinks' ) . "</h2>\n" );
 
-               $dbr =& wfGetDB( DB_READ );
+               $dbr =& wfGetDB( DB_SLAVE );
                $cur = $dbr->tableName( 'cur' );
                $imagelinks = $dbr->tableName( 'imagelinks' );
                
                $sql = "SELECT cur_namespace,cur_title FROM $imagelinks,$cur WHERE il_to=" .
-                 $dbr->addQuotes( $this->mTitle->getDBkey() ) . " AND il_from=cur_id";
-               $res = $dbr->query( $sql, DB_READ, "Article::imageLinks" );
+                 $dbr->addQuotes( $this->mTitle->getDBkey() ) . " AND il_from=cur_id"
+                 . " LIMIT 500"; # quickie emergency brake
+               $res = $dbr->query( $sql, DB_SLAVE, "Article::imageLinks" );
 
                if ( 0 == $dbr->numRows( $res ) ) {
-                       $wgOut->addHtml( "<p>" . wfMsg( "nolinkstoimage" ) . "</p>\n" );
+                       $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
                        return;
                }
-               $wgOut->addHTML( "<p>" . wfMsg( "linkstoimage" ) .  "</p>\n<ul>" );
+               $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) .  "</p>\n<ul>" );
 
                $sk = $wgUser->getSkin();
                while ( $s = $dbr->fetchObject( $res ) ) {
@@ -118,7 +161,7 @@ class ImagePage extends Article {
                
                # Only sysops can delete images. Previously ordinary users could delete 
                # old revisions, but this is no longer the case.
-               if ( !$wgUser->isSysop() ) {
+               if ( !$wgUser->isAllowed('delete') ) {
                        $wgOut->sysopRequired();
                        return;
                }
@@ -128,10 +171,10 @@ class ImagePage extends Article {
                }
 
                # Better double-check that it hasn't been deleted yet!
-               $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
+               $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
                if ( !is_null( $image ) ) {
-                       if ( "" == trim( $image ) ) {
-                               $wgOut->fatalError( wfMsg( "cannotdelete" ) );
+                       if ( '' == trim( $image ) ) {
+                               $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
                                return;
                        }
                }
@@ -143,32 +186,51 @@ class ImagePage extends Article {
                }
                
                if ( !is_null( $image ) ) {
-                       $q = "&image=" . urlencode( $image );
+                       $q = '&image=' . urlencode( $image );
                } else if ( !is_null( $oldimage ) ) {
-                       $q = "&oldimage=" . urlencode( $oldimage );
+                       $q = '&oldimage=' . urlencode( $oldimage );
                } else {
-                       $q = "";
+                       $q = '';
                }
                return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
        }
 
        function doDelete()
        {
-               global $wgOut, $wgUser, $wgLang, $wgRequest;
+               global $wgOut, $wgUser, $wgContLang, $wgRequest;
                global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
-               $fname = "Article::doDelete";
+               $fname = 'ImagePage::doDelete';
 
                $reason = $wgRequest->getVal( 'wpReason' );
                $image = $wgRequest->getVal( 'image' );
                $oldimage = $wgRequest->getVal( 'oldimage' );
                
-               $dbw =& wfGetDB( DB_WRITE );
+               $dbw =& wfGetDB( DB_MASTER );
 
-               if ( !is_null( $image ) ) {
+               if ( !is_null( $oldimage ) ) {
+                       # Squid purging
+                       if ( $wgUseSquid ) {
+                               $urlArr = Array(
+                                       $wgInternalServer.wfImageArchiveUrl( $oldimage )
+                               );
+                               wfPurgeSquidServers($urlArr);
+                       }
+                       $this->doDeleteOldImage( $oldimage );
+                       $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
+                       $deleted = $oldimage;
+               } else {
+                       if ( is_null ( $image ) ) {
+                               $image = $this->mTitle->getDBkey();
+                       }
                        $dest = wfImageDir( $image );
                        $archive = wfImageDir( $image );
-                       if ( ! @unlink( "{$dest}/{$image}" ) ) {
-                               $wgOut->fileDeleteError( "{$dest}/{$image}" );
+                       
+                       # Delete the image file if it exists; due to sync problems
+                       # or manual trimming sometimes the file will be missing.
+                       $targetFile = "{$dest}/{$image}";
+                       if( file_exists( $targetFile ) && ! @unlink( $targetFile ) ) {
+                               # If the deletion operation actually failed, bug out:
+                               $wgOut->fileDeleteError( $targetFile );
                                return;
                        }
                        $dbw->delete( 'image', array( 'img_name' => $image ) );
@@ -184,7 +246,7 @@ class ImagePage extends Article {
                        
 
                        $urlArr = Array();
-                       while ( $s = $dbr->fetchObject( $res ) ) {
+                       while ( $s = $dbw->fetchObject( $res ) ) {
                                $this->doDeleteOldImage( $s->oi_archive_name );
                                $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name );
                        }       
@@ -201,37 +263,24 @@ class ImagePage extends Article {
                        # Image itself is now gone, and database is cleaned.
                        # Now we remove the image description page.
 
-                       $nt = Title::newFromText( $wgLang->getNsText( Namespace::getImage() ) . ":" . $image );
+                       $nt = Title::newFromText( $wgContLang->getNsText( Namespace::getImage() ) . ":" . $image );
                        $article = new Article( $nt );
                        $article->doDeleteArticle( $reason ); # ignore errors
 
                        $deleted = $image;
-               } else if ( !is_null( $oldimage ) ) {
-                       # Squid purging
-                       if ( $wgUseSquid ) {
-                               $urlArr = Array(
-                                       $wgInternalServer.wfImageArchiveUrl( $oldimage )
-                               );
-                               wfPurgeSquidServers($urlArr);
-                       }
-                       $this->doDeleteOldImage( $oldimage );
-                       $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
-                       $deleted = $oldimage;
-               } else {
-                       $this->doDeleteArticle( $reason ); # ignore errors
-                       $deleted = $this->mTitle->getPrefixedText();
                }
-               $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
-               $wgOut->setRobotpolicy( "noindex,nofollow" );
+
+               $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
+               $wgOut->setRobotpolicy( 'noindex,nofollow' );
 
                $sk = $wgUser->getSkin();
-               $loglink = $sk->makeKnownLink( $wgLang->getNsText(
+               $loglink = $sk->makeKnownLink( $wgContLang->getNsText(
                  Namespace::getWikipedia() ) .
-                 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
+                 ':' . wfMsg( 'dellogpage' ), wfMsg( 'deletionlog' ) );
 
-               $text = wfMsg( "deletedtext", $deleted, $loglink );
+               $text = wfMsg( 'deletedtext', $deleted, $loglink );
 
-               $wgOut->addHTML( "<p>" . $text . "</p>\n" );
+               $wgOut->addHTML( '<p>' . $text . "</p>\n" );
                $wgOut->returnToMain( false );
        }
 
@@ -241,8 +290,22 @@ class ImagePage extends Article {
 
                $name = substr( $oldimage, 15 );
                $archive = wfImageArchiveDir( $name );
-               if ( ! unlink( "{$archive}/{$oldimage}" ) ) {
+               
+               # 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.
+               # Also some records may end up with an empty oi_archive_name field
+               # if the original file was missing when a new upload was made;
+               # don't try to delete the directory then!
+               #
+               $targetFile = "{$archive}/{$oldimage}";
+               if( $oldimage != '' && file_exists( $targetFile ) && !@unlink( $targetFile ) ) {
+                       # If we actually have a file and can't delete it, throw an error.
                        $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
+               } else {
+                       # Log the deletion
+                       $log = new LogPage( 'delete' );
+                       $log->addEntry( 'delete', $this->mTitle, wfMsg('deletedrevision',$oldimage) );
                }
        }
 
@@ -252,15 +315,23 @@ class ImagePage extends Article {
                global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
 
                $oldimage = $wgRequest->getText( 'oldimage' );
-               
                if ( strlen( $oldimage ) < 16 ) {
-                       $wgOut->unexpectedValueError( "oldimage", $oldimage );
+                       $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
                        return;
                }
+               if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
+                       $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
+                       return;
+               }
+
                if ( wfReadOnly() ) {
                        $wgOut->readOnlyPage();
                        return;
                }
+               if ( ! $this->mTitle->userCanEdit() ) {
+                       $wgOut->sysopRequired();
+                       return;
+               }
                $name = substr( $oldimage, 15 );
 
                $dest = wfImageDir( $name );
@@ -268,13 +339,13 @@ class ImagePage extends Article {
                $curfile = "{$dest}/{$name}";
 
                if ( ! is_file( $curfile ) ) {
-                       $wgOut->fileNotFoundError( $curfile );
+                       $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
                        return;
                }
                $oldver = wfTimestampNow() . "!{$name}";
                
-               $dbr =& wfGetDB( DB_READ );
-               $size = $dbr->getField( "oldimage", "oi_size", "oi_archive_name='" .
+               $dbr =& wfGetDB( DB_SLAVE );
+               $size = $dbr->selectField( 'oldimage', 'oi_size', 'oi_archive_name=\'' .
                  $dbr->strencode( $oldimage ) . "'" );
 
                if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
@@ -294,11 +365,86 @@ class ImagePage extends Article {
                        wfPurgeSquidServers($urlArr);
                }
 
-               $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
-               $wgOut->setRobotpolicy( "noindex,nofollow" );
-               $wgOut->addHTML( wfMsg( "imagereverted" ) );
+               $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
+               $wgOut->setRobotpolicy( 'noindex,nofollow' );
+               $wgOut->addHTML( wfMsg( 'imagereverted' ) );
                $wgOut->returnToMain( false );
        }
 }
 
+class ImageHistoryList {
+       function ImageHistoryList( &$skin ) {
+               $this->skin =& $skin;
+       }
+       
+       function beginImageHistoryList() {
+               $s = "\n<h2>" . wfMsg( 'imghistory' ) . "</h2>\n" .
+                 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
+               return $s;
+       }
+
+       function endImageHistoryList() {
+               $s = "</ul>\n";
+               return $s;
+       }
+
+       function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
+               global $wgUser, $wgLang, $wgContLang, $wgTitle;
+
+               $datetime = $wgLang->timeanddate( $timestamp, true );
+               $del = wfMsg( 'deleteimg' );
+               $delall = wfMsg( 'deleteimgcompletely' );
+               $cur = wfMsg( 'cur' );
+
+               if ( $iscur ) {
+                       $url = Image::wfImageUrl( $img );
+                       $rlink = $cur;
+                       if ( $wgUser->isAllowed('delete') ) {
+                               $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
+                                 '&action=delete' );
+                               $style = $this->skin->getInternalLinkAttributes( $link, $delall );
+
+                               $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
+                       } else {
+                               $dlink = $del;
+                       }
+               } else {
+                       $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
+                       if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
+                               $rlink = $this->skin->makeKnownLink( $wgTitle->getPrefixedText(),
+                                          wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
+                                          urlencode( $img ) );
+                               $dlink = $this->skin->makeKnownLink( $wgTitle->getPrefixedText(),
+                                          $del, 'action=delete&oldimage=' . urlencode( $img ) );
+                       } else {
+                               # Having live active links for non-logged in users
+                               # means that bots and spiders crawling our site can
+                               # inadvertently change content. Baaaad idea.
+                               $rlink = wfMsg( 'revertimg' );
+                               $dlink = $del;
+                       }
+               }
+               if ( 0 == $user ) {
+                       $userlink = $usertext;
+               } else {
+                       $userlink = $this->skin->makeLink( $wgContLang->getNsText( Namespace::getUser() ) .
+                                      ':'.$usertext, $usertext );
+               }
+               $nbytes = wfMsg( 'nbytes', $size );
+               $style = $this->skin->getInternalLinkAttributes( $url, $datetime );
+
+               $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
+                 . " . . {$userlink} ({$nbytes})";
+
+               if ( '' != $description && '*' != $description ) {
+                       $sk=$wgUser->getSkin();
+                       $s .= $wgContLang->emphasize(' (' . $sk->formatComment($description,$wgTitle) . ')');
+               }
+               $s .= "</li>\n";
+               return $s;
+       }
+
+}
+
+
 ?>