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;
25 $wgOut->setArticleBodyOnly(true);
26 $wgOut->addWikitext($this->getContent(true));
30 global $wgUseExternalEditor, $wgOut, $wgShowEXIF;
32 $this->img
= new Image( $this->mTitle
);
34 if( $this->mTitle
->getNamespace() == NS_IMAGE
) {
35 if ($wgShowEXIF && $this->img
->exists()) {
36 $exif = $this->img
->getExifData();
37 $showmeta = count($exif) ?
true : false;
43 if ($this->img
->exists())
44 $wgOut->addHTML($this->showTOC($showmeta));
46 $this->openShowImage();
48 # No need to display noarticletext, we use our own message, output in openShowImage()
49 if( $this->getID() ) {
52 # Just need to set the right headers
53 $wgOut->setArticleFlag( true );
54 $wgOut->setRobotpolicy( 'index,follow' );
55 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
56 $wgOut->addMetaTags();
60 if ($this->mExtraDescription
) {
61 $fol = wfMsg('shareddescriptionfollows');
63 $wgOut->addWikiText(wfMsg('shareddescriptionfollows'));
64 $wgOut->addHTML($this->mExtraDescription
);
67 $this->closeShowImage();
68 $this->imageHistory();
72 $expand = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-expand' ) ) );
73 $collapse = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-collapse' ) ) );
74 $wgOut->addHTML( "<h2 id=\"metadata\">" . wfMsgHtml( 'metadata' ) . "</h2>\n" );
75 $wgOut->addWikiText( $this->makeMetadataTable( $exif ) );
77 "<script type=\"text/javascript\" src=\"$wgStylePath/common/metadata.js\"></script>\n" .
78 "<script type=\"text/javascript\">attachMetadataToggle('mw_metadata', '$expand', '$collapse');</script>\n" );
90 * @param bool $metadata Whether or not to show the metadata link
93 function showTOC( $metadata ) {
95 $r = '<ul id="filetoc">
96 <li><a href="#file">' . $wgLang->getNsText( NS_IMAGE
) . '</a></li>
97 <li><a href="#filehistory">' . wfMsgHtml( 'imghistory' ) . '</a></li>
98 <li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>' .
99 ($metadata ?
'<li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>' : '') . '
105 * Make a table with metadata to be shown in the output page.
109 * @param array $exif The array containing the EXIF data
112 function makeMetadataTable( $exif ) {
113 $r = wfMsg( 'metadata-help' ) . "\n\n";
114 $r .= "{| id=mw_metadata class=mw_metadata\n";
115 $visibleFields = $this->visibleMetadataFields();
116 foreach( $exif as $k => $v ) {
117 $tag = strtolower( $k );
118 $msg = wfMsg( "exif-$tag" );
119 $class = "exif-$tag";
120 if( !in_array( $tag, $visibleFields ) ) {
121 $class .= ' collapsable';
123 $r .= "|- class=\"$class\"\n";
132 * Get a list of EXIF metadata items which should be displayed when
133 * the metadata table is collapsed.
135 * @return array of strings
138 function visibleMetadataFields() {
140 $lines = explode( "\n", wfMsgForContent( 'metadata-fields' ) );
141 foreach( $lines as $line ) {
142 if( preg_match( '/^\\*\s*(.*?)\s*$/', $line, $matches ) ) {
143 $fields[] = $matches[1];
150 * Overloading Article's getContent method.
151 * Omit noarticletext if sharedupload
153 * @param $noredir If true, do not follow redirects
155 function getContent( $noredir )
157 if ( $this->img
&& $this->img
->fromSharedDirectory
&& 0 == $this->getID() ) {
160 return Article
::getContent( $noredir );
163 function openShowImage()
165 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
166 $wgUseImageResize, $wgRepositoryBaseUrl,
167 $wgUseExternalEditor, $wgServer, $wgFetchCommonsDescriptions;
168 $full_url = $this->img
->getURL();
172 if( $wgUser->getOption( 'imagesize' ) == '' ) {
173 $sizeSel = User
::getDefaultOption( 'imagesize' );
175 $sizeSel = intval( $wgUser->getOption( 'imagesize' ) );
177 if( !isset( $wgImageLimits[$sizeSel] ) ) {
178 $sizeSel = User
::getDefaultOption( 'imagesize' );
180 $max = $wgImageLimits[$sizeSel];
182 $maxHeight = $max[1];
183 $sk = $wgUser->getSkin();
185 if ( $this->img
->exists() ) {
187 $width = $this->img
->getWidth();
188 $height = $this->img
->getHeight();
191 if ( $this->img
->allowInlineDisplay() and $width and $height) {
194 # "Download high res version" link below the image
195 $msg = wfMsgHtml('showbigimage', $width, $height, intval( $this->img
->getSize()/1024 ) );
197 # We'll show a thumbnail of this image
198 if ( $width > $maxWidth ||
$height > $maxHeight ) {
199 # Calculate the thumbnail size.
200 # First case, the limiting factor is the width, not the height.
201 if ( $width / $height >= $maxWidth / $maxHeight ) {
202 $height = round( $height * $maxWidth / $width);
204 # Note that $height <= $maxHeight now.
206 $newwidth = floor( $width * $maxHeight / $height);
207 $height = round( $height * $newwidth / $width );
209 # Note that $height <= $maxHeight now, but might not be identical
210 # because of rounding.
213 if( $wgUseImageResize ) {
214 $thumbnail = $this->img
->getThumbnail( $width );
215 if ( $thumbnail == null ) {
216 $url = $this->img
->getViewURL();
218 $url = $thumbnail->getURL();
221 # No resize ability? Show the full image, but scale
222 # it down in the browser so it fits on the page.
223 $url = $this->img
->getViewURL();
225 $anchoropen = "<a href=\"{$full_url}\">";
226 $anchorclose = "</a><br />";
227 if( $this->img
->mustRender() ) {
230 $anchorclose .= "\n$anchoropen{$msg}</a>";
233 $url = $this->img
->getViewURL();
236 $wgOut->addHTML( '<div class="fullImageLink" id="file">' . $anchoropen .
237 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
238 htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>' );
240 #if direct link is allowed but it's not a renderable image, show an icon.
241 if ($this->img
->isSafeFile()) {
242 $icon= $this->img
->iconThumb();
244 $wgOut->addHTML( '<div class="fullImageLink" id="file"><a href="' . $full_url . '">' .
254 $filename = wfEscapeWikiText( $this->img
->getName() );
255 $info = wfMsg( 'fileinfo',
256 ceil($this->img
->getSize()/1024.0),
257 $this->img
->getMimeType() );
259 if (!$this->img
->isSafeFile()) {
260 $warning = wfMsg( 'mediawarning' );
261 $wgOut->addWikiText( <<<END
262 <div class="fullMedia">
263 <span class="dangerousLink">[[Media:$filename|$filename]]</span>
264 <span class="fileInfo"> ($info)</span>
267 <div class="mediaWarning">$warning</div>
271 $wgOut->addWikiText( <<<END
272 <div class="fullMedia">
273 [[Media:$filename|$filename]] <span class="fileInfo"> ($info)</span>
280 if($this->img
->fromSharedDirectory
) {
281 $this->printSharedImageText();
284 # Image does not exist
286 $title = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
287 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('noimage-linktext'),
288 'wpDestFile=' . urlencode( $this->img
->getName() ) );
289 $wgOut->addHTML( wfMsgWikiHtml( 'noimage', $link ) );
293 function printSharedImageText() {
294 global $wgRepositoryBaseUrl, $wgFetchCommonsDescriptions, $wgOut, $wgUser;
296 $url = $wgRepositoryBaseUrl . urlencode($this->mTitle
->getDBkey());
297 $sharedtext = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml("sharedupload");
298 if ($wgRepositoryBaseUrl && !$wgFetchCommonsDescriptions) {
300 $sk = $wgUser->getSkin();
301 $title = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
302 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('shareduploadwiki-linktext'),
303 array( 'wpDestFile' => urlencode( $this->img
->getName() )));
304 $sharedtext .= " " . wfMsgWikiHtml('shareduploadwiki', $link);
306 $sharedtext .= "</div>";
307 $wgOut->addHTML($sharedtext);
309 if ($wgRepositoryBaseUrl && $wgFetchCommonsDescriptions) {
310 require_once("HttpFunctions.php");
311 $ur = ini_set('allow_url_fopen', true);
312 $text = wfGetHTTP($url . '?action=render');
313 ini_set('allow_url_fopen', $ur);
315 $this->mExtraDescription
= $text;
319 function getUploadUrl() {
321 $uploadTitle = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
322 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img
->getName() ) );
326 function uploadLinksBox()
328 global $wgUser, $wgOut;
330 if ($this->img
->fromSharedDirectory
)
333 $sk = $wgUser->getSkin();
334 $wgOut->addHTML( '<br /><ul>' );
335 if( $wgUser->isAllowed( 'reupload' ) ) {
336 $wgOut->addWikiText( "<li>\n<div>". wfMsg( 'uploadnewversion', $this->getUploadUrl() ) ."</div>\n</li>\n" );
338 $wgOut->addHTML( '<li>' );
339 $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle
,
340 wfMsg( 'edit-externally' ), "action=edit&externaledit=true&mode=file" ) );
341 $wgOut->addWikiText( '<div>' . wfMsg('edit-externally-help') . '</div>' );
342 $wgOut->addHTML( '</li></ul>' );
345 function closeShowImage()
352 * If the page we've just displayed is in the "Image" namespace,
353 * we follow it with an upload history of the image and its usage.
355 function imageHistory()
357 global $wgUser, $wgOut, $wgUseExternalEditor;
359 $sk = $wgUser->getSkin();
361 $line = $this->img
->nextHistoryLine();
364 $list =& new ImageHistoryList( $sk );
365 $s = $list->beginImageHistoryList() .
366 $list->imageHistoryLine( true, wfTimestamp(TS_MW
, $line->img_timestamp
),
367 $this->mTitle
->getDBkey(), $line->img_user
,
368 $line->img_user_text
, $line->img_size
, $line->img_description
,
369 $line->img_width
, $line->img_height
372 while ( $line = $this->img
->nextHistoryLine() ) {
373 $s .= $list->imageHistoryLine( false, $line->img_timestamp
,
374 $line->oi_archive_name
, $line->img_user
,
375 $line->img_user_text
, $line->img_size
, $line->img_description
,
376 $line->img_width
, $line->img_height
379 $s .= $list->endImageHistoryList();
381 $wgOut->addHTML( $s );
383 # Exist check because we don't want to show this on pages where an image
384 # doesn't exist along with the noimage message, that would suck. -ævar
385 if( $wgUseExternalEditor && $this->img
->exists() ) {
386 $this->uploadLinksBox();
391 function imageLinks()
393 global $wgUser, $wgOut;
395 $wgOut->addHTML( '<h2 id="filelinks">' . wfMsg( 'imagelinks' ) . "</h2>\n" );
397 $dbr =& wfGetDB( DB_SLAVE
);
398 $page = $dbr->tableName( 'page' );
399 $imagelinks = $dbr->tableName( 'imagelinks' );
401 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
402 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id";
403 $sql = $dbr->limitResult($sql, 500, 0);
404 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
406 if ( 0 == $dbr->numRows( $res ) ) {
407 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
410 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
412 $sk = $wgUser->getSkin();
413 while ( $s = $dbr->fetchObject( $res ) ) {
414 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
415 $link = $sk->makeKnownLinkObj( $name, "" );
416 $wgOut->addHTML( "<li>{$link}</li>\n" );
418 $wgOut->addHTML( "</ul>\n" );
423 global $wgUser, $wgOut, $wgRequest;
425 $confirm = $wgRequest->wasPosted();
426 $image = $wgRequest->getVal( 'image' );
427 $oldimage = $wgRequest->getVal( 'oldimage' );
429 # Only sysops can delete images. Previously ordinary users could delete
430 # old revisions, but this is no longer the case.
431 if ( !$wgUser->isAllowed('delete') ) {
432 $wgOut->sysopRequired();
435 if ( $wgUser->isBlocked() ) {
436 return $this->blockedIPpage();
438 if ( wfReadOnly() ) {
439 $wgOut->readOnlyPage();
443 # Better double-check that it hasn't been deleted yet!
444 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
445 if ( ( !is_null( $image ) )
446 && ( '' == trim( $image ) ) ) {
447 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
451 $this->img
= new Image( $this->mTitle
);
453 # Deleting old images doesn't require confirmation
454 if ( !is_null( $oldimage ) ||
$confirm ) {
455 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
458 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
463 if ( !is_null( $image ) ) {
464 $q = '&image=' . urlencode( $image );
465 } else if ( !is_null( $oldimage ) ) {
466 $q = '&oldimage=' . urlencode( $oldimage );
470 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
475 global $wgOut, $wgUser, $wgContLang, $wgRequest;
476 global $wgUseSquid, $wgInternalServer, $wgPostCommitUpdateList;
477 $fname = 'ImagePage::doDelete';
479 $reason = $wgRequest->getVal( 'wpReason' );
480 $oldimage = $wgRequest->getVal( 'oldimage' );
482 $dbw =& wfGetDB( DB_MASTER
);
484 if ( !is_null( $oldimage ) ) {
485 if ( strlen( $oldimage ) < 16 ) {
486 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
489 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
490 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
494 # Invalidate description page cache
495 $this->mTitle
->invalidateCache();
500 $wgInternalServer.wfImageArchiveUrl( $oldimage ),
501 $wgInternalServer.$this->mTitle
->getFullURL()
503 wfPurgeSquidServers($urlArr);
505 $this->doDeleteOldImage( $oldimage );
506 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
507 $deleted = $oldimage;
509 $image = $this->mTitle
->getDBkey();
510 $dest = wfImageDir( $image );
511 $archive = wfImageDir( $image );
513 # Delete the image file if it exists; due to sync problems
514 # or manual trimming sometimes the file will be missing.
515 $targetFile = "{$dest}/{$image}";
516 if( file_exists( $targetFile ) && ! @unlink
( $targetFile ) ) {
517 # If the deletion operation actually failed, bug out:
518 $wgOut->fileDeleteError( $targetFile );
521 $dbw->delete( 'image', array( 'img_name' => $image ) );
522 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
524 # Purge archive URLs from the squid
526 while ( $s = $dbw->fetchObject( $res ) ) {
527 $this->doDeleteOldImage( $s->oi_archive_name
);
528 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name
);
531 # And also the HTML of all pages using this image
532 $linksTo = $this->img
->getLinksTo();
534 $u = SquidUpdate
::newFromTitles( $linksTo, $urlArr );
535 array_push( $wgPostCommitUpdateList, $u );
538 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
540 # Image itself is now gone, and database is cleaned.
541 # Now we remove the image description page.
543 $article = new Article( $this->mTitle
);
544 $article->doDeleteArticle( $reason ); # ignore errors
546 # Invalidate parser cache and client cache for pages using this image
547 # This is left until relatively late to reduce lock time
548 Title
::touchArray( $linksTo );
550 /* Delete thumbnails and refresh image metadata cache */
551 $this->img
->purgeCache();
557 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
558 $wgOut->setRobotpolicy( 'noindex,nofollow' );
560 $loglink = '[[Special:Log/delete|' . wfMsg( 'deletionlog' ) . ']]';
561 $text = wfMsg( 'deletedtext', $deleted, $loglink );
563 $wgOut->addWikiText( $text );
565 $wgOut->returnToMain( false, $this->mTitle
->getPrefixedText() );
568 function doDeleteOldImage( $oldimage )
572 $name = substr( $oldimage, 15 );
573 $archive = wfImageArchiveDir( $name );
575 # Delete the image if it exists. Sometimes the file will be missing
576 # due to manual intervention or weird sync problems; treat that
577 # condition gracefully and continue to delete the database entry.
578 # Also some records may end up with an empty oi_archive_name field
579 # if the original file was missing when a new upload was made;
580 # don't try to delete the directory then!
582 $targetFile = "{$archive}/{$oldimage}";
583 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink
( $targetFile ) ) {
584 # If we actually have a file and can't delete it, throw an error.
585 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
588 $log = new LogPage( 'delete' );
589 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
595 global $wgOut, $wgRequest, $wgUser;
596 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
598 $oldimage = $wgRequest->getText( 'oldimage' );
599 if ( strlen( $oldimage ) < 16 ) {
600 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
603 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
604 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
608 if ( wfReadOnly() ) {
609 $wgOut->readOnlyPage();
612 if( $wgUser->isAnon() ) {
613 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
616 if ( ! $this->mTitle
->userCanEdit() ) {
617 $wgOut->sysopRequired();
620 if ( $wgUser->isBlocked() ) {
621 return $this->blockedIPpage();
623 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
624 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
627 $name = substr( $oldimage, 15 );
629 $dest = wfImageDir( $name );
630 $archive = wfImageArchiveDir( $name );
631 $curfile = "{$dest}/{$name}";
633 if ( ! is_file( $curfile ) ) {
634 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
637 $oldver = wfTimestampNow() . "!{$name}";
639 $dbr =& wfGetDB( DB_SLAVE
);
640 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
642 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
643 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
646 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
647 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
650 # Record upload and update metadata cache
651 $img = Image
::newFromName( $name );
652 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
654 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
655 $wgOut->setRobotpolicy( 'noindex,nofollow' );
656 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
658 $descTitle = $img->getTitle();
659 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
662 function blockedIPpage() {
663 require_once( 'EditPage.php' );
664 $edit = new EditPage( $this );
665 return $edit->blockedIPpage();
674 class ImageHistoryList
{
675 function ImageHistoryList( &$skin ) {
676 $this->skin
=& $skin;
679 function beginImageHistoryList() {
680 $s = "\n<h2 id=\"filehistory\">" . wfMsg( 'imghistory' ) . "</h2>\n" .
681 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
685 function endImageHistoryList() {
690 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description, $width, $height ) {
691 global $wgUser, $wgLang, $wgContLang, $wgTitle;
693 $datetime = $wgLang->timeanddate( $timestamp, true );
694 $del = wfMsg( 'deleteimg' );
695 $delall = wfMsg( 'deleteimgcompletely' );
696 $cur = wfMsg( 'cur' );
699 $url = Image
::imageUrl( $img );
701 if ( $wgUser->isAllowed('delete') ) {
702 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
704 $style = $this->skin
->getInternalLinkAttributes( $link, $delall );
706 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
711 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
712 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
713 $token = urlencode( $wgUser->editToken( $img ) );
714 $rlink = $this->skin
->makeKnownLinkObj( $wgTitle,
715 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
716 urlencode( $img ) . "&wpEditToken=$token" );
717 $dlink = $this->skin
->makeKnownLinkObj( $wgTitle,
718 $del, 'action=delete&oldimage=' . urlencode( $img ) .
719 "&wpEditToken=$token" );
721 # Having live active links for non-logged in users
722 # means that bots and spiders crawling our site can
723 # inadvertently change content. Baaaad idea.
724 $rlink = wfMsg( 'revertimg' );
729 $userlink = $usertext;
731 $userlink = $this->skin
->makeLinkObj(
732 Title
::makeTitle( NS_USER
, $usertext ),
735 $nbytes = wfMsg( 'nbytes', $size );
736 $widthheight = wfMsg( 'widthheight', $width, $height );
737 $style = $this->skin
->getInternalLinkAttributes( $url, $datetime );
739 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a> . . {$userlink} . . {$widthheight} ({$nbytes})";
741 $s .= $this->skin
->commentBlock( $description, $wgTitle );