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
->getURL();
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 ( $width != $this->img
->getWidth() ||
$height != $this->img
->getHeight() ) {
172 if( $wgUseImageResize ) {
173 $thumbnail = $this->img
->getThumbnail( $width );
174 if ( $thumbnail == null ) {
175 $url = $img->getViewURL();
177 $url = $thumbnail->getURL();
180 # No resize ability? Show the full image, but scale
181 # it down in the browser so it fits on the page.
182 $url = $img->getViewURL();
184 $anchoropen = "<a href=\"{$full_url}\">";
185 $anchorclose = "</a><br />";
186 if( $this->img
->mustRender() ) {
189 $anchorclose .= "\n$anchoropen{$msg}</a>";
192 $url = $this->img
->getViewURL();
195 $wgOut->addHTML( '<div class="fullImageLink" id="file">' . $anchoropen .
196 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
197 htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>' );
199 #if direct link is allowed but it's not a renderable image, show an icon.
200 if ($this->img
->isSafeFile()) {
201 $icon= $this->img
->iconThumb();
203 $wgOut->addHTML( '<div class="fullImageLink" id="file"><a href="' . $full_url . '">' .
213 $filename = wfEscapeWikiText( $this->img
->getName() );
214 $info = wfMsg( 'fileinfo',
215 ceil($this->img
->getSize()/1024.0),
216 $this->img
->getMimeType() );
218 if (!$this->img
->isSafeFile()) {
219 $warning = wfMsg( 'mediawarning' );
220 $wgOut->addWikiText( <<<END
221 <div class="fullMedia">
222 <span class="dangerousLink">[[Media:$filename|$filename]]</span>
223 <span class="fileInfo"> ($info)</span>
226 <div class="mediaWarning">$warning</div>
230 $wgOut->addWikiText( <<<END
231 <div class="fullMedia">
232 [[Media:$filename|$filename]] <span class="fileInfo"> ($info)</span>
239 if($this->img
->fromSharedDirectory
) {
240 $this->printSharedImageText();
243 # Image does not exist
245 $title = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
246 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('noimage-linktext'),
247 'wpDestFile=' . urlencode( $this->img
->getName() ) );
248 $wgOut->addHTML( wfMsgWikiHtml( 'noimage', $link ) );
252 function printSharedImageText() {
253 global $wgRepositoryBaseUrl, $wgFetchCommonsDescriptions, $wgOut, $wgUser;
255 $url = $wgRepositoryBaseUrl . urlencode($this->mTitle
->getDBkey());
256 $sharedtext = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml("sharedupload");
257 if ($wgRepositoryBaseUrl && !$wgFetchCommonsDescriptions) {
259 $sk = $wgUser->getSkin();
260 $title = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
261 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('shareduploadwiki-linktext'),
262 array( 'wpDestFile' => urlencode( $this->img
->getName() )));
263 $sharedtext .= " " . wfMsgWikiHtml('shareduploadwiki', $link);
265 $sharedtext .= "</div>";
266 $wgOut->addHTML($sharedtext);
268 if ($wgRepositoryBaseUrl && $wgFetchCommonsDescriptions) {
269 require_once("HttpFunctions.php");
270 $ur = ini_set('allow_url_fopen', true);
271 $text = wfGetHTTP($url . '?action=render');
272 ini_set('allow_url_fopen', $ur);
274 $this->mExtraDescription
= $text;
278 function getUploadUrl() {
280 $uploadTitle = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
281 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img
->getName() ) );
285 function uploadLinksBox()
287 global $wgUser, $wgOut;
289 if ($this->img
->fromSharedDirectory
)
292 $sk = $wgUser->getSkin();
293 $wgOut->addHTML( '<br /><ul>' );
294 if( $wgUser->isAllowed( 'reupload' ) ) {
295 $wgOut->addWikiText( "<li>\n<div>". wfMsg( 'uploadnewversion', $this->getUploadUrl() ) ."</div>\n</li>\n" );
297 $wgOut->addHTML( '<li>' );
298 $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle
,
299 wfMsg( 'edit-externally' ), "action=edit&externaledit=true&mode=file" ) );
300 $wgOut->addWikiText( '<div>' . wfMsg('edit-externally-help') . '</div>' );
301 $wgOut->addHTML( '</li></ul>' );
304 function closeShowImage()
311 * If the page we've just displayed is in the "Image" namespace,
312 * we follow it with an upload history of the image and its usage.
314 function imageHistory()
316 global $wgUser, $wgOut, $wgUseExternalEditor;
318 $sk = $wgUser->getSkin();
320 $line = $this->img
->nextHistoryLine();
323 $list =& new ImageHistoryList( $sk );
324 $s = $list->beginImageHistoryList() .
325 $list->imageHistoryLine( true, wfTimestamp(TS_MW
, $line->img_timestamp
),
326 $this->mTitle
->getDBkey(), $line->img_user
,
327 $line->img_user_text
, $line->img_size
, $line->img_description
);
329 while ( $line = $this->img
->nextHistoryLine() ) {
330 $s .= $list->imageHistoryLine( false, $line->img_timestamp
,
331 $line->oi_archive_name
, $line->img_user
,
332 $line->img_user_text
, $line->img_size
, $line->img_description
);
334 $s .= $list->endImageHistoryList();
336 $wgOut->addHTML( $s );
338 # Exist check because we don't want to show this on pages where an image
339 # doesn't exist along with the noimage message, that would suck. -ævar
340 if( $wgUseExternalEditor && $this->img
->exists() ) {
341 $this->uploadLinksBox();
346 function imageLinks()
348 global $wgUser, $wgOut;
350 $wgOut->addHTML( '<h2 id="filelinks">' . wfMsg( 'imagelinks' ) . "</h2>\n" );
352 $dbr =& wfGetDB( DB_SLAVE
);
353 $page = $dbr->tableName( 'page' );
354 $imagelinks = $dbr->tableName( 'imagelinks' );
356 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
357 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id";
358 $sql = $dbr->limitResult($sql, 500, 0);
359 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
361 if ( 0 == $dbr->numRows( $res ) ) {
362 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
365 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
367 $sk = $wgUser->getSkin();
368 while ( $s = $dbr->fetchObject( $res ) ) {
369 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
370 $link = $sk->makeKnownLinkObj( $name, "" );
371 $wgOut->addHTML( "<li>{$link}</li>\n" );
373 $wgOut->addHTML( "</ul>\n" );
378 global $wgUser, $wgOut, $wgRequest;
380 $confirm = $wgRequest->getBool( 'wpConfirmB' );
381 $image = $wgRequest->getVal( 'image' );
382 $oldimage = $wgRequest->getVal( 'oldimage' );
384 # Only sysops can delete images. Previously ordinary users could delete
385 # old revisions, but this is no longer the case.
386 if ( !$wgUser->isAllowed('delete') ) {
387 $wgOut->sysopRequired();
390 if ( $wgUser->isBlocked() ) {
391 return $this->blockedIPpage();
393 if ( wfReadOnly() ) {
394 $wgOut->readOnlyPage();
398 # Better double-check that it hasn't been deleted yet!
399 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
400 if ( ( !is_null( $image ) )
401 && ( '' == trim( $image ) ) ) {
402 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
406 $this->img
= new Image( $this->mTitle
);
408 # Deleting old images doesn't require confirmation
409 if ( !is_null( $oldimage ) ||
$confirm ) {
410 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
413 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
418 if ( !is_null( $image ) ) {
419 $q = '&image=' . urlencode( $image );
420 } else if ( !is_null( $oldimage ) ) {
421 $q = '&oldimage=' . urlencode( $oldimage );
425 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
430 global $wgOut, $wgUser, $wgContLang, $wgRequest;
431 global $wgUseSquid, $wgInternalServer, $wgPostCommitUpdateList;
432 $fname = 'ImagePage::doDelete';
434 $reason = $wgRequest->getVal( 'wpReason' );
435 $oldimage = $wgRequest->getVal( 'oldimage' );
437 $dbw =& wfGetDB( DB_MASTER
);
439 if ( !is_null( $oldimage ) ) {
440 if ( strlen( $oldimage ) < 16 ) {
441 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
444 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
445 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
449 # Invalidate description page cache
450 $this->mTitle
->invalidateCache();
455 $wgInternalServer.wfImageArchiveUrl( $oldimage ),
456 $wgInternalServer.$this->mTitle
->getFullURL()
458 wfPurgeSquidServers($urlArr);
460 $this->doDeleteOldImage( $oldimage );
461 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
462 $deleted = $oldimage;
464 $image = $this->mTitle
->getDBkey();
465 $dest = wfImageDir( $image );
466 $archive = wfImageDir( $image );
468 # Delete the image file if it exists; due to sync problems
469 # or manual trimming sometimes the file will be missing.
470 $targetFile = "{$dest}/{$image}";
471 if( file_exists( $targetFile ) && ! @unlink
( $targetFile ) ) {
472 # If the deletion operation actually failed, bug out:
473 $wgOut->fileDeleteError( $targetFile );
476 $dbw->delete( 'image', array( 'img_name' => $image ) );
477 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
479 # Purge archive URLs from the squid
481 while ( $s = $dbw->fetchObject( $res ) ) {
482 $this->doDeleteOldImage( $s->oi_archive_name
);
483 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name
);
486 # And also the HTML of all pages using this image
487 $linksTo = $this->img
->getLinksTo();
489 $u = SquidUpdate
::newFromTitles( $linksTo, $urlArr );
490 array_push( $wgPostCommitUpdateList, $u );
493 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
495 # Image itself is now gone, and database is cleaned.
496 # Now we remove the image description page.
498 $article = new Article( $this->mTitle
);
499 $article->doDeleteArticle( $reason ); # ignore errors
501 # Invalidate parser cache and client cache for pages using this image
502 # This is left until relatively late to reduce lock time
503 Title
::touchArray( $linksTo );
505 /* Delete thumbnails and refresh image metadata cache */
506 $this->img
->purgeCache();
512 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
513 $wgOut->setRobotpolicy( 'noindex,nofollow' );
515 $loglink = '[[Special:Log/delete|' . wfMsg( 'deletionlog' ) . ']]';
516 $text = wfMsg( 'deletedtext', $deleted, $loglink );
518 $wgOut->addWikiText( $text );
520 $wgOut->returnToMain( false, $this->mTitle
->getPrefixedText() );
523 function doDeleteOldImage( $oldimage )
527 $name = substr( $oldimage, 15 );
528 $archive = wfImageArchiveDir( $name );
530 # Delete the image if it exists. Sometimes the file will be missing
531 # due to manual intervention or weird sync problems; treat that
532 # condition gracefully and continue to delete the database entry.
533 # Also some records may end up with an empty oi_archive_name field
534 # if the original file was missing when a new upload was made;
535 # don't try to delete the directory then!
537 $targetFile = "{$archive}/{$oldimage}";
538 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink
( $targetFile ) ) {
539 # If we actually have a file and can't delete it, throw an error.
540 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
543 $log = new LogPage( 'delete' );
544 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
550 global $wgOut, $wgRequest, $wgUser;
551 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
553 $oldimage = $wgRequest->getText( 'oldimage' );
554 if ( strlen( $oldimage ) < 16 ) {
555 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
558 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
559 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
563 if ( wfReadOnly() ) {
564 $wgOut->readOnlyPage();
567 if( $wgUser->isAnon() ) {
568 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
571 if ( ! $this->mTitle
->userCanEdit() ) {
572 $wgOut->sysopRequired();
575 if ( $wgUser->isBlocked() ) {
576 return $this->blockedIPpage();
578 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
579 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
582 $name = substr( $oldimage, 15 );
584 $dest = wfImageDir( $name );
585 $archive = wfImageArchiveDir( $name );
586 $curfile = "{$dest}/{$name}";
588 if ( ! is_file( $curfile ) ) {
589 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
592 $oldver = wfTimestampNow() . "!{$name}";
594 $dbr =& wfGetDB( DB_SLAVE
);
595 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
597 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
598 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
601 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
602 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
605 # Record upload and update metadata cache
606 $img = Image
::newFromName( $name );
607 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
609 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
610 $wgOut->setRobotpolicy( 'noindex,nofollow' );
611 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
613 $descTitle = $img->getTitle();
614 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
617 function blockedIPpage() {
618 require_once( 'EditPage.php' );
619 $edit = new EditPage( $this );
620 return $edit->blockedIPpage();
629 class ImageHistoryList
{
630 function ImageHistoryList( &$skin ) {
631 $this->skin
=& $skin;
634 function beginImageHistoryList() {
635 $s = "\n<h2 id=\"filehistory\">" . wfMsg( 'imghistory' ) . "</h2>\n" .
636 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
640 function endImageHistoryList() {
645 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
646 global $wgUser, $wgLang, $wgContLang, $wgTitle;
648 $datetime = $wgLang->timeanddate( $timestamp, true );
649 $del = wfMsg( 'deleteimg' );
650 $delall = wfMsg( 'deleteimgcompletely' );
651 $cur = wfMsg( 'cur' );
654 $url = Image
::imageUrl( $img );
656 if ( $wgUser->isAllowed('delete') ) {
657 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
659 $style = $this->skin
->getInternalLinkAttributes( $link, $delall );
661 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
666 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
667 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
668 $token = urlencode( $wgUser->editToken( $img ) );
669 $rlink = $this->skin
->makeKnownLinkObj( $wgTitle,
670 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
671 urlencode( $img ) . "&wpEditToken=$token" );
672 $dlink = $this->skin
->makeKnownLinkObj( $wgTitle,
673 $del, 'action=delete&oldimage=' . urlencode( $img ) .
674 "&wpEditToken=$token" );
676 # Having live active links for non-logged in users
677 # means that bots and spiders crawling our site can
678 # inadvertently change content. Baaaad idea.
679 $rlink = wfMsg( 'revertimg' );
684 $userlink = $usertext;
686 $userlink = $this->skin
->makeLinkObj(
687 Title
::makeTitle( NS_USER
, $usertext ),
690 $nbytes = wfMsg( 'nbytes', $size );
691 $style = $this->skin
->getInternalLinkAttributes( $url, $datetime );
693 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
694 . " . . {$userlink} ({$nbytes})";
696 $s .= $this->skin
->commentBlock( $description, $wgTitle );