* Added CSS styles to each Exif tag to make them styleable.
[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, $wgShowEXIF;
24
25 $this->img = new Image( $this->mTitle );
26
27 if( $this->mTitle->getNamespace() == NS_IMAGE ) {
28 if ($wgShowEXIF && $this->img->exists()) {
29 $exif = $this->img->getExifData();
30 $showmeta = count($exif) ? true : false;
31 } else {
32 $exif = false;
33 $showmeta = false;
34 }
35
36 if ($this->img->exists())
37 $wgOut->addHTML($this->showTOC($showmeta));
38
39 $this->openShowImage();
40 if ($exif)
41 $wgOut->addWikiText($this->makeMetadataTable($exif));
42
43 # No need to display noarticletext, we use our own message, output in openShowImage()
44 if( $this->getID() ) {
45 Article::view();
46 } else {
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();
52 $this->viewUpdates();
53 }
54
55 $this->closeShowImage();
56 $this->imageHistory();
57 $this->imageLinks();
58 } else {
59 Article::view();
60 }
61 }
62
63 /**
64 * Create the TOC
65 *
66 * @access private
67 *
68 * @param bool $metadata Whether or not to show the metadata link
69 * @return string
70 */
71 function showTOC( $metadata ) {
72 global $wgLang;
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>
78 </ul>';
79 return $r;
80 }
81
82 /**
83 * Make a table with metadata to be shown in the output page.
84 *
85 * @access private
86 *
87 * @param array $exif The array containing the EXIF data
88 * @return string
89 */
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";
97 $r .= "|-\n";
98 }
99 return substr($r, 0, -3) . '|}';
100 }
101
102 function openShowImage()
103 {
104 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
105 $wgUseImageResize, $wgRepositoryBaseUrl,
106 $wgUseExternalEditor, $wgServer;
107 $full_url = $this->img->getViewURL();
108 $anchoropen = '';
109 $anchorclose = '';
110
111 if( $wgUser->getOption( 'imagesize' ) == '' ) {
112 $sizeSel = User::getDefaultOption( 'imagesize' );
113 } else {
114 $sizeSel = IntVal( $wgUser->getOption( 'imagesize' ) );
115 }
116 if( !isset( $wgImageLimits[$sizeSel] ) ) {
117 $sizeSel = User::getDefaultOption( 'imagesize' );
118 }
119 $max = $wgImageLimits[$sizeSel];
120 $maxWidth = $max[0];
121 $maxHeight = $max[1];
122 $sk = $wgUser->getSkin();
123
124 if ( $this->img->exists() ) {
125 if ( $this->img->getType() ) {
126 # image
127 $width = $this->img->getWidth();
128 $height = $this->img->getHeight();
129 # "Download high res version" link below the image
130 $msg = wfMsg('showbigimage', $width, $height, intval( $this->img->getSize()/1024 ) );
131 if ( $width > $maxWidth ) {
132 $height = floor( $height * $maxWidth / $width );
133 $width = $maxWidth;
134 }
135 if ( $height > $maxHeight ) {
136 $width = floor( $width * $maxHeight / $height );
137 $height = $maxHeight;
138 }
139 if ( $width != $this->img->getWidth() || $height != $this->img->getHeight() ) {
140 if( $wgUseImageResize ) {
141 $thumbnail = $this->img->getThumbnail( $width );
142 $url = $thumbnail->getUrl();
143 } else {
144 # No resize ability? Show the full image, but scale
145 # it down in the browser so it fits on the page.
146 $url = $full_url;
147 }
148 $anchoropen = "<a href=\"{$full_url}\">";
149 $anchorclose = "</a><br />\n$anchoropen{$msg}</a>";
150 } else {
151 $url = $full_url;
152 }
153 $s = '<div class="fullImageLink" id="file">' . $anchoropen .
154 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
155 htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>';
156 } else {
157 $s = "<div class=\"fullMedia\">" . $sk->makeMediaLink( $this->img->getName(),'' ) . '</div>';
158 }
159 $wgOut->addHTML( $s );
160 if($this->img->fromSharedDirectory) {
161 $sharedtext="<div class=\"sharedUploadNotice\">" . wfMsg("sharedupload");
162 if($wgRepositoryBaseUrl) {
163 $sharedtext .= " ". wfMsg("shareduploadwiki",$wgRepositoryBaseUrl . urlencode($this->mTitle->getDBkey()));
164 }
165 $sharedtext.="</div>";
166 $wgOut->addWikiText($sharedtext);
167 }
168
169 } else {
170 # Image does not exist
171 $wgOut->addWikiText( wfMsg( 'noimage', $this->getUploadUrl() ) );
172 }
173 }
174
175 function getUploadUrl() {
176 global $wgServer;
177 $uploadTitle = Title::makeTitle( NS_SPECIAL, 'Upload' );
178 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img->getName() ) );
179 }
180
181
182 function uploadLinksBox()
183 {
184 global $wgUser,$wgOut;
185 $sk = $wgUser->getSkin();
186 $wgOut->addHTML( '<br /><ul><li>' );
187 $wgOut->addWikiText( '<div>'. wfMsg( 'uploadnewversion', $this->getUploadUrl() ) .'</div>' );
188 $wgOut->addHTML( '</li><li>' );
189 $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle,
190 wfMsg( 'edit-externally' ), "action=edit&externaledit=true&mode=file" ) );
191 $wgOut->addWikiText( '<div>' . wfMsg('edit-externally-help') . '</div>' );
192 $wgOut->addHTML( '</li></ul>' );
193 }
194
195 function closeShowImage()
196 {
197 # For overloading
198
199 }
200
201 /**
202 * If the page we've just displayed is in the "Image" namespace,
203 * we follow it with an upload history of the image and its usage.
204 */
205 function imageHistory()
206 {
207 global $wgUser, $wgOut, $wgUseExternalEditor;
208
209 $sk = $wgUser->getSkin();
210
211 $line = $this->img->nextHistoryLine();
212
213 if ( $line ) {
214 $list =& new ImageHistoryList( $sk );
215 $s = $list->beginImageHistoryList() .
216 $list->imageHistoryLine( true, $line->img_timestamp,
217 $this->mTitle->getDBkey(), $line->img_user,
218 $line->img_user_text, $line->img_size, $line->img_description );
219
220 while ( $line = $this->img->nextHistoryLine() ) {
221 $s .= $list->imageHistoryLine( false, $line->img_timestamp,
222 $line->oi_archive_name, $line->img_user,
223 $line->img_user_text, $line->img_size, $line->img_description );
224 }
225 $s .= $list->endImageHistoryList();
226 } else { $s=''; }
227 $wgOut->addHTML( $s );
228
229 # Exist check because we don't want to show this on pages where an image
230 # doesn't exist along with the noimage message, that would suck. -ævar
231 if( $wgUseExternalEditor && $this->img->exists() ) {
232 $this->uploadLinksBox();
233 }
234
235 }
236
237 function imageLinks()
238 {
239 global $wgUser, $wgOut;
240
241 $wgOut->addHTML( '<h2 id="filelinks">' . wfMsg( 'imagelinks' ) . "</h2>\n" );
242
243 $dbr =& wfGetDB( DB_SLAVE );
244 $page = $dbr->tableName( 'page' );
245 $imagelinks = $dbr->tableName( 'imagelinks' );
246
247 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
248 $dbr->addQuotes( $this->mTitle->getDBkey() ) . " AND il_from=page_id"
249 . " LIMIT 500"; # quickie emergency brake
250 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
251
252 if ( 0 == $dbr->numRows( $res ) ) {
253 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
254 return;
255 }
256 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
257
258 $sk = $wgUser->getSkin();
259 while ( $s = $dbr->fetchObject( $res ) ) {
260 $name = Title::MakeTitle( $s->page_namespace, $s->page_title );
261 $link = $sk->makeKnownLinkObj( $name, "" );
262 $wgOut->addHTML( "<li>{$link}</li>\n" );
263 }
264 $wgOut->addHTML( "</ul>\n" );
265 }
266
267 function delete()
268 {
269 global $wgUser, $wgOut, $wgRequest;
270
271 $confirm = $wgRequest->getBool( 'wpConfirmB' );
272 $image = $wgRequest->getVal( 'image' );
273 $oldimage = $wgRequest->getVal( 'oldimage' );
274
275 # Only sysops can delete images. Previously ordinary users could delete
276 # old revisions, but this is no longer the case.
277 if ( !$wgUser->isAllowed('delete') ) {
278 $wgOut->sysopRequired();
279 return;
280 }
281 if ( wfReadOnly() ) {
282 $wgOut->readOnlyPage();
283 return;
284 }
285
286 # Better double-check that it hasn't been deleted yet!
287 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
288 if ( ( !is_null( $image ) )
289 && ( '' == trim( $image ) ) ) {
290 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
291 return;
292 }
293
294 $this->img = new Image( $this->mTitle );
295
296 # Deleting old images doesn't require confirmation
297 if ( !is_null( $oldimage ) || $confirm ) {
298 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
299 $this->doDelete();
300 } else {
301 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
302 }
303 return;
304 }
305
306 if ( !is_null( $image ) ) {
307 $q = '&image=' . urlencode( $image );
308 } else if ( !is_null( $oldimage ) ) {
309 $q = '&oldimage=' . urlencode( $oldimage );
310 } else {
311 $q = '';
312 }
313 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
314 }
315
316 function doDelete()
317 {
318 global $wgOut, $wgUser, $wgContLang, $wgRequest;
319 global $wgUseSquid, $wgInternalServer, $wgPostCommitUpdateList;
320 $fname = 'ImagePage::doDelete';
321
322 $reason = $wgRequest->getVal( 'wpReason' );
323 $oldimage = $wgRequest->getVal( 'oldimage' );
324
325 $dbw =& wfGetDB( DB_MASTER );
326
327 if ( !is_null( $oldimage ) ) {
328 if ( strlen( $oldimage ) < 16 ) {
329 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
330 return;
331 }
332 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
333 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
334 return;
335 }
336
337 # Invalidate description page cache
338 $this->mTitle->invalidateCache();
339
340 # Squid purging
341 if ( $wgUseSquid ) {
342 $urlArr = Array(
343 $wgInternalServer.wfImageArchiveUrl( $oldimage ),
344 $wgInternalServer.$this->mTitle->getFullURL()
345 );
346 wfPurgeSquidServers($urlArr);
347 }
348 $this->doDeleteOldImage( $oldimage );
349 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
350 $deleted = $oldimage;
351 } else {
352 $image = $this->mTitle->getDBkey();
353 $dest = wfImageDir( $image );
354 $archive = wfImageDir( $image );
355
356 # Delete the image file if it exists; due to sync problems
357 # or manual trimming sometimes the file will be missing.
358 $targetFile = "{$dest}/{$image}";
359 if( file_exists( $targetFile ) && ! @unlink( $targetFile ) ) {
360 # If the deletion operation actually failed, bug out:
361 $wgOut->fileDeleteError( $targetFile );
362 return;
363 }
364 $dbw->delete( 'image', array( 'img_name' => $image ) );
365 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
366
367 # Purge archive URLs from the squid
368 $urlArr = Array();
369 while ( $s = $dbw->fetchObject( $res ) ) {
370 $this->doDeleteOldImage( $s->oi_archive_name );
371 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name );
372 }
373
374 # And also the HTML of all pages using this image
375 $linksTo = $this->img->getLinksTo();
376 if ( $wgUseSquid ) {
377 $u = SquidUpdate::newFromTitles( $linksTo, $urlArr );
378 array_push( $wgPostCommitUpdateList, $u );
379 }
380
381 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
382
383 # Image itself is now gone, and database is cleaned.
384 # Now we remove the image description page.
385
386 $article = new Article( $this->mTitle );
387 $article->doDeleteArticle( $reason ); # ignore errors
388
389 # Invalidate parser cache and client cache for pages using this image
390 # This is left until relatively late to reduce lock time
391 Title::touchArray( $linksTo );
392
393 /* Delete thumbnails and refresh image metadata cache */
394 $this->img->purgeCache();
395
396
397 $deleted = $image;
398 }
399
400 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
401 $wgOut->setRobotpolicy( 'noindex,nofollow' );
402
403 $sk = $wgUser->getSkin();
404 $loglink = $sk->makeKnownLinkObj(
405 Title::makeTitle( NS_SPECIAL, 'Log/delete' ),
406 wfMsg( 'deletionlog' ) );
407
408 $text = wfMsg( 'deletedtext', $deleted, $loglink );
409
410 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
411 $wgOut->returnToMain( false, $this->mTitle->getPrefixedText() );
412 }
413
414 function doDeleteOldImage( $oldimage )
415 {
416 global $wgOut;
417
418 $name = substr( $oldimage, 15 );
419 $archive = wfImageArchiveDir( $name );
420
421 # Delete the image if it exists. Sometimes the file will be missing
422 # due to manual intervention or weird sync problems; treat that
423 # condition gracefully and continue to delete the database entry.
424 # Also some records may end up with an empty oi_archive_name field
425 # if the original file was missing when a new upload was made;
426 # don't try to delete the directory then!
427 #
428 $targetFile = "{$archive}/{$oldimage}";
429 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink( $targetFile ) ) {
430 # If we actually have a file and can't delete it, throw an error.
431 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
432 } else {
433 # Log the deletion
434 $log = new LogPage( 'delete' );
435 $log->addEntry( 'delete', $this->mTitle, wfMsg('deletedrevision',$oldimage) );
436 }
437 }
438
439 function revert()
440 {
441 global $wgOut, $wgRequest, $wgUser;
442 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
443
444 $oldimage = $wgRequest->getText( 'oldimage' );
445 if ( strlen( $oldimage ) < 16 ) {
446 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
447 return;
448 }
449 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
450 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
451 return;
452 }
453
454 if ( wfReadOnly() ) {
455 $wgOut->readOnlyPage();
456 return;
457 }
458 if( $wgUser->isAnon() ) {
459 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
460 return;
461 }
462 if ( ! $this->mTitle->userCanEdit() ) {
463 $wgOut->sysopRequired();
464 return;
465 }
466 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
467 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
468 return;
469 }
470 $name = substr( $oldimage, 15 );
471
472 $dest = wfImageDir( $name );
473 $archive = wfImageArchiveDir( $name );
474 $curfile = "{$dest}/{$name}";
475
476 if ( ! is_file( $curfile ) ) {
477 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
478 return;
479 }
480 $oldver = wfTimestampNow() . "!{$name}";
481
482 $dbr =& wfGetDB( DB_SLAVE );
483 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
484
485 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
486 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
487 return;
488 }
489 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
490 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
491 }
492
493 # Record upload and update metadata cache
494 $img = Image::newFromName( $name );
495 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
496
497 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
498 $wgOut->setRobotpolicy( 'noindex,nofollow' );
499 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
500
501 $descTitle = $img->getTitle();
502 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
503 }
504 }
505
506 /**
507 * @todo document
508 * @package MediaWiki
509 */
510 class ImageHistoryList {
511 function ImageHistoryList( &$skin ) {
512 $this->skin =& $skin;
513 }
514
515 function beginImageHistoryList() {
516 $s = "\n<h2 id=\"filehistory\">" . wfMsg( 'imghistory' ) . "</h2>\n" .
517 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
518 return $s;
519 }
520
521 function endImageHistoryList() {
522 $s = "</ul>\n";
523 return $s;
524 }
525
526 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
527 global $wgUser, $wgLang, $wgContLang, $wgTitle;
528
529 $datetime = $wgLang->timeanddate( $timestamp, true );
530 $del = wfMsg( 'deleteimg' );
531 $delall = wfMsg( 'deleteimgcompletely' );
532 $cur = wfMsg( 'cur' );
533
534 if ( $iscur ) {
535 $url = Image::imageUrl( $img );
536 $rlink = $cur;
537 if ( $wgUser->isAllowed('delete') ) {
538 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
539 '&action=delete' );
540 $style = $this->skin->getInternalLinkAttributes( $link, $delall );
541
542 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
543 } else {
544 $dlink = $del;
545 }
546 } else {
547 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
548 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
549 $token = urlencode( $wgUser->editToken( $img ) );
550 $rlink = $this->skin->makeKnownLinkObj( $wgTitle,
551 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
552 urlencode( $img ) . "&wpEditToken=$token" );
553 $dlink = $this->skin->makeKnownLinkObj( $wgTitle,
554 $del, 'action=delete&oldimage=' . urlencode( $img ) .
555 "&wpEditToken=$token" );
556 } else {
557 # Having live active links for non-logged in users
558 # means that bots and spiders crawling our site can
559 # inadvertently change content. Baaaad idea.
560 $rlink = wfMsg( 'revertimg' );
561 $dlink = $del;
562 }
563 }
564 if ( 0 == $user ) {
565 $userlink = $usertext;
566 } else {
567 $userlink = $this->skin->makeLinkObj(
568 Title::makeTitle( NS_USER, $usertext ),
569 $usertext );
570 }
571 $nbytes = wfMsg( 'nbytes', $size );
572 $style = $this->skin->getInternalLinkAttributes( $url, $datetime );
573
574 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
575 . " . . {$userlink} ({$nbytes})";
576
577 $s .= $this->skin->commentBlock( $description, $wgTitle );
578 $s .= "</li>\n";
579 return $s;
580 }
581
582 }
583
584
585 ?>