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