Wikify some image messages:
[lhc/web/wiklou.git] / includes / ImagePage.php
index 573bc4d..400fea9 100644 (file)
@@ -38,12 +38,15 @@ class ImagePage extends Article {
        function render() {
                global $wgOut;
                $wgOut->setArticleBodyOnly( true );
-               $wgOut->addSecondaryWikitext( $this->getContent() );
+               parent::view();
        }
 
        function view() {
                global $wgOut, $wgShowEXIF, $wgRequest, $wgUser;
 
+               if ( $this->img->getRedirected() )
+                       return Article::view();
+
                $diff = $wgRequest->getVal( 'diff' );
                $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
 
@@ -51,7 +54,7 @@ class ImagePage extends Article {
                        return Article::view();
 
                if ($wgShowEXIF && $this->img->exists()) {
-                       // FIXME: bad interface, see note on MediaHandler::formatMetadata(). 
+                       // FIXME: bad interface, see note on MediaHandler::formatMetadata().
                        $formattedMetadata = $this->img->formatMetadata();
                        $showmeta = $formattedMetadata !== false;
                } else {
@@ -81,11 +84,14 @@ class ImagePage extends Article {
                                $wgOut->addWikiText( $fol );
                        }
                        $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription . '</div>' );
+               } else {
+                       $this->checkSharedConflict();
                }
 
                $this->closeShowImage();
                $this->imageHistory();
                $this->imageLinks();
+               if ( $this->img->isLocal() ) $this->imageRedirects();
 
                if ( $showmeta ) {
                        global $wgStylePath, $wgStyleVersion;
@@ -98,6 +104,28 @@ class ImagePage extends Article {
                                "<script type=\"text/javascript\">attachMetadataToggle('mw_metadata', '$expand', '$collapse');</script>\n" );
                }
        }
+       
+       public function getRedirectTarget() {
+               if ( $this->img->isLocal() )
+                       return parent::getRedirectTarget();
+               
+               // Foreign image page
+               $from = $this->img->getRedirected();
+               return $this->mRedirectTarget = Title::makeTitle( NS_IMAGE, $from );
+       }
+       public function followRedirect() {
+               if ( $this->img->isLocal() )
+                       return parent::followRedirect();
+                       
+               $from = $this->img->getRedirected();
+               return Title::makeTitle( NS_IMAGE, $from );             
+       }
+       public function isRedirect() {
+               if ( $this->img->isLocal() )
+                       return parent::isRedirect();
+                       
+               return (bool)$this->img->getRedirected();
+       }
 
        /**
         * Create the TOC
@@ -121,7 +149,7 @@ class ImagePage extends Article {
        /**
         * Make a table with metadata to be shown in the output page.
         *
-        * FIXME: bad interface, see note on MediaHandler::formatMetadata(). 
+        * FIXME: bad interface, see note on MediaHandler::formatMetadata().
         *
         * @access private
         *
@@ -148,7 +176,7 @@ class ImagePage extends Article {
 
        /**
         * Overloading Article's getContent method.
-        * 
+        *
         * Omit noarticletext if sharedupload; text will be fetched from the
         * shared upload server if possible.
         */
@@ -237,7 +265,7 @@ class ImagePage extends Article {
                                if( $this->img->mustRender() ) {
                                        $showLink = true;
                                } else {
-                                       $anchorclose .= 
+                                       $anchorclose .=
                                                $msgsmall .
                                                '<br />' . Xml::tags( 'a', $linkAttribs,  $msgbig ) . "$dirmark " . $longDesc;
                                }
@@ -247,11 +275,11 @@ class ImagePage extends Article {
                                }
 
                                if ( $thumbnail ) {
-                                       $options = array( 
+                                       $options = array(
                                                'alt' => $this->img->getTitle()->getPrefixedText(),
                                                'file-link' => true,
                                        );
-                                       $wgOut->addHTML( '<div class="fullImageLink" id="file">' . 
+                                       $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
                                                $thumbnail->toHtml( $options ) .
                                                $anchorclose . '</div>' );
                                }
@@ -262,7 +290,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->mTitle, $this->img, $link, $label, 'none', 
+                                               $thumb1 = $sk->makeThumbLinkObj( $this->mTitle, $this->img, $link, $label, 'none',
                                                        array( 'page' => $page - 1 ) );
                                        } else {
                                                $thumb1 = '';
@@ -271,14 +299,14 @@ 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->img, $link, $label, 'none',
                                                        array( 'page' => $page + 1 ) );
                                        } else {
                                                $thumb2 = '';
                                        }
 
                                        global $wgScript;
-                                       $select = '<form name="pageselector" action="' . 
+                                       $select = '<form name="pageselector" action="' .
                                                htmlspecialchars( $wgScript ) .
                                                '" method="get" onchange="document.pageselector.submit();">' .
                                                Xml::hidden( 'title', $this->getTitle()->getPrefixedDbKey() );
@@ -346,25 +374,72 @@ EOT
                }
        }
 
