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() )
30 $this->openShowImage();
31 $this->closeShowImage();
32 if ( $this->img
->exists() )
33 $this->showEXIFdata();
36 # No need to display noarticletext, we use our own message, output in openShowImage()
37 if ( $this->getID() ) {
40 # Just need to set the right headers
41 $wgOut->setArticleFlag( true );
42 $wgOut->setRobotpolicy( 'index,follow' );
43 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
44 $wgOut->addMetaTags();
48 $this->imageHistory();
49 # Exist check because we don't want to show this on pages where an image
50 # doesn't exist along with the noimage message, that would suck. -ævar
51 if ( $wgUseExternalEditor and $this->img
->exists() )
52 $this->uploadLinksBox();
60 global $wgOut, $wgShowEXIF, $wgLang;
62 $r = '<ul id="imagetoc">
63 <li><a href="#imagetoc">' . $wgLang->getNsText( NS_IMAGE
) . '</a></li>
64 <li><a href="#imghistory">' . wfMsg( 'imghistory' ) . '</a></li>
65 <li><a href="#imagelinks">' . wfMsg( 'imagelinks' ) . '</a></li>' .
66 ($wgShowEXIF ?
'<li><a href="#metadata">' . wfMsg( 'metadata' ) . '</a></li>' : '') .
68 $wgOut->addHTML( $r );
72 function showEXIFdata() {
73 global $wgOut, $wgShowEXIF;
74 if ( ! $wgShowEXIF ) return;
77 $exif = $this->img
->getExifData();
79 return; # No EXIF data available
82 $r = "<div id='metadata'>\n{| class=metadata\n"; # For those wondering whether the div is redundant, then no, it isn't -ævar
83 $r .= '|+ ' . wfMsg( 'metadata' ) . "\n|-\n" ;
84 foreach ( $exif as $k => $v ) {
85 $r .= '! ' . wfMsg( 'exif-' . strtolower($k) ) . "\n";
89 $wgOut->addWikiText( substr($r, 0, -3) . "|}\n</div>" );
92 function openShowImage()
94 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
95 $wgUseImageResize, $wgRepositoryBaseUrl,
96 $wgUseExternalEditor, $wgServer;
97 $full_url = $this->img
->getViewURL();
101 if( $wgUser->getOption( 'imagesize' ) == '' ) {
102 $sizeSel = User
::getDefaultOption( 'imagesize' );
104 $sizeSel = IntVal( $wgUser->getOption( 'imagesize' ) );
106 if( !isset( $wgImageLimits[$sizeSel] ) ) {
107 $sizeSel = User
::getDefaultOption( 'imagesize' );
109 $max = $wgImageLimits[$sizeSel];
111 $maxHeight = $max[1];
112 $sk = $wgUser->getSkin();
114 if ( $this->img
->exists() ) {
115 if ( $this->img
->getType() != '' ) {
117 $width = $this->img
->getWidth();
118 $height = $this->img
->getHeight();
119 # "Download high res version" link below the image
120 $msg = wfMsg('showbigimage', $width, $height, intval( $this->img
->getSize()/1024 ) );
121 if ( $width > $maxWidth ) {
122 $height = floor( $height * $maxWidth / $width );
125 if ( $height > $maxHeight ) {
126 $width = floor( $width * $maxHeight / $height );
127 $height = $maxHeight;
129 if ( $width != $this->img
->getWidth() ||
$height != $this->img
->getHeight() ) {
130 if( $wgUseImageResize ) {
131 $thumbnail = $this->img
->getThumbnail( $width );
132 $url = $thumbnail->getUrl();
134 # No resize ability? Show the full image, but scale
135 # it down in the browser so it fits on the page.
138 $anchoropen = "<a href=\"{$full_url}\">";
139 $anchorclose = "</a><br />\n$anchoropen{$msg}</a>";
143 $s = '<div class="fullImageLink">' . $anchoropen .
144 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
145 htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>';
147 $s = "<div class=\"fullMedia\">" . $sk->makeMediaLink( $this->img
->getName(),'' ) . '</div>';
149 $wgOut->addHTML( $s );
150 if($this->img
->fromSharedDirectory
) {
151 $sharedtext="<div class=\"sharedUploadNotice\">" . wfMsg("sharedupload");
152 if($wgRepositoryBaseUrl) {
153 $sharedtext .= " ". wfMsg("shareduploadwiki",$wgRepositoryBaseUrl . urlencode($this->mTitle
->getDBkey()));
155 $sharedtext.="</div>";
156 $wgOut->addWikiText($sharedtext);
160 # Image does not exist
161 $wgOut->addWikiText( wfMsg( 'noimage', $this->getUploadUrl() ) );
165 function getUploadUrl() {
167 $uploadTitle = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
168 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img
->getName() ) );
172 function uploadLinksBox()
174 global $wgUser,$wgOut;
175 $sk = $wgUser->getSkin();
176 $wgOut->addHTML( '<ul><li>' );
177 $wgOut->addWikiText( '<div>'. wfMsg( 'uploadnewversion', $this->getUploadUrl() ) .'</div>' );
178 $wgOut->addHTML( '</li><li>' );
179 $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle
,
180 wfMsg( 'edit-externally' ), "action=edit&externaledit=true&mode=file" ) );
181 $wgOut->addWikiText( '<div>' . wfMsg('edit-externally-help') . '</div>' );
182 $wgOut->addHTML( '</li></ul>' );
185 function closeShowImage()
192 * If the page we've just displayed is in the "Image" namespace,
193 * we follow it with an upload history of the image and its usage.
195 function imageHistory()
197 global $wgUser, $wgOut, $wgUseExternalEditor;
199 $sk = $wgUser->getSkin();
201 $line = $this->img
->nextHistoryLine();
204 $list =& new ImageHistoryList( $sk );
205 $s = $list->beginImageHistoryList() .
206 $list->imageHistoryLine( true, $line->img_timestamp
,
207 $this->mTitle
->getDBkey(), $line->img_user
,
208 $line->img_user_text
, $line->img_size
, $line->img_description
);
209 while ( $line = $this->img
->nextHistoryLine() ) {
210 $s .= $list->imageHistoryLine( false, $line->img_timestamp
,
211 $line->oi_archive_name
, $line->img_user
,
212 $line->img_user_text
, $line->img_size
, $line->img_description
);
214 $s .= $list->endImageHistoryList();
216 $wgOut->addHTML( $s );
219 function imageLinks()
221 global $wgUser, $wgOut;
223 $wgOut->addHTML( '<h2 id="imagelinks">' . wfMsg( 'imagelinks' ) . "</h2>\n" );
225 $dbr =& wfGetDB( DB_SLAVE
);
226 $page = $dbr->tableName( 'page' );
227 $imagelinks = $dbr->tableName( 'imagelinks' );
229 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
230 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id"
231 . " LIMIT 500"; # quickie emergency brake
232 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
234 if ( 0 == $dbr->numRows( $res ) ) {
235 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
238 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
240 $sk = $wgUser->getSkin();
241 while ( $s = $dbr->fetchObject( $res ) ) {
242 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
243 $link = $sk->makeKnownLinkObj( $name, "" );
244 $wgOut->addHTML( "<li>{$link}</li>\n" );
246 $wgOut->addHTML( "</ul>\n" );
251 global $wgUser, $wgOut, $wgRequest;
253 $confirm = $wgRequest->getBool( 'wpConfirmB' );
254 $image = $wgRequest->getVal( 'image' );
255 $oldimage = $wgRequest->getVal( 'oldimage' );
257 # Only sysops can delete images. Previously ordinary users could delete
258 # old revisions, but this is no longer the case.
259 if ( !$wgUser->isAllowed('delete') ) {
260 $wgOut->sysopRequired();
263 if ( wfReadOnly() ) {
264 $wgOut->readOnlyPage();
268 # Better double-check that it hasn't been deleted yet!
269 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
270 if ( ( !is_null( $image ) )
271 && ( '' == trim( $image ) ) ) {
272 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
276 $this->img
= new Image( $this->mTitle
);
278 # Deleting old images doesn't require confirmation
279 if ( !is_null( $oldimage ) ||
$confirm ) {
280 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
283 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
288 if ( !is_null( $image ) ) {
289 $q = '&image=' . urlencode( $image );
290 } else if ( !is_null( $oldimage ) ) {
291 $q = '&oldimage=' . urlencode( $oldimage );
295 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
300 global $wgOut, $wgUser, $wgContLang, $wgRequest;
301 global $wgUseSquid, $wgInternalServer, $wgPostCommitUpdateList;
302 $fname = 'ImagePage::doDelete';
304 $reason = $wgRequest->getVal( 'wpReason' );
305 $oldimage = $wgRequest->getVal( 'oldimage' );
307 $dbw =& wfGetDB( DB_MASTER
);
309 if ( !is_null( $oldimage ) ) {
310 if ( strlen( $oldimage ) < 16 ) {
311 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
314 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
315 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
319 # Invalidate description page cache
320 $this->mTitle
->invalidateCache();
325 $wgInternalServer.wfImageArchiveUrl( $oldimage ),
326 $wgInternalServer.$this->mTitle
->getFullURL()
328 wfPurgeSquidServers($urlArr);
330 $this->doDeleteOldImage( $oldimage );
331 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
332 $deleted = $oldimage;
334 $image = $this->mTitle
->getDBkey();
335 $dest = wfImageDir( $image );
336 $archive = wfImageDir( $image );
338 # Delete the image file if it exists; due to sync problems
339 # or manual trimming sometimes the file will be missing.
340 $targetFile = "{$dest}/{$image}";
341 if( file_exists( $targetFile ) && ! @unlink
( $targetFile ) ) {
342 # If the deletion operation actually failed, bug out:
343 $wgOut->fileDeleteError( $targetFile );
346 $dbw->delete( 'image', array( 'img_name' => $image ) );
347 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
349 # Purge archive URLs from the squid
351 while ( $s = $dbw->fetchObject( $res ) ) {
352 $this->doDeleteOldImage( $s->oi_archive_name
);
353 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name
);
356 # And also the HTML of all pages using this image
357 $linksTo = $this->img
->getLinksTo();
359 $u = SquidUpdate
::newFromTitles( $linksTo, $urlArr );
360 array_push( $wgPostCommitUpdateList, $u );
363 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
365 # Image itself is now gone, and database is cleaned.
366 # Now we remove the image description page.
368 $article = new Article( $this->mTitle
);
369 $article->doDeleteArticle( $reason ); # ignore errors
371 # Invalidate parser cache and client cache for pages using this image
372 # This is left until relatively late to reduce lock time
373 Title
::touchArray( $linksTo );
375 /* Delete thumbnails and refresh image metadata cache */
376 $this->img
->purgeCache();
382 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
383 $wgOut->setRobotpolicy( 'noindex,nofollow' );
385 $sk = $wgUser->getSkin();
386 $loglink = $sk->makeKnownLinkObj(
387 Title
::makeTitle( NS_SPECIAL
, 'Log/delete' ),
388 wfMsg( 'deletionlog' ) );
390 $text = wfMsg( 'deletedtext', $deleted, $loglink );
392 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
393 $wgOut->returnToMain( false, $this->mTitle
->getPrefixedText() );
396 function doDeleteOldImage( $oldimage )
400 $name = substr( $oldimage, 15 );
401 $archive = wfImageArchiveDir( $name );
403 # Delete the image if it exists. Sometimes the file will be missing
404 # due to manual intervention or weird sync problems; treat that
405 # condition gracefully and continue to delete the database entry.
406 # Also some records may end up with an empty oi_archive_name field
407 # if the original file was missing when a new upload was made;
408 # don't try to delete the directory then!
410 $targetFile = "{$archive}/{$oldimage}";
411 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink
( $targetFile ) ) {
412 # If we actually have a file and can't delete it, throw an error.
413 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
416 $log = new LogPage( 'delete' );
417 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
423 global $wgOut, $wgRequest, $wgUser;
424 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
426 $oldimage = $wgRequest->getText( 'oldimage' );
427 if ( strlen( $oldimage ) < 16 ) {
428 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
431 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
432 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
436 if ( wfReadOnly() ) {
437 $wgOut->readOnlyPage();
440 if( $wgUser->isAnon() ) {
441 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
444 if ( ! $this->mTitle
->userCanEdit() ) {
445 $wgOut->sysopRequired();
448 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
449 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
452 $name = substr( $oldimage, 15 );
454 $dest = wfImageDir( $name );
455 $archive = wfImageArchiveDir( $name );
456 $curfile = "{$dest}/{$name}";
458 if ( ! is_file( $curfile ) ) {
459 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
462 $oldver = wfTimestampNow() . "!{$name}";
464 $dbr =& wfGetDB( DB_SLAVE
);
465 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
467 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
468 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
471 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
472 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
475 # Record upload and update metadata cache
476 $img = Image
::newFromName( $name );
477 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
479 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
480 $wgOut->setRobotpolicy( 'noindex,nofollow' );
481 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
483 $descTitle = $img->getTitle();
484 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
492 class ImageHistoryList
{
493 function ImageHistoryList( &$skin ) {
494 $this->skin
=& $skin;
497 function beginImageHistoryList() {
498 $s = "\n<h2 id=\"imghistory\">" . wfMsg( 'imghistory' ) . "</h2>\n" .
499 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
503 function endImageHistoryList() {
508 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
509 global $wgUser, $wgLang, $wgContLang, $wgTitle;
511 $datetime = $wgLang->timeanddate( $timestamp, true );
512 $del = wfMsg( 'deleteimg' );
513 $delall = wfMsg( 'deleteimgcompletely' );
514 $cur = wfMsg( 'cur' );
517 $url = Image
::imageUrl( $img );
519 if ( $wgUser->isAllowed('delete') ) {
520 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
522 $style = $this->skin
->getInternalLinkAttributes( $link, $delall );
524 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
529 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
530 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
531 $token = urlencode( $wgUser->editToken( $img ) );
532 $rlink = $this->skin
->makeKnownLinkObj( $wgTitle,
533 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
534 urlencode( $img ) . "&wpEditToken=$token" );
535 $dlink = $this->skin
->makeKnownLinkObj( $wgTitle,
536 $del, 'action=delete&oldimage=' . urlencode( $img ) .
537 "&wpEditToken=$token" );
539 # Having live active links for non-logged in users
540 # means that bots and spiders crawling our site can
541 # inadvertently change content. Baaaad idea.
542 $rlink = wfMsg( 'revertimg' );
547 $userlink = $usertext;
549 $userlink = $this->skin
->makeLinkObj(
550 Title
::makeTitle( NS_USER
, $usertext ),
553 $nbytes = wfMsg( 'nbytes', $size );
554 $style = $this->skin
->getInternalLinkAttributes( $url, $datetime );
556 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
557 . " . . {$userlink} ({$nbytes})";
559 $s .= $this->skin
->commentBlock( $description, $wgTitle );