* (bug 28010) Passing a non existant user to list=users gives internal error
[lhc/web/wiklou.git] / includes / ImagePage.php
index 613a36a..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;
@@ -61,13 +69,22 @@ class ImagePage extends Article {
 
        public function view() {
                global $wgOut, $wgShowEXIF, $wgRequest, $wgUser;
+
+               $diff = $wgRequest->getVal( 'diff' );
+               $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
+
+               if ( $this->mTitle->getNamespace() != NS_FILE || ( isset( $diff ) && $diffOnly ) ) {
+                       return parent::view();
+               }
+                       
                $this->loadFile();
 
                if ( $this->mTitle->getNamespace() == NS_FILE && $this->img->getRedirected() ) {
-                       if ( $this->mTitle->getDBkey() == $this->img->getName() ) {
+                       if ( $this->mTitle->getDBkey() == $this->img->getName() || isset( $diff ) ) {
                                // mTitle is the same as the redirect target so ask Article
                                // to perform the redirect for us.
-                               return Article::view();
+                               $wgRequest->setVal( 'diffonly', 'true' );
+                               return parent::view();
                        } else {
                                // mTitle is not the same as the redirect target so it is 
                                // probably the redirect page itself. Fake the redirect symbol
@@ -79,12 +96,6 @@ class ImagePage extends Article {
                        }
                }
 
-               $diff = $wgRequest->getVal( 'diff' );
-               $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
-
-               if ( $this->mTitle->getNamespace() != NS_FILE || ( isset( $diff ) && $diffOnly ) )
-                       return Article::view();
-                       
                $this->showRedirectedFromHeader();
 
                if ( $wgShowEXIF && $this->displayImg->exists() ) {
@@ -103,7 +114,7 @@ class ImagePage extends Article {
 
                # No need to display noarticletext, we use our own message, output in openShowImage()
                if ( $this->getID() ) {
-                       Article::view();
+                       parent::view();
                } else {
                        # Just need to set the right headers
                        $wgOut->setArticleFlag( true );
@@ -113,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" );
                }
@@ -140,14 +151,14 @@ class ImagePage extends Article {
                        $wgOut->addHTML( $html );
 
                if ( $showmeta ) {
-                       global $wgStylePath, $wgStyleVersion;
-                       $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->addScriptFile( 'metadata.js' );
-                       $wgOut->addHTML(
-                               "<script type=\"text/javascript\">attachMetadataToggle('mw_metadata', '$expand', '$collapse');</script>\n" );
+                       $wgOut->addModules( array( 'mediawiki.legacy.metadata' ) );
+               }
+               
+               $css = $this->repo->getDescriptionStylesheetUrl();
+               if ( $css ) {
+                       $wgOut->addStyle( $css );
                }
        }
        
@@ -164,6 +175,7 @@ class ImagePage extends Article {
                }
                return $this->mRedirectTarget = Title::makeTitle( NS_FILE, $to );
        }
+
        public function followRedirect() {
                $this->loadFile();
                if ( $this->img->isLocal() ) {
@@ -176,6 +188,7 @@ class ImagePage extends Article {
                }
                return Title::makeTitle( NS_FILE, $to );
        }
+
        public function isRedirect( $text = false ) {
                $this->loadFile();
                if ( $this->img->isLocal() )
@@ -221,13 +234,12 @@ class ImagePage extends Article {
                return $this->dupes = $dupes;
                
        }
-       
 
        /**
         * Create the TOC
         *
-        * @param bool $metadata Whether or not to show the metadata link
-        * @return string
+        * @param $metadata Boolean: whether or not to show the metadata link
+        * @return String
         */
        protected function showTOC( $metadata ) {
                $r = array(
@@ -249,8 +261,8 @@ class ImagePage extends Article {
         *
         * FIXME: bad interface, see note on MediaHandler::formatMetadata().
         *
-        * @param array $exif The array containing the EXIF data
-        * @return string
+        * @param $metadata Array: the array containing the EXIF data
+        * @return String
         */
        protected function makeMetadataTable( $metadata ) {
                $r = "<div class=\"mw-imagepage-section-metadata\">";
@@ -283,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() {
@@ -292,8 +304,6 @@ class ImagePage extends Article {
 
                $this->loadFile();
 
-               $full_url  = $this->displayImg->getURL();
-               $linkAttribs = false;
                $sizeSel = intval( $wgUser->getOption( 'imagesize' ) );
                if ( !isset( $wgImageLimits[$sizeSel] ) ) {
                        $sizeSel = User::getDefaultOption( 'imagesize' );
@@ -320,14 +330,12 @@ class ImagePage extends Article {
                        } else {
                                $params = array( 'page' => $page );
                        }
-                       $width_orig = $this->displayImg->getWidth();
+                       $width_orig = $this->displayImg->getWidth( $page );
                        $width = $width_orig;
-                       $height_orig = $this->displayImg->getHeight();
+                       $height_orig = $this->displayImg->getHeight( $page );
                        $height = $height_orig;
-                       $mime = $this->displayImg->getMimeType();
-                       $showLink = false;
-                       $linkAttribs = array( 'href' => $full_url );
-                       $longDesc = $this->displayImg->getLongDesc();
+
+                       $longDesc = wfMsg( 'parentheses', $this->displayImg->getLongDesc() );
 
                        wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this, &$wgOut ) );
 
@@ -358,21 +366,14 @@ class ImagePage extends Article {
                                        );
                                } else {
                                        # Image is small enough to show full size on image page
-                                       $msgbig = htmlspecialchars( $this->displayImg->getName() );
                                        $msgsmall = wfMsgExt( 'file-nohires', array( 'parseinline' ) );
                                }
 
                                $params['width'] = $width;
                                $thumbnail = $this->displayImg->transform( $params );
 
-                               $anchorclose = "<br />";
-                               if ( $this->displayImg->mustRender() ) {
-                                       $showLink = true;
-                               } else {
-                                       $anchorclose .=
-                                               $msgsmall .
-                                               '<br />' . Xml::tags( 'a', $linkAttribs,  $msgbig ) . "$dirmark " . $longDesc;
-                               }
+                               $showLink = true;
+                               $anchorclose = "<br />" . $msgsmall;
 
                                $isMulti = $this->displayImg->isMultipage() && $this->displayImg->pageCount() > 1;
                                if ( $isMulti ) {
@@ -430,7 +431,6 @@ class ImagePage extends Article {
                                                'onchange' => 'document.pageselector.submit();',
                                        );
 
-                                       $option = array();
                                        for ( $i = 1; $i <= $count; $i++ ) {
                                                $options[] = Xml::option( $wgLang->formatNum( $i ), $i, $i == $page );
                                        }
@@ -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' ) .
@@ -464,23 +464,22 @@ class ImagePage extends Article {
 
                        if ( $showLink ) {
                                $filename = wfEscapeWikiText( $this->displayImg->getName() );
-                               $medialink = "[[Media:$filename|$filename]]";
+                               $linktext = $filename;
+                               if ( isset( $msgbig ) ) {
+                                       $linktext = wfEscapeWikiText( $msgbig );
+                               }
+                               $medialink = "[[Media:$filename|$linktext]]";
 
                                if ( !$this->displayImg->isSafeFile() ) {
                                        $warning = wfMsgNoTrans( 'mediawarning' );
                                        $wgOut->addWikiText( <<<EOT
-<div class="fullMedia">
-<span class="dangerousLink">{$medialink}</span>$dirmark
-<span class="fileInfo">$longDesc</span>
-</div>
+<div class="fullMedia"><span class="dangerousLink">{$medialink}</span>$dirmark <span class="fileInfo">$longDesc</span></div>
 <div class="mediaWarning">$warning</div>
 EOT
                                                );
                                } else {
                                        $wgOut->addWikiText( <<<EOT
-<div class="fullMedia">
-{$medialink}{$dirmark}
-<span class="fileInfo">$longDesc</span>
+<div class="fullMedia">{$medialink}{$dirmark} <span class="fileInfo">$longDesc</span>
 </div>
 EOT
                                        );
@@ -504,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" );
+                       }
                }
        }
 
@@ -520,10 +527,14 @@ 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();
 
-               $msg = '';
                if ( $descUrl && $descText && wfMsgNoTrans( 'sharedupload-desc-here' ) !== '-'  ) {
                        $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-here', $repo, $descUrl ) );
                } elseif ( $descUrl && wfMsgNoTrans( 'sharedupload-desc-there' ) !== '-' ) {
@@ -553,7 +564,9 @@ EOT
        protected function uploadLinksBox() {
                global $wgUser, $wgOut, $wgEnableUploads, $wgUseExternalEditor;
 
-               if ( !$wgEnableUploads ) { return; }
+               if ( !$wgEnableUploads ) {
+                       return;
+               }
 
                $this->loadFile();
                if ( !$this->img->isLocal() )
@@ -600,6 +613,7 @@ EOT
                $this->loadFile();
                $pager = new ImageHistoryPseudoPager( $this );
                $wgOut->addHTML( $pager->getBody() );
+               $wgOut->preventClickjacking( $pager->getPreventClickjacking() );
 
                $this->img->resetHistory(); // free db resources
 
@@ -626,9 +640,7 @@ EOT
                );
                $count = $dbr->numRows( $res );
                if ( $count == 0 ) {
-                       $wgOut->addHTML( "<div id='mw-imagepage-nolinkstoimage'>\n" );
-                       $wgOut->addWikiMsg( 'nolinkstoimage' );
-                       $wgOut->addHTML( "</div>\n" );
+                       $wgOut->wrapWikiMsg( Html::rawElement( 'div', array ( 'id' => 'mw-imagepage-nolinkstoimage' ), "\n$1\n" ), 'nolinkstoimage' );
                        return;
                }
                
@@ -643,30 +655,40 @@ EOT
                        );
                }
 
-               $wgOut->addHTML( "<ul class='mw-imagepage-linkstoimage'>\n" );
+               $wgOut->addHTML( Html::openElement( 'ul', array( 'class' => 'mw-imagepage-linkstoimage' ) ) . "\n" );
                $sk = $wgUser->getSkin();
                $count = 0;
-               while ( $s = $res->fetchObject() ) {
+               $elements = array();
+               foreach ( $res as $s ) {
                        $count++;
                        if ( $count <= $limit ) {
                                // We have not yet reached the extra one that tells us there is more to fetch
-                               $link = $sk->link(
-                                       Title::makeTitle( $s->page_namespace, $s->page_title ),
-                                       null,
-                                       array(),
-                                       array(),
-                                       array( 'known', 'noclasses' )
-                               );
-                               $wgOut->addHTML( "<li>{$link}</li>\n" );
+                               $elements[] =  $s;
                        }
                }
-               $wgOut->addHTML( "</ul>\n" );
+
+               // Sort the list by namespace:title
+               usort ( $elements, array( $this, 'compare' ) );
+
+               // Create links for every element
+               foreach( $elements as $element ) {    
+                       $link = $sk->linkKnown( Title::makeTitle( $element->page_namespace, $element->page_title ) );
+                       $wgOut->addHTML( Html::rawElement(
+                                               'li',
+                                               array( 'id' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace ),
+                                               $link
+                                       ) . "\n"
+                       );
+
+               };
+               $wgOut->addHTML( Html::closeElement( 'ul' ) . "\n" );
                $res->free();
 
                // Add a links to [[Special:Whatlinkshere]]
-               if ( $count > $limit )
+               if ( $count > $limit ) {
                        $wgOut->addWikiMsg( 'morelinkstoimage', $this->mTitle->getPrefixedDBkey() );
-               $wgOut->addHTML( "</div>\n" );
+               }
+               $wgOut->addHTML( Html::closeElement( 'div' ) . "\n" );
        }
        
        protected function imageRedirects() {
@@ -702,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',
@@ -738,14 +762,14 @@ EOT
                global $wgUploadMaintenance;
                if ( $wgUploadMaintenance && $this->mTitle && $this->mTitle->getNamespace() == NS_FILE ) {
                        global $wgOut;
-                       $wgOut->wrapWikiMsg( "<div class='error'>\n$1</div>\n", array( 'filedelete-maintenance' ) );
+                       $wgOut->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n", array( 'filedelete-maintenance' ) );
                        return;
                }
 
                $this->loadFile();
                if ( !$this->img->exists() || !$this->img->isLocal() || $this->img->getRedirected() ) {
                        // Standard article deletion
-                       Article::delete();
+                       parent::delete();
                        return;
                }
                $deleter = new FileDeleteForm( $this->img );
@@ -793,6 +817,21 @@ EOT
                $wgOut->addWikiText( $description );
        }
 
+       /**
+        * Callback for usort() to do link sorts by (namespace, title)
+        * Function copied from Title::compare()
+        * 
+        * @param $a object page to compare with
+        * @param $b object page to compare with
+        * @return Integer: result of string comparison, or namespace comparison
+        */
+       protected function compare( $a, $b ) {
+               if ( $a->page_namespace == $b->page_namespace ) {
+                       return strcmp( $a->page_title, $b->page_title );
+               } else {
+                       return $a->page_namespace - $b->page_namespace;
+               }
+       }
 }
 
 /**
@@ -802,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();
@@ -847,8 +910,13 @@ class ImageHistoryList {
                return "</table>\n$navLinks\n</div>\n";
        }
 
+       /**
+        * @param  $iscur
+        * @param File $file
+        * @return string
+        */
        public function imageHistoryLine( $iscur, $file ) {
-               global $wgUser, $wgLang, $wgContLang, $wgTitle;
+               global $wgUser, $wgLang;
 
                $timestamp = wfTimestamp( TS_MW, $file->getTimestamp() );
                $img = $iscur ? $file->getName() : $file->getArchiveName();
@@ -857,7 +925,7 @@ class ImageHistoryList {
                $description = $file->getDescription();
 
                $local = $this->current->isLocal();
-               $row = $css = $selected = '';
+               $row = $selected = '';
 
                // Deletion link
                if ( $local && ( $wgUser->isAllowed( 'delete' ) || $wgUser->isAllowed( 'deletedhistory' ) ) ) {
@@ -886,7 +954,7 @@ class ImageHistoryList {
                                        list( $ts, $name ) = explode( '!', $img, 2 );
                                        $query = array(
                                                'type'   => 'oldimage',
-                                               'target' => $wgTitle->getPrefixedText(),
+                                               'target' => $this->title->getPrefixedText(),
                                                'ids'    => $ts,
                                        );
                                        $del = $this->skin->revDeleteLink( $query,
@@ -929,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(
@@ -936,7 +1005,7 @@ class ImageHistoryList {
                                $wgLang->timeAndDate( $timestamp, true ),
                                array(),
                                array(
-                                       'target' => $wgTitle->getPrefixedText(),
+                                       'target' => $this->title->getPrefixedText(),
                                        'file' => $img,
                                        'token' => $wgUser->editToken( $img )
                                ),
@@ -983,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;
 
@@ -1015,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;
@@ -1058,6 +1155,10 @@ class ImageHistoryPseudoPager extends ReverseChronologicalPager {
                                $s .= $list->imageHistoryLine( !$file->isOld(), $file );
                        }
                        $s .= $list->endImageHistoryList( $navLink );
+
+                       if ( $list->getPreventClickjacking() ) {
+                               $this->preventClickjacking();
+                       }
                }
                return $s;
        }
@@ -1140,4 +1241,13 @@ class ImageHistoryPseudoPager extends ReverseChronologicalPager {
                }
                $this->mQueryDone = true;
        }
+       
+       protected function preventClickjacking( $enable = true ) {
+               $this->preventClickjacking = $enable;
+       }
+
+       public function getPreventClickjacking() {
+               return $this->preventClickjacking;
+       }
+
 }