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 ;
25 $this->img
= new Image( $this->mTitle
);
27 if( $this->mTitle
->getNamespace() == NS_IMAGE
) {
28 if ( $this->img
->exists() ) {
31 $this->openShowImage();
33 # No need to display noarticletext, we use our own message, output in openShowImage()
34 if ( $this->getID() ) {
37 # Just need to set the right headers
38 $wgOut->setArticleFlag( true );
39 $wgOut->setRobotpolicy( 'index,follow' );
40 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
41 $wgOut->addMetaTags();
45 $this->closeShowImage();
46 $this->imageHistory();
48 if ( $this->img
->exists() ) $this->showEXIFdata();
55 global $wgOut, $wgShowEXIF, $wgLang;
57 $r= '<div class="imagepagetoc" style="float:right; margin-top:-4em;"><table id="toc" border="0"><tr><td valign="top"><b>'.wfMsg( 'toc' ).'</b><br />';
58 $r .= '<a href="#toc">' . $wgLang->getNStext( NS_IMAGE
) . '</a href> - ';
59 $r .= '<a href="#imghistory">' . wfMsg( 'imghistory' ) . '</a href> - ';
60 $r .= '<a href="#imagelinks">' . wfMsg( 'imagelinks' ) . '</a href>';
62 $r .= ' - <a href="#showexif">' . wfMsg( 'exifdata' ) . '</a>';
66 $wgOut->addHTML( $r . '</td></tr></table></div>' );
70 function showEXIFdata() {
71 global $wgOut, $wgShowEXIF;
72 if ( ! $wgShowEXIF ) return;
75 $exif = $this->img
->getExifData();
76 if ( count ( $exif ) == 0 ) return; # No EXIF data available
79 $r = '<h2 id="exifdata">'. wfMsg( 'exifdata' ) . "</h2>\n";
80 $r .= "<table class=\"exif\">\n" ;
82 foreach ( $exif as $k => $v ) {
86 $r .= "<th>$k</th>\n";
87 $r .= '<td>' . htmlspecialchars($v) . "</td>\n";
91 $r .= "<td style=\"background: white;\"> </td>\n";
96 $r .= "<td></td><td></td></tr>\n";
99 $wgOut->addHTML( $r . "</table>\n" );
102 function openShowImage()
104 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
105 $wgUseImageResize, $wgRepositoryBaseUrl,
106 $wgUseExternalEditor, $wgServer;
107 $full_url = $this->img
->getViewURL();
111 if( $wgUser->getOption( 'imagesize' ) == '' ) {
112 $sizeSel = User
::getDefaultOption( 'imagesize' );
114 $sizeSel = IntVal( $wgUser->getOption( 'imagesize' ) );
116 if( !isset( $wgImageLimits[$sizeSel] ) ) {
117 $sizeSel = User
::getDefaultOption( 'imagesize' );
119 $max = $wgImageLimits[$sizeSel];
121 $maxHeight = $max[1];
122 $sk = $wgUser->getSkin();
124 if ( $this->img
->exists() ) {
125 if ( $this->img
->getType() != '' ) {
127 $width = $this->img
->getWidth();
128 $height = $this->img
->getHeight();
129 # "Download high res version" link below the image
130 $msg = wfMsg('showbigimage', $width, $height, intval( $this->img
->getSize()/1024 ) );
131 if ( $width > $maxWidth ) {
132 $height = floor( $height * $maxWidth / $width );
135 if ( $height > $maxHeight ) {
136 $width = floor( $width * $maxHeight / $height );
137 $height = $maxHeight;
139 if ( $width != $this->img
->getWidth() ||
$height != $this->img
->getHeight() ) {
140 if( $wgUseImageResize ) {
141 $thumbnail = $this->img
->getThumbnail( $width );
142 $url = $thumbnail->getUrl();
144 # No resize ability? Show the full image, but scale
145 # it down in the browser so it fits on the page.
148 $anchoropen = "<a href=\"{$full_url}\">";
149 $anchorclose = "</a><br />\n$anchoropen{$msg}</a>";
153 $s = '<div class="fullImageLink">' . $anchoropen .
154 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
155 htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>';
157 $s = "<div class=\"fullMedia\">" . $sk->makeMediaLink( $this->img
->getName(),'' ) . '</div>';
159 $wgOut->addHTML( $s );
160 if($this->img
->fromSharedDirectory
) {
161 $sharedtext="<div class=\"sharedUploadNotice\">" . wfMsg("sharedupload");
162 if($wgRepositoryBaseUrl) {
163 $sharedtext .= " ". wfMsg("shareduploadwiki",$wgRepositoryBaseUrl . urlencode($this->mTitle
->getDBkey()));
165 $sharedtext.="</div>";
166 $wgOut->addWikiText($sharedtext);
170 # Image does not exist
171 $wgOut->addWikiText( wfMsg( 'noimage', $this->getUploadUrl() ) );
175 function getUploadUrl() {
177 $uploadTitle = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
178 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img
->getName() ) );
182 function uploadLinksBox()
184 global $wgUser,$wgOut;
185 $sk = $wgUser->getSkin();
187 $wgOut->addHTML( '<table id="toc" border="0"><tr><td valign="top">' );
188 $wgOut->addWikiText( '<div class="editExternally">'. wfMsg( 'uploadnewversion', $this->getUploadUrl() ) .'</div>' );
189 $wgOut->addHTML( '</td><td><div class="editExternally">' );
190 $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle
,
191 wfMsg( 'edit-externally' ),
192 "action=edit&externaledit=true&mode=file" ) );
193 $wgOut->addWikiText( '<div class="editExternallyHelp">' .
194 wfMsg('edit-externally-help') . '</div>' );
195 $wgOut->addHTML( '</div></td></tr></table><br clear="both">' );
197 $wgOut->addHTML( '<br /><ul><li>' );
198 $wgOut->addWikiText( '<div>'. wfMsg( 'uploadnewversion', $this->getUploadUrl() ) .'</div>' );
199 $wgOut->addHTML( '</li><li>' );
200 $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle
,
201 wfMsg( 'edit-externally' ), "action=edit&externaledit=true&mode=file" ) );
202 $wgOut->addWikiText( '<div>' . wfMsg('edit-externally-help') . '</div>' );
203 $wgOut->addHTML( '</li></ul>' );
208 function closeShowImage()
215 * If the page we've just displayed is in the "Image" namespace,
216 * we follow it with an upload history of the image and its usage.
218 function imageHistory()
220 global $wgUser, $wgOut, $wgUseExternalEditor;
222 $sk = $wgUser->getSkin();
224 $line = $this->img
->nextHistoryLine();
227 $list =& new ImageHistoryList( $sk );
228 $s = $list->beginImageHistoryList() .
229 $list->imageHistoryLine( true, $line->img_timestamp
,
230 $this->mTitle
->getDBkey(), $line->img_user
,
231 $line->img_user_text
, $line->img_size
, $line->img_description
);
233 while ( $line = $this->img
->nextHistoryLine() ) {
234 $s .= $list->imageHistoryLine( false, $line->img_timestamp
,
235 $line->oi_archive_name
, $line->img_user
,
236 $line->img_user_text
, $line->img_size
, $line->img_description
);
238 $s .= $list->endImageHistoryList();
240 $wgOut->addHTML( $s );
242 if ( $wgUseExternalEditor ) {
243 $this->uploadLinksBox();
248 function imageLinks()
250 global $wgUser, $wgOut;
252 $wgOut->addHTML( '<h2 id="imagelinks">' . wfMsg( 'imagelinks' ) . "</h2>\n" );
254 $dbr =& wfGetDB( DB_SLAVE
);
255 $page = $dbr->tableName( 'page' );
256 $imagelinks = $dbr->tableName( 'imagelinks' );
258 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
259 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id"
260 . " LIMIT 500"; # quickie emergency brake
261 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
263 if ( 0 == $dbr->numRows( $res ) ) {
264 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
267 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
269 $sk = $wgUser->getSkin();
270 while ( $s = $dbr->fetchObject( $res ) ) {
271 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
272 $link = $sk->makeKnownLinkObj( $name, "" );
273 $wgOut->addHTML( "<li>{$link}</li>\n" );
275 $wgOut->addHTML( "</ul>\n" );
280 global $wgUser, $wgOut, $wgRequest;
282 $confirm = $wgRequest->getBool( 'wpConfirmB' );
283 $image = $wgRequest->getVal( 'image' );
284 $oldimage = $wgRequest->getVal( 'oldimage' );
286 # Only sysops can delete images. Previously ordinary users could delete
287 # old revisions, but this is no longer the case.
288 if ( !$wgUser->isAllowed('delete') ) {
289 $wgOut->sysopRequired();
292 if ( wfReadOnly() ) {
293 $wgOut->readOnlyPage();
297 # Better double-check that it hasn't been deleted yet!
298 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
299 if ( ( !is_null( $image ) )
300 && ( '' == trim( $image ) ) ) {
301 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
305 $this->img
= new Image( $this->mTitle
);
307 # Deleting old images doesn't require confirmation
308 if ( !is_null( $oldimage ) ||
$confirm ) {
309 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
312 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
317 if ( !is_null( $image ) ) {
318 $q = '&image=' . urlencode( $image );
319 } else if ( !is_null( $oldimage ) ) {
320 $q = '&oldimage=' . urlencode( $oldimage );
324 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
329 global $wgOut, $wgUser, $wgContLang, $wgRequest;
330 global $wgUseSquid, $wgInternalServer, $wgPostCommitUpdateList;
331 $fname = 'ImagePage::doDelete';
333 $reason = $wgRequest->getVal( 'wpReason' );
334 $oldimage = $wgRequest->getVal( 'oldimage' );
336 $dbw =& wfGetDB( DB_MASTER
);
338 if ( !is_null( $oldimage ) ) {
339 if ( strlen( $oldimage ) < 16 ) {
340 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
343 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
344 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
348 # Invalidate description page cache
349 $this->mTitle
->invalidateCache();
354 $wgInternalServer.wfImageArchiveUrl( $oldimage ),
355 $wgInternalServer.$this->mTitle
->getFullURL()
357 wfPurgeSquidServers($urlArr);
359 $this->doDeleteOldImage( $oldimage );
360 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
361 $deleted = $oldimage;
363 $image = $this->mTitle
->getDBkey();
364 $dest = wfImageDir( $image );
365 $archive = wfImageDir( $image );
367 # Delete the image file if it exists; due to sync problems
368 # or manual trimming sometimes the file will be missing.
369 $targetFile = "{$dest}/{$image}";
370 if( file_exists( $targetFile ) && ! @unlink
( $targetFile ) ) {
371 # If the deletion operation actually failed, bug out:
372 $wgOut->fileDeleteError( $targetFile );
375 $dbw->delete( 'image', array( 'img_name' => $image ) );
376 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
378 # Purge archive URLs from the squid
380 while ( $s = $dbw->fetchObject( $res ) ) {
381 $this->doDeleteOldImage( $s->oi_archive_name
);
382 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name
);
385 # And also the HTML of all pages using this image
386 $linksTo = $this->img
->getLinksTo();
388 $u = SquidUpdate
::newFromTitles( $linksTo, $urlArr );
389 array_push( $wgPostCommitUpdateList, $u );
392 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
394 # Image itself is now gone, and database is cleaned.
395 # Now we remove the image description page.
397 $article = new Article( $this->mTitle
);
398 $article->doDeleteArticle( $reason ); # ignore errors
400 # Invalidate parser cache and client cache for pages using this image
401 # This is left until relatively late to reduce lock time
402 Title
::touchArray( $linksTo );
404 /* Delete thumbnails and refresh image metadata cache */
405 $this->img
->purgeCache();
411 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
412 $wgOut->setRobotpolicy( 'noindex,nofollow' );
414 $sk = $wgUser->getSkin();
415 $loglink = $sk->makeKnownLinkObj(
416 Title
::makeTitle( NS_SPECIAL
, 'Log/delete' ),
417 wfMsg( 'deletionlog' ) );
419 $text = wfMsg( 'deletedtext', $deleted, $loglink );
421 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
422 $wgOut->returnToMain( false, $this->mTitle
->getPrefixedText() );
425 function doDeleteOldImage( $oldimage )
429 $name = substr( $oldimage, 15 );
430 $archive = wfImageArchiveDir( $name );
432 # Delete the image if it exists. Sometimes the file will be missing
433 # due to manual intervention or weird sync problems; treat that
434 # condition gracefully and continue to delete the database entry.
435 # Also some records may end up with an empty oi_archive_name field
436 # if the original file was missing when a new upload was made;
437 # don't try to delete the directory then!
439 $targetFile = "{$archive}/{$oldimage}";
440 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink
( $targetFile ) ) {
441 # If we actually have a file and can't delete it, throw an error.
442 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
445 $log = new LogPage( 'delete' );
446 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
452 global $wgOut, $wgRequest, $wgUser;
453 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
455 $oldimage = $wgRequest->getText( 'oldimage' );
456 if ( strlen( $oldimage ) < 16 ) {
457 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
460 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
461 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
465 if ( wfReadOnly() ) {
466 $wgOut->readOnlyPage();
469 if( $wgUser->isAnon() ) {
470 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
473 if ( ! $this->mTitle
->userCanEdit() ) {
474 $wgOut->sysopRequired();
477 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
478 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
481 $name = substr( $oldimage, 15 );
483 $dest = wfImageDir( $name );
484 $archive = wfImageArchiveDir( $name );
485 $curfile = "{$dest}/{$name}";
487 if ( ! is_file( $curfile ) ) {
488 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
491 $oldver = wfTimestampNow() . "!{$name}";
493 $dbr =& wfGetDB( DB_SLAVE
);
494 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
496 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
497 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
500 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
501 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
504 # Record upload and update metadata cache
505 $img = Image
::newFromName( $name );
506 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
508 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
509 $wgOut->setRobotpolicy( 'noindex,nofollow' );
510 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
512 $descTitle = $img->getTitle();
513 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
521 class ImageHistoryList
{
522 function ImageHistoryList( &$skin ) {
523 $this->skin
=& $skin;
526 function beginImageHistoryList() {
527 $s = "\n<h2 id=\"imghistory\">" . wfMsg( 'imghistory' ) . "</h2>\n" .
528 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
532 function endImageHistoryList() {
537 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
538 global $wgUser, $wgLang, $wgContLang, $wgTitle;
540 $datetime = $wgLang->timeanddate( $timestamp, true );
541 $del = wfMsg( 'deleteimg' );
542 $delall = wfMsg( 'deleteimgcompletely' );
543 $cur = wfMsg( 'cur' );
546 $url = Image
::imageUrl( $img );
548 if ( $wgUser->isAllowed('delete') ) {
549 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
551 $style = $this->skin
->getInternalLinkAttributes( $link, $delall );
553 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
558 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
559 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
560 $token = urlencode( $wgUser->editToken( $img ) );
561 $rlink = $this->skin
->makeKnownLinkObj( $wgTitle,
562 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
563 urlencode( $img ) . "&wpEditToken=$token" );
564 $dlink = $this->skin
->makeKnownLinkObj( $wgTitle,
565 $del, 'action=delete&oldimage=' . urlencode( $img ) .
566 "&wpEditToken=$token" );
568 # Having live active links for non-logged in users
569 # means that bots and spiders crawling our site can
570 # inadvertently change content. Baaaad idea.
571 $rlink = wfMsg( 'revertimg' );
576 $userlink = $usertext;
578 $userlink = $this->skin
->makeLinkObj(
579 Title
::makeTitle( NS_USER
, $usertext ),
582 $nbytes = wfMsg( 'nbytes', $size );
583 $style = $this->skin
->getInternalLinkAttributes( $url, $datetime );
585 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
586 . " . . {$userlink} ({$nbytes})";
588 $s .= $this->skin
->commentBlock( $description, $wgTitle );