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