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