+       /**
+        * Show a notice that the file is from a shared repository
+        */
        function printSharedImageText() {
                global $wgOut, $wgUser;
 
                $descUrl = $this->img->getDescriptionUrl();
                $descText = $this->img->getDescriptionText();
-               $s = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml("sharedupload");
-               if ( $descUrl && !$descText) {
+               $s = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml( 'sharedupload' );
+               if ( $descUrl ) {
                        $sk = $wgUser->getSkin();
-                       $link = $sk->makeExternalLink( $descUrl, wfMsg('shareduploadwiki-linktext') );
-                       $s .= " " . wfMsgWikiHtml('shareduploadwiki', $link);
+                       $link = $sk->makeExternalLink( $descUrl, wfMsg( 'shareduploadwiki-linktext' ) );
+                       $msg = ( $descText ) ? 'shareduploadwiki-desc' : 'shareduploadwiki';
+                       $msg = wfMsgExt( $msg, array( 'parseinline', 'replaceafter' ), $link );
+                       if ( $msg != '-' ) {
+                               # Show message only if not voided by local sysops
+                               $s .= $msg;
+                       }
                }
                $s .= "</div>";
-               $wgOut->addHTML($s);
+               $wgOut->addHTML( $s );
 
                if ( $descText ) {
                        $this->mExtraDescription = $descText;
                }
        }
 
+       function checkSharedConflict() {
+               global $wgOut, $wgUser;
+               $repoGroup = RepoGroup::singleton();
+               if( !$repoGroup->hasForeignRepos() ) {
+                       return;
+               }
+               if( !$this->img->isLocal() ) {
+                       return;
+               }
+
+               $this->dupFile = null;
+               $repoGroup->forEachForeignRepo( array( $this, 'checkSharedConflictCallback' ) );
+               
+               if( !$this->dupFile )
+                       return;
+               $dupfile = $this->dupFile;
+               $same = (
+                       ($this->img->getSha1() == $dupfile->getSha1()) &&
+                       ($this->img->getSize() == $dupfile->getSize())
+               );
+
+               $sk = $wgUser->getSkin();
+               $descUrl = $dupfile->getDescriptionUrl();
+               if( $same ) {
+                       $link = $sk->makeExternalLink( $descUrl, wfMsg( 'shareduploadduplicate-linktext' ) );
+                       $wgOut->addHTML( '<div id="shared-image-dup">' . wfMsgWikiHtml( 'shareduploadduplicate', $link ) . '</div>' );
+               } else {
+                       $link = $sk->makeExternalLink( $descUrl, wfMsg( 'shareduploadconflict-linktext' ) );
+                       $wgOut->addHTML( '<div id="shared-image-conflict">' . wfMsgWikiHtml( 'shareduploadconflict', $link ) . '</div>' );
+               }
+       }
+
+       function checkSharedConflictCallback( $repo ) {
+               $dupfile = $repo->newFile( $this->img->getTitle() );
+               if( $dupfile->exists() )
+                       $this->dupFile = $dupfile;
+               return $dupfile->exists();
+       }
+
        function getUploadUrl() {
                $uploadTitle = SpecialPage::getTitleFor( 'Upload' );
                return $uploadTitle->getFullUrl( 'wpDestFile=' . urlencode( $this->img->getName() ) );
@@ -381,19 +456,23 @@ EOT
                        return;
 
                $sk = $wgUser->getSkin();
-               
+
                $wgOut->addHtml( '<br /><ul>' );
-               
+
                # "Upload a new version of this file" link
                if( UploadForm::userCanReUpload($wgUser,$this->img->name) ) {
                        $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
                        $wgOut->addHtml( "<li><div class='plainlinks'>{$ulink}</div></li>" );
                }
-               
+
+               # Link to Special:FileDuplicateSearch
+               $dupeLink = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'FileDuplicateSearch', $this->mTitle->getDBkey() ), wfMsgHtml( 'imagepage-searchdupe' ) );
+               $wgOut->addHtml( "<li>{$dupeLink}</li>" );
+
                # External editing link
                $elink = $sk->makeKnownLinkObj( $this->mTitle, wfMsgHtml( 'edit-externally' ), 'action=edit&externaledit=true&mode=file' );
                $wgOut->addHtml( '<li>' . $elink . '<div>' . wfMsgWikiHtml( 'edit-externally-help' ) . '</div></li>' );
