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 $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() {
164 global $wgOut, $wgUser, $wgImageLimits, $wgRequest, $wgUseImageResize;
166 $full_url = $this->img
->getURL();
170 if( $wgUser->getOption( 'imagesize' ) == '' ) {
171 $sizeSel = User
::getDefaultOption( 'imagesize' );
173 $sizeSel = intval( $wgUser->getOption( 'imagesize' ) );
175 if( !isset( $wgImageLimits[$sizeSel] ) ) {
176 $sizeSel = User
::getDefaultOption( 'imagesize' );
178 $max = $wgImageLimits[$sizeSel];
180 $maxHeight = $max[1];
181 $sk = $wgUser->getSkin();
183 if ( $this->img
->exists() ) {
185 $width = $this->img
->getWidth();
186 $height = $this->img
->getHeight();
189 if ( $this->img
->allowInlineDisplay() and $width and $height) {
192 # "Download high res version" link below the image
193 $msg = wfMsgHtml('showbigimage', $width, $height, intval( $this->img
->getSize()/1024 ) );
195 # We'll show a thumbnail of this image
196 if ( $width > $maxWidth ||
$height > $maxHeight ) {
197 # Calculate the thumbnail size.
198 # First case, the limiting factor is the width, not the height.
199 if ( $width / $height >= $maxWidth / $maxHeight ) {
200 $height = round( $height * $maxWidth / $width);
202 # Note that $height <= $maxHeight now.
204 $newwidth = floor( $width * $maxHeight / $height);
205 $height = round( $height * $newwidth / $width );
207 # Note that $height <= $maxHeight now, but might not be identical
208 # because of rounding.
211 if( $wgUseImageResize ) {
212 $thumbnail = $this->img
->getThumbnail( $width );
213 if ( $thumbnail == null ) {
214 $url = $this->img
->getViewURL();
216 $url = $thumbnail->getURL();
219 # No resize ability? Show the full image, but scale
220 # it down in the browser so it fits on the page.
221 $url = $this->img
->getViewURL();
223 $anchoropen = "<a href=\"{$full_url}\">";
224 $anchorclose = "</a><br />";
225 if( $this->img
->mustRender() ) {
228 $anchorclose .= "\n$anchoropen{$msg}</a>";
231 $url = $this->img
->getViewURL();
234 $wgOut->addHTML( '<div class="fullImageLink" id="file">' . $anchoropen .
235 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
236 htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>' );
238 #if direct link is allowed but it's not a renderable image, show an icon.
239 if ($this->img
->isSafeFile()) {
240 $icon= $this->img
->iconThumb();
242 $wgOut->addHTML( '<div class="fullImageLink" id="file"><a href="' . $full_url . '">' .
252 $filename = wfEscapeWikiText( $this->img
->getName() );
253 $info = wfMsg( 'fileinfo',
254 ceil($this->img
->getSize()/1024.0),
255 $this->img
->getMimeType() );
257 if (!$this->img
->isSafeFile()) {
258 $warning = wfMsg( 'mediawarning' );
259 $wgOut->addWikiText( <<<END
260 <div class="fullMedia">
261 <span class="dangerousLink">[[Media:$filename|$filename]]</span>
262 <span class="fileInfo"> ($info)</span>
265 <div class="mediaWarning">$warning</div>
269 $wgOut->addWikiText( <<<END
270 <div class="fullMedia">
271 [[Media:$filename|$filename]] <span class="fileInfo"> ($info)</span>
278 if($this->img
->fromSharedDirectory
) {
279 $this->printSharedImageText();
282 # Image does not exist
284 $title = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
285 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('noimage-linktext'),
286 'wpDestFile=' . urlencode( $this->img
->getName() ) );
287 $wgOut->addHTML( wfMsgWikiHtml( 'noimage', $link ) );
291 function printSharedImageText() {
292 global $wgRepositoryBaseUrl, $wgFetchCommonsDescriptions, $wgOut, $wgUser;
294 $url = $wgRepositoryBaseUrl . urlencode($this->mTitle
->getDBkey());
295 $sharedtext = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml("sharedupload");
296 if ($wgRepositoryBaseUrl && !$wgFetchCommonsDescriptions) {
298 $sk = $wgUser->getSkin();
299 $title = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
300 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('shareduploadwiki-linktext'),
301 array( 'wpDestFile' => urlencode( $this->img
->getName() )));
302 $sharedtext .= " " . wfMsgWikiHtml('shareduploadwiki', $link);
304 $sharedtext .= "</div>";
305 $wgOut->addHTML($sharedtext);
307 if ($wgRepositoryBaseUrl && $wgFetchCommonsDescriptions) {
308 require_once("HttpFunctions.php");
309 $ur = ini_set('allow_url_fopen', true);
310 $text = wfGetHTTP($url . '?action=render');
311 ini_set('allow_url_fopen', $ur);
313 $this->mExtraDescription
= $text;
317 function getUploadUrl() {
319 $uploadTitle = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
320 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img
->getName() ) );
324 function uploadLinksBox()
326 global $wgUser, $wgOut;
328 if ($this->img
->fromSharedDirectory
)
331 $sk = $wgUser->getSkin();
332 $wgOut->addHTML( '<br /><ul>' );
333 if( $wgUser->isAllowed( 'reupload' ) ) {
334 $wgOut->addWikiText( "<li>\n<div>". wfMsg( 'uploadnewversion', $this->getUploadUrl() ) ."</div>\n</li>\n" );
336 $wgOut->addHTML( '<li>' );
337 $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle
,
338 wfMsg( 'edit-externally' ), "action=edit&externaledit=true&mode=file" ) );
339 $wgOut->addWikiText( '<div>' . wfMsg('edit-externally-help') . '</div>' );
340 $wgOut->addHTML( '</li></ul>' );
343 function closeShowImage()
350 * If the page we've just displayed is in the "Image" namespace,
351 * we follow it with an upload history of the image and its usage.
353 function imageHistory()
355 global $wgUser, $wgOut, $wgUseExternalEditor;
357 $sk = $wgUser->getSkin();
359 $line = $this->img
->nextHistoryLine();
362 $list =& new ImageHistoryList( $sk );
363 $s = $list->beginImageHistoryList() .
364 $list->imageHistoryLine( true, wfTimestamp(TS_MW
, $line->img_timestamp
),
365 $this->mTitle
->getDBkey(), $line->img_user
,
366 $line->img_user_text
, $line->img_size
, $line->img_description
,
367 $line->img_width
, $line->img_height
370 while ( $line = $this->img
->nextHistoryLine() ) {
371 $s .= $list->imageHistoryLine( false, $line->img_timestamp
,
372 $line->oi_archive_name
, $line->img_user
,
373 $line->img_user_text
, $line->img_size
, $line->img_description
,
374 $line->img_width
, $line->img_height
377 $s .= $list->endImageHistoryList();
379 $wgOut->addHTML( $s );
381 # Exist check because we don't want to show this on pages where an image
382 # doesn't exist along with the noimage message, that would suck. -ævar
383 if( $wgUseExternalEditor && $this->img
->exists() ) {
384 $this->uploadLinksBox();
389 function imageLinks()
391 global $wgUser, $wgOut;
393 $wgOut->addHTML( '<h2 id="filelinks">' . wfMsg( 'imagelinks' ) . "</h2>\n" );
395 $dbr =& wfGetDB( DB_SLAVE
);
396 $page = $dbr->tableName( 'page' );
397 $imagelinks = $dbr->tableName( 'imagelinks' );
399 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
400 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id";
401 $sql = $dbr->limitResult($sql, 500, 0);
402 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
404 if ( 0 == $dbr->numRows( $res ) ) {
405 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
408 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
410 $sk = $wgUser->getSkin();
411 while ( $s = $dbr->fetchObject( $res ) ) {
412 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
413 $link = $sk->makeKnownLinkObj( $name, "" );
414 $wgOut->addHTML( "<li>{$link}</li>\n" );
416 $wgOut->addHTML( "</ul>\n" );
421 global $wgUser, $wgOut, $wgRequest;
423 $confirm = $wgRequest->wasPosted();
424 $image = $wgRequest->getVal( 'image' );
425 $oldimage = $wgRequest->getVal( 'oldimage' );
427 # Only sysops can delete images. Previously ordinary users could delete
428 # old revisions, but this is no longer the case.
429 if ( !$wgUser->isAllowed('delete') ) {
430 $wgOut->sysopRequired();
433 if ( $wgUser->isBlocked() ) {
434 return $this->blockedIPpage();
436 if ( wfReadOnly() ) {
437 $wgOut->readOnlyPage();
441 # Better double-check that it hasn't been deleted yet!
442 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
443 if ( ( !is_null( $image ) )
444 && ( '' == trim( $image ) ) ) {
445 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
449 $this->img
= new Image( $this->mTitle
);
451 # Deleting old images doesn't require confirmation
452 if ( !is_null( $oldimage ) ||
$confirm ) {
453 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
456 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
461 if ( !is_null( $image ) ) {
462 $q = '&image=' . urlencode( $image );
463 } else if ( !is_null( $oldimage ) ) {
464 $q = '&oldimage=' . urlencode( $oldimage );
468 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
471 function doDelete() {
472 global $wgOut, $wgUser, $wgRequest, $wgUseSquid, $wgInternalServer;
473 global $wgPostCommitUpdateList;
475 $fname = 'ImagePage::doDelete';
477 $reason = $wgRequest->getVal( 'wpReason' );
478 $oldimage = $wgRequest->getVal( 'oldimage' );
480 $dbw =& wfGetDB( DB_MASTER
);
482 if ( !is_null( $oldimage ) ) {
483 if ( strlen( $oldimage ) < 16 ) {
484 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
487 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
488 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
492 # Invalidate description page cache
493 $this->mTitle
->invalidateCache();
498 $wgInternalServer.wfImageArchiveUrl( $oldimage ),
499 $wgInternalServer.$this->mTitle
->getFullURL()
501 wfPurgeSquidServers($urlArr);
503 $this->doDeleteOldImage( $oldimage );
504 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
505 $deleted = $oldimage;
507 $image = $this->mTitle
->getDBkey();
508 $dest = wfImageDir( $image );
509 $archive = wfImageDir( $image );
511 # Delete the image file if it exists; due to sync problems
512 # or manual trimming sometimes the file will be missing.
513 $targetFile = "{$dest}/{$image}";
514 if( file_exists( $targetFile ) && ! @unlink
( $targetFile ) ) {
515 # If the deletion operation actually failed, bug out:
516 $wgOut->fileDeleteError( $targetFile );
519 $dbw->delete( 'image', array( 'img_name' => $image ) );
520 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
522 # Purge archive URLs from the squid
524 while ( $s = $dbw->fetchObject( $res ) ) {
525 $this->doDeleteOldImage( $s->oi_archive_name
);
526 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name
);
529 # And also the HTML of all pages using this image
530 $linksTo = $this->img
->getLinksTo();
532 $u = SquidUpdate
::newFromTitles( $linksTo, $urlArr );
533 array_push( $wgPostCommitUpdateList, $u );
536 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
538 # Image itself is now gone, and database is cleaned.
539 # Now we remove the image description page.
541 $article = new Article( $this->mTitle
);
542 $article->doDeleteArticle( $reason ); # ignore errors
544 # Invalidate parser cache and client cache for pages using this image
545 # This is left until relatively late to reduce lock time
546 Title
::touchArray( $linksTo );
548 /* Delete thumbnails and refresh image metadata cache */
549 $this->img
->purgeCache();
555 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
556 $wgOut->setRobotpolicy( 'noindex,nofollow' );
558 $loglink = '[[Special:Log/delete|' . wfMsg( 'deletionlog' ) . ']]';
559 $text = wfMsg( 'deletedtext', $deleted, $loglink );
561 $wgOut->addWikiText( $text );
563 $wgOut->returnToMain( false, $this->mTitle
->getPrefixedText() );
566 function doDeleteOldImage( $oldimage )
570 $name = substr( $oldimage, 15 );
571 $archive = wfImageArchiveDir( $name );
573 # Delete the image if it exists. Sometimes the file will be missing
574 # due to manual intervention or weird sync problems; treat that
575 # condition gracefully and continue to delete the database entry.
576 # Also some records may end up with an empty oi_archive_name field
577 # if the original file was missing when a new upload was made;
578 # don't try to delete the directory then!
580 $targetFile = "{$archive}/{$oldimage}";
581 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink
( $targetFile ) ) {
582 # If we actually have a file and can't delete it, throw an error.
583 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
586 $log = new LogPage( 'delete' );
587 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
592 global $wgOut, $wgRequest, $wgUser, $wgUseSquid, $wgInternalServer;
594 $oldimage = $wgRequest->getText( 'oldimage' );
595 if ( strlen( $oldimage ) < 16 ) {
596 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
599 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
600 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
604 if ( wfReadOnly() ) {
605 $wgOut->readOnlyPage();
608 if( $wgUser->isAnon() ) {
609 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
612 if ( ! $this->mTitle
->userCanEdit() ) {
613 $wgOut->sysopRequired();
616 if ( $wgUser->isBlocked() ) {
617 return $this->blockedIPpage();
619 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
620 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
623 $name = substr( $oldimage, 15 );
625 $dest = wfImageDir( $name );
626 $archive = wfImageArchiveDir( $name );
627 $curfile = "{$dest}/{$name}";
629 if ( ! is_file( $curfile ) ) {
630 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
633 $oldver = wfTimestampNow() . "!{$name}";
635 $dbr =& wfGetDB( DB_SLAVE
);
636 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
638 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
639 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
642 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
643 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
646 # Record upload and update metadata cache
647 $img = Image
::newFromName( $name );
648 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
650 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
651 $wgOut->setRobotpolicy( 'noindex,nofollow' );
652 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
654 $descTitle = $img->getTitle();
655 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
658 function blockedIPpage() {
659 require_once( 'EditPage.php' );
660 $edit = new EditPage( $this );
661 return $edit->blockedIPpage();
670 class ImageHistoryList
{
671 function ImageHistoryList( &$skin ) {
672 $this->skin
=& $skin;
675 function beginImageHistoryList() {
676 $s = "\n<h2 id=\"filehistory\">" . wfMsg( 'imghistory' ) . "</h2>\n" .
677 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
681 function endImageHistoryList() {
686 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description, $width, $height ) {
687 global $wgUser, $wgLang, $wgTitle;
689 $datetime = $wgLang->timeanddate( $timestamp, true );
690 $del = wfMsg( 'deleteimg' );
691 $delall = wfMsg( 'deleteimgcompletely' );
692 $cur = wfMsg( 'cur' );
695 $url = Image
::imageUrl( $img );
697 if ( $wgUser->isAllowed('delete') ) {
698 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
700 $style = $this->skin
->getInternalLinkAttributes( $link, $delall );
702 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
707 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
708 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
709 $token = urlencode( $wgUser->editToken( $img ) );
710 $rlink = $this->skin
->makeKnownLinkObj( $wgTitle,
711 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
712 urlencode( $img ) . "&wpEditToken=$token" );
713 $dlink = $this->skin
->makeKnownLinkObj( $wgTitle,
714 $del, 'action=delete&oldimage=' . urlencode( $img ) .
715 "&wpEditToken=$token" );
717 # Having live active links for non-logged in users
718 # means that bots and spiders crawling our site can
719 # inadvertently change content. Baaaad idea.
720 $rlink = wfMsg( 'revertimg' );
725 $userlink = $usertext;
727 $userlink = $this->skin
->makeLinkObj(
728 Title
::makeTitle( NS_USER
, $usertext ),
731 $nbytes = wfMsg( 'nbytes', $size );
732 $widthheight = wfMsg( 'widthheight', $width, $height );
733 $style = $this->skin
->getInternalLinkAttributes( $url, $datetime );
735 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a> . . {$userlink} . . {$widthheight} ({$nbytes})";
737 $s .= $this->skin
->commentBlock( $description, $wgTitle );