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() ) {
139 if ( $this->img
->getType() ) {
141 $width = $this->img
->getWidth();
142 $height = $this->img
->getHeight();
143 # "Download high res version" link below the image
144 $msg = wfMsg('showbigimage', $width, $height, intval( $this->img
->getSize()/1024 ) );
145 if ( $width > $maxWidth ) {
146 $height = floor( $height * $maxWidth / $width );
149 if ( $height > $maxHeight ) {
150 $width = floor( $width * $maxHeight / $height );
151 $height = $maxHeight;
153 if ( $width != $this->img
->getWidth() ||
$height != $this->img
->getHeight() ) {
154 if( $wgUseImageResize ) {
155 $thumbnail = $this->img
->getThumbnail( $width );
156 $url = $thumbnail->getUrl();
158 # No resize ability? Show the full image, but scale
159 # it down in the browser so it fits on the page.
162 $anchoropen = "<a href=\"{$full_url}\">";
163 $anchorclose = "</a><br />\n$anchoropen{$msg}</a>";
167 $s = '<div class="fullImageLink" id="file">' . $anchoropen .
168 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
169 htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>';
171 $s = "<div class=\"fullMedia\">" . $sk->makeMediaLink( $this->img
->getName(),'' ) . '</div>';
173 $wgOut->addHTML( $s );
174 if($this->img
->fromSharedDirectory
) {
175 $sharedtext="<div class=\"sharedUploadNotice\">" . wfMsg("sharedupload");
176 if($wgRepositoryBaseUrl) {
177 $sharedtext .= " ". wfMsg("shareduploadwiki",$wgRepositoryBaseUrl . urlencode($this->mTitle
->getDBkey()));
179 $sharedtext.="</div>";
180 $wgOut->addWikiText($sharedtext);
184 # Image does not exist
185 $wgOut->addWikiText( wfMsg( 'noimage', $this->getUploadUrl() ) );
189 function getUploadUrl() {
191 $uploadTitle = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
192 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img
->getName() ) );
196 function uploadLinksBox()
198 global $wgUser,$wgOut;
199 $sk = $wgUser->getSkin();
200 $wgOut->addHTML( '<br /><ul><li>' );
201 $wgOut->addWikiText( '<div>'. wfMsg( 'uploadnewversion', $this->getUploadUrl() ) .'</div>' );
202 $wgOut->addHTML( '</li><li>' );
203 $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle
,
204 wfMsg( 'edit-externally' ), "action=edit&externaledit=true&mode=file" ) );
205 $wgOut->addWikiText( '<div>' . wfMsg('edit-externally-help') . '</div>' );
206 $wgOut->addHTML( '</li></ul>' );
209 function closeShowImage()
216 * If the page we've just displayed is in the "Image" namespace,
217 * we follow it with an upload history of the image and its usage.
219 function imageHistory()
221 global $wgUser, $wgOut, $wgUseExternalEditor;
223 $sk = $wgUser->getSkin();
225 $line = $this->img
->nextHistoryLine();
228 $list =& new ImageHistoryList( $sk );
229 $s = $list->beginImageHistoryList() .
230 $list->imageHistoryLine( true, $line->img_timestamp
,
231 $this->mTitle
->getDBkey(), $line->img_user
,
232 $line->img_user_text
, $line->img_size
, $line->img_description
);
234 while ( $line = $this->img
->nextHistoryLine() ) {
235 $s .= $list->imageHistoryLine( false, $line->img_timestamp
,
236 $line->oi_archive_name
, $line->img_user
,
237 $line->img_user_text
, $line->img_size
, $line->img_description
);
239 $s .= $list->endImageHistoryList();
241 $wgOut->addHTML( $s );
243 # Exist check because we don't want to show this on pages where an image
244 # doesn't exist along with the noimage message, that would suck. -ævar
245 if( $wgUseExternalEditor && $this->img
->exists() ) {
246 $this->uploadLinksBox();
251 function imageLinks()
253 global $wgUser, $wgOut;
255 $wgOut->addHTML( '<h2 id="filelinks">' . wfMsg( 'imagelinks' ) . "</h2>\n" );
257 $dbr =& wfGetDB( DB_SLAVE
);
258 $page = $dbr->tableName( 'page' );
259 $imagelinks = $dbr->tableName( 'imagelinks' );
261 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
262 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id"
263 . " LIMIT 500"; # quickie emergency brake
264 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
266 if ( 0 == $dbr->numRows( $res ) ) {
267 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
270 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
272 $sk = $wgUser->getSkin();
273 while ( $s = $dbr->fetchObject( $res ) ) {
274 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
275 $link = $sk->makeKnownLinkObj( $name, "" );
276 $wgOut->addHTML( "<li>{$link}</li>\n" );
278 $wgOut->addHTML( "</ul>\n" );
283 global $wgUser, $wgOut, $wgRequest;
285 $confirm = $wgRequest->getBool( 'wpConfirmB' );
286 $image = $wgRequest->getVal( 'image' );
287 $oldimage = $wgRequest->getVal( 'oldimage' );
289 # Only sysops can delete images. Previously ordinary users could delete
290 # old revisions, but this is no longer the case.
291 if ( !$wgUser->isAllowed('delete') ) {
292 $wgOut->sysopRequired();
295 if ( wfReadOnly() ) {
296 $wgOut->readOnlyPage();
300 # Better double-check that it hasn't been deleted yet!
301 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
302 if ( ( !is_null( $image ) )
303 && ( '' == trim( $image ) ) ) {
304 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
308 $this->img
= new Image( $this->mTitle
);
310 # Deleting old images doesn't require confirmation
311 if ( !is_null( $oldimage ) ||
$confirm ) {
312 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
315 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
320 if ( !is_null( $image ) ) {
321 $q = '&image=' . urlencode( $image );
322 } else if ( !is_null( $oldimage ) ) {
323 $q = '&oldimage=' . urlencode( $oldimage );
327 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
332 global $wgOut, $wgUser, $wgContLang, $wgRequest;
333 global $wgUseSquid, $wgInternalServer, $wgPostCommitUpdateList;
334 $fname = 'ImagePage::doDelete';
336 $reason = $wgRequest->getVal( 'wpReason' );
337 $oldimage = $wgRequest->getVal( 'oldimage' );
339 $dbw =& wfGetDB( DB_MASTER
);
341 if ( !is_null( $oldimage ) ) {
342 if ( strlen( $oldimage ) < 16 ) {
343 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
346 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
347 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
351 # Invalidate description page cache
352 $this->mTitle
->invalidateCache();
357 $wgInternalServer.wfImageArchiveUrl( $oldimage ),
358 $wgInternalServer.$this->mTitle
->getFullURL()
360 wfPurgeSquidServers($urlArr);
362 $this->doDeleteOldImage( $oldimage );
363 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
364 $deleted = $oldimage;
366 $image = $this->mTitle
->getDBkey();
367 $dest = wfImageDir( $image );
368 $archive = wfImageDir( $image );
370 # Delete the image file if it exists; due to sync problems
371 # or manual trimming sometimes the file will be missing.
372 $targetFile = "{$dest}/{$image}";
373 if( file_exists( $targetFile ) && ! @unlink
( $targetFile ) ) {
374 # If the deletion operation actually failed, bug out:
375 $wgOut->fileDeleteError( $targetFile );
378 $dbw->delete( 'image', array( 'img_name' => $image ) );
379 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
381 # Purge archive URLs from the squid
383 while ( $s = $dbw->fetchObject( $res ) ) {
384 $this->doDeleteOldImage( $s->oi_archive_name
);
385 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name
);
388 # And also the HTML of all pages using this image
389 $linksTo = $this->img
->getLinksTo();
391 $u = SquidUpdate
::newFromTitles( $linksTo, $urlArr );
392 array_push( $wgPostCommitUpdateList, $u );
395 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
397 # Image itself is now gone, and database is cleaned.
398 # Now we remove the image description page.
400 $article = new Article( $this->mTitle
);
401 $article->doDeleteArticle( $reason ); # ignore errors
403 # Invalidate parser cache and client cache for pages using this image
404 # This is left until relatively late to reduce lock time
405 Title
::touchArray( $linksTo );
407 /* Delete thumbnails and refresh image metadata cache */
408 $this->img
->purgeCache();
414 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
415 $wgOut->setRobotpolicy( 'noindex,nofollow' );
417 $sk = $wgUser->getSkin();
418 $loglink = $sk->makeKnownLinkObj(
419 Title
::makeTitle( NS_SPECIAL
, 'Log/delete' ),
420 wfMsg( 'deletionlog' ) );
422 $text = wfMsg( 'deletedtext', $deleted, $loglink );
424 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
425 $wgOut->returnToMain( false, $this->mTitle
->getPrefixedText() );
428 function doDeleteOldImage( $oldimage )
432 $name = substr( $oldimage, 15 );
433 $archive = wfImageArchiveDir( $name );
435 # Delete the image if it exists. Sometimes the file will be missing
436 # due to manual intervention or weird sync problems; treat that
437 # condition gracefully and continue to delete the database entry.
438 # Also some records may end up with an empty oi_archive_name field
439 # if the original file was missing when a new upload was made;
440 # don't try to delete the directory then!
442 $targetFile = "{$archive}/{$oldimage}";
443 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink
( $targetFile ) ) {
444 # If we actually have a file and can't delete it, throw an error.
445 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
448 $log = new LogPage( 'delete' );
449 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
455 global $wgOut, $wgRequest, $wgUser;
456 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
458 $oldimage = $wgRequest->getText( 'oldimage' );
459 if ( strlen( $oldimage ) < 16 ) {
460 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
463 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
464 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
468 if ( wfReadOnly() ) {
469 $wgOut->readOnlyPage();
472 if( $wgUser->isAnon() ) {
473 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
476 if ( ! $this->mTitle
->userCanEdit() ) {
477 $wgOut->sysopRequired();
480 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
481 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
484 $name = substr( $oldimage, 15 );
486 $dest = wfImageDir( $name );
487 $archive = wfImageArchiveDir( $name );
488 $curfile = "{$dest}/{$name}";
490 if ( ! is_file( $curfile ) ) {
491 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
494 $oldver = wfTimestampNow() . "!{$name}";
496 $dbr =& wfGetDB( DB_SLAVE
);
497 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
499 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
500 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
503 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
504 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
507 # Record upload and update metadata cache
508 $img = Image
::newFromName( $name );
509 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
511 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
512 $wgOut->setRobotpolicy( 'noindex,nofollow' );
513 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
515 $descTitle = $img->getTitle();
516 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
524 class ImageHistoryList
{
525 function ImageHistoryList( &$skin ) {
526 $this->skin
=& $skin;
529 function beginImageHistoryList() {
530 $s = "\n<h2 id=\"filehistory\">" . wfMsg( 'imghistory' ) . "</h2>\n" .
531 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
535 function endImageHistoryList() {
540 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
541 global $wgUser, $wgLang, $wgContLang, $wgTitle;
543 $datetime = $wgLang->timeanddate( $timestamp, true );
544 $del = wfMsg( 'deleteimg' );
545 $delall = wfMsg( 'deleteimgcompletely' );
546 $cur = wfMsg( 'cur' );
549 $url = Image
::imageUrl( $img );
551 if ( $wgUser->isAllowed('delete') ) {
552 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
554 $style = $this->skin
->getInternalLinkAttributes( $link, $delall );
556 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
561 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
562 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
563 $token = urlencode( $wgUser->editToken( $img ) );
564 $rlink = $this->skin
->makeKnownLinkObj( $wgTitle,
565 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
566 urlencode( $img ) . "&wpEditToken=$token" );
567 $dlink = $this->skin
->makeKnownLinkObj( $wgTitle,
568 $del, 'action=delete&oldimage=' . urlencode( $img ) .
569 "&wpEditToken=$token" );
571 # Having live active links for non-logged in users
572 # means that bots and spiders crawling our site can
573 # inadvertently change content. Baaaad idea.
574 $rlink = wfMsg( 'revertimg' );
579 $userlink = $usertext;
581 $userlink = $this->skin
->makeLinkObj(
582 Title
::makeTitle( NS_USER
, $usertext ),
585 $nbytes = wfMsg( 'nbytes', $size );
586 $style = $this->skin
->getInternalLinkAttributes( $url, $datetime );
588 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
589 . " . . {$userlink} ({$nbytes})";
591 $s .= $this->skin
->commentBlock( $description, $wgTitle );