-               
+
                $wgOut->addHtml( '</ul>' );
        }
 
@@ -418,20 +497,12 @@ EOT
                        $file = $this->current;
                        $dims = $file->getDimensionsString();
                        $s = $list->beginImageHistoryList() .
-                               $list->imageHistoryLine( true, wfTimestamp(TS_MW, $file->getTimestamp()),
-                                       $this->mTitle->getDBkey(),  $file->getUser('id'),
-                                       $file->getUser('text'), $file->getSize(), $file->getDescription(),
-                                       $dims
-                               );
-
+                               $list->imageHistoryLine( true, $file );
+                       // old image versions
                        $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->imageHistoryLine( false, $file );
                        }
                        $s .= $list->endImageHistoryList();
                } else { $s=''; }
@@ -450,31 +521,81 @@ EOT
        function imageLinks()
        {
                global $wgUser, $wgOut;
+               
+               $limit = 100;
 
                $wgOut->addHTML( Xml::element( 'h2', array( 'id' => 'filelinks' ), wfMsg( 'imagelinks' ) ) . "\n" );
 
                $dbr = wfGetDB( DB_SLAVE );
-               $page = $dbr->tableName( 'page' );
-               $imagelinks = $dbr->tableName( 'imagelinks' );
 
-               $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
-                 $dbr->addQuotes( $this->mTitle->getDBkey() ) . " AND il_from=page_id";
-               $sql = $dbr->limitResult($sql, 500, 0);
-               $res = $dbr->query( $sql, "ImagePage::imageLinks" );
+               $res = $dbr->select(
+                       array( 'imagelinks', 'page' ),
+                       array( 'page_namespace', 'page_title' ),
+                       array( 'il_to' => $this->mTitle->getDBkey(), 'il_from = page_id' ),
+                       __METHOD__,
+                       array( 'LIMIT' => $limit + 1)   
+               );
 
                if ( 0 == $dbr->numRows( $res ) ) {
-                       $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
+                       $wgOut->addWikiMsg( 'nolinkstoimage' );
                        return;
                }
-               $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) .  "</p>\n<ul>" );
+               $wgOut->addWikiMsg( 'linkstoimage' );
+               $wgOut->addHTML( "<ul>\n" );
 
                $sk = $wgUser->getSkin();
-               while ( $s = $dbr->fetchObject( $res ) ) {
-                       $name = Title::MakeTitle( $s->page_namespace, $s->page_title );
+               $count = 0;
+               while ( $s = $res->fetchObject() ) {
+                       $count++;
+                       if ( $count <= $limit ) {
+                               // We have not yet reached the extra one that tells us there is more to fetch
+                               $name = Title::makeTitle( $s->page_namespace, $s->page_title );
+                               $link = $sk->makeKnownLinkObj( $name, "" );
+                               $wgOut->addHTML( "<li>{$link}</li>\n" );
+                       }
+               }
+               $wgOut->addHTML( "</ul>\n" );
+               $res->free();
+               
+               // Add a links to [[Special:Whatlinkshere]]
+               if ( $count > $limit )
+                       $wgOut->addWikiMsg( 'morelinkstoimage', $this->mTitle->getPrefixedDBkey() );
+       }
+       
+       function imageRedirects() 
+       {
+               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__
+               );
+               
+
+               if ( 0 == $dbr->numRows( $res ) ) 
+                       return;
+
+               $wgOut->addWikiMsg( 'redirectstofile' );
+               $wgOut->addHTML( "<ul>\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");
                        $wgOut->addHTML( "<li>{$link}</li>\n" );
                }
                $wgOut->addHTML( "</ul>\n" );
+               
+               $res->free();
        }
 
        /**
@@ -497,7 +618,7 @@ EOT
                $reverter = new FileRevertForm( $this->img );
                $reverter->execute();
        }
-       
+
        /**
         * Override handling of action=purge
         */
@@ -545,11 +666,12 @@ 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() && $wgUser->isAllowed( 'delete' ) ? '<td></td>' : '' )
+                       . ( $this->img->isLocal() && $deleteColumn ? '<td></td>' : '' )
                        . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
                        . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
                        . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
@@ -562,36 +684,55 @@ class ImageHistoryList {
                return "</table>\n";
        }
 
