Unclutter ImagePage. Move EXIF data to the end of the page. Move image upload links...
[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 if ( $this->img->exists() ) {
29 $this->showTOC();
30 }
31 $this->openShowImage();
32
33 # No need to display noarticletext, we use our own message, output in openShowImage()
34 if ( $this->getID() ) {
35 Article::view();
36 } else {
37 # Just need to set the right headers
38 $wgOut->setArticleFlag( true );
39 $wgOut->setRobotpolicy( 'index,follow' );
40 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
41 $wgOut->addMetaTags();
42 $this->viewUpdates();
43 }
44
45 $this->closeShowImage();
46 $this->imageHistory();
47 $this->imageLinks();
48 if ( $this->img->exists() ) $this->showEXIFdata();
49 } else {
50 Article::view();
51 }
52 }
53
54 function showTOC() {
55 global $wgOut, $wgShowEXIF, $wgLang;
56
57 $r= '<div class="imagepagetoc" style="float:right; margin-top:-4em;"><table id="toc" border="0"><tr><td valign="top"><b>'.wfMsg( 'toc' ).'</b><br />';
58 $r .= '<a href="#toc">' . $wgLang->getNStext( NS_IMAGE ) . '</a href> - ';
59 $r .= '<a href="#imghistory">' . wfMsg( 'imghistory' ) . '</a href> - ';
60 $r .= '<a href="#imagelinks">' . wfMsg( 'imagelinks' ) . '</a href>';
61 if ( $wgShowEXIF ) {
62 $r .= ' - <a href="#showexif">' . wfMsg( 'exifdata' ) . '</a>';
63 }
64
65
66 $wgOut->addHTML( $r . '</td></tr></table></div>' );
67
68 }
69
70 function showEXIFdata() {
71 global $wgOut, $wgShowEXIF;
72 if ( ! $wgShowEXIF ) return;
73
74 # Get the EXIF data
75 $exif = $this->img->getExifData();
76 if ( count ( $exif ) == 0 ) return; # No EXIF data available
77
78 # Create the table
79 $r = '<h2 id="exifdata">'. wfMsg( 'exifdata' ) . "</h2>\n";
80 $r .= "<table class=\"exif\">\n" ;
81 $n = 0;
82 foreach ( $exif as $k => $v ) {
83 if ( $n % 2 == 0 ) {
84 $r .= '<tr>';
85 }
86 $r .= "<th>$k</th>\n";
87 $r .= '<td>' . htmlspecialchars($v) . "</td>\n";
88 if ( $n % 2 == 1 ) {
89 $r .= "</tr>\n";
90 } else {
91 $r .= "<td style=\"background: white;\">&nbsp;&nbsp;</td>\n";
92 }
93 $n++;
94 }
95 if ( $n % 2 == 1 ) {
96 $r .= "<td></td><td></td></tr>\n";
97 }
98
99 $wgOut->addHTML( $r . "</table>\n" );
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">' . $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 if (0) {
187 $wgOut->addHTML( '<table id="toc" border="0"><tr><td valign="top">' );
188 $wgOut->addWikiText( '<div class="editExternally">'. wfMsg( 'uploadnewversion', $this->getUploadUrl() ) .'</div>' );
189 $wgOut->addHTML( '</td><td><div class="editExternally">' );
190 $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle,
191 wfMsg( 'edit-externally' ),
192 "action=edit&externaledit=true&mode=file" ) );
193 $wgOut->addWikiText( '<div class="editExternallyHelp">' .
194 wfMsg('edit-externally-help') . '</div>' );
195 $wgOut->addHTML( '</div></td></tr></table><br clear="both">' );
196 } else if (0) {
197 $wgOut->addHTML( '<br /><ul><li>' );
198 $wgOut->addWikiText( '<div>'. wfMsg( 'uploadnewversion', $this->getUploadUrl() ) .'</div>' );
199 $wgOut->addHTML( '</li><li>' );
200 $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle,
201 wfMsg( 'edit-externally' ), "action=edit&externaledit=true&mode=file" ) );
202 $wgOut->addWikiText( '<div>' . wfMsg('edit-externally-help') . '</div>' );
203 $wgOut->addHTML( '</li></ul>' );
204 } else {
205 }
206 }
207
208 function closeShowImage()
209 {
210 # For overloading
211
212 }
213
214 /**
215 * If the page we've just displayed is in the "Image" namespace,
216 * we follow it with an upload history of the image and its usage.
217 */
218 function imageHistory()
219 {
220 global $wgUser, $wgOut, $wgUseExternalEditor;
221
222 $sk = $wgUser->getSkin();
223
224 $line = $this->img->nextHistoryLine();
225
226 if ( $line ) {
227 $list =& new ImageHistoryList( $sk );
228 $s = $list->beginImageHistoryList() .
229 $list->imageHistoryLine( true, $line->img_timestamp,
230 $this->mTitle->getDBkey(), $line->img_user,
231 $line->img_user_text, $line->img_size, $line->img_description );
232
233 while ( $line = $this->img->nextHistoryLine() ) {
234 $s .= $list->imageHistoryLine( false, $line->img_timestamp,
235 $line->oi_archive_name, $line->img_user,
236 $line->img_user_text, $line->img_size, $line->img_description );
237 }
238 $s .= $list->endImageHistoryList();
239 } else { $s=''; }
240 $wgOut->addHTML( $s );
241
242 if ( $wgUseExternalEditor ) {
243 $this->uploadLinksBox();
244 }
245
246 }
247
248 function imageLinks()
249 {
250 global $wgUser, $wgOut;
251
252 $wgOut->addHTML( '<h2 id="imagelinks">' . wfMsg( 'imagelinks' ) . "</h2>\n" );
253
254 $dbr =& wfGetDB( DB_SLAVE );
255 $page = $dbr->tableName( 'page' );
256 $imagelinks = $dbr->tableName( 'imagelinks' );
257
258 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
259 $dbr->addQuotes( $this->mTitle->getDBkey() ) . " AND il_from=page_id"
260 . " LIMIT 500"; # quickie emergency brake
261 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
262
263 if ( 0 == $dbr->numRows( $res ) ) {
264 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
265 return;
266 }
267 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
268
269 $sk = $wgUser->getSkin();
270 while ( $s = $dbr->fetchObject( $res ) ) {
271 $name = Title::MakeTitle( $s->page_namespace, $s->page_title );
272 $link = $sk->makeKnownLinkObj( $name, "" );
273 $wgOut->addHTML( "<li>{$link}</li>\n" );
274 }
275 $wgOut->addHTML( "</ul>\n" );
276 }
277
278 function delete()
279 {
280 global $wgUser, $wgOut, $wgRequest;
281
282 $confirm = $wgRequest->getBool( 'wpConfirmB' );
283 $image = $wgRequest->getVal( 'image' );
284 $oldimage = $wgRequest->getVal( 'oldimage' );
285
286 # Only sysops can delete images. Previously ordinary users could delete
287 # old revisions, but this is no longer the case.
288 if ( !$wgUser->isAllowed('delete') ) {
289 $wgOut->sysopRequired();
290 return;
291 }
292 if ( wfReadOnly() ) {
293 $wgOut->readOnlyPage();
294 return;
295 }
296
297 # Better double-check that it hasn't been deleted yet!
298 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
299 if ( ( !is_null( $image ) )
300 && ( '' == trim( $image ) ) ) {
301 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
302 return;
303 }
304
305 $this->img = new Image( $this->mTitle );
306
307 # Deleting old images doesn't require confirmation
308 if ( !is_null( $oldimage ) || $confirm ) {
309 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
310 $this->doDelete();
311 } else {
312 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
313 }
314 return;
315 }
316
317 if ( !is_null( $image ) ) {
318 $q = '&image=' . urlencode( $image );
319 } else if ( !is_null( $oldimage ) ) {
320 $q = '&oldimage=' . urlencode( $oldimage );
321 } else {
322 $q = '';
323 }
324 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
325 }
326
327 function doDelete()
328 {
329 global $wgOut, $wgUser, $wgContLang, $wgRequest;
330 global $wgUseSquid, $wgInternalServer, $wgPostCommitUpdateList;
331 $fname = 'ImagePage::doDelete';
332
333 $reason = $wgRequest->getVal( 'wpReason' );
334 $oldimage = $wgRequest->getVal( 'oldimage' );
335
336 $dbw =& wfGetDB( DB_MASTER );
337
338 if ( !is_null( $oldimage ) ) {
339 if ( strlen( $oldimage ) < 16 ) {
340 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
341 return;
342 }
343 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
344 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
345 return;
346 }
347
348 # Invalidate description page cache
349 $this->mTitle->invalidateCache();
350
351 # Squid purging
352 if ( $wgUseSquid ) {
353 $urlArr = Array(
354 $wgInternalServer.wfImageArchiveUrl( $oldimage ),
355 $wgInternalServer.$this->mTitle->getFullURL()
356 );
357 wfPurgeSquidServers($urlArr);
358 }
359 $this->doDeleteOldImage( $oldimage );
360 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
361 $deleted = $oldimage;
362 } else {
363 $image = $this->mTitle->getDBkey();
364 $dest = wfImageDir( $image );
365 $archive = wfImageDir( $image );
366
367 # Delete the image file if it exists; due to sync problems
368 # or manual trimming sometimes the file will be missing.
369 $targetFile = "{$dest}/{$image}";
370 if( file_exists( $targetFile ) && ! @unlink( $targetFile ) ) {
371 # If the deletion operation actually failed, bug out:
372 $wgOut->fileDeleteError( $targetFile );
373 return;
374 }
375 $dbw->delete( 'image', array( 'img_name' => $image ) );
376 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
377
378 # Purge archive URLs from the squid
379 $urlArr = Array();
380 while ( $s = $dbw->fetchObject( $res ) ) {
381 $this->doDeleteOldImage( $s->oi_archive_name );
382 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name );
383 }
384
385 # And also the HTML of all pages using this image
386 $linksTo = $this->img->getLinksTo();
387 if ( $wgUseSquid ) {
388 $u = SquidUpdate::newFromTitles( $linksTo, $urlArr );
389 array_push( $wgPostCommitUpdateList, $u );
390 }
391
392 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
393
394 # Image itself is now gone, and database is cleaned.
395 # Now we remove the image description page.
396
397 $article = new Article( $this->mTitle );
398 $article->doDeleteArticle( $reason ); # ignore errors
399
400 # Invalidate parser cache and client cache for pages using this image
401 # This is left until relatively late to reduce lock time
402 Title::touchArray( $linksTo );
403
404 /* Delete thumbnails and refresh image metadata cache */
405 $this->img->purgeCache();
406
407
408 $deleted = $image;
409 }
410
411 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
412 $wgOut->setRobotpolicy( 'noindex,nofollow' );
413
414 $sk = $wgUser->getSkin();
415 $loglink = $sk->makeKnownLinkObj(
416 Title::makeTitle( NS_SPECIAL, 'Log/delete' ),
417 wfMsg( 'deletionlog' ) );
418
419 $text = wfMsg( 'deletedtext', $deleted, $loglink );
420
421 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
422 $wgOut->returnToMain( false, $this->mTitle->getPrefixedText() );
423 }
424
425 function doDeleteOldImage( $oldimage )
426 {
427 global $wgOut;
428
429 $name = substr( $oldimage, 15 );
430 $archive = wfImageArchiveDir( $name );
431
432 # Delete the image if it exists. Sometimes the file will be missing
433 # due to manual intervention or weird sync problems; treat that
434 # condition gracefully and continue to delete the database entry.
435 # Also some records may end up with an empty oi_archive_name field
436 # if the original file was missing when a new upload was made;
437 # don't try to delete the directory then!
438 #
439 $targetFile = "{$archive}/{$oldimage}";
440 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink( $targetFile ) ) {
441 # If we actually have a file and can't delete it, throw an error.
442 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
443 } else {
444 # Log the deletion
445 $log = new LogPage( 'delete' );
446 $log->addEntry( 'delete', $this->mTitle, wfMsg('deletedrevision',$oldimage) );
447 }
448 }
449
450 function revert()
451 {
452 global $wgOut, $wgRequest, $wgUser;
453 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
454
455 $oldimage = $wgRequest->getText( 'oldimage' );
456 if ( strlen( $oldimage ) < 16 ) {
457 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
458 return;
459 }
460 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
461 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
462 return;
463 }
464
465 if ( wfReadOnly() ) {
466 $wgOut->readOnlyPage();
467 return;
468 }
469 if( $wgUser->isAnon() ) {
470 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
471 return;
472 }
473 if ( ! $this->mTitle->userCanEdit() ) {
474 $wgOut->sysopRequired();
475 return;
476 }
477 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
478 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
479 return;
480 }
481 $name = substr( $oldimage, 15 );
482
483 $dest = wfImageDir( $name );
484 $archive = wfImageArchiveDir( $name );
485 $curfile = "{$dest}/{$name}";
486
487 if ( ! is_file( $curfile ) ) {
488 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
489 return;
490 }
491 $oldver = wfTimestampNow() . "!{$name}";
492
493 $dbr =& wfGetDB( DB_SLAVE );
494 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
495
496 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
497 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
498 return;
499 }
500 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
501 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
502 }
503
504 # Record upload and update metadata cache
505 $img = Image::newFromName( $name );
506 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
507
508 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
509 $wgOut->setRobotpolicy( 'noindex,nofollow' );
510 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
511
512 $descTitle = $img->getTitle();
513 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
514 }
515 }
516
517 /**
518 * @todo document
519 * @package MediaWiki
520 */
521 class ImageHistoryList {
522 function ImageHistoryList( &$skin ) {
523 $this->skin =& $skin;
524 }
525
526 function beginImageHistoryList() {
527 $s = "\n<h2 id=\"imghistory\">" . wfMsg( 'imghistory' ) . "</h2>\n" .
528 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
529 return $s;
530 }
531
532 function endImageHistoryList() {
533 $s = "</ul>\n";
534 return $s;
535 }
536
537 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
538 global $wgUser, $wgLang, $wgContLang, $wgTitle;
539
540 $datetime = $wgLang->timeanddate( $timestamp, true );
541 $del = wfMsg( 'deleteimg' );
542 $delall = wfMsg( 'deleteimgcompletely' );
543 $cur = wfMsg( 'cur' );
544
545 if ( $iscur ) {
546 $url = Image::imageUrl( $img );
547 $rlink = $cur;
548 if ( $wgUser->isAllowed('delete') ) {
549 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
550 '&action=delete' );
551 $style = $this->skin->getInternalLinkAttributes( $link, $delall );
552
553 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
554 } else {
555 $dlink = $del;
556 }
557 } else {
558 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
559 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
560 $token = urlencode( $wgUser->editToken( $img ) );
561 $rlink = $this->skin->makeKnownLinkObj( $wgTitle,
562 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
563 urlencode( $img ) . "&wpEditToken=$token" );
564 $dlink = $this->skin->makeKnownLinkObj( $wgTitle,
565 $del, 'action=delete&oldimage=' . urlencode( $img ) .
566 "&wpEditToken=$token" );
567 } else {
568 # Having live active links for non-logged in users
569 # means that bots and spiders crawling our site can
570 # inadvertently change content. Baaaad idea.
571 $rlink = wfMsg( 'revertimg' );
572 $dlink = $del;
573 }
574 }
575 if ( 0 == $user ) {
576 $userlink = $usertext;
577 } else {
578 $userlink = $this->skin->makeLinkObj(
579 Title::makeTitle( NS_USER, $usertext ),
580 $usertext );
581 }
582 $nbytes = wfMsg( 'nbytes', $size );
583 $style = $this->skin->getInternalLinkAttributes( $url, $datetime );
584
585 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
586 . " . . {$userlink} ({$nbytes})";
587
588 $s .= $this->skin->commentBlock( $description, $wgTitle );
589 $s .= "</li>\n";
590 return $s;
591 }
592
593 }
594
595
596 ?>