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
23 global $wgUseExternalEditor, $wgOut, $wgShowEXIF;
25 $this->img
= new Image( $this->mTitle
);
27 if( $this->mTitle
->getNamespace() == NS_IMAGE
) {
28 if ($wgShowEXIF && $this->img
->exists()) {
29 $exif = $this->img
->getExifData();
30 $showmeta = count($exif) ?
true : false;
36 if ($this->img
->exists())
37 $wgOut->addHTML($this->showTOC($showmeta));
39 $this->openShowImage();
41 $wgOut->addWikiText($this->makeMetadataTable($exif));
43 # No need to display noarticletext, we use our own message, output in openShowImage()
44 if( $this->getID() ) {
47 # Just need to set the right headers
48 $wgOut->setArticleFlag( true );
49 $wgOut->setRobotpolicy( 'index,follow' );
50 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
51 $wgOut->addMetaTags();
55 $this->closeShowImage();
56 $this->imageHistory();
68 * @param bool $metadata Whether or not to show the metadata link
71 function showTOC( $metadata ) {
73 $r = '<ul id="filetoc">
74 <li><a href="#file">' . $wgLang->getNsText( NS_IMAGE
) . '</a></li>' .
75 ($metadata ?
'<li><a href="#metadata">' . wfMsg( 'metadata' ) . '</a></li>' : '') . '
76 <li><a href="#filehistory">' . wfMsg( 'imghistory' ) . '</a></li>
77 <li><a href="#filelinks">' . wfMsg( 'imagelinks' ) . '</a></li>
83 * Make a table with metadata to be shown in the output page.
87 * @param array $exif The array containing the EXIF data
90 function makeMetadataTable( $exif ) {
91 $r = "{| class=metadata align=right width=250px\n";
92 $r .= '|+ id=metadata | '. htmlspecialchars( wfMsg( 'metadata' ) ) . "\n";
93 foreach( $exif as $k => $v ) {
94 $tag = strtolower( $k );
95 $r .= "! class=$tag |" . wfMsg( "exif-$tag" ) . "\n";
96 $r .= "| class=$tag |" . htmlspecialchars( $v ) . "\n";
99 return substr($r, 0, -3) . '|}';
103 * Overloading Article's getContent method.
104 * Omit noarticletext if sharedupload
106 * @param $noredir If true, do not follow redirects
108 function getContent( $noredir )
110 if ( $this->img
&& $this->img
->fromSharedDirectory
&& 0 == $this->getID() ) {
113 return Article
::getContent( $noredir );
116 function openShowImage()
118 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
119 $wgUseImageResize, $wgRepositoryBaseUrl,
120 $wgUseExternalEditor, $wgServer;
121 $full_url = $this->img
->getViewURL();
125 if( $wgUser->getOption( 'imagesize' ) == '' ) {
126 $sizeSel = User
::getDefaultOption( 'imagesize' );
128 $sizeSel = IntVal( $wgUser->getOption( 'imagesize' ) );
130 if( !isset( $wgImageLimits[$sizeSel] ) ) {
131 $sizeSel = User
::getDefaultOption( 'imagesize' );
133 $max = $wgImageLimits[$sizeSel];
135 $maxHeight = $max[1];
136 $sk = $wgUser->getSkin();
138 if ( $this->img
->exists() ) {
140 $width = $this->img
->getWidth();
141 $height = $this->img
->getHeight();
144 if ( $this->img
->allowInlineDisplay() and $width and $height) {
147 # "Download high res version" link below the image
148 $msg = wfMsg('showbigimage', $width, $height, intval( $this->img
->getSize()/1024 ) );
149 if ( $width > $maxWidth ) {
150 $height = floor( $height * $maxWidth / $width );
153 if ( $height > $maxHeight ) {
154 $width = floor( $width * $maxHeight / $height );
155 $height = $maxHeight;
157 if ( !$this->img
->mustRender()
158 && ( $width != $this->img
->getWidth() ||
$height != $this->img
->getHeight() ) ) {
159 if( $wgUseImageResize ) {
160 $thumbnail = $this->img
->getThumbnail( $width );
161 if ( $thumbnail == null ) {
164 $url = $thumbnail->getUrl();
167 # No resize ability? Show the full image, but scale
168 # it down in the browser so it fits on the page.
171 $anchoropen = "<a href=\"{$full_url}\">";
172 $anchorclose = "</a><br />\n$anchoropen{$msg}</a>";
175 $showLink = $this->img
->mustRender();
177 $wgOut->addHTML( '<div class="fullImageLink" id="file">' . $anchoropen .
178 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
179 htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>' );
181 #if direct link is allowed but it's not a renderable image, show an icon.
182 if ($this->img
->isSafeFile()) {
183 $icon= $this->img
->iconThumb();
185 $wgOut->addHTML( '<div class="fullImageLink" id="file"><a href="' . $full_url . '">' .
195 $s= $sk->makeMediaLink( $this->img
->getName(), '', '', true );
196 $info= wfMsg( 'fileinfo', ceil($this->img
->getSize()/1024.0), $this->img
->getMimeType() );
198 if (!$this->img
->isSafeFile()) {
199 $wgOut->addHTML("<div class=\"fullMedia\">");
200 $wgOut->addHTML("<span class=\"dangerousLink\">");
202 $wgOut->addHTML("</span>");
204 $wgOut->addHTML("<span class=\"fileInfo\"> (");
205 $wgOut->addWikiText( $info, false );
206 $wgOut->addHTML(")</span>");
207 $wgOut->addHTML("</div>");
209 #this should be formated a little nicer. Is CSS sufficient?
210 $wgOut->addHTML("<div class=\"mediaWarning\">");
211 $wgOut->addWikiText( wfMsg( 'mediawarning' ) );
212 $wgOut->addHTML('</div>');
215 $wgOut->addHTML("<div class=\"fullMedia\">");
218 $wgOut->addHTML("<span class=\"fileInfo\"> (");
219 $wgOut->addWikiText( $info, false );
220 $wgOut->addHTML(")</span>");
222 $wgOut->addHTML("</div>");
226 if($this->img
->fromSharedDirectory
) {
227 $sharedtext="<div class=\"sharedUploadNotice\">" . wfMsg("sharedupload");
228 if($wgRepositoryBaseUrl) {
229 $sharedtext .= " ". wfMsg("shareduploadwiki",$wgRepositoryBaseUrl . urlencode($this->mTitle
->getDBkey()));
231 $sharedtext.="</div>";
232 $wgOut->addWikiText($sharedtext);
236 # Image does not exist
237 $wgOut->addWikiText( wfMsg( 'noimage', $this->getUploadUrl() ) );
241 function getUploadUrl() {
243 $uploadTitle = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
244 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img
->getName() ) );
248 function uploadLinksBox()
250 global $wgUser,$wgOut;
251 $sk = $wgUser->getSkin();
252 $wgOut->addHTML( '<br /><ul><li>' );
253 $wgOut->addWikiText( '<div>'. wfMsg( 'uploadnewversion', $this->getUploadUrl() ) .'</div>' );
254 $wgOut->addHTML( '</li><li>' );
255 $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle
,
256 wfMsg( 'edit-externally' ), "action=edit&externaledit=true&mode=file" ) );
257 $wgOut->addWikiText( '<div>' . wfMsg('edit-externally-help') . '</div>' );
258 $wgOut->addHTML( '</li></ul>' );
261 function closeShowImage()
268 * If the page we've just displayed is in the "Image" namespace,
269 * we follow it with an upload history of the image and its usage.
271 function imageHistory()
273 global $wgUser, $wgOut, $wgUseExternalEditor;
275 $sk = $wgUser->getSkin();
277 $line = $this->img
->nextHistoryLine();
280 $list =& new ImageHistoryList( $sk );
281 $s = $list->beginImageHistoryList() .
282 $list->imageHistoryLine( true, $line->img_timestamp
,
283 $this->mTitle
->getDBkey(), $line->img_user
,
284 $line->img_user_text
, $line->img_size
, $line->img_description
);
286 while ( $line = $this->img
->nextHistoryLine() ) {
287 $s .= $list->imageHistoryLine( false, $line->img_timestamp
,
288 $line->oi_archive_name
, $line->img_user
,
289 $line->img_user_text
, $line->img_size
, $line->img_description
);
291 $s .= $list->endImageHistoryList();
293 $wgOut->addHTML( $s );
295 # Exist check because we don't want to show this on pages where an image
296 # doesn't exist along with the noimage message, that would suck. -ævar
297 if( $wgUseExternalEditor && $this->img
->exists() ) {
298 $this->uploadLinksBox();
303 function imageLinks()
305 global $wgUser, $wgOut;
307 $wgOut->addHTML( '<h2 id="filelinks">' . wfMsg( 'imagelinks' ) . "</h2>\n" );
309 $dbr =& wfGetDB( DB_SLAVE
);
310 $page = $dbr->tableName( 'page' );
311 $imagelinks = $dbr->tableName( 'imagelinks' );
313 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
314 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id"
315 . " LIMIT 500"; # quickie emergency brake
316 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
318 if ( 0 == $dbr->numRows( $res ) ) {
319 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
322 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
324 $sk = $wgUser->getSkin();
325 while ( $s = $dbr->fetchObject( $res ) ) {
326 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
327 $link = $sk->makeKnownLinkObj( $name, "" );
328 $wgOut->addHTML( "<li>{$link}</li>\n" );
330 $wgOut->addHTML( "</ul>\n" );
335 global $wgUser, $wgOut, $wgRequest;
337 $confirm = $wgRequest->getBool( 'wpConfirmB' );
338 $image = $wgRequest->getVal( 'image' );
339 $oldimage = $wgRequest->getVal( 'oldimage' );
341 # Only sysops can delete images. Previously ordinary users could delete
342 # old revisions, but this is no longer the case.
343 if ( !$wgUser->isAllowed('delete') ) {
344 $wgOut->sysopRequired();
347 if ( wfReadOnly() ) {
348 $wgOut->readOnlyPage();
352 # Better double-check that it hasn't been deleted yet!
353 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
354 if ( ( !is_null( $image ) )
355 && ( '' == trim( $image ) ) ) {
356 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
360 $this->img
= new Image( $this->mTitle
);
362 # Deleting old images doesn't require confirmation
363 if ( !is_null( $oldimage ) ||
$confirm ) {
364 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
367 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
372 if ( !is_null( $image ) ) {
373 $q = '&image=' . urlencode( $image );
374 } else if ( !is_null( $oldimage ) ) {
375 $q = '&oldimage=' . urlencode( $oldimage );
379 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
384 global $wgOut, $wgUser, $wgContLang, $wgRequest;
385 global $wgUseSquid, $wgInternalServer, $wgPostCommitUpdateList;
386 $fname = 'ImagePage::doDelete';
388 $reason = $wgRequest->getVal( 'wpReason' );
389 $oldimage = $wgRequest->getVal( 'oldimage' );
391 $dbw =& wfGetDB( DB_MASTER
);
393 if ( !is_null( $oldimage ) ) {
394 if ( strlen( $oldimage ) < 16 ) {
395 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
398 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
399 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
403 # Invalidate description page cache
404 $this->mTitle
->invalidateCache();
409 $wgInternalServer.wfImageArchiveUrl( $oldimage ),
410 $wgInternalServer.$this->mTitle
->getFullURL()
412 wfPurgeSquidServers($urlArr);
414 $this->doDeleteOldImage( $oldimage );
415 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
416 $deleted = $oldimage;
418 $image = $this->mTitle
->getDBkey();
419 $dest = wfImageDir( $image );
420 $archive = wfImageDir( $image );
422 # Delete the image file if it exists; due to sync problems
423 # or manual trimming sometimes the file will be missing.
424 $targetFile = "{$dest}/{$image}";
425 if( file_exists( $targetFile ) && ! @unlink
( $targetFile ) ) {
426 # If the deletion operation actually failed, bug out:
427 $wgOut->fileDeleteError( $targetFile );
430 $dbw->delete( 'image', array( 'img_name' => $image ) );
431 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
433 # Purge archive URLs from the squid
435 while ( $s = $dbw->fetchObject( $res ) ) {
436 $this->doDeleteOldImage( $s->oi_archive_name
);
437 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name
);
440 # And also the HTML of all pages using this image
441 $linksTo = $this->img
->getLinksTo();
443 $u = SquidUpdate
::newFromTitles( $linksTo, $urlArr );
444 array_push( $wgPostCommitUpdateList, $u );
447 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
449 # Image itself is now gone, and database is cleaned.
450 # Now we remove the image description page.
452 $article = new Article( $this->mTitle
);
453 $article->doDeleteArticle( $reason ); # ignore errors
455 # Invalidate parser cache and client cache for pages using this image
456 # This is left until relatively late to reduce lock time
457 Title
::touchArray( $linksTo );
459 /* Delete thumbnails and refresh image metadata cache */
460 $this->img
->purgeCache();
466 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
467 $wgOut->setRobotpolicy( 'noindex,nofollow' );
469 $loglink = '[[Special:Log/delete|' . wfMsg( 'deletionlog' ) . ']]';
470 $text = wfMsg( 'deletedtext', $deleted, $loglink );
472 $wgOut->addWikiText( $text );
474 $wgOut->returnToMain( false, $this->mTitle
->getPrefixedText() );
477 function doDeleteOldImage( $oldimage )
481 $name = substr( $oldimage, 15 );
482 $archive = wfImageArchiveDir( $name );
484 # Delete the image if it exists. Sometimes the file will be missing
485 # due to manual intervention or weird sync problems; treat that
486 # condition gracefully and continue to delete the database entry.
487 # Also some records may end up with an empty oi_archive_name field
488 # if the original file was missing when a new upload was made;
489 # don't try to delete the directory then!
491 $targetFile = "{$archive}/{$oldimage}";
492 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink
( $targetFile ) ) {
493 # If we actually have a file and can't delete it, throw an error.
494 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
497 $log = new LogPage( 'delete' );
498 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
504 global $wgOut, $wgRequest, $wgUser;
505 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
507 $oldimage = $wgRequest->getText( 'oldimage' );
508 if ( strlen( $oldimage ) < 16 ) {
509 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
512 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
513 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
517 if ( wfReadOnly() ) {
518 $wgOut->readOnlyPage();
521 if( $wgUser->isAnon() ) {
522 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
525 if ( ! $this->mTitle
->userCanEdit() ) {
526 $wgOut->sysopRequired();
529 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
530 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
533 $name = substr( $oldimage, 15 );
535 $dest = wfImageDir( $name );
536 $archive = wfImageArchiveDir( $name );
537 $curfile = "{$dest}/{$name}";
539 if ( ! is_file( $curfile ) ) {
540 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
543 $oldver = wfTimestampNow() . "!{$name}";
545 $dbr =& wfGetDB( DB_SLAVE
);
546 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
548 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
549 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
552 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
553 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
556 # Record upload and update metadata cache
557 $img = Image
::newFromName( $name );
558 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
560 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
561 $wgOut->setRobotpolicy( 'noindex,nofollow' );
562 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
564 $descTitle = $img->getTitle();
565 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
573 class ImageHistoryList
{
574 function ImageHistoryList( &$skin ) {
575 $this->skin
=& $skin;
578 function beginImageHistoryList() {
579 $s = "\n<h2 id=\"filehistory\">" . wfMsg( 'imghistory' ) . "</h2>\n" .
580 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
584 function endImageHistoryList() {
589 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
590 global $wgUser, $wgLang, $wgContLang, $wgTitle;
592 $datetime = $wgLang->timeanddate( $timestamp, true );
593 $del = wfMsg( 'deleteimg' );
594 $delall = wfMsg( 'deleteimgcompletely' );
595 $cur = wfMsg( 'cur' );
598 $url = Image
::imageUrl( $img );
600 if ( $wgUser->isAllowed('delete') ) {
601 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
603 $style = $this->skin
->getInternalLinkAttributes( $link, $delall );
605 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
610 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
611 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
612 $token = urlencode( $wgUser->editToken( $img ) );
613 $rlink = $this->skin
->makeKnownLinkObj( $wgTitle,
614 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
615 urlencode( $img ) . "&wpEditToken=$token" );
616 $dlink = $this->skin
->makeKnownLinkObj( $wgTitle,
617 $del, 'action=delete&oldimage=' . urlencode( $img ) .
618 "&wpEditToken=$token" );
620 # Having live active links for non-logged in users
621 # means that bots and spiders crawling our site can
622 # inadvertently change content. Baaaad idea.
623 $rlink = wfMsg( 'revertimg' );
628 $userlink = $usertext;
630 $userlink = $this->skin
->makeLinkObj(
631 Title
::makeTitle( NS_USER
, $usertext ),
634 $nbytes = wfMsg( 'nbytes', $size );
635 $style = $this->skin
->getInternalLinkAttributes( $url, $datetime );
637 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
638 . " . . {$userlink} ({$nbytes})";
640 $s .= $this->skin
->commentBlock( $description, $wgTitle );