8 if( !defined( 'MEDIAWIKI' ) )
12 * Special handling for image description pages
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
);
26 $this->img
= wfLocalFile( $this->mTitle
);
28 $this->repo
= $this->img
->repo
;
32 * Handler for action=render
33 * Include body text only; none of the image extras
37 $wgOut->setArticleBodyOnly( true );
38 $wgOut->addSecondaryWikitext( $this->getContent() );
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;
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() ) {
67 # Just need to set the right headers
68 $wgOut->setArticleFlag( true );
69 $wgOut->setRobotpolicy( 'index,follow' );
70 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
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();
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 ) );
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" );
104 * @param bool $metadata Whether or not to show the metadata link
107 function showTOC( $metadata ) {
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>' : '') . '
119 * Make a table with metadata to be shown in the output page.
121 * FIXME: bad interface, see note on MediaHandler::formatMetadata().
125 * @param array $exif The array containing the EXIF data
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";
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() ) {
156 return Article
::getContent();
159 function openShowImage() {
160 global $wgOut, $wgUser, $wgImageLimits, $wgRequest, $wgLang;
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
175 $max = $wgImageLimits[$sizeSel];
177 $maxHeight = $max[1];
178 $sk = $wgUser->getSkin();
180 if ( $this->img
->exists() ) {
182 $page = $wgRequest->getIntOrNull( 'page' );
183 if ( is_null( $page ) ) {
187 $params = array( 'page' => $page );
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();
195 $linkAttribs = array( 'href' => $full_url );
196 $longDesc = $this->img
->getLongDesc();
198 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this , &$wgOut ) ) ;
200 if ( $this->img
->allowInlineDisplay() ) {
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);
212 # Note that $height <= $maxHeight now.
214 $newwidth = floor( $width * $maxHeight / $height);
215 $height = round( $height * $newwidth / $width );
217 # Note that $height <= $maxHeight now, but might not be identical
218 # because of rounding.
220 $msgbig = wfMsgHtml( 'show-big-image' );
221 $msgsmall = wfMsgExt( 'show-big-image-thumb',
222 array( 'parseinline' ), $width, $height );
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' ) );
229 $params['width'] = $width;
230 $thumbnail = $this->img
->transform( $params );
232 $anchorclose = "<br />";
233 if( $this->img
->mustRender() ) {
238 '<br />' . Xml
::tags( 'a', $linkAttribs, $msgbig ) . ' ' . $longDesc;
241 if ( $this->img
->isMultipage() ) {
242 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
247 'alt' => $this->img
->getTitle()->getPrefixedText(),
250 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
251 $thumbnail->toHtml( $options ) .
252 $anchorclose . '</div>' );
255 if ( $this->img
->isMultipage() ) {
256 $count = $this->img
->pageCount();
259 $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
260 $link = $sk->makeKnownLinkObj( $this->mTitle
, $label, 'page='. ($page-1) );
261 $thumb1 = $sk->makeThumbLinkObj( $this->mTitle
, $this->img
, $link, $label, 'none',
262 array( 'page' => $page - 1 ) );
267 if ( $page < $count ) {
268 $label = wfMsg( 'imgmultipagenext' );
269 $link = $sk->makeKnownLinkObj( $this->mTitle
, $label, 'page='. ($page+
1) );
270 $thumb2 = $sk->makeThumbLinkObj( $this->mTitle
, $this->img
, $link, $label, 'none',
271 array( 'page' => $page +
1 ) );
277 $select = '<form name="pageselector" action="' .
278 htmlspecialchars( $wgScript ) .
279 '" method="get" onchange="document.pageselector.submit();">' .
280 Xml
::hidden( 'title', $this->getTitle()->getPrefixedDbKey() );
281 $select .= $wgOut->parse( wfMsg( 'imgmultigotopre' ), false ) .
282 ' <select id="pageselector" name="page">';
283 for ( $i=1; $i <= $count; $i++
) {
284 $select .= Xml
::option( $wgLang->formatNum( $i ), $i,
287 $select .= '</select>' . $wgOut->parse( wfMsg( 'imgmultigotopost' ), false ) .
288 '<input type="submit" value="' .
289 htmlspecialchars( wfMsg( 'imgmultigo' ) ) . '"></form>';
291 $wgOut->addHTML( '</td><td><div class="multipageimagenavbox">' .
292 "$select<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>" );
295 #if direct link is allowed but it's not a renderable image, show an icon.
296 if ($this->img
->isSafeFile()) {
297 $icon= $this->img
->iconThumb();
299 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
300 $icon->toHtml( array( 'desc-link' => true ) ) .
309 $filename = wfEscapeWikiText( $this->img
->getName() );
312 $dirmark = $wgContLang->getDirMark();
313 if (!$this->img
->isSafeFile()) {
314 $warning = wfMsg( 'mediawarning' );
315 $wgOut->addWikiText( <<<EOT
316 <div class="fullMedia">
317 <span class="dangerousLink">[[Media:$filename|$filename]]</span>$dirmark
318 <span class="fileInfo"> $longDesc</span>
321 <div class="mediaWarning">$warning</div>
325 $wgOut->addWikiText( <<<EOT
326 <div class="fullMedia">
327 [[Media:$filename|$filename]]$dirmark <span class="fileInfo"> $longDesc</span>
334 if(!$this->img
->isLocal()) {
335 $this->printSharedImageText();
338 # Image does not exist
340 $title = SpecialPage
::getTitleFor( 'Upload' );
341 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('noimage-linktext'),
342 'wpDestFile=' . urlencode( $this->img
->getName() ) );
343 $wgOut->addHTML( wfMsgWikiHtml( 'noimage', $link ) );
347 function printSharedImageText() {
348 global $wgOut, $wgUser;
350 $descUrl = $this->img
->getDescriptionUrl();
351 $descText = $this->img
->getDescriptionText();
352 $s = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml("sharedupload");
353 if ( $descUrl && !$descText) {
354 $sk = $wgUser->getSkin();
355 $link = $sk->makeExternalLink( $descUrl, wfMsg('shareduploadwiki-linktext') );
356 $s .= " " . wfMsgWikiHtml('shareduploadwiki', $link);
362 $this->mExtraDescription
= $descText;
366 function getUploadUrl() {
368 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
369 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img
->getName() ) );
373 * Print out the various links at the bottom of the image page, e.g. reupload,
374 * external editing (and instructions link) etc.
376 function uploadLinksBox() {
377 global $wgUser, $wgOut;
379 if( !$this->img
->isLocal() )
382 $sk = $wgUser->getSkin();
384 $wgOut->addHtml( '<br /><ul>' );
386 # "Upload a new version of this file" link
387 if( UploadForm
::userCanReUpload($wgUser,$this->img
->name
) ) {
388 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
389 $wgOut->addHtml( "<li><div class='plainlinks'>{$ulink}</div></li>" );
392 # External editing link
393 $elink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsgHtml( 'edit-externally' ), 'action=edit&externaledit=true&mode=file' );
394 $wgOut->addHtml( '<li>' . $elink . '<div>' . wfMsgWikiHtml( 'edit-externally-help' ) . '</div></li>' );
396 $wgOut->addHtml( '</ul>' );
399 function closeShowImage()
406 * If the page we've just displayed is in the "Image" namespace,
407 * we follow it with an upload history of the image and its usage.
409 function imageHistory()
411 global $wgUser, $wgOut, $wgUseExternalEditor;
413 $sk = $wgUser->getSkin();
415 $line = $this->img
->nextHistoryLine();
418 $list = new ImageHistoryList( $sk, $this->img
);
419 $file = $this->repo
->newFileFromRow( $line );
420 $dims = $file->getDimensionsString();
421 $s = $list->beginImageHistoryList() .
422 $list->imageHistoryLine( true, wfTimestamp(TS_MW
, $line->img_timestamp
),
423 $this->mTitle
->getDBkey(), $line->img_user
,
424 $line->img_user_text
, $line->img_size
, $line->img_description
,
428 while ( $line = $this->img
->nextHistoryLine() ) {
429 $file = $this->repo
->newFileFromRow( $line );
430 $dims = $file->getDimensionsString();
431 $s .= $list->imageHistoryLine( false, $line->oi_timestamp
,
432 $line->oi_archive_name
, $line->oi_user
,
433 $line->oi_user_text
, $line->oi_size
, $line->oi_description
,
437 $s .= $list->endImageHistoryList();
439 $wgOut->addHTML( $s );
441 $this->img
->resetHistory(); // free db resources
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();
451 function imageLinks()
453 global $wgUser, $wgOut;
455 $wgOut->addHTML( Xml
::element( 'h2', array( 'id' => 'filelinks' ), wfMsg( 'imagelinks' ) ) . "\n" );
457 $dbr = wfGetDB( DB_SLAVE
);
458 $page = $dbr->tableName( 'page' );
459 $imagelinks = $dbr->tableName( 'imagelinks' );
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" );
466 if ( 0 == $dbr->numRows( $res ) ) {
467 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
470 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
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" );
478 $wgOut->addHTML( "</ul>\n" );
482 * Delete the file, or an earlier version of it
484 public function delete() {
485 if( !$this->img
->exists() ||
!$this->img
->isLocal() ) {
486 // Standard article deletion
490 $deleter = new FileDeleteForm( $this->img
);
495 * Revert the file to an earlier version
497 public function revert() {
498 $reverter = new FileRevertForm( $this->img
);
499 $reverter->execute();
503 * Override handling of action=purge
506 if( $this->img
->exists() ) {
507 wfDebug( "ImagePage::doPurge purging " . $this->img
->getName() . "\n" );
508 $update = new HTMLCacheUpdate( $this->mTitle
, 'imagelinks' );
510 $this->img
->upgradeRow();
511 $this->img
->purgeCache();
513 wfDebug( "ImagePage::doPurge no image\n" );
519 * Display an error with a wikitext description
521 function showError( $description ) {
523 $wgOut->setPageTitle( wfMsg( "internalerror" ) );
524 $wgOut->setRobotpolicy( "noindex,nofollow" );
525 $wgOut->setArticleRelated( false );
526 $wgOut->enableClientCache( false );
527 $wgOut->addWikiText( $description );
533 * Builds the image revision log shown on image pages
537 class ImageHistoryList
{
539 protected $img, $skin, $title, $repo;
541 public function __construct( $skin, $img ) {
544 $this->title
= $img->getTitle();
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"
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>'
562 public function endImageHistoryList() {
566 public function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description, $dims ) {
567 global $wgUser, $wgLang, $wgContLang;
568 $local = $this->img
->isLocal();
572 if( $local && $wgUser->isAllowed( 'delete' ) ) {
575 $q[] = 'action=delete';
577 $q[] = 'oldimage=' . urlencode( $img );
578 $row .= '(' . $this->skin
->makeKnownLinkObj(
580 wfMsgHtml( $iscur ?
'filehist-deleteall' : 'filehist-deleteone' ),
586 // Reversion link/current indicator
589 $row .= '(' . wfMsgHtml( 'filehist-current' ) . ')';
590 } elseif( $local && $wgUser->isLoggedIn() && $this->title
->userCan( 'edit' ) ) {
592 $q[] = 'action=revert';
593 $q[] = 'oldimage=' . urlencode( $img );
594 $q[] = 'wpEditToken=' . urlencode( $wgUser->editToken( $img ) );
595 $row .= '(' . $this->skin
->makeKnownLinkObj(
597 wfMsgHtml( 'filehist-revert' ),
603 // Date/time and image link
605 $url = $iscur ?
$this->img
->getUrl() : $this->img
->getArchiveUrl( $img );
606 $row .= Xml
::element(
608 array( 'href' => $url ),
609 $wgLang->timeAndDate( $timestamp, true )
616 $row .= $this->skin
->userLink( $user, $usertext ) . $this->skin
->userToolLinks( $user, $usertext );
618 $row .= htmlspecialchars( $usertext );
623 $row .= '<td>' . htmlspecialchars( $dims ) . '</td>';
626 $row .= '<td class="mw-imagepage-filesize">' . $this->skin
->formatSize( $size ) . '</td>';
629 $row .= '<td>' . $this->skin
->formatComment( $description, $this->title
) . '</td>';
631 return "<tr>{$row}</tr>\n";