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 $url = $thumbnail->getUrl();
163 # No resize ability? Show the full image, but scale
164 # it down in the browser so it fits on the page.
167 $anchoropen = "<a href=\"{$full_url}\">";
168 $anchorclose = "</a><br />\n$anchoropen{$msg}</a>";
171 $showLink = $this->img
->mustRender();
173 $wgOut->addHTML( '<div class="fullImageLink" id="file">' . $anchoropen .
174 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
175 htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>' );
177 #if direct link is allowed but it's not a renderable image, show an icon.
178 if ($this->img
->isSafeFile()) {
179 $icon= $this->img
->iconThumb();
181 $wgOut->addHTML( '<div class="fullImageLink" id="file"><a href="' . $full_url . '">' .
191 $s= $sk->makeMediaLink( $this->img
->getName(), '', '', true );
192 $info= wfMsg( 'fileinfo', ceil($this->img
->getSize()/1024.0), $this->img
->getMimeType() );
194 if (!$this->img
->isSafeFile()) {
195 $wgOut->addHTML("<div class=\"fullMedia\">");
196 $wgOut->addHTML("<span class=\"dangerousLink\">");
198 $wgOut->addHTML("</span>");
200 $wgOut->addHTML("<span class=\"fileInfo\"> (");
201 $wgOut->addWikiText( $info, false );
202 $wgOut->addHTML(")</span>");
203 $wgOut->addHTML("</div>");
205 #this should be formated a little nicer. Is CSS sufficient?
206 $wgOut->addHTML("<div class=\"mediaWarning\">");
207 $wgOut->addWikiText( wfMsg( 'mediawarning' ) );
208 $wgOut->addHTML('</div>');
211 $wgOut->addHTML("<div class=\"fullMedia\">");
214 $wgOut->addHTML("<span class=\"fileInfo\"> (");
215 $wgOut->addWikiText( $info, false );
216 $wgOut->addHTML(")</span>");
218 $wgOut->addHTML("</div>");
222 if($this->img
->fromSharedDirectory
) {
223 $sharedtext="<div class=\"sharedUploadNotice\">" . wfMsg("sharedupload");
224 if($wgRepositoryBaseUrl) {
225 $sharedtext .= " ". wfMsg("shareduploadwiki",$wgRepositoryBaseUrl . urlencode($this->mTitle
->getDBkey()));
227 $sharedtext.="</div>";
228 $wgOut->addWikiText($sharedtext);
232 # Image does not exist
233 $wgOut->addWikiText( wfMsg( 'noimage', $this->getUploadUrl() ) );
237 function getUploadUrl() {
239 $uploadTitle = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
240 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img
->getName() ) );
244 function uploadLinksBox()
246 global $wgUser,$wgOut;
247 $sk = $wgUser->getSkin();
248 $wgOut->addHTML( '<br /><ul><li>' );
249 $wgOut->addWikiText( '<div>'. wfMsg( 'uploadnewversion', $this->getUploadUrl() ) .'</div>' );
250 $wgOut->addHTML( '</li><li>' );
251 $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle
,
252 wfMsg( 'edit-externally' ), "action=edit&externaledit=true&mode=file" ) );
253 $wgOut->addWikiText( '<div>' . wfMsg('edit-externally-help') . '</div>' );
254 $wgOut->addHTML( '</li></ul>' );
257 function closeShowImage()
264 * If the page we've just displayed is in the "Image" namespace,
265 * we follow it with an upload history of the image and its usage.
267 function imageHistory()
269 global $wgUser, $wgOut, $wgUseExternalEditor;
271 $sk = $wgUser->getSkin();
273 $line = $this->img
->nextHistoryLine();
276 $list =& new ImageHistoryList( $sk );
277 $s = $list->beginImageHistoryList() .
278 $list->imageHistoryLine( true, $line->img_timestamp
,
279 $this->mTitle
->getDBkey(), $line->img_user
,
280 $line->img_user_text
, $line->img_size
, $line->img_description
);
282 while ( $line = $this->img
->nextHistoryLine() ) {
283 $s .= $list->imageHistoryLine( false, $line->img_timestamp
,
284 $line->oi_archive_name
, $line->img_user
,
285 $line->img_user_text
, $line->img_size
, $line->img_description
);
287 $s .= $list->endImageHistoryList();
289 $wgOut->addHTML( $s );
291 # Exist check because we don't want to show this on pages where an image
292 # doesn't exist along with the noimage message, that would suck. -ævar
293 if( $wgUseExternalEditor && $this->img
->exists() ) {
294 $this->uploadLinksBox();
299 function imageLinks()
301 global $wgUser, $wgOut;
303 $wgOut->addHTML( '<h2 id="filelinks">' . wfMsg( 'imagelinks' ) . "</h2>\n" );
305 $dbr =& wfGetDB( DB_SLAVE
);
306 $page = $dbr->tableName( 'page' );
307 $imagelinks = $dbr->tableName( 'imagelinks' );
309 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
310 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id"
311 . " LIMIT 500"; # quickie emergency brake
312 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
314 if ( 0 == $dbr->numRows( $res ) ) {
315 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
318 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
320 $sk = $wgUser->getSkin();
321 while ( $s = $dbr->fetchObject( $res ) ) {
322 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
323 $link = $sk->makeKnownLinkObj( $name, "" );
324 $wgOut->addHTML( "<li>{$link}</li>\n" );
326 $wgOut->addHTML( "</ul>\n" );
331 global $wgUser, $wgOut, $wgRequest;
333 $confirm = $wgRequest->getBool( 'wpConfirmB' );
334 $image = $wgRequest->getVal( 'image' );
335 $oldimage = $wgRequest->getVal( 'oldimage' );
337 # Only sysops can delete images. Previously ordinary users could delete
338 # old revisions, but this is no longer the case.
339 if ( !$wgUser->isAllowed('delete') ) {
340 $wgOut->sysopRequired();
343 if ( wfReadOnly() ) {
344 $wgOut->readOnlyPage();
348 # Better double-check that it hasn't been deleted yet!
349 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
350 if ( ( !is_null( $image ) )
351 && ( '' == trim( $image ) ) ) {
352 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
356 $this->img
= new Image( $this->mTitle
);
358 # Deleting old images doesn't require confirmation
359 if ( !is_null( $oldimage ) ||
$confirm ) {
360 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
363 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
368 if ( !is_null( $image ) ) {
369 $q = '&image=' . urlencode( $image );
370 } else if ( !is_null( $oldimage ) ) {
371 $q = '&oldimage=' . urlencode( $oldimage );
375 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
380 global $wgOut, $wgUser, $wgContLang, $wgRequest;
381 global $wgUseSquid, $wgInternalServer, $wgPostCommitUpdateList;
382 $fname = 'ImagePage::doDelete';
384 $reason = $wgRequest->getVal( 'wpReason' );
385 $oldimage = $wgRequest->getVal( 'oldimage' );
387 $dbw =& wfGetDB( DB_MASTER
);
389 if ( !is_null( $oldimage ) ) {
390 if ( strlen( $oldimage ) < 16 ) {
391 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
394 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
395 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
399 # Invalidate description page cache
400 $this->mTitle
->invalidateCache();
405 $wgInternalServer.wfImageArchiveUrl( $oldimage ),
406 $wgInternalServer.$this->mTitle
->getFullURL()
408 wfPurgeSquidServers($urlArr);
410 $this->doDeleteOldImage( $oldimage );
411 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
412 $deleted = $oldimage;
414 $image = $this->mTitle
->getDBkey();
415 $dest = wfImageDir( $image );
416 $archive = wfImageDir( $image );
418 # Delete the image file if it exists; due to sync problems
419 # or manual trimming sometimes the file will be missing.
420 $targetFile = "{$dest}/{$image}";
421 if( file_exists( $targetFile ) && ! @unlink
( $targetFile ) ) {
422 # If the deletion operation actually failed, bug out:
423 $wgOut->fileDeleteError( $targetFile );
426 $dbw->delete( 'image', array( 'img_name' => $image ) );
427 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
429 # Purge archive URLs from the squid
431 while ( $s = $dbw->fetchObject( $res ) ) {
432 $this->doDeleteOldImage( $s->oi_archive_name
);
433 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name
);
436 # And also the HTML of all pages using this image
437 $linksTo = $this->img
->getLinksTo();
439 $u = SquidUpdate
::newFromTitles( $linksTo, $urlArr );
440 array_push( $wgPostCommitUpdateList, $u );
443 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
445 # Image itself is now gone, and database is cleaned.
446 # Now we remove the image description page.
448 $article = new Article( $this->mTitle
);
449 $article->doDeleteArticle( $reason ); # ignore errors
451 # Invalidate parser cache and client cache for pages using this image
452 # This is left until relatively late to reduce lock time
453 Title
::touchArray( $linksTo );
455 /* Delete thumbnails and refresh image metadata cache */
456 $this->img
->purgeCache();
462 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
463 $wgOut->setRobotpolicy( 'noindex,nofollow' );
465 $sk = $wgUser->getSkin();
466 $loglink = $sk->makeKnownLinkObj(
467 Title
::makeTitle( NS_SPECIAL
, 'Log/delete' ),
468 wfMsg( 'deletionlog' ) );
470 $text = wfMsg( 'deletedtext', $deleted, $loglink );
472 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
473 $wgOut->returnToMain( false, $this->mTitle
->getPrefixedText() );
476 function doDeleteOldImage( $oldimage )
480 $name = substr( $oldimage, 15 );
481 $archive = wfImageArchiveDir( $name );
483 # Delete the image if it exists. Sometimes the file will be missing
484 # due to manual intervention or weird sync problems; treat that
485 # condition gracefully and continue to delete the database entry.
486 # Also some records may end up with an empty oi_archive_name field
487 # if the original file was missing when a new upload was made;
488 # don't try to delete the directory then!
490 $targetFile = "{$archive}/{$oldimage}";
491 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink
( $targetFile ) ) {
492 # If we actually have a file and can't delete it, throw an error.
493 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
496 $log = new LogPage( 'delete' );
497 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
503 global $wgOut, $wgRequest, $wgUser;
504 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
506 $oldimage = $wgRequest->getText( 'oldimage' );
507 if ( strlen( $oldimage ) < 16 ) {
508 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
511 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
512 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
516 if ( wfReadOnly() ) {
517 $wgOut->readOnlyPage();
520 if( $wgUser->isAnon() ) {
521 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
524 if ( ! $this->mTitle
->userCanEdit() ) {
525 $wgOut->sysopRequired();
528 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
529 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
532 $name = substr( $oldimage, 15 );
534 $dest = wfImageDir( $name );
535 $archive = wfImageArchiveDir( $name );
536 $curfile = "{$dest}/{$name}";
538 if ( ! is_file( $curfile ) ) {
539 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
542 $oldver = wfTimestampNow() . "!{$name}";
544 $dbr =& wfGetDB( DB_SLAVE
);
545 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
547 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
548 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
551 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
552 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
555 # Record upload and update metadata cache
556 $img = Image
::newFromName( $name );
557 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
559 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
560 $wgOut->setRobotpolicy( 'noindex,nofollow' );
561 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
563 $descTitle = $img->getTitle();
564 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
572 class ImageHistoryList
{
573 function ImageHistoryList( &$skin ) {
574 $this->skin
=& $skin;
577 function beginImageHistoryList() {
578 $s = "\n<h2 id=\"filehistory\">" . wfMsg( 'imghistory' ) . "</h2>\n" .
579 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
583 function endImageHistoryList() {
588 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
589 global $wgUser, $wgLang, $wgContLang, $wgTitle;
591 $datetime = $wgLang->timeanddate( $timestamp, true );
592 $del = wfMsg( 'deleteimg' );
593 $delall = wfMsg( 'deleteimgcompletely' );
594 $cur = wfMsg( 'cur' );
597 $url = Image
::imageUrl( $img );
599 if ( $wgUser->isAllowed('delete') ) {
600 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
602 $style = $this->skin
->getInternalLinkAttributes( $link, $delall );
604 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
609 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
610 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
611 $token = urlencode( $wgUser->editToken( $img ) );
612 $rlink = $this->skin
->makeKnownLinkObj( $wgTitle,
613 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
614 urlencode( $img ) . "&wpEditToken=$token" );
615 $dlink = $this->skin
->makeKnownLinkObj( $wgTitle,
616 $del, 'action=delete&oldimage=' . urlencode( $img ) .
617 "&wpEditToken=$token" );
619 # Having live active links for non-logged in users
620 # means that bots and spiders crawling our site can
621 # inadvertently change content. Baaaad idea.
622 $rlink = wfMsg( 'revertimg' );
627 $userlink = $usertext;
629 $userlink = $this->skin
->makeLinkObj(
630 Title
::makeTitle( NS_USER
, $usertext ),
633 $nbytes = wfMsg( 'nbytes', $size );
634 $style = $this->skin
->getInternalLinkAttributes( $url, $datetime );
636 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
637 . " . . {$userlink} ({$nbytes})";
639 $s .= $this->skin
->commentBlock( $description, $wgTitle );