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 var $mExtraDescription = false;
21 function __construct( $title ) {
22 parent
::__construct( $title );
23 $this->img
= wfFindFile( $this->mTitle
);
25 $this->img
= wfLocalFile( $this->mTitle
);
30 * Handler for action=render
31 * Include body text only; none of the image extras
35 $wgOut->setArticleBodyOnly( true );
36 $wgOut->addSecondaryWikitext( $this->getContent() );
40 global $wgOut, $wgShowEXIF, $wgRequest, $wgUser;
42 $diff = $wgRequest->getVal( 'diff' );
43 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
45 if ( $this->mTitle
->getNamespace() != NS_IMAGE ||
( isset( $diff ) && $diffOnly ) )
46 return Article
::view();
48 if ($wgShowEXIF && $this->img
->exists()) {
49 $formattedMetadata = $this->img
->formatMetadata();
50 $showmeta = $formattedMetadata !== false;
55 if ($this->img
->exists())
56 $wgOut->addHTML($this->showTOC($showmeta));
58 $this->openShowImage();
60 # No need to display noarticletext, we use our own message, output in openShowImage()
61 if ( $this->getID() ) {
64 # Just need to set the right headers
65 $wgOut->setArticleFlag( true );
66 $wgOut->setRobotpolicy( 'index,follow' );
67 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
71 # Show shared description, if needed
72 if ( $this->mExtraDescription
) {
73 $fol = wfMsg( 'shareddescriptionfollows' );
74 if( $fol != '-' && !wfEmptyMsg( 'shareddescriptionfollows', $fol ) ) {
75 $wgOut->addWikiText( $fol );
77 $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription
. '</div>' );
80 $this->closeShowImage();
81 $this->imageHistory();
85 global $wgStylePath, $wgStyleVersion;
86 $expand = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-expand' ) ) );
87 $collapse = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-collapse' ) ) );
88 $wgOut->addHTML( Xml
::element( 'h2', array( 'id' => 'metadata' ), wfMsg( 'metadata' ) ). "\n" );
89 $wgOut->addWikiText( $this->makeMetadataTable( $formattedMetadata ) );
91 "<script type=\"text/javascript\" src=\"$wgStylePath/common/metadata.js?$wgStyleVersion\"></script>\n" .
92 "<script type=\"text/javascript\">attachMetadataToggle('mw_metadata', '$expand', '$collapse');</script>\n" );
101 * @param bool $metadata Whether or not to show the metadata link
104 function showTOC( $metadata ) {
106 $r = '<ul id="filetoc">
107 <li><a href="#file">' . $wgLang->getNsText( NS_IMAGE
) . '</a></li>
108 <li><a href="#filehistory">' . wfMsgHtml( 'filehist' ) . '</a></li>
109 <li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>' .
110 ($metadata ?
' <li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>' : '') . '
116 * Make a table with metadata to be shown in the output page.
120 * @param array $exif The array containing the EXIF data
123 function makeMetadataTable( $metadata ) {
124 $r = wfMsg( 'metadata-help' ) . "\n\n";
125 $r .= "{| id=mw_metadata class=mw_metadata\n";
126 foreach ( $metadata as $type => $stuff ) {
127 foreach ( $stuff as $k => $v ) {
128 $class = Sanitizer
::escapeId( $v['id'] );
129 if( $type == 'collapsed' ) {
130 $class .= ' collapsable';
132 $r .= "|- class=\"$class\"\n";
133 $r .= "!| {$v['name']}\n";
134 $r .= "|| {$v['value']}\n";
142 * Overloading Article's getContent method.
144 * Omit noarticletext if sharedupload; text will be fetched from the
145 * shared upload server if possible.
147 function getContent() {
148 if( $this->img
&& !$this->img
->isLocal() && 0 == $this->getID() ) {
151 return Article
::getContent();
154 function openShowImage() {
155 global $wgOut, $wgUser, $wgImageLimits, $wgRequest, $wgLang;
157 $full_url = $this->img
->getURL();
158 $linkAttribs = false;
159 $sizeSel = intval( $wgUser->getOption( 'imagesize') );
160 if( !isset( $wgImageLimits[$sizeSel] ) ) {
161 $sizeSel = User
::getDefaultOption( 'imagesize' );
163 // The user offset might still be incorrect, specially if
164 // $wgImageLimits got changed (see bug #8858).
165 if( !isset( $wgImageLimits[$sizeSel] ) ) {
166 // Default to the first offset in $wgImageLimits
170 $max = $wgImageLimits[$sizeSel];
172 $maxHeight = $max[1];
173 $sk = $wgUser->getSkin();
175 if ( $this->img
->exists() ) {
177 $page = $wgRequest->getIntOrNull( 'page' );
178 if ( is_null( $page ) ) {
182 $params = array( 'page' => $page );
184 $width_orig = $this->img
->getWidth();
185 $width = $width_orig;
186 $height_orig = $this->img
->getHeight();
187 $height = $height_orig;
188 $mime = $this->img
->getMimeType();
190 $linkAttribs = array( 'href' => $full_url );
192 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this , &$wgOut ) ) ;
194 if ( $this->img
->allowInlineDisplay() and $width and $height) {
197 # "Download high res version" link below the image
198 $msgsize = wfMsgHtml('file-info-size', $width_orig, $height_orig, $sk->formatSize( $this->img
->getSize() ), $mime );
199 # We'll show a thumbnail of this image
200 if ( $width > $maxWidth ||
$height > $maxHeight ) {
201 # Calculate the thumbnail size.
202 # First case, the limiting factor is the width, not the height.
203 if ( $width / $height >= $maxWidth / $maxHeight ) {
204 $height = round( $height * $maxWidth / $width);
206 # Note that $height <= $maxHeight now.
208 $newwidth = floor( $width * $maxHeight / $height);
209 $height = round( $height * $newwidth / $width );
211 # Note that $height <= $maxHeight now, but might not be identical
212 # because of rounding.
214 $msgbig = wfMsgHtml( 'show-big-image' );
215 $msgsmall = wfMsgExt( 'show-big-image-thumb',
216 array( 'parseinline' ), $width, $height );
218 # Image is small enough to show full size on image page
219 $msgbig = htmlspecialchars( $this->img
->getName() );
220 $msgsmall = wfMsgExt( 'file-nohires', array( 'parseinline' ) );
223 $params['width'] = $width;
224 $thumbnail = $this->img
->transform( $params );
226 $anchorclose = "<br />";
227 if( $this->img
->mustRender() ) {
232 '<br />' . Xml
::tags( 'a', $linkAttribs, $msgbig ) . ' ' . $msgsize;
235 if ( $this->img
->isMultipage() ) {
236 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
241 'alt' => $this->img
->getTitle()->getPrefixedText()
245 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
246 $thumbnail->toHtml( $imgAttribs, $linkAttribs ) .
247 $anchorclose . '</div>' );
250 if ( $this->img
->isMultipage() ) {
251 $count = $this->img
->pageCount();
254 $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
255 $link = $sk->makeKnownLinkObj( $this->mTitle
, $label, 'page='. ($page-1) );
256 $thumb1 = $sk->makeThumbLinkObj( $this->mTitle
, $this->img
, $link, $label, 'none',
257 array( 'page' => $page - 1 ) );
262 if ( $page < $count ) {
263 $label = wfMsg( 'imgmultipagenext' );
264 $link = $sk->makeKnownLinkObj( $this->mTitle
, $label, 'page='. ($page+
1) );
265 $thumb2 = $sk->makeThumbLinkObj( $this->mTitle
, $this->img
, $link, $label, 'none',
266 array( 'page' => $page +
1 ) );
272 $select = '<form name="pageselector" action="' .
273 htmlspecialchars( $wgScript ) .
274 '" method="get" onchange="document.pageselector.submit();">' .
275 Xml
::hidden( 'title', $this->getTitle()->getPrefixedDbKey() );
276 $select .= $wgOut->parse( wfMsg( 'imgmultigotopre' ), false ) .
277 ' <select id="pageselector" name="page">';
278 for ( $i=1; $i <= $count; $i++
) {
279 $select .= Xml
::option( $wgLang->formatNum( $i ), $i,
282 $select .= '</select>' . $wgOut->parse( wfMsg( 'imgmultigotopost' ), false ) .
283 '<input type="submit" value="' .
284 htmlspecialchars( wfMsg( 'imgmultigo' ) ) . '"></form>';
286 $wgOut->addHTML( '</td><td><div class="multipageimagenavbox">' .
287 "$select<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>" );
290 #if direct link is allowed but it's not a renderable image, show an icon.
291 if ($this->img
->isSafeFile()) {
292 $icon= $this->img
->iconThumb();
294 $wgOut->addHTML( '<div class="fullImageLink" id="file"><a href="' . $full_url . '">' .
304 // Workaround for incorrect MIME type on SVGs uploaded in previous versions
305 if ($mime == 'image/svg') $mime = 'image/svg+xml';
307 $filename = wfEscapeWikiText( $this->img
->getName() );
308 $info = wfMsg( 'file-info', $sk->formatSize( $this->img
->getSize() ), $mime );
311 // Check for MIME type. Other types may have more information in the future.
312 if (substr($mime,0,9) == 'image/svg' ) {
313 $infores = wfMsg('file-svg', $width_orig, $height_orig ) . '<br />';
317 $dirmark = $wgContLang->getDirMark();
318 if (!$this->img
->isSafeFile()) {
319 $warning = wfMsg( 'mediawarning' );
320 $wgOut->addWikiText( <<<EOT
321 <div class="fullMedia">$infores
322 <span class="dangerousLink">[[Media:$filename|$filename]]</span>$dirmark
323 <span class="fileInfo"> $info</span>
326 <div class="mediaWarning">$warning</div>
330 $wgOut->addWikiText( <<<EOT
331 <div class="fullMedia">$infores
332 [[Media:$filename|$filename]]$dirmark <span class="fileInfo"> $info</span>
339 if(!$this->img
->isLocal()) {
340 $this->printSharedImageText();
343 # Image does not exist
345 $title = SpecialPage
::getTitleFor( 'Upload' );
346 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('noimage-linktext'),
347 'wpDestFile=' . urlencode( $this->img
->getName() ) );
348 $wgOut->addHTML( wfMsgWikiHtml( 'noimage', $link ) );
352 function printSharedImageText() {
353 global $wgOut, $wgUser;
355 $descUrl = $this->img
->getDescriptionUrl();
356 $descText = $this->img
->getDescriptionText();
357 $s = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml("sharedupload");
358 if ( $descUrl && !$descText) {
359 $sk = $wgUser->getSkin();
360 $link = $sk->makeExternalLink( $descUrl, wfMsg('shareduploadwiki-linktext') );
361 $s .= " " . wfMsgWikiHtml('shareduploadwiki', $link);
367 $this->mExtraDescription
= $descText;
371 function getUploadUrl() {
373 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
374 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img
->getName() ) );
378 * Print out the various links at the bottom of the image page, e.g. reupload,
379 * external editing (and instructions link) etc.
381 function uploadLinksBox() {
382 global $wgUser, $wgOut;
384 if( !$this->img
->isLocal() )
387 $sk = $wgUser->getSkin();
389 $wgOut->addHtml( '<br /><ul>' );
391 # "Upload a new version of this file" link
392 if( UploadForm
::userCanReUpload($wgUser,$this->img
->name
) ) {
393 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
394 $wgOut->addHtml( "<li><div class='plainlinks'>{$ulink}</div></li>" );
397 # External editing link
398 $elink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsgHtml( 'edit-externally' ), 'action=edit&externaledit=true&mode=file' );
399 $wgOut->addHtml( '<li>' . $elink . '<div>' . wfMsgWikiHtml( 'edit-externally-help' ) . '</div></li>' );
401 $wgOut->addHtml( '</ul>' );
404 function closeShowImage()
411 * If the page we've just displayed is in the "Image" namespace,
412 * we follow it with an upload history of the image and its usage.
414 function imageHistory()
416 global $wgUser, $wgOut, $wgUseExternalEditor;
418 $sk = $wgUser->getSkin();
420 $line = $this->img
->nextHistoryLine();
423 $list = new ImageHistoryList( $sk, $this->img
);
424 $s = $list->beginImageHistoryList() .
425 $list->imageHistoryLine( true, wfTimestamp(TS_MW
, $line->img_timestamp
),
426 $this->mTitle
->getDBkey(), $line->img_user
,
427 $line->img_user_text
, $line->img_size
, $line->img_description
,
428 $line->img_width
, $line->img_height
431 while ( $line = $this->img
->nextHistoryLine() ) {
432 $s .= $list->imageHistoryLine( false, $line->img_timestamp
,
433 $line->oi_archive_name
, $line->img_user
,
434 $line->img_user_text
, $line->img_size
, $line->img_description
,
435 $line->img_width
, $line->img_height
438 $s .= $list->endImageHistoryList();
440 $wgOut->addHTML( $s );
442 $this->img
->resetHistory(); // free db resources
444 # Exist check because we don't want to show this on pages where an image
445 # doesn't exist along with the noimage message, that would suck. -ævar
446 if( $wgUseExternalEditor && $this->img
->exists() ) {
447 $this->uploadLinksBox();
452 function imageLinks()
454 global $wgUser, $wgOut;
456 $wgOut->addHTML( Xml
::element( 'h2', array( 'id' => 'filelinks' ), wfMsg( 'imagelinks' ) ) . "\n" );
458 $dbr = wfGetDB( DB_SLAVE
);
459 $page = $dbr->tableName( 'page' );
460 $imagelinks = $dbr->tableName( 'imagelinks' );
462 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
463 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id";
464 $sql = $dbr->limitResult($sql, 500, 0);
465 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
467 if ( 0 == $dbr->numRows( $res ) ) {
468 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
471 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
473 $sk = $wgUser->getSkin();
474 while ( $s = $dbr->fetchObject( $res ) ) {
475 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
476 $link = $sk->makeKnownLinkObj( $name, "" );
477 $wgOut->addHTML( "<li>{$link}</li>\n" );
479 $wgOut->addHTML( "</ul>\n" );
484 global $wgUser, $wgOut, $wgRequest;
486 if ( !$this->img
->exists() ||
!$this->img
->isLocal() ) {
487 # Use standard article deletion
492 $confirm = $wgRequest->wasPosted();
493 $reason = $wgRequest->getVal( 'wpReason' );
494 $image = $wgRequest->getVal( 'image' );
495 $oldimage = $wgRequest->getVal( 'oldimage' );
497 # Only sysops can delete images. Previously ordinary users could delete
498 # old revisions, but this is no longer the case.
499 if ( !$wgUser->isAllowed('delete') ) {
500 $wgOut->permissionRequired( 'delete' );
503 if ( $wgUser->isBlocked() ) {
504 $wgOut->blockedPage();
507 if ( wfReadOnly() ) {
508 $wgOut->readOnlyPage();
512 # Better double-check that it hasn't been deleted yet!
513 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
514 if ( ( !is_null( $image ) )
515 && ( '' == trim( $image ) ) ) {
516 $wgOut->showFatalError( wfMsg( 'cannotdelete' ) );
520 # Deleting old images doesn't require confirmation
521 if ( !is_null( $oldimage ) ||
$confirm ) {
522 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
523 $this->doDeleteImage( $reason );
525 $wgOut->showFatalError( wfMsg( 'sessionfailure' ) );
530 if ( !is_null( $image ) ) {
531 $q = '&image=' . urlencode( $image );
532 } else if ( !is_null( $oldimage ) ) {
533 $q = '&oldimage=' . urlencode( $oldimage );
537 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
542 * Called doDeleteImage() not doDelete() so that Article::delete() doesn't
545 * @param $reason User provided reason for deletion.
547 function doDeleteImage( $reason ) {
548 global $wgOut, $wgRequest;
550 $oldimage = $wgRequest->getVal( 'oldimage' );
552 if ( !is_null( $oldimage ) ) {
553 if ( strlen( $oldimage ) < 16 ) {
554 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
557 if( strpos( $oldimage, '/' ) !== false ||
strpos( $oldimage, '\\' ) !== false ) {
558 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
561 $status = $this->doDeleteOldImage( $oldimage );
562 $deleted = $oldimage;
564 $status = $this->img
->delete( $reason );
565 if ( !$status->isGood() ) {
567 $wgOut->addWikiText( $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' ) );
570 # Image itself is now gone, and database is cleaned.
571 # Now we remove the image description page.
572 $article = new Article( $this->mTitle
);
573 $article->doDeleteArticle( $reason ); # ignore errors
574 $deleted = $this->img
->getName();
578 $wgOut->setRobotpolicy( 'noindex,nofollow' );
580 if ( !$status->ok
) {
581 // Fatal error flagged
582 $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
583 $wgOut->returnToMain( false, $this->mTitle
->getPrefixedText() );
585 // Operation completed
586 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
587 $loglink = '[[Special:Log/delete|' . wfMsg( 'deletionlog' ) . ']]';
588 $text = wfMsg( 'deletedtext', $deleted, $loglink );
589 $wgOut->addWikiText( $text );
590 $wgOut->returnToMain( false, $this->mTitle
->getPrefixedText() );
595 * Delete an old revision of an image,
596 * @return FileRepoStatus
598 function doDeleteOldImage( $oldimage ) {
601 $status = $this->img
->deleteOld( $oldimage, '' );
602 if( !$status->isGood() ) {
603 $wgOut->addWikiText( $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' ) );
607 $log = new LogPage( 'delete' );
608 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
614 * Revert the file to an earlier version
617 $reverter = new FileRevertForm( $this->img
);
618 $reverter->execute();
622 * Override handling of action=purge
625 if( $this->img
->exists() ) {
626 wfDebug( "ImagePage::doPurge purging " . $this->img
->getName() . "\n" );
627 $update = new HTMLCacheUpdate( $this->mTitle
, 'imagelinks' );
629 $this->img
->upgradeRow();
630 $this->img
->purgeCache();
632 wfDebug( "ImagePage::doPurge no image\n" );
638 * Display an error with a wikitext description
640 function showError( $description ) {
642 $wgOut->setPageTitle( wfMsg( "internalerror" ) );
643 $wgOut->setRobotpolicy( "noindex,nofollow" );
644 $wgOut->setArticleRelated( false );
645 $wgOut->enableClientCache( false );
646 $wgOut->addWikiText( $description );
652 * Builds the image revision log shown on image pages
656 class ImageHistoryList
{
658 protected $img, $skin, $title;
660 public function __construct( $skin, $img ) {
663 $this->title
= $img->getTitle();
666 public function beginImageHistoryList() {
667 global $wgOut, $wgUser;
668 return Xml
::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) )
669 . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
670 . Xml
::openElement( 'table', array( 'class' => 'filehistory' ) ) . "\n"
672 . ( $this->img
->isLocal() && $wgUser->isAllowed( 'delete' ) ?
'<td></td>' : '' )
673 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
674 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
675 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
676 . '<th class="mw-imagepage-filesize">' . wfMsgHtml( 'filehist-filesize' ) . '</th>'
677 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
681 public function endImageHistoryList() {
685 public function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description, $width, $height ) {
686 global $wgUser, $wgLang, $wgTitle, $wgContLang;
687 $local = $this->img
->isLocal();
691 if( $local && $wgUser->isAllowed( 'delete' ) ) {
694 $q[] = 'action=delete';
695 $q[] = ( $iscur ?
'image=' . $this->title
->getPartialUrl() : 'oldimage=' . urlencode( $img ) );
697 $q[] = 'wpEditToken=' . urlencode( $wgUser->editToken( $img ) );
698 $row .= '(' . $this->skin
->makeKnownLinkObj(
700 wfMsgHtml( $iscur ?
'filehist-deleteall' : 'filehist-deleteone' ),
706 // Reversion link/current indicator
709 $row .= '(' . wfMsgHtml( 'filehist-current' ) . ')';
710 } elseif( $local && $wgUser->isLoggedIn() && $this->title
->userCan( 'edit' ) ) {
712 $q[] = 'action=revert';
713 $q[] = 'oldimage=' . urlencode( $img );
714 $q[] = 'wpEditToken=' . urlencode( $wgUser->editToken( $img ) );
715 $row .= '(' . $this->skin
->makeKnownLinkObj(
717 wfMsgHtml( 'filehist-revert' ),
723 // Date/time and image link
725 $url = $iscur ?
$this->img
->getUrl() : $this->img
->getArchiveUrl( $img );
726 $row .= Xml
::element(
728 array( 'href' => $url ),
729 $wgLang->timeAndDate( $timestamp, true )
736 $row .= $this->skin
->userLink( $user, $usertext ) . $this->skin
->userToolLinks( $user, $usertext );
738 $row .= htmlspecialchars( $usertext );
743 // FIXME: It would be nice to show the duration (sound files) or
744 // width/height/duration (video files) here, but this needs some
745 // additional media handler work
746 $row .= '<td>' . wfMsgHtml( 'widthheight', $width, $height ) . '</td>';
749 $row .= '<td class="mw-imagepage-filesize">' . $this->skin
->formatSize( $size ) . '</td>';
752 $row .= '<td>' . $this->skin
->formatComment( $description, $this->title
) . '</td>';
754 return "<tr>{$row}</tr>\n";