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 );
41 $wgOut->addWikiTextTitleTidy( $this->getContent(), $this->mTitle
);
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>' );
86 $this->closeShowImage();
87 $this->imageHistory();
91 global $wgStylePath, $wgStyleVersion;
92 $expand = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-expand' ) ) );
93 $collapse = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-collapse' ) ) );
94 $wgOut->addHTML( Xml
::element( 'h2', array( 'id' => 'metadata' ), wfMsg( 'metadata' ) ). "\n" );
95 $wgOut->addWikiText( $this->makeMetadataTable( $formattedMetadata ) );
97 "<script type=\"text/javascript\" src=\"$wgStylePath/common/metadata.js?$wgStyleVersion\"></script>\n" .
98 "<script type=\"text/javascript\">attachMetadataToggle('mw_metadata', '$expand', '$collapse');</script>\n" );
107 * @param bool $metadata Whether or not to show the metadata link
110 function showTOC( $metadata ) {
112 $r = '<ul id="filetoc">
113 <li><a href="#file">' . $wgLang->getNsText( NS_IMAGE
) . '</a></li>
114 <li><a href="#filehistory">' . wfMsgHtml( 'filehist' ) . '</a></li>
115 <li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>' .
116 ($metadata ?
' <li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>' : '') . '
122 * Make a table with metadata to be shown in the output page.
124 * FIXME: bad interface, see note on MediaHandler::formatMetadata().
128 * @param array $exif The array containing the EXIF data
131 function makeMetadataTable( $metadata ) {
132 $r = wfMsg( 'metadata-help' ) . "\n\n";
133 $r .= "{| id=mw_metadata class=mw_metadata\n";
134 foreach ( $metadata as $type => $stuff ) {
135 foreach ( $stuff as $v ) {
136 $class = Sanitizer
::escapeId( $v['id'] );
137 if( $type == 'collapsed' ) {
138 $class .= ' collapsable';
140 $r .= "|- class=\"$class\"\n";
141 $r .= "!| {$v['name']}\n";
142 $r .= "|| {$v['value']}\n";
150 * Overloading Article's getContent method.
152 * Omit noarticletext if sharedupload; text will be fetched from the
153 * shared upload server if possible.
155 function getContent() {
156 if( $this->img
&& !$this->img
->isLocal() && 0 == $this->getID() ) {
159 return Article
::getContent();
162 function openShowImage() {
163 global $wgOut, $wgUser, $wgImageLimits, $wgRequest, $wgLang, $wgContLang;
165 $full_url = $this->img
->getURL();
166 $linkAttribs = false;
167 $sizeSel = intval( $wgUser->getOption( 'imagesize') );
168 if( !isset( $wgImageLimits[$sizeSel] ) ) {
169 $sizeSel = User
::getDefaultOption( 'imagesize' );
171 // The user offset might still be incorrect, specially if
172 // $wgImageLimits got changed (see bug #8858).
173 if( !isset( $wgImageLimits[$sizeSel] ) ) {
174 // Default to the first offset in $wgImageLimits
178 $max = $wgImageLimits[$sizeSel];
180 $maxHeight = $max[1];
181 $sk = $wgUser->getSkin();
182 $dirmark = $wgContLang->getDirMark();
184 if ( $this->img
->exists() ) {
186 $page = $wgRequest->getIntOrNull( 'page' );
187 if ( is_null( $page ) ) {
191 $params = array( 'page' => $page );
193 $width_orig = $this->img
->getWidth();
194 $width = $width_orig;
195 $height_orig = $this->img
->getHeight();
196 $height = $height_orig;
197 $mime = $this->img
->getMimeType();
199 $linkAttribs = array( 'href' => $full_url );
200 $longDesc = $this->img
->getLongDesc();
202 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this , &$wgOut ) ) ;
204 if ( $this->img
->allowInlineDisplay() ) {
207 # "Download high res version" link below the image
208 #$msgsize = wfMsgHtml('file-info-size', $width_orig, $height_orig, $sk->formatSize( $this->img->getSize() ), $mime );
209 # We'll show a thumbnail of this image
210 if ( $width > $maxWidth ||
$height > $maxHeight ) {
211 # Calculate the thumbnail size.
212 # First case, the limiting factor is the width, not the height.
213 if ( $width / $height >= $maxWidth / $maxHeight ) {
214 $height = round( $height * $maxWidth / $width);
216 # Note that $height <= $maxHeight now.
218 $newwidth = floor( $width * $maxHeight / $height);
219 $height = round( $height * $newwidth / $width );
221 # Note that $height <= $maxHeight now, but might not be identical
222 # because of rounding.
224 $msgbig = wfMsgHtml( 'show-big-image' );
225 $msgsmall = wfMsgExt( 'show-big-image-thumb',
226 array( 'parseinline' ), $wgLang->formatNum( $width ), $wgLang->formatNum( $height ) );
228 # Image is small enough to show full size on image page
229 $msgbig = htmlspecialchars( $this->img
->getName() );
230 $msgsmall = wfMsgExt( 'file-nohires', array( 'parseinline' ) );
233 $params['width'] = $width;
234 $thumbnail = $this->img
->transform( $params );
236 $anchorclose = "<br />";
237 if( $this->img
->mustRender() ) {
242 '<br />' . Xml
::tags( 'a', $linkAttribs, $msgbig ) . "$dirmark " . $longDesc;
245 if ( $this->img
->isMultipage() ) {
246 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
251 'alt' => $this->img
->getTitle()->getPrefixedText(),
254 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
255 $thumbnail->toHtml( $options ) .
256 $anchorclose . '</div>' );
259 if ( $this->img
->isMultipage() ) {
260 $count = $this->img
->pageCount();
263 $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
264 $link = $sk->makeKnownLinkObj( $this->mTitle
, $label, 'page='. ($page-1) );
265 $thumb1 = $sk->makeThumbLinkObj( $this->mTitle
, $this->img
, $link, $label, 'none',
266 array( 'page' => $page - 1 ) );
271 if ( $page < $count ) {
272 $label = wfMsg( 'imgmultipagenext' );
273 $link = $sk->makeKnownLinkObj( $this->mTitle
, $label, 'page='. ($page+
1) );
274 $thumb2 = $sk->makeThumbLinkObj( $this->mTitle
, $this->img
, $link, $label, 'none',
275 array( 'page' => $page +
1 ) );
281 $select = '<form name="pageselector" action="' .
282 htmlspecialchars( $wgScript ) .
283 '" method="get" onchange="document.pageselector.submit();">' .
284 Xml
::hidden( 'title', $this->getTitle()->getPrefixedDbKey() );
285 $select .= $wgOut->parse( wfMsg( 'imgmultigotopre' ), false ) .
286 ' <select id="pageselector" name="page">';
287 for ( $i=1; $i <= $count; $i++
) {
288 $select .= Xml
::option( $wgLang->formatNum( $i ), $i,
291 $select .= '</select>' . $wgOut->parse( wfMsg( 'imgmultigotopost' ), false ) .
292 '<input type="submit" value="' .
293 htmlspecialchars( wfMsg( 'imgmultigo' ) ) . '"></form>';
295 $wgOut->addHTML( '</td><td><div class="multipageimagenavbox">' .
296 "$select<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>" );
299 #if direct link is allowed but it's not a renderable image, show an icon.
300 if ($this->img
->isSafeFile()) {
301 $icon= $this->img
->iconThumb();
303 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
304 $icon->toHtml( array( 'desc-link' => true ) ) .
313 $filename = wfEscapeWikiText( $this->img
->getName() );
315 if (!$this->img
->isSafeFile()) {
316 $warning = wfMsgNoTrans( 'mediawarning' );
317 $wgOut->addWikiText( <<<EOT
318 <div class="fullMedia">
319 <span class="dangerousLink">[[Media:$filename|$filename]]</span>$dirmark
320 <span class="fileInfo"> $longDesc</span>
323 <div class="mediaWarning">$warning</div>
327 $wgOut->addWikiText( <<<EOT
328 <div class="fullMedia">
329 [[Media:$filename|$filename]]$dirmark <span class="fileInfo"> $longDesc</span>
336 if(!$this->img
->isLocal()) {
337 $this->printSharedImageText();
340 # Image does not exist
342 $title = SpecialPage
::getTitleFor( 'Upload' );
343 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('noimage-linktext'),
344 'wpDestFile=' . urlencode( $this->img
->getName() ) );
345 $wgOut->addHTML( wfMsgWikiHtml( 'noimage', $link ) );
350 * Show a notice that the file is from a shared repository
352 function printSharedImageText() {
353 global $wgOut, $wgUser;
355 $descUrl = $this->img
->getDescriptionUrl();
356 $descText = $this->img
->getDescriptionText();
357 $s = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml( 'sharedupload' );
359 $sk = $wgUser->getSkin();
360 $link = $sk->makeExternalLink( $descUrl, wfMsg( 'shareduploadwiki-linktext' ) );
361 $msg = ( $descText ) ?
'shareduploadwiki-desc' : 'shareduploadwiki';
362 $msg = wfMsgExt( $msg, array( 'parseinline', 'replaceafter' ), $link );
364 # Show message only if not voided by local sysops
369 $wgOut->addHTML( $s );
372 $this->mExtraDescription
= $descText;
376 function getUploadUrl() {
377 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
378 return $uploadTitle->getFullUrl( 'wpDestFile=' . urlencode( $this->img
->getName() ) );
382 * Print out the various links at the bottom of the image page, e.g. reupload,
383 * external editing (and instructions link) etc.
385 function uploadLinksBox() {
386 global $wgUser, $wgOut;
388 if( !$this->img
->isLocal() )
391 $sk = $wgUser->getSkin();
393 $wgOut->addHtml( '<br /><ul>' );
395 # "Upload a new version of this file" link
396 if( UploadForm
::userCanReUpload($wgUser,$this->img
->name
) ) {
397 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
398 $wgOut->addHtml( "<li><div class='plainlinks'>{$ulink}</div></li>" );
401 # Link to Special:FileDuplicateSearch
402 $dupeLink = $sk->makeKnownLinkObj( SpecialPage
::getTitleFor( 'FileDuplicateSearch', $this->mTitle
->getDBkey() ), wfMsgHtml( 'imagepage-searchdupe' ) );
403 $wgOut->addHtml( "<li>{$dupeLink}</li>" );
405 # External editing link
406 $elink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsgHtml( 'edit-externally' ), 'action=edit&externaledit=true&mode=file' );
407 $wgOut->addHtml( '<li>' . $elink . '<div>' . wfMsgWikiHtml( 'edit-externally-help' ) . '</div></li>' );
409 $wgOut->addHtml( '</ul>' );
412 function closeShowImage()
419 * If the page we've just displayed is in the "Image" namespace,
420 * we follow it with an upload history of the image and its usage.
422 function imageHistory()
424 global $wgUser, $wgOut, $wgUseExternalEditor;
426 $sk = $wgUser->getSkin();
428 if ( $this->img
->exists() ) {
429 $list = new ImageHistoryList( $sk, $this->current
);
430 $file = $this->current
;
431 $dims = $file->getDimensionsString();
432 $s = $list->beginImageHistoryList() .
433 $list->imageHistoryLine( true, $file );
434 // old image versions
435 $hist = $this->img
->getHistory();
436 foreach( $hist as $file ) {
437 $dims = $file->getDimensionsString();
438 $s .= $list->imageHistoryLine( false, $file );
440 $s .= $list->endImageHistoryList();
442 $wgOut->addHTML( $s );
444 $this->img
->resetHistory(); // free db resources
446 # Exist check because we don't want to show this on pages where an image
447 # doesn't exist along with the noimage message, that would suck. -ævar
448 if( $wgUseExternalEditor && $this->img
->exists() ) {
449 $this->uploadLinksBox();
454 function imageLinks()
456 global $wgUser, $wgOut;
458 $wgOut->addHTML( Xml
::element( 'h2', array( 'id' => 'filelinks' ), wfMsg( 'imagelinks' ) ) . "\n" );
460 $dbr = wfGetDB( DB_SLAVE
);
461 $page = $dbr->tableName( 'page' );
462 $imagelinks = $dbr->tableName( 'imagelinks' );
464 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
465 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id";
466 $sql = $dbr->limitResult($sql, 500, 0);
467 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
469 if ( 0 == $dbr->numRows( $res ) ) {
470 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
473 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
475 $sk = $wgUser->getSkin();
476 while ( $s = $dbr->fetchObject( $res ) ) {
477 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
478 $link = $sk->makeKnownLinkObj( $name, "" );
479 $wgOut->addHTML( "<li>{$link}</li>\n" );
481 $wgOut->addHTML( "</ul>\n" );
485 * Delete the file, or an earlier version of it
487 public function delete() {
488 if( !$this->img
->exists() ||
!$this->img
->isLocal() ) {
489 // Standard article deletion
493 $deleter = new FileDeleteForm( $this->img
);
498 * Revert the file to an earlier version
500 public function revert() {
501 $reverter = new FileRevertForm( $this->img
);
502 $reverter->execute();
506 * Override handling of action=purge
509 if( $this->img
->exists() ) {
510 wfDebug( "ImagePage::doPurge purging " . $this->img
->getName() . "\n" );
511 $update = new HTMLCacheUpdate( $this->mTitle
, 'imagelinks' );
513 $this->img
->upgradeRow();
514 $this->img
->purgeCache();
516 wfDebug( "ImagePage::doPurge no image\n" );
522 * Display an error with a wikitext description
524 function showError( $description ) {
526 $wgOut->setPageTitle( wfMsg( "internalerror" ) );
527 $wgOut->setRobotpolicy( "noindex,nofollow" );
528 $wgOut->setArticleRelated( false );
529 $wgOut->enableClientCache( false );
530 $wgOut->addWikiText( $description );
536 * Builds the image revision log shown on image pages
540 class ImageHistoryList
{
542 protected $img, $skin, $title, $repo;
544 public function __construct( $skin, $img ) {
547 $this->title
= $img->getTitle();
550 public function beginImageHistoryList() {
551 global $wgOut, $wgUser;
552 $deleteColumn = $wgUser->isAllowed( 'delete' ) ||
$wgUser->isAllowed( 'deleterevision' );
553 return Xml
::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) )
554 . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
555 . Xml
::openElement( 'table', array( 'class' => 'filehistory' ) ) . "\n"
557 . ( $this->img
->isLocal() && $deleteColumn ?
'<td></td>' : '' )
558 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
559 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
560 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
561 . '<th class="mw-imagepage-filesize">' . wfMsgHtml( 'filehist-filesize' ) . '</th>'
562 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
566 public function endImageHistoryList() {
570 public function imageHistoryLine( $iscur, $file ) {
571 global $wgUser, $wgLang, $wgContLang, $wgTitle;
573 $timestamp = wfTimestamp(TS_MW
, $file->getTimestamp());
574 $img = $iscur ?
$file->getName() : $file->getArchiveName();
575 $user = $file->getUser('id');
576 $usertext = $file->getUser('text');
577 $size = $file->getSize();
578 $description = $file->getDescription();
579 $dims = $file->getDimensionsString();
580 $sha1 = $file->getSha1();
582 $local = $this->img
->isLocal();
586 if( $local && ($wgUser->isAllowed('delete') ||
$wgUser->isAllowed('deleterevision') ) ) {
588 # Link to remove from history
589 if( $wgUser->isAllowed( 'delete' ) ) {
591 $q[] = 'action=delete';
593 $q[] = 'oldimage=' . urlencode( $img );
594 $row .= $this->skin
->makeKnownLinkObj(
596 wfMsgHtml( $iscur ?
'filehist-deleteall' : 'filehist-deleteone' ),
601 # Link to hide content
602 if( $wgUser->isAllowed( 'deleterevision' ) ) {
603 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
604 // If file is top revision or locked from this user, don't link
605 if( $iscur ||
!$file->userCan(File
::DELETED_RESTRICTED
) ) {
606 $del = wfMsgHtml( 'rev-delundel' );
608 // If the file was hidden, link to sha-1
609 list($ts,$name) = explode('!',$img,2);
610 $del = $this->skin
->makeKnownLinkObj( $revdel, wfMsg( 'rev-delundel' ),
611 'target=' . urlencode( $wgTitle->getPrefixedText() ) .
612 '&oldimage=' . urlencode( $ts ) );
613 // Bolden oversighted content
614 if( $file->isDeleted(File
::DELETED_RESTRICTED
) )
615 $del = "<strong>$del</strong>";
617 $row .= "<tt><small>$del</small></tt>";
622 // Reversion link/current indicator
625 $row .= wfMsgHtml( 'filehist-current' );
626 } elseif( $local && $wgUser->isLoggedIn() && $this->title
->userCan( 'edit' ) ) {
627 if( $file->isDeleted(File
::DELETED_FILE
) ) {
628 $row .= wfMsgHtml('filehist-revert');
631 $q[] = 'action=revert';
632 $q[] = 'oldimage=' . urlencode( $img );
633 $q[] = 'wpEditToken=' . urlencode( $wgUser->editToken( $img ) );
634 $row .= $this->skin
->makeKnownLinkObj( $this->title
,
635 wfMsgHtml( 'filehist-revert' ),
636 implode( '&', $q ) );
641 // Date/time and image link
643 if( !$file->userCan(File
::DELETED_FILE
) ) {
644 # Don't link to unviewable files
645 $row .= '<span class="history-deleted">' . $wgLang->timeAndDate( $timestamp, true ) . '</span>';
646 } else if( $file->isDeleted(File
::DELETED_FILE
) ) {
647 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
648 # Make a link to review the image
649 $url = $this->skin
->makeKnownLinkObj( $revdel, $wgLang->timeAndDate( $timestamp, true ),
650 "target=".$wgTitle->getPrefixedText()."&file=$sha1.".$this->img
->getExtension() );
651 $row .= '<span class="history-deleted">'.$url.'</span>';
653 $url = $iscur ?
$this->img
->getUrl() : $this->img
->getArchiveUrl( $img );
654 $row .= Xml
::element( 'a',
655 array( 'href' => $url ),
656 $wgLang->timeAndDate( $timestamp, true ) );
664 // Hide deleted usernames
665 if( $file->isDeleted(File
::DELETED_USER
) )
666 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
668 $row .= $this->skin
->userLink( $user, $usertext ) .
669 $this->skin
->userToolLinks( $user, $usertext );
671 $row .= htmlspecialchars( $usertext );
676 $row .= '<td>' . htmlspecialchars( $dims ) . '</td>';
679 $row .= '<td class="mw-imagepage-filesize">' . $this->skin
->formatSize( $size ) . '</td>';
681 // Don't show deleted descriptions
682 if ( $file->isDeleted(File
::DELETED_COMMENT
) )
683 $row .= '<td><span class="history-deleted">' . wfMsgHtml('rev-deleted-comment') . '</span></td>';
685 $row .= '<td>' . $this->skin
->commentBlock( $description, $this->title
) . '</td>';
687 return "<tr>{$row}</tr>\n";