9 if( !defined( 'MEDIAWIKI' ) )
13 * 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;
22 * Handler for action=render
23 * Include body text only; none of the image extras
27 $wgOut->setArticleBodyOnly( true );
28 $wgOut->addSecondaryWikitext( $this->getContent() );
32 global $wgOut, $wgShowEXIF;
34 $this->img
= new Image( $this->mTitle
);
36 if( $this->mTitle
->getNamespace() == NS_IMAGE
) {
37 if ($wgShowEXIF && $this->img
->exists()) {
38 $exif = $this->img
->getExifData();
39 $showmeta = count($exif) ?
true : false;
45 if ($this->img
->exists())
46 $wgOut->addHTML($this->showTOC($showmeta));
48 $this->openShowImage();
50 # No need to display noarticletext, we use our own message, output in openShowImage()
51 if( $this->getID() ) {
54 # Just need to set the right headers
55 $wgOut->setArticleFlag( true );
56 $wgOut->setRobotpolicy( 'index,follow' );
57 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
61 # Show shared description, if needed
62 if( $this->mExtraDescription
) {
63 $fol = wfMsg( 'shareddescriptionfollows' );
65 $wgOut->addWikiText( $fol );
67 $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription
. '</div>' );
70 $this->closeShowImage();
71 $this->imageHistory();
74 global $wgStylePath, $wgStyleVersion;
75 $expand = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-expand' ) ) );
76 $collapse = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-collapse' ) ) );
77 $wgOut->addHTML( "<h2 id=\"metadata\">" . wfMsgHtml( 'metadata' ) . "</h2>\n" );
78 $wgOut->addWikiText( $this->makeMetadataTable( $exif ) );
80 "<script type=\"text/javascript\" src=\"$wgStylePath/common/metadata.js?wgStyleVersion\"></script>\n" .
81 "<script type=\"text/javascript\">attachMetadataToggle('mw_metadata', '$expand', '$collapse');</script>\n" );
93 * @param bool $metadata Whether or not to show the metadata link
96 function showTOC( $metadata ) {
98 $r = '<ul id="filetoc">
99 <li><a href="#file">' . $wgLang->getNsText( NS_IMAGE
) . '</a></li>
100 <li><a href="#filehistory">' . wfMsgHtml( 'imghistory' ) . '</a></li>
101 <li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>' .
102 ($metadata ?
'<li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>' : '') . '
108 * Make a table with metadata to be shown in the output page.
112 * @param array $exif The array containing the EXIF data
115 function makeMetadataTable( $exif ) {
116 $r = wfMsg( 'metadata-help' ) . "\n\n";
117 $r .= "{| id=mw_metadata class=mw_metadata\n";
118 $visibleFields = $this->visibleMetadataFields();
119 foreach( $exif as $k => $v ) {
120 $tag = strtolower( $k );
121 $msg = wfMsg( "exif-$tag" );
122 $class = "exif-$tag";
123 if( !in_array( $tag, $visibleFields ) ) {
124 $class .= ' collapsable';
126 $r .= "|- class=\"$class\"\n";
135 * Get a list of EXIF metadata items which should be displayed when
136 * the metadata table is collapsed.
138 * @return array of strings
141 function visibleMetadataFields() {
143 $lines = explode( "\n", wfMsgForContent( 'metadata-fields' ) );
144 foreach( $lines as $line ) {
145 if( preg_match( '/^\\*\s*(.*?)\s*$/', $line, $matches ) ) {
146 $fields[] = $matches[1];
153 * Overloading Article's getContent method.
155 * Omit noarticletext if sharedupload; text will be fetched from the
156 * shared upload server if possible.
158 function getContent() {
159 if( $this->img
&& $this->img
->fromSharedDirectory
&& 0 == $this->getID() ) {
162 return Article
::getContent();
165 function openShowImage() {
166 global $wgOut, $wgUser, $wgImageLimits, $wgRequest, $wgLang;
167 global $wgUseImageResize, $wgGenerateThumbnailOnParse;
169 $full_url = $this->img
->getURL();
173 if( $wgUser->getOption( 'imagesize' ) == '' ) {
174 $sizeSel = User
::getDefaultOption( 'imagesize' );
176 $sizeSel = intval( $wgUser->getOption( 'imagesize' ) );
178 if( !isset( $wgImageLimits[$sizeSel] ) ) {
179 $sizeSel = User
::getDefaultOption( 'imagesize' );
181 $max = $wgImageLimits[$sizeSel];
183 $maxHeight = $max[1];
185 if ( $this->img
->exists() ) {
187 $page = $wgRequest->getIntOrNull( 'page' );
188 if ( ! is_null( $page ) ) {
189 $this->img
->selectPage( $page );
193 $width = $this->img
->getWidth();
194 $height = $this->img
->getHeight();
197 if ( $this->img
->allowInlineDisplay() and $width and $height) {
200 # "Download high res version" link below the image
201 $msg = wfMsgHtml('showbigimage', $width, $height, intval( $this->img
->getSize()/1024 ) );
203 # We'll show a thumbnail of this image
204 if ( $width > $maxWidth ||
$height > $maxHeight ) {
205 # Calculate the thumbnail size.
206 # First case, the limiting factor is the width, not the height.
207 if ( $width / $height >= $maxWidth / $maxHeight ) {
208 $height = round( $height * $maxWidth / $width);
210 # Note that $height <= $maxHeight now.
212 $newwidth = floor( $width * $maxHeight / $height);
213 $height = round( $height * $newwidth / $width );
215 # Note that $height <= $maxHeight now, but might not be identical
216 # because of rounding.
219 if( $wgUseImageResize ) {
220 $thumbnail = $this->img
->getThumbnail( $width, -1, $wgGenerateThumbnailOnParse );
221 if ( $thumbnail == null ) {
222 $url = $this->img
->getViewURL();
224 $url = $thumbnail->getURL();
227 # No resize ability? Show the full image, but scale
228 # it down in the browser so it fits on the page.
229 $url = $this->img
->getViewURL();
231 $anchoropen = "<a href=\"{$full_url}\">";
232 $anchorclose = "</a><br />";
233 if( $this->img
->mustRender() ) {
236 $anchorclose .= "\n$anchoropen{$msg}</a>";
239 $url = $this->img
->getViewURL();
243 if ( $this->img
->isMultipage() ) {
244 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
247 $wgOut->addHTML( '<div class="fullImageLink" id="file">' . $anchoropen .
248 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
249 htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>' );
251 if ( $this->img
->isMultipage() ) {
252 $count = $this->img
->pageCount();
255 $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
256 $link = Linker
::makeLinkObj( $this->mTitle
, $label, 'page='. ($page-1) );
257 $this->img
->selectPage( $page - 1 );
258 $thumb1 = Linker
::makeThumbLinkObj( $this->img
, $link, $label, 'none' );
263 if ( $page < $count ) {
264 $label = wfMsg( 'imgmultipagenext' );
265 $this->img
->selectPage( $page +
1 );
266 $link = Linker
::makeLinkObj( $this->mTitle
, $label, 'page='. ($page+
1) );
267 $thumb2 = Linker
::makeThumbLinkObj( $this->img
, $link, $label, 'none' );
272 $select = '<form name="pageselector" action="' . $this->img
->getEscapeLocalUrl( '' ) . '" method="GET" onchange="document.pageselector.submit();">' ;
273 $select .= $wgOut->parse( wfMsg( 'imgmultigotopre' ), false ) .
274 ' <select id="pageselector" name="page">';
275 for ( $i=1; $i <= $count; $i++
) {
276 $select .= Xml
::option( $wgLang->formatNum( $i ), $i,
279 $select .= '</select>' . $wgOut->parse( wfMsg( 'imgmultigotopost' ), false ) .
280 '<input type="submit" value="' .
281 htmlspecialchars( wfMsg( 'imgmultigo' ) ) . '"></form>';
283 $wgOut->addHTML( '</td><td><div class="multipageimagenavbox">' .
284 "$select<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>" );
287 #if direct link is allowed but it's not a renderable image, show an icon.
288 if ($this->img
->isSafeFile()) {
289 $icon= $this->img
->iconThumb();
291 $wgOut->addHTML( '<div class="fullImageLink" id="file"><a href="' . $full_url . '">' .
301 $filename = wfEscapeWikiText( $this->img
->getName() );
302 $info = wfMsg( 'fileinfo',
303 ceil($this->img
->getSize()/1024.0),
304 $this->img
->getMimeType() );
307 $dirmark = $wgContLang->getDirMark();
308 if (!$this->img
->isSafeFile()) {
309 $warning = wfMsg( 'mediawarning' );
310 $wgOut->addWikiText( <<<END
311 <div class="fullMedia">
312 <span class="dangerousLink">[[Media:$filename|$filename]]</span>$dirmark
313 <span class="fileInfo"> ($info)</span>
316 <div class="mediaWarning">$warning</div>
320 $wgOut->addWikiText( <<<END
321 <div class="fullMedia">
322 [[Media:$filename|$filename]]$dirmark <span class="fileInfo"> ($info)</span>
329 if($this->img
->fromSharedDirectory
) {
330 $this->printSharedImageText();
333 # Image does not exist
335 $title = SpecialPage
::getTitleFor( 'Upload' );
336 $link = Linker
::makeKnownLinkObj($title, wfMsgHtml('noimage-linktext'),
337 'wpDestFile=' . urlencode( $this->img
->getName() ) );
338 $wgOut->addHTML( wfMsgWikiHtml( 'noimage', $link ) );
342 function printSharedImageText() {
343 global $wgRepositoryBaseUrl, $wgFetchCommonsDescriptions, $wgOut;
345 $url = $wgRepositoryBaseUrl . urlencode($this->mTitle
->getDBkey());
346 $sharedtext = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml("sharedupload");
347 if ($wgRepositoryBaseUrl && !$wgFetchCommonsDescriptions) {
348 $title = SpecialPage
::getTitleFor( 'Upload' );
349 $link = Linker
::makeKnownLinkObj($title, wfMsgHtml('shareduploadwiki-linktext'),
350 array( 'wpDestFile' => urlencode( $this->img
->getName() )));
351 $sharedtext .= " " . wfMsgWikiHtml('shareduploadwiki', $link);
353 $sharedtext .= "</div>";
354 $wgOut->addHTML($sharedtext);
356 if ($wgRepositoryBaseUrl && $wgFetchCommonsDescriptions) {
357 $text = Http
::get($url . '?action=render');
359 $this->mExtraDescription
= $text;
363 function getUploadUrl() {
365 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
366 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img
->getName() ) );
370 * Print out the various links at the bottom of the image page, e.g. reupload,
371 * external editing (and instructions link) etc.
373 function uploadLinksBox() {
374 global $wgUser, $wgOut;
376 if( $this->img
->fromSharedDirectory
)
379 $wgOut->addHtml( '<br /><ul>' );
381 # "Upload a new version of this file" link
382 if( $wgUser->isAllowed( 'reupload' ) ) {
383 $ulink = Linker
::makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
384 $wgOut->addHtml( "<li><div>{$ulink}</div></li>" );
387 # External editing link
388 $elink = Linker
::makeKnownLinkObj( $this->mTitle
, wfMsg( 'edit-externally' ), 'action=edit&externaledit=true&mode=file' );
389 $wgOut->addHtml( '<li>' . $elink . '<div>' . wfMsgWikiHtml( 'edit-externally-help' ) . '</div></li>' );
391 $wgOut->addHtml( '</ul>' );
394 function closeShowImage()
401 * If the page we've just displayed is in the "Image" namespace,
402 * we follow it with an upload history of the image and its usage.
404 function imageHistory() {
405 global $wgOut, $wgUseExternalEditor;
407 $line = $this->img
->nextHistoryLine();
410 $list = new ImageHistoryList();
411 $s = $list->beginImageHistoryList() .
412 $list->imageHistoryLine( true, wfTimestamp(TS_MW
, $line->img_timestamp
),
413 $this->mTitle
->getDBkey(), $line->img_user
,
414 $line->img_user_text
, $line->img_size
, $line->img_description
,
415 $line->img_width
, $line->img_height
418 while ( $line = $this->img
->nextHistoryLine() ) {
419 $s .= $list->imageHistoryLine( false, $line->img_timestamp
,
420 $line->oi_archive_name
, $line->img_user
,
421 $line->img_user_text
, $line->img_size
, $line->img_description
,
422 $line->img_width
, $line->img_height
425 $s .= $list->endImageHistoryList();
427 $wgOut->addHTML( $s );
429 # Exist check because we don't want to show this on pages where an image
430 # doesn't exist along with the noimage message, that would suck. -ævar
431 if( $wgUseExternalEditor && $this->img
->exists() ) {
432 $this->uploadLinksBox();
437 function imageLinks() {
440 $wgOut->addHTML( '<h2 id="filelinks">' . wfMsg( 'imagelinks' ) . "</h2>\n" );
442 $dbr =& wfGetDB( DB_SLAVE
);
443 $page = $dbr->tableName( 'page' );
444 $imagelinks = $dbr->tableName( 'imagelinks' );
446 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
447 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id";
448 $sql = $dbr->limitResult($sql, 500, 0);
449 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
451 if ( 0 == $dbr->numRows( $res ) ) {
452 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
455 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
457 while ( $s = $dbr->fetchObject( $res ) ) {
458 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
459 $link = Linker
::makeKnownLinkObj( $name, "" );
460 $wgOut->addHTML( "<li>{$link}</li>\n" );
462 $wgOut->addHTML( "</ul>\n" );
467 global $wgUser, $wgOut, $wgRequest;
469 $confirm = $wgRequest->wasPosted();
470 $reason = $wgRequest->getVal( 'wpReason' );
471 $image = $wgRequest->getVal( 'image' );
472 $oldimage = $wgRequest->getVal( 'oldimage' );
474 # Only sysops can delete images. Previously ordinary users could delete
475 # old revisions, but this is no longer the case.
476 if ( !$wgUser->isAllowed('delete') ) {
477 $wgOut->permissionRequired( 'delete' );
480 if ( $wgUser->isBlocked() ) {
481 return $this->blockedIPpage();
483 if ( wfReadOnly() ) {
484 $wgOut->readOnlyPage();
488 # Better double-check that it hasn't been deleted yet!
489 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
490 if ( ( !is_null( $image ) )
491 && ( '' == trim( $image ) ) ) {
492 $wgOut->showFatalError( wfMsg( 'cannotdelete' ) );
496 $this->img
= new Image( $this->mTitle
);
498 # Deleting old images doesn't require confirmation
499 if ( !is_null( $oldimage ) ||
$confirm ) {
500 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
501 $this->doDelete( $reason );
503 $wgOut->showFatalError( wfMsg( 'sessionfailure' ) );
508 if ( !is_null( $image ) ) {
509 $q = '&image=' . urlencode( $image );
510 } else if ( !is_null( $oldimage ) ) {
511 $q = '&oldimage=' . urlencode( $oldimage );
515 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
520 * @param $reason User provided reason for deletion.
522 function doDelete( $reason ) {
523 global $wgOut, $wgRequest, $wgUseSquid;
524 global $wgPostCommitUpdateList;
526 $fname = 'ImagePage::doDelete';
528 $oldimage = $wgRequest->getVal( 'oldimage' );
530 $dbw =& wfGetDB( DB_MASTER
);
532 if ( !is_null( $oldimage ) ) {
533 if ( strlen( $oldimage ) < 16 ) {
534 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
537 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
538 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
541 if ( !$this->doDeleteOldImage( $oldimage ) ) {
544 $deleted = $oldimage;
546 $ok = $this->img
->delete( $reason );
548 # If the deletion operation actually failed, bug out:
549 $wgOut->showFileDeleteError( $this->img
->getName() );
553 # Image itself is now gone, and database is cleaned.
554 # Now we remove the image description page.
556 $article = new Article( $this->mTitle
);
557 $article->doDeleteArticle( $reason ); # ignore errors
559 $deleted = $this->img
->getName();
562 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
563 $wgOut->setRobotpolicy( 'noindex,nofollow' );
565 $loglink = '[[Special:Log/delete|' . wfMsg( 'deletionlog' ) . ']]';
566 $text = wfMsg( 'deletedtext', $deleted, $loglink );
568 $wgOut->addWikiText( $text );
570 $wgOut->returnToMain( false, $this->mTitle
->getPrefixedText() );
576 function doDeleteOldImage( $oldimage )
580 $ok = $this->img
->deleteOld( $oldimage, '' );
582 # If we actually have a file and can't delete it, throw an error.
583 # Something went awry...
584 $wgOut->showFileDeleteError( "$oldimage" );
587 $log = new LogPage( 'delete' );
588 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
594 global $wgOut, $wgRequest, $wgUser;
596 $oldimage = $wgRequest->getText( 'oldimage' );
597 if ( strlen( $oldimage ) < 16 ) {
598 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
601 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
602 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
606 if ( wfReadOnly() ) {
607 $wgOut->readOnlyPage();
610 if( $wgUser->isAnon() ) {
611 $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' );
614 if ( ! $this->mTitle
->userCanEdit() ) {
615 $wgOut->readOnlyPage( $this->getContent(), true );
618 if ( $wgUser->isBlocked() ) {
619 return $this->blockedIPpage();
621 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
622 $wgOut->showErrorPage( 'internalerror', 'sessionfailure' );
625 $name = substr( $oldimage, 15 );
627 $dest = wfImageDir( $name );
628 $archive = wfImageArchiveDir( $name );
629 $curfile = "{$dest}/{$name}";
631 if ( !is_dir( $dest ) ) wfMkdirParents( $dest );
632 if ( !is_dir( $archive ) ) wfMkdirParents( $archive );
634 if ( ! is_file( $curfile ) ) {
635 $wgOut->showFileNotFoundError( htmlspecialchars( $curfile ) );
638 $oldver = wfTimestampNow() . "!{$name}";
640 $dbr =& wfGetDB( DB_SLAVE
);
641 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
643 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
644 $wgOut->showFileRenameError( $curfile, "${archive}/{$oldver}" );
647 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
648 $wgOut->showFileCopyError( "${archive}/{$oldimage}", $curfile );
652 # Record upload and update metadata cache
653 $img = Image
::newFromName( $name );
654 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
656 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
657 $wgOut->setRobotpolicy( 'noindex,nofollow' );
658 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
660 $descTitle = $img->getTitle();
661 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
664 function blockedIPpage() {
665 $edit = new EditPage( $this );
666 return $edit->blockedIPpage();
670 * Override handling of action=purge
673 $this->img
= new Image( $this->mTitle
);
674 if( $this->img
->exists() ) {
675 wfDebug( "ImagePage::doPurge purging " . $this->img
->getName() . "\n" );
676 $update = new HTMLCacheUpdate( $this->mTitle
, 'imagelinks' );
678 $this->img
->purgeCache();
680 wfDebug( "ImagePage::doPurge no image\n" );
691 class ImageHistoryList
{
692 function ImageHistoryList() {
695 function beginImageHistoryList() {
696 $s = "\n<h2 id=\"filehistory\">" . wfMsg( 'imghistory' ) . "</h2>\n" .
697 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
701 function endImageHistoryList() {
706 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description, $width, $height ) {
707 global $wgUser, $wgLang, $wgTitle, $wgContLang;
709 $datetime = $wgLang->timeanddate( $timestamp, true );
710 $del = wfMsg( 'deleteimg' );
711 $delall = wfMsg( 'deleteimgcompletely' );
712 $cur = wfMsg( 'cur' );
715 $url = Image
::imageUrl( $img );
717 if ( $wgUser->isAllowed('delete') ) {
718 $dlink = Linker
::makeKnownLinkObj( $wgTitle, $delall,
719 'image=' . $wgTitle->getPartialURL() . '&action=delete');
724 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
725 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
726 $token = urlencode( $wgUser->editToken( $img ) );
727 $rlink = Linker
::makeKnownLinkObj( $wgTitle,
728 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
729 urlencode( $img ) . "&wpEditToken=$token" );
730 $dlink = Linker
::makeKnownLinkObj( $wgTitle,
731 $del, 'action=delete&oldimage=' . urlencode( $img ) .
732 "&wpEditToken=$token" );
734 # Having live active links for non-logged in users
735 # means that bots and spiders crawling our site can
736 # inadvertently change content. Baaaad idea.
737 $rlink = wfMsg( 'revertimg' );
742 $userlink = Linker
::userLink( $user, $usertext ) . Linker
::userToolLinks( $user, $usertext );
743 $nbytes = wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
744 $wgLang->formatNum( $size ) );
745 $widthheight = wfMsg( 'widthheight', $width, $height );
746 $style = Linker
::getInternalLinkAttributes( $url, $datetime );
748 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a> . . {$userlink} . . {$widthheight} ({$nbytes})";
750 $s .= Linker
::commentBlock( $description, $wgTitle );