* Confirmation is now required when deleting old versions of files
[lhc/web/wiklou.git] / includes / ImagePage.php
1 <?php
2 /**
3 */
4
5 /**
6 *
7 */
8 if( !defined( 'MEDIAWIKI' ) )
9 die( 1 );
10
11 /**
12 * Special handling for image description pages
13 *
14 * @addtogroup Media
15 */
16 class ImagePage extends Article {
17
18 /* private */ var $img; // Image object this page is shown for
19 /* private */ var $repo;
20 var $mExtraDescription = false;
21
22 function __construct( $title ) {
23 parent::__construct( $title );
24 $this->img = wfFindFile( $this->mTitle );
25 if ( !$this->img ) {
26 $this->img = wfLocalFile( $this->mTitle );
27 }
28 $this->repo = $this->img->repo;
29 }
30
31 /**
32 * Handler for action=render
33 * Include body text only; none of the image extras
34 */
35 function render() {
36 global $wgOut;
37 $wgOut->setArticleBodyOnly( true );
38 $wgOut->addSecondaryWikitext( $this->getContent() );
39 }
40
41 function view() {
42 global $wgOut, $wgShowEXIF, $wgRequest, $wgUser;
43
44 $diff = $wgRequest->getVal( 'diff' );
45 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
46
47 if ( $this->mTitle->getNamespace() != NS_IMAGE || ( isset( $diff ) && $diffOnly ) )
48 return Article::view();
49
50 if ($wgShowEXIF && $this->img->exists()) {
51 // FIXME: bad interface, see note on MediaHandler::formatMetadata().
52 $formattedMetadata = $this->img->formatMetadata();
53 $showmeta = $formattedMetadata !== false;
54 } else {
55 $showmeta = false;
56 }
57
58 if ($this->img->exists())
59 $wgOut->addHTML($this->showTOC($showmeta));
60
61 $this->openShowImage();
62
63 # No need to display noarticletext, we use our own message, output in openShowImage()
64 if ( $this->getID() ) {
65 Article::view();
66 } else {
67 # Just need to set the right headers
68 $wgOut->setArticleFlag( true );
69 $wgOut->setRobotpolicy( 'index,follow' );
70 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
71 $this->viewUpdates();
72 }
73
74 # Show shared description, if needed
75 if ( $this->mExtraDescription ) {
76 $fol = wfMsg( 'shareddescriptionfollows' );
77 if( $fol != '-' && !wfEmptyMsg( 'shareddescriptionfollows', $fol ) ) {
78 $wgOut->addWikiText( $fol );
79 }
80 $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription . '</div>' );
81 }
82
83 $this->closeShowImage();
84 $this->imageHistory();
85 $this->imageLinks();
86
87 if ( $showmeta ) {
88 global $wgStylePath, $wgStyleVersion;
89 $expand = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-expand' ) ) );
90 $collapse = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-collapse' ) ) );
91 $wgOut->addHTML( Xml::element( 'h2', array( 'id' => 'metadata' ), wfMsg( 'metadata' ) ). "\n" );
92 $wgOut->addWikiText( $this->makeMetadataTable( $formattedMetadata ) );
93 $wgOut->addHTML(
94 "<script type=\"text/javascript\" src=\"$wgStylePath/common/metadata.js?$wgStyleVersion\"></script>\n" .
95 "<script type=\"text/javascript\">attachMetadataToggle('mw_metadata', '$expand', '$collapse');</script>\n" );
96 }
97 }
98
99 /**
100 * Create the TOC
101 *
102 * @access private
103 *
104 * @param bool $metadata Whether or not to show the metadata link
105 * @return string
106 */
107 function showTOC( $metadata ) {
108 global $wgLang;
109 $r = '<ul id="filetoc">
110 <li><a href="#file">' . $wgLang->getNsText( NS_IMAGE ) . '</a></li>
111 <li><a href="#filehistory">' . wfMsgHtml( 'filehist' ) . '</a></li>
112 <li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>' .
113 ($metadata ? ' <li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>' : '') . '
114 </ul>';
115 return $r;
116 }
117
118 /**
119 * Make a table with metadata to be shown in the output page.
120 *
121 * FIXME: bad interface, see note on MediaHandler::formatMetadata().
122 *
123 * @access private
124 *
125 * @param array $exif The array containing the EXIF data
126 * @return string
127 */
128 function makeMetadataTable( $metadata ) {
129 $r = wfMsg( 'metadata-help' ) . "\n\n";
130 $r .= "{| id=mw_metadata class=mw_metadata\n";
131 foreach ( $metadata as $type => $stuff ) {
132 foreach ( $stuff as $k => $v ) {
133 $class = Sanitizer::escapeId( $v['id'] );
134 if( $type == 'collapsed' ) {
135 $class .= ' collapsable';
136 }
137 $r .= "|- class=\"$class\"\n";
138 $r .= "!| {$v['name']}\n";
139 $r .= "|| {$v['value']}\n";
140 }
141 }
142 $r .= '|}';
143 return $r;
144 }
145
146 /**
147 * Overloading Article's getContent method.
148 *
149 * Omit noarticletext if sharedupload; text will be fetched from the
150 * shared upload server if possible.
151 */
152 function getContent() {
153 if( $this->img && !$this->img->isLocal() && 0 == $this->getID() ) {
154 return '';
155 }
156 return Article::getContent();
157 }
158
159 function openShowImage() {
160 global $wgOut, $wgUser, $wgImageLimits, $wgRequest, $wgLang;
161
162 $full_url = $this->img->getURL();
163 $linkAttribs = false;
164 $sizeSel = intval( $wgUser->getOption( 'imagesize') );
165 if( !isset( $wgImageLimits[$sizeSel] ) ) {
166 $sizeSel = User::getDefaultOption( 'imagesize' );
167
168 // The user offset might still be incorrect, specially if
169 // $wgImageLimits got changed (see bug #8858).
170 if( !isset( $wgImageLimits[$sizeSel] ) ) {
171 // Default to the first offset in $wgImageLimits
172 $sizeSel = 0;
173 }
174 }
175 $max = $wgImageLimits[$sizeSel];
176 $maxWidth = $max[0];
177 $maxHeight = $max[1];
178 $sk = $wgUser->getSkin();
179
180 if ( $this->img->exists() ) {
181 # image
182 $page = $wgRequest->getIntOrNull( 'page' );
183 if ( is_null( $page ) ) {
184 $params = array();
185 $page = 1;
186 } else {
187 $params = array( 'page' => $page );
188 }
189 $width_orig = $this->img->getWidth();
190 $width = $width_orig;
191 $height_orig = $this->img->getHeight();
192 $height = $height_orig;
193 $mime = $this->img->getMimeType();
194 $showLink = false;
195 $linkAttribs = array( 'href' => $full_url );
196 $longDesc = $this->img->getLongDesc();
197
198 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this , &$wgOut ) ) ;
199
200 if ( $this->img->allowInlineDisplay() ) {
201 # image
202
203 # "Download high res version" link below the image
204 #$msgsize = wfMsgHtml('file-info-size', $width_orig, $height_orig, $sk->formatSize( $this->img->getSize() ), $mime );
205 # We'll show a thumbnail of this image
206 if ( $width > $maxWidth || $height > $maxHeight ) {
207 # Calculate the thumbnail size.
208 # First case, the limiting factor is the width, not the height.
209 if ( $width / $height >= $maxWidth / $maxHeight ) {
210 $height = round( $height * $maxWidth / $width);
211 $width = $maxWidth;
212 # Note that $height <= $maxHeight now.
213 } else {
214 $newwidth = floor( $width * $maxHeight / $height);
215 $height = round( $height * $newwidth / $width );
216 $width = $newwidth;
217 # Note that $height <= $maxHeight now, but might not be identical
218 # because of rounding.
219 }
220 $msgbig = wfMsgHtml( 'show-big-image' );
221 $msgsmall = wfMsgExt( 'show-big-image-thumb',
222 array( 'parseinline' ), $width, $height );
223 } else {
224 # Image is small enough to show full size on image page
225 $msgbig = htmlspecialchars( $this->img->getName() );
226 $msgsmall = wfMsgExt( 'file-nohires', array( 'parseinline' ) );
227 }
228
229 $params['width'] = $width;
230 $thumbnail = $this->img->transform( $params );
231
232 $anchorclose = "<br />";
233 if( $this->img->mustRender() ) {
234 $showLink = true;
235 } else {
236 $anchorclose .=
237 $msgsmall .
238 '<br />' . Xml::tags( 'a', $linkAttribs, $msgbig ) . ' ' . $longDesc;
239 }
240
241 if ( $this->img->isMultipage() ) {
242 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
243 }
244
245 $imgAttribs = array(
246 'border' => 0,
247 'alt' => $this->img->getTitle()->getPrefixedText()
248 );
249
250 if ( $thumbnail ) {
251 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
252 $thumbnail->toHtml( $imgAttribs, $linkAttribs ) .
253 $anchorclose . '</div>' );
254 }
255
256 if ( $this->img->isMultipage() ) {
257 $count = $this->img->pageCount();
258
259 if ( $page > 1 ) {
260 $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
261 $link = $sk->makeKnownLinkObj( $this->mTitle, $label, 'page='. ($page-1) );
262 $thumb1 = $sk->makeThumbLinkObj( $this->mTitle, $this->img, $link, $label, 'none',
263 array( 'page' => $page - 1 ) );
264 } else {
265 $thumb1 = '';
266 }
267
268 if ( $page < $count ) {
269 $label = wfMsg( 'imgmultipagenext' );
270 $link = $sk->makeKnownLinkObj( $this->mTitle, $label, 'page='. ($page+1) );
271 $thumb2 = $sk->makeThumbLinkObj( $this->mTitle, $this->img, $link, $label, 'none',
272 array( 'page' => $page + 1 ) );
273 } else {
274 $thumb2 = '';
275 }
276
277 global $wgScript;
278 $select = '<form name="pageselector" action="' .
279 htmlspecialchars( $wgScript ) .
280 '" method="get" onchange="document.pageselector.submit();">' .
281 Xml::hidden( 'title', $this->getTitle()->getPrefixedDbKey() );
282 $select .= $wgOut->parse( wfMsg( 'imgmultigotopre' ), false ) .
283 ' <select id="pageselector" name="page">';
284 for ( $i=1; $i <= $count; $i++ ) {
285 $select .= Xml::option( $wgLang->formatNum( $i ), $i,
286 $i == $page );
287 }
288 $select .= '</select>' . $wgOut->parse( wfMsg( 'imgmultigotopost' ), false ) .
289 '<input type="submit" value="' .
290 htmlspecialchars( wfMsg( 'imgmultigo' ) ) . '"></form>';
291
292 $wgOut->addHTML( '</td><td><div class="multipageimagenavbox">' .
293 "$select<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>" );
294 }
295 } else {
296 #if direct link is allowed but it's not a renderable image, show an icon.
297 if ($this->img->isSafeFile()) {
298 $icon= $this->img->iconThumb();
299
300 $wgOut->addHTML( '<div class="fullImageLink" id="file"><a href="' . $full_url . '">' .
301 $icon->toHtml() .
302 '</a></div>' );
303 }
304
305 $showLink = true;
306 }
307
308
309 if ($showLink) {
310 $filename = wfEscapeWikiText( $this->img->getName() );
311 $info = wfMsg( 'file-info', $sk->formatSize( $this->img->getSize() ), $mime );
312
313 global $wgContLang;
314 $dirmark = $wgContLang->getDirMark();
315 if (!$this->img->isSafeFile()) {
316 $warning = wfMsg( 'mediawarning' );
317 $wgOut->addWikiText( <<<EOT
318 <div class="fullMedia">
319 <span class="dangerousLink">[[Media:$filename|$filename]]</span>$dirmark
320 <span class="fileInfo"> $longDesc</span>
321 </div>
322
323 <div class="mediaWarning">$warning</div>
324 EOT
325 );
326 } else {
327 $wgOut->addWikiText( <<<EOT
328 <div class="fullMedia">
329 [[Media:$filename|$filename]]$dirmark <span class="fileInfo"> $longDesc</span>
330 </div>
331 EOT
332 );
333 }
334 }
335
336 if(!$this->img->isLocal()) {
337 $this->printSharedImageText();
338 }
339 } else {
340 # Image does not exist
341
342 $title = SpecialPage::getTitleFor( 'Upload' );
343 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('noimage-linktext'),
344 'wpDestFile=' . urlencode( $this->img->getName() ) );
345 $wgOut->addHTML( wfMsgWikiHtml( 'noimage', $link ) );
346 }
347 }
348
349 function printSharedImageText() {
350 global $wgOut, $wgUser;
351
352 $descUrl = $this->img->getDescriptionUrl();
353 $descText = $this->img->getDescriptionText();
354 $s = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml("sharedupload");
355 if ( $descUrl && !$descText) {
356 $sk = $wgUser->getSkin();
357 $link = $sk->makeExternalLink( $descUrl, wfMsg('shareduploadwiki-linktext') );
358 $s .= " " . wfMsgWikiHtml('shareduploadwiki', $link);
359 }
360 $s .= "</div>";
361 $wgOut->addHTML($s);
362
363 if ( $descText ) {
364 $this->mExtraDescription = $descText;
365 }
366 }
367
368 function getUploadUrl() {
369 global $wgServer;
370 $uploadTitle = SpecialPage::getTitleFor( 'Upload' );
371 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img->getName() ) );
372 }
373
374 /**
375 * Print out the various links at the bottom of the image page, e.g. reupload,
376 * external editing (and instructions link) etc.
377 */
378 function uploadLinksBox() {
379 global $wgUser, $wgOut;
380
381 if( !$this->img->isLocal() )
382 return;
383
384 $sk = $wgUser->getSkin();
385
386 $wgOut->addHtml( '<br /><ul>' );
387
388 # "Upload a new version of this file" link
389 if( UploadForm::userCanReUpload($wgUser,$this->img->name) ) {
390 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
391 $wgOut->addHtml( "<li><div class='plainlinks'>{$ulink}</div></li>" );
392 }
393
394 # External editing link
395 $elink = $sk->makeKnownLinkObj( $this->mTitle, wfMsgHtml( 'edit-externally' ), 'action=edit&externaledit=true&mode=file' );
396 $wgOut->addHtml( '<li>' . $elink . '<div>' . wfMsgWikiHtml( 'edit-externally-help' ) . '</div></li>' );
397
398 $wgOut->addHtml( '</ul>' );
399 }
400
401 function closeShowImage()
402 {
403 # For overloading
404
405 }
406
407 /**
408 * If the page we've just displayed is in the "Image" namespace,
409 * we follow it with an upload history of the image and its usage.
410 */
411 function imageHistory()
412 {
413 global $wgUser, $wgOut, $wgUseExternalEditor;
414
415 $sk = $wgUser->getSkin();
416
417 $line = $this->img->nextHistoryLine();
418
419 if ( $line ) {
420 $list = new ImageHistoryList( $sk, $this->img );
421 $file = $this->repo->newFileFromRow( $line );
422 $dims = $file->getDimensionsString();
423 $s = $list->beginImageHistoryList() .
424 $list->imageHistoryLine( true, wfTimestamp(TS_MW, $line->img_timestamp),
425 $this->mTitle->getDBkey(), $line->img_user,
426 $line->img_user_text, $line->img_size, $line->img_description,
427 $dims
428 );
429
430 while ( $line = $this->img->nextHistoryLine() ) {
431 $file = $this->repo->newFileFromRow( $line );
432 $dims = $file->getDimensionsString();
433 $s .= $list->imageHistoryLine( false, $line->oi_timestamp,
434 $line->oi_archive_name, $line->oi_user,
435 $line->oi_user_text, $line->oi_size, $line->oi_description,
436 $dims
437 );
438 }
439 $s .= $list->endImageHistoryList();
440 } else { $s=''; }
441 $wgOut->addHTML( $s );
442
443 $this->img->resetHistory(); // free db resources
444
445 # Exist check because we don't want to show this on pages where an image
446 # doesn't exist along with the noimage message, that would suck. -ævar
447 if( $wgUseExternalEditor && $this->img->exists() ) {
448 $this->uploadLinksBox();
449 }
450
451 }
452
453 function imageLinks()
454 {
455 global $wgUser, $wgOut;
456
457 $wgOut->addHTML( Xml::element( 'h2', array( 'id' => 'filelinks' ), wfMsg( 'imagelinks' ) ) . "\n" );
458
459 $dbr = wfGetDB( DB_SLAVE );
460 $page = $dbr->tableName( 'page' );
461 $imagelinks = $dbr->tableName( 'imagelinks' );
462
463 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
464 $dbr->addQuotes( $this->mTitle->getDBkey() ) . " AND il_from=page_id";
465 $sql = $dbr->limitResult($sql, 500, 0);
466 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
467
468 if ( 0 == $dbr->numRows( $res ) ) {
469 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
470 return;
471 }
472 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
473
474 $sk = $wgUser->getSkin();
475 while ( $s = $dbr->fetchObject( $res ) ) {
476 $name = Title::MakeTitle( $s->page_namespace, $s->page_title );
477 $link = $sk->makeKnownLinkObj( $name, "" );
478 $wgOut->addHTML( "<li>{$link}</li>\n" );
479 }
480 $wgOut->addHTML( "</ul>\n" );
481 }
482
483 /**
484 * Delete the file, or an earlier version of it
485 */
486 public function delete() {
487 if( !$this->img->exists() || !$this->img->isLocal() ) {
488 // Standard article deletion
489 Article::delete();
490 return;
491 }
492 $deleter = new FileDeleteForm( $this->img );
493 $deleter->execute();
494 }
495
496 /**
497 * Revert the file to an earlier version
498 */
499 public function revert() {
500 $reverter = new FileRevertForm( $this->img );
501 $reverter->execute();
502 }
503
504 /**
505 * Override handling of action=purge
506 */
507 function doPurge() {
508 if( $this->img->exists() ) {
509 wfDebug( "ImagePage::doPurge purging " . $this->img->getName() . "\n" );
510 $update = new HTMLCacheUpdate( $this->mTitle, 'imagelinks' );
511 $update->doUpdate();
512 $this->img->upgradeRow();
513 $this->img->purgeCache();
514 } else {
515 wfDebug( "ImagePage::doPurge no image\n" );
516 }
517 parent::doPurge();
518 }
519
520 /**
521 * Display an error with a wikitext description
522 */
523 function showError( $description ) {
524 global $wgOut;
525 $wgOut->setPageTitle( wfMsg( "internalerror" ) );
526 $wgOut->setRobotpolicy( "noindex,nofollow" );
527 $wgOut->setArticleRelated( false );
528 $wgOut->enableClientCache( false );
529 $wgOut->addWikiText( $description );
530 }
531
532 }
533
534 /**
535 * Builds the image revision log shown on image pages
536 *
537 * @addtogroup Media
538 */
539 class ImageHistoryList {
540
541 protected $img, $skin, $title, $repo;
542
543 public function __construct( $skin, $img ) {
544 $this->skin = $skin;
545 $this->img = $img;
546 $this->title = $img->getTitle();
547 }
548
549 public function beginImageHistoryList() {
550 global $wgOut, $wgUser;
551 return Xml::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) )
552 . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
553 . Xml::openElement( 'table', array( 'class' => 'filehistory' ) ) . "\n"
554 . '<tr><td></td>'
555 . ( $this->img->isLocal() && $wgUser->isAllowed( 'delete' ) ? '<td></td>' : '' )
556 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
557 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
558 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
559 . '<th class="mw-imagepage-filesize">' . wfMsgHtml( 'filehist-filesize' ) . '</th>'
560 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
561 . "</tr>\n";
562 }
563
564 public function endImageHistoryList() {
565 return "</table>\n";
566 }
567
568 public function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description, $dims ) {
569 global $wgUser, $wgLang, $wgTitle, $wgContLang;
570 $local = $this->img->isLocal();
571 $row = '';
572
573 // Deletion link
574 if( $local && $wgUser->isAllowed( 'delete' ) ) {
575 $row .= '<td>';
576 $q = array();
577 $q[] = 'action=delete';
578 $q[] = ( $iscur ? 'image=' . $this->title->getPartialUrl() : 'oldimage=' . urlencode( $img ) );
579 if( !$iscur )
580 $q[] = 'wpEditToken=' . urlencode( $wgUser->editToken( $img ) );
581 $row .= '(' . $this->skin->makeKnownLinkObj(
582 $this->title,
583 wfMsgHtml( $iscur ? 'filehist-deleteall' : 'filehist-deleteone' ),
584 implode( '&', $q )
585 ) . ')';
586 $row .= '</td>';
587 }
588
589 // Reversion link/current indicator
590 $row .= '<td>';
591 if( $iscur ) {
592 $row .= '(' . wfMsgHtml( 'filehist-current' ) . ')';
593 } elseif( $local && $wgUser->isLoggedIn() && $this->title->userCan( 'edit' ) ) {
594 $q = array();
595 $q[] = 'action=revert';
596 $q[] = 'oldimage=' . urlencode( $img );
597 $q[] = 'wpEditToken=' . urlencode( $wgUser->editToken( $img ) );
598 $row .= '(' . $this->skin->makeKnownLinkObj(
599 $this->title,
600 wfMsgHtml( 'filehist-revert' ),
601 implode( '&', $q )
602 ) . ')';
603 }
604 $row .= '</td>';
605
606 // Date/time and image link
607 $row .= '<td>';
608 $url = $iscur ? $this->img->getUrl() : $this->img->getArchiveUrl( $img );
609 $row .= Xml::element(
610 'a',
611 array( 'href' => $url ),
612 $wgLang->timeAndDate( $timestamp, true )
613 );
614 $row .= '</td>';
615
616 // Uploading user
617 $row .= '<td>';
618 if( $local ) {
619 $row .= $this->skin->userLink( $user, $usertext ) . $this->skin->userToolLinks( $user, $usertext );
620 } else {
621 $row .= htmlspecialchars( $usertext );
622 }
623 $row .= '</td>';
624
625 // Image dimensions
626 $row .= '<td>' . htmlspecialchars( $dims ) . '</td>';
627
628 // File size
629 $row .= '<td class="mw-imagepage-filesize">' . $this->skin->formatSize( $size ) . '</td>';
630
631 // Comment
632 $row .= '<td>' . $this->skin->formatComment( $description, $this->title ) . '</td>';
633
634 return "<tr>{$row}</tr>\n";
635 }
636
637 }