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