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
23 global $wgUseExternalEditor, $wgOut, $wgShowEXIF;
25 $this->img
= new Image( $this->mTitle
);
27 if( $this->mTitle
->getNamespace() == NS_IMAGE
) {
28 if ($wgShowEXIF && $this->img
->exists()) {
29 $exif = $this->img
->getExifData();
30 $showmeta = count($exif) ?
true : false;
36 if ($this->img
->exists())
37 $wgOut->addHTML($this->showTOC($showmeta));
39 $this->openShowImage();
41 $wgOut->addWikiText($this->makeMetadataTable($exif));
43 # No need to display noarticletext, we use our own message, output in openShowImage()
44 if( $this->getID() ) {
47 # Just need to set the right headers
48 $wgOut->setArticleFlag( true );
49 $wgOut->setRobotpolicy( 'index,follow' );
50 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
51 $wgOut->addMetaTags();
55 $this->closeShowImage();
56 $this->imageHistory();
68 * @param bool $metadata Whether or not to show the metadata link
71 function showTOC( $metadata ) {
73 $r = '<ul id="filetoc">
74 <li><a href="#file">' . $wgLang->getNsText( NS_IMAGE
) . '</a></li>' .
75 ($metadata ?
'<li><a href="#metadata">' . wfMsg( 'metadata' ) . '</a></li>' : '') . '
76 <li><a href="#filehistory">' . wfMsg( 'imghistory' ) . '</a></li>
77 <li><a href="#filelinks">' . wfMsg( 'imagelinks' ) . '</a></li>
83 * Make a table with metadata to be shown in the output page.
87 * @param array $exif The array containing the EXIF data
90 function makeMetadataTable( $exif ) {
91 $r = "{| class=metadata align=right width=250px\n";
92 $r .= '|+ id=metadata | '. htmlspecialchars( wfMsg( 'metadata' ) ) . "\n";
93 foreach( $exif as $k => $v ) {
94 $tag = strtolower( $k );
95 $r .= "! class=$tag |" . wfMsg( "exif-$tag" ) . "\n";
96 $r .= "| class=$tag |" . htmlspecialchars( $v ) . "\n";
99 return substr($r, 0, -3) . '|}';
103 * Overloading Article's getContent method.
104 * Omit noarticletext if sharedupload
106 * @param $noredir If true, do not follow redirects
108 function getContent( $noredir )
110 if ( $this->img
&& $this->img
->fromSharedDirectory
&& 0 == $this->getID() ) {
113 return Article
::getContent( $noredir );
116 function openShowImage()
118 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
119 $wgUseImageResize, $wgRepositoryBaseUrl,
120 $wgUseExternalEditor, $wgServer;
121 $full_url = $this->img
->getViewURL();
125 if( $wgUser->getOption( 'imagesize' ) == '' ) {
126 $sizeSel = User
::getDefaultOption( 'imagesize' );
128 $sizeSel = IntVal( $wgUser->getOption( 'imagesize' ) );
130 if( !isset( $wgImageLimits[$sizeSel] ) ) {
131 $sizeSel = User
::getDefaultOption( 'imagesize' );
133 $max = $wgImageLimits[$sizeSel];
135 $maxHeight = $max[1];
136 $sk = $wgUser->getSkin();
138 if ( $this->img
->exists() ) {
140 $width = $this->img
->getWidth();
141 $height = $this->img
->getHeight();
144 if ( $this->img
->allowInlineDisplay() and $width and $height) {
147 # "Download high res version" link below the image
148 $msg = wfMsg('showbigimage', $width, $height, intval( $this->img
->getSize()/1024 ) );
149 if ( $width > $maxWidth ) {
150 $height = floor( $height * $maxWidth / $width );
153 if ( $height > $maxHeight ) {
154 $width = floor( $width * $maxHeight / $height );
155 $height = $maxHeight;
157 if ( !$this->img
->mustRender()
158 && ( $width != $this->img
->getWidth() ||
$height != $this->img
->getHeight() ) ) {
159 if( $wgUseImageResize ) {
160 $thumbnail = $this->img
->getThumbnail( $width );
161 if ( $thumbnail == null ) {
164 $url = $thumbnail->getUrl();
167 # No resize ability? Show the full image, but scale
168 # it down in the browser so it fits on the page.
171 $anchoropen = "<a href=\"{$full_url}\">";
172 $anchorclose = "</a><br />\n$anchoropen{$msg}</a>";
175 $showLink = $this->img
->mustRender();
177 $wgOut->addHTML( '<div class="fullImageLink" id="file">' . $anchoropen .
178 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
179 htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>' );
181 #if direct link is allowed but it's not a renderable image, show an icon.
182 if ($this->img
->isSafeFile()) {
183 $icon= $this->img
->iconThumb();
185 $wgOut->addHTML( '<div class="fullImageLink" id="file"><a href="' . $full_url . '">' .
195 $s= $sk->makeMediaLink( $this->img
->getName(), '', '', true );
196 $info= wfMsg( 'fileinfo', ceil($this->img
->getSize()/1024.0), $this->img
->getMimeType() );
198 if (!$this->img
->isSafeFile()) {
199 $wgOut->addHTML("<div class=\"fullMedia\">");
200 $wgOut->addHTML("<span class=\"dangerousLink\">");
202 $wgOut->addHTML("</span>");
204 $wgOut->addHTML("<span class=\"fileInfo\"> (");
205 $wgOut->addWikiText( $info, false );
206 $wgOut->addHTML(")</span>");
207 $wgOut->addHTML("</div>");
209 #this should be formated a little nicer. Is CSS sufficient?
210 $wgOut->addHTML("<div class=\"mediaWarning\">");
211 $wgOut->addWikiText( wfMsg( 'mediawarning' ) );
212 $wgOut->addHTML('</div>');
215 $wgOut->addHTML("<div class=\"fullMedia\">");
218 $wgOut->addHTML("<span class=\"fileInfo\"> (");
219 $wgOut->addWikiText( $info, false );
220 $wgOut->addHTML(")</span>");
222 $wgOut->addHTML("</div>");
226 if($this->img
->fromSharedDirectory
) {
227 $sharedtext="<div class=\"sharedUploadNotice\">" . wfMsg("sharedupload");
228 if($wgRepositoryBaseUrl) {
229 $sharedtext .= " ". wfMsg("shareduploadwiki",$wgRepositoryBaseUrl . urlencode($this->mTitle
->getDBkey()));
231 $sharedtext.="</div>";
232 $wgOut->addWikiText($sharedtext);
236 # Image does not exist
237 $wgOut->addWikiText( wfMsg( 'noimage', $this->getUploadUrl() ) );
241 function getUploadUrl() {
243 $uploadTitle = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
244 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img
->getName() ) );
248 function uploadLinksBox()
250 global $wgUser,$wgOut;
251 $sk = $wgUser->getSkin();
252 $wgOut->addHTML( '<br /><ul><li>' );
253 $wgOut->addWikiText( '<div>'. wfMsg( 'uploadnewversion', $this->getUploadUrl() ) .'</div>' );
254 $wgOut->addHTML( '</li><li>' );
255 $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle
,
256 wfMsg( 'edit-externally' ), "action=edit&externaledit=true&mode=file" ) );
257 $wgOut->addWikiText( '<div>' . wfMsg('edit-externally-help') . '</div>' );
258 $wgOut->addHTML( '</li></ul>' );
261 function closeShowImage()
268 * If the page we've just displayed is in the "Image" namespace,
269 * we follow it with an upload history of the image and its usage.
271 function imageHistory()
273 global $wgUser, $wgOut, $wgUseExternalEditor;
275 $sk = $wgUser->getSkin();
277 $line = $this->img
->nextHistoryLine();
280 $list =& new ImageHistoryList( $sk );
281 $s = $list->beginImageHistoryList() .
282 $list->imageHistoryLine( true, $line->img_timestamp
,
283 $this->mTitle
->getDBkey(), $line->img_user
,
284 $line->img_user_text
, $line->img_size
, $line->img_description
);
286 while ( $line = $this->img
->nextHistoryLine() ) {
287 $s .= $list->imageHistoryLine( false, $line->img_timestamp
,
288 $line->oi_archive_name
, $line->img_user
,
289 $line->img_user_text
, $line->img_size
, $line->img_description
);
291 $s .= $list->endImageHistoryList();
293 $wgOut->addHTML( $s );
295 # Exist check because we don't want to show this on pages where an image
296 # doesn't exist along with the noimage message, that would suck. -ævar
297 if( $wgUseExternalEditor && $this->img
->exists() ) {
298 $this->uploadLinksBox();
303 function imageLinks()
305 global $wgUser, $wgOut;
307 $wgOut->addHTML( '<h2 id="filelinks">' . wfMsg( 'imagelinks' ) . "</h2>\n" );
309 $dbr =& wfGetDB( DB_SLAVE
);
310 $page = $dbr->tableName( 'page' );
311 $imagelinks = $dbr->tableName( 'imagelinks' );
313 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
314 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id"
315 . " LIMIT 500"; # quickie emergency brake
316 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
318 if ( 0 == $dbr->numRows( $res ) ) {
319 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
322 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
324 $sk = $wgUser->getSkin();
325 while ( $s = $dbr->fetchObject( $res ) ) {
326 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
327 $link = $sk->makeKnownLinkObj( $name, "" );
328 $wgOut->addHTML( "<li>{$link}</li>\n" );
330 $wgOut->addHTML( "</ul>\n" );
335 global $wgUser, $wgOut, $wgRequest;
337 $confirm = $wgRequest->getBool( 'wpConfirmB' );
338 $image = $wgRequest->getVal( 'image' );
339 $oldimage = $wgRequest->getVal( 'oldimage' );
341 # Only sysops can delete images. Previously ordinary users could delete
342 # old revisions, but this is no longer the case.
343 if ( !$wgUser->isAllowed('delete') ) {
344 $wgOut->sysopRequired();
347 if ( wfReadOnly() ) {
348 $wgOut->readOnlyPage();
352 # Better double-check that it hasn't been deleted yet!
353 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
354 if ( ( !is_null( $image ) )
355 && ( '' == trim( $image ) ) ) {
356 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
360 $this->img
= new Image( $this->mTitle
);
362 # Deleting old images doesn't require confirmation
363 if ( !is_null( $oldimage ) ||
$confirm ) {
364 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
367 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
372 if ( !is_null( $image ) ) {
373 $q = '&image=' . urlencode( $image );
374 } else if ( !is_null( $oldimage ) ) {
375 $q = '&oldimage=' . urlencode( $oldimage );
379 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
384 global $wgOut, $wgUser, $wgContLang, $wgRequest;
385 global $wgUseSquid, $wgInternalServer, $wgPostCommitUpdateList;
386 $fname = 'ImagePage::doDelete';
388 $reason = $wgRequest->getVal( 'wpReason' );
389 $oldimage = $wgRequest->getVal( 'oldimage' );
391 $dbw =& wfGetDB( DB_MASTER
);
393 if ( !is_null( $oldimage ) ) {
394 if ( strlen( $oldimage ) < 16 ) {
395 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
398 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
399 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
403 # Invalidate description page cache
404 $this->mTitle
->invalidateCache();
409 $wgInternalServer.wfImageArchiveUrl( $oldimage ),
410 $wgInternalServer.$this->mTitle
->getFullURL()
412 wfPurgeSquidServers($urlArr);
414 $this->doDeleteOldImage( $oldimage );
415 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
416 $deleted = $oldimage;
418 $image = $this->mTitle
->getDBkey();
419 $dest = wfImageDir( $image );
420 $archive = wfImageDir( $image );
422 # Delete the image file if it exists; due to sync problems
423 # or manual trimming sometimes the file will be missing.
424 $targetFile = "{$dest}/{$image}";
425 if( file_exists( $targetFile ) && ! @unlink
( $targetFile ) ) {
426 # If the deletion operation actually failed, bug out:
427 $wgOut->fileDeleteError( $targetFile );
430 $dbw->delete( 'image', array( 'img_name' => $image ) );
431 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
433 # Purge archive URLs from the squid
435 while ( $s = $dbw->fetchObject( $res ) ) {
436 $this->doDeleteOldImage( $s->oi_archive_name
);
437 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name
);
440 # And also the HTML of all pages using this image
441 $linksTo = $this->img
->getLinksTo();
443 $u = SquidUpdate
::newFromTitles( $linksTo, $urlArr );
444 array_push( $wgPostCommitUpdateList, $u );
447 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
449 # Image itself is now gone, and database is cleaned.
450 # Now we remove the image description page.
452 $article = new Article( $this->mTitle
);
453 $article->doDeleteArticle( $reason ); # ignore errors
455 # Invalidate parser cache and client cache for pages using this image
456 # This is left until relatively late to reduce lock time
457 Title
::touchArray( $linksTo );
459 /* Delete thumbnails and refresh image metadata cache */
460 $this->img
->purgeCache();
466 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
467 $wgOut->setRobotpolicy( 'noindex,nofollow' );
469 $sk = $wgUser->getSkin();
470 $loglink = $sk->makeKnownLinkObj(
471 Title
::makeTitle( NS_SPECIAL
, 'Log/delete' ),
472 wfMsg( 'deletionlog' ) );
474 $text = wfMsg( 'deletedtext', $deleted, $loglink );
476 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
477 $wgOut->returnToMain( false, $this->mTitle
->getPrefixedText() );
480 function doDeleteOldImage( $oldimage )
484 $name = substr( $oldimage, 15 );
485 $archive = wfImageArchiveDir( $name );
487 # Delete the image if it exists. Sometimes the file will be missing
488 # due to manual intervention or weird sync problems; treat that
489 # condition gracefully and continue to delete the database entry.
490 # Also some records may end up with an empty oi_archive_name field
491 # if the original file was missing when a new upload was made;
492 # don't try to delete the directory then!
494 $targetFile = "{$archive}/{$oldimage}";
495 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink
( $targetFile ) ) {
496 # If we actually have a file and can't delete it, throw an error.
497 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
500 $log = new LogPage( 'delete' );
501 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
507 global $wgOut, $wgRequest, $wgUser;
508 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
510 $oldimage = $wgRequest->getText( 'oldimage' );
511 if ( strlen( $oldimage ) < 16 ) {
512 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
515 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
516 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
520 if ( wfReadOnly() ) {
521 $wgOut->readOnlyPage();
524 if( $wgUser->isAnon() ) {
525 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
528 if ( ! $this->mTitle
->userCanEdit() ) {
529 $wgOut->sysopRequired();
532 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
533 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
536 $name = substr( $oldimage, 15 );
538 $dest = wfImageDir( $name );
539 $archive = wfImageArchiveDir( $name );
540 $curfile = "{$dest}/{$name}";
542 if ( ! is_file( $curfile ) ) {
543 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
546 $oldver = wfTimestampNow() . "!{$name}";
548 $dbr =& wfGetDB( DB_SLAVE
);
549 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
551 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
552 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
555 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
556 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
559 # Record upload and update metadata cache
560 $img = Image
::newFromName( $name );
561 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
563 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
564 $wgOut->setRobotpolicy( 'noindex,nofollow' );
565 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
567 $descTitle = $img->getTitle();
568 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
576 class ImageHistoryList
{
577 function ImageHistoryList( &$skin ) {
578 $this->skin
=& $skin;
581 function beginImageHistoryList() {
582 $s = "\n<h2 id=\"filehistory\">" . wfMsg( 'imghistory' ) . "</h2>\n" .
583 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
587 function endImageHistoryList() {
592 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
593 global $wgUser, $wgLang, $wgContLang, $wgTitle;
595 $datetime = $wgLang->timeanddate( $timestamp, true );
596 $del = wfMsg( 'deleteimg' );
597 $delall = wfMsg( 'deleteimgcompletely' );
598 $cur = wfMsg( 'cur' );
601 $url = Image
::imageUrl( $img );
603 if ( $wgUser->isAllowed('delete') ) {
604 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
606 $style = $this->skin
->getInternalLinkAttributes( $link, $delall );
608 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
613 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
614 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
615 $token = urlencode( $wgUser->editToken( $img ) );
616 $rlink = $this->skin
->makeKnownLinkObj( $wgTitle,
617 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
618 urlencode( $img ) . "&wpEditToken=$token" );
619 $dlink = $this->skin
->makeKnownLinkObj( $wgTitle,
620 $del, 'action=delete&oldimage=' . urlencode( $img ) .
621 "&wpEditToken=$token" );
623 # Having live active links for non-logged in users
624 # means that bots and spiders crawling our site can
625 # inadvertently change content. Baaaad idea.
626 $rlink = wfMsg( 'revertimg' );
631 $userlink = $usertext;
633 $userlink = $this->skin
->makeLinkObj(
634 Title
::makeTitle( NS_USER
, $usertext ),
637 $nbytes = wfMsg( 'nbytes', $size );
638 $style = $this->skin
->getInternalLinkAttributes( $url, $datetime );
640 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
641 . " . . {$userlink} ({$nbytes})";
643 $s .= $this->skin
->commentBlock( $description, $wgTitle );