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