8 if( !defined( 'MEDIAWIKI' ) )
12 * Special handling for image description pages
16 class ImagePage
extends Article
{
18 /* private */ var $img; // Image object this page is shown for
19 /* private */ var $repo;
20 var $mExtraDescription = false;
22 function __construct( $title, $time = false ) {
23 parent
::__construct( $title );
24 $this->img
= wfFindFile( $this->mTitle
, $time );
26 $this->img
= wfLocalFile( $this->mTitle
);
27 $this->current
= $this->img
;
29 $this->current
= $time ?
wfLocalFile( $this->mTitle
) : $this->img
;
31 $this->repo
= $this->img
->repo
;
35 * Handler for action=render
36 * Include body text only; none of the image extras
40 $wgOut->setArticleBodyOnly( true );
45 global $wgOut, $wgShowEXIF, $wgRequest, $wgUser;
47 $diff = $wgRequest->getVal( 'diff' );
48 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
50 if ( $this->mTitle
->getNamespace() != NS_IMAGE ||
( isset( $diff ) && $diffOnly ) )
51 return Article
::view();
53 if ($wgShowEXIF && $this->img
->exists()) {
54 // FIXME: bad interface, see note on MediaHandler::formatMetadata().
55 $formattedMetadata = $this->img
->formatMetadata();
56 $showmeta = $formattedMetadata !== false;
61 if ($this->img
->exists())
62 $wgOut->addHTML($this->showTOC($showmeta));
64 $this->openShowImage();
66 # No need to display noarticletext, we use our own message, output in openShowImage()
67 if ( $this->getID() ) {
70 # Just need to set the right headers
71 $wgOut->setArticleFlag( true );
72 $wgOut->setRobotpolicy( 'noindex,nofollow' );
73 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
77 # Show shared description, if needed
78 if ( $this->mExtraDescription
) {
79 $fol = wfMsgNoTrans( 'shareddescriptionfollows' );
80 if( $fol != '-' && !wfEmptyMsg( 'shareddescriptionfollows', $fol ) ) {
81 $wgOut->addWikiText( $fol );
83 $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription
. '</div>' );
85 $this->checkSharedConflict();
88 $this->closeShowImage();
89 $this->imageHistory();
93 global $wgStylePath, $wgStyleVersion;
94 $expand = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-expand' ) ) );
95 $collapse = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-collapse' ) ) );
96 $wgOut->addHTML( Xml
::element( 'h2', array( 'id' => 'metadata' ), wfMsg( 'metadata' ) ). "\n" );
97 $wgOut->addWikiText( $this->makeMetadataTable( $formattedMetadata ) );
99 "<script type=\"text/javascript\" src=\"$wgStylePath/common/metadata.js?$wgStyleVersion\"></script>\n" .
100 "<script type=\"text/javascript\">attachMetadataToggle('mw_metadata', '$expand', '$collapse');</script>\n" );
109 * @param bool $metadata Whether or not to show the metadata link
112 function showTOC( $metadata ) {
114 $r = '<ul id="filetoc">
115 <li><a href="#file">' . $wgLang->getNsText( NS_IMAGE
) . '</a></li>
116 <li><a href="#filehistory">' . wfMsgHtml( 'filehist' ) . '</a></li>
117 <li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>' .
118 ($metadata ?
' <li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>' : '') . '
124 * Make a table with metadata to be shown in the output page.
126 * FIXME: bad interface, see note on MediaHandler::formatMetadata().
130 * @param array $exif The array containing the EXIF data
133 function makeMetadataTable( $metadata ) {
134 $r = wfMsg( 'metadata-help' ) . "\n\n";
135 $r .= "{| id=mw_metadata class=mw_metadata\n";
136 foreach ( $metadata as $type => $stuff ) {
137 foreach ( $stuff as $v ) {
138 $class = Sanitizer
::escapeId( $v['id'] );
139 if( $type == 'collapsed' ) {
140 $class .= ' collapsable';
142 $r .= "|- class=\"$class\"\n";
143 $r .= "!| {$v['name']}\n";
144 $r .= "|| {$v['value']}\n";
152 * Overloading Article's getContent method.
154 * Omit noarticletext if sharedupload; text will be fetched from the
155 * shared upload server if possible.
157 function getContent() {
158 if( $this->img
&& !$this->img
->isLocal() && 0 == $this->getID() ) {
161 return Article
::getContent();
164 function openShowImage() {
165 global $wgOut, $wgUser, $wgImageLimits, $wgRequest, $wgLang, $wgContLang;
167 $full_url = $this->img
->getURL();
168 $linkAttribs = false;
169 $sizeSel = intval( $wgUser->getOption( 'imagesize') );
170 if( !isset( $wgImageLimits[$sizeSel] ) ) {
171 $sizeSel = User
::getDefaultOption( 'imagesize' );
173 // The user offset might still be incorrect, specially if
174 // $wgImageLimits got changed (see bug #8858).
175 if( !isset( $wgImageLimits[$sizeSel] ) ) {
176 // Default to the first offset in $wgImageLimits
180 $max = $wgImageLimits[$sizeSel];
182 $maxHeight = $max[1];
183 $sk = $wgUser->getSkin();
184 $dirmark = $wgContLang->getDirMark();
186 if ( $this->img
->exists() ) {
188 $page = $wgRequest->getIntOrNull( 'page' );
189 if ( is_null( $page ) ) {
193 $params = array( 'page' => $page );
195 $width_orig = $this->img
->getWidth();
196 $width = $width_orig;
197 $height_orig = $this->img
->getHeight();
198 $height = $height_orig;
199 $mime = $this->img
->getMimeType();
201 $linkAttribs = array( 'href' => $full_url );
202 $longDesc = $this->img
->getLongDesc();
204 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this , &$wgOut ) ) ;
206 if ( $this->img
->allowInlineDisplay() ) {
209 # "Download high res version" link below the image
210 #$msgsize = wfMsgHtml('file-info-size', $width_orig, $height_orig, $sk->formatSize( $this->img->getSize() ), $mime );
211 # We'll show a thumbnail of this image
212 if ( $width > $maxWidth ||
$height > $maxHeight ) {
213 # Calculate the thumbnail size.
214 # First case, the limiting factor is the width, not the height.
215 if ( $width / $height >= $maxWidth / $maxHeight ) {
216 $height = round( $height * $maxWidth / $width);
218 # Note that $height <= $maxHeight now.
220 $newwidth = floor( $width * $maxHeight / $height);
221 $height = round( $height * $newwidth / $width );
223 # Note that $height <= $maxHeight now, but might not be identical
224 # because of rounding.
226 $msgbig = wfMsgHtml( 'show-big-image' );
227 $msgsmall = wfMsgExt( 'show-big-image-thumb',
228 array( 'parseinline' ), $wgLang->formatNum( $width ), $wgLang->formatNum( $height ) );
230 # Image is small enough to show full size on image page
231 $msgbig = htmlspecialchars( $this->img
->getName() );
232 $msgsmall = wfMsgExt( 'file-nohires', array( 'parseinline' ) );
235 $params['width'] = $width;
236 $thumbnail = $this->img
->transform( $params );
238 $anchorclose = "<br />";
239 if( $this->img
->mustRender() ) {
244 '<br />' . Xml
::tags( 'a', $linkAttribs, $msgbig ) . "$dirmark " . $longDesc;
247 if ( $this->img
->isMultipage() ) {
248 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
253 'alt' => $this->img
->getTitle()->getPrefixedText(),
256 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
257 $thumbnail->toHtml( $options ) .
258 $anchorclose . '</div>' );
261 if ( $this->img
->isMultipage() ) {
262 $count = $this->img
->pageCount();
265 $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
266 $link = $sk->makeKnownLinkObj( $this->mTitle
, $label, 'page='. ($page-1) );
267 $thumb1 = $sk->makeThumbLinkObj( $this->mTitle
, $this->img
, $link, $label, 'none',
268 array( 'page' => $page - 1 ) );
273 if ( $page < $count ) {
274 $label = wfMsg( 'imgmultipagenext' );
275 $link = $sk->makeKnownLinkObj( $this->mTitle
, $label, 'page='. ($page+
1) );
276 $thumb2 = $sk->makeThumbLinkObj( $this->mTitle
, $this->img
, $link, $label, 'none',
277 array( 'page' => $page +
1 ) );
283 $select = '<form name="pageselector" action="' .
284 htmlspecialchars( $wgScript ) .
285 '" method="get" onchange="document.pageselector.submit();">' .
286 Xml
::hidden( 'title', $this->getTitle()->getPrefixedDbKey() );
287 $select .= $wgOut->parse( wfMsg( 'imgmultigotopre' ), false ) .
288 ' <select id="pageselector" name="page">';
289 for ( $i=1; $i <= $count; $i++
) {
290 $select .= Xml
::option( $wgLang->formatNum( $i ), $i,
293 $select .= '</select>' . $wgOut->parse( wfMsg( 'imgmultigotopost' ), false ) .
294 '<input type="submit" value="' .
295 htmlspecialchars( wfMsg( 'imgmultigo' ) ) . '"></form>';
297 $wgOut->addHTML( '</td><td><div class="multipageimagenavbox">' .
298 "$select<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>" );
301 #if direct link is allowed but it's not a renderable image, show an icon.
302 if ($this->img
->isSafeFile()) {
303 $icon= $this->img
->iconThumb();
305 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
306 $icon->toHtml( array( 'desc-link' => true ) ) .
315 $filename = wfEscapeWikiText( $this->img
->getName() );
317 if (!$this->img
->isSafeFile()) {
318 $warning = wfMsgNoTrans( 'mediawarning' );
319 $wgOut->addWikiText( <<<EOT
320 <div class="fullMedia">
321 <span class="dangerousLink">[[Media:$filename|$filename]]</span>$dirmark
322 <span class="fileInfo"> $longDesc</span>
325 <div class="mediaWarning">$warning</div>
329 $wgOut->addWikiText( <<<EOT
330 <div class="fullMedia">
331 [[Media:$filename|$filename]]$dirmark <span class="fileInfo"> $longDesc</span>
338 if(!$this->img
->isLocal()) {
339 $this->printSharedImageText();
342 # Image does not exist
344 $title = SpecialPage
::getTitleFor( 'Upload' );
345 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('noimage-linktext'),
346 'wpDestFile=' . urlencode( $this->img
->getName() ) );
347 $wgOut->addHTML( wfMsgWikiHtml( 'noimage', $link ) );
352 * Show a notice that the file is from a shared repository
354 function printSharedImageText() {
355 global $wgOut, $wgUser;
357 $descUrl = $this->img
->getDescriptionUrl();
358 $descText = $this->img
->getDescriptionText();
359 $s = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml( 'sharedupload' );
361 $sk = $wgUser->getSkin();
362 $link = $sk->makeExternalLink( $descUrl, wfMsg( 'shareduploadwiki-linktext' ) );
363 $msg = ( $descText ) ?
'shareduploadwiki-desc' : 'shareduploadwiki';
364 $msg = wfMsgExt( $msg, array( 'parseinline', 'replaceafter' ), $link );
366 # Show message only if not voided by local sysops
371 $wgOut->addHTML( $s );
374 $this->mExtraDescription
= $descText;
378 function checkSharedConflict() {
379 global $wgOut, $wgUser, $wgUseSharedUploads;
380 if( !$wgUseSharedUploads ) {
383 if( $this->repo
->getName() != 'local' ) {
387 $repo = RepoGroup
::singleton()->getRepoByName( 'shared' );
388 $dupfile = $repo->newFile( $this->img
->getTitle() );
389 if( !$dupfile->exists() ) {
393 ($this->img
->getSha1() == $dupfile->getSha1()) &&
394 ($this->img
->getSize() == $dupfile->getSize())
397 $sk = $wgUser->getSkin();
398 $descUrl = $dupfile->getDescriptionUrl();
400 $link = $sk->makeExternalLink( $descUrl, wfMsg( 'shareduploadduplicate-linktext' ) );
401 $wgOut->addHTML( '<div id="shared-image-dup">' . wfMsgWikiHtml( 'shareduploadduplicate', $link ) . '</div>' );
403 $link = $sk->makeExternalLink( $descUrl, wfMsg( 'shareduploadconflict-linktext' ) );
404 $wgOut->addHTML( '<div id="shared-image-conflict">' . wfMsgWikiHtml( 'shareduploadconflict', $link ) . '</div>' );
408 function getUploadUrl() {
409 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
410 return $uploadTitle->getFullUrl( 'wpDestFile=' . urlencode( $this->img
->getName() ) );
414 * Print out the various links at the bottom of the image page, e.g. reupload,
415 * external editing (and instructions link) etc.
417 function uploadLinksBox() {
418 global $wgUser, $wgOut;
420 if( !$this->img
->isLocal() )
423 $sk = $wgUser->getSkin();
425 $wgOut->addHtml( '<br /><ul>' );
427 # "Upload a new version of this file" link
428 if( UploadForm
::userCanReUpload($wgUser,$this->img
->name
) ) {
429 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
430 $wgOut->addHtml( "<li><div class='plainlinks'>{$ulink}</div></li>" );
433 # Link to Special:FileDuplicateSearch
434 $dupeLink = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'FileDuplicateSearch', $this->mTitle
->getDBkey() ), wfMsgHtml( 'imagepage-searchdupe' ) );
435 $wgOut->addHtml( "<li>{$dupeLink}</li>" );
437 # External editing link
438 $elink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsgHtml( 'edit-externally' ), 'action=edit&externaledit=true&mode=file' );
439 $wgOut->addHtml( '<li>' . $elink . '<div>' . wfMsgWikiHtml( 'edit-externally-help' ) . '</div></li>' );
441 $wgOut->addHtml( '</ul>' );
444 function closeShowImage()
451 * If the page we've just displayed is in the "Image" namespace,
452 * we follow it with an upload history of the image and its usage.
454 function imageHistory()
456 global $wgUser, $wgOut, $wgUseExternalEditor;
458 $sk = $wgUser->getSkin();
460 if ( $this->img
->exists() ) {
461 $list = new ImageHistoryList( $sk, $this->current
);
462 $file = $this->current
;
463 $dims = $file->getDimensionsString();
464 $s = $list->beginImageHistoryList() .
465 $list->imageHistoryLine( true, $file );
466 // old image versions
467 $hist = $this->img
->getHistory();
468 foreach( $hist as $file ) {
469 $dims = $file->getDimensionsString();
470 $s .= $list->imageHistoryLine( false, $file );
472 $s .= $list->endImageHistoryList();
474 $wgOut->addHTML( $s );
476 $this->img
->resetHistory(); // free db resources
478 # Exist check because we don't want to show this on pages where an image
479 # doesn't exist along with the noimage message, that would suck. -ævar
480 if( $wgUseExternalEditor && $this->img
->exists() ) {
481 $this->uploadLinksBox();
486 function imageLinks()
488 global $wgUser, $wgOut;
490 $wgOut->addHTML( Xml
::element( 'h2', array( 'id' => 'filelinks' ), wfMsg( 'imagelinks' ) ) . "\n" );
492 $dbr = wfGetDB( DB_SLAVE
);
493 $page = $dbr->tableName( 'page' );
494 $imagelinks = $dbr->tableName( 'imagelinks' );
496 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
497 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id";
498 $sql = $dbr->limitResult($sql, 500, 0);
499 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
501 if ( 0 == $dbr->numRows( $res ) ) {
502 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
505 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
507 $sk = $wgUser->getSkin();
508 while ( $s = $dbr->fetchObject( $res ) ) {
509 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
510 $link = $sk->makeKnownLinkObj( $name, "" );
511 $wgOut->addHTML( "<li>{$link}</li>\n" );
513 $wgOut->addHTML( "</ul>\n" );
517 * Delete the file, or an earlier version of it
519 public function delete() {
520 if( !$this->img
->exists() ||
!$this->img
->isLocal() ) {
521 // Standard article deletion
525 $deleter = new FileDeleteForm( $this->img
);
530 * Revert the file to an earlier version
532 public function revert() {
533 $reverter = new FileRevertForm( $this->img
);
534 $reverter->execute();
538 * Override handling of action=purge
541 if( $this->img
->exists() ) {
542 wfDebug( "ImagePage::doPurge purging " . $this->img
->getName() . "\n" );
543 $update = new HTMLCacheUpdate( $this->mTitle
, 'imagelinks' );
545 $this->img
->upgradeRow();
546 $this->img
->purgeCache();
548 wfDebug( "ImagePage::doPurge no image\n" );
554 * Display an error with a wikitext description
556 function showError( $description ) {
558 $wgOut->setPageTitle( wfMsg( "internalerror" ) );
559 $wgOut->setRobotpolicy( "noindex,nofollow" );
560 $wgOut->setArticleRelated( false );
561 $wgOut->enableClientCache( false );
562 $wgOut->addWikiText( $description );
568 * Builds the image revision log shown on image pages
572 class ImageHistoryList
{
574 protected $img, $skin, $title, $repo;
576 public function __construct( $skin, $img ) {
579 $this->title
= $img->getTitle();
582 public function beginImageHistoryList() {
583 global $wgOut, $wgUser;
584 $deleteColumn = $wgUser->isAllowed( 'delete' ) ||
$wgUser->isAllowed( 'deleterevision' );
585 return Xml
::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) )
586 . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
587 . Xml
::openElement( 'table', array( 'class' => 'filehistory' ) ) . "\n"
589 . ( $this->img
->isLocal() && $deleteColumn ?
'<td></td>' : '' )
590 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
591 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
592 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
593 . '<th class="mw-imagepage-filesize">' . wfMsgHtml( 'filehist-filesize' ) . '</th>'
594 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
598 public function endImageHistoryList() {
602 public function imageHistoryLine( $iscur, $file ) {
603 global $wgUser, $wgLang, $wgContLang, $wgTitle;
605 $timestamp = wfTimestamp(TS_MW
, $file->getTimestamp());
606 $img = $iscur ?
$file->getName() : $file->getArchiveName();
607 $user = $file->getUser('id');
608 $usertext = $file->getUser('text');
609 $size = $file->getSize();
610 $description = $file->getDescription();
611 $dims = $file->getDimensionsString();
612 $sha1 = $file->getSha1();
614 $local = $this->img
->isLocal();
618 if( $local && ($wgUser->isAllowed('delete') ||
$wgUser->isAllowed('deleterevision') ) ) {
620 # Link to remove from history
621 if( $wgUser->isAllowed( 'delete' ) ) {
623 $q[] = 'action=delete';
625 $q[] = 'oldimage=' . urlencode( $img );
626 $row .= $this->skin
->makeKnownLinkObj(
628 wfMsgHtml( $iscur ?
'filehist-deleteall' : 'filehist-deleteone' ),
633 # Link to hide content
634 if( $wgUser->isAllowed( 'deleterevision' ) ) {
635 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
636 // If file is top revision or locked from this user, don't link
637 if( $iscur ||
!$file->userCan(File
::DELETED_RESTRICTED
) ) {
638 $del = wfMsgHtml( 'rev-delundel' );
640 // If the file was hidden, link to sha-1
641 list($ts,$name) = explode('!',$img,2);
642 $del = $this->skin
->makeKnownLinkObj( $revdel, wfMsg( 'rev-delundel' ),
643 'target=' . urlencode( $wgTitle->getPrefixedText() ) .
644 '&oldimage=' . urlencode( $ts ) );
645 // Bolden oversighted content
646 if( $file->isDeleted(File
::DELETED_RESTRICTED
) )
647 $del = "<strong>$del</strong>";
649 $row .= "<tt><small>$del</small></tt>";
654 // Reversion link/current indicator
657 $row .= wfMsgHtml( 'filehist-current' );
658 } elseif( $local && $wgUser->isLoggedIn() && $this->title
->userCan( 'edit' ) ) {
659 if( $file->isDeleted(File
::DELETED_FILE
) ) {
660 $row .= wfMsgHtml('filehist-revert');
663 $q[] = 'action=revert';
664 $q[] = 'oldimage=' . urlencode( $img );
665 $q[] = 'wpEditToken=' . urlencode( $wgUser->editToken( $img ) );
666 $row .= $this->skin
->makeKnownLinkObj( $this->title
,
667 wfMsgHtml( 'filehist-revert' ),
668 implode( '&', $q ) );
673 // Date/time and image link
675 if( !$file->userCan(File
::DELETED_FILE
) ) {
676 # Don't link to unviewable files
677 $row .= '<span class="history-deleted">' . $wgLang->timeAndDate( $timestamp, true ) . '</span>';
678 } else if( $file->isDeleted(File
::DELETED_FILE
) ) {
679 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
680 # Make a link to review the image
681 $url = $this->skin
->makeKnownLinkObj( $revdel, $wgLang->timeAndDate( $timestamp, true ),
682 "target=".$wgTitle->getPrefixedText()."&file=$sha1.".$this->img
->getExtension() );
683 $row .= '<span class="history-deleted">'.$url.'</span>';
685 $url = $iscur ?
$this->img
->getUrl() : $this->img
->getArchiveUrl( $img );
686 $row .= Xml
::element( 'a',
687 array( 'href' => $url ),
688 $wgLang->timeAndDate( $timestamp, true ) );
696 // Hide deleted usernames
697 if( $file->isDeleted(File
::DELETED_USER
) )
698 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
700 $row .= $this->skin
->userLink( $user, $usertext ) .
701 $this->skin
->userToolLinks( $user, $usertext );
703 $row .= htmlspecialchars( $usertext );
708 $row .= '<td>' . htmlspecialchars( $dims ) . '</td>';
711 $row .= '<td class="mw-imagepage-filesize">' . $this->skin
->formatSize( $size ) . '</td>';
713 // Don't show deleted descriptions
714 if ( $file->isDeleted(File
::DELETED_COMMENT
) )
715 $row .= '<td><span class="history-deleted">' . wfMsgHtml('rev-deleted-comment') . '</span></td>';
717 $row .= '<td>' . $this->skin
->commentBlock( $description, $this->title
) . '</td>';
719 return "<tr>{$row}</tr>\n";