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