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 ;
25 $this->img
= new Image( $this->mTitle
);
27 if( $this->mTitle
->getNamespace() == NS_IMAGE
) {
28 $this->openShowImage();
29 if ( $this->img
->exists() ) $this->showEXIFdata();
31 # No need to display noarticletext, we use our own message, output in openShowImage()
32 if ( $this->getID() ) {
35 # Just need to set the right headers
36 $wgOut->setArticleFlag( true );
37 $wgOut->setRobotpolicy( 'index,follow' );
38 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
39 $wgOut->addMetaTags();
43 if ( $this->img
->exists() ) {
44 $this->uploadNewVersionLink();
45 if ( $wgUseExternalEditor && $this->img
->exists() ) {
46 $this->externalEditorLink();
49 $this->closeShowImage();
50 $this->imageHistory();
57 function showEXIFdata()
59 global $wgOut , $wgShowEXIF ;
60 if ( ! $wgShowEXIF ) return ;
63 $exif = $this->img
->getExifData () ;
64 if ( count ( $exif ) == 0 ) return ; # No EXIF data available
65 if ( count ( $exif ) == 1 && isset ( $exif["EXIF"] ) && $exif["EXIF"] == "NO" ) return ; # This image does not have EXIF data
68 $r = "<table border='1' cellspacing='0' cellpadding='0' align='right'>" ;
69 $r .= "<caption>EXIF data</caption>" ;
70 foreach ( $exif AS $k => $v ) {
72 $v = str_replace ( "\0" , "" , $v ) ;
73 $r .= "<th><small>" . htmlspecialchars ( $k ) . "</small></th>" ;
74 $r .= "<td><small>" . htmlspecialchars ( $v ) . "</small></td>" ;
78 $wgOut->addHTML ( $r ) ;
81 function openShowImage()
83 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
84 $wgUseImageResize, $wgRepositoryBaseUrl,
85 $wgUseExternalEditor, $wgServer;
86 $full_url = $this->img
->getViewURL();
90 if( $wgUser->getOption( 'imagesize' ) == '' ) {
91 $sizeSel = User
::getDefaultOption( 'imagesize' );
93 $sizeSel = IntVal( $wgUser->getOption( 'imagesize' ) );
95 if( !isset( $wgImageLimits[$sizeSel] ) ) {
96 $sizeSel = User
::getDefaultOption( 'imagesize' );
98 $max = $wgImageLimits[$sizeSel];
100 $maxHeight = $max[1];
101 $sk = $wgUser->getSkin();
103 if ( $this->img
->exists() ) {
104 if ( $this->img
->getType() != '' ) {
106 $width = $this->img
->getWidth();
107 $height = $this->img
->getHeight();
108 $msg = wfMsg('showbigimage', $width, $height, intval( $this->img
->getSize()/1024 ) );
109 if ( $width > $maxWidth ) {
110 $height = floor( $height * $maxWidth / $width );
113 if ( $height > $maxHeight ) {
114 $width = floor( $width * $maxHeight / $height );
115 $height = $maxHeight;
117 if ( $width != $this->img
->getWidth() ||
$height != $this->img
->getHeight() ) {
118 if( $wgUseImageResize ) {
119 $thumbnail = $this->img
->getThumbnail( $width );
120 $url = $thumbnail->getUrl();
122 # No resize ability? Show the full image, but scale
123 # it down in the browser so it fits on the page.
126 $anchoropen = "<a href=\"{$full_url}\">";
127 $anchorclose = "</a><br />\n$anchoropen{$msg}</a>";
131 $s = '<div class="fullImageLink">' . $anchoropen .
132 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
133 htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>';
135 $s = "<div class=\"fullMedia\">" . $sk->makeMediaLink( $this->img
->getName(),'' ) . '</div>';
137 $wgOut->addHTML( $s );
138 if($this->img
->fromSharedDirectory
) {
139 $sharedtext="<div class=\"sharedUploadNotice\">" . wfMsg("sharedupload");
140 if($wgRepositoryBaseUrl) {
141 $sharedtext .= " ". wfMsg("shareduploadwiki",$wgRepositoryBaseUrl . urlencode($this->mTitle
->getDBkey()));
143 $sharedtext.="</div>";
144 $wgOut->addWikiText($sharedtext);
148 # Image does not exist
149 $wgOut->addWikiText( wfMsg( 'noimage', $this->getUploadUrl() ) );
153 function getUploadUrl() {
155 $uploadTitle = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
156 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img
->getName() ) );
160 function uploadNewVersionLink() {
162 $wgOut->addWikiText( wfMsg( 'uploadnewversion', $this->getUploadUrl() ) );
165 function externalEditorLink()
167 global $wgUser,$wgOut;
168 $sk = $wgUser->getSkin();
169 $wgOut->addHTML( '<div class="editExternally">' );
170 $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle
,
171 wfMsg( 'edit-externally' ),
172 "action=edit&externaledit=true&mode=file" ) );
173 $wgOut->addWikiText( '<div class="editExternallyHelp">' .
174 wfMsg('edit-externally-help') . '</div>' );
175 $wgOut->addHTML( '</div><br clear="both" />' );
178 function closeShowImage()
185 * If the page we've just displayed is in the "Image" namespace,
186 * we follow it with an upload history of the image and its usage.
188 function imageHistory()
190 global $wgUser, $wgOut;
192 $sk = $wgUser->getSkin();
194 $line = $this->img
->nextHistoryLine();
197 $list =& new ImageHistoryList( $sk );
198 $s = $list->beginImageHistoryList() .
199 $list->imageHistoryLine( true, $line->img_timestamp
,
200 $this->mTitle
->getDBkey(), $line->img_user
,
201 $line->img_user_text
, $line->img_size
, $line->img_description
);
203 while ( $line = $this->img
->nextHistoryLine() ) {
204 $s .= $list->imageHistoryLine( false, $line->img_timestamp
,
205 $line->oi_archive_name
, $line->img_user
,
206 $line->img_user_text
, $line->img_size
, $line->img_description
);
208 $s .= $list->endImageHistoryList();
210 $wgOut->addHTML( $s );
213 function imageLinks()
215 global $wgUser, $wgOut;
217 $wgOut->addHTML( '<h2>' . wfMsg( 'imagelinks' ) . "</h2>\n" );
219 $dbr =& wfGetDB( DB_SLAVE
);
220 $page = $dbr->tableName( 'page' );
221 $imagelinks = $dbr->tableName( 'imagelinks' );
223 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
224 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id"
225 . " LIMIT 500"; # quickie emergency brake
226 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
228 if ( 0 == $dbr->numRows( $res ) ) {
229 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
232 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
234 $sk = $wgUser->getSkin();
235 while ( $s = $dbr->fetchObject( $res ) ) {
236 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
237 $link = $sk->makeKnownLinkObj( $name, "" );
238 $wgOut->addHTML( "<li>{$link}</li>\n" );
240 $wgOut->addHTML( "</ul>\n" );
245 global $wgUser, $wgOut, $wgRequest;
247 $confirm = $wgRequest->getBool( 'wpConfirmB' );
248 $image = $wgRequest->getVal( 'image' );
249 $oldimage = $wgRequest->getVal( 'oldimage' );
251 # Only sysops can delete images. Previously ordinary users could delete
252 # old revisions, but this is no longer the case.
253 if ( !$wgUser->isAllowed('delete') ) {
254 $wgOut->sysopRequired();
257 if ( wfReadOnly() ) {
258 $wgOut->readOnlyPage();
262 # Better double-check that it hasn't been deleted yet!
263 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
264 if ( ( !is_null( $image ) )
265 && ( '' == trim( $image ) ) ) {
266 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
270 $this->img
= new Image( $this->mTitle
);
272 # Deleting old images doesn't require confirmation
273 if ( !is_null( $oldimage ) ||
$confirm ) {
274 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
277 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
282 if ( !is_null( $image ) ) {
283 $q = '&image=' . urlencode( $image );
284 } else if ( !is_null( $oldimage ) ) {
285 $q = '&oldimage=' . urlencode( $oldimage );
289 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
294 global $wgOut, $wgUser, $wgContLang, $wgRequest;
295 global $wgUseSquid, $wgInternalServer, $wgPostCommitUpdateList;
296 $fname = 'ImagePage::doDelete';
298 $reason = $wgRequest->getVal( 'wpReason' );
299 $oldimage = $wgRequest->getVal( 'oldimage' );
301 $dbw =& wfGetDB( DB_MASTER
);
303 if ( !is_null( $oldimage ) ) {
304 if ( strlen( $oldimage ) < 16 ) {
305 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
308 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
309 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
313 # Invalidate description page cache
314 $this->mTitle
->invalidateCache();
319 $wgInternalServer.wfImageArchiveUrl( $oldimage ),
320 $wgInternalServer.$this->mTitle
->getFullURL()
322 wfPurgeSquidServers($urlArr);
324 $this->doDeleteOldImage( $oldimage );
325 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
326 $deleted = $oldimage;
328 $image = $this->mTitle
->getDBkey();
329 $dest = wfImageDir( $image );
330 $archive = wfImageDir( $image );
332 # Delete the image file if it exists; due to sync problems
333 # or manual trimming sometimes the file will be missing.
334 $targetFile = "{$dest}/{$image}";
335 if( file_exists( $targetFile ) && ! @unlink
( $targetFile ) ) {
336 # If the deletion operation actually failed, bug out:
337 $wgOut->fileDeleteError( $targetFile );
340 $dbw->delete( 'image', array( 'img_name' => $image ) );
341 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
343 # Purge archive URLs from the squid
345 while ( $s = $dbw->fetchObject( $res ) ) {
346 $this->doDeleteOldImage( $s->oi_archive_name
);
347 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name
);
350 # And also the HTML of all pages using this image
351 $linksTo = $this->img
->getLinksTo();
353 $u = SquidUpdate
::newFromTitles( $linksTo, $urlArr );
354 array_push( $wgPostCommitUpdateList, $u );
357 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
359 # Image itself is now gone, and database is cleaned.
360 # Now we remove the image description page.
362 $article = new Article( $this->mTitle
);
363 $article->doDeleteArticle( $reason ); # ignore errors
365 # Invalidate parser cache and client cache for pages using this image
366 # This is left until relatively late to reduce lock time
367 Title
::touchArray( $linksTo );
369 /* Delete thumbnails and refresh image metadata cache */
370 $this->img
->purgeCache();
376 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
377 $wgOut->setRobotpolicy( 'noindex,nofollow' );
379 $sk = $wgUser->getSkin();
380 $loglink = $sk->makeKnownLinkObj(
381 Title
::makeTitle( NS_SPECIAL
, 'Log/delete' ),
382 wfMsg( 'deletionlog' ) );
384 $text = wfMsg( 'deletedtext', $deleted, $loglink );
386 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
387 $wgOut->returnToMain( false, $this->mTitle
->getPrefixedText() );
390 function doDeleteOldImage( $oldimage )
394 $name = substr( $oldimage, 15 );
395 $archive = wfImageArchiveDir( $name );
397 # Delete the image if it exists. Sometimes the file will be missing
398 # due to manual intervention or weird sync problems; treat that
399 # condition gracefully and continue to delete the database entry.
400 # Also some records may end up with an empty oi_archive_name field
401 # if the original file was missing when a new upload was made;
402 # don't try to delete the directory then!
404 $targetFile = "{$archive}/{$oldimage}";
405 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink
( $targetFile ) ) {
406 # If we actually have a file and can't delete it, throw an error.
407 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
410 $log = new LogPage( 'delete' );
411 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
417 global $wgOut, $wgRequest, $wgUser;
418 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
420 $oldimage = $wgRequest->getText( 'oldimage' );
421 if ( strlen( $oldimage ) < 16 ) {
422 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
425 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
426 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
430 if ( wfReadOnly() ) {
431 $wgOut->readOnlyPage();
434 if( $wgUser->isAnon() ) {
435 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
438 if ( ! $this->mTitle
->userCanEdit() ) {
439 $wgOut->sysopRequired();
442 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
443 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
446 $name = substr( $oldimage, 15 );
448 $dest = wfImageDir( $name );
449 $archive = wfImageArchiveDir( $name );
450 $curfile = "{$dest}/{$name}";
452 if ( ! is_file( $curfile ) ) {
453 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
456 $oldver = wfTimestampNow() . "!{$name}";
458 $dbr =& wfGetDB( DB_SLAVE
);
459 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
461 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
462 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
465 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
466 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
469 # Record upload and update metadata cache
470 $img = Image
::newFromName( $name );
471 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
473 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
474 $wgOut->setRobotpolicy( 'noindex,nofollow' );
475 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
477 $descTitle = $img->getTitle();
478 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
486 class ImageHistoryList
{
487 function ImageHistoryList( &$skin ) {
488 $this->skin
=& $skin;
491 function beginImageHistoryList() {
492 $s = "\n<h2>" . wfMsg( 'imghistory' ) . "</h2>\n" .
493 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
497 function endImageHistoryList() {
502 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
503 global $wgUser, $wgLang, $wgContLang, $wgTitle;
505 $datetime = $wgLang->timeanddate( $timestamp, true );
506 $del = wfMsg( 'deleteimg' );
507 $delall = wfMsg( 'deleteimgcompletely' );
508 $cur = wfMsg( 'cur' );
511 $url = Image
::imageUrl( $img );
513 if ( $wgUser->isAllowed('delete') ) {
514 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
516 $style = $this->skin
->getInternalLinkAttributes( $link, $delall );
518 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
523 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
524 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
525 $token = urlencode( $wgUser->editToken( $img ) );
526 $rlink = $this->skin
->makeKnownLinkObj( $wgTitle,
527 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
528 urlencode( $img ) . "&wpEditToken=$token" );
529 $dlink = $this->skin
->makeKnownLinkObj( $wgTitle,
530 $del, 'action=delete&oldimage=' . urlencode( $img ) .
531 "&wpEditToken=$token" );
533 # Having live active links for non-logged in users
534 # means that bots and spiders crawling our site can
535 # inadvertently change content. Baaaad idea.
536 $rlink = wfMsg( 'revertimg' );
541 $userlink = $usertext;
543 $userlink = $this->skin
->makeLinkObj(
544 Title
::makeTitle( NS_USER
, $usertext ),
547 $nbytes = wfMsg( 'nbytes', $size );
548 $style = $this->skin
->getInternalLinkAttributes( $url, $datetime );
550 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
551 . " . . {$userlink} ({$nbytes})";
553 $s .= $this->skin
->commentBlock( $description, $wgTitle );