9 if( !defined( 'MEDIAWIKI' ) )
12 require_once( 'Image.php' );
15 * Special handling for image description pages
18 class ImagePage
extends Article
{
20 /* private */ var $img; // Image object this page is shown for
21 var $mExtraDescription = false;
25 $wgOut->setArticleBodyOnly(true);
26 $wgOut->addWikitext($this->getContent(true));
30 global $wgUseExternalEditor, $wgOut, $wgShowEXIF;
32 $this->img
= new Image( $this->mTitle
);
34 if( $this->mTitle
->getNamespace() == NS_IMAGE
) {
35 if ($wgShowEXIF && $this->img
->exists()) {
36 $exif = $this->img
->getExifData();
37 $showmeta = count($exif) ?
true : false;
43 if ($this->img
->exists())
44 $wgOut->addHTML($this->showTOC($showmeta));
46 $this->openShowImage();
48 $wgOut->addWikiText($this->makeMetadataTable($exif));
50 # No need to display noarticletext, we use our own message, output in openShowImage()
51 if( $this->getID() ) {
54 # Just need to set the right headers
55 $wgOut->setArticleFlag( true );
56 $wgOut->setRobotpolicy( 'index,follow' );
57 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
58 $wgOut->addMetaTags();
62 if ($this->mExtraDescription
) {
63 $fol = wfMsg('shareddescriptionfollows');
65 $wgOut->addWikiText(wfMsg('shareddescriptionfollows'));
66 $wgOut->addHTML($this->mExtraDescription
);
69 $this->closeShowImage();
70 $this->imageHistory();
82 * @param bool $metadata Whether or not to show the metadata link
85 function showTOC( $metadata ) {
87 $r = '<ul id="filetoc">
88 <li><a href="#file">' . $wgLang->getNsText( NS_IMAGE
) . '</a></li>' .
89 ($metadata ?
'<li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>' : '') . '
90 <li><a href="#filehistory">' . wfMsgHtml( 'imghistory' ) . '</a></li>
91 <li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>
97 * Make a table with metadata to be shown in the output page.
101 * @param array $exif The array containing the EXIF data
104 function makeMetadataTable( $exif ) {
105 $r = "{| class=metadata align=right width=250px\n";
106 $r .= '|+ id=metadata | '. htmlspecialchars( wfMsgHtml( 'metadata' ) ) . "\n";
107 foreach( $exif as $k => $v ) {
108 $tag = strtolower( $k );
109 $r .= "! class=$tag |" . wfMsg( "exif-$tag" ) . "\n";
110 $r .= "| class=$tag |" . htmlspecialchars( $v ) . "\n";
113 return substr($r, 0, -3) . '|}';
117 * Overloading Article's getContent method.
118 * Omit noarticletext if sharedupload
120 * @param $noredir If true, do not follow redirects
122 function getContent( $noredir )
124 if ( $this->img
&& $this->img
->fromSharedDirectory
&& 0 == $this->getID() ) {
127 return Article
::getContent( $noredir );
130 function openShowImage()
132 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
133 $wgUseImageResize, $wgRepositoryBaseUrl,
134 $wgUseExternalEditor, $wgServer, $wgFetchCommonsDescriptions;
135 $full_url = $this->img
->getViewURL();
139 if( $wgUser->getOption( 'imagesize' ) == '' ) {
140 $sizeSel = User
::getDefaultOption( 'imagesize' );
142 $sizeSel = intval( $wgUser->getOption( 'imagesize' ) );
144 if( !isset( $wgImageLimits[$sizeSel] ) ) {
145 $sizeSel = User
::getDefaultOption( 'imagesize' );
147 $max = $wgImageLimits[$sizeSel];
149 $maxHeight = $max[1];
150 $sk = $wgUser->getSkin();
152 if ( $this->img
->exists() ) {
154 $width = $this->img
->getWidth();
155 $height = $this->img
->getHeight();
158 if ( $this->img
->allowInlineDisplay() and $width and $height) {
161 # "Download high res version" link below the image
162 $msg = wfMsgHtml('showbigimage', $width, $height, intval( $this->img
->getSize()/1024 ) );
163 if ( $width > $maxWidth ) {
164 $height = floor( $height * $maxWidth / $width );
167 if ( $height > $maxHeight ) {
168 $width = floor( $width * $maxHeight / $height );
169 $height = $maxHeight;
171 if ( !$this->img
->mustRender()
172 && ( $width != $this->img
->getWidth() ||
$height != $this->img
->getHeight() ) ) {
173 if( $wgUseImageResize ) {
174 $thumbnail = $this->img
->getThumbnail( $width );
175 if ( $thumbnail == null ) {
178 $url = $thumbnail->getUrl();
181 # No resize ability? Show the full image, but scale
182 # it down in the browser so it fits on the page.
185 $anchoropen = "<a href=\"{$full_url}\">";
186 $anchorclose = "</a><br />\n$anchoropen{$msg}</a>";
189 $showLink = $this->img
->mustRender();
191 $wgOut->addHTML( '<div class="fullImageLink" id="file">' . $anchoropen .
192 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
193 htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>' );
195 #if direct link is allowed but it's not a renderable image, show an icon.
196 if ($this->img
->isSafeFile()) {
197 $icon= $this->img
->iconThumb();
199 $wgOut->addHTML( '<div class="fullImageLink" id="file"><a href="' . $full_url . '">' .
209 $filename = wfEscapeWikiText( $this->img
->getName() );
210 $info = wfMsg( 'fileinfo',
211 ceil($this->img
->getSize()/1024.0),
212 $this->img
->getMimeType() );
214 if (!$this->img
->isSafeFile()) {
215 $warning = wfMsg( 'mediawarning' );
216 $wgOut->addWikiText( <<<END
217 <div class="fullMedia">
218 <span class="dangerousLink">[[Media:$filename|$filename]]</span>
219 <span class="fileInfo"> ($info)</span>
222 <div class="mediaWarning">$warning</div>
226 $wgOut->addWikiText( <<<END
227 <div class="fullMedia">
228 [[Media:$filename|$filename]] <span class="fileInfo"> ($info)</span>
235 if($this->img
->fromSharedDirectory
) {
236 $this->printSharedImageText();
239 # Image does not exist
241 $title = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
242 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('noimage-linktext'),
243 'wpDestFile=' . urlencode( $this->img
->getName() ) );
244 $wgOut->addHTML( wfMsgWikiHtml( 'noimage', $link ) );
248 function printSharedImageText() {
249 global $wgRepositoryBaseUrl, $wgFetchCommonsDescriptions, $wgOut, $wgUser;
251 $url = $wgRepositoryBaseUrl . urlencode($this->mTitle
->getDBkey());
252 $sharedtext = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml("sharedupload");
253 if ($wgRepositoryBaseUrl && !$wgFetchCommonsDescriptions) {
255 $sk = $wgUser->getSkin();
256 $title = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
257 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('shareduploadwiki-linktext'),
258 array( 'wpDestFile' => urlencode( $this->img
->getName() )));
259 $sharedtext .= " " . wfMsgWikiHtml('shareduploadwiki', $link);
261 $sharedtext .= "</div>";
262 $wgOut->addHTML($sharedtext);
264 if ($wgRepositoryBaseUrl && $wgFetchCommonsDescriptions) {
265 require_once("HttpFunctions.php");
266 $ur = ini_set('allow_url_fopen', true);
267 $text = wfGetHTTP($url . '?action=render');
268 ini_set('allow_url_fopen', $ur);
270 $this->mExtraDescription
= $text;
274 function getUploadUrl() {
276 $uploadTitle = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
277 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img
->getName() ) );
281 function uploadLinksBox()
283 global $wgUser, $wgOut;
285 if ($this->img
->fromSharedDirectory
)
288 $sk = $wgUser->getSkin();
289 $wgOut->addHTML( '<br /><ul>' );
290 if( $wgUser->isAllowed( 'reupload' ) ) {
291 $wgOut->addWikiText( "<li>\n<div>". wfMsg( 'uploadnewversion', $this->getUploadUrl() ) ."</div>\n</li>\n" );
293 $wgOut->addHTML( '<li>' );
294 $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle
,
295 wfMsg( 'edit-externally' ), "action=edit&externaledit=true&mode=file" ) );
296 $wgOut->addWikiText( '<div>' . wfMsg('edit-externally-help') . '</div>' );
297 $wgOut->addHTML( '</li></ul>' );
300 function closeShowImage()
307 * If the page we've just displayed is in the "Image" namespace,
308 * we follow it with an upload history of the image and its usage.
310 function imageHistory()
312 global $wgUser, $wgOut, $wgUseExternalEditor;
314 $sk = $wgUser->getSkin();
316 $line = $this->img
->nextHistoryLine();
319 $list =& new ImageHistoryList( $sk );
320 $s = $list->beginImageHistoryList() .
321 $list->imageHistoryLine( true, wfTimestamp(TS_MW
, $line->img_timestamp
),
322 $this->mTitle
->getDBkey(), $line->img_user
,
323 $line->img_user_text
, $line->img_size
, $line->img_description
);
325 while ( $line = $this->img
->nextHistoryLine() ) {
326 $s .= $list->imageHistoryLine( false, $line->img_timestamp
,
327 $line->oi_archive_name
, $line->img_user
,
328 $line->img_user_text
, $line->img_size
, $line->img_description
);
330 $s .= $list->endImageHistoryList();
332 $wgOut->addHTML( $s );
334 # Exist check because we don't want to show this on pages where an image
335 # doesn't exist along with the noimage message, that would suck. -ævar
336 if( $wgUseExternalEditor && $this->img
->exists() ) {
337 $this->uploadLinksBox();
342 function imageLinks()
344 global $wgUser, $wgOut;
346 $wgOut->addHTML( '<h2 id="filelinks">' . wfMsg( 'imagelinks' ) . "</h2>\n" );
348 $dbr =& wfGetDB( DB_SLAVE
);
349 $page = $dbr->tableName( 'page' );
350 $imagelinks = $dbr->tableName( 'imagelinks' );
352 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
353 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id";
354 $sql = $dbr->limitResult($sql, 500, 0);
355 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
357 if ( 0 == $dbr->numRows( $res ) ) {
358 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
361 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
363 $sk = $wgUser->getSkin();
364 while ( $s = $dbr->fetchObject( $res ) ) {
365 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
366 $link = $sk->makeKnownLinkObj( $name, "" );
367 $wgOut->addHTML( "<li>{$link}</li>\n" );
369 $wgOut->addHTML( "</ul>\n" );
374 global $wgUser, $wgOut, $wgRequest;
376 $confirm = $wgRequest->getBool( 'wpConfirmB' );
377 $image = $wgRequest->getVal( 'image' );
378 $oldimage = $wgRequest->getVal( 'oldimage' );
380 # Only sysops can delete images. Previously ordinary users could delete
381 # old revisions, but this is no longer the case.
382 if ( !$wgUser->isAllowed('delete') ) {
383 $wgOut->sysopRequired();
386 if ( $wgUser->isBlocked() ) {
387 return $this->blockedIPpage();
389 if ( wfReadOnly() ) {
390 $wgOut->readOnlyPage();
394 # Better double-check that it hasn't been deleted yet!
395 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
396 if ( ( !is_null( $image ) )
397 && ( '' == trim( $image ) ) ) {
398 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
402 $this->img
= new Image( $this->mTitle
);
404 # Deleting old images doesn't require confirmation
405 if ( !is_null( $oldimage ) ||
$confirm ) {
406 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
409 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
414 if ( !is_null( $image ) ) {
415 $q = '&image=' . urlencode( $image );
416 } else if ( !is_null( $oldimage ) ) {
417 $q = '&oldimage=' . urlencode( $oldimage );
421 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
426 global $wgOut, $wgUser, $wgContLang, $wgRequest;
427 global $wgUseSquid, $wgInternalServer, $wgPostCommitUpdateList;
428 $fname = 'ImagePage::doDelete';
430 $reason = $wgRequest->getVal( 'wpReason' );
431 $oldimage = $wgRequest->getVal( 'oldimage' );
433 $dbw =& wfGetDB( DB_MASTER
);
435 if ( !is_null( $oldimage ) ) {
436 if ( strlen( $oldimage ) < 16 ) {
437 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
440 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
441 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
445 # Invalidate description page cache
446 $this->mTitle
->invalidateCache();
451 $wgInternalServer.wfImageArchiveUrl( $oldimage ),
452 $wgInternalServer.$this->mTitle
->getFullURL()
454 wfPurgeSquidServers($urlArr);
456 $this->doDeleteOldImage( $oldimage );
457 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
458 $deleted = $oldimage;
460 $image = $this->mTitle
->getDBkey();
461 $dest = wfImageDir( $image );
462 $archive = wfImageDir( $image );
464 # Delete the image file if it exists; due to sync problems
465 # or manual trimming sometimes the file will be missing.
466 $targetFile = "{$dest}/{$image}";
467 if( file_exists( $targetFile ) && ! @unlink
( $targetFile ) ) {
468 # If the deletion operation actually failed, bug out:
469 $wgOut->fileDeleteError( $targetFile );
472 $dbw->delete( 'image', array( 'img_name' => $image ) );
473 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
475 # Purge archive URLs from the squid
477 while ( $s = $dbw->fetchObject( $res ) ) {
478 $this->doDeleteOldImage( $s->oi_archive_name
);
479 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name
);
482 # And also the HTML of all pages using this image
483 $linksTo = $this->img
->getLinksTo();
485 $u = SquidUpdate
::newFromTitles( $linksTo, $urlArr );
486 array_push( $wgPostCommitUpdateList, $u );
489 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
491 # Image itself is now gone, and database is cleaned.
492 # Now we remove the image description page.
494 $article = new Article( $this->mTitle
);
495 $article->doDeleteArticle( $reason ); # ignore errors
497 # Invalidate parser cache and client cache for pages using this image
498 # This is left until relatively late to reduce lock time
499 Title
::touchArray( $linksTo );
501 /* Delete thumbnails and refresh image metadata cache */
502 $this->img
->purgeCache();
508 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
509 $wgOut->setRobotpolicy( 'noindex,nofollow' );
511 $loglink = '[[Special:Log/delete|' . wfMsg( 'deletionlog' ) . ']]';
512 $text = wfMsg( 'deletedtext', $deleted, $loglink );
514 $wgOut->addWikiText( $text );
516 $wgOut->returnToMain( false, $this->mTitle
->getPrefixedText() );
519 function doDeleteOldImage( $oldimage )
523 $name = substr( $oldimage, 15 );
524 $archive = wfImageArchiveDir( $name );
526 # Delete the image if it exists. Sometimes the file will be missing
527 # due to manual intervention or weird sync problems; treat that
528 # condition gracefully and continue to delete the database entry.
529 # Also some records may end up with an empty oi_archive_name field
530 # if the original file was missing when a new upload was made;
531 # don't try to delete the directory then!
533 $targetFile = "{$archive}/{$oldimage}";
534 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink
( $targetFile ) ) {
535 # If we actually have a file and can't delete it, throw an error.
536 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
539 $log = new LogPage( 'delete' );
540 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
546 global $wgOut, $wgRequest, $wgUser;
547 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
549 $oldimage = $wgRequest->getText( 'oldimage' );
550 if ( strlen( $oldimage ) < 16 ) {
551 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
554 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
555 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
559 if ( wfReadOnly() ) {
560 $wgOut->readOnlyPage();
563 if( $wgUser->isAnon() ) {
564 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
567 if ( ! $this->mTitle
->userCanEdit() ) {
568 $wgOut->sysopRequired();
571 if ( $wgUser->isBlocked() ) {
572 return $this->blockedIPpage();
574 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
575 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
578 $name = substr( $oldimage, 15 );
580 $dest = wfImageDir( $name );
581 $archive = wfImageArchiveDir( $name );
582 $curfile = "{$dest}/{$name}";
584 if ( ! is_file( $curfile ) ) {
585 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
588 $oldver = wfTimestampNow() . "!{$name}";
590 $dbr =& wfGetDB( DB_SLAVE
);
591 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
593 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
594 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
597 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
598 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
601 # Record upload and update metadata cache
602 $img = Image
::newFromName( $name );
603 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
605 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
606 $wgOut->setRobotpolicy( 'noindex,nofollow' );
607 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
609 $descTitle = $img->getTitle();
610 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
613 function blockedIPpage() {
614 require_once( 'EditPage.php' );
615 $edit = new EditPage( $this );
616 return $edit->blockedIPpage();
625 class ImageHistoryList
{
626 function ImageHistoryList( &$skin ) {
627 $this->skin
=& $skin;
630 function beginImageHistoryList() {
631 $s = "\n<h2 id=\"filehistory\">" . wfMsg( 'imghistory' ) . "</h2>\n" .
632 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
636 function endImageHistoryList() {
641 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
642 global $wgUser, $wgLang, $wgContLang, $wgTitle;
644 $datetime = $wgLang->timeanddate( $timestamp, true );
645 $del = wfMsg( 'deleteimg' );
646 $delall = wfMsg( 'deleteimgcompletely' );
647 $cur = wfMsg( 'cur' );
650 $url = Image
::imageUrl( $img );
652 if ( $wgUser->isAllowed('delete') ) {
653 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
655 $style = $this->skin
->getInternalLinkAttributes( $link, $delall );
657 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
662 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
663 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
664 $token = urlencode( $wgUser->editToken( $img ) );
665 $rlink = $this->skin
->makeKnownLinkObj( $wgTitle,
666 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
667 urlencode( $img ) . "&wpEditToken=$token" );
668 $dlink = $this->skin
->makeKnownLinkObj( $wgTitle,
669 $del, 'action=delete&oldimage=' . urlencode( $img ) .
670 "&wpEditToken=$token" );
672 # Having live active links for non-logged in users
673 # means that bots and spiders crawling our site can
674 # inadvertently change content. Baaaad idea.
675 $rlink = wfMsg( 'revertimg' );
680 $userlink = $usertext;
682 $userlink = $this->skin
->makeLinkObj(
683 Title
::makeTitle( NS_USER
, $usertext ),
686 $nbytes = wfMsg( 'nbytes', $size );
687 $style = $this->skin
->getInternalLinkAttributes( $url, $datetime );
689 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
690 . " . . {$userlink} ({$nbytes})";
692 $s .= $this->skin
->commentBlock( $description, $wgTitle );