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() ) {
49 $this->showEXIFdata();
57 global $wgOut, $wgShowEXIF, $wgLang;
59 $r= '<div class="imagepagetoc"><table class="toc"><tr><td><b>'.htmlspecialchars( wfMsg( 'toc' )) .'</b><br />';
60 $r .= '<a href="#toc">' . htmlspecialchars( $wgLang->getNStext( NS_IMAGE
) ) . '</a> - ';
61 $r .= '<a href="#imghistory">' . htmlspecialchars( wfMsg( 'imghistory' ) ) . '</a> - ';
62 $r .= '<a href="#imagelinks">' . htmlspecialchars( wfMsg( 'imagelinks' ) ) . '</a>';
64 $r .= ' - <a href="#exifdata">' . htmlspecialchars( wfMsg( 'exifdata' ) ) . '</a>';
68 $wgOut->addHTML( $r . '</td></tr></table></div>' );
72 function showEXIFdata() {
73 global $wgOut, $wgShowEXIF;
79 $exif = $this->img
->getExifData();
80 if( count ( $exif ) == 0 ) {
81 return; # No EXIF data available
85 $r = '<h2 id="exifdata">'. htmlspecialchars( wfMsg( 'exifdata' ) ) . "</h2>\n";
86 $r .= "<table class=\"exif\">\n" ;
88 foreach( $exif as $k => $v ) {
92 $r .= '<th>' . wfMsg( 'exif-' . strtolower( $k ) ) . "</th>\n";
93 $r .= '<td>' . htmlspecialchars( $v ) . "</td>\n";
97 $r .= "<td class='spacer'> </td>\n";
102 $r .= "<th></th><td></td></tr>\n";
105 $wgOut->addHTML( $r . "</table>\n" );
108 function openShowImage()
110 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
111 $wgUseImageResize, $wgRepositoryBaseUrl,
112 $wgUseExternalEditor, $wgServer;
113 $full_url = $this->img
->getViewURL();
117 if( $wgUser->getOption( 'imagesize' ) == '' ) {
118 $sizeSel = User
::getDefaultOption( 'imagesize' );
120 $sizeSel = IntVal( $wgUser->getOption( 'imagesize' ) );
122 if( !isset( $wgImageLimits[$sizeSel] ) ) {
123 $sizeSel = User
::getDefaultOption( 'imagesize' );
125 $max = $wgImageLimits[$sizeSel];
127 $maxHeight = $max[1];
128 $sk = $wgUser->getSkin();
130 if ( $this->img
->exists() ) {
131 if ( $this->img
->getType() ) {
133 $width = $this->img
->getWidth();
134 $height = $this->img
->getHeight();
135 # "Download high res version" link below the image
136 $msg = wfMsg('showbigimage', $width, $height, intval( $this->img
->getSize()/1024 ) );
137 if ( $width > $maxWidth ) {
138 $height = floor( $height * $maxWidth / $width );
141 if ( $height > $maxHeight ) {
142 $width = floor( $width * $maxHeight / $height );
143 $height = $maxHeight;
145 if ( $width != $this->img
->getWidth() ||
$height != $this->img
->getHeight() ) {
146 if( $wgUseImageResize ) {
147 $thumbnail = $this->img
->getThumbnail( $width );
148 $url = $thumbnail->getUrl();
150 # No resize ability? Show the full image, but scale
151 # it down in the browser so it fits on the page.
154 $anchoropen = "<a href=\"{$full_url}\">";
155 $anchorclose = "</a><br />\n$anchoropen{$msg}</a>";
159 $s = '<div class="fullImageLink">' . $anchoropen .
160 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
161 htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>';
163 $s = "<div class=\"fullMedia\">" . $sk->makeMediaLink( $this->img
->getName(),'' ) . '</div>';
165 $wgOut->addHTML( $s );
166 if($this->img
->fromSharedDirectory
) {
167 $sharedtext="<div class=\"sharedUploadNotice\">" . wfMsg("sharedupload");
168 if($wgRepositoryBaseUrl) {
169 $sharedtext .= " ". wfMsg("shareduploadwiki",$wgRepositoryBaseUrl . urlencode($this->mTitle
->getDBkey()));
171 $sharedtext.="</div>";
172 $wgOut->addWikiText($sharedtext);
176 # Image does not exist
177 $wgOut->addWikiText( wfMsg( 'noimage', $this->getUploadUrl() ) );
181 function getUploadUrl() {
183 $uploadTitle = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
184 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img
->getName() ) );
188 function uploadLinksBox()
190 global $wgUser,$wgOut;
191 $sk = $wgUser->getSkin();
192 $wgOut->addHTML( '<br /><ul><li>' );
193 $wgOut->addWikiText( '<div>'. wfMsg( 'uploadnewversion', $this->getUploadUrl() ) .'</div>' );
194 $wgOut->addHTML( '</li><li>' );
195 $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle
,
196 wfMsg( 'edit-externally' ), "action=edit&externaledit=true&mode=file" ) );
197 $wgOut->addWikiText( '<div>' . wfMsg('edit-externally-help') . '</div>' );
198 $wgOut->addHTML( '</li></ul>' );
201 function closeShowImage()
208 * If the page we've just displayed is in the "Image" namespace,
209 * we follow it with an upload history of the image and its usage.
211 function imageHistory()
213 global $wgUser, $wgOut, $wgUseExternalEditor;
215 $sk = $wgUser->getSkin();
217 $line = $this->img
->nextHistoryLine();
220 $list =& new ImageHistoryList( $sk );
221 $s = $list->beginImageHistoryList() .
222 $list->imageHistoryLine( true, $line->img_timestamp
,
223 $this->mTitle
->getDBkey(), $line->img_user
,
224 $line->img_user_text
, $line->img_size
, $line->img_description
);
226 while ( $line = $this->img
->nextHistoryLine() ) {
227 $s .= $list->imageHistoryLine( false, $line->img_timestamp
,
228 $line->oi_archive_name
, $line->img_user
,
229 $line->img_user_text
, $line->img_size
, $line->img_description
);
231 $s .= $list->endImageHistoryList();
233 $wgOut->addHTML( $s );
235 # Exist check because we don't want to show this on pages where an image
236 # doesn't exist along with the noimage message, that would suck. -ævar
237 if( $wgUseExternalEditor && $this->img
->exists() ) {
238 $this->uploadLinksBox();
243 function imageLinks()
245 global $wgUser, $wgOut;
247 $wgOut->addHTML( '<h2 id="imagelinks">' . wfMsg( 'imagelinks' ) . "</h2>\n" );
249 $dbr =& wfGetDB( DB_SLAVE
);
250 $page = $dbr->tableName( 'page' );
251 $imagelinks = $dbr->tableName( 'imagelinks' );
253 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
254 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id"
255 . " LIMIT 500"; # quickie emergency brake
256 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
258 if ( 0 == $dbr->numRows( $res ) ) {
259 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
262 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
264 $sk = $wgUser->getSkin();
265 while ( $s = $dbr->fetchObject( $res ) ) {
266 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
267 $link = $sk->makeKnownLinkObj( $name, "" );
268 $wgOut->addHTML( "<li>{$link}</li>\n" );
270 $wgOut->addHTML( "</ul>\n" );
275 global $wgUser, $wgOut, $wgRequest;
277 $confirm = $wgRequest->getBool( 'wpConfirmB' );
278 $image = $wgRequest->getVal( 'image' );
279 $oldimage = $wgRequest->getVal( 'oldimage' );
281 # Only sysops can delete images. Previously ordinary users could delete
282 # old revisions, but this is no longer the case.
283 if ( !$wgUser->isAllowed('delete') ) {
284 $wgOut->sysopRequired();
287 if ( wfReadOnly() ) {
288 $wgOut->readOnlyPage();
292 # Better double-check that it hasn't been deleted yet!
293 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
294 if ( ( !is_null( $image ) )
295 && ( '' == trim( $image ) ) ) {
296 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
300 $this->img
= new Image( $this->mTitle
);
302 # Deleting old images doesn't require confirmation
303 if ( !is_null( $oldimage ) ||
$confirm ) {
304 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
307 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
312 if ( !is_null( $image ) ) {
313 $q = '&image=' . urlencode( $image );
314 } else if ( !is_null( $oldimage ) ) {
315 $q = '&oldimage=' . urlencode( $oldimage );
319 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
324 global $wgOut, $wgUser, $wgContLang, $wgRequest;
325 global $wgUseSquid, $wgInternalServer, $wgPostCommitUpdateList;
326 $fname = 'ImagePage::doDelete';
328 $reason = $wgRequest->getVal( 'wpReason' );
329 $oldimage = $wgRequest->getVal( 'oldimage' );
331 $dbw =& wfGetDB( DB_MASTER
);
333 if ( !is_null( $oldimage ) ) {
334 if ( strlen( $oldimage ) < 16 ) {
335 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
338 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
339 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
343 # Invalidate description page cache
344 $this->mTitle
->invalidateCache();
349 $wgInternalServer.wfImageArchiveUrl( $oldimage ),
350 $wgInternalServer.$this->mTitle
->getFullURL()
352 wfPurgeSquidServers($urlArr);
354 $this->doDeleteOldImage( $oldimage );
355 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
356 $deleted = $oldimage;
358 $image = $this->mTitle
->getDBkey();
359 $dest = wfImageDir( $image );
360 $archive = wfImageDir( $image );
362 # Delete the image file if it exists; due to sync problems
363 # or manual trimming sometimes the file will be missing.
364 $targetFile = "{$dest}/{$image}";
365 if( file_exists( $targetFile ) && ! @unlink
( $targetFile ) ) {
366 # If the deletion operation actually failed, bug out:
367 $wgOut->fileDeleteError( $targetFile );
370 $dbw->delete( 'image', array( 'img_name' => $image ) );
371 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
373 # Purge archive URLs from the squid
375 while ( $s = $dbw->fetchObject( $res ) ) {
376 $this->doDeleteOldImage( $s->oi_archive_name
);
377 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name
);
380 # And also the HTML of all pages using this image
381 $linksTo = $this->img
->getLinksTo();
383 $u = SquidUpdate
::newFromTitles( $linksTo, $urlArr );
384 array_push( $wgPostCommitUpdateList, $u );
387 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
389 # Image itself is now gone, and database is cleaned.
390 # Now we remove the image description page.
392 $article = new Article( $this->mTitle
);
393 $article->doDeleteArticle( $reason ); # ignore errors
395 # Invalidate parser cache and client cache for pages using this image
396 # This is left until relatively late to reduce lock time
397 Title
::touchArray( $linksTo );
399 /* Delete thumbnails and refresh image metadata cache */
400 $this->img
->purgeCache();
406 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
407 $wgOut->setRobotpolicy( 'noindex,nofollow' );
409 $sk = $wgUser->getSkin();
410 $loglink = $sk->makeKnownLinkObj(
411 Title
::makeTitle( NS_SPECIAL
, 'Log/delete' ),
412 wfMsg( 'deletionlog' ) );
414 $text = wfMsg( 'deletedtext', $deleted, $loglink );
416 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
417 $wgOut->returnToMain( false, $this->mTitle
->getPrefixedText() );
420 function doDeleteOldImage( $oldimage )
424 $name = substr( $oldimage, 15 );
425 $archive = wfImageArchiveDir( $name );
427 # Delete the image if it exists. Sometimes the file will be missing
428 # due to manual intervention or weird sync problems; treat that
429 # condition gracefully and continue to delete the database entry.
430 # Also some records may end up with an empty oi_archive_name field
431 # if the original file was missing when a new upload was made;
432 # don't try to delete the directory then!
434 $targetFile = "{$archive}/{$oldimage}";
435 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink
( $targetFile ) ) {
436 # If we actually have a file and can't delete it, throw an error.
437 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
440 $log = new LogPage( 'delete' );
441 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
447 global $wgOut, $wgRequest, $wgUser;
448 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
450 $oldimage = $wgRequest->getText( 'oldimage' );
451 if ( strlen( $oldimage ) < 16 ) {
452 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
455 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
456 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
460 if ( wfReadOnly() ) {
461 $wgOut->readOnlyPage();
464 if( $wgUser->isAnon() ) {
465 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
468 if ( ! $this->mTitle
->userCanEdit() ) {
469 $wgOut->sysopRequired();
472 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
473 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
476 $name = substr( $oldimage, 15 );
478 $dest = wfImageDir( $name );
479 $archive = wfImageArchiveDir( $name );
480 $curfile = "{$dest}/{$name}";
482 if ( ! is_file( $curfile ) ) {
483 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
486 $oldver = wfTimestampNow() . "!{$name}";
488 $dbr =& wfGetDB( DB_SLAVE
);
489 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
491 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
492 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
495 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
496 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
499 # Record upload and update metadata cache
500 $img = Image
::newFromName( $name );
501 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
503 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
504 $wgOut->setRobotpolicy( 'noindex,nofollow' );
505 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
507 $descTitle = $img->getTitle();
508 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
516 class ImageHistoryList
{
517 function ImageHistoryList( &$skin ) {
518 $this->skin
=& $skin;
521 function beginImageHistoryList() {
522 $s = "\n<h2 id=\"imghistory\">" . wfMsg( 'imghistory' ) . "</h2>\n" .
523 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
527 function endImageHistoryList() {
532 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
533 global $wgUser, $wgLang, $wgContLang, $wgTitle;
535 $datetime = $wgLang->timeanddate( $timestamp, true );
536 $del = wfMsg( 'deleteimg' );
537 $delall = wfMsg( 'deleteimgcompletely' );
538 $cur = wfMsg( 'cur' );
541 $url = Image
::imageUrl( $img );
543 if ( $wgUser->isAllowed('delete') ) {
544 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
546 $style = $this->skin
->getInternalLinkAttributes( $link, $delall );
548 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
553 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
554 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
555 $token = urlencode( $wgUser->editToken( $img ) );
556 $rlink = $this->skin
->makeKnownLinkObj( $wgTitle,
557 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
558 urlencode( $img ) . "&wpEditToken=$token" );
559 $dlink = $this->skin
->makeKnownLinkObj( $wgTitle,
560 $del, 'action=delete&oldimage=' . urlencode( $img ) .
561 "&wpEditToken=$token" );
563 # Having live active links for non-logged in users
564 # means that bots and spiders crawling our site can
565 # inadvertently change content. Baaaad idea.
566 $rlink = wfMsg( 'revertimg' );
571 $userlink = $usertext;
573 $userlink = $this->skin
->makeLinkObj(
574 Title
::makeTitle( NS_USER
, $usertext ),
577 $nbytes = wfMsg( 'nbytes', $size );
578 $style = $this->skin
->getInternalLinkAttributes( $url, $datetime );
580 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
581 . " . . {$userlink} ({$nbytes})";
583 $s .= $this->skin
->commentBlock( $description, $wgTitle );