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