Don't force edit encoding when LanguageEo.php is loaded; defer until $wgContLang...
[lhc/web/wiklou.git] / includes / ImagePage.php
index d29a431..d0285e8 100644 (file)
@@ -6,6 +6,9 @@
 /**
  *
  */
+if( !defined( 'MEDIAWIKI' ) )
+       die();
+
 require_once( 'Image.php' );
 
 /**
@@ -18,7 +21,7 @@ class ImagePage extends Article {
                                 // available in doDelete etc.
 
        function view() {
-               if ( Namespace::getImage() == $this->mTitle->getNamespace() ) {
+               if( $this->mTitle->getNamespace() == NS_IMAGE ) {
                        $this->openShowImage();
                }
 
@@ -28,7 +31,7 @@ class ImagePage extends Article {
                # follow it with the history list and link list for the image
                # it describes.
 
-               if ( Namespace::getImage() == $this->mTitle->getNamespace() ) {
+               if( $this->mTitle->getNamespace() == NS_IMAGE ) {
                        $this->closeShowImage();
                        $this->imageHistory();
                        $this->imageLinks();
@@ -40,14 +43,21 @@ class ImagePage extends Article {
                global $wgOut, $wgUser, $wgImageLimits, $wgRequest, 
                       $wgUseImageResize, $wgRepositoryBaseUrl;
                $this->img  = Image::newFromTitle( $this->mTitle );
-               $url  = $this->img->getViewURL();
+               $full_url  = $this->img->getViewURL();
                $anchoropen = '';
                $anchorclose = '';
-               if ( $wgUseImageResize && $wgUser->getOption( 'imagesize' ) != '' ) {
-                       $max = $wgImageLimits[ intval( $wgUser->getOption( 'imagesize' ) ) ];
-                       $maxWidth = $max[0];
-                       $maxHeight = $max[1];
+
+               if( $wgUser->getOption( 'imagesize' ) == '' ) {
+                       $sizeSel = User::getDefaultOption( 'imagesize' );
+               } else {
+                       $sizeSel = IntVal( $wgUser->getOption( 'imagesize' ) );
+               }
+               if( !isset( $wgImageLimits[$sizeSel] ) ) {
+                       $sizeSel = User::getDefaultOption( 'imagesize' );
                }
+               $max = $wgImageLimits[$sizeSel];
+               $maxWidth = $max[0];
+               $maxHeight = $max[1];
 
 
                if ( $this->img->exists() ) {
@@ -59,27 +69,43 @@ class ImagePage extends Article {
                                $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 );
+                               if ( $width > $maxWidth ) {
                                        $height = floor( $height * $maxWidth / $width );
                                        $width  = $maxWidth;
                                } 
-                               if ( $height > $maxHeight && $wgUseImageResize ) {
-                                       $anchoropen  = "<a href=\"{$url}\">";
-                                       $anchorclose = "<br>{$msg}</a>";
-
+                               if ( $height > $maxHeight ) {
                                        $width = floor( $width * $maxHeight / $height );
                                        $height = $maxHeight;
-                                       $url = $this->img->createThumb( $width );
                                }
-                               $s = "<div class=\"fullImageLink\">" . $anchoropen .
+                               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();
+                                               }
+                                       } else {
+                                               # No resize ability? Show the full image, but scale
+                                               # it down in the browser so it fits on the page.
+                                               $url = $full_url;
+                                       }
+                                       $anchoropen  = "<a href=\"{$full_url}\">";
+                                       $anchorclose = "</a><br />\n$anchoropen{$msg}</a>";
+                               } else {
+                                       $url = $full_url;
+                               }
+                               $s = '<div class="fullImageLink">' . $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>";
+                               $s = "<div class=\"fullMedia\">" . $sk->makeMediaLink( $this->img->getName(),'' ) . '</div>';
                        }
                        $wgOut->addHTML( $s );
                        if($this->img->fromSharedDirectory) {
@@ -134,11 +160,11 @@ class ImagePage extends Article {
                $wgOut->addHTML( '<h2>' . wfMsg( 'imagelinks' ) . "</h2>\n" );
 
                $dbr =& wfGetDB( DB_SLAVE );
-               $cur = $dbr->tableName( 'cur' );
+               $page = $dbr->tableName( 'page' );
                $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"
+               $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
                $res = $dbr->query( $sql, DB_SLAVE, "Article::imageLinks" );
 
@@ -150,7 +176,7 @@ class ImagePage extends Article {
 
                $sk = $wgUser->getSkin();
                while ( $s = $dbr->fetchObject( $res ) ) {
-                       $name = Title::MakeTitle( $s->cur_namespace, $s->cur_title );
+                       $name = Title::MakeTitle( $s->page_namespace, $s->page_title );
                        $link = $sk->makeKnownLinkObj( $name, "" );
                        $wgOut->addHTML( "<li>{$link}</li>\n" );
                }
@@ -161,7 +187,7 @@ class ImagePage extends Article {
        {
                global $wgUser, $wgOut, $wgRequest;
 
-               $confirm = $wgRequest->getBool( 'wpConfirm' );
+               $confirm = $wgRequest->getBool( 'wpConfirmB' );
                $image = $wgRequest->getVal( 'image' );
                $oldimage = $wgRequest->getVal( 'oldimage' );
                
@@ -178,16 +204,19 @@ class ImagePage extends Article {
 
                # Better double-check that it hasn't been deleted yet!
                $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
-               if ( !is_null( $image ) ) {
-                       if ( '' == trim( $image ) ) {
-                               $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
-                               return;
-                       }
+               if ( ( !is_null( $image ) )
+                 && ( '' == trim( $image ) ) ) {
+                       $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
+                       return;
                }
                
                # Deleting old images doesn't require confirmation
                if ( !is_null( $oldimage ) || $confirm ) {
-                       $this->doDelete();
+                       if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
+                               $this->doDelete();
+                       } else {
+                               $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
+                       }
                        return;
                }
                
@@ -208,12 +237,19 @@ class ImagePage extends Article {
                $fname = 'ImagePage::doDelete';
 
                $reason = $wgRequest->getVal( 'wpReason' );
-               $image = $wgRequest->getVal( 'image' );
                $oldimage = $wgRequest->getVal( 'oldimage' );
                
                $dbw =& wfGetDB( DB_MASTER );
 
                if ( !is_null( $oldimage ) ) {
+                       if ( strlen( $oldimage ) < 16 ) {
+                               $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
+                               return;
+                       }
+                       if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
+                               $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
+                               return;
+                       }
                        # Squid purging
                        if ( $wgUseSquid ) {
                                $urlArr = Array(
@@ -225,9 +261,7 @@ class ImagePage extends Article {
                        $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
                        $deleted = $oldimage;
                } else {
-                       if ( is_null ( $image ) ) {
-                               $image = $this->mTitle->getDBkey();
-                       }
+                       $image = $this->mTitle->getDBkey();
                        $dest = wfImageDir( $image );
                        $archive = wfImageDir( $image );
                        
@@ -269,10 +303,13 @@ class ImagePage extends Article {
                        # Image itself is now gone, and database is cleaned.
                        # Now we remove the image description page.
 
-                       $nt = Title::newFromText( $wgContLang->getNsText( Namespace::getImage() ) . ":" . $image );
+                       $nt = Title::makeTitleSafe( NS_IMAGE, $image );
                        $article = new Article( $nt );
                        $article->doDeleteArticle( $reason ); # ignore errors
 
+                       /* refresh image metadata cache */
+                       new Image( $image, true );
+
                        $deleted = $image;
                }
 
