X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FImagePage.php;h=fb4fe248fc2f46f2343c44b32b7ee3bc4dc66c7d;hb=6145465e34a26d498cdca0a196cc025213127c9f;hp=b975dc207762082d20c2d584380a2dd9e51eef58;hpb=06ad0d2551fdeebced15d0759971550e10e305c1;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/ImagePage.php b/includes/ImagePage.php index b975dc2077..fb4fe248fc 100644 --- a/includes/ImagePage.php +++ b/includes/ImagePage.php @@ -1,81 +1,117 @@ img = wfFindFile( $this->mTitle, $time ); - if ( !$this->img ) { - $this->img = wfLocalFile( $this->mTitle ); - $this->current = $this->img; - } else { - $this->current = $time ? wfLocalFile( $this->mTitle ) : $this->img; + $this->dupes = null; + $this->repo = null; + } + + public function setFile( $file ) { + $this->displayImg = $file; + $this->img = $file; + $this->fileLoaded = true; + } + + protected function loadFile() { + if( $this->fileLoaded ) { + return true; } - $this->repo = $this->img->repo; + $this->fileLoaded = true; + + $this->displayImg = $this->img = false; + wfRunHooks( 'ImagePageFindFile', array( $this, &$this->img, &$this->displayImg ) ); + if( !$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(); } /** * Handler for action=render * Include body text only; none of the image extras */ - function render() { + public function render() { global $wgOut; $wgOut->setArticleBodyOnly( true ); parent::view(); } - function view() { + public function view() { global $wgOut, $wgShowEXIF, $wgRequest, $wgUser; + $this->loadFile(); + + if( $this->mTitle->getNamespace() == NS_FILE && $this->img->getRedirected() ) { + if( $this->mTitle->getDBkey() == $this->img->getName() ) { + // mTitle is the same as the redirect target so ask Article + // to perform the redirect for us. + return Article::view(); + } else { + // mTitle is not the same as the redirect target so it is + // probably the redirect page itself. Fake the redirect symbol + $wgOut->setPageTitle( $this->mTitle->getPrefixedText() ); + $wgOut->addHTML( $this->viewRedirect( Title::makeTitle( NS_FILE, $this->img->getName() ), + /* $appendSubtitle */ true, /* $forceKnown */ true ) ); + $this->viewUpdates(); + return; + } + } $diff = $wgRequest->getVal( 'diff' ); $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) ); - if ( $this->mTitle->getNamespace() != NS_IMAGE || ( isset( $diff ) && $diffOnly ) ) + if( $this->mTitle->getNamespace() != NS_FILE || ( 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( !$diff && $this->displayImg->exists() ) + $wgOut->addHTML( $this->showTOC($showmeta) ); - $this->openShowImage(); + if( !$diff ) + $this->openShowImage(); # No need to display noarticletext, we use our own message, output in openShowImage() - if ( $this->getID() ) { + if( $this->getID() ) { Article::view(); } else { # Just need to set the right headers $wgOut->setArticleFlag( true ); - $wgOut->setRobotpolicy( 'noindex,nofollow' ); + $wgOut->setRobotPolicy( 'noindex,nofollow' ); $wgOut->setPageTitle( $this->mTitle->getPrefixedText() ); $this->viewUpdates(); } # Show shared description, if needed - if ( $this->mExtraDescription ) { + if( $this->mExtraDescription ) { $fol = wfMsgNoTrans( 'shareddescriptionfollows' ); if( $fol != '-' && !wfEmptyMsg( 'shareddescriptionfollows', $fol ) ) { $wgOut->addWikiText( $fol ); @@ -87,36 +123,111 @@ class ImagePage extends Article { $this->closeShowImage(); $this->imageHistory(); + // TODO: Cleanup the following - - if ( $showmeta ) { + $wgOut->addHTML( Xml::element( 'h2', + array( 'id' => 'filelinks' ), + wfMsg( 'imagelinks' ) ) . "\n" ); + $this->imageDupes(); + $this->imageRedirects(); + $this->imageLinks(); + + if( $showmeta ) { global $wgStylePath, $wgStyleVersion; - $expand = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-expand' ) ) ); - $collapse = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-collapse' ) ) ); + $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( - "\n" . "\n" ); } } + + public function getRedirectTarget() { + $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; + } + return $this->mRedirectTarget = Title::makeTitle( NS_FILE, $to ); + } + public function followRedirect() { + $this->loadFile(); + if( $this->img->isLocal() ) { + return parent::followRedirect(); + } + $from = $this->img->getRedirected(); + $to = $this->img->getName(); + if( $from == $to ) { + return false; + } + return Title::makeTitle( NS_FILE, $to ); + } + public function isRedirect( $text = false ) { + $this->loadFile(); + if( $this->img->isLocal() ) + return parent::isRedirect( $text ); + + return (bool)$this->img->getRedirected(); + } + + 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() { + $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(); + 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 * - * @access private - * * @param bool $metadata Whether or not to show the metadata link * @return string */ - function showTOC( $metadata ) { + protected function showTOC( $metadata ) { global $wgLang; - $wlh = SpecialPage::getTitleFor( 'Whatlinkshere' ); - $backlinksUrl = $wlh->getLocalUrl( 'hidelinks=1&hidetrans=1&target=' . urlencode( $this->mTitle->getFullText() ) ); $r = ''; return $r; @@ -127,16 +238,15 @@ class ImagePage extends Article { * * FIXME: bad interface, see note on MediaHandler::formatMetadata(). * - * @access private - * * @param array $exif The array containing the EXIF data * @return string */ - function makeMetadataTable( $metadata ) { + protected function makeMetadataTable( $metadata ) { $r = wfMsg( 'metadata-help' ) . "\n\n"; $r .= "{| id=mw_metadata class=mw_metadata\n"; foreach ( $metadata as $type => $stuff ) { foreach ( $stuff as $v ) { + # FIXME, why is this using escapeId for a class?! $class = Sanitizer::escapeId( $v['id'] ); if( $type == 'collapsed' ) { $class .= ' collapsable'; @@ -156,17 +266,20 @@ class ImagePage extends Article { * Omit noarticletext if sharedupload; text will be fetched from the * shared upload server if possible. */ - function getContent() { + public function getContent() { + $this->loadFile(); if( $this->img && !$this->img->isLocal() && 0 == $this->getID() ) { return ''; } return Article::getContent(); } - function openShowImage() { + protected 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] ) ) { @@ -185,36 +298,36 @@ 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 ) ) { + if( is_null( $page ) ) { $params = array(); $page = 1; } 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 ) { + if( $width > $maxWidth || $height > $maxHeight ) { # Calculate the thumbnail size. # First case, the limiting factor is the width, not the height. - if ( $width / $height >= $maxWidth / $maxHeight ) { + if( $width / $height >= $maxWidth / $maxHeight ) { $height = round( $height * $maxWidth / $width); $width = $maxWidth; # Note that $height <= $maxHeight now. @@ -226,19 +339,21 @@ class ImagePage extends Article { # because of rounding. } $msgbig = wfMsgHtml( 'show-big-image' ); - $msgsmall = wfMsgExt( 'show-big-image-thumb', - array( 'parseinline' ), $wgLang->formatNum( $width ), $wgLang->formatNum( $height ) ); + $msgsmall = wfMsgExt( 'show-big-image-thumb', '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 = "
"; - if( $this->img->mustRender() ) { + if( $this->displayImg->mustRender() ) { $showLink = true; } else { $anchorclose .= @@ -246,13 +361,13 @@ class ImagePage extends Article { '
' . Xml::tags( 'a', $linkAttribs, $msgbig ) . "$dirmark " . $longDesc; } - if ( $this->img->isMultipage() ) { + if( $this->displayImg->isMultipage() ) { $wgOut->addHTML( '
' ); } - if ( $thumbnail ) { + if( $thumbnail ) { $options = array( - 'alt' => $this->img->getTitle()->getPrefixedText(), + 'alt' => $this->displayImg->getTitle()->getPrefixedText(), 'file-link' => true, ); $wgOut->addHTML( '' ); } - if ( $this->img->isMultipage() ) { - $count = $this->img->pageCount(); + if( $this->displayImg->isMultipage() ) { + $count = $this->displayImg->pageCount(); - if ( $page > 1 ) { + 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 = ''; } - if ( $page < $count ) { + 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 = ''; } global $wgScript; - $select = '
' . - Xml::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ); - $select .= $wgOut->parse( wfMsg( 'imgmultigotopre' ), false ) . - ' ' . $wgOut->parse( wfMsg( 'imgmultigotopost' ), false ) . - '
'; - - $wgOut->addHTML( '
' . - "$select
$thumb1\n$thumb2
" ); + $select = Xml::tags( 'select', + array( 'id' => 'pageselector', 'name' => 'page' ), + implode( "\n", $options ) ); + + $wgOut->addHTML( + '
' . + Xml::openElement( 'form', $formParams ) . + Xml::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) . + wfMsgExt( 'imgmultigoto', array( 'parseinline', 'replaceafter' ), $select ) . + Xml::submitButton( wfMsg( 'imgmultigo' ) ) . + Xml::closeElement( 'form' ) . + "
$thumb1\n$thumb2
" + ); } } 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( '