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->addSecondaryWikitext( $this->getContent() );
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 # External editing link
402 $elink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsgHtml( 'edit-externally' ), 'action=edit&externaledit=true&mode=file' );
403 $wgOut->addHtml( '<li>' . $elink . '<div>' . wfMsgWikiHtml( 'edit-externally-help' ) . '</div></li>' );
405 $wgOut->addHtml( '</ul>' );
408 function closeShowImage()
415 * If the page we've just displayed is in the "Image" namespace,
416 * we follow it with an upload history of the image and its usage.
418 function imageHistory()
420 global $wgUser, $wgOut, $wgUseExternalEditor;
422 $sk = $wgUser->getSkin();
424 if ( $this->img
->exists() ) {
425 $list = new ImageHistoryList( $sk, $this->current
);
426 $file = $this->current
;
427 $dims = $file->getDimensionsString();
428 $s = $list->beginImageHistoryList() .
429 $list->imageHistoryLine( true, wfTimestamp(TS_MW
, $file->getTimestamp()),
430 $this->mTitle
->getDBkey(), $file->getUser('id'),
431 $file->getUser('text'), $file->getSize(), $file->getDescription(),
435 $hist = $this->img
->getHistory();
436 foreach( $hist as $file ) {
437 $dims = $file->getDimensionsString();
438 $s .= $list->imageHistoryLine( false, wfTimestamp(TS_MW
, $file->getTimestamp()),
439 $file->getArchiveName(), $file->getUser('id'),
440 $file->getUser('text'), $file->getSize(), $file->getDescription(),
444 $s .= $list->endImageHistoryList();
446 $wgOut->addHTML( $s );
448 $this->img
->resetHistory(); // free db resources
450 # Exist check because we don't want to show this on pages where an image
451 # doesn't exist along with the noimage message, that would suck. -ævar
452 if( $wgUseExternalEditor && $this->img
->exists() ) {
453 $this->uploadLinksBox();
458 function imageLinks()
460 global $wgUser, $wgOut;
462 $wgOut->addHTML( Xml
::element( 'h2', array( 'id' => 'filelinks' ), wfMsg( 'imagelinks' ) ) . "\n" );
464 $dbr = wfGetDB( DB_SLAVE
);
465 $page = $dbr->tableName( 'page' );
466 $imagelinks = $dbr->tableName( 'imagelinks' );
468 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
469 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id";
470 $sql = $dbr->limitResult($sql, 500, 0);
471 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
473 if ( 0 == $dbr->numRows( $res ) ) {
474 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
477 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
479 $sk = $wgUser->getSkin();
480 while ( $s = $dbr->fetchObject( $res ) ) {
481 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
482 $link = $sk->makeKnownLinkObj( $name, "" );
483 $wgOut->addHTML( "<li>{$link}</li>\n" );
485 $wgOut->addHTML( "</ul>\n" );
489 * Delete the file, or an earlier version of it
491 public function delete() {
492 if( !$this->img
->exists() ||
!$this->img
->isLocal() ) {
493 // Standard article deletion
497 $deleter = new FileDeleteForm( $this->img
);
502 * Revert the file to an earlier version
504 public function revert() {
505 $reverter = new FileRevertForm( $this->img
);
506 $reverter->execute();
510 * Override handling of action=purge
513 if( $this->img
->exists() ) {
514 wfDebug( "ImagePage::doPurge purging " . $this->img
->getName() . "\n" );
515 $update = new HTMLCacheUpdate( $this->mTitle
, 'imagelinks' );
517 $this->img
->upgradeRow();
518 $this->img
->purgeCache();
520 wfDebug( "ImagePage::doPurge no image\n" );
526 * Display an error with a wikitext description
528 function showError( $description ) {
530 $wgOut->setPageTitle( wfMsg( "internalerror" ) );
531 $wgOut->setRobotpolicy( "noindex,nofollow" );
532 $wgOut->setArticleRelated( false );
533 $wgOut->enableClientCache( false );
534 $wgOut->addWikiText( $description );
540 * Builds the image revision log shown on image pages
544 class ImageHistoryList
{
546 protected $img, $skin, $title, $repo;
548 public function __construct( $skin, $img ) {
551 $this->title
= $img->getTitle();
554 public function beginImageHistoryList() {
555 global $wgOut, $wgUser;
556 return Xml
::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) )
557 . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
558 . Xml
::openElement( 'table', array( 'class' => 'filehistory' ) ) . "\n"
560 . ( $this->img
->isLocal() && $wgUser->isAllowed( 'delete' ) ?
'<td></td>' : '' )
561 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
562 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
563 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
564 . '<th class="mw-imagepage-filesize">' . wfMsgHtml( 'filehist-filesize' ) . '</th>'
565 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
569 public function endImageHistoryList() {
574 * Create one row of file history
576 * @param bool $iscur is this the current file version?
577 * @param string $timestamp timestamp of file version
578 * @param string $img filename
579 * @param int $user ID of uploading user
580 * @param string $usertext username of uploading user
581 * @param int $size size of file version
582 * @param string $description description of file version
583 * @param string $dims dimensions of file version
584 * @return string a HTML formatted table row
586 public function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description, $dims ) {
587 global $wgUser, $wgLang, $wgContLang;
588 $local = $this->img
->isLocal();
592 if( $local && $wgUser->isAllowed( 'delete' ) ) {
595 $q[] = 'action=delete';
597 $q[] = 'oldimage=' . urlencode( $img );
598 $row .= $this->skin
->makeKnownLinkObj(
600 wfMsgHtml( $iscur ?
'filehist-deleteall' : 'filehist-deleteone' ),
606 // Reversion link/current indicator
609 $row .= wfMsgHtml( 'filehist-current' );
610 } elseif( $local && $wgUser->isLoggedIn() && $this->title
->userCan( 'edit' ) ) {
612 $q[] = 'action=revert';
613 $q[] = 'oldimage=' . urlencode( $img );
614 $q[] = 'wpEditToken=' . urlencode( $wgUser->editToken( $img ) );
615 $row .= $this->skin
->makeKnownLinkObj(
617 wfMsgHtml( 'filehist-revert' ),
623 // Date/time and image link
625 $url = $iscur ?
$this->img
->getUrl() : $this->img
->getArchiveUrl( $img );
626 $row .= Xml
::element(
628 array( 'href' => $url ),
629 $wgLang->timeAndDate( $timestamp, true )
636 $row .= $this->skin
->userLink( $user, $usertext ) . $this->skin
->userToolLinks( $user, $usertext );
638 $row .= htmlspecialchars( $usertext );
643 $row .= '<td>' . htmlspecialchars( $dims ) . '</td>';
646 $row .= '<td class="mw-imagepage-filesize">' . $this->skin
->formatSize( $size ) . '</td>';
649 $row .= '<td>' . $this->skin
->formatComment( $description, $this->title
) . '</td>';
651 return "<tr>{$row}</tr>\n";