d64a41835cd158a9a300a04eeeb4e528f9f157b7
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 $k => $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()
251 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
252 $thumbnail->toHtml( $imgAttribs, $linkAttribs ) .
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"><a href="' . $full_url . '">' .
310 $filename = wfEscapeWikiText( $this->img
->getName() );
311 $info = wfMsg( 'file-info', $sk->formatSize( $this->img
->getSize() ), $mime );
314 $dirmark = $wgContLang->getDirMark();
315 if (!$this->img
->isSafeFile()) {
316 $warning = wfMsg( 'mediawarning' );
317 $wgOut->addWikiText( <<<EOT
318 <div class="fullMedia">
319 <span class="dangerousLink">[[Media:$filename|$filename]]</span>$dirmark
320 <span class="fileInfo"> $longDesc</span>
323 <div class="mediaWarning">$warning</div>
327 $wgOut->addWikiText( <<<EOT
328 <div class="fullMedia">
329 [[Media:$filename|$filename]]$dirmark <span class="fileInfo"> $longDesc</span>
336 if(!$this->img
->isLocal()) {
337 $this->printSharedImageText();
340 # Image does not exist
342 $title = SpecialPage
::getTitleFor( 'Upload' );
343 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('noimage-linktext'),
344 'wpDestFile=' . urlencode( $this->img
->getName() ) );
345 $wgOut->addHTML( wfMsgWikiHtml( 'noimage', $link ) );
349 function printSharedImageText() {
350 global $wgOut, $wgUser;
352 $descUrl = $this->img
->getDescriptionUrl();
353 $descText = $this->img
->getDescriptionText();
354 $s = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml("sharedupload");
355 if ( $descUrl && !$descText) {
356 $sk = $wgUser->getSkin();
357 $link = $sk->makeExternalLink( $descUrl, wfMsg('shareduploadwiki-linktext') );
358 $s .= " " . wfMsgWikiHtml('shareduploadwiki', $link);
364 $this->mExtraDescription
= $descText;
368 function getUploadUrl() {
370 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
371 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img
->getName() ) );
375 * Print out the various links at the bottom of the image page, e.g. reupload,
376 * external editing (and instructions link) etc.
378 function uploadLinksBox() {
379 global $wgUser, $wgOut;
381 if( !$this->img
->isLocal() )
384 $sk = $wgUser->getSkin();
386 $wgOut->addHtml( '<br /><ul>' );
388 # "Upload a new version of this file" link
389 if( UploadForm
::userCanReUpload($wgUser,$this->img
->name
) ) {
390 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
391 $wgOut->addHtml( "<li><div class='plainlinks'>{$ulink}</div></li>" );
394 # External editing link
395 $elink = $sk->makeKnownLinkObj( $this->mTitle
, wfMsgHtml( 'edit-externally' ), 'action=edit&externaledit=true&mode=file' );
396 $wgOut->addHtml( '<li>' . $elink . '<div>' . wfMsgWikiHtml( 'edit-externally-help' ) . '</div></li>' );
398 $wgOut->addHtml( '</ul>' );
401 function closeShowImage()
408 * If the page we've just displayed is in the "Image" namespace,
409 * we follow it with an upload history of the image and its usage.
411 function imageHistory()
413 global $wgUser, $wgOut, $wgUseExternalEditor;
415 $sk = $wgUser->getSkin();
417 $line = $this->img
->nextHistoryLine();
420 $list = new ImageHistoryList( $sk, $this->img
);
421 $file = $this->repo
->newFileFromRow( $line );
422 $dims = $file->getDimensionsString();
423 $s = $list->beginImageHistoryList() .
424 $list->imageHistoryLine( true, wfTimestamp(TS_MW
, $line->img_timestamp
),
425 $this->mTitle
->getDBkey(), $line->img_user
,
426 $line->img_user_text
, $line->img_size
, $line->img_description
,
430 while ( $line = $this->img
->nextHistoryLine() ) {
431 $file = $this->repo
->newFileFromRow( $line );
432 $dims = $file->getDimensionsString();
433 $s .= $list->imageHistoryLine( false, $line->oi_timestamp
,
434 $line->oi_archive_name
, $line->oi_user
,
435 $line->oi_user_text
, $line->oi_size
, $line->oi_description
,
439 $s .= $list->endImageHistoryList();
441 $wgOut->addHTML( $s );
443 $this->img
->resetHistory(); // free db resources
445 # Exist check because we don't want to show this on pages where an image
446 # doesn't exist along with the noimage message, that would suck. -ævar
447 if( $wgUseExternalEditor && $this->img
->exists() ) {
448 $this->uploadLinksBox();
453 function imageLinks()
455 global $wgUser, $wgOut;
457 $wgOut->addHTML( Xml
::element( 'h2', array( 'id' => 'filelinks' ), wfMsg( 'imagelinks' ) ) . "\n" );
459 $dbr = wfGetDB( DB_SLAVE
);
460 $page = $dbr->tableName( 'page' );
461 $imagelinks = $dbr->tableName( 'imagelinks' );
463 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
464 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id";
465 $sql = $dbr->limitResult($sql, 500, 0);
466 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
468 if ( 0 == $dbr->numRows( $res ) ) {
469 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
472 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
474 $sk = $wgUser->getSkin();
475 while ( $s = $dbr->fetchObject( $res ) ) {
476 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
477 $link = $sk->makeKnownLinkObj( $name, "" );
478 $wgOut->addHTML( "<li>{$link}</li>\n" );
480 $wgOut->addHTML( "</ul>\n" );
485 global $wgUser, $wgOut, $wgRequest;
487 if ( !$this->img
->exists() ||
!$this->img
->isLocal() ) {
488 # Use standard article deletion
493 $confirm = $wgRequest->wasPosted();
494 $reason = $wgRequest->getVal( 'wpReason' );
495 $image = $wgRequest->getVal( 'image' );
496 $oldimage = $wgRequest->getVal( 'oldimage' );
498 # Only sysops can delete images. Previously ordinary users could delete
499 # old revisions, but this is no longer the case.
500 if ( !$wgUser->isAllowed('delete') ) {
501 $wgOut->permissionRequired( 'delete' );
504 if ( $wgUser->isBlocked() ) {
505 $wgOut->blockedPage();
508 if ( wfReadOnly() ) {
509 $wgOut->readOnlyPage();
513 # Better double-check that it hasn't been deleted yet!
514 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
515 if ( ( !is_null( $image ) )
516 && ( '' == trim( $image ) ) ) {
517 $wgOut->showFatalError( wfMsg( 'cannotdelete' ) );
521 # Deleting old images doesn't require confirmation
522 if ( !is_null( $oldimage ) ||
$confirm ) {
523 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
524 $this->doDeleteImage( $reason );
526 $wgOut->showFatalError( wfMsg( 'sessionfailure' ) );
531 if ( !is_null( $image ) ) {
532 $q = '&image=' . urlencode( $image );
533 } else if ( !is_null( $oldimage ) ) {
534 $q = '&oldimage=' . urlencode( $oldimage );
538 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
543 * Called doDeleteImage() not doDelete() so that Article::delete() doesn't
546 * @param $reason User provided reason for deletion.
548 function doDeleteImage( $reason ) {
549 global $wgOut, $wgRequest;
551 $oldimage = $wgRequest->getVal( 'oldimage' );
553 if ( !is_null( $oldimage ) ) {
554 if ( strlen( $oldimage ) < 16 ) {
555 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
558 if( strpos( $oldimage, '/' ) !== false ||
strpos( $oldimage, '\\' ) !== false ) {
559 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
562 $status = $this->doDeleteOldImage( $oldimage );
563 $deleted = $oldimage;
565 $status = $this->img
->delete( $reason );
566 if ( !$status->isGood() ) {
568 $wgOut->addWikiText( $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' ) );
571 # Image itself is now gone, and database is cleaned.
572 # Now we remove the image description page.
573 $article = new Article( $this->mTitle
);
574 $article->doDeleteArticle( $reason ); # ignore errors
575 $deleted = $this->img
->getName();
579 $wgOut->setRobotpolicy( 'noindex,nofollow' );
581 if ( !$status->ok
) {
582 // Fatal error flagged
583 $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
584 $wgOut->returnToMain( false, $this->mTitle
->getPrefixedText() );
586 // Operation completed
587 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
588 $loglink = '[[Special:Log/delete|' . wfMsg( 'deletionlog' ) . ']]';
589 $text = wfMsg( 'deletedtext', $deleted, $loglink );
590 $wgOut->addWikiText( $text );
591 $wgOut->returnToMain( false, $this->mTitle
->getPrefixedText() );
596 * Delete an old revision of an image,
597 * @return FileRepoStatus
599 function doDeleteOldImage( $oldimage ) {
602 $status = $this->img
->deleteOld( $oldimage, '' );
603 if( !$status->isGood() ) {
604 $wgOut->addWikiText( $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' ) );
608 $log = new LogPage( 'delete' );
609 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
615 * Revert the file to an earlier version
618 $reverter = new FileRevertForm( $this->img
);
619 $reverter->execute();
623 * Override handling of action=purge
626 if( $this->img
->exists() ) {
627 wfDebug( "ImagePage::doPurge purging " . $this->img
->getName() . "\n" );
628 $update = new HTMLCacheUpdate( $this->mTitle
, 'imagelinks' );
630 $this->img
->upgradeRow();
631 $this->img
->purgeCache();
633 wfDebug( "ImagePage::doPurge no image\n" );
639 * Display an error with a wikitext description
641 function showError( $description ) {
643 $wgOut->setPageTitle( wfMsg( "internalerror" ) );
644 $wgOut->setRobotpolicy( "noindex,nofollow" );
645 $wgOut->setArticleRelated( false );
646 $wgOut->enableClientCache( false );
647 $wgOut->addWikiText( $description );
653 * Builds the image revision log shown on image pages
657 class ImageHistoryList
{
659 protected $img, $skin, $title, $repo;
661 public function __construct( $skin, $img ) {
664 $this->title
= $img->getTitle();
667 public function beginImageHistoryList() {
668 global $wgOut, $wgUser;
669 return Xml
::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) )
670 . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
671 . Xml
::openElement( 'table', array( 'class' => 'filehistory' ) ) . "\n"
673 . ( $this->img
->isLocal() && $wgUser->isAllowed( 'delete' ) ?
'<td></td>' : '' )
674 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
675 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
676 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
677 . '<th class="mw-imagepage-filesize">' . wfMsgHtml( 'filehist-filesize' ) . '</th>'
678 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
682 public function endImageHistoryList() {
686 public function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description, $dims ) {
687 global $wgUser, $wgLang, $wgTitle, $wgContLang;
688 $local = $this->img
->isLocal();
692 if( $local && $wgUser->isAllowed( 'delete' ) ) {
695 $q[] = 'action=delete';
696 $q[] = ( $iscur ?
'image=' . $this->title
->getPartialUrl() : 'oldimage=' . urlencode( $img ) );
698 $q[] = 'wpEditToken=' . urlencode( $wgUser->editToken( $img ) );
699 $row .= '(' . $this->skin
->makeKnownLinkObj(
701 wfMsgHtml( $iscur ?
'filehist-deleteall' : 'filehist-deleteone' ),
707 // Reversion link/current indicator
710 $row .= '(' . wfMsgHtml( 'filehist-current' ) . ')';
711 } elseif( $local && $wgUser->isLoggedIn() && $this->title
->userCan( 'edit' ) ) {
713 $q[] = 'action=revert';
714 $q[] = 'oldimage=' . urlencode( $img );
715 $q[] = 'wpEditToken=' . urlencode( $wgUser->editToken( $img ) );
716 $row .= '(' . $this->skin
->makeKnownLinkObj(
718 wfMsgHtml( 'filehist-revert' ),
724 // Date/time and image link
726 $url = $iscur ?
$this->img
->getUrl() : $this->img
->getArchiveUrl( $img );
727 $row .= Xml
::element(
729 array( 'href' => $url ),
730 $wgLang->timeAndDate( $timestamp, true )
737 $row .= $this->skin
->userLink( $user, $usertext ) . $this->skin
->userToolLinks( $user, $usertext );
739 $row .= htmlspecialchars( $usertext );
744 $row .= '<td>' . htmlspecialchars( $dims ) . '</td>';
747 $row .= '<td class="mw-imagepage-filesize">' . $this->skin
->formatSize( $size ) . '</td>';
750 $row .= '<td>' . $this->skin
->formatComment( $description, $this->title
) . '</td>';
752 return "<tr>{$row}</tr>\n";