Use redirect=no on redirects listed in file links.
[lhc/web/wiklou.git] / includes / ImagePage.php
index 86ba8cb..5d4db92 100644 (file)
@@ -1,36 +1,46 @@
 <?php
-/**
- */
 
-/**
- *
- */
 if( !defined( 'MEDIAWIKI' ) )
        die( 1 );
 
 /**
  * Special handling for image description pages
  *
- * @addtogroup Media
+ * @ingroup Media
  */
 class ImagePage extends Article {
 
-       /* private */ var $img;  // Image object this page is shown for
+       /* private */ var $img;  // Image object
+       /* private */ var $displayImg;
        /* private */ var $repo;
+       /* private */ var $fileLoaded;
        var $mExtraDescription = false;
        var $dupes;
 
-       function __construct( $title, $time = false ) {
+       function __construct( $title ) {
                parent::__construct( $title );
-               $this->img = wfFindFile( $this->mTitle, $time );
+               $this->dupes = null;
+               $this->repo = null;
+       }
+
+       protected function loadFile() {
+               if ( $this->fileLoaded ) {
+                       return true;
+               }
+               $this->fileLoaded = true;
+
+               $this->displayImg = $this->img = false;
+               wfRunHooks( 'ImagePageFindFile', array( $this, &$this->img, &$this->displayImg ) );
                if ( !$this->img ) {
-                       $this->img = wfLocalFile( $this->mTitle );
-                       $this->current = $this->img;
-               } else {
-                       $this->current = $time ? wfLocalFile( $this->mTitle ) : $this->img;
+                       $this->img = wfFindFile( $this->mTitle );
+                       if ( !$this->img ) {
+                               $this->img = wfLocalFile( $this->mTitle );
+                       }
+               }
+               if ( !$this->displayImg ) {
+                       $this->displayImg = $this->img;
                }
                $this->repo = $this->img->getRepo();
-               $this->dupes = null;
        }
 
        /**
@@ -45,6 +55,7 @@ class ImagePage extends Article {
 
        function view() {
                global $wgOut, $wgShowEXIF, $wgRequest, $wgUser;
+               $this->loadFile();
 
                if ( $this->mTitle->getNamespace() == NS_IMAGE && $this->img->getRedirected() ) {
                        if ( $this->mTitle->getDBkey() == $this->img->getName() ) {
@@ -68,16 +79,16 @@ class ImagePage extends Article {
                if ( $this->mTitle->getNamespace() != NS_IMAGE || ( isset( $diff ) && $diffOnly ) )
                        return Article::view();
 
-               if ($wgShowEXIF && $this->img->exists()) {
+               if ( $wgShowEXIF && $this->displayImg->exists() ) {
                        // FIXME: bad interface, see note on MediaHandler::formatMetadata().
-                       $formattedMetadata = $this->img->formatMetadata();
+                       $formattedMetadata = $this->displayImg->formatMetadata();
                        $showmeta = $formattedMetadata !== false;
                } else {
                        $showmeta = false;
                }
 
-               if ($this->img->exists())
-                       $wgOut->addHTML($this->showTOC($showmeta));
+               if ( $this->displayImg->exists() )
+                       $wgOut->addHTML( $this->showTOC($showmeta) );
 
                $this->openShowImage();
 
@@ -113,7 +124,9 @@ class ImagePage extends Article {
                $this->imageDupes();
                // TODO: We may want to find local images redirecting to a foreign 
                // file: "The following local files redirect to this file"
-               if ( $this->img->isLocal() ) $this->imageRedirects();           
+               if ( $this->img->isLocal() ) {
+                       $this->imageRedirects();
+               }
                $this->imageLinks();
 
                if ( $showmeta ) {
@@ -129,25 +142,32 @@ class ImagePage extends Article {
        }
        
        public function getRedirectTarget() {
-               if ( $this->img->isLocal() )
+               $this->loadFile();
+               if ( $this->img->isLocal() ) {
                        return parent::getRedirectTarget();
-               
+               }
                // Foreign image page
                $from = $this->img->getRedirected();
                $to = $this->img->getName();
-               if ($from == $to) return null; 
+               if ( $from == $to ) {
+                       return null; 
+               }
                return $this->mRedirectTarget = Title::makeTitle( NS_IMAGE, $to );
        }
        public function followRedirect() {
-               if ( $this->img->isLocal() )
+               $this->loadFile();
+               if ( $this->img->isLocal() ) {
                        return parent::followRedirect();
-                       
+               }
                $from = $this->img->getRedirected();
                $to = $this->img->getName();
-               if ($from == $to) return false; 
+               if ( $from == $to ) {
+                       return false; 
+               }
                return Title::makeTitle( NS_IMAGE, $to );       
        }
        public function isRedirect( $text = false ) {
+               $this->loadFile();
                if ( $this->img->isLocal() )
                        return parent::isRedirect( $text );
                        
@@ -155,20 +175,29 @@ class ImagePage extends Article {
        }
        
        public function isLocal() {
+               $this->loadFile();
                return $this->img->isLocal();
        }
        
        public function getFile() {
+               $this->loadFile();
                return $this->img;
        }
        
+       public function getDisplayedFile() {
+               $this->loadFile();
+               return $this->displayImg;
+       }
+       
        public function getDuplicates() {
-               if ( !is_null($this->dupes) ) return $this->dupes;
-               
-               if ( !( $hash = $this->img->getSha1() ) ) 
+               $this->loadFile();
+               if ( !is_null($this->dupes) ) {
+                       return $this->dupes;
+               }
+               if ( !( $hash = $this->img->getSha1() ) ) {
                        return $this->dupes = array();
+               }
                $dupes = RepoGroup::singleton()->findBySha1( $hash );
-               
                // Remove duplicates with self and non matching file sizes
                $self = $this->img->getRepoName().':'.$this->img->getName();
                $size = $this->img->getSize();
@@ -238,6 +267,7 @@ class ImagePage extends Article {
         * shared upload server if possible.
         */
        function getContent() {
+               $this->loadFile();
                if( $this->img && !$this->img->isLocal() && 0 == $this->getID() ) {
                        return '';
                }
@@ -247,7 +277,9 @@ class ImagePage extends Article {
        function openShowImage() {
                global $wgOut, $wgUser, $wgImageLimits, $wgRequest, $wgLang, $wgContLang;
 
-               $full_url  = $this->img->getURL();
+               $this->loadFile();
+
+               $full_url  = $this->displayImg->getURL();
                $linkAttribs = false;
                $sizeSel = intval( $wgUser->getOption( 'imagesize') );
                if( !isset( $wgImageLimits[$sizeSel] ) ) {
@@ -266,7 +298,7 @@ class ImagePage extends Article {
                $sk = $wgUser->getSkin();
                $dirmark = $wgContLang->getDirMark();
 
-               if ( $this->img->exists() ) {
+               if ( $this->displayImg->exists() ) {
                        # image
                        $page = $wgRequest->getIntOrNull( 'page' );
                        if ( is_null( $page ) ) {
@@ -275,22 +307,22 @@ class ImagePage extends Article {
                        } else {
                                $params = array( 'page' => $page );
                        }
-                       $width_orig = $this->img->getWidth();
+                       $width_orig = $this->displayImg->getWidth();
                        $width = $width_orig;
-                       $height_orig = $this->img->getHeight();
+                       $height_orig = $this->displayImg->getHeight();
                        $height = $height_orig;
-                       $mime = $this->img->getMimeType();
+                       $mime = $this->displayImg->getMimeType();
                        $showLink = false;
                        $linkAttribs = array( 'href' => $full_url );
-                       $longDesc = $this->img->getLongDesc();
+                       $longDesc = $this->displayImg->getLongDesc();
 
                        wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this , &$wgOut ) )       ;
 
-                       if ( $this->img->allowInlineDisplay() ) {
+                       if ( $this->displayImg->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->displayImg->getSize() ), $mime );
                                # We'll show a thumbnail of this image
                                if ( $width > $maxWidth || $height > $maxHeight ) {
                                        # Calculate the thumbnail size.
@@ -311,15 +343,15 @@ class ImagePage extends Article {
                                                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() );
+                                       $msgbig = htmlspecialchars( $this->displayImg->getName() );
                                        $msgsmall = wfMsgExt( 'file-nohires', array( 'parseinline' ) );
                                }
 
                                $params['width'] = $width;
-                               $thumbnail = $this->img->transform( $params );
+                               $thumbnail = $this->displayImg->transform( $params );
 
                                $anchorclose = "<br />";
-                               if( $this->img->mustRender() ) {
+                               if( $this->displayImg->mustRender() ) {
                                        $showLink = true;
                                } else {
                                        $anchorclose .=
@@ -327,13 +359,13 @@ class ImagePage extends Article {
                                                '<br />' . Xml::tags( 'a', $linkAttribs,  $msgbig ) . "$dirmark " . $longDesc;
                                }
 
-                               if ( $this->img->isMultipage() ) {
+                               if ( $this->displayImg->isMultipage() ) {
                                        $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
                                }
 
                                if ( $thumbnail ) {
                                        $options = array(
-                                               'alt' => $this->img->getTitle()->getPrefixedText(),
+                                               'alt' => $this->displayImg->getTitle()->getPrefixedText(),
                                                'file-link' => true,
                                        );
                                        $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
@@ -341,13 +373,13 @@ class ImagePage extends Article {
                                                $anchorclose . '</div>' );
                                }
 
-                               if ( $this->img->isMultipage() ) {
-                                       $count = $this->img->pageCount();
+                               if ( $this->displayImg->isMultipage() ) {
+                                       $count = $this->displayImg->pageCount();
 
                                        if ( $page > 1 ) {
                                                $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
                                                $link = $sk->makeKnownLinkObj( $this->mTitle, $label, 'page='. ($page-1) );
-                                               $thumb1 = $sk->makeThumbLinkObj( $this->mTitle, $this->img, $link, $label, 'none',
+                                               $thumb1 = $sk->makeThumbLinkObj( $this->mTitle, $this->displayImg, $link, $label, 'none',
                                                        array( 'page' => $page - 1 ) );
                                        } else {
                                                $thumb1 = '';
@@ -356,7 +388,7 @@ class ImagePage extends Article {
                                        if ( $page < $count ) {
                                                $label = wfMsg( 'imgmultipagenext' );
                                                $link = $sk->makeKnownLinkObj( $this->mTitle, $label, 'page='. ($page+1) );
-                                               $thumb2 = $sk->makeThumbLinkObj( $this->mTitle, $this->img, $link, $label, 'none',
+                                               $thumb2 = $sk->makeThumbLinkObj( $this->mTitle, $this->displayImg, $link, $label, 'none',
                                                        array( 'page' => $page + 1 ) );
                                        } else {
                                                $thumb2 = '';
@@ -390,8 +422,8 @@ class ImagePage extends Article {
                                }
                        } else {
                                #if direct link is allowed but it's not a renderable image, show an icon.
-                               if ($this->img->isSafeFile()) {
-                                       $icon= $this->img->iconThumb();
+                               if ( $this->displayImg->isSafeFile() ) {
+                                       $icon= $this->displayImg->iconThumb();
 
                                        $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
                                        $icon->toHtml( array( 'desc-link' => true ) ) .
@@ -403,9 +435,9 @@ class ImagePage extends Article {
 
 
                        if ($showLink) {
-                               $filename = wfEscapeWikiText( $this->img->getName() );
+                               $filename = wfEscapeWikiText( $this->displayImg->getName() );
 
-                               if (!$this->img->isSafeFile()) {
+                               if ( !$this->displayImg->isSafeFile() ) {
                                        $warning = wfMsgNoTrans( 'mediawarning' );
                                        $wgOut->addWikiText( <<<EOT
 <div class="fullMedia">
@@ -426,7 +458,7 @@ EOT
                                }
                        }
 
-                       if(!$this->img->isLocal()) {
+                       if( !$this->displayImg->isLocal() ) {
                                $this->printSharedImageText();
                        }
                } else {
@@ -434,7 +466,7 @@ EOT
 
                        $title = SpecialPage::getTitleFor( 'Upload' );
                        $link = $sk->makeKnownLinkObj($title, wfMsgHtml('noimage-linktext'),
-                               'wpDestFile=' . urlencode( $this->img->getName() ) );
+                               'wpDestFile=' . urlencode( $this->displayImg->getName() ) );
                        $wgOut->addHTML( wfMsgWikiHtml( 'noimage', $link ) );
                }
        }
@@ -445,6 +477,8 @@ EOT
        function printSharedImageText() {
                global $wgOut, $wgUser;
 
+               $this->loadFile();
+
                $descUrl = $this->img->getDescriptionUrl();
                $descText = $this->img->getDescriptionText();
                $s = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml( 'sharedupload' );
@@ -471,10 +505,13 @@ EOT
         */
        function checkSharedConflict() {
                global $wgOut, $wgUser;
+               
                $repoGroup = RepoGroup::singleton();
                if( !$repoGroup->hasForeignRepos() ) {
                        return;
                }
+               
+               $this->loadFile();
                if( !$this->img->isLocal() ) {
                        return;
                }
@@ -502,13 +539,17 @@ EOT
        }
 
        function checkSharedConflictCallback( $repo ) {
+               $this->loadFile();
                $dupfile = $repo->newFile( $this->img->getTitle() );
-               if( $dupfile->exists() )
+               if( $dupfile && $dupfile->exists() ) {
                        $this->dupFile = $dupfile;
-               return $dupfile->exists();
+                       return $dupfile->exists();
+               }
+               return false;
        }
 
        function getUploadUrl() {
+               $this->loadFile();
                $uploadTitle = SpecialPage::getTitleFor( 'Upload' );
                return $uploadTitle->getFullUrl( 'wpDestFile=' . urlencode( $this->img->getName() ) );
        }
@@ -520,6 +561,7 @@ EOT
        function uploadLinksBox() {
                global $wgUser, $wgOut;
 
+               $this->loadFile();
                if( !$this->img->isLocal() )
                        return;
 
@@ -556,13 +598,12 @@ EOT
         */
        function imageHistory()
        {
-               global $wgUser, $wgOut, $wgUseExternalEditor;
-
-               $sk = $wgUser->getSkin();
+               global $wgOut, $wgUseExternalEditor;
 
+               $this->loadFile();
                if ( $this->img->exists() ) {
-                       $list = new ImageHistoryList( $sk, $this->current );
-                       $file = $this->current;
+                       $list = new ImageHistoryList( $this );
+                       $file = $this->img;
                        $dims = $file->getDimensionsString();
                        $s = $list->beginImageHistoryList();
                        $s .= $list->imageHistoryLine( true, $file );
@@ -589,7 +630,7 @@ EOT
        function imageLinks()
        {
                global $wgUser, $wgOut;
-               
+
                $limit = 100;
 
                $dbr = wfGetDB( DB_SLAVE );
@@ -601,12 +642,15 @@ EOT
                        __METHOD__,
                        array( 'LIMIT' => $limit + 1)   
                );
-
-               if ( 0 == $dbr->numRows( $res ) ) {
+               $count = $dbr->numRows( $res );
+               if ( $count == 0 ) {
+                       $wgOut->addHTML( "<div id='mw-imagepage-nolinkstoimage'>\n" );
                        $wgOut->addWikiMsg( 'nolinkstoimage' );
+                       $wgOut->addHTML( "</div>\n" );
                        return;
                }
-               $wgOut->addWikiMsg( 'linkstoimage' );
+               $wgOut->addHTML( "<div id='mw-imagepage-section-linkstoimage'>\n" );
+               $wgOut->addWikiMsg( 'linkstoimage', $count );
                $wgOut->addHTML( "<ul class='mw-imagepage-linktoimage'>\n" );
 
                $sk = $wgUser->getSkin();
@@ -620,41 +664,44 @@ EOT
                                $wgOut->addHTML( "<li>{$link}</li>\n" );
                        }
                }
-               $wgOut->addHTML( "</ul>\n" );
+               $wgOut->addHTML( "</ul></div>\n" );
                $res->free();
-               
+
                // Add a links to [[Special:Whatlinkshere]]
                if ( $count > $limit )
                        $wgOut->addWikiMsg( 'morelinkstoimage', $this->mTitle->getPrefixedDBkey() );
        }
        
-       function imageRedirects() 
+       function imageRedirects()
        {
                global $wgUser, $wgOut;
-               
+
                $redirects = $this->getTitle()->getRedirectsHere( NS_IMAGE );
                if ( count( $redirects ) == 0 ) return;
-               
 
-               $wgOut->addWikiMsg( 'redirectstofile' );
+               $wgOut->addHTML( "<div id='mw-imagepage-section-redirectstofile'>\n" );
+               $wgOut->addWikiMsg( 'redirectstofile', count( $redirects ) );
                $wgOut->addHTML( "<ul class='mw-imagepage-redirectstofile'>\n" );
 
                $sk = $wgUser->getSkin();
                foreach ( $redirects as $title ) {
-                       $link = $sk->makeKnownLinkObj( $title, "" );
+                       $link = $sk->makeKnownLinkObj( $title, "", "redirect=no" );
                        $wgOut->addHTML( "<li>{$link}</li>\n" );
                }
-               $wgOut->addHTML( "</ul>\n" );
+               $wgOut->addHTML( "</ul></div>\n" );
 
        }
-       
+
        function imageDupes() {
-               global $wgOut, $wgUser;         
-       
+               global $wgOut, $wgUser;
+
+               $this->loadFile();
+
                $dupes = $this->getDuplicates();
                if ( count( $dupes ) == 0 ) return;
 
-               $wgOut->addWikiMsg( 'duplicatesoffile' );
+               $wgOut->addHTML( "<div id='mw-imagepage-section-duplicates'>\n" );
+               $wgOut->addWikiMsg( 'duplicatesoffile', count( $dupes ) );
                $wgOut->addHTML( "<ul class='mw-imagepage-duplicates'>\n" );
 
                $sk = $wgUser->getSkin();
@@ -666,14 +713,15 @@ EOT
                                        $file->getTitle()->getPrefixedText() );
                        $wgOut->addHTML( "<li>{$link}</li>\n" );
                }
-               $wgOut->addHTML( "</ul>\n" );
+               $wgOut->addHTML( "</ul></div>\n" );
        }
 
        /**
         * Delete the file, or an earlier version of it
         */
        public function delete() {
-               if( !$this->img->exists() || !$this->img->isLocal() ) {
+               $this->loadFile();
+               if( !$this->img->exists() || !$this->img->isLocal() || $this->img->getRedirected() ) {
                        // Standard article deletion
                        Article::delete();
                        return;
@@ -686,6 +734,7 @@ EOT
         * Revert the file to an earlier version
         */
        public function revert() {
+               $this->loadFile();
                $reverter = new FileRevertForm( $this->img );
                $reverter->execute();
        }
@@ -694,6 +743,7 @@ EOT
         * Override handling of action=purge
         */
        function doPurge() {
+               $this->loadFile();
                if( $this->img->exists() ) {
                        wfDebug( "ImagePage::doPurge purging " . $this->img->getName() . "\n" );
                        $update = new HTMLCacheUpdate( $this->mTitle, 'imagelinks' );
@@ -723,16 +773,31 @@ EOT
 /**
  * Builds the image revision log shown on image pages
  *
- * @addtogroup Media
+ * @ingroup Media
  */
 class ImageHistoryList {
 
-       protected $img, $skin, $title, $repo;
+       protected $imagePage, $img, $skin, $title, $repo;
+
+       public function __construct( $imagePage ) {
+               global $wgUser;
+               $this->skin = $wgUser->getSkin();
+               $this->current = $imagePage->getFile();
+               $this->img = $imagePage->getDisplayedFile();
+               $this->title = $imagePage->getTitle();
+               $this->imagePage = $imagePage;
+       }
 
-       public function __construct( $skin, $img ) {
-               $this->skin = $skin;
-               $this->img = $img;
-               $this->title = $img->getTitle();
+       function getImagePage() {
+               return $this->imagePage;
+       }
+
+       function getSkin() {
+               return $this->skin;
+       }
+
+       function getFile() {
+               return $this->img;
        }
 
        public function beginImageHistoryList() {
@@ -741,7 +806,7 @@ class ImageHistoryList {
                        . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
                        . Xml::openElement( 'table', array( 'class' => 'filehistory' ) ) . "\n"
                        . '<tr><td></td>'
-                       . ( $this->img->isLocal() && ($wgUser->isAllowed('delete') || $wgUser->isAllowed('deleterevision') ) ? '<td></td>' : '' )
+                       . ( $this->current->isLocal() && ($wgUser->isAllowed('delete') || $wgUser->isAllowed('deleterevision') ) ? '<td></td>' : '' )
                        . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
                        . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
                        . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>' 
@@ -765,8 +830,8 @@ class ImageHistoryList {
                $dims = $file->getDimensionsString();
                $sha1 = $file->getSha1();
 
-               $local = $this->img->isLocal();
-               $row = $css = '';
+               $local = $this->current->isLocal();
+               $row = $css = $selected = '';
 
                // Deletion link
                if( $local && ($wgUser->isAllowed('delete') || $wgUser->isAllowed('deleterevision') ) ) {
@@ -827,7 +892,10 @@ class ImageHistoryList {
                $row .= '</td>';
 
                // Date/time and image link
-               $row .= "<td style='white-space: nowrap;'>";
+               if( $file->getTimestamp() === $this->img->getTimestamp() ) {
+                       $selected = "class='filehistory-selected'";
+               }
+               $row .= "<td $selected style='white-space: nowrap;'>";
                if( !$file->userCan(File::DELETED_FILE) ) {
                        # Don't link to unviewable files
                        $row .= '<span class="history-deleted">' . $wgLang->timeAndDate( $timestamp, true ) . '</span>';
@@ -835,10 +903,10 @@ class ImageHistoryList {
                        $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
                        # Make a link to review the image
                        $url = $this->skin->makeKnownLinkObj( $revdel, $wgLang->timeAndDate( $timestamp, true ),
-                               "target=".$wgTitle->getPrefixedText()."&file=$sha1.".$this->img->getExtension() );
+                               "target=".$wgTitle->getPrefixedText()."&file=$sha1.".$this->current->getExtension() );
                        $row .= '<span class="history-deleted">'.$url.'</span>';
                } else {
-                       $url = $iscur ? $this->img->getUrl() : $this->img->getArchiveUrl( $img );
+                       $url = $iscur ? $this->current->getUrl() : $this->current->getArchiveUrl( $img );
                        $row .= Xml::element( 'a', array( 'href' => $url ), $wgLang->timeAndDate( $timestamp, true ) );
                }
 
@@ -873,9 +941,9 @@ class ImageHistoryList {
                }
                $row .= '</td>';
 
-               wfRunHooks( 'ImagePageFileHistoryLine', array( &$file, &$row, &$css ) );
-               $tagCSS = $css ? " class='$css'" : "";
-               return "<tr{$tagCSS}>{$row}</tr>\n";
+               wfRunHooks( 'ImagePageFileHistoryLine', array( $this, $file, &$row, &$rowClass ) );
+               $classAttr = $rowClass ? " class='$rowClass'" : "";
+
+               return "<tr{$classAttr}>{$row}</tr>\n";
        }
 }