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, $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
175 $max = $wgImageLimits[$sizeSel];
177 $maxHeight = $max[1];
178 $sk = $wgUser->getSkin();
179 $dirmark = $wgContLang->getDirMark();
181 if ( $this->img
->exists() ) {
183 $page = $wgRequest->getIntOrNull( 'page' );
184 if ( is_null( $page ) ) {
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();
196 $linkAttribs = array( 'href' => $full_url );
197 $longDesc = $this->img
->getLongDesc();
199 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this , &$wgOut ) ) ;
201 if ( $this->img
->allowInlineDisplay() ) {
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);
213 # Note that $height <= $maxHeight now.
215 $newwidth = floor( $width * $maxHeight / $height);
216 $height = round( $height * $newwidth / $width );
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 ) );
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() ) {
239 '<br />' . Xml
::tags( 'a', $linkAttribs, $msgbig ) . "$dirmark " . $longDesc;
242 if ( $this->img
->isMultipage() ) {
243 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
248 'alt' => $this->img
->getTitle()->getPrefixedText(),
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();
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 ) );
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 ) );
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,
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>" );
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 ) ) .
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>
320 <div class="mediaWarning">$warning</div>
324 $wgOut->addWikiText( <<<EOT
325 <div class="fullMedia">
326 [[Media:$filename|$filename]]$dirmark <span class="fileInfo"> $longDesc</span>
333 if(!$this->img
->isLocal()) {
334 $this->printSharedImageText();
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);
361 $this->mExtraDescription
= $descText;
365 function getUploadUrl() {
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() )
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()
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();
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
,
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
,
436 $s .= $list->endImageHistoryList();
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" );
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
489 $deleter = new FileDeleteForm( $this->img
);
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
505 if( $this->img
->exists() ) {
506 wfDebug( "ImagePage::doPurge purging " . $this->img
->getName() . "\n" );
507 $update = new HTMLCacheUpdate( $this->mTitle
, 'imagelinks' );
509 $this->img
->upgradeRow();
510 $this->img
->purgeCache();
512 wfDebug( "ImagePage::doPurge no image\n" );
518 * Display an error with a wikitext description
520 function showError( $description ) {
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
536 class ImageHistoryList
{
538 protected $img, $skin, $title, $repo;
540 public function __construct( $skin, $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"
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>'
561 public function endImageHistoryList() {
565 public function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description, $dims ) {
566 global $wgUser, $wgLang, $wgContLang;
567 $local = $this->img
->isLocal();
571 if( $local && $wgUser->isAllowed( 'delete' ) ) {
574 $q[] = 'action=delete';
576 $q[] = 'oldimage=' . urlencode( $img );
577 $row .= '(' . $this->skin
->makeKnownLinkObj(
579 wfMsgHtml( $iscur ?
'filehist-deleteall' : 'filehist-deleteone' ),
585 // Reversion link/current indicator
588 $row .= '(' . wfMsgHtml( 'filehist-current' ) . ')';
589 } elseif( $local && $wgUser->isLoggedIn() && $this->title
->userCan( 'edit' ) ) {
591 $q[] = 'action=revert';
592 $q[] = 'oldimage=' . urlencode( $img );
593 $q[] = 'wpEditToken=' . urlencode( $wgUser->editToken( $img ) );
594 $row .= '(' . $this->skin
->makeKnownLinkObj(
596 wfMsgHtml( 'filehist-revert' ),
602 // Date/time and image link
604 $url = $iscur ?
$this->img
->getUrl() : $this->img
->getArchiveUrl( $img );
605 $row .= Xml
::element(
607 array( 'href' => $url ),
608 $wgLang->timeAndDate( $timestamp, true )
615 $row .= $this->skin
->userLink( $user, $usertext ) . $this->skin
->userToolLinks( $user, $usertext );
617 $row .= htmlspecialchars( $usertext );
622 $row .= '<td>' . htmlspecialchars( $dims ) . '</td>';
625 $row .= '<td class="mw-imagepage-filesize">' . $this->skin
->formatSize( $size ) . '</td>';
628 $row .= '<td>' . $this->skin
->formatComment( $description, $this->title
) . '</td>';
630 return "<tr>{$row}</tr>\n";