* $wgDebugTidy feature
[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 $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, $wgContLang;
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 $dirmark = $wgContLang->getDirMark();
180
181 if ( $this->img->exists() ) {
182 # image
183 $page = $wgRequest->getIntOrNull( 'page' );
184 if ( is_null( $page ) ) {
185 $params = array();
186 $page = 1;
187 } else {
188 $params = array( 'page' => $page );
189 }
190 $width_orig = $this->img->getWidth();
191 $width = $width_orig;
192 $height_orig = $this->img->getHeight();
193 $height = $height_orig;
194 $mime = $this->img->getMimeType();
195 $showLink = false;
196 $linkAttribs = array( 'href' => $full_url );
197 $longDesc = $this->img->getLongDesc();
198
199 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this , &$wgOut ) ) ;
200
201 if ( $this->img->allowInlineDisplay() ) {
202 # image
203
204 # "Download high res version" link below the image
205 #$msgsize = wfMsgHtml('file-info-size', $width_orig, $height_orig, $sk->formatSize( $this->img->getSize() ), $mime );
206 # We'll show a thumbnail of this image
207 if ( $width > $maxWidth || $height > $maxHeight ) {
208 # Calculate the thumbnail size.
209 # First case, the limiting factor is the width, not the height.
210 if ( $width / $height >= $maxWidth / $maxHeight ) {
211 $height = round( $height * $maxWidth / $width);
212 $width = $maxWidth;
213 # Note that $height <= $maxHeight now.
214 } else {
215 $newwidth = floor( $width * $maxHeight / $height);
216 $height = round( $height * $newwidth / $width );
217 $width = $newwidth;
218 # Note that $height <= $maxHeight now, but might not be identical
219 # because of rounding.
220 }
221 $msgbig = wfMsgHtml( 'show-big-image' );
222 $msgsmall = wfMsgExt( 'show-big-image-thumb',
223 array( 'parseinline' ), $wgLang->formatNum( $width ), $wgLang->formatNum( $height ) );
224 } else {
225 # Image is small enough to show full size on image page
226 $msgbig = htmlspecialchars( $this->img->getName() );
227 $msgsmall = wfMsgExt( 'file-nohires', array( 'parseinline' ) );
228 }
229
230 $params['width'] = $width;
231 $thumbnail = $this->img->transform( $params );
232
233 $anchorclose = "<br />";
234 if( $this->img->mustRender() ) {
235 $showLink = true;
236 } else {
237 $anchorclose .=
238 $msgsmall .
239 '<br />' . Xml::tags( 'a', $linkAttribs, $msgbig ) . "$dirmark " . $longDesc;
240 }
241
242 if ( $this->img->isMultipage() ) {
243 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
244 }
245
246 if ( $thumbnail ) {
247 $options = array(
248 'alt' => $this->img->getTitle()->getPrefixedText(),
249 'file-link' => true,
250 );
251 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
252 $thumbnail->toHtml( $options ) .
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">' .
301 $icon->toHtml( array( 'desc-link' => true ) ) .
302 '</div>' );
303 }
304
305 $showLink = true;
306 }
307
308
309 if ($showLink) {
310 $filename = wfEscapeWikiText( $this->img->getName() );
311
312 if (!$this->img->isSafeFile()) {
313 $warning = wfMsg( 'mediawarning' );
314 $wgOut->addWikiText( <<<EOT
315 <div class="fullMedia">
316 <span class="dangerousLink">[[Media:$filename|$filename]]</span>$dirmark
317 <span class="fileInfo"> $longDesc</span>
318 </div>
319
320 <div class="mediaWarning">$warning</div>
321 EOT
322 );
323 } else {
324 $wgOut->addWikiText( <<<EOT
325 <div class="fullMedia">
326 [[Media:$filename|$filename]]$dirmark <span class="fileInfo"> $longDesc</span>
327 </div>
328 EOT
329 );
330 }
331 }
332
333 if(!$this->img->isLocal()) {
334 $this->printSharedImageText();
335 }
336 } else {
337 # Image does not exist
338
339 $title = SpecialPage::getTitleFor( 'Upload' );
340 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('noimage-linktext'),
341 'wpDestFile=' . urlencode( $this->img->getName() ) );
342 $wgOut->addHTML( wfMsgWikiHtml( 'noimage', $link ) );
343 }
344 }
345
346 function printSharedImageText() {
347 global $wgOut, $wgUser;
348
349 $descUrl = $this->img->getDescriptionUrl();
350 $descText = $this->img->getDescriptionText();
351 $s = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml("sharedupload");
352 if ( $descUrl && !$descText) {
353 $sk = $wgUser->getSkin();
354 $link = $sk->makeExternalLink( $descUrl, wfMsg('shareduploadwiki-linktext') );
355 $s .= " " . wfMsgWikiHtml('shareduploadwiki', $link);
356 }
357 $s .= "</div>";
358 $wgOut->addHTML($s);
359
360 if ( $descText ) {
361 $this->mExtraDescription = $descText;
362 }
363 }
364
365 function getUploadUrl() {
366 global $wgServer;
367 $uploadTitle = SpecialPage::getTitleFor( 'Upload' );
368 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img->getName() ) );
369 }
370
371 /**
372 * Print out the various links at the bottom of the image page, e.g. reupload,
373 * external editing (and instructions link) etc.
374 */
375 function uploadLinksBox() {
376 global $wgUser, $wgOut;
377
378 if( !$this->img->isLocal() )
379 return;
380
381 $sk = $wgUser->getSkin();
382
383 $wgOut->addHtml( '<br /><ul>' );
384
385 # "Upload a new version of this file" link
386 if( UploadForm::userCanReUpload($wgUser,$this->img->name) ) {
387 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
388 $wgOut->addHtml( "<li><div class='plainlinks'>{$ulink}</div></li>" );
389 }
390
391 # External editing link
392 $elink = $sk->makeKnownLinkObj( $this->mTitle, wfMsgHtml( 'edit-externally' ), 'action=edit&externaledit=true&mode=file' );
393 $wgOut->addHtml( '<li>' . $elink . '<div>' . wfMsgWikiHtml( 'edit-externally-help' ) . '</div></li>' );
394
395 $wgOut->addHtml( '</ul>' );
396 }
397
398 function closeShowImage()
399 {
400 # For overloading
401
402 }
403
404 /**
405 * If the page we've just displayed is in the "Image" namespace,
406 * we follow it with an upload history of the image and its usage.
407 */
408 function imageHistory()
409 {
410 global $wgUser, $wgOut, $wgUseExternalEditor;
411
412 $sk = $wgUser->getSkin();
413
414 $line = $this->img->nextHistoryLine();
415
416 if ( $line ) {
417 $list = new ImageHistoryList( $sk, $this->img );
418 $file = $this->repo->newFileFromRow( $line );
419 $dims = $file->getDimensionsString();
420 $s = $list->beginImageHistoryList() .
421 $list->imageHistoryLine( true, wfTimestamp(TS_MW, $line->img_timestamp),
422 $this->mTitle->getDBkey(), $line->img_user,
423 $line->img_user_text, $line->img_size, $line->img_description,
424 $dims
425 );
426
427 while ( $line = $this->img->nextHistoryLine() ) {
428 $file = $this->repo->newFileFromRow( $line );
429 $dims = $file->getDimensionsString();
430 $s .= $list->imageHistoryLine( false, $line->oi_timestamp,
431 $line->oi_archive_name, $line->oi_user,
432 $line->oi_user_text, $line->oi_size, $line->oi_description,
433 $dims
434 );
435 }
436 $s .= $list->endImageHistoryList();
437 } else { $s=''; }
438 $wgOut->addHTML( $s );
439
440 $this->img->resetHistory(); // free db resources
441
442 # Exist check because we don't want to show this on pages where an image
443 # doesn't exist along with the noimage message, that would suck. -ævar
444 if( $wgUseExternalEditor && $this->img->exists() ) {
445 $this->uploadLinksBox();
446 }
447
448 }
449
450 function imageLinks()
451 {
452 global $wgUser, $wgOut;
453
454 $wgOut->addHTML( Xml::element( 'h2', array( 'id' => 'filelinks' ), wfMsg( 'imagelinks' ) ) . "\n" );
455
456 $dbr = wfGetDB( DB_SLAVE );
457 $page = $dbr->tableName( 'page' );
458 $imagelinks = $dbr->tableName( 'imagelinks' );
459
460 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
461 $dbr->addQuotes( $this->mTitle->getDBkey() ) . " AND il_from=page_id";
462 $sql = $dbr->limitResult($sql, 500, 0);
463 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
464
465 if ( 0 == $dbr->numRows( $res ) ) {
466 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
467 return;
468 }
469 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
470
471 $sk = $wgUser->getSkin();
472 while ( $s = $dbr->fetchObject( $res ) ) {
473 $name = Title::MakeTitle( $s->page_namespace, $s->page_title );
474 $link = $sk->makeKnownLinkObj( $name, "" );
475 $wgOut->addHTML( "<li>{$link}</li>\n" );
476 }
477 $wgOut->addHTML( "</ul>\n" );
478 }
479
480 /**
481 * Delete the file, or an earlier version of it
482 */
483 public function delete() {
484 if( !$this->img->exists() || !$this->img->isLocal() ) {
485 // Standard article deletion
486 Article::delete();
487 return;
488 }
489 $deleter = new FileDeleteForm( $this->img );
490 $deleter->execute();
491 }
492
493 /**
494 * Revert the file to an earlier version
495 */
496 public function revert() {
497 $reverter = new FileRevertForm( $this->img );
498 $reverter->execute();
499 }
500
501 /**
502 * Override handling of action=purge
503 */
504 function doPurge() {
505 if( $this->img->exists() ) {
506 wfDebug( "ImagePage::doPurge purging " . $this->img->getName() . "\n" );
507 $update = new HTMLCacheUpdate( $this->mTitle, 'imagelinks' );
508 $update->doUpdate();
509 $this->img->upgradeRow();
510 $this->img->purgeCache();
511 } else {
512 wfDebug( "ImagePage::doPurge no image\n" );
513 }
514 parent::doPurge();
515 }
516
517 /**
518 * Display an error with a wikitext description
519 */
520 function showError( $description ) {
521 global $wgOut;
522 $wgOut->setPageTitle( wfMsg( "internalerror" ) );
523 $wgOut->setRobotpolicy( "noindex,nofollow" );
524 $wgOut->setArticleRelated( false );
525 $wgOut->enableClientCache( false );
526 $wgOut->addWikiText( $description );
527 }
528
529 }
530
531 /**
532 * Builds the image revision log shown on image pages
533 *
534 * @addtogroup Media
535 */
536 class ImageHistoryList {
537
538 protected $img, $skin, $title, $repo;
539
540 public function __construct( $skin, $img ) {
541 $this->skin = $skin;
542 $this->img = $img;
543 $this->title = $img->getTitle();
544 }
545
546 public function beginImageHistoryList() {
547 global $wgOut, $wgUser;
548 return Xml::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) )
549 . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
550 . Xml::openElement( 'table', array( 'class' => 'filehistory' ) ) . "\n"
551 . '<tr><td></td>'
552 . ( $this->img->isLocal() && $wgUser->isAllowed( 'delete' ) ? '<td></td>' : '' )
553 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
554 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
555 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
556 . '<th class="mw-imagepage-filesize">' . wfMsgHtml( 'filehist-filesize' ) . '</th>'
557 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
558 . "</tr>\n";
559 }
560
561 public function endImageHistoryList() {
562 return "</table>\n";
563 }
564
565 public function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description, $dims ) {
566 global $wgUser, $wgLang, $wgContLang;
567 $local = $this->img->isLocal();
568 $row = '';
569
570 // Deletion link
571 if( $local && $wgUser->isAllowed( 'delete' ) ) {
572 $row .= '<td>';
573 $q = array();
574 $q[] = 'action=delete';
575 if( !$iscur )
576 $q[] = 'oldimage=' . urlencode( $img );
577 $row .= '(' . $this->skin->makeKnownLinkObj(
578 $this->title,
579 wfMsgHtml( $iscur ? 'filehist-deleteall' : 'filehist-deleteone' ),
580 implode( '&', $q )
581 ) . ')';
582 $row .= '</td>';
583 }
584
585 // Reversion link/current indicator
586 $row .= '<td>';
587 if( $iscur ) {
588 $row .= '(' . wfMsgHtml( 'filehist-current' ) . ')';
589 } elseif( $local && $wgUser->isLoggedIn() && $this->title->userCan( 'edit' ) ) {
590 $q = array();
591 $q[] = 'action=revert';
592 $q[] = 'oldimage=' . urlencode( $img );
593 $q[] = 'wpEditToken=' . urlencode( $wgUser->editToken( $img ) );
594 $row .= '(' . $this->skin->makeKnownLinkObj(
595 $this->title,
596 wfMsgHtml( 'filehist-revert' ),
597 implode( '&', $q )
598 ) . ')';
599 }
600 $row .= '</td>';
601
602 // Date/time and image link
603 $row .= '<td>';
604 $url = $iscur ? $this->img->getUrl() : $this->img->getArchiveUrl( $img );
605 $row .= Xml::element(
606 'a',
607 array( 'href' => $url ),
608 $wgLang->timeAndDate( $timestamp, true )
609 );
610 $row .= '</td>';
611
612 // Uploading user
613 $row .= '<td>';
614 if( $local ) {
615 $row .= $this->skin->userLink( $user, $usertext ) . $this->skin->userToolLinks( $user, $usertext );
616 } else {
617 $row .= htmlspecialchars( $usertext );
618 }
619 $row .= '</td>';
620
621 // Image dimensions
622 $row .= '<td>' . htmlspecialchars( $dims ) . '</td>';
623
624 // File size
625 $row .= '<td class="mw-imagepage-filesize">' . $this->skin->formatSize( $size ) . '</td>';
626
627 // Comment
628 $row .= '<td>' . $this->skin->formatComment( $description, $this->title ) . '</td>';
629
630 return "<tr>{$row}</tr>\n";
631 }
632
633 }