9 if( !defined( 'MEDIAWIKI' ) )
12 require_once( 'Image.php' );
15 * Special handling for image description pages
18 class ImagePage
extends Article
{
20 /* private */ var $img; // Image object this page is shown for
21 var $mExtraDescription = false;
24 * Handler for action=render
25 * Include body text only; none of the image extras
29 $wgOut->setArticleBodyOnly( true );
30 $wgOut->addSecondaryWikitext( $this->getContent() );
34 global $wgOut, $wgShowEXIF;
36 $this->img
= new Image( $this->mTitle
);
38 if( $this->mTitle
->getNamespace() == NS_IMAGE
) {
39 if ($wgShowEXIF && $this->img
->exists()) {
40 $exif = $this->img
->getExifData();
41 $showmeta = count($exif) ?
true : false;
47 if ($this->img
->exists())
48 $wgOut->addHTML($this->showTOC($showmeta));
50 $this->openShowImage();
52 # No need to display noarticletext, we use our own message, output in openShowImage()
53 if( $this->getID() ) {
56 # Just need to set the right headers
57 $wgOut->setArticleFlag( true );
58 $wgOut->setRobotpolicy( 'index,follow' );
59 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
63 # Show shared description, if needed
64 if( $this->mExtraDescription
) {
65 $fol = wfMsg( 'shareddescriptionfollows' );
67 $wgOut->addWikiText( $fol );
69 $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription
. '</div>' );
72 $this->closeShowImage();
73 $this->imageHistory();
77 $expand = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-expand' ) ) );
78 $collapse = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-collapse' ) ) );
79 $wgOut->addHTML( "<h2 id=\"metadata\">" . wfMsgHtml( 'metadata' ) . "</h2>\n" );
80 $wgOut->addWikiText( $this->makeMetadataTable( $exif ) );
82 "<script type=\"text/javascript\" src=\"$wgStylePath/common/metadata.js\"></script>\n" .
83 "<script type=\"text/javascript\">attachMetadataToggle('mw_metadata', '$expand', '$collapse');</script>\n" );
95 * @param bool $metadata Whether or not to show the metadata link
98 function showTOC( $metadata ) {
100 $r = '<ul id="filetoc">
101 <li><a href="#file">' . $wgLang->getNsText( NS_IMAGE
) . '</a></li>
102 <li><a href="#filehistory">' . wfMsgHtml( 'imghistory' ) . '</a></li>
103 <li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>' .
104 ($metadata ?
'<li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>' : '') . '
110 * Make a table with metadata to be shown in the output page.
114 * @param array $exif The array containing the EXIF data
117 function makeMetadataTable( $exif ) {
118 $r = wfMsg( 'metadata-help' ) . "\n\n";
119 $r .= "{| id=mw_metadata class=mw_metadata\n";
120 $visibleFields = $this->visibleMetadataFields();
121 foreach( $exif as $k => $v ) {
122 $tag = strtolower( $k );
123 $msg = wfMsg( "exif-$tag" );
124 $class = "exif-$tag";
125 if( !in_array( $tag, $visibleFields ) ) {
126 $class .= ' collapsable';
128 $r .= "|- class=\"$class\"\n";
137 * Get a list of EXIF metadata items which should be displayed when
138 * the metadata table is collapsed.
140 * @return array of strings
143 function visibleMetadataFields() {
145 $lines = explode( "\n", wfMsgForContent( 'metadata-fields' ) );
146 foreach( $lines as $line ) {
147 if( preg_match( '/^\\*\s*(.*?)\s*$/', $line, $matches ) ) {
148 $fields[] = $matches[1];
155 * Overloading Article's getContent method.
157 * Omit noarticletext if sharedupload; text will be fetched from the
158 * shared upload server if possible.
160 function getContent() {
161 if( $this->img
&& $this->img
->fromSharedDirectory
&& 0 == $this->getID() ) {
164 return Article
::getContent();
167 function openShowImage() {
168 global $wgOut, $wgUser, $wgImageLimits, $wgRequest;
169 global $wgUseImageResize, $wgGenerateThumbnailOnParse;
171 $full_url = $this->img
->getURL();
175 if( $wgUser->getOption( 'imagesize' ) == '' ) {
176 $sizeSel = User
::getDefaultOption( 'imagesize' );
178 $sizeSel = intval( $wgUser->getOption( 'imagesize' ) );
180 if( !isset( $wgImageLimits[$sizeSel] ) ) {
181 $sizeSel = User
::getDefaultOption( 'imagesize' );
183 $max = $wgImageLimits[$sizeSel];
185 $maxHeight = $max[1];
186 $sk = $wgUser->getSkin();
188 if ( $this->img
->exists() ) {
190 $width = $this->img
->getWidth();
191 $height = $this->img
->getHeight();
194 if ( $this->img
->allowInlineDisplay() and $width and $height) {
197 # "Download high res version" link below the image
198 $msg = wfMsgHtml('showbigimage', $width, $height, intval( $this->img
->getSize()/1024 ) );
200 # We'll show a thumbnail of this image
201 if ( $width > $maxWidth ||
$height > $maxHeight ) {
202 # Calculate the thumbnail size.
203 # First case, the limiting factor is the width, not the height.
204 if ( $width / $height >= $maxWidth / $maxHeight ) {
205 $height = round( $height * $maxWidth / $width);
207 # Note that $height <= $maxHeight now.
209 $newwidth = floor( $width * $maxHeight / $height);
210 $height = round( $height * $newwidth / $width );
212 # Note that $height <= $maxHeight now, but might not be identical
213 # because of rounding.
216 if( $wgUseImageResize ) {
217 $thumbnail = $this->img
->getThumbnail( $width, -1, $wgGenerateThumbnailOnParse );
218 if ( $thumbnail == null ) {
219 $url = $this->img
->getViewURL();
221 $url = $thumbnail->getURL();
224 # No resize ability? Show the full image, but scale
225 # it down in the browser so it fits on the page.
226 $url = $this->img
->getViewURL();
228 $anchoropen = "<a href=\"{$full_url}\">";
229 $anchorclose = "</a><br />";
230 if( $this->img
->mustRender() ) {
233 $anchorclose .= "\n$anchoropen{$msg}</a>";
236 $url = $this->img
->getViewURL();
239 $wgOut->addHTML( '<div class="fullImageLink" id="file">' . $anchoropen .
240 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
241 htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>' );
243 #if direct link is allowed but it's not a renderable image, show an icon.
244 if ($this->img
->isSafeFile()) {
245 $icon= $this->img
->iconThumb();
247 $wgOut->addHTML( '<div class="fullImageLink" id="file"><a href="' . $full_url . '">' .
257 $filename = wfEscapeWikiText( $this->img
->getName() );
258 $info = wfMsg( 'fileinfo',
259 ceil($this->img
->getSize()/1024.0),
260 $this->img
->getMimeType() );
263 $dirmark = $wgContLang->getDirMark();
264 if (!$this->img
->isSafeFile()) {
265 $warning = wfMsg( 'mediawarning' );
266 $wgOut->addWikiText( <<<END
267 <div class="fullMedia">
268 <span class="dangerousLink">[[Media:$filename|$filename]]</span>$dirmark
269 <span class="fileInfo"> ($info)</span>
272 <div class="mediaWarning">$warning</div>
276 $wgOut->addWikiText( <<<END
277 <div class="fullMedia">
278 [[Media:$filename|$filename]]$dirmark <span class="fileInfo"> ($info)</span>
285 if($this->img
->fromSharedDirectory
) {
286 $this->printSharedImageText();
289 # Image does not exist
291 $title = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
292 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('noimage-linktext'),
293 'wpDestFile=' . urlencode( $this->img
->getName() ) );
294 $wgOut->addHTML( wfMsgWikiHtml( 'noimage', $link ) );
298 function printSharedImageText() {
299 global $wgRepositoryBaseUrl, $wgFetchCommonsDescriptions, $wgOut, $wgUser;
301 $url = $wgRepositoryBaseUrl . urlencode($this->mTitle
->getDBkey());
302 $sharedtext = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml("sharedupload");
303 if ($wgRepositoryBaseUrl && !$wgFetchCommonsDescriptions) {
305 $sk = $wgUser->getSkin();
306 $title = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
307 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('shareduploadwiki-linktext'),
308 array( 'wpDestFile' => urlencode( $this->img
->getName() )));
309 $sharedtext .= " " . wfMsgWikiHtml('shareduploadwiki', $link);
311 $sharedtext .= "</div>";
312 $wgOut->addHTML($sharedtext);
314 if ($wgRepositoryBaseUrl && $wgFetchCommonsDescriptions) {
315 require_once("HttpFunctions.php");
316 $ur = ini_set('allow_url_fopen', true);
317 $text = wfGetHTTP($url . '?action=render');
318 ini_set('allow_url_fopen', $ur);
320 $this->mExtraDescription
= $text;
324 function getUploadUrl() {
326 $uploadTitle = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
327 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img
->getName() ) );
331 * Print out the various links at the bottom of the image page, e.g. reupload,
332 * external editing (and instructions link) etc.
334 function uploadLinksBox() {
335 global $wgUser, $wgOut;
337 if( $this->img
->fromSharedDirectory
)
340 $sk = $wgUser->getSkin();
342 $wgOut->addHtml( '<br /><ul>' );
344 # "Upload a new version of this file" link
345 if( $wgUser->isAllowed( 'reupload' ) ) {
346 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
347 $wgOut->addHtml( "<li><div>{$ulink}</div></li>" );
350 # External editing link
351 $elink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsg( 'edit-externally' ), 'action=edit&externaledit=true&mode=file' );
352 $wgOut->addHtml( '<li>' . $elink . '<div>' . wfMsgWikiHtml( 'edit-externally-help' ) . '</div></li>' );
354 $wgOut->addHtml( '</ul>' );
357 function closeShowImage()
364 * If the page we've just displayed is in the "Image" namespace,
365 * we follow it with an upload history of the image and its usage.
367 function imageHistory()
369 global $wgUser, $wgOut, $wgUseExternalEditor;
371 $sk = $wgUser->getSkin();
373 $line = $this->img
->nextHistoryLine();
376 $list = new ImageHistoryList( $sk );
377 $s = $list->beginImageHistoryList() .
378 $list->imageHistoryLine( true, wfTimestamp(TS_MW
, $line->img_timestamp
),
379 $this->mTitle
->getDBkey(), $line->img_user
,
380 $line->img_user_text
, $line->img_size
, $line->img_description
,
381 $line->img_width
, $line->img_height
384 while ( $line = $this->img
->nextHistoryLine() ) {
385 $s .= $list->imageHistoryLine( false, $line->img_timestamp
,
386 $line->oi_archive_name
, $line->img_user
,
387 $line->img_user_text
, $line->img_size
, $line->img_description
,
388 $line->img_width
, $line->img_height
391 $s .= $list->endImageHistoryList();
393 $wgOut->addHTML( $s );
395 # Exist check because we don't want to show this on pages where an image
396 # doesn't exist along with the noimage message, that would suck. -ævar
397 if( $wgUseExternalEditor && $this->img
->exists() ) {
398 $this->uploadLinksBox();
403 function imageLinks()
405 global $wgUser, $wgOut;
407 $wgOut->addHTML( '<h2 id="filelinks">' . wfMsg( 'imagelinks' ) . "</h2>\n" );
409 $dbr =& wfGetDB( DB_SLAVE
);
410 $page = $dbr->tableName( 'page' );
411 $imagelinks = $dbr->tableName( 'imagelinks' );
413 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
414 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id";
415 $sql = $dbr->limitResult($sql, 500, 0);
416 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
418 if ( 0 == $dbr->numRows( $res ) ) {
419 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
422 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
424 $sk = $wgUser->getSkin();
425 while ( $s = $dbr->fetchObject( $res ) ) {
426 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
427 $link = $sk->makeKnownLinkObj( $name, "" );
428 $wgOut->addHTML( "<li>{$link}</li>\n" );
430 $wgOut->addHTML( "</ul>\n" );
435 global $wgUser, $wgOut, $wgRequest;
437 $confirm = $wgRequest->wasPosted();
438 $reason = $wgRequest->getVal( 'wpReason' );
439 $image = $wgRequest->getVal( 'image' );
440 $oldimage = $wgRequest->getVal( 'oldimage' );
442 # Only sysops can delete images. Previously ordinary users could delete
443 # old revisions, but this is no longer the case.
444 if ( !$wgUser->isAllowed('delete') ) {
445 $wgOut->sysopRequired();
448 if ( $wgUser->isBlocked() ) {
449 return $this->blockedIPpage();
451 if ( wfReadOnly() ) {
452 $wgOut->readOnlyPage();
456 # Better double-check that it hasn't been deleted yet!
457 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
458 if ( ( !is_null( $image ) )
459 && ( '' == trim( $image ) ) ) {
460 $wgOut->showFatalError( wfMsg( 'cannotdelete' ) );
464 $this->img
= new Image( $this->mTitle
);
466 # Deleting old images doesn't require confirmation
467 if ( !is_null( $oldimage ) ||
$confirm ) {
468 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
469 $this->doDelete( $reason );
471 $wgOut->showFatalError( wfMsg( 'sessionfailure' ) );
476 if ( !is_null( $image ) ) {
477 $q = '&image=' . urlencode( $image );
478 } else if ( !is_null( $oldimage ) ) {
479 $q = '&oldimage=' . urlencode( $oldimage );
483 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
488 * @param $reason User provided reason for deletion.
490 function doDelete( $reason ) {
491 global $wgOut, $wgRequest, $wgUseSquid;
492 global $wgPostCommitUpdateList;
494 $fname = 'ImagePage::doDelete';
496 $oldimage = $wgRequest->getVal( 'oldimage' );
498 $dbw =& wfGetDB( DB_MASTER
);
500 if ( !is_null( $oldimage ) ) {
501 if ( strlen( $oldimage ) < 16 ) {
502 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
505 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
506 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
509 if ( !$this->doDeleteOldImage( $oldimage ) ) {
512 $deleted = $oldimage;
514 $ok = $this->img
->delete( $reason );
516 # If the deletion operation actually failed, bug out:
517 $wgOut->showFileDeleteError( $this->img
->getName() );
521 # Image itself is now gone, and database is cleaned.
522 # Now we remove the image description page.
524 $article = new Article( $this->mTitle
);
525 $article->doDeleteArticle( $reason ); # ignore errors
527 $deleted = $this->img
->getName();
530 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
531 $wgOut->setRobotpolicy( 'noindex,nofollow' );
533 $loglink = '[[Special:Log/delete|' . wfMsg( 'deletionlog' ) . ']]';
534 $text = wfMsg( 'deletedtext', $deleted, $loglink );
536 $wgOut->addWikiText( $text );
538 $wgOut->returnToMain( false, $this->mTitle
->getPrefixedText() );
544 function doDeleteOldImage( $oldimage )
548 $ok = $this->img
->deleteOld( $oldimage, '' );
550 # If we actually have a file and can't delete it, throw an error.
551 # Something went awry...
552 $wgOut->showFileDeleteError( "$oldimage" );
555 $log = new LogPage( 'delete' );
556 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
562 global $wgOut, $wgRequest, $wgUser;
564 $oldimage = $wgRequest->getText( 'oldimage' );
565 if ( strlen( $oldimage ) < 16 ) {
566 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
569 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
570 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
574 if ( wfReadOnly() ) {
575 $wgOut->readOnlyPage();
578 if( $wgUser->isAnon() ) {
579 $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' );
582 if ( ! $this->mTitle
->userCanEdit() ) {
583 $wgOut->sysopRequired();
586 if ( $wgUser->isBlocked() ) {
587 return $this->blockedIPpage();
589 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
590 $wgOut->showErrorPage( 'internalerror', 'sessionfailure' );
593 $name = substr( $oldimage, 15 );
595 $dest = wfImageDir( $name );
596 $archive = wfImageArchiveDir( $name );
597 $curfile = "{$dest}/{$name}";
599 if ( !is_dir( $dest ) ) wfMkdirParents( $dest );
600 if ( !is_dir( $archive ) ) wfMkdirParents( $archive );
602 if ( ! is_file( $curfile ) ) {
603 $wgOut->showFileNotFoundError( htmlspecialchars( $curfile ) );
606 $oldver = wfTimestampNow() . "!{$name}";
608 $dbr =& wfGetDB( DB_SLAVE
);
609 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
611 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
612 $wgOut->showFileRenameError( $curfile, "${archive}/{$oldver}" );
615 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
616 $wgOut->showFileCopyError( "${archive}/{$oldimage}", $curfile );
620 # Record upload and update metadata cache
621 $img = Image
::newFromName( $name );
622 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
624 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
625 $wgOut->setRobotpolicy( 'noindex,nofollow' );
626 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
628 $descTitle = $img->getTitle();
629 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
632 function blockedIPpage() {
633 $edit = new EditPage( $this );
634 return $edit->blockedIPpage();
638 * Override handling of action=purge
641 $this->img
= new Image( $this->mTitle
);
642 if( $this->img
->exists() ) {
643 wfDebug( "ImagePage::doPurge purging " . $this->img
->getName() . "\n" );
644 $update = new HTMLCacheUpdate( $this->mTitle
, 'imagelinks' );
646 $this->img
->purgeCache();
648 wfDebug( "ImagePage::doPurge no image\n" );
659 class ImageHistoryList
{
660 function ImageHistoryList( &$skin ) {
661 $this->skin
=& $skin;
664 function beginImageHistoryList() {
665 $s = "\n<h2 id=\"filehistory\">" . wfMsg( 'imghistory' ) . "</h2>\n" .
666 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
670 function endImageHistoryList() {
675 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description, $width, $height ) {
676 global $wgUser, $wgLang, $wgTitle, $wgContLang;
678 $datetime = $wgLang->timeanddate( $timestamp, true );
679 $del = wfMsg( 'deleteimg' );
680 $delall = wfMsg( 'deleteimgcompletely' );
681 $cur = wfMsg( 'cur' );
684 $url = Image
::imageUrl( $img );
686 if ( $wgUser->isAllowed('delete') ) {
687 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
689 $style = $this->skin
->getInternalLinkAttributes( $link, $delall );
691 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
696 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
697 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
698 $token = urlencode( $wgUser->editToken( $img ) );
699 $rlink = $this->skin
->makeKnownLinkObj( $wgTitle,
700 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
701 urlencode( $img ) . "&wpEditToken=$token" );
702 $dlink = $this->skin
->makeKnownLinkObj( $wgTitle,
703 $del, 'action=delete&oldimage=' . urlencode( $img ) .
704 "&wpEditToken=$token" );
706 # Having live active links for non-logged in users
707 # means that bots and spiders crawling our site can
708 # inadvertently change content. Baaaad idea.
709 $rlink = wfMsg( 'revertimg' );
714 $userlink = $this->skin
->userLink( $user, $usertext ) . $this->skin
->userToolLinks( $user, $usertext );
715 $nbytes = wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
716 $wgLang->formatNum( $size ) );
717 $widthheight = wfMsg( 'widthheight', $width, $height );
718 $style = $this->skin
->getInternalLinkAttributes( $url, $datetime );
720 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a> . . {$userlink} . . {$widthheight} ({$nbytes})";
722 $s .= $this->skin
->commentBlock( $description, $wgTitle );