*Tweak rev_deleted message
[mediawiki.git] / includes / ImagePage.php
blobe68fbfa8c405d0dde61fb791fe252fca62c09f77
1 <?php
2 /**
3 */
5 /**
7 */
8 if( !defined( 'MEDIAWIKI' ) )
9 die( 1 );
11 /**
12 * Special handling for image description pages
14 * @addtogroup Media
16 class ImagePage extends Article {
18 /* private */ var $img; // Image object this page is shown for
19 /* private */ var $repo;
20 var $mExtraDescription = false;
22 function __construct( $title ) {
23 parent::__construct( $title );
24 $this->img = wfFindFile( $this->mTitle );
25 if ( !$this->img ) {
26 $this->img = wfLocalFile( $this->mTitle );
28 $this->repo = $this->img->repo;
31 /**
32 * Handler for action=render
33 * Include body text only; none of the image extras
35 function render() {
36 global $wgOut;
37 $wgOut->setArticleBodyOnly( true );
38 $wgOut->addSecondaryWikitext( $this->getContent() );
41 function view() {
42 global $wgOut, $wgShowEXIF, $wgRequest, $wgUser;
44 $diff = $wgRequest->getVal( 'diff' );
45 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
47 if ( $this->mTitle->getNamespace() != NS_IMAGE || ( isset( $diff ) && $diffOnly ) )
48 return Article::view();
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;
58 if ($this->img->exists())
59 $wgOut->addHTML($this->showTOC($showmeta));
61 $this->openShowImage();
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();
74 # Show shared description, if needed
75 if ( $this->mExtraDescription ) {
76 $fol = wfMsg( 'shareddescriptionfollows' );
77 if( $fol != '-' && !wfEmptyMsg( 'shareddescriptionfollows', $fol ) ) {
78 $wgOut->addWikiText( $fol );
80 $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription . '</div>' );
83 $this->closeShowImage();
84 $this->imageHistory();
85 $this->imageLinks();
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" );
99 /**
100 * Create the TOC
102 * @access private
104 * @param bool $metadata Whether or not to show the metadata link
105 * @return string
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;
119 * Make a table with metadata to be shown in the output page.
121 * FIXME: bad interface, see note on MediaHandler::formatMetadata().
123 * @access private
125 * @param array $exif The array containing the EXIF data
126 * @return string
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';
137 $r .= "|- class=\"$class\"\n";
138 $r .= "!| {$v['name']}\n";
139 $r .= "|| {$v['value']}\n";
142 $r .= '|}';
143 return $r;
147 * Overloading Article's getContent method.
149 * Omit noarticletext if sharedupload; text will be fetched from the
150 * shared upload server if possible.
152 function getContent() {
153 if( $this->img && !$this->img->isLocal() && 0 == $this->getID() ) {
154 return '';
156 return Article::getContent();
159 function openShowImage() {
160 global $wgOut, $wgUser, $wgImageLimits, $wgRequest, $wgLang, $wgContLang;
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' );
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;
175 $max = $wgImageLimits[$sizeSel];
176 $maxWidth = $max[0];
177 $maxHeight = $max[1];
178 $sk = $wgUser->getSkin();
179 $dirmark = $wgContLang->getDirMark();
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 );
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();
199 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this , &$wgOut ) ) ;
201 if ( $this->img->allowInlineDisplay() ) {
202 # image
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.
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' ) );
230 $params['width'] = $width;
231 $thumbnail = $this->img->transform( $params );
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;
242 if ( $this->img->isMultipage() ) {
243 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
246 if ( $thumbnail ) {
247 $options = array(
248 'alt' => $this->img->getTitle()->getPrefixedText(),
249 'file-link' => true,
251 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
252 $thumbnail->toHtml( $options ) .
253 $anchorclose . '</div>' );
256 if ( $this->img->isMultipage() ) {
257 $count = $this->img->pageCount();
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 = '';
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 = '';
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 );
288 $select .= '</select>' . $wgOut->parse( wfMsg( 'imgmultigotopost' ), false ) .
289 '<input type="submit" value="' .
290 htmlspecialchars( wfMsg( 'imgmultigo' ) ) . '"></form>';
292 $wgOut->addHTML( '</td><td><div class="multipageimagenavbox">' .
293 "$select<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>" );
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();
300 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
301 $icon->toHtml( array( 'desc-link' => true ) ) .
302 '</div>' );
305 $showLink = true;
309 if ($showLink) {
310 $filename = wfEscapeWikiText( $this->img->getName() );
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>
320 <div class="mediaWarning">$warning</div>
323 } else {
324 $wgOut->addWikiText( <<<EOT
325 <div class="fullMedia">
326 [[Media:$filename|$filename]]$dirmark <span class="fileInfo"> $longDesc</span>
327 </div>
333 if(!$this->img->isLocal()) {
334 $this->printSharedImageText();
336 } else {
337 # Image does not exist
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 ) );
346 function printSharedImageText() {
347 global $wgOut, $wgUser;
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);
357 $s .= "</div>";
358 $wgOut->addHTML($s);
360 if ( $descText ) {
361 $this->mExtraDescription = $descText;
365 function getUploadUrl() {
366 global $wgServer;
367 $uploadTitle = SpecialPage::getTitleFor( 'Upload' );
368 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img->getName() ) );
372 * Print out the various links at the bottom of the image page, e.g. reupload,
373 * external editing (and instructions link) etc.
375 function uploadLinksBox() {
376 global $wgUser, $wgOut;
378 if( !$this->img->isLocal() )
379 return;
381 $sk = $wgUser->getSkin();
383 $wgOut->addHtml( '<br /><ul>' );
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>" );
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>' );
395 $wgOut->addHtml( '</ul>' );
398 function closeShowImage()
400 # For overloading
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.
408 function imageHistory()
410 global $wgUser, $wgOut, $wgUseExternalEditor;
412 $sk = $wgUser->getSkin();
414 $line = $this->img->nextHistoryLine();
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
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
436 $s .= $list->endImageHistoryList();
437 } else { $s=''; }
438 $wgOut->addHTML( $s );
440 $this->img->resetHistory(); // free db resources
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();
450 function imageLinks()
452 global $wgUser, $wgOut;
454 $wgOut->addHTML( Xml::element( 'h2', array( 'id' => 'filelinks' ), wfMsg( 'imagelinks' ) ) . "\n" );
456 $dbr = wfGetDB( DB_SLAVE );
457 $page = $dbr->tableName( 'page' );
458 $imagelinks = $dbr->tableName( 'imagelinks' );
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" );
465 if ( 0 == $dbr->numRows( $res ) ) {
466 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
467 return;
469 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
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" );
477 $wgOut->addHTML( "</ul>\n" );
481 * Delete the file, or an earlier version of it
483 public function delete() {
484 if( !$this->img->exists() || !$this->img->isLocal() ) {
485 // Standard article deletion
486 Article::delete();
487 return;
489 $deleter = new FileDeleteForm( $this->img );
490 $deleter->execute();
494 * Revert the file to an earlier version
496 public function revert() {
497 $reverter = new FileRevertForm( $this->img );
498 $reverter->execute();
502 * Override handling of action=purge
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" );
514 parent::doPurge();
518 * Display an error with a wikitext description
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 );
532 * Builds the image revision log shown on image pages
534 * @addtogroup Media
536 class ImageHistoryList {
538 protected $img, $skin, $title, $repo;
540 public function __construct( $skin, $img ) {
541 $this->skin = $skin;
542 $this->img = $img;
543 $this->title = $img->getTitle();
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";
561 public function endImageHistoryList() {
562 return "</table>\n";
565 public function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description, $dims ) {
566 global $wgUser, $wgLang, $wgContLang;
567 $local = $this->img->isLocal();
568 $row = '';
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>';
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 ) . ')';
600 $row .= '</td>';
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 )
610 $row .= '</td>';
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 );
619 $row .= '</td>';
621 // Image dimensions
622 $row .= '<td>' . htmlspecialchars( $dims ) . '</td>';
624 // File size
625 $row .= '<td class="mw-imagepage-filesize">' . $this->skin->formatSize( $size ) . '</td>';
627 // Comment
628 $row .= '<td>' . $this->skin->formatComment( $description, $this->title ) . '</td>';
630 return "<tr>{$row}</tr>\n";