* (bug 28010) Passing a non existant user to list=users gives internal error
[lhc/web/wiklou.git] / includes / ImagePage.php
index e53be26..4cd93ee 100644 (file)
@@ -1,8 +1,5 @@
 <?php
 
-if ( !defined( 'MEDIAWIKI' ) )
-       die( 1 );
-
 /**
  * Special handling for image description pages
  *
@@ -10,10 +7,17 @@ if ( !defined( 'MEDIAWIKI' ) )
  */
 class ImagePage extends Article {
 
-       /* private */ var $img;  // Image object
+       /**
+        * @var File
+        */
+       /* private */ var $img;
+       /**
+        * @var File
+        */
        /* private */ var $displayImg;
        /* private */ var $repo;
        /* private */ var $fileLoaded;
+
        var $mExtraDescription = false;
        var $dupes;
 
@@ -22,7 +26,11 @@ class ImagePage extends Article {
                $this->dupes = null;
                $this->repo = null;
        }
-       
+
+       /**
+        * @param  $file File:
+        * @return void
+        */
        public function setFile( $file ) {
                $this->displayImg = $file;
                $this->img = $file;
@@ -116,9 +124,9 @@ class ImagePage extends Article {
 
                # Show shared description, if needed
                if ( $this->mExtraDescription ) {
-                       $fol = wfMsgNoTrans( 'shareddescriptionfollows' );
-                       if ( $fol != '-' && !wfEmptyMsg( 'shareddescriptionfollows', $fol ) ) {
-                               $wgOut->addWikiText( $fol );
+                       $fol = wfMessage( 'shareddescriptionfollows' );
+                       if ( !$fol->isDisabled() ) {
+                               $wgOut->addWikiText( $fol->plain() );
                        }
                        $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription . "</div>\n" );
                }
@@ -143,13 +151,9 @@ class ImagePage extends Article {
                        $wgOut->addHTML( $html );
 
                if ( $showmeta ) {
-                       $expand = htmlspecialchars( Xml::escapeJsString( wfMsg( 'metadata-expand' ) ) );
-                       $collapse = htmlspecialchars( Xml::escapeJsString( wfMsg( 'metadata-collapse' ) ) );
                        $wgOut->addHTML( Xml::element( 'h2', array( 'id' => 'metadata' ), wfMsg( 'metadata' ) ) . "\n" );
                        $wgOut->addWikiText( $this->makeMetadataTable( $formattedMetadata ) );
                        $wgOut->addModules( array( 'mediawiki.legacy.metadata' ) );
-                       $wgOut->addHTML(
-                               "<script type=\"text/javascript\">attachMetadataToggle('mw_metadata', '$expand', '$collapse');</script>\n" );
                }
                
                $css = $this->repo->getDescriptionStylesheetUrl();
@@ -171,6 +175,7 @@ class ImagePage extends Article {
                }
                return $this->mRedirectTarget = Title::makeTitle( NS_FILE, $to );
        }
+
        public function followRedirect() {
                $this->loadFile();
                if ( $this->img->isLocal() ) {
@@ -183,6 +188,7 @@ class ImagePage extends Article {
                }
                return Title::makeTitle( NS_FILE, $to );
        }
+
        public function isRedirect( $text = false ) {
                $this->loadFile();
                if ( $this->img->isLocal() )
@@ -228,7 +234,6 @@ class ImagePage extends Article {
                return $this->dupes = $dupes;
                
        }
-       
 
        /**
         * Create the TOC
@@ -290,7 +295,7 @@ class ImagePage extends Article {
                if ( $this->img && !$this->img->isLocal() && 0 == $this->getID() ) {
                        return '';
                }
-               return Article::getContent();
+               return parent::getContent();
        }
 
        protected function openShowImage() {
@@ -299,7 +304,6 @@ class ImagePage extends Article {
 
                $this->loadFile();
 
-               $full_url  = $this->displayImg->getURL();
                $sizeSel = intval( $wgUser->getOption( 'imagesize' ) );
                if ( !isset( $wgImageLimits[$sizeSel] ) ) {
                        $sizeSel = User::getDefaultOption( 'imagesize' );
@@ -331,9 +335,7 @@ class ImagePage extends Article {
                        $height_orig = $this->displayImg->getHeight( $page );
                        $height = $height_orig;
 
-                       $showLink = false;
-                       $linkAttribs = array( 'href' => $full_url );
-                       $longDesc = $this->displayImg->getLongDesc();
+                       $longDesc = wfMsg( 'parentheses', $this->displayImg->getLongDesc() );
 
                        wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this, &$wgOut ) );
 
@@ -371,9 +373,7 @@ class ImagePage extends Article {
                                $thumbnail = $this->displayImg->transform( $params );
 
                                $showLink = true;
-                               if ( !$this->displayImg->mustRender() ) {
-                                       $anchorclose = "<br />" . $msgsmall;
-                               }
+                               $anchorclose = "<br />" . $msgsmall;
 
                                $isMulti = $this->displayImg->isMultipage() && $this->displayImg->pageCount() > 1;
                                if ( $isMulti ) {
@@ -441,7 +441,7 @@ class ImagePage extends Article {
                                        $wgOut->addHTML(
                                                '</td><td><div class="multipageimagenavbox">' .
                                                Xml::openElement( 'form', $formParams ) .
-                                               Xml::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) .
+                                               Html::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) .
                                                wfMsgExt( 'imgmultigoto', array( 'parseinline', 'replaceafter' ), $select ) .
                                                Xml::submitButton( wfMsg( 'imgmultigo' ) ) .
                                                Xml::closeElement( 'form' ) .
@@ -503,8 +503,16 @@ EOT
                        {
                                $nofile = 'filepage-nofile';
                        }
+                       // Note, if there is an image description page, but
+                       // no image, then this setRobotPolicy is overriden
+                       // by Article::View().
                        $wgOut->setRobotPolicy( 'noindex,nofollow' );
                        $wgOut->wrapWikiMsg( "<div id='mw-imagepage-nofile' class='plainlinks'>\n$1\n</div>", $nofile );
+                       if ( !$this->getID() ) {
+                               // If there is no image, no shared image, and no description page,
+                               // output a 404, to be consistent with articles.
+                               $wgRequest->response()->header( "HTTP/1.x 404 Not Found" );
+                       }
                }
        }
 
@@ -519,6 +527,11 @@ EOT
                $descUrl = $this->img->getDescriptionUrl();
                $descText = $this->img->getDescriptionText();
 
+               /* Add canonical to head if there is no local page for this shared file */
+               if( $descUrl && $this->getID() == 0 ) {
+                       $wgOut->addLink( array( 'rel' => 'canonical', 'href' => $descUrl ) );
+               }
+
                $wrap = "<div class=\"sharedUploadNotice\">\n$1\n</div>\n";
                $repo = $this->img->getRepo()->getDisplayName();
 
@@ -551,7 +564,9 @@ EOT
        protected function uploadLinksBox() {
                global $wgUser, $wgOut, $wgEnableUploads, $wgUseExternalEditor;
 
-               if ( !$wgEnableUploads ) { return; }
+               if ( !$wgEnableUploads ) {
+                       return;
+               }
 
                $this->loadFile();
                if ( !$this->img->isLocal() )
@@ -598,6 +613,7 @@ EOT
                $this->loadFile();
                $pager = new ImageHistoryPseudoPager( $this );
                $wgOut->addHTML( $pager->getBody() );
+               $wgOut->preventClickjacking( $pager->getPreventClickjacking() );
 
                $this->img->resetHistory(); // free db resources
 
@@ -643,7 +659,7 @@ EOT
                $sk = $wgUser->getSkin();
                $count = 0;
                $elements = array();
-               while ( $s = $res->fetchObject() ) {
+               foreach ( $res as $s ) {
                        $count++;
                        if ( $count <= $limit ) {
                                // We have not yet reached the extra one that tells us there is more to fetch
@@ -708,7 +724,9 @@ EOT
                $this->loadFile();
 
                $dupes = $this->getDuplicates();
-               if ( count( $dupes ) == 0 ) return;
+               if ( count( $dupes ) == 0 ) {
+                       return;
+               }
 
                $wgOut->addHTML( "<div id='mw-imagepage-section-duplicates'>\n" );
                $wgOut->addWikiMsg( 'duplicatesoffile',
@@ -799,7 +817,6 @@ EOT
                $wgOut->addWikiText( $description );
        }
 
-
        /**
         * Callback for usort() to do link sorts by (namespace, title)
         * Function copied from Title::compare()
@@ -824,8 +841,32 @@ EOT
  */
 class ImageHistoryList {
 
-       protected $imagePage, $img, $skin, $title, $repo, $showThumb;
+       /**
+        * @var Title
+        */
+       protected $title;
+
+       /**
+        * @var File
+        */
+       protected $img;
+
+       /**
+        * @var ImagePage
+        */
+       protected $imagePage;
+
+       /**
+        * @var Skin
+        */
+       protected $skin;
+
+       protected $repo, $showThumb;
+       protected $preventClickjacking = false;
 
+       /**
+        * @param ImagePage $imagePage
+        */
        public function __construct( $imagePage ) {
                global $wgUser, $wgShowArchiveThumbnails;
                $this->skin = $wgUser->getSkin();
@@ -869,6 +910,11 @@ class ImageHistoryList {
                return "</table>\n$navLinks\n</div>\n";
        }
 
+       /**
+        * @param  $iscur
+        * @param File $file
+        * @return string
+        */
        public function imageHistoryLine( $iscur, $file ) {
                global $wgUser, $wgLang;
 
@@ -951,6 +997,7 @@ class ImageHistoryList {
                        # Don't link to unviewable files
                        $row .= '<span class="history-deleted">' . $wgLang->timeAndDate( $timestamp, true ) . '</span>';
                } elseif ( $file->isDeleted( File::DELETED_FILE ) ) {
+                       $this->preventClickjacking();
                        $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
                        # Make a link to review the image
                        $url = $this->skin->link(
@@ -1005,12 +1052,17 @@ class ImageHistoryList {
                }
                $row .= '</td>';
 
+               $rowClass = null;
                wfRunHooks( 'ImagePageFileHistoryLine', array( $this, $file, &$row, &$rowClass ) );
                $classAttr = $rowClass ? " class='$rowClass'" : "";
 
                return "<tr{$classAttr}>{$row}</tr>\n";
        }
 
+       /**
+        * @param File $file
+        * @return string
+        */
        protected function getThumbForLine( $file ) {
                global $wgLang;
 
@@ -1037,9 +1089,32 @@ class ImageHistoryList {
                        return wfMsgHtml( 'filehist-nothumb' );
                }
        }
+
+       protected function preventClickjacking( $enable = true ) {
+               $this->preventClickjacking = $enable;
+       }
+
+       public function getPreventClickjacking() {
+               return $this->preventClickjacking;
+       }
 }
 
 class ImageHistoryPseudoPager extends ReverseChronologicalPager {
+       protected $preventClickjacking = false;
+
+       /**
+        * @var File
+        */
+       protected $mImg;
+
+       /**
+        * @var Title
+        */
+       protected $mTitle;
+
+       /**
+        * @param ImagePage $imagePage
+        */
        function __construct( $imagePage ) {
                parent::__construct();
                $this->mImagePage = $imagePage;
@@ -1080,6 +1155,10 @@ class ImageHistoryPseudoPager extends ReverseChronologicalPager {
                                $s .= $list->imageHistoryLine( !$file->isOld(), $file );
                        }
                        $s .= $list->endImageHistoryList( $navLink );
+
+                       if ( $list->getPreventClickjacking() ) {
+                               $this->preventClickjacking();
+                       }
                }
                return $s;
        }
@@ -1162,4 +1241,13 @@ class ImageHistoryPseudoPager extends ReverseChronologicalPager {
                }
                $this->mQueryDone = true;
        }
+       
+       protected function preventClickjacking( $enable = true ) {
+               $this->preventClickjacking = $enable;
+       }
+
+       public function getPreventClickjacking() {
+               return $this->preventClickjacking;
+       }
+
 }