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();
186 $wgOut->addHTML( '<br /><ul><li>' );
187 $wgOut->addWikiText( '<div>'. wfMsg( 'uploadnewversion', $this->getUploadUrl() ) .'</div>' );
188 $wgOut->addHTML( '</li><li>' );
189 $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle
,
190 wfMsg( 'edit-externally' ), "action=edit&externaledit=true&mode=file" ) );
191 $wgOut->addWikiText( '<div>' . wfMsg('edit-externally-help') . '</div>' );
192 $wgOut->addHTML( '</li></ul>' );
195 function closeShowImage()
202 * If the page we've just displayed is in the "Image" namespace,
203 * we follow it with an upload history of the image and its usage.
205 function imageHistory()
207 global $wgUser, $wgOut, $wgUseExternalEditor;
209 $sk = $wgUser->getSkin();
211 $line = $this->img
->nextHistoryLine();
214 $list =& new ImageHistoryList( $sk );
215 $s = $list->beginImageHistoryList() .
216 $list->imageHistoryLine( true, $line->img_timestamp
,
217 $this->mTitle
->getDBkey(), $line->img_user
,
218 $line->img_user_text
, $line->img_size
, $line->img_description
);
220 while ( $line = $this->img
->nextHistoryLine() ) {
221 $s .= $list->imageHistoryLine( false, $line->img_timestamp
,
222 $line->oi_archive_name
, $line->img_user
,
223 $line->img_user_text
, $line->img_size
, $line->img_description
);
225 $s .= $list->endImageHistoryList();
227 $wgOut->addHTML( $s );
229 if ( $wgUseExternalEditor ) {
230 $this->uploadLinksBox();
235 function imageLinks()
237 global $wgUser, $wgOut;
239 $wgOut->addHTML( '<h2 id="imagelinks">' . wfMsg( 'imagelinks' ) . "</h2>\n" );
241 $dbr =& wfGetDB( DB_SLAVE
);
242 $page = $dbr->tableName( 'page' );
243 $imagelinks = $dbr->tableName( 'imagelinks' );
245 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
246 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id"
247 . " LIMIT 500"; # quickie emergency brake
248 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
250 if ( 0 == $dbr->numRows( $res ) ) {
251 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
254 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
256 $sk = $wgUser->getSkin();
257 while ( $s = $dbr->fetchObject( $res ) ) {
258 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
259 $link = $sk->makeKnownLinkObj( $name, "" );
260 $wgOut->addHTML( "<li>{$link}</li>\n" );
262 $wgOut->addHTML( "</ul>\n" );
267 global $wgUser, $wgOut, $wgRequest;
269 $confirm = $wgRequest->getBool( 'wpConfirmB' );
270 $image = $wgRequest->getVal( 'image' );
271 $oldimage = $wgRequest->getVal( 'oldimage' );
273 # Only sysops can delete images. Previously ordinary users could delete
274 # old revisions, but this is no longer the case.
275 if ( !$wgUser->isAllowed('delete') ) {
276 $wgOut->sysopRequired();
279 if ( wfReadOnly() ) {
280 $wgOut->readOnlyPage();
284 # Better double-check that it hasn't been deleted yet!
285 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
286 if ( ( !is_null( $image ) )
287 && ( '' == trim( $image ) ) ) {
288 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
292 $this->img
= new Image( $this->mTitle
);
294 # Deleting old images doesn't require confirmation
295 if ( !is_null( $oldimage ) ||
$confirm ) {
296 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
299 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
304 if ( !is_null( $image ) ) {
305 $q = '&image=' . urlencode( $image );
306 } else if ( !is_null( $oldimage ) ) {
307 $q = '&oldimage=' . urlencode( $oldimage );
311 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
316 global $wgOut, $wgUser, $wgContLang, $wgRequest;
317 global $wgUseSquid, $wgInternalServer, $wgPostCommitUpdateList;
318 $fname = 'ImagePage::doDelete';
320 $reason = $wgRequest->getVal( 'wpReason' );
321 $oldimage = $wgRequest->getVal( 'oldimage' );
323 $dbw =& wfGetDB( DB_MASTER
);
325 if ( !is_null( $oldimage ) ) {
326 if ( strlen( $oldimage ) < 16 ) {
327 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
330 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
331 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
335 # Invalidate description page cache
336 $this->mTitle
->invalidateCache();
341 $wgInternalServer.wfImageArchiveUrl( $oldimage ),
342 $wgInternalServer.$this->mTitle
->getFullURL()
344 wfPurgeSquidServers($urlArr);
346 $this->doDeleteOldImage( $oldimage );
347 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
348 $deleted = $oldimage;
350 $image = $this->mTitle
->getDBkey();
351 $dest = wfImageDir( $image );
352 $archive = wfImageDir( $image );
354 # Delete the image file if it exists; due to sync problems
355 # or manual trimming sometimes the file will be missing.
356 $targetFile = "{$dest}/{$image}";
357 if( file_exists( $targetFile ) && ! @unlink
( $targetFile ) ) {
358 # If the deletion operation actually failed, bug out:
359 $wgOut->fileDeleteError( $targetFile );
362 $dbw->delete( 'image', array( 'img_name' => $image ) );
363 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
365 # Purge archive URLs from the squid
367 while ( $s = $dbw->fetchObject( $res ) ) {
368 $this->doDeleteOldImage( $s->oi_archive_name
);
369 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name
);
372 # And also the HTML of all pages using this image
373 $linksTo = $this->img
->getLinksTo();
375 $u = SquidUpdate
::newFromTitles( $linksTo, $urlArr );
376 array_push( $wgPostCommitUpdateList, $u );
379 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
381 # Image itself is now gone, and database is cleaned.
382 # Now we remove the image description page.
384 $article = new Article( $this->mTitle
);
385 $article->doDeleteArticle( $reason ); # ignore errors
387 # Invalidate parser cache and client cache for pages using this image
388 # This is left until relatively late to reduce lock time
389 Title
::touchArray( $linksTo );
391 /* Delete thumbnails and refresh image metadata cache */
392 $this->img
->purgeCache();
398 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
399 $wgOut->setRobotpolicy( 'noindex,nofollow' );
401 $sk = $wgUser->getSkin();
402 $loglink = $sk->makeKnownLinkObj(
403 Title
::makeTitle( NS_SPECIAL
, 'Log/delete' ),
404 wfMsg( 'deletionlog' ) );
406 $text = wfMsg( 'deletedtext', $deleted, $loglink );
408 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
409 $wgOut->returnToMain( false, $this->mTitle
->getPrefixedText() );
412 function doDeleteOldImage( $oldimage )
416 $name = substr( $oldimage, 15 );
417 $archive = wfImageArchiveDir( $name );
419 # Delete the image if it exists. Sometimes the file will be missing
420 # due to manual intervention or weird sync problems; treat that
421 # condition gracefully and continue to delete the database entry.
422 # Also some records may end up with an empty oi_archive_name field
423 # if the original file was missing when a new upload was made;
424 # don't try to delete the directory then!
426 $targetFile = "{$archive}/{$oldimage}";
427 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink
( $targetFile ) ) {
428 # If we actually have a file and can't delete it, throw an error.
429 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
432 $log = new LogPage( 'delete' );
433 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
439 global $wgOut, $wgRequest, $wgUser;
440 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
442 $oldimage = $wgRequest->getText( 'oldimage' );
443 if ( strlen( $oldimage ) < 16 ) {
444 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
447 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
448 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
452 if ( wfReadOnly() ) {
453 $wgOut->readOnlyPage();
456 if( $wgUser->isAnon() ) {
457 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
460 if ( ! $this->mTitle
->userCanEdit() ) {
461 $wgOut->sysopRequired();
464 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
465 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
468 $name = substr( $oldimage, 15 );
470 $dest = wfImageDir( $name );
471 $archive = wfImageArchiveDir( $name );
472 $curfile = "{$dest}/{$name}";
474 if ( ! is_file( $curfile ) ) {
475 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
478 $oldver = wfTimestampNow() . "!{$name}";
480 $dbr =& wfGetDB( DB_SLAVE
);
481 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
483 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
484 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
487 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
488 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
491 # Record upload and update metadata cache
492 $img = Image
::newFromName( $name );
493 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
495 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
496 $wgOut->setRobotpolicy( 'noindex,nofollow' );
497 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
499 $descTitle = $img->getTitle();
500 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
508 class ImageHistoryList
{
509 function ImageHistoryList( &$skin ) {
510 $this->skin
=& $skin;
513 function beginImageHistoryList() {
514 $s = "\n<h2 id=\"imghistory\">" . wfMsg( 'imghistory' ) . "</h2>\n" .
515 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
519 function endImageHistoryList() {
524 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
525 global $wgUser, $wgLang, $wgContLang, $wgTitle;
527 $datetime = $wgLang->timeanddate( $timestamp, true );
528 $del = wfMsg( 'deleteimg' );
529 $delall = wfMsg( 'deleteimgcompletely' );
530 $cur = wfMsg( 'cur' );
533 $url = Image
::imageUrl( $img );
535 if ( $wgUser->isAllowed('delete') ) {
536 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
538 $style = $this->skin
->getInternalLinkAttributes( $link, $delall );
540 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
545 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
546 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
547 $token = urlencode( $wgUser->editToken( $img ) );
548 $rlink = $this->skin
->makeKnownLinkObj( $wgTitle,
549 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
550 urlencode( $img ) . "&wpEditToken=$token" );
551 $dlink = $this->skin
->makeKnownLinkObj( $wgTitle,
552 $del, 'action=delete&oldimage=' . urlencode( $img ) .
553 "&wpEditToken=$token" );
555 # Having live active links for non-logged in users
556 # means that bots and spiders crawling our site can
557 # inadvertently change content. Baaaad idea.
558 $rlink = wfMsg( 'revertimg' );
563 $userlink = $usertext;
565 $userlink = $this->skin
->makeLinkObj(
566 Title
::makeTitle( NS_USER
, $usertext ),
569 $nbytes = wfMsg( 'nbytes', $size );
570 $style = $this->skin
->getInternalLinkAttributes( $url, $datetime );
572 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
573 . " . . {$userlink} ({$nbytes})";
575 $s .= $this->skin
->commentBlock( $description, $wgTitle );