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