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 | '. wfMsg( 'metadata' ) . "\n";
107 foreach( $exif as $k => $v ) {
108 $tag = strtolower( $k );
109 $msg = wfMsg( "exif-$tag" );
111 $r .= "! class=$tag | $msg\n";
112 $r .= "| class=$tag | $v\n";
115 return substr($r, 0, -3) . '|}';
119 * Overloading Article's getContent method.
120 * Omit noarticletext if sharedupload
122 * @param $noredir If true, do not follow redirects
124 function getContent( $noredir )
126 if ( $this->img
&& $this->img
->fromSharedDirectory
&& 0 == $this->getID() ) {
129 return Article
::getContent( $noredir );
132 function openShowImage()
134 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
135 $wgUseImageResize, $wgRepositoryBaseUrl,
136 $wgUseExternalEditor, $wgServer, $wgFetchCommonsDescriptions;
137 $full_url = $this->img
->getURL();
141 if( $wgUser->getOption( 'imagesize' ) == '' ) {
142 $sizeSel = User
::getDefaultOption( 'imagesize' );
144 $sizeSel = intval( $wgUser->getOption( 'imagesize' ) );
146 if( !isset( $wgImageLimits[$sizeSel] ) ) {
147 $sizeSel = User
::getDefaultOption( 'imagesize' );
149 $max = $wgImageLimits[$sizeSel];
151 $maxHeight = $max[1];
152 $sk = $wgUser->getSkin();
154 if ( $this->img
->exists() ) {
156 $width = $this->img
->getWidth();
157 $height = $this->img
->getHeight();
160 if ( $this->img
->allowInlineDisplay() and $width and $height) {
163 # "Download high res version" link below the image
164 $msg = wfMsgHtml('showbigimage', $width, $height, intval( $this->img
->getSize()/1024 ) );
165 if ( $width > $maxWidth ) {
166 $height = floor( $height * $maxWidth / $width );
169 if ( $height > $maxHeight ) {
170 $width = floor( $width * $maxHeight / $height );
171 $height = $maxHeight;
173 if ( $width != $this->img
->getWidth() ||
$height != $this->img
->getHeight() ) {
174 if( $wgUseImageResize ) {
175 $thumbnail = $this->img
->getThumbnail( $width );
176 if ( $thumbnail == null ) {
177 $url = $this->img
->getViewURL();
179 $url = $thumbnail->getURL();
182 # No resize ability? Show the full image, but scale
183 # it down in the browser so it fits on the page.
184 $url = $this->img
->getViewURL();
186 $anchoropen = "<a href=\"{$full_url}\">";
187 $anchorclose = "</a><br />";
188 if( $this->img
->mustRender() ) {
191 $anchorclose .= "\n$anchoropen{$msg}</a>";
194 $url = $this->img
->getViewURL();
197 $wgOut->addHTML( '<div class="fullImageLink" id="file">' . $anchoropen .
198 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
199 htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>' );
201 #if direct link is allowed but it's not a renderable image, show an icon.
202 if ($this->img
->isSafeFile()) {
203 $icon= $this->img
->iconThumb();
205 $wgOut->addHTML( '<div class="fullImageLink" id="file"><a href="' . $full_url . '">' .
215 $filename = wfEscapeWikiText( $this->img
->getName() );
216 $info = wfMsg( 'fileinfo',
217 ceil($this->img
->getSize()/1024.0),
218 $this->img
->getMimeType() );
220 if (!$this->img
->isSafeFile()) {
221 $warning = wfMsg( 'mediawarning' );
222 $wgOut->addWikiText( <<<END
223 <div class="fullMedia">
224 <span class="dangerousLink">[[Media:$filename|$filename]]</span>
225 <span class="fileInfo"> ($info)</span>
228 <div class="mediaWarning">$warning</div>
232 $wgOut->addWikiText( <<<END
233 <div class="fullMedia">
234 [[Media:$filename|$filename]] <span class="fileInfo"> ($info)</span>
241 if($this->img
->fromSharedDirectory
) {
242 $this->printSharedImageText();
245 # Image does not exist
247 $title = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
248 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('noimage-linktext'),
249 'wpDestFile=' . urlencode( $this->img
->getName() ) );
250 $wgOut->addHTML( wfMsgWikiHtml( 'noimage', $link ) );
254 function printSharedImageText() {
255 global $wgRepositoryBaseUrl, $wgFetchCommonsDescriptions, $wgOut, $wgUser;
257 $url = $wgRepositoryBaseUrl . urlencode($this->mTitle
->getDBkey());
258 $sharedtext = "<div class='sharedUploadNotice'>" . wfMsgWikiHtml("sharedupload");
259 if ($wgRepositoryBaseUrl && !$wgFetchCommonsDescriptions) {
261 $sk = $wgUser->getSkin();
262 $title = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
263 $link = $sk->makeKnownLinkObj($title, wfMsgHtml('shareduploadwiki-linktext'),
264 array( 'wpDestFile' => urlencode( $this->img
->getName() )));
265 $sharedtext .= " " . wfMsgWikiHtml('shareduploadwiki', $link);
267 $sharedtext .= "</div>";
268 $wgOut->addHTML($sharedtext);
270 if ($wgRepositoryBaseUrl && $wgFetchCommonsDescriptions) {
271 require_once("HttpFunctions.php");
272 $ur = ini_set('allow_url_fopen', true);
273 $text = wfGetHTTP($url . '?action=render');
274 ini_set('allow_url_fopen', $ur);
276 $this->mExtraDescription
= $text;
280 function getUploadUrl() {
282 $uploadTitle = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
283 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img
->getName() ) );
287 function uploadLinksBox()
289 global $wgUser, $wgOut;
291 if ($this->img
->fromSharedDirectory
)
294 $sk = $wgUser->getSkin();
295 $wgOut->addHTML( '<br /><ul>' );
296 if( $wgUser->isAllowed( 'reupload' ) ) {
297 $wgOut->addWikiText( "<li>\n<div>". wfMsg( 'uploadnewversion', $this->getUploadUrl() ) ."</div>\n</li>\n" );
299 $wgOut->addHTML( '<li>' );
300 $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle
,
301 wfMsg( 'edit-externally' ), "action=edit&externaledit=true&mode=file" ) );
302 $wgOut->addWikiText( '<div>' . wfMsg('edit-externally-help') . '</div>' );
303 $wgOut->addHTML( '</li></ul>' );
306 function closeShowImage()
313 * If the page we've just displayed is in the "Image" namespace,
314 * we follow it with an upload history of the image and its usage.
316 function imageHistory()
318 global $wgUser, $wgOut, $wgUseExternalEditor;
320 $sk = $wgUser->getSkin();
322 $line = $this->img
->nextHistoryLine();
325 $list =& new ImageHistoryList( $sk );
326 $s = $list->beginImageHistoryList() .
327 $list->imageHistoryLine( true, wfTimestamp(TS_MW
, $line->img_timestamp
),
328 $this->mTitle
->getDBkey(), $line->img_user
,
329 $line->img_user_text
, $line->img_size
, $line->img_description
,
330 $line->img_width
, $line->img_height
333 while ( $line = $this->img
->nextHistoryLine() ) {
334 $s .= $list->imageHistoryLine( false, $line->img_timestamp
,
335 $line->oi_archive_name
, $line->img_user
,
336 $line->img_user_text
, $line->img_size
, $line->img_description
,
337 $line->img_width
, $line->img_height
340 $s .= $list->endImageHistoryList();
342 $wgOut->addHTML( $s );
344 # Exist check because we don't want to show this on pages where an image
345 # doesn't exist along with the noimage message, that would suck. -ævar
346 if( $wgUseExternalEditor && $this->img
->exists() ) {
347 $this->uploadLinksBox();
352 function imageLinks()
354 global $wgUser, $wgOut;
356 $wgOut->addHTML( '<h2 id="filelinks">' . wfMsg( 'imagelinks' ) . "</h2>\n" );
358 $dbr =& wfGetDB( DB_SLAVE
);
359 $page = $dbr->tableName( 'page' );
360 $imagelinks = $dbr->tableName( 'imagelinks' );
362 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
363 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id";
364 $sql = $dbr->limitResult($sql, 500, 0);
365 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
367 if ( 0 == $dbr->numRows( $res ) ) {
368 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
371 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
373 $sk = $wgUser->getSkin();
374 while ( $s = $dbr->fetchObject( $res ) ) {
375 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
376 $link = $sk->makeKnownLinkObj( $name, "" );
377 $wgOut->addHTML( "<li>{$link}</li>\n" );
379 $wgOut->addHTML( "</ul>\n" );
384 global $wgUser, $wgOut, $wgRequest;
386 $confirm = $wgRequest->getBool( 'wpConfirmB' );
387 $image = $wgRequest->getVal( 'image' );
388 $oldimage = $wgRequest->getVal( 'oldimage' );
390 # Only sysops can delete images. Previously ordinary users could delete
391 # old revisions, but this is no longer the case.
392 if ( !$wgUser->isAllowed('delete') ) {
393 $wgOut->sysopRequired();
396 if ( $wgUser->isBlocked() ) {
397 return $this->blockedIPpage();
399 if ( wfReadOnly() ) {
400 $wgOut->readOnlyPage();
404 # Better double-check that it hasn't been deleted yet!
405 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
406 if ( ( !is_null( $image ) )
407 && ( '' == trim( $image ) ) ) {
408 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
412 $this->img
= new Image( $this->mTitle
);
414 # Deleting old images doesn't require confirmation
415 if ( !is_null( $oldimage ) ||
$confirm ) {
416 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
419 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
424 if ( !is_null( $image ) ) {
425 $q = '&image=' . urlencode( $image );
426 } else if ( !is_null( $oldimage ) ) {
427 $q = '&oldimage=' . urlencode( $oldimage );
431 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
436 global $wgOut, $wgUser, $wgContLang, $wgRequest;
437 global $wgUseSquid, $wgInternalServer, $wgPostCommitUpdateList;
438 $fname = 'ImagePage::doDelete';
440 $reason = $wgRequest->getVal( 'wpReason' );
441 $oldimage = $wgRequest->getVal( 'oldimage' );
443 $dbw =& wfGetDB( DB_MASTER
);
445 if ( !is_null( $oldimage ) ) {
446 if ( strlen( $oldimage ) < 16 ) {
447 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
450 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
451 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
455 # Invalidate description page cache
456 $this->mTitle
->invalidateCache();
461 $wgInternalServer.wfImageArchiveUrl( $oldimage ),
462 $wgInternalServer.$this->mTitle
->getFullURL()
464 wfPurgeSquidServers($urlArr);
466 $this->doDeleteOldImage( $oldimage );
467 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
468 $deleted = $oldimage;
470 $image = $this->mTitle
->getDBkey();
471 $dest = wfImageDir( $image );
472 $archive = wfImageDir( $image );
474 # Delete the image file if it exists; due to sync problems
475 # or manual trimming sometimes the file will be missing.
476 $targetFile = "{$dest}/{$image}";
477 if( file_exists( $targetFile ) && ! @unlink
( $targetFile ) ) {
478 # If the deletion operation actually failed, bug out:
479 $wgOut->fileDeleteError( $targetFile );
482 $dbw->delete( 'image', array( 'img_name' => $image ) );
483 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
485 # Purge archive URLs from the squid
487 while ( $s = $dbw->fetchObject( $res ) ) {
488 $this->doDeleteOldImage( $s->oi_archive_name
);
489 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name
);
492 # And also the HTML of all pages using this image
493 $linksTo = $this->img
->getLinksTo();
495 $u = SquidUpdate
::newFromTitles( $linksTo, $urlArr );
496 array_push( $wgPostCommitUpdateList, $u );
499 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
501 # Image itself is now gone, and database is cleaned.
502 # Now we remove the image description page.
504 $article = new Article( $this->mTitle
);
505 $article->doDeleteArticle( $reason ); # ignore errors
507 # Invalidate parser cache and client cache for pages using this image
508 # This is left until relatively late to reduce lock time
509 Title
::touchArray( $linksTo );
511 /* Delete thumbnails and refresh image metadata cache */
512 $this->img
->purgeCache();
518 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
519 $wgOut->setRobotpolicy( 'noindex,nofollow' );
521 $loglink = '[[Special:Log/delete|' . wfMsg( 'deletionlog' ) . ']]';
522 $text = wfMsg( 'deletedtext', $deleted, $loglink );
524 $wgOut->addWikiText( $text );
526 $wgOut->returnToMain( false, $this->mTitle
->getPrefixedText() );
529 function doDeleteOldImage( $oldimage )
533 $name = substr( $oldimage, 15 );
534 $archive = wfImageArchiveDir( $name );
536 # Delete the image if it exists. Sometimes the file will be missing
537 # due to manual intervention or weird sync problems; treat that
538 # condition gracefully and continue to delete the database entry.
539 # Also some records may end up with an empty oi_archive_name field
540 # if the original file was missing when a new upload was made;
541 # don't try to delete the directory then!
543 $targetFile = "{$archive}/{$oldimage}";
544 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink
( $targetFile ) ) {
545 # If we actually have a file and can't delete it, throw an error.
546 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
549 $log = new LogPage( 'delete' );
550 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
556 global $wgOut, $wgRequest, $wgUser;
557 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
559 $oldimage = $wgRequest->getText( 'oldimage' );
560 if ( strlen( $oldimage ) < 16 ) {
561 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
564 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
565 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
569 if ( wfReadOnly() ) {
570 $wgOut->readOnlyPage();
573 if( $wgUser->isAnon() ) {
574 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
577 if ( ! $this->mTitle
->userCanEdit() ) {
578 $wgOut->sysopRequired();
581 if ( $wgUser->isBlocked() ) {
582 return $this->blockedIPpage();
584 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
585 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
588 $name = substr( $oldimage, 15 );
590 $dest = wfImageDir( $name );
591 $archive = wfImageArchiveDir( $name );
592 $curfile = "{$dest}/{$name}";
594 if ( ! is_file( $curfile ) ) {
595 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
598 $oldver = wfTimestampNow() . "!{$name}";
600 $dbr =& wfGetDB( DB_SLAVE
);
601 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
603 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
604 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
607 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
608 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
611 # Record upload and update metadata cache
612 $img = Image
::newFromName( $name );
613 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
615 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
616 $wgOut->setRobotpolicy( 'noindex,nofollow' );
617 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
619 $descTitle = $img->getTitle();
620 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
623 function blockedIPpage() {
624 require_once( 'EditPage.php' );
625 $edit = new EditPage( $this );
626 return $edit->blockedIPpage();
635 class ImageHistoryList
{
636 function ImageHistoryList( &$skin ) {
637 $this->skin
=& $skin;
640 function beginImageHistoryList() {
641 $s = "\n<h2 id=\"filehistory\">" . wfMsg( 'imghistory' ) . "</h2>\n" .
642 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
646 function endImageHistoryList() {
651 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description, $width, $height ) {
652 global $wgUser, $wgLang, $wgContLang, $wgTitle;
654 $datetime = $wgLang->timeanddate( $timestamp, true );
655 $del = wfMsg( 'deleteimg' );
656 $delall = wfMsg( 'deleteimgcompletely' );
657 $cur = wfMsg( 'cur' );
660 $url = Image
::imageUrl( $img );
662 if ( $wgUser->isAllowed('delete') ) {
663 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
665 $style = $this->skin
->getInternalLinkAttributes( $link, $delall );
667 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
672 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
673 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
674 $token = urlencode( $wgUser->editToken( $img ) );
675 $rlink = $this->skin
->makeKnownLinkObj( $wgTitle,
676 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
677 urlencode( $img ) . "&wpEditToken=$token" );
678 $dlink = $this->skin
->makeKnownLinkObj( $wgTitle,
679 $del, 'action=delete&oldimage=' . urlencode( $img ) .
680 "&wpEditToken=$token" );
682 # Having live active links for non-logged in users
683 # means that bots and spiders crawling our site can
684 # inadvertently change content. Baaaad idea.
685 $rlink = wfMsg( 'revertimg' );
690 $userlink = $usertext;
692 $userlink = $this->skin
->makeLinkObj(
693 Title
::makeTitle( NS_USER
, $usertext ),
696 $nbytes = wfMsg( 'nbytes', $size );
697 $widthheight = wfMsg( 'widthheight', $width, $height );
698 $style = $this->skin
->getInternalLinkAttributes( $url, $datetime );
700 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a> . . {$userlink} . . {$widthheight} ({$nbytes})";
702 $s .= $this->skin
->commentBlock( $description, $wgTitle );