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