@@ -280,9 +317,9 @@ class ImagePage extends Article {
                $wgOut->setRobotpolicy( 'noindex,nofollow' );
 
                $sk = $wgUser->getSkin();
-               $loglink = $sk->makeKnownLink( $wgContLang->getNsText(
-                 Namespace::getWikipedia() ) .
-                 ':' . wfMsg( 'dellogpage' ), wfMsg( 'deletionlog' ) );
+               $loglink = $sk->makeKnownLinkObj(
+                       Title::makeTitle( NS_SPECIAL, 'Delete/log' ),
+                       wfMsg( 'deletionlog' ) );
 
                $text = wfMsg( 'deletedtext', $deleted, $loglink );
 
@@ -317,7 +354,7 @@ class ImagePage extends Article {
 
        function revert()
        {
-               global $wgOut, $wgRequest;
+               global $wgOut, $wgRequest, $wgUser;
                global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
 
                $oldimage = $wgRequest->getText( 'oldimage' );
@@ -334,10 +371,18 @@ class ImagePage extends Article {
                        $wgOut->readOnlyPage();
                        return;
                }
+               if( $wgUser->isAnon() ) {
+                       $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
+                       return;
+               }
                if ( ! $this->mTitle->userCanEdit() ) {
                        $wgOut->sysopRequired();
                        return;
                }
+               if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
+                       $wgOut->errorpage( 'internalerror', 'sessionfailure' );
+                       return;
+               }               
                $name = substr( $oldimage, 15 );
 
                $dest = wfImageDir( $name );
@@ -362,6 +407,10 @@ class ImagePage extends Article {
                        $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(
@@ -378,6 +427,10 @@ class ImagePage extends Article {
        }
 }
 
+/**
+ * @todo document
+ * @package MediaWiki
+ */
 class ImageHistoryList {
        function ImageHistoryList( &$skin ) {
                $this->skin =& $skin;
@@ -417,11 +470,13 @@ class ImageHistoryList {
                } else {
                        $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
                        if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
+                               $token = urlencode( $wgUser->editToken( $img ) );
                                $rlink = $this->skin->makeKnownLink( $wgTitle->getPrefixedText(),
                                           wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
-                                          urlencode( $img ) );
+                                          urlencode( $img ) . "&wpEditToken=$token" );
                                $dlink = $this->skin->makeKnownLink( $wgTitle->getPrefixedText(),
-                                          $del, 'action=delete&oldimage=' . urlencode( $img ) );
+                                          $del, 'action=delete&oldimage=' . urlencode( $img ) .
+                                          "&wpEditToken=$token" );
                        } else {
                                # Having live active links for non-logged in users
                                # means that bots and spiders crawling our site can
@@ -433,8 +488,9 @@ class ImageHistoryList {
                if ( 0 == $user ) {
                        $userlink = $usertext;
                } else {
-                       $userlink = $this->skin->makeLink( $wgContLang->getNsText( Namespace::getUser() ) .
-                                      ':'.$usertext, $usertext );
+                       $userlink = $this->skin->makeLinkObj(
+                               Title::makeTitle( NS_USER, $usertext ),
+                               $usertext );
                }
                $nbytes = wfMsg( 'nbytes', $size );
                $style = $this->skin->getInternalLinkAttributes( $url, $datetime );
@@ -442,10 +498,7 @@ class ImageHistoryList {
                $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 .= $this->skin->commentBlock( $description, $wgTitle );
                $s .= "</li>\n";
                return $s;
        }