X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FImagePage.php;h=f95e536a9af79283bde93e4bc39dd82b3adac83a;hb=48fe5241a539e1d5afcc75a40a9d23f53d092ed4;hp=002fc250c3365319c89e026e7313dba52465f8ef;hpb=2ed1bedb949fa19f5fda1c3ef789d92fa1ae0e23;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/ImagePage.php b/includes/ImagePage.php index 002fc250c3..f95e536a9a 100644 --- a/includes/ImagePage.php +++ b/includes/ImagePage.php @@ -1,168 +1,288 @@ 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->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 ); + } } - $this->repo = $this->img->repo; + 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 ); - $wgOut->addWikiTextTitleTidy( $this->getContent(), $this->mTitle ); + 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(); + + $this->showRedirectedFromHeader(); - if ($wgShowEXIF && $this->img->exists()) { - // FIXME: bad interface, see note on MediaHandler::formatMetadata(). - $formattedMetadata = $this->img->formatMetadata(); + if( $wgShowEXIF && $this->displayImg->exists() ) { + // FIXME: bad interface, see note on MediaHandler::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->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 ); } - $wgOut->addHTML( '
' . $this->mExtraDescription . '
' ); + $wgOut->addHTML( '
' . $this->mExtraDescription . "
\n" ); } $this->closeShowImage(); $this->imageHistory(); + // TODO: Cleanup the following + + $wgOut->addHTML( Xml::element( 'h2', + array( 'id' => 'filelinks' ), + wfMsg( 'imagelinks' ) ) . "\n" ); + $this->imageDupes(); + # TODO! FIXME! For some freaky reason, we can't redirect to foreign images. + # Yet we return metadata about the target. Definitely an issue in the FileRepo + $this->imageRedirects(); $this->imageLinks(); - if ( $showmeta ) { + if( $showmeta ) { global $wgStylePath, $wgStyleVersion; - $expand = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-expand' ) ) ); - $collapse = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-collapse' ) ) ); - $wgOut->addHTML( Xml::element( 'h2', array( 'id' => 'metadata' ), wfMsg( 'metadata' ) ). "\n" ); + $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; $r = ''; +
  • ' . wfMsgHtml( 'imagelinks' ) . "
  • \n" . + ($metadata ? '
  • ' . wfMsgHtml( 'metadata' ) . '
  • ' : '') . " + \n"; return $r; } /** * Make a table with metadata to be shown in the output page. * - * FIXME: bad interface, see note on MediaHandler::formatMetadata(). - * - * @access private + * FIXME: bad interface, see note on MediaHandler::formatMetadata(). * * @param array $exif The array containing the EXIF data * @return string */ - function makeMetadataTable( $metadata ) { - $r = wfMsg( 'metadata-help' ) . "\n\n"; - $r .= "{| id=mw_metadata class=mw_metadata\n"; + protected function makeMetadataTable( $metadata ) { + $r = "
    "; + $r .= wfMsgNoTrans( 'metadata-help' ); + $r .= "\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'; } - $r .= "|- class=\"$class\"\n"; - $r .= "!| {$v['name']}\n"; - $r .= "|| {$v['value']}\n"; + $r .= "\n"; + $r .= "\n"; + $r .= "\n"; } } - $r .= '|}'; + $r .= "
    {$v['name']}{$v['value']}
    \n
    \n"; return $r; } /** * Overloading Article's getContent method. - * + * * 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() { - global $wgOut, $wgUser, $wgImageLimits, $wgRequest, $wgLang, $wgContLang; + protected function openShowImage() { + global $wgOut, $wgUser, $wgImageLimits, $wgRequest, + $wgLang, $wgContLang, $wgEnableUploads; + + $this->loadFile(); - $full_url = $this->img->getURL(); + $full_url = $this->displayImg->getURL(); $linkAttribs = false; $sizeSel = intval( $wgUser->getOption( 'imagesize') ); if( !isset( $wgImageLimits[$sizeSel] ) ) { @@ -181,36 +301,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. @@ -222,270 +342,395 @@ 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 .= + $anchorclose .= $msgsmall . '
    ' . Xml::tags( 'a', $linkAttribs, $msgbig ) . "$dirmark " . $longDesc; } - if ( $this->img->isMultipage() ) { + if( $this->displayImg->isMultipage() ) { $wgOut->addHTML( '
    ' ); } - if ( $thumbnail ) { - $options = array( - 'alt' => $this->img->getTitle()->getPrefixedText(), + if( $thumbnail ) { + $options = array( + 'alt' => $this->displayImg->getTitle()->getPrefixedText(), 'file-link' => true, ); - $wgOut->addHTML( '\n" ); } - 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', + $link = $sk->link( + $this->mTitle, + $label, + array(), + array( 'page' => $page - 1 ), + array( 'known', 'noclasses' ) + ); + $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', + $link = $sk->link( + $this->mTitle, + $label, + array(), + array( 'page' => $page + 1 ), + array( 'known', 'noclasses' ) + ); + $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( '' ); + $icon->toHtml( array( 'file-link' => true ) ) . + "\n" ); } $showLink = true; } - if ($showLink) { - $filename = wfEscapeWikiText( $this->img->getName() ); + if($showLink) { + $filename = wfEscapeWikiText( $this->displayImg->getName() ); + $medialink = "[[Media:$filename|$filename]]"; - if (!$this->img->isSafeFile()) { + if( !$this->displayImg->isSafeFile() ) { $warning = wfMsgNoTrans( 'mediawarning' ); $wgOut->addWikiText( << -[[Media:$filename|$filename]]$dirmark - $longDesc +{$medialink}$dirmark +$longDesc -
    $warning
    EOT ); } else { $wgOut->addWikiText( << -[[Media:$filename|$filename]]$dirmark $longDesc +{$medialink}{$dirmark} +$longDesc EOT - ); + ); } } - if(!$this->img->isLocal()) { + if( !$this->displayImg->isLocal() ) { $this->printSharedImageText(); } } else { # Image does not exist - - $title = SpecialPage::getTitleFor( 'Upload' ); - $link = $sk->makeKnownLinkObj($title, wfMsgHtml('noimage-linktext'), - 'wpDestFile=' . urlencode( $this->img->getName() ) ); - $wgOut->addHTML( wfMsgWikiHtml( 'noimage', $link ) ); + if ( $wgEnableUploads && $wgUser->isAllowed( 'upload' ) ) { + // Only show an upload link if the user can upload + $uploadTitle = SpecialPage::getTitleFor( 'Upload' ); + $nofile = array( + 'filepage-nofile-link', + $uploadTitle->getFullUrl( array( 'wpDestFile' => $this->img->getName() ) ) + ); + } + else + { + $nofile = 'filepage-nofile'; + } + $wgOut->setRobotPolicy( 'noindex,nofollow' ); + $wgOut->wrapWikiMsg( "", $nofile ); } } /** * Show a notice that the file is from a shared repository */ - function printSharedImageText() { - global $wgOut, $wgUser; + protected function printSharedImageText() { + global $wgOut; + + $this->loadFile(); $descUrl = $this->img->getDescriptionUrl(); $descText = $this->img->getDescriptionText(); - $s = "
    " . wfMsgWikiHtml( 'sharedupload' ); - if ( $descUrl ) { - $sk = $wgUser->getSkin(); - $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; - } + + $wrap = "
    \n$1\n
    \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' ) !== '-' ) { + $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-there', $repo, $descUrl ) ); + } else { + $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload', $repo ), ''/*BACKCOMPAT*/ ); } - $s .= "
    "; - $wgOut->addHTML( $s ); - if ( $descText ) { + if( $descText ) { $this->mExtraDescription = $descText; } } - function getUploadUrl() { + public function getUploadUrl() { + $this->loadFile(); $uploadTitle = SpecialPage::getTitleFor( 'Upload' ); - return $uploadTitle->getFullUrl( 'wpDestFile=' . urlencode( $this->img->getName() ) ); + return $uploadTitle->getFullUrl( array( + 'wpDestFile' => $this->img->getName(), + 'wpForReUpload' => 1 + ) ); } /** * Print out the various links at the bottom of the image page, e.g. reupload, * external editing (and instructions link) etc. */ - function uploadLinksBox() { - global $wgUser, $wgOut; + protected function uploadLinksBox() { + global $wgUser, $wgOut, $wgEnableUploads; + + if( !$wgEnableUploads ) { return; } + $this->loadFile(); if( !$this->img->isLocal() ) return; $sk = $wgUser->getSkin(); - - $wgOut->addHtml( '
    \n" ); } + protected function closeShowImage() {} # For overloading + /** * If the page we've just displayed is in the "Image" namespace, * we follow it with an upload history of the image and its usage. */ - function imageHistory() - { - global $wgUser, $wgOut, $wgUseExternalEditor; - - $sk = $wgUser->getSkin(); + protected function imageHistory() { + global $wgOut, $wgUseExternalEditor; - if ( $this->img->exists() ) { - $list = new ImageHistoryList( $sk, $this->current ); - $file = $this->current; - $dims = $file->getDimensionsString(); - $s = $list->beginImageHistoryList() . - $list->imageHistoryLine( true, $file ); - // old image versions - $hist = $this->img->getHistory(); - foreach( $hist as $file ) { - $dims = $file->getDimensionsString(); - $s .= $list->imageHistoryLine( false, $file ); - } - $s .= $list->endImageHistoryList(); - } else { $s=''; } - $wgOut->addHTML( $s ); + $this->loadFile(); + $pager = new ImageHistoryPseudoPager( $this ); + $wgOut->addHTML( $pager->getBody() ); - $this->img->resetHistory(); // free db resources + $this->img->resetHistory(); // free db resources # Exist check because we don't want to show this on pages where an image # doesn't exist along with the noimage message, that would suck. -ævar if( $wgUseExternalEditor && $this->img->exists() ) { $this->uploadLinksBox(); } - } - function imageLinks() - { - global $wgUser, $wgOut; + protected function imageLinks() { + global $wgUser, $wgOut, $wgLang; - $wgOut->addHTML( Xml::element( 'h2', array( 'id' => 'filelinks' ), wfMsg( 'imagelinks' ) ) . "\n" ); + $limit = 100; $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" ); - - if ( 0 == $dbr->numRows( $res ) ) { - $wgOut->addHtml( '

    ' . wfMsg( "nolinkstoimage" ) . "

    \n" ); + $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) + ); + $count = $dbr->numRows( $res ); + if( $count == 0 ) { + $wgOut->addHTML( "
    \n" ); + $wgOut->addWikiMsg( 'nolinkstoimage' ); + $wgOut->addHTML( "
    \n" ); return; } - $wgOut->addHTML( '

    ' . wfMsg( 'linkstoimage' ) . "

    \n