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