-       /**
-        * 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;
+       public function imageHistoryLine( $iscur, $file ) {
+               global $wgUser, $wgLang, $wgContLang, $wgTitle;
+
+               $timestamp = wfTimestamp(TS_MW, $file->getTimestamp());
+               $img = $iscur ? $file->getName() : $file->getArchiveName();
+               $user = $file->getUser('id');
+               $usertext = $file->getUser('text');
+               $size = $file->getSize();
+               $description = $file->getDescription();
+               $dims = $file->getDimensionsString();
+               $sha1 = $file->getSha1();
+
                $local = $this->img->isLocal();
                $row = '';
 
                // Deletion link
-               if( $local && $wgUser->isAllowed( 'delete' ) ) {
+               if( $local && ($wgUser->isAllowed('delete') || $wgUser->isAllowed('deleterevision') ) ) {
                        $row .= '<td>';
-                       $q = array();
-                       $q[] = 'action=delete';
-                       if( !$iscur )
-                               $q[] = 'oldimage=' . urlencode( $img );
-                       $row .= $this->skin->makeKnownLinkObj(
-                               $this->title,
-                               wfMsgHtml( $iscur ? 'filehist-deleteall' : 'filehist-deleteone' ),
-                               implode( '&', $q )
-                       );
+                       # Link to remove from history
+                       if( $wgUser->isAllowed( 'delete' ) ) {
+                               $q = array();
+                               $q[] = 'action=delete';
+                               if( !$iscur )
+                                       $q[] = 'oldimage=' . urlencode( $img );
+                               $row .= $this->skin->makeKnownLinkObj(
+                                       $this->title,
+                                       wfMsgHtml( $iscur ? 'filehist-deleteall' : 'filehist-deleteone' ),
+                                       implode( '&', $q )
+                               );
+                               $row .= '<br/>';
+                       }
+                       # Link to hide content
+                       if( $wgUser->isAllowed( 'deleterevision' ) ) {
+                               $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
+                               // If file is top revision or locked from this user, don't link
+                               if( $iscur || !$file->userCan(File::DELETED_RESTRICTED) ) {
+                                       $del = wfMsgHtml( 'rev-delundel' );
+                               } else {
+                                       // If the file was hidden, link to sha-1
+                                       list($ts,$name) = explode('!',$img,2);
+                                       $del = $this->skin->makeKnownLinkObj( $revdel,  wfMsg( 'rev-delundel' ),
+                                               'target=' . urlencode( $wgTitle->getPrefixedText() ) .
+                                               '&oldimage=' . urlencode( $ts ) );
+                                       // Bolden oversighted content
+                                       if( $file->isDeleted(File::DELETED_RESTRICTED) )
+                                               $del = "<strong>$del</strong>";
+                               }
+                               $row .= "<tt><small>$del</small></tt>";
+                       }
                        $row .= '</td>';
                }
 
@@ -600,32 +741,49 @@ class ImageHistoryList {
                if( $iscur ) {
                        $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(
-                               $this->title,
-                               wfMsgHtml( 'filehist-revert' ),
-                               implode( '&', $q )
-                       );
+                       if( $file->isDeleted(File::DELETED_FILE) ) {
+                               $row .= wfMsgHtml('filehist-revert');
+                       } else {
+                               $q = array();
+                               $q[] = 'action=revert';
+                               $q[] = 'oldimage=' . urlencode( $img );
+                               $q[] = 'wpEditToken=' . urlencode( $wgUser->editToken( $img ) );
+                               $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 );
-               $row .= Xml::element(
-                       'a',
-                       array( 'href' => $url ),
-                       $wgLang->timeAndDate( $timestamp, true )
-               );
+               if( !$file->userCan(File::DELETED_FILE) ) {
+                       # Don't link to unviewable files
+                       $row .= '<span class="history-deleted">' . $wgLang->timeAndDate( $timestamp, true ) . '</span>';
+               } else if( $file->isDeleted(File::DELETED_FILE) ) {
+                       $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() );
+                       $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 .= '</td>';
 
                // Uploading user
                $row .= '<td>';
                if( $local ) {
-                       $row .= $this->skin->userLink( $user, $usertext ) . $this->skin->userToolLinks( $user, $usertext );
+                       // Hide deleted usernames
+                       if( $file->isDeleted(File::DELETED_USER) )
+                               $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
+                       else
+                               $row .= $this->skin->userLink( $user, $usertext ) .
+                                       $this->skin->userToolLinks( $user, $usertext );
                } else {
                        $row .= htmlspecialchars( $usertext );
                }
@@ -637,10 +795,12 @@ class ImageHistoryList {
                // File size
                $row .= '<td class="mw-imagepage-filesize">' . $this->skin->formatSize( $size ) . '</td>';
 
-               // Comment
-               $row .= '<td>' . $this->skin->formatComment( $description, $this->title ) . '</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>';
 
                return "<tr>{$row}</tr>\n";
        }
-
 }