Added iterator interface to ResultWrapper. No support in Oracle yet. Updated document...
[lhc/web/wiklou.git] / includes / ImagePage.php
index 4cf8730..3265b7f 100644 (file)
@@ -18,6 +18,14 @@ class ImagePage extends Article {
        /* private */ var $img;  // Image object this page is shown for
        var $mExtraDescription = false;
 
+       function __construct( $title ) {
+               parent::__construct( $title );
+               $this->img = wfFindFile( $this->mTitle );
+               if ( !$this->img ) {
+                       $this->img = wfLocalFile( $this->mTitle );
+               }
+       }
+
        /**
         * Handler for action=render
         * Include body text only; none of the image extras
@@ -31,8 +39,6 @@ class ImagePage extends Article {
        function view() {
                global $wgOut, $wgShowEXIF, $wgRequest, $wgUser;
 
-               $this->img = new Image( $this->mTitle );
-
                $diff = $wgRequest->getVal( 'diff' );
                $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
 
@@ -102,7 +108,7 @@ class ImagePage extends Article {
                        <li><a href="#file">' . $wgLang->getNsText( NS_IMAGE ) . '</a></li>
                        <li><a href="#filehistory">' . wfMsgHtml( 'imghistory' ) . '</a></li>
                        <li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>' .
-                       ($metadata ? '<li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>' : '') . '
+                       ($metadata ? ' <li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>' : '') . '
                </ul>';
                return $r;
        }
@@ -160,7 +166,7 @@ class ImagePage extends Article {
         * shared upload server if possible.
         */
        function getContent() {
-               if( $this->img && $this->img->fromSharedDirectory && 0 == $this->getID() ) {
+               if( $this->img && !$this->img->isLocal() && 0 == $this->getID() ) {
                        return '';
                }
                return Article::getContent();
@@ -204,6 +210,8 @@ class ImagePage extends Article {
                        $showLink = false;
                        $linkAttribs = array( 'href' => $full_url );
 
+      wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this , &$wgOut ) )        ;
+
                        if ( $this->img->allowInlineDisplay() and $width and $height) {
                                # image
 
@@ -266,7 +274,7 @@ class ImagePage extends Article {
                                        if ( $page > 1 ) {
                                                $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
                                                $link = $sk->makeKnownLinkObj( $this->mTitle, $label, 'page='. ($page-1) );
-                                               $thumb1 = $sk->makeThumbLinkObj( $this->img, $link, $label, 'none', 
+                                               $thumb1 = $sk->makeThumbLinkObj( $this->mTitle, $this->img, $link, $label, 'none', 
                                                        array( 'page' => $page - 1 ) );
                                        } else {
                                                $thumb1 = '';
@@ -275,7 +283,7 @@ class ImagePage extends Article {
                                        if ( $page < $count ) {
                                                $label = wfMsg( 'imgmultipagenext' );
                                                $link = $sk->makeKnownLinkObj( $this->mTitle, $label, 'page='. ($page+1) );
-                                               $thumb2 = $sk->makeThumbLinkObj( $this->img, $link, $label, 'none', 
+                                               $thumb2 = $sk->makeThumbLinkObj( $this->mTitle, $this->img, $link, $label, 'none', 
                                                        array( 'page' => $page + 1 ) );
                                        } else {
                                                $thumb2 = '';
@@ -330,26 +338,26 @@ class ImagePage extends Article {
                                $dirmark = $wgContLang->getDirMark();
                                if (!$this->img->isSafeFile()) {
                                        $warning = wfMsg( 'mediawarning' );
-                                       $wgOut->addWikiText( <<<END
+                                       $wgOut->addWikiText( <<<EOT
 <div class="fullMedia">$infores
 <span class="dangerousLink">[[Media:$filename|$filename]]</span>$dirmark
 <span class="fileInfo"> $info</span>
 </div>
 
 <div class="mediaWarning">$warning</div>
-END
+EOT
                                                );
                                } else {
-                                       $wgOut->addWikiText( <<<END
+                                       $wgOut->addWikiText( <<<EOT
 <div class="fullMedia">$infores
 [[Media:$filename|$filename]]$dirmark <span class="fileInfo"> $info</span>
 </div>
-END
+EOT
                                                );
                                }
                        }
 
-                       if($this->img->fromSharedDirectory) {
+                       if(!$this->img->isLocal()) {
                                $this->printSharedImageText();
                        }
                } else {
@@ -363,27 +371,21 @@ END
        }
 
        function printSharedImageText() {
-               global $wgRepositoryBaseUrl, $wgFetchCommonsDescriptions, $wgOut, $wgUser;
-
-               $url = $wgRepositoryBaseUrl . urlencode($this->mTitle->getDBkey());
-               $sharedtext = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml("sharedupload");
-               if ($wgRepositoryBaseUrl && !$wgFetchCommonsDescriptions) {
+               global $wgOut, $wgUser;
 
+               $descUrl = $this->img->getDescriptionUrl();
+               $descText = $this->img->getDescriptionText();
+               $s = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml("sharedupload");
+               if ( $descUrl && !$descText) {
                        $sk = $wgUser->getSkin();
-                       $title = SpecialPage::getTitleFor( 'Upload' );
-                       $link = $sk->makeKnownLinkObj($title, wfMsgHtml('shareduploadwiki-linktext'),
-                       array( 'wpDestFile' => urlencode( $this->img->getName() )));
-                       $sharedtext .= " " . wfMsgWikiHtml('shareduploadwiki', $link);
+                       $link = $sk->makeExternalLink( $descUrl, wfMsg('shareduploadwiki-linktext') );
+                       $s .= " " . wfMsgWikiHtml('shareduploadwiki', $link);
                }
-               $sharedtext .= "</div>";
-               $wgOut->addHTML($sharedtext);
+               $s .= "</div>";
+               $wgOut->addHTML($s);
 
-               if ($wgRepositoryBaseUrl && $wgFetchCommonsDescriptions) {
-                       $renderUrl = wfAppendQuery( $url, 'action=render' );
-                       wfDebug( "Fetching shared description from $renderUrl\n" );
-                       $text = Http::get( $renderUrl );
-                       if ($text)
-                               $this->mExtraDescription = $text;
+               if ( $descText ) {
+                       $this->mExtraDescription = $descText;
                }
        }
 
@@ -400,7 +402,7 @@ END
        function uploadLinksBox() {
                global $wgUser, $wgOut;
 
-               if( $this->img->fromSharedDirectory )
+               if( !$this->img->isLocal() )
                        return;
 
                $sk = $wgUser->getSkin();
@@ -408,7 +410,7 @@ END
                $wgOut->addHtml( '<br /><ul>' );
                
                # "Upload a new version of this file" link
-               if( $wgUser->isAllowed( 'reupload' ) ) {
+               if( UploadForm::userCanReUpload($wgUser,$this->img->name) ) {
                        $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
                        $wgOut->addHtml( "<li><div class='plainlinks'>{$ulink}</div></li>" );
                }
@@ -439,7 +441,7 @@ END
                $line = $this->img->nextHistoryLine();
 
                if ( $line ) {
-                       $list = new ImageHistoryList( $sk );
+                       $list = new ImageHistoryList( $sk, $this->img );
                        $s = $list->beginImageHistoryList() .
                                $list->imageHistoryLine( true, wfTimestamp(TS_MW, $line->img_timestamp),
                                        $this->mTitle->getDBkey(),  $line->img_user,
@@ -458,6 +460,8 @@ END
                } else { $s=''; }
                $wgOut->addHTML( $s );
 
+               $this->img->resetHistory();     // free db resources
+
                # 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() ) {
@@ -500,6 +504,12 @@ END
        {
                global $wgUser, $wgOut, $wgRequest;
 
+               if ( !$this->img->exists() || !$this->img->isLocal() ) {
+                       # Use standard article deletion
+                       Article::delete();
+                       return;
+               }
+
                $confirm = $wgRequest->wasPosted();
                $reason = $wgRequest->getVal( 'wpReason' );
                $image = $wgRequest->getVal( 'image' );
@@ -528,12 +538,10 @@ END
                        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 ) ) {
-                               $this->doDelete( $reason );
+                               $this->doDeleteImage( $reason );
                        } else {
                                $wgOut->showFatalError( wfMsg( 'sessionfailure' ) );
                        }
@@ -552,9 +560,12 @@ END
 
        /*
         * 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 doDelete( $reason ) {
+       function doDeleteImage( $reason ) {
                global $wgOut, $wgRequest;
 
                $oldimage = $wgRequest->getVal( 'oldimage' );
@@ -653,39 +664,21 @@ END
                        $wgOut->showErrorPage( 'internalerror', 'sessionfailure' );
                        return;
                }
-               $name = substr( $oldimage, 15 );
-
-               $dest = wfImageDir( $name );
-               $archive = wfImageArchiveDir( $name );
-               $curfile = "{$dest}/{$name}";
 
-               if ( !is_dir( $dest ) ) wfMkdirParents( $dest );
-               if ( !is_dir( $archive ) ) wfMkdirParents( $archive );
+               $sourcePath = $this->img->getArchiveVirtualUrl( $oldimage );
+               $comment = wfMsg( "reverted" );
+               $result = $this->img->upload( $sourcePath, $comment, $comment );
 
-               if ( ! is_file( $curfile ) ) {
-                       $wgOut->showFileNotFoundError( htmlspecialchars( $curfile ) );
+               if ( WikiError::isError( $result ) ) {
+                       $this->showError( $result );
                        return;
                }
-               $oldver = wfTimestampNow() . "!{$name}";
-
-               if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
-                       $wgOut->showFileRenameError( $curfile, "${archive}/{$oldver}" );
-                       return;
-               }
-               if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
-                       $wgOut->showFileCopyError( "${archive}/{$oldimage}", $curfile );
-                       return;
-               }
-
-               # 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' ) );
 
-               $descTitle = $img->getTitle();
+               $descTitle = $this->img->getTitle();
                $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
        }
        
@@ -693,11 +686,11 @@ END
         * Override handling of action=purge
         */
        function doPurge() {
-               $this->img = new Image( $this->mTitle );
                if( $this->img->exists() ) {
                        wfDebug( "ImagePage::doPurge purging " . $this->img->getName() . "\n" );
                        $update = new HTMLCacheUpdate( $this->mTitle, 'imagelinks' );
                        $update->doUpdate();
+                       $this->img->upgradeRow();
                        $this->img->purgeCache();
                } else {
                        wfDebug( "ImagePage::doPurge no image\n" );
@@ -705,6 +698,18 @@ END
                parent::doPurge();
        }
 
+       /**
+        * Display an error from a wikitext-formatted WikiError object
+        */
+       function showError( WikiError $error ) {
+               global $wgOut;
+               $wgOut->setPageTitle( wfMsg( "internalerror" ) );
+               $wgOut->setRobotpolicy( "noindex,nofollow" );
+               $wgOut->setArticleRelated( false );
+               $wgOut->enableClientCache( false );
+               $wgOut->addWikiText( $error->getMessage() );
+       }
+
 }
 
 /**
@@ -712,8 +717,10 @@ END
  * @addtogroup Media
  */
 class ImageHistoryList {
-       function ImageHistoryList( &$skin ) {
-               $this->skin =& $skin;
+       var $img, $skin;
+       function ImageHistoryList( $skin, $img ) {
+               $this->skin = $skin;
+               $this->img = $img;
        }
 
        function beginImageHistoryList() {
@@ -735,11 +742,12 @@ class ImageHistoryList {
                $del = wfMsgHtml( 'deleteimg' );
                $delall = wfMsgHtml( 'deleteimgcompletely' );
                $cur = wfMsgHtml( 'cur' );
+               $local = $this->img->isLocal();
 
                if ( $iscur ) {
-                       $url = Image::imageUrl( $img );
+                       $url = htmlspecialchars( $this->img->getURL() );
                        $rlink = $cur;
-                       if ( $wgUser->isAllowed('delete') ) {
+                       if ( $local && $wgUser->isAllowed('delete') ) {
                                $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
                                  '&action=delete' );
                                $style = $this->skin->getInternalLinkAttributes( $link, $delall );
@@ -749,8 +757,8 @@ class ImageHistoryList {
                                $dlink = $del;
                        }
                } else {
-                       $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
-                       if( $wgUser->getID() != 0 && $wgTitle->userCan( 'edit' ) ) {
+                       $url = htmlspecialchars( $this->img->getArchiveUrl( $img ) );
+                       if( $local && $wgUser->getID() != 0 && $wgTitle->userCan( 'edit' ) ) {
                                $token = urlencode( $wgUser->editToken( $img ) );
                                $rlink = $this->skin->makeKnownLinkObj( $wgTitle,
                                           wfMsgHtml( 'revertimg' ), 'action=revert&oldimage=' .
@@ -766,8 +774,12 @@ class ImageHistoryList {
                                $dlink = $del;
                        }
                }
-               
-               $userlink = $this->skin->userLink( $user, $usertext ) . $this->skin->userToolLinks( $user, $usertext );
+
+               if ( $local ) {
+                       $userlink = $this->skin->userLink( $user, $usertext ) . $this->skin->userToolLinks( $user, $usertext );
+               } else {
+                       $userlink = htmlspecialchars( $usertext );
+               }
                $nbytes = wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
                        $wgLang->formatNum( $size ) );
                $widthheight = wfMsgHtml( 'widthheight', $width, $height );
@@ -779,8 +791,7 @@ class ImageHistoryList {
                $s .= "</li>\n";
                return $s;
        }
-
 }
 
 
-?>
+