9 if( !defined( 'MEDIAWIKI' ) )
12 require_once( 'Image.php' );
15 require_once ( 'exifReader.inc' ) ;
19 * Special handling for image description pages
22 class ImagePage
extends Article
{
24 /* private */ var $img; // Image object this page is shown for
27 global $wgUseExternalEditor, $wgOut ;
29 $this->img
= new Image( $this->mTitle
);
31 if( $this->mTitle
->getNamespace() == NS_IMAGE
) {
32 if ( $this->img
->exists() ) $this->showEXIFdata();
33 $this->openShowImage();
35 # No need to display noarticletext, we use our own message, output in openShowImage()
36 if ( $this->getID() ) {
39 # Just need to set the right headers
40 $wgOut->setArticleFlag( true );
41 $wgOut->setRobotpolicy( 'index,follow' );
42 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
43 $wgOut->addMetaTags();
47 if ( $this->img
->exists() ) {
48 $this->uploadNewVersionLink();
49 if ( $wgUseExternalEditor && $this->img
->exists() ) {
50 $this->externalEditorLink();
53 $this->closeShowImage();
54 $this->imageHistory();
61 function showEXIFdata()
63 global $wgOut , $wgShowEXIF ;
64 if ( ! $wgShowEXIF ) return ;
65 $file = $this->img
->getImagePath () ;
66 $per = new phpExifReader ( $file ) ;
67 $per->processFile () ;
69 $r = "<table border='1' cellspacing='0' cellpadding='0' align='right'>" ;
70 $r .= "<caption>EXIF data</caption>" ;
71 $a = $per->getImageInfo() ;
72 if ( count ( $a ) == 0 ) return ; # No EXIF data
73 unset ( $a["FileName"] ) ;
74 unset ( $a["Thumbnail"] ) ;
75 foreach ( $a AS $k => $v ) {
77 $v = str_replace ( "\0" , "" , $v ) ;
78 $r .= "<th><small>" . htmlspecialchars ( $k ) . "</small></th>" ;
79 $r .= "<td><small>" . htmlspecialchars ( $v ) . "</small></td>" ;
83 $wgOut->addHTML ( $r ) ;
86 function openShowImage()
88 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
89 $wgUseImageResize, $wgRepositoryBaseUrl,
90 $wgUseExternalEditor, $wgServer;
91 $full_url = $this->img
->getViewURL();
95 if( $wgUser->getOption( 'imagesize' ) == '' ) {
96 $sizeSel = User
::getDefaultOption( 'imagesize' );
98 $sizeSel = IntVal( $wgUser->getOption( 'imagesize' ) );
100 if( !isset( $wgImageLimits[$sizeSel] ) ) {
101 $sizeSel = User
::getDefaultOption( 'imagesize' );
103 $max = $wgImageLimits[$sizeSel];
105 $maxHeight = $max[1];
106 $sk = $wgUser->getSkin();
108 if ( $this->img
->exists() ) {
109 if ( $this->img
->getType() != '' ) {
111 $width = $this->img
->getWidth();
112 $height = $this->img
->getHeight();
113 $msg = wfMsg('showbigimage', $width, $height, intval( $this->img
->getSize()/1024 ) );
114 if ( $width > $maxWidth ) {
115 $height = floor( $height * $maxWidth / $width );
118 if ( $height > $maxHeight ) {
119 $width = floor( $width * $maxHeight / $height );
120 $height = $maxHeight;
122 if ( $width != $this->img
->getWidth() ||
$height != $this->img
->getHeight() ) {
123 if( $wgUseImageResize ) {
124 $thumbnail = $this->img
->getThumbnail( $width );
125 $url = $thumbnail->getUrl();
127 # No resize ability? Show the full image, but scale
128 # it down in the browser so it fits on the page.
131 $anchoropen = "<a href=\"{$full_url}\">";
132 $anchorclose = "</a><br />\n$anchoropen{$msg}</a>";
136 $s = '<div class="fullImageLink">' . $anchoropen .
137 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
138 htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>';
140 $s = "<div class=\"fullMedia\">" . $sk->makeMediaLink( $this->img
->getName(),'' ) . '</div>';
142 $wgOut->addHTML( $s );
143 if($this->img
->fromSharedDirectory
) {
144 $sharedtext="<div class=\"sharedUploadNotice\">" . wfMsg("sharedupload");
145 if($wgRepositoryBaseUrl) {
146 $sharedtext .= " ". wfMsg("shareduploadwiki",$wgRepositoryBaseUrl . urlencode($this->mTitle
->getDBkey()));
148 $sharedtext.="</div>";
149 $wgOut->addWikiText($sharedtext);
153 # Image does not exist
154 $wgOut->addWikiText( wfMsg( 'noimage', $this->getUploadUrl() ) );
158 function getUploadUrl() {
160 $uploadTitle = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
161 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img
->getName() ) );
165 function uploadNewVersionLink() {
167 $wgOut->addWikiText( wfMsg( 'uploadnewversion', $this->getUploadUrl() ) );
170 function externalEditorLink()
172 global $wgUser,$wgOut;
173 $sk = $wgUser->getSkin();
174 $wgOut->addHTML( '<div class="editExternally">' );
175 $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle
,
176 wfMsg( 'edit-externally' ),
177 "action=edit&externaledit=true&mode=file" ) );
178 $wgOut->addWikiText( '<div class="editExternallyHelp">' .
179 wfMsg('edit-externally-help') . '</div>' );
180 $wgOut->addHTML( '</div><br clear="both" />' );
183 function closeShowImage()
190 * If the page we've just displayed is in the "Image" namespace,
191 * we follow it with an upload history of the image and its usage.
193 function imageHistory()
195 global $wgUser, $wgOut;
197 $sk = $wgUser->getSkin();
199 $line = $this->img
->nextHistoryLine();
202 $list =& new ImageHistoryList( $sk );
203 $s = $list->beginImageHistoryList() .
204 $list->imageHistoryLine( true, $line->img_timestamp
,
205 $this->mTitle
->getDBkey(), $line->img_user
,
206 $line->img_user_text
, $line->img_size
, $line->img_description
);
208 while ( $line = $this->img
->nextHistoryLine() ) {
209 $s .= $list->imageHistoryLine( false, $line->img_timestamp
,
210 $line->oi_archive_name
, $line->img_user
,
211 $line->img_user_text
, $line->img_size
, $line->img_description
);
213 $s .= $list->endImageHistoryList();
215 $wgOut->addHTML( $s );
218 function imageLinks()
220 global $wgUser, $wgOut;
222 $wgOut->addHTML( '<h2>' . wfMsg( 'imagelinks' ) . "</h2>\n" );
224 $dbr =& wfGetDB( DB_SLAVE
);
225 $page = $dbr->tableName( 'page' );
226 $imagelinks = $dbr->tableName( 'imagelinks' );
228 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
229 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id"
230 . " LIMIT 500"; # quickie emergency brake
231 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
233 if ( 0 == $dbr->numRows( $res ) ) {
234 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
237 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
239 $sk = $wgUser->getSkin();
240 while ( $s = $dbr->fetchObject( $res ) ) {
241 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
242 $link = $sk->makeKnownLinkObj( $name, "" );
243 $wgOut->addHTML( "<li>{$link}</li>\n" );
245 $wgOut->addHTML( "</ul>\n" );
250 global $wgUser, $wgOut, $wgRequest;
252 $confirm = $wgRequest->getBool( 'wpConfirmB' );
253 $image = $wgRequest->getVal( 'image' );
254 $oldimage = $wgRequest->getVal( 'oldimage' );
256 # Only sysops can delete images. Previously ordinary users could delete
257 # old revisions, but this is no longer the case.
258 if ( !$wgUser->isAllowed('delete') ) {
259 $wgOut->sysopRequired();
262 if ( wfReadOnly() ) {
263 $wgOut->readOnlyPage();
267 # Better double-check that it hasn't been deleted yet!
268 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
269 if ( ( !is_null( $image ) )
270 && ( '' == trim( $image ) ) ) {
271 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
275 $this->img
= new Image( $this->mTitle
);
277 # Deleting old images doesn't require confirmation
278 if ( !is_null( $oldimage ) ||
$confirm ) {
279 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
282 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
287 if ( !is_null( $image ) ) {
288 $q = '&image=' . urlencode( $image );
289 } else if ( !is_null( $oldimage ) ) {
290 $q = '&oldimage=' . urlencode( $oldimage );
294 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
299 global $wgOut, $wgUser, $wgContLang, $wgRequest;
300 global $wgUseSquid, $wgInternalServer, $wgPostCommitUpdateList;
301 $fname = 'ImagePage::doDelete';
303 $reason = $wgRequest->getVal( 'wpReason' );
304 $oldimage = $wgRequest->getVal( 'oldimage' );
306 $dbw =& wfGetDB( DB_MASTER
);
308 if ( !is_null( $oldimage ) ) {
309 if ( strlen( $oldimage ) < 16 ) {
310 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
313 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
314 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
318 # Invalidate description page cache
319 $this->mTitle
->invalidateCache();
324 $wgInternalServer.wfImageArchiveUrl( $oldimage ),
325 $wgInternalServer.$this->mTitle
->getFullURL()
327 wfPurgeSquidServers($urlArr);
329 $this->doDeleteOldImage( $oldimage );
330 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
331 $deleted = $oldimage;
333 $image = $this->mTitle
->getDBkey();
334 $dest = wfImageDir( $image );
335 $archive = wfImageDir( $image );
337 # Delete the image file if it exists; due to sync problems
338 # or manual trimming sometimes the file will be missing.
339 $targetFile = "{$dest}/{$image}";
340 if( file_exists( $targetFile ) && ! @unlink
( $targetFile ) ) {
341 # If the deletion operation actually failed, bug out:
342 $wgOut->fileDeleteError( $targetFile );
345 $dbw->delete( 'image', array( 'img_name' => $image ) );
346 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
348 # Purge archive URLs from the squid
350 while ( $s = $dbw->fetchObject( $res ) ) {
351 $this->doDeleteOldImage( $s->oi_archive_name
);
352 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name
);
355 # And also the HTML of all pages using this image
356 $linksTo = $this->img
->getLinksTo();
358 $u = SquidUpdate
::newFromTitles( $linksTo, $urlArr );
359 array_push( $wgPostCommitUpdateList, $u );
362 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
364 # Image itself is now gone, and database is cleaned.
365 # Now we remove the image description page.
367 $article = new Article( $this->mTitle
);
368 $article->doDeleteArticle( $reason ); # ignore errors
370 # Invalidate parser cache and client cache for pages using this image
371 # This is left until relatively late to reduce lock time
372 Title
::touchArray( $linksTo );
374 /* Delete thumbnails and refresh image metadata cache */
375 $this->img
->purgeCache();
381 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
382 $wgOut->setRobotpolicy( 'noindex,nofollow' );
384 $sk = $wgUser->getSkin();
385 $loglink = $sk->makeKnownLinkObj(
386 Title
::makeTitle( NS_SPECIAL
, 'Log/delete' ),
387 wfMsg( 'deletionlog' ) );
389 $text = wfMsg( 'deletedtext', $deleted, $loglink );
391 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
392 $wgOut->returnToMain( false, $this->mTitle
->getPrefixedText() );
395 function doDeleteOldImage( $oldimage )
399 $name = substr( $oldimage, 15 );
400 $archive = wfImageArchiveDir( $name );
402 # Delete the image if it exists. Sometimes the file will be missing
403 # due to manual intervention or weird sync problems; treat that
404 # condition gracefully and continue to delete the database entry.
405 # Also some records may end up with an empty oi_archive_name field
406 # if the original file was missing when a new upload was made;
407 # don't try to delete the directory then!
409 $targetFile = "{$archive}/{$oldimage}";
410 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink
( $targetFile ) ) {
411 # If we actually have a file and can't delete it, throw an error.
412 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
415 $log = new LogPage( 'delete' );
416 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
422 global $wgOut, $wgRequest, $wgUser;
423 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
425 $oldimage = $wgRequest->getText( 'oldimage' );
426 if ( strlen( $oldimage ) < 16 ) {
427 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
430 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
431 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
435 if ( wfReadOnly() ) {
436 $wgOut->readOnlyPage();
439 if( $wgUser->isAnon() ) {
440 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
443 if ( ! $this->mTitle
->userCanEdit() ) {
444 $wgOut->sysopRequired();
447 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
448 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
451 $name = substr( $oldimage, 15 );
453 $dest = wfImageDir( $name );
454 $archive = wfImageArchiveDir( $name );
455 $curfile = "{$dest}/{$name}";
457 if ( ! is_file( $curfile ) ) {
458 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
461 $oldver = wfTimestampNow() . "!{$name}";
463 $dbr =& wfGetDB( DB_SLAVE
);
464 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
466 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
467 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
470 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
471 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
474 # Record upload and update metadata cache
475 $img = Image
::newFromName( $name );
476 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
478 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
479 $wgOut->setRobotpolicy( 'noindex,nofollow' );
480 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
482 $descTitle = $img->getTitle();
483 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
491 class ImageHistoryList
{
492 function ImageHistoryList( &$skin ) {
493 $this->skin
=& $skin;
496 function beginImageHistoryList() {
497 $s = "\n<h2>" . wfMsg( 'imghistory' ) . "</h2>\n" .
498 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
502 function endImageHistoryList() {
507 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
508 global $wgUser, $wgLang, $wgContLang, $wgTitle;
510 $datetime = $wgLang->timeanddate( $timestamp, true );
511 $del = wfMsg( 'deleteimg' );
512 $delall = wfMsg( 'deleteimgcompletely' );
513 $cur = wfMsg( 'cur' );
516 $url = Image
::imageUrl( $img );
518 if ( $wgUser->isAllowed('delete') ) {
519 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
521 $style = $this->skin
->getInternalLinkAttributes( $link, $delall );
523 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
528 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
529 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
530 $token = urlencode( $wgUser->editToken( $img ) );
531 $rlink = $this->skin
->makeKnownLinkObj( $wgTitle,
532 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
533 urlencode( $img ) . "&wpEditToken=$token" );
534 $dlink = $this->skin
->makeKnownLinkObj( $wgTitle,
535 $del, 'action=delete&oldimage=' . urlencode( $img ) .
536 "&wpEditToken=$token" );
538 # Having live active links for non-logged in users
539 # means that bots and spiders crawling our site can
540 # inadvertently change content. Baaaad idea.
541 $rlink = wfMsg( 'revertimg' );
546 $userlink = $usertext;
548 $userlink = $this->skin
->makeLinkObj(
549 Title
::makeTitle( NS_USER
, $usertext ),
552 $nbytes = wfMsg( 'nbytes', $size );
553 $style = $this->skin
->getInternalLinkAttributes( $url, $datetime );
555 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
556 . " . . {$userlink} ({$nbytes})";
558 $s .= $this->skin
->commentBlock( $description, $wgTitle );