Static code analysis housekeeping time... things that could be double-checked are...
[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;
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
312 global $wgContLang;
313 $dirmark = $wgContLang->getDirMark();
314 if (!$this->img->isSafeFile()) {
315 $warning = wfMsg( 'mediawarning' );
316 $wgOut->addWikiText( <<<EOT
317 <div class="fullMedia">
318 <span class="dangerousLink">[[Media:$filename|$filename]]</span>$dirmark
319 <span class="fileInfo"> $longDesc</span>
320 </div>
321
322 <div class="mediaWarning">$warning</div>
323 EOT
324 );
325 } else {
326 $wgOut->addWikiText( <<<EOT
327 <div class="fullMedia">
328 [[Media:$filename|$filename]]$dirmark <span class="fileInfo"> $longDesc</span>
329 </div>
330 EOT
331 );
332 }
333 }
334
335 if(!$this->img->isLocal()) {
336 $this->printSharedImageText();
337 }
338 } else {
339 # Image does not exist
340
341 $title = SpecialPage::getTitleFor( 'Upload' );
342 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('noimage-linktext'),
343 'wpDestFile=' . urlencode( $this->img->getName() ) );
344 $wgOut->addHTML( wfMsgWikiHtml( 'noimage', $link ) );
345 }
346 }
347
348 function printSharedImageText() {
349 global $wgOut, $wgUser;
350
351 $descUrl = $this->img->getDescriptionUrl();
352 $descText = $this->img->getDescriptionText();
353 $s = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml("sharedupload");
354 if ( $descUrl && !$descText) {
355 $sk = $wgUser->getSkin();
356 $link = $sk->makeExternalLink( $descUrl, wfMsg('shareduploadwiki-linktext') );
357 $s .= " " . wfMsgWikiHtml('shareduploadwiki', $link);
358 }
359 $s .= "</div>";
360 $wgOut->addHTML($s);
361
362 if ( $descText ) {
363 $this->mExtraDescription = $descText;
364 }
365 }
366
367 function getUploadUrl() {
368 global $wgServer;
369 $uploadTitle = SpecialPage::getTitleFor( 'Upload' );
370 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img->getName() ) );
371 }
372
373 /**
374 * Print out the various links at the bottom of the image page, e.g. reupload,
375 * external editing (and instructions link) etc.
376 */
377 function uploadLinksBox() {
378 global $wgUser, $wgOut;
379
380 if( !$this->img->isLocal() )
381 return;
382
383 $sk = $wgUser->getSkin();
384
385 $wgOut->addHtml( '<br /><ul>' );
386
387 # "Upload a new version of this file" link
388 if( UploadForm::userCanReUpload($wgUser,$this->img->name) ) {
389 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
390 $wgOut->addHtml( "<li><div class='plainlinks'>{$ulink}</div></li>" );
391 }
392
393 # External editing link
394 $elink = $sk->makeKnownLinkObj( $this->mTitle, wfMsgHtml( 'edit-externally' ), 'action=edit&externaledit=true&mode=file' );
395 $wgOut->addHtml( '<li>' . $elink . '<div>' . wfMsgWikiHtml( 'edit-externally-help' ) . '</div></li>' );
396
397 $wgOut->addHtml( '</ul>' );
398 }
399
400 function closeShowImage()
401 {
402 # For overloading
403
404 }
405
406 /**
407 * If the page we've just displayed is in the "Image" namespace,
408 * we follow it with an upload history of the image and its usage.
409 */
410 function imageHistory()
411 {
412 global $wgUser, $wgOut, $wgUseExternalEditor;
413
414 $sk = $wgUser->getSkin();
415
416 $line = $this->img->nextHistoryLine();
417
418 if ( $line ) {
419 $list = new ImageHistoryList( $sk, $this->img );
420 $file = $this->repo->newFileFromRow( $line );
421 $dims = $file->getDimensionsString();
422 $s = $list->beginImageHistoryList() .
423 $list->imageHistoryLine( true, wfTimestamp(TS_MW, $line->img_timestamp),
424 $this->mTitle->getDBkey(), $line->img_user,
425 $line->img_user_text, $line->img_size, $line->img_description,
426 $dims
427 );
428
429 while ( $line = $this->img->nextHistoryLine() ) {
430 $file = $this->repo->newFileFromRow( $line );
431 $dims = $file->getDimensionsString();
432 $s .= $list->imageHistoryLine( false, $line->oi_timestamp,
433 $line->oi_archive_name, $line->oi_user,
434 $line->oi_user_text, $line->oi_size, $line->oi_description,
435 $dims
436 );
437 }
438 $s .= $list->endImageHistoryList();
439 } else { $s=''; }
440 $wgOut->addHTML( $s );
441
442 $this->img->resetHistory(); // free db resources
443
444 # Exist check because we don't want to show this on pages where an image
445 # doesn't exist along with the noimage message, that would suck. -ævar
446 if( $wgUseExternalEditor && $this->img->exists() ) {
447 $this->uploadLinksBox();
448 }
449
450 }
451
452 function imageLinks()
453 {
454 global $wgUser, $wgOut;
455
456 $wgOut->addHTML( Xml::element( 'h2', array( 'id' => 'filelinks' ), wfMsg( 'imagelinks' ) ) . "\n" );
457
458 $dbr = wfGetDB( DB_SLAVE );
459 $page = $dbr->tableName( 'page' );
460 $imagelinks = $dbr->tableName( 'imagelinks' );
461
462 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
463 $dbr->addQuotes( $this->mTitle->getDBkey() ) . " AND il_from=page_id";
464 $sql = $dbr->limitResult($sql, 500, 0);
465 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
466
467 if ( 0 == $dbr->numRows( $res ) ) {
468 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
469 return;
470 }
471 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
472
473 $sk = $wgUser->getSkin();
474 while ( $s = $dbr->fetchObject( $res ) ) {
475 $name = Title::MakeTitle( $s->page_namespace, $s->page_title );
476 $link = $sk->makeKnownLinkObj( $name, "" );
477 $wgOut->addHTML( "<li>{$link}</li>\n" );
478 }
479 $wgOut->addHTML( "</ul>\n" );
480 }
481
482 /**
483 * Delete the file, or an earlier version of it
484 */
485 public function delete() {
486 if( !$this->img->exists() || !$this->img->isLocal() ) {
487 // Standard article deletion
488 Article::delete();
489 return;
490 }
491 $deleter = new FileDeleteForm( $this->img );
492 $deleter->execute();
493 }
494
495 /**
496 * Revert the file to an earlier version
497 */
498 public function revert() {
499 $reverter = new FileRevertForm( $this->img );
500 $reverter->execute();
501 }
502
503 /**
504 * Override handling of action=purge
505 */
506 function doPurge() {
507 if( $this->img->exists() ) {
508 wfDebug( "ImagePage::doPurge purging " . $this->img->getName() . "\n" );
509 $update = new HTMLCacheUpdate( $this->mTitle, 'imagelinks' );
510 $update->doUpdate();
511 $this->img->upgradeRow();
512 $this->img->purgeCache();
513 } else {
514 wfDebug( "ImagePage::doPurge no image\n" );
515 }
516 parent::doPurge();
517 }
518
519 /**
520 * Display an error with a wikitext description
521 */
522 function showError( $description ) {
523 global $wgOut;
524 $wgOut->setPageTitle( wfMsg( "internalerror" ) );
525 $wgOut->setRobotpolicy( "noindex,nofollow" );
526 $wgOut->setArticleRelated( false );
527 $wgOut->enableClientCache( false );
528 $wgOut->addWikiText( $description );
529 }
530
531 }
532
533 /**
534 * Builds the image revision log shown on image pages
535 *
536 * @addtogroup Media
537 */
538 class ImageHistoryList {
539
540 protected $img, $skin, $title, $repo;
541
542 public function __construct( $skin, $img ) {
543 $this->skin = $skin;
544 $this->img = $img;
545 $this->title = $img->getTitle();
546 }
547
548 public function beginImageHistoryList() {
549 global $wgOut, $wgUser;
550 return Xml::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) )
551 . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
552 . Xml::openElement( 'table', array( 'class' => 'filehistory' ) ) . "\n"
553 . '<tr><td></td>'
554 . ( $this->img->isLocal() && $wgUser->isAllowed( 'delete' ) ? '<td></td>' : '' )
555 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
556 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
557 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
558 . '<th class="mw-imagepage-filesize">' . wfMsgHtml( 'filehist-filesize' ) . '</th>'
559 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
560 . "</tr>\n";
561 }
562
563 public function endImageHistoryList() {
564 return "</table>\n";
565 }
566
567 public function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description, $dims ) {
568 global $wgUser, $wgLang, $wgContLang;
569 $local = $this->img->isLocal();
570 $row = '';
571
572 // Deletion link
573 if( $local && $wgUser->isAllowed( 'delete' ) ) {
574 $row .= '<td>';
575 $q = array();
576 $q[] = 'action=delete';
577 $q[] = ( $iscur ? 'image=' . $this->title->getPartialUrl() : 'oldimage=' . urlencode( $img ) );
578 if( !$iscur )
579 $q[] = 'wpEditToken=' . urlencode( $wgUser->editToken( $img ) );
580 $row .= '(' . $this->skin->makeKnownLinkObj(
581 $this->title,
582 wfMsgHtml( $iscur ? 'filehist-deleteall' : 'filehist-deleteone' ),
583 implode( '&', $q )
584 ) . ')';
585 $row .= '</td>';
586 }
587
588 // Reversion link/current indicator
589 $row .= '<td>';
590 if( $iscur ) {
591 $row .= '(' . wfMsgHtml( 'filehist-current' ) . ')';
592 } elseif( $local && $wgUser->isLoggedIn() && $this->title->userCan( 'edit' ) ) {
593 $q = array();
594 $q[] = 'action=revert';
595 $q[] = 'oldimage=' . urlencode( $img );
596 $q[] = 'wpEditToken=' . urlencode( $wgUser->editToken( $img ) );
597 $row .= '(' . $this->skin->makeKnownLinkObj(
598 $this->title,
599 wfMsgHtml( 'filehist-revert' ),
600 implode( '&', $q )
601 ) . ')';
602 }
603 $row .= '</td>';
604
605 // Date/time and image link
606 $row .= '<td>';
607 $url = $iscur ? $this->img->getUrl() : $this->img->getArchiveUrl( $img );
608 $row .= Xml::element(
609 'a',
610 array( 'href' => $url ),
611 $wgLang->timeAndDate( $timestamp, true )
612 );
613 $row .= '</td>';
614
615 // Uploading user
616 $row .= '<td>';
617 if( $local ) {
618 $row .= $this->skin->userLink( $user, $usertext ) . $this->skin->userToolLinks( $user, $usertext );
619 } else {
620 $row .= htmlspecialchars( $usertext );
621 }
622 $row .= '</td>';
623
624 // Image dimensions
625 $row .= '<td>' . htmlspecialchars( $dims ) . '</td>';
626
627 // File size
628 $row .= '<td class="mw-imagepage-filesize">' . $this->skin->formatSize( $size ) . '</td>';
629
630 // Comment
631 $row .= '<td>' . $this->skin->formatComment( $description, $this->title ) . '</td>';
632
633 return "<tr>{$row}</tr>\n";
634 }
635
636 }