* Show exif data after the image not before it.
[lhc/web/wiklou.git] / includes / ImagePage.php
1 <?php
2 /**
3 * @package MediaWiki
4 */
5
6 /**
7 *
8 */
9 if( !defined( 'MEDIAWIKI' ) )
10 die();
11
12 require_once( 'Image.php' );
13
14 /**
15 * Special handling for image description pages
16 * @package MediaWiki
17 */
18 class ImagePage extends Article {
19
20 /* private */ var $img; // Image object this page is shown for
21
22 function view() {
23 global $wgUseExternalEditor, $wgOut ;
24
25 $this->img = new Image( $this->mTitle );
26
27 if( $this->mTitle->getNamespace() == NS_IMAGE ) {
28 $this->openShowImage();
29 if ( $this->img->exists() ) $this->showEXIFdata();
30
31 # No need to display noarticletext, we use our own message, output in openShowImage()
32 if ( $this->getID() ) {
33 Article::view();
34 } else {
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();
40 $this->viewUpdates();
41 }
42
43 if ( $this->img->exists() ) {
44 $this->uploadNewVersionLink();
45 if ( $wgUseExternalEditor && $this->img->exists() ) {
46 $this->externalEditorLink();
47 }
48 }
49 $this->closeShowImage();
50 $this->imageHistory();
51 $this->imageLinks();
52 } else {
53 Article::view();
54 }
55 }
56
57 function showEXIFdata()
58 {
59 global $wgOut , $wgShowEXIF ;
60 if ( ! $wgShowEXIF ) return ;
61
62 # Get the EXIF data
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
66
67 # Create the table
68 $r = "<table border='1' cellspacing='0' cellpadding='0' align='right'>" ;
69 $r .= "<caption>EXIF data</caption>" ;
70 foreach ( $exif AS $k => $v ) {
71 $r .= "<tr>" ;
72 $v = str_replace ( "\0" , "" , $v ) ;
73 $r .= "<th><small>" . htmlspecialchars ( $k ) . "</small></th>" ;
74 $r .= "<td><small>" . htmlspecialchars ( $v ) . "</small></td>" ;
75 $r .= "</tr>" ;
76 }
77 $r .= "</table>" ;
78 $wgOut->addHTML ( $r ) ;
79 }
80
81 function openShowImage()
82 {
83 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
84 $wgUseImageResize, $wgRepositoryBaseUrl,
85 $wgUseExternalEditor, $wgServer;
86 $full_url = $this->img->getViewURL();
87 $anchoropen = '';
88 $anchorclose = '';
89
90 if( $wgUser->getOption( 'imagesize' ) == '' ) {
91 $sizeSel = User::getDefaultOption( 'imagesize' );
92 } else {
93 $sizeSel = IntVal( $wgUser->getOption( 'imagesize' ) );
94 }
95 if( !isset( $wgImageLimits[$sizeSel] ) ) {
96 $sizeSel = User::getDefaultOption( 'imagesize' );
97 }
98 $max = $wgImageLimits[$sizeSel];
99 $maxWidth = $max[0];
100 $maxHeight = $max[1];
101 $sk = $wgUser->getSkin();
102
103 if ( $this->img->exists() ) {
104 if ( $this->img->getType() != '' ) {
105 # image
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 );
111 $width = $maxWidth;
112 }
113 if ( $height > $maxHeight ) {
114 $width = floor( $width * $maxHeight / $height );
115 $height = $maxHeight;
116 }
117 if ( $width != $this->img->getWidth() || $height != $this->img->getHeight() ) {
118 if( $wgUseImageResize ) {
119 $thumbnail = $this->img->getThumbnail( $width );
120 $url = $thumbnail->getUrl();
121 } else {
122 # No resize ability? Show the full image, but scale
123 # it down in the browser so it fits on the page.
124 $url = $full_url;
125 }
126 $anchoropen = "<a href=\"{$full_url}\">";
127 $anchorclose = "</a><br />\n$anchoropen{$msg}</a>";
128 } else {
129 $url = $full_url;
130 }
131 $s = '<div class="fullImageLink">' . $anchoropen .
132 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
133 htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>';
134 } else {
135 $s = "<div class=\"fullMedia\">" . $sk->makeMediaLink( $this->img->getName(),'' ) . '</div>';
136 }
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()));
142 }
143 $sharedtext.="</div>";
144 $wgOut->addWikiText($sharedtext);
145 }
146
147 } else {
148 # Image does not exist
149 $wgOut->addWikiText( wfMsg( 'noimage', $this->getUploadUrl() ) );
150 }
151 }
152
153 function getUploadUrl() {
154 global $wgServer;
155 $uploadTitle = Title::makeTitle( NS_SPECIAL, 'Upload' );
156 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img->getName() ) );
157 }
158
159
160 function uploadNewVersionLink() {
161 global $wgOut;
162 $wgOut->addWikiText( wfMsg( 'uploadnewversion', $this->getUploadUrl() ) );
163 }
164
165 function externalEditorLink()
166 {
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" />' );
176 }
177
178 function closeShowImage()
179 {
180 # For overloading
181
182 }
183
184 /**
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.
187 */
188 function imageHistory()
189 {
190 global $wgUser, $wgOut;
191
192 $sk = $wgUser->getSkin();
193
194 $line = $this->img->nextHistoryLine();
195
196 if ( $line ) {
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 );
202
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 );
207 }
208 $s .= $list->endImageHistoryList();
209 } else { $s=''; }
210 $wgOut->addHTML( $s );
211 }
212
213 function imageLinks()
214 {
215 global $wgUser, $wgOut;
216
217 $wgOut->addHTML( '<h2>' . wfMsg( 'imagelinks' ) . "</h2>\n" );
218
219 $dbr =& wfGetDB( DB_SLAVE );
220 $page = $dbr->tableName( 'page' );
221 $imagelinks = $dbr->tableName( 'imagelinks' );
222
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" );
227
228 if ( 0 == $dbr->numRows( $res ) ) {
229 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
230 return;
231 }
232 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
233
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" );
239 }
240 $wgOut->addHTML( "</ul>\n" );
241 }
242
243 function delete()
244 {
245 global $wgUser, $wgOut, $wgRequest;
246
247 $confirm = $wgRequest->getBool( 'wpConfirmB' );
248 $image = $wgRequest->getVal( 'image' );
249 $oldimage = $wgRequest->getVal( 'oldimage' );
250
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();
255 return;
256 }
257 if ( wfReadOnly() ) {
258 $wgOut->readOnlyPage();
259 return;
260 }
261
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' ) );
267 return;
268 }
269
270 $this->img = new Image( $this->mTitle );
271
272 # Deleting old images doesn't require confirmation
273 if ( !is_null( $oldimage ) || $confirm ) {
274 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
275 $this->doDelete();
276 } else {
277 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
278 }
279 return;
280 }
281
282 if ( !is_null( $image ) ) {
283 $q = '&image=' . urlencode( $image );
284 } else if ( !is_null( $oldimage ) ) {
285 $q = '&oldimage=' . urlencode( $oldimage );
286 } else {
287 $q = '';
288 }
289 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
290 }
291
292 function doDelete()
293 {
294 global $wgOut, $wgUser, $wgContLang, $wgRequest;
295 global $wgUseSquid, $wgInternalServer, $wgPostCommitUpdateList;
296 $fname = 'ImagePage::doDelete';
297
298 $reason = $wgRequest->getVal( 'wpReason' );
299 $oldimage = $wgRequest->getVal( 'oldimage' );
300
301 $dbw =& wfGetDB( DB_MASTER );
302
303 if ( !is_null( $oldimage ) ) {
304 if ( strlen( $oldimage ) < 16 ) {
305 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
306 return;
307 }
308 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
309 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
310 return;
311 }
312
313 # Invalidate description page cache
314 $this->mTitle->invalidateCache();
315
316 # Squid purging
317 if ( $wgUseSquid ) {
318 $urlArr = Array(
319 $wgInternalServer.wfImageArchiveUrl( $oldimage ),
320 $wgInternalServer.$this->mTitle->getFullURL()
321 );
322 wfPurgeSquidServers($urlArr);
323 }
324 $this->doDeleteOldImage( $oldimage );
325 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
326 $deleted = $oldimage;
327 } else {
328 $image = $this->mTitle->getDBkey();
329 $dest = wfImageDir( $image );
330 $archive = wfImageDir( $image );
331
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 );
338 return;
339 }
340 $dbw->delete( 'image', array( 'img_name' => $image ) );
341 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
342
343 # Purge archive URLs from the squid
344 $urlArr = Array();
345 while ( $s = $dbw->fetchObject( $res ) ) {
346 $this->doDeleteOldImage( $s->oi_archive_name );
347 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name );
348 }
349
350 # And also the HTML of all pages using this image
351 $linksTo = $this->img->getLinksTo();
352 if ( $wgUseSquid ) {
353 $u = SquidUpdate::newFromTitles( $linksTo, $urlArr );
354 array_push( $wgPostCommitUpdateList, $u );
355 }
356
357 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
358
359 # Image itself is now gone, and database is cleaned.
360 # Now we remove the image description page.
361
362 $article = new Article( $this->mTitle );
363 $article->doDeleteArticle( $reason ); # ignore errors
364
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 );
368
369 /* Delete thumbnails and refresh image metadata cache */
370 $this->img->purgeCache();
371
372
373 $deleted = $image;
374 }
375
376 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
377 $wgOut->setRobotpolicy( 'noindex,nofollow' );
378
379 $sk = $wgUser->getSkin();
380 $loglink = $sk->makeKnownLinkObj(
381 Title::makeTitle( NS_SPECIAL, 'Log/delete' ),
382 wfMsg( 'deletionlog' ) );
383
384 $text = wfMsg( 'deletedtext', $deleted, $loglink );
385
386 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
387 $wgOut->returnToMain( false, $this->mTitle->getPrefixedText() );
388 }
389
390 function doDeleteOldImage( $oldimage )
391 {
392 global $wgOut;
393
394 $name = substr( $oldimage, 15 );
395 $archive = wfImageArchiveDir( $name );
396
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!
403 #
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}" );
408 } else {
409 # Log the deletion
410 $log = new LogPage( 'delete' );
411 $log->addEntry( 'delete', $this->mTitle, wfMsg('deletedrevision',$oldimage) );
412 }
413 }
414
415 function revert()
416 {
417 global $wgOut, $wgRequest, $wgUser;
418 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
419
420 $oldimage = $wgRequest->getText( 'oldimage' );
421 if ( strlen( $oldimage ) < 16 ) {
422 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
423 return;
424 }
425 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
426 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
427 return;
428 }
429
430 if ( wfReadOnly() ) {
431 $wgOut->readOnlyPage();
432 return;
433 }
434 if( $wgUser->isAnon() ) {
435 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
436 return;
437 }
438 if ( ! $this->mTitle->userCanEdit() ) {
439 $wgOut->sysopRequired();
440 return;
441 }
442 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
443 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
444 return;
445 }
446 $name = substr( $oldimage, 15 );
447
448 $dest = wfImageDir( $name );
449 $archive = wfImageArchiveDir( $name );
450 $curfile = "{$dest}/{$name}";
451
452 if ( ! is_file( $curfile ) ) {
453 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
454 return;
455 }
456 $oldver = wfTimestampNow() . "!{$name}";
457
458 $dbr =& wfGetDB( DB_SLAVE );
459 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
460
461 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
462 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
463 return;
464 }
465 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
466 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
467 }
468
469 # Record upload and update metadata cache
470 $img = Image::newFromName( $name );
471 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
472
473 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
474 $wgOut->setRobotpolicy( 'noindex,nofollow' );
475 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
476
477 $descTitle = $img->getTitle();
478 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
479 }
480 }
481
482 /**
483 * @todo document
484 * @package MediaWiki
485 */
486 class ImageHistoryList {
487 function ImageHistoryList( &$skin ) {
488 $this->skin =& $skin;
489 }
490
491 function beginImageHistoryList() {
492 $s = "\n<h2>" . wfMsg( 'imghistory' ) . "</h2>\n" .
493 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
494 return $s;
495 }
496
497 function endImageHistoryList() {
498 $s = "</ul>\n";
499 return $s;
500 }
501
502 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
503 global $wgUser, $wgLang, $wgContLang, $wgTitle;
504
505 $datetime = $wgLang->timeanddate( $timestamp, true );
506 $del = wfMsg( 'deleteimg' );
507 $delall = wfMsg( 'deleteimgcompletely' );
508 $cur = wfMsg( 'cur' );
509
510 if ( $iscur ) {
511 $url = Image::imageUrl( $img );
512 $rlink = $cur;
513 if ( $wgUser->isAllowed('delete') ) {
514 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
515 '&action=delete' );
516 $style = $this->skin->getInternalLinkAttributes( $link, $delall );
517
518 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
519 } else {
520 $dlink = $del;
521 }
522 } else {
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" );
532 } else {
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' );
537 $dlink = $del;
538 }
539 }
540 if ( 0 == $user ) {
541 $userlink = $usertext;
542 } else {
543 $userlink = $this->skin->makeLinkObj(
544 Title::makeTitle( NS_USER, $usertext ),
545 $usertext );
546 }
547 $nbytes = wfMsg( 'nbytes', $size );
548 $style = $this->skin->getInternalLinkAttributes( $url, $datetime );
549
550 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
551 . " . . {$userlink} ({$nbytes})";
552
553 $s .= $this->skin->commentBlock( $description, $wgTitle );
554 $s .= "</li>\n";
555 return $s;
556 }
557
558 }
559
560
561 ?>