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 $exifdata = wfMsg( 'exifdata' );
69 $r = "\n<table class='exif'>\n" ;
70 $r .= "<caption>$exifdata</caption>\n" ;
71 foreach ( $exif AS $k => $v ) {
72 $r .= '<tr><th>' . htmlspecialchars ( $k ) . '</th><td>' . htmlspecialchars ( $v ) . "</td></tr>\n";
75 $wgOut->addHTML ( $r ) ;
78 function openShowImage()
80 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
81 $wgUseImageResize, $wgRepositoryBaseUrl,
82 $wgUseExternalEditor, $wgServer;
83 $full_url = $this->img
->getViewURL();
87 if( $wgUser->getOption( 'imagesize' ) == '' ) {
88 $sizeSel = User
::getDefaultOption( 'imagesize' );
90 $sizeSel = IntVal( $wgUser->getOption( 'imagesize' ) );
92 if( !isset( $wgImageLimits[$sizeSel] ) ) {
93 $sizeSel = User
::getDefaultOption( 'imagesize' );
95 $max = $wgImageLimits[$sizeSel];
98 $sk = $wgUser->getSkin();
100 if ( $this->img
->exists() ) {
101 if ( $this->img
->getType() != '' ) {
103 $width = $this->img
->getWidth();
104 $height = $this->img
->getHeight();
105 $msg = wfMsg('showbigimage', $width, $height, intval( $this->img
->getSize()/1024 ) );
106 if ( $width > $maxWidth ) {
107 $height = floor( $height * $maxWidth / $width );
110 if ( $height > $maxHeight ) {
111 $width = floor( $width * $maxHeight / $height );
112 $height = $maxHeight;
114 if ( $width != $this->img
->getWidth() ||
$height != $this->img
->getHeight() ) {
115 if( $wgUseImageResize ) {
116 $thumbnail = $this->img
->getThumbnail( $width );
117 $url = $thumbnail->getUrl();
119 # No resize ability? Show the full image, but scale
120 # it down in the browser so it fits on the page.
123 $anchoropen = "<a href=\"{$full_url}\">";
124 $anchorclose = "</a><br />\n$anchoropen{$msg}</a>";
128 $s = '<div class="fullImageLink">' . $anchoropen .
129 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
130 htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>';
132 $s = "<div class=\"fullMedia\">" . $sk->makeMediaLink( $this->img
->getName(),'' ) . '</div>';
134 $wgOut->addHTML( $s );
135 if($this->img
->fromSharedDirectory
) {
136 $sharedtext="<div class=\"sharedUploadNotice\">" . wfMsg("sharedupload");
137 if($wgRepositoryBaseUrl) {
138 $sharedtext .= " ". wfMsg("shareduploadwiki",$wgRepositoryBaseUrl . urlencode($this->mTitle
->getDBkey()));
140 $sharedtext.="</div>";
141 $wgOut->addWikiText($sharedtext);
145 # Image does not exist
146 $wgOut->addWikiText( wfMsg( 'noimage', $this->getUploadUrl() ) );
150 function getUploadUrl() {
152 $uploadTitle = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
153 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img
->getName() ) );
157 function uploadNewVersionLink() {
159 $wgOut->addHTML("<div class='editExternally'>");
160 $wgOut->addWikiText( wfMsg( 'uploadnewversion', $this->getUploadUrl() ) );
161 $wgOut->addHTML("</div><br clear='both' />");
164 function externalEditorLink()
166 global $wgUser,$wgOut;
167 $sk = $wgUser->getSkin();
168 $wgOut->addHTML( '<div class="editExternally">' );
169 $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle
,
170 wfMsg( 'edit-externally' ),
171 "action=edit&externaledit=true&mode=file" ) );
172 $wgOut->addWikiText( '<div class="editExternallyHelp">' .
173 wfMsg('edit-externally-help') . '</div>' );
174 $wgOut->addHTML( '</div><br clear="all" />' );
177 function closeShowImage()
184 * If the page we've just displayed is in the "Image" namespace,
185 * we follow it with an upload history of the image and its usage.
187 function imageHistory()
189 global $wgUser, $wgOut;
191 $sk = $wgUser->getSkin();
193 $line = $this->img
->nextHistoryLine();
196 $list =& new ImageHistoryList( $sk );
197 $s = $list->beginImageHistoryList() .
198 $list->imageHistoryLine( true, $line->img_timestamp
,
199 $this->mTitle
->getDBkey(), $line->img_user
,
200 $line->img_user_text
, $line->img_size
, $line->img_description
);
202 while ( $line = $this->img
->nextHistoryLine() ) {
203 $s .= $list->imageHistoryLine( false, $line->img_timestamp
,
204 $line->oi_archive_name
, $line->img_user
,
205 $line->img_user_text
, $line->img_size
, $line->img_description
);
207 $s .= $list->endImageHistoryList();
209 $wgOut->addHTML( $s );
212 function imageLinks()
214 global $wgUser, $wgOut;
216 $wgOut->addHTML( '<h2>' . wfMsg( 'imagelinks' ) . "</h2>\n" );
218 $dbr =& wfGetDB( DB_SLAVE
);
219 $page = $dbr->tableName( 'page' );
220 $imagelinks = $dbr->tableName( 'imagelinks' );
222 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
223 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id"
224 . " LIMIT 500"; # quickie emergency brake
225 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
227 if ( 0 == $dbr->numRows( $res ) ) {
228 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
231 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
233 $sk = $wgUser->getSkin();
234 while ( $s = $dbr->fetchObject( $res ) ) {
235 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
236 $link = $sk->makeKnownLinkObj( $name, "" );
237 $wgOut->addHTML( "<li>{$link}</li>\n" );
239 $wgOut->addHTML( "</ul>\n" );
244 global $wgUser, $wgOut, $wgRequest;
246 $confirm = $wgRequest->getBool( 'wpConfirmB' );
247 $image = $wgRequest->getVal( 'image' );
248 $oldimage = $wgRequest->getVal( 'oldimage' );
250 # Only sysops can delete images. Previously ordinary users could delete
251 # old revisions, but this is no longer the case.
252 if ( !$wgUser->isAllowed('delete') ) {
253 $wgOut->sysopRequired();
256 if ( wfReadOnly() ) {
257 $wgOut->readOnlyPage();
261 # Better double-check that it hasn't been deleted yet!
262 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
263 if ( ( !is_null( $image ) )
264 && ( '' == trim( $image ) ) ) {
265 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
269 $this->img
= new Image( $this->mTitle
);
271 # Deleting old images doesn't require confirmation
272 if ( !is_null( $oldimage ) ||
$confirm ) {
273 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
276 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
281 if ( !is_null( $image ) ) {
282 $q = '&image=' . urlencode( $image );
283 } else if ( !is_null( $oldimage ) ) {
284 $q = '&oldimage=' . urlencode( $oldimage );
288 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
293 global $wgOut, $wgUser, $wgContLang, $wgRequest;
294 global $wgUseSquid, $wgInternalServer, $wgPostCommitUpdateList;
295 $fname = 'ImagePage::doDelete';
297 $reason = $wgRequest->getVal( 'wpReason' );
298 $oldimage = $wgRequest->getVal( 'oldimage' );
300 $dbw =& wfGetDB( DB_MASTER
);
302 if ( !is_null( $oldimage ) ) {
303 if ( strlen( $oldimage ) < 16 ) {
304 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
307 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
308 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
312 # Invalidate description page cache
313 $this->mTitle
->invalidateCache();
318 $wgInternalServer.wfImageArchiveUrl( $oldimage ),
319 $wgInternalServer.$this->mTitle
->getFullURL()
321 wfPurgeSquidServers($urlArr);
323 $this->doDeleteOldImage( $oldimage );
324 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
325 $deleted = $oldimage;
327 $image = $this->mTitle
->getDBkey();
328 $dest = wfImageDir( $image );
329 $archive = wfImageDir( $image );
331 # Delete the image file if it exists; due to sync problems
332 # or manual trimming sometimes the file will be missing.
333 $targetFile = "{$dest}/{$image}";
334 if( file_exists( $targetFile ) && ! @unlink
( $targetFile ) ) {
335 # If the deletion operation actually failed, bug out:
336 $wgOut->fileDeleteError( $targetFile );
339 $dbw->delete( 'image', array( 'img_name' => $image ) );
340 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
342 # Purge archive URLs from the squid
344 while ( $s = $dbw->fetchObject( $res ) ) {
345 $this->doDeleteOldImage( $s->oi_archive_name
);
346 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name
);
349 # And also the HTML of all pages using this image
350 $linksTo = $this->img
->getLinksTo();
352 $u = SquidUpdate
::newFromTitles( $linksTo, $urlArr );
353 array_push( $wgPostCommitUpdateList, $u );
356 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
358 # Image itself is now gone, and database is cleaned.
359 # Now we remove the image description page.
361 $article = new Article( $this->mTitle
);
362 $article->doDeleteArticle( $reason ); # ignore errors
364 # Invalidate parser cache and client cache for pages using this image
365 # This is left until relatively late to reduce lock time
366 Title
::touchArray( $linksTo );
368 /* Delete thumbnails and refresh image metadata cache */
369 $this->img
->purgeCache();
375 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
376 $wgOut->setRobotpolicy( 'noindex,nofollow' );
378 $sk = $wgUser->getSkin();
379 $loglink = $sk->makeKnownLinkObj(
380 Title
::makeTitle( NS_SPECIAL
, 'Log/delete' ),
381 wfMsg( 'deletionlog' ) );
383 $text = wfMsg( 'deletedtext', $deleted, $loglink );
385 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
386 $wgOut->returnToMain( false, $this->mTitle
->getPrefixedText() );
389 function doDeleteOldImage( $oldimage )
393 $name = substr( $oldimage, 15 );
394 $archive = wfImageArchiveDir( $name );
396 # Delete the image if it exists. Sometimes the file will be missing
397 # due to manual intervention or weird sync problems; treat that
398 # condition gracefully and continue to delete the database entry.
399 # Also some records may end up with an empty oi_archive_name field
400 # if the original file was missing when a new upload was made;
401 # don't try to delete the directory then!
403 $targetFile = "{$archive}/{$oldimage}";
404 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink
( $targetFile ) ) {
405 # If we actually have a file and can't delete it, throw an error.
406 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
409 $log = new LogPage( 'delete' );
410 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
416 global $wgOut, $wgRequest, $wgUser;
417 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
419 $oldimage = $wgRequest->getText( 'oldimage' );
420 if ( strlen( $oldimage ) < 16 ) {
421 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
424 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
425 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
429 if ( wfReadOnly() ) {
430 $wgOut->readOnlyPage();
433 if( $wgUser->isAnon() ) {
434 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
437 if ( ! $this->mTitle
->userCanEdit() ) {
438 $wgOut->sysopRequired();
441 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
442 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
445 $name = substr( $oldimage, 15 );
447 $dest = wfImageDir( $name );
448 $archive = wfImageArchiveDir( $name );
449 $curfile = "{$dest}/{$name}";
451 if ( ! is_file( $curfile ) ) {
452 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
455 $oldver = wfTimestampNow() . "!{$name}";
457 $dbr =& wfGetDB( DB_SLAVE
);
458 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
460 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
461 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
464 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
465 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
468 # Record upload and update metadata cache
469 $img = Image
::newFromName( $name );
470 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
472 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
473 $wgOut->setRobotpolicy( 'noindex,nofollow' );
474 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
476 $descTitle = $img->getTitle();
477 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
485 class ImageHistoryList
{
486 function ImageHistoryList( &$skin ) {
487 $this->skin
=& $skin;
490 function beginImageHistoryList() {
491 $s = "\n<h2>" . wfMsg( 'imghistory' ) . "</h2>\n" .
492 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
496 function endImageHistoryList() {
501 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
502 global $wgUser, $wgLang, $wgContLang, $wgTitle;
504 $datetime = $wgLang->timeanddate( $timestamp, true );
505 $del = wfMsg( 'deleteimg' );
506 $delall = wfMsg( 'deleteimgcompletely' );
507 $cur = wfMsg( 'cur' );
510 $url = Image
::imageUrl( $img );
512 if ( $wgUser->isAllowed('delete') ) {
513 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
515 $style = $this->skin
->getInternalLinkAttributes( $link, $delall );
517 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
522 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
523 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
524 $token = urlencode( $wgUser->editToken( $img ) );
525 $rlink = $this->skin
->makeKnownLinkObj( $wgTitle,
526 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
527 urlencode( $img ) . "&wpEditToken=$token" );
528 $dlink = $this->skin
->makeKnownLinkObj( $wgTitle,
529 $del, 'action=delete&oldimage=' . urlencode( $img ) .
530 "&wpEditToken=$token" );
532 # Having live active links for non-logged in users
533 # means that bots and spiders crawling our site can
534 # inadvertently change content. Baaaad idea.
535 $rlink = wfMsg( 'revertimg' );
540 $userlink = $usertext;
542 $userlink = $this->skin
->makeLinkObj(
543 Title
::makeTitle( NS_USER
, $usertext ),
546 $nbytes = wfMsg( 'nbytes', $size );
547 $style = $this->skin
->getInternalLinkAttributes( $url, $datetime );
549 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
550 . " . . {$userlink} ({$nbytes})";
552 $s .= $this->skin
->commentBlock( $description, $wgTitle );