*Add ImagePageFileHistoryLine hook
[lhc/web/wiklou.git] / includes / ImagePage.php
index 399fc1c..705cf22 100644 (file)
@@ -18,6 +18,7 @@ class ImagePage extends Article {
        /* private */ var $img;  // Image object this page is shown for
        /* private */ var $repo;
        var $mExtraDescription = false;
+       var $dupes;
 
        function __construct( $title, $time = false ) {
                parent::__construct( $title );
@@ -28,7 +29,8 @@ class ImagePage extends Article {
                } else {
                        $this->current = $time ? wfLocalFile( $this->mTitle ) : $this->img;
                }
-               $this->repo = $this->img->repo;
+               $this->repo = $this->img->getRepo();
+               $this->dupes = null;
        }
 
        /**
@@ -103,8 +105,16 @@ class ImagePage extends Article {
 
                $this->closeShowImage();
                $this->imageHistory();
+               // TODO: Cleanup the following
+               
+               $wgOut->addHTML( Xml::element( 'h2', 
+                       array( 'id' => 'filelinks' ), 
+                       wfMsg( 'imagelinks' ) ) . "\n" );
+               $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();           
                $this->imageLinks();
-               if ( $this->img->isLocal() ) $this->imageRedirects();
 
                if ( $showmeta ) {
                        global $wgStylePath, $wgStyleVersion;
@@ -151,6 +161,28 @@ class ImagePage extends Article {
        public function getFile() {
                return $this->img;
        }
+       
+       public function getDuplicates() {
+               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();
+               foreach ( $dupes as $index => $file ) {
+                       $key = $file->getRepoName().':'.$file->getName();
+                       if ( $key == $self )
+                               unset( $dupes[$index] );
+                       if ( $file->getSize() != $size )
+                               unset( $dupes[$index] );
+               }
+               return $this->dupes = $dupes;
+               
+       }
+       
 
        /**
         * Create the TOC
@@ -434,6 +466,9 @@ EOT
                }
        }
 
+       /*
+        * Check for files with the same name on the foreign repos.
+        */
        function checkSharedConflict() {
                global $wgOut, $wgUser;
                $repoGroup = RepoGroup::singleton();
@@ -529,8 +564,8 @@ EOT
                        $list = new ImageHistoryList( $sk, $this->current );
                        $file = $this->current;
                        $dims = $file->getDimensionsString();
-                       $s = $list->beginImageHistoryList() .
-                               $list->imageHistoryLine( true, $file );
+                       $s = $list->beginImageHistoryList();
+                       $s .= $list->imageHistoryLine( true, $file );
                        // old image versions
                        $hist = $this->img->getHistory();
                        foreach( $hist as $file ) {
@@ -557,8 +592,6 @@ EOT
                
                $limit = 100;
 
-               $wgOut->addHTML( Xml::element( 'h2', array( 'id' => 'filelinks' ), wfMsg( 'imagelinks' ) ) . "\n" );
-
                $dbr = wfGetDB( DB_SLAVE );
 
                $res = $dbr->select(
@@ -574,7 +607,7 @@ EOT
                        return;
                }
                $wgOut->addWikiMsg( 'linkstoimage' );
-               $wgOut->addHTML( "<ul>\n" );
+               $wgOut->addHTML( "<ul class='mw-imagepage-linktoimage'>\n" );
 
                $sk = $wgUser->getSkin();
                $count = 0;
@@ -599,36 +632,41 @@ EOT
        {
                global $wgUser, $wgOut;
                
-               $dbr = wfGetDB( DB_SLAVE );
-               $res = $dbr->select(
-                       array( 'redirect', 'page' ),
-                       array( 'page_title' ),
-                       array(
-                               'rd_namespace' => NS_IMAGE,
-                               'rd_title' => $this->mTitle->getDBkey(),
-                               'page_namespace' => NS_IMAGE,
-                               'rd_from = page_id'
-                       ),
-                       __METHOD__
-               );
+               $redirects = $this->getTitle()->getRedirectsHere( NS_IMAGE );
+               if ( count( $redirects ) == 0 ) return;
                
 
-               if ( 0 == $dbr->numRows( $res ) ) 
-                       return;
-
                $wgOut->addWikiMsg( 'redirectstofile' );
-               $wgOut->addHTML( "<ul>\n" );
+               $wgOut->addHTML( "<ul class='mw-imagepage-redirectstofile'>\n" );
 
                $sk = $wgUser->getSkin();
-               while ( $row = $dbr->fetchObject( $res ) ) {
-                       $name = Title::makeTitle( NS_IMAGE, $row->page_title );
-                       $link = $sk->makeKnownLinkObj( $name, "" );
-                       wfDebug("Image redirect: {$row->page_title}\n");
+               foreach ( $redirects as $title ) {
+                       $link = $sk->makeKnownLinkObj( $title, "" );
+                       $wgOut->addHTML( "<li>{$link}</li>\n" );
+               }
+               $wgOut->addHTML( "</ul>\n" );
+
+       }
+       
+       function imageDupes() {
+               global $wgOut, $wgUser;         
+       
+               $dupes = $this->getDuplicates();
+               if ( count( $dupes ) == 0 ) return;
+
+               $wgOut->addWikiMsg( 'duplicatesoffile' );
+               $wgOut->addHTML( "<ul class='mw-imagepage-duplicates'>\n" );
+
+               $sk = $wgUser->getSkin();
+               foreach ( $dupes as $file ) {
+                       if ( $file->isLocal() )
+                               $link = $sk->makeKnownLinkObj( $file->getTitle(), "" );
+                       else
+                               $link = $sk->makeExternalLink( $file->getDescriptionUrl(), 
+                                       $file->getTitle()->getPrefixedText() );
                        $wgOut->addHTML( "<li>{$link}</li>\n" );
                }
                $wgOut->addHTML( "</ul>\n" );
-               
-               $res->free();
        }
 
        /**
@@ -699,18 +737,9 @@ class ImageHistoryList {
 
        public function beginImageHistoryList() {
                global $wgOut, $wgUser;
-               $deleteColumn = $wgUser->isAllowed( 'delete' ) || $wgUser->isAllowed( 'deleterevision' );
                return Xml::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) )
                        . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
-                       . Xml::openElement( 'table', array( 'class' => 'filehistory' ) ) . "\n"
-                       . '<tr><td></td>'
-                       . ( $this->img->isLocal() && $deleteColumn ? '<td></td>' : '' )
-                       . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
-                       . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
-                       . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
-                       . '<th class="mw-imagepage-filesize">' . wfMsgHtml( 'filehist-filesize' ) . '</th>'
-                       . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
-                       . "</tr>\n";
+                       . Xml::openElement( 'table', array( 'class' => 'filehistory' ) ) . "\n";
        }
 
        public function endImageHistoryList() {
@@ -730,23 +759,22 @@ class ImageHistoryList {
                $sha1 = $file->getSha1();
 
                $local = $this->img->isLocal();
-               $row = '';
+               $row = $css = '';
 
                // Deletion link
                if( $local && ($wgUser->isAllowed('delete') || $wgUser->isAllowed('deleterevision') ) ) {
-                       $row .= '<td>';
                        # Link to remove from history
                        if( $wgUser->isAllowed( 'delete' ) ) {
                                $q = array();
                                $q[] = 'action=delete';
                                if( !$iscur )
                                        $q[] = 'oldimage=' . urlencode( $img );
-                               $row .= $this->skin->makeKnownLinkObj(
+                               $row .= '<td>' . $this->skin->makeKnownLinkObj(
                                        $this->title,
                                        wfMsgHtml( $iscur ? 'filehist-deleteall' : 'filehist-deleteone' ),
                                        implode( '&', $q )
                                );
-                               $row .= '<br/>';
+                               $row .= '</td>';
                        }
                        # Link to hide content
                        if( $wgUser->isAllowed( 'deleterevision' ) ) {
@@ -764,9 +792,8 @@ class ImageHistoryList {
                                        if( $file->isDeleted(File::DELETED_RESTRICTED) )
                                                $del = "<strong>$del</strong>";
                                }
-                               $row .= "<tt><small>$del</small></tt>";
+                               $row .= "<td style='white-space: nowrap;'><tt><small>$del</small></tt></td>";
                        }
-                       $row .= '</td>';
                }
 
                // Reversion link/current indicator
@@ -789,7 +816,7 @@ class ImageHistoryList {
                $row .= '</td>';
 
                // Date/time and image link
-               $row .= '<td>';
+               $row .= "<td 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>';
@@ -801,15 +828,19 @@ class ImageHistoryList {
                        $row .= '<span class="history-deleted">'.$url.'</span>';
                } else {
                        $url = $iscur ? $this->img->getUrl() : $this->img->getArchiveUrl( $img );
-                       $row .= Xml::element( 'a',
-                               array( 'href' => $url ),
-                               $wgLang->timeAndDate( $timestamp, true ) );
+                       $row .= Xml::element( 'a', array( 'href' => $url ), $wgLang->timeAndDate( $timestamp, true ) );
                }
 
-               $row .= '</td>';
+               $row .= "</td><td>";
+
+               // Image dimensions
+               $row .= '&nbsp;' . htmlspecialchars( $dims );
+
+               // File size
+               $row .= "&nbsp;<span style='white-space: nowrap;'>(" . $this->skin->formatSize( $size ) . ')</span>';
 
                // Uploading user
-               $row .= '<td>';
+               $row .= '</td><td>';
                if( $local ) {
                        // Hide deleted usernames
                        if( $file->isDeleted(File::DELETED_USER) )
@@ -820,20 +851,18 @@ class ImageHistoryList {
                } else {
                        $row .= htmlspecialchars( $usertext );
                }
-               $row .= '</td>';
-
-               // Image dimensions
-               $row .= '<td>' . htmlspecialchars( $dims ) . '</td>';
-
-               // File size
-               $row .= '<td class="mw-imagepage-filesize">' . $this->skin->formatSize( $size ) . '</td>';
 
                // Don't show deleted descriptions
-               if ( $file->isDeleted(File::DELETED_COMMENT) )
-                       $row .= '<td><span class="history-deleted">' . wfMsgHtml('rev-deleted-comment') . '</span></td>';
-               else
-                       $row .= '<td>' . $this->skin->commentBlock( $description, $this->title ) . '</td>';
+               if ( $file->isDeleted(File::DELETED_COMMENT) ) {
+                       $row .= '&nbsp;<span class="history-deleted">' . wfMsgHtml('rev-deleted-comment') . '</span>';
+               } else {
+                       $row .= '&nbsp;' . $this->skin->commentBlock( $description, $this->title );
+               }
+               $row .= '</td>';
 
-               return "<tr>{$row}</tr>\n";
+               wfRunHooks( 'ImagePageFileHistoryLine', array( &$file, &$row, &$css ) );
+               $tagCSS = $css ? " class='$css'" : "";
+               return "<tr{$tagCSS}>{$row}</tr>\n";
        }
 }