* (bug 12938) Fix template expansion and 404 returns for action=raw with section
[lhc/web/wiklou.git] / includes / ImagePage.php
index db2846b..8336837 100644 (file)
@@ -16,14 +16,19 @@ if( !defined( 'MEDIAWIKI' ) )
 class ImagePage extends Article {
 
        /* private */ var $img;  // Image object this page is shown for
+       /* private */ var $repo;
        var $mExtraDescription = false;
 
-       function __construct( $title ) {
+       function __construct( $title, $time = false ) {
                parent::__construct( $title );
-               $this->img = wfFindFile( $this->mTitle );
+               $this->img = wfFindFile( $this->mTitle, $time );
                if ( !$this->img ) {
                        $this->img = wfLocalFile( $this->mTitle );
+                       $this->current = $this->img;
+               } else {
+                       $this->current = $time ? wfLocalFile( $this->mTitle ) : $this->img;
                }
+               $this->repo = $this->img->repo;
        }
 
        /**
@@ -46,6 +51,7 @@ class ImagePage extends Article {
                        return Article::view();
 
                if ($wgShowEXIF && $this->img->exists()) {
+                       // FIXME: bad interface, see note on MediaHandler::formatMetadata(). 
                        $formattedMetadata = $this->img->formatMetadata();
                        $showmeta = $formattedMetadata !== false;
                } else {
@@ -63,7 +69,7 @@ class ImagePage extends Article {
                } else {
                        # Just need to set the right headers
                        $wgOut->setArticleFlag( true );
-                       $wgOut->setRobotpolicy( 'index,follow' );
+                       $wgOut->setRobotpolicy( 'noindex,nofollow' );
                        $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
                        $this->viewUpdates();
                }
@@ -115,6 +121,8 @@ class ImagePage extends Article {
        /**
         * Make a table with metadata to be shown in the output page.
         *
+        * FIXME: bad interface, see note on MediaHandler::formatMetadata(). 
+        *
         * @access private
         *
         * @param array $exif The array containing the EXIF data
@@ -124,7 +132,7 @@ class ImagePage extends Article {
                $r = wfMsg( 'metadata-help' ) . "\n\n";
                $r .= "{| id=mw_metadata class=mw_metadata\n";
                foreach ( $metadata as $type => $stuff ) {
-                       foreach ( $stuff as $k => $v ) {
+                       foreach ( $stuff as $v ) {
                                $class = Sanitizer::escapeId( $v['id'] );
                                if( $type == 'collapsed' ) {
                                        $class .= ' collapsable';
@@ -152,7 +160,7 @@ class ImagePage extends Article {
        }
 
        function openShowImage() {
-               global $wgOut, $wgUser, $wgImageLimits, $wgRequest, $wgLang;
+               global $wgOut, $wgUser, $wgImageLimits, $wgRequest, $wgLang, $wgContLang;
 
                $full_url  = $this->img->getURL();
                $linkAttribs = false;
@@ -171,6 +179,7 @@ class ImagePage extends Article {
                $maxWidth = $max[0];
                $maxHeight = $max[1];
                $sk = $wgUser->getSkin();
+               $dirmark = $wgContLang->getDirMark();
 
                if ( $this->img->exists() ) {
                        # image
@@ -188,14 +197,15 @@ class ImagePage extends Article {
                        $mime = $this->img->getMimeType();
                        $showLink = false;
                        $linkAttribs = array( 'href' => $full_url );
+                       $longDesc = $this->img->getLongDesc();
 
-      wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this , &$wgOut ) )        ;
+                       wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this , &$wgOut ) )       ;
 
-                       if ( $this->img->allowInlineDisplay() and $width and $height) {
+                       if ( $this->img->allowInlineDisplay() ) {
                                # image
 
                                # "Download high res version" link below the image
-                               $msgsize = wfMsgHtml('file-info-size', $width_orig, $height_orig, $sk->formatSize( $this->img->getSize() ), $mime );
+                               #$msgsize = wfMsgHtml('file-info-size', $width_orig, $height_orig, $sk->formatSize( $this->img->getSize() ), $mime );
                                # We'll show a thumbnail of this image
                                if ( $width > $maxWidth || $height > $maxHeight ) {
                                        # Calculate the thumbnail size.
@@ -213,7 +223,7 @@ class ImagePage extends Article {
                                        }
                                        $msgbig  = wfMsgHtml( 'show-big-image' );
                                        $msgsmall = wfMsgExt( 'show-big-image-thumb',
-                                               array( 'parseinline' ), $width, $height );
+                                               array( 'parseinline' ), $wgLang->formatNum( $width ), $wgLang->formatNum( $height ) );
                                } else {
                                        # Image is small enough to show full size on image page
                                        $msgbig = htmlspecialchars( $this->img->getName() );
@@ -229,21 +239,20 @@ class ImagePage extends Article {
                                } else {
                                        $anchorclose .= 
                                                $msgsmall .
-                                               '<br />' . Xml::tags( 'a', $linkAttribs,  $msgbig ) . ' ' . $msgsize;
+                                               '<br />' . Xml::tags( 'a', $linkAttribs,  $msgbig ) . "$dirmark " . $longDesc;
                                }
 
                                if ( $this->img->isMultipage() ) {
                                        $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
                                }
 
-                               $imgAttribs = array(
-                                       'border' => 0,
-                                       'alt' => $this->img->getTitle()->getPrefixedText()
-                               );
-
                                if ( $thumbnail ) {
+                                       $options = array( 
+                                               'alt' => $this->img->getTitle()->getPrefixedText(),
+                                               'file-link' => true,
+                                       );
                                        $wgOut->addHTML( '<div class="fullImageLink" id="file">' . 
-                                               $thumbnail->toHtml( $imgAttribs, $linkAttribs ) .
+                                               $thumbnail->toHtml( $options ) .
                                                $anchorclose . '</div>' );
                                }
 
@@ -291,9 +300,9 @@ class ImagePage extends Article {
                                if ($this->img->isSafeFile()) {
                                        $icon= $this->img->iconThumb();
 
-                                       $wgOut->addHTML( '<div class="fullImageLink" id="file"><a href="' . $full_url . '">' .
-                                       $icon->toHtml() .
-                                       '</a></div>' );
+                                       $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
+                                       $icon->toHtml( array( 'desc-link' => true ) ) .
+                                       '</div>' );
                                }
 
                                $showLink = true;
@@ -301,26 +310,14 @@ class ImagePage extends Article {
 
 
                        if ($showLink) {
-                               // Workaround for incorrect MIME type on SVGs uploaded in previous versions
-                               if ($mime == 'image/svg') $mime = 'image/svg+xml';
-
                                $filename = wfEscapeWikiText( $this->img->getName() );
-                               $info = wfMsg( 'file-info', $sk->formatSize( $this->img->getSize() ), $mime );
-                               $infores = '';
-
-                               // Check for MIME type. Other types may have more information in the future.
-                               if (substr($mime,0,9) == 'image/svg' ) {
-                                       $infores = wfMsg('file-svg', $width_orig, $height_orig ) . '<br />';
-                               }
 
-                               global $wgContLang;
-                               $dirmark = $wgContLang->getDirMark();
                                if (!$this->img->isSafeFile()) {
                                        $warning = wfMsg( 'mediawarning' );
                                        $wgOut->addWikiText( <<<EOT
-<div class="fullMedia">$infores
+<div class="fullMedia">
 <span class="dangerousLink">[[Media:$filename|$filename]]</span>$dirmark
-<span class="fileInfo"> $info</span>
+<span class="fileInfo"> $longDesc</span>
 </div>
 
 <div class="mediaWarning">$warning</div>
@@ -328,8 +325,8 @@ EOT
                                                );
                                } else {
                                        $wgOut->addWikiText( <<<EOT
-<div class="fullMedia">$infores
-[[Media:$filename|$filename]]$dirmark <span class="fileInfo"> $info</span>
+<div class="fullMedia">
+[[Media:$filename|$filename]]$dirmark <span class="fileInfo"> $longDesc</span>
 </div>
 EOT
                                                );
@@ -417,22 +414,24 @@ EOT
 
                $sk = $wgUser->getSkin();
 
-               $line = $this->img->nextHistoryLine();
-
-               if ( $line ) {
-                       $list = new ImageHistoryList( $sk, $this->img );
+               if ( $this->img->exists() ) {
+                       $list = new ImageHistoryList( $sk, $this->current );
+                       $file = $this->current;
+                       $dims = $file->getDimensionsString();
                        $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_width, $line->img_height
+                               $list->imageHistoryLine( true, wfTimestamp(TS_MW, $file->getTimestamp()),
+                                       $this->mTitle->getDBkey(),  $file->getUser('id'),
+                                       $file->getUser('text'), $file->getSize(), $file->getDescription(),
+                                       $dims
                                );
 
-                       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->img_width, $line->img_height
+                       $hist = $this->img->getHistory();
+                       foreach( $hist as $file ) {
+                               $dims = $file->getDimensionsString();
+                               $s .= $list->imageHistoryLine( false, wfTimestamp(TS_MW, $file->getTimestamp()),
+                                       $file->getArchiveName(), $file->getUser('id'),
+                                       $file->getUser('text'), $file->getSize(), $file->getDescription(),
+                                       $dims
                                );
                        }
                        $s .= $list->endImageHistoryList();
@@ -479,187 +478,25 @@ EOT
                $wgOut->addHTML( "</ul>\n" );
        }
 
-       function delete()
-       {
-               global $wgUser, $wgOut, $wgRequest;
-
-               if ( !$this->img->exists() || !$this->img->isLocal() ) {
-                       # Use standard article deletion
+       /**
+        * Delete the file, or an earlier version of it
+        */
+       public function delete() {
+               if( !$this->img->exists() || !$this->img->isLocal() ) {
+                       // Standard article deletion
                        Article::delete();
                        return;
                }
-
-               $confirm = $wgRequest->wasPosted();
-               $reason = $wgRequest->getVal( 'wpReason' );
-               $image = $wgRequest->getVal( 'image' );
-               $oldimage = $wgRequest->getVal( 'oldimage' );
-
-               # Only sysops can delete images. Previously ordinary users could delete
-               # old revisions, but this is no longer the case.
-               if ( !$wgUser->isAllowed('delete') ) {
-                       $wgOut->permissionRequired( 'delete' );
-                       return;
-               }
-               if ( $wgUser->isBlocked() ) {
-                       $wgOut->blockedPage();
-                       return;
-               }
-               if ( wfReadOnly() ) {
-                       $wgOut->readOnlyPage();
-                       return;
-               }
-
-               # Better double-check that it hasn't been deleted yet!
-               $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
-               if ( ( !is_null( $image ) )
-                 && ( '' == trim( $image ) ) ) {
-                       $wgOut->showFatalError( wfMsg( 'cannotdelete' ) );
-                       return;
-               }
-
-               # Deleting old images doesn't require confirmation
-               if ( !is_null( $oldimage ) || $confirm ) {
-                       if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
-                               $this->doDeleteImage( $reason );
-                       } else {
-                               $wgOut->showFatalError( wfMsg( 'sessionfailure' ) );
-                       }
-                       return;
-               }
-
-               if ( !is_null( $image ) ) {
-                       $q = '&image=' . urlencode( $image );
-               } else if ( !is_null( $oldimage ) ) {
-                       $q = '&oldimage=' . urlencode( $oldimage );
-               } else {
-                       $q = '';
-               }
-               return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
-       }
-
-       /*
-        * Delete an image.
-        * Called doDeleteImage() not doDelete() so that Article::delete() doesn't 
-        * call back to here.
-        *
-        * @param $reason User provided reason for deletion.
-        */
-       function doDeleteImage( $reason ) {
-               global $wgOut, $wgRequest;
-
-               $oldimage = $wgRequest->getVal( 'oldimage' );
-
-               if ( !is_null( $oldimage ) ) {
-                       if ( strlen( $oldimage ) < 16 ) {
-                               $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
-                               return;
-                       }
-                       if( strpos( $oldimage, '/' ) !== false || strpos( $oldimage, '\\' ) !== false ) {
-                               $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
-                               return;
-                       }
-                       $status = $this->doDeleteOldImage( $oldimage );
-                       $deleted = $oldimage;
-               } else {
-                       $status = $this->img->delete( $reason );
-                       if ( !$status->isGood() ) {
-                               // Warning or error
-                               $wgOut->addWikiText( $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' ) );
-                       }
-                       if ( $status->ok ) {
-                               # Image itself is now gone, and database is cleaned.
-                               # Now we remove the image description page.
-                               $article = new Article( $this->mTitle );
-                               $article->doDeleteArticle( $reason ); # ignore errors
-                               $deleted = $this->img->getName();
-                       }
-               }
-
-               $wgOut->setRobotpolicy( 'noindex,nofollow' );
-
-               if ( !$status->ok ) {
-                       // Fatal error flagged
-                       $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
-                       $wgOut->returnToMain( false, $this->mTitle->getPrefixedText() );
-               } else {
-                       // Operation completed
-                       $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
-                       $loglink = '[[Special:Log/delete|' . wfMsg( 'deletionlog' ) . ']]';
-                       $text = wfMsg( 'deletedtext', $deleted, $loglink );
-                       $wgOut->addWikiText( $text );
-                       $wgOut->returnToMain( false, $this->mTitle->getPrefixedText() );
-               }
+               $deleter = new FileDeleteForm( $this->img );
+               $deleter->execute();
        }
 
        /**
-        * Delete an old revision of an image, 
-        * @return FileRepoStatus
+        * Revert the file to an earlier version
         */
-       function doDeleteOldImage( $oldimage ) {
-               global $wgOut;
-
-               $status = $this->img->deleteOld( $oldimage, '' );
-               if( !$status->isGood() ) {
-                       $wgOut->addWikiText( $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' ) );
-               }
-               if ( $status->ok ) {
-                       # Log the deletion
-                       $log = new LogPage( 'delete' );
-                       $log->addEntry( 'delete', $this->mTitle, wfMsg('deletedrevision',$oldimage) );
-               }
-               return $status;
-       }
-
-       function revert() {
-               global $wgOut, $wgRequest, $wgUser;
-
-               $oldimage = $wgRequest->getText( 'oldimage' );
-               if ( strlen( $oldimage ) < 16 ) {
-                       $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
-                       return;
-               }
-               if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
-                       $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
-                       return;
-               }
-
-               if ( wfReadOnly() ) {
-                       $wgOut->readOnlyPage();
-                       return;
-               }
-               if( $wgUser->isAnon() ) {
-                       $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' );
-                       return;
-               }
-               if ( ! $this->mTitle->userCan( 'edit' ) ) {
-                       $wgOut->readOnlyPage( $this->getContent(), true );
-                       return;
-               }
-               if ( $wgUser->isBlocked() ) {
-                       $wgOut->blockedPage();
-                       return;
-               }
-               if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
-                       $wgOut->showErrorPage( 'internalerror', 'sessionfailure' );
-                       return;
-               }
-
-               $sourcePath = $this->img->getArchiveVirtualUrl( $oldimage );
-               $comment = wfMsg( "reverted" );
-               // TODO: preserve file properties from DB instead of reloading from file
-               $status = $this->img->upload( $sourcePath, $comment, $comment );
-
-               if ( !$status->isGood() ) {
-                       $this->showError( $status->getWikiText() );
-                       return;
-               }
-
-               $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
-               $wgOut->setRobotpolicy( 'noindex,nofollow' );
-               $wgOut->addHTML( wfMsg( 'imagereverted' ) );
-
-               $descTitle = $this->img->getTitle();
-               $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
+       public function revert() {
+               $reverter = new FileRevertForm( $this->img );
+               $reverter->execute();
        }
        
        /**
@@ -699,7 +536,7 @@ EOT
  */
 class ImageHistoryList {
 
-       protected $img, $skin, $title;
+       protected $img, $skin, $title, $repo;
 
        public function __construct( $skin, $img ) {
                $this->skin = $skin;
@@ -712,12 +549,12 @@ class ImageHistoryList {
                return Xml::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) )
                        . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
                        . Xml::openElement( 'table', array( 'class' => 'filehistory' ) ) . "\n"
-                       . '<tr><th></th>'
-                       . ( $this->img->isLocal() && $wgUser->isAllowed( 'delete' ) ? '<th></th>' : '' )
+                       . '<tr><td></td>'
+                       . ( $this->img->isLocal() && $wgUser->isAllowed( 'delete' ) ? '<td></td>' : '' )
                        . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
                        . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
                        . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
-                       . '<th>' . wfMsgHtml( 'filehist-filesize' ) . '</th>'
+                       . '<th class="mw-imagepage-filesize">' . wfMsgHtml( 'filehist-filesize' ) . '</th>'
                        . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
                        . "</tr>\n";
        }
@@ -726,42 +563,56 @@ class ImageHistoryList {
                return "</table>\n";
        }
 
-       public function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description, $width, $height ) {
-               global $wgUser, $wgLang, $wgTitle, $wgContLang;
+       /**
+        * Create one row of file history
+        *
+        * @param bool $iscur is this the current file version?
+        * @param string $timestamp timestamp of file version
+        * @param string $img filename
+        * @param int $user ID of uploading user
+        * @param string $usertext username of uploading user
+        * @param int $size size of file version
+        * @param string $description description of file version
+        * @param string $dims dimensions of file version
+        * @return string a HTML formatted table row
+        */
+       public function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description, $dims ) {
+               global $wgUser, $wgLang, $wgContLang;
                $local = $this->img->isLocal();
                $row = '';
-               
+
                // Deletion link
                if( $local && $wgUser->isAllowed( 'delete' ) ) {
                        $row .= '<td>';
+                       $q = array();
                        $q[] = 'action=delete';
-                       $q[] = ( $iscur ? 'image=' . $this->title->getPartialUrl() : 'oldimage=' . urlencode( $img ) );
                        if( !$iscur )
-                               $q[] = 'wpEditToken=' . urlencode( $wgUser->editToken( $img ) );
-                       $row .= '(' . $this->skin->makeKnownLinkObj(
+                               $q[] = 'oldimage=' . urlencode( $img );
+                       $row .= $this->skin->makeKnownLinkObj(
                                $this->title,
                                wfMsgHtml( $iscur ? 'filehist-deleteall' : 'filehist-deleteone' ),
                                implode( '&', $q )
-                       ) . ')';
+                       );
                        $row .= '</td>';
                }
-               
+
                // Reversion link/current indicator
                $row .= '<td>';
                if( $iscur ) {
-                       $row .= '(' . wfMsgHtml( 'filehist-current' ) . ')';
+                       $row .= wfMsgHtml( 'filehist-current' );
                } elseif( $local && $wgUser->isLoggedIn() && $this->title->userCan( 'edit' ) ) {
+                       $q = array();
                        $q[] = 'action=revert';
                        $q[] = 'oldimage=' . urlencode( $img );
                        $q[] = 'wpEditToken=' . urlencode( $wgUser->editToken( $img ) );
-                       $row .= '(' . $this->skin->makeKnownLinkObj(
+                       $row .= $this->skin->makeKnownLinkObj(
                                $this->title,
                                wfMsgHtml( 'filehist-revert' ),
                                implode( '&', $q )
-                       ) . ')';
+                       );
                }
                $row .= '</td>';
-               
+
                // Date/time and image link
                $row .= '<td>';
                $url = $iscur ? $this->img->getUrl() : $this->img->getArchiveUrl( $img );
@@ -771,7 +622,7 @@ class ImageHistoryList {
                        $wgLang->timeAndDate( $timestamp, true )
                );
                $row .= '</td>';
-               
+
                // Uploading user
                $row .= '<td>';
                if( $local ) {
@@ -780,20 +631,17 @@ class ImageHistoryList {
                        $row .= htmlspecialchars( $usertext );
                }
                $row .= '</td>';
-               
+
                // Image dimensions
-               // FIXME: It would be nice to show the duration (sound files) or
-               // width/height/duration (video files) here, but this needs some
-               // additional media handler work
-               $row .= '<td>' . wfMsgHtml( 'widthheight', $width, $height ) . '</td>';
-               
+               $row .= '<td>' . htmlspecialchars( $dims ) . '</td>';
+
                // File size
-               $row .= '<td>' . $this->skin->formatSize( $size ) . '</td>';
-               
+               $row .= '<td class="mw-imagepage-filesize">' . $this->skin->formatSize( $size ) . '</td>';
+
                // Comment
                $row .= '<td>' . $this->skin->formatComment( $description, $this->title ) . '</td>';
-               
+
                return "<tr>{$row}</tr>\n";
        }
 
-}
\ No newline at end of file
+}