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) . '|}';
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" id="file">' . $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 # Exist check because we don't want to show this on pages where an image
230 # doesn't exist along with the noimage message, that would suck. -ævar
231 if( $wgUseExternalEditor && $this->img
->exists() ) {
232 $this->uploadLinksBox();
237 function imageLinks()
239 global $wgUser, $wgOut;
241 $wgOut->addHTML( '<h2 id="filelinks">' . wfMsg( 'imagelinks' ) . "</h2>\n" );
243 $dbr =& wfGetDB( DB_SLAVE
);
244 $page = $dbr->tableName( 'page' );
245 $imagelinks = $dbr->tableName( 'imagelinks' );
247 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
248 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id"
249 . " LIMIT 500"; # quickie emergency brake
250 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
252 if ( 0 == $dbr->numRows( $res ) ) {
253 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
256 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
258 $sk = $wgUser->getSkin();
259 while ( $s = $dbr->fetchObject( $res ) ) {
260 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
261 $link = $sk->makeKnownLinkObj( $name, "" );
262 $wgOut->addHTML( "<li>{$link}</li>\n" );
264 $wgOut->addHTML( "</ul>\n" );
269 global $wgUser, $wgOut, $wgRequest;
271 $confirm = $wgRequest->getBool( 'wpConfirmB' );
272 $image = $wgRequest->getVal( 'image' );
273 $oldimage = $wgRequest->getVal( 'oldimage' );
275 # Only sysops can delete images. Previously ordinary users could delete
276 # old revisions, but this is no longer the case.
277 if ( !$wgUser->isAllowed('delete') ) {
278 $wgOut->sysopRequired();
281 if ( wfReadOnly() ) {
282 $wgOut->readOnlyPage();
286 # Better double-check that it hasn't been deleted yet!
287 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
288 if ( ( !is_null( $image ) )
289 && ( '' == trim( $image ) ) ) {
290 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
294 $this->img
= new Image( $this->mTitle
);
296 # Deleting old images doesn't require confirmation
297 if ( !is_null( $oldimage ) ||
$confirm ) {
298 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
301 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
306 if ( !is_null( $image ) ) {
307 $q = '&image=' . urlencode( $image );
308 } else if ( !is_null( $oldimage ) ) {
309 $q = '&oldimage=' . urlencode( $oldimage );
313 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
318 global $wgOut, $wgUser, $wgContLang, $wgRequest;
319 global $wgUseSquid, $wgInternalServer, $wgPostCommitUpdateList;
320 $fname = 'ImagePage::doDelete';
322 $reason = $wgRequest->getVal( 'wpReason' );
323 $oldimage = $wgRequest->getVal( 'oldimage' );
325 $dbw =& wfGetDB( DB_MASTER
);
327 if ( !is_null( $oldimage ) ) {
328 if ( strlen( $oldimage ) < 16 ) {
329 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
332 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
333 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
337 # Invalidate description page cache
338 $this->mTitle
->invalidateCache();
343 $wgInternalServer.wfImageArchiveUrl( $oldimage ),
344 $wgInternalServer.$this->mTitle
->getFullURL()
346 wfPurgeSquidServers($urlArr);
348 $this->doDeleteOldImage( $oldimage );
349 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
350 $deleted = $oldimage;
352 $image = $this->mTitle
->getDBkey();
353 $dest = wfImageDir( $image );
354 $archive = wfImageDir( $image );
356 # Delete the image file if it exists; due to sync problems
357 # or manual trimming sometimes the file will be missing.
358 $targetFile = "{$dest}/{$image}";
359 if( file_exists( $targetFile ) && ! @unlink
( $targetFile ) ) {
360 # If the deletion operation actually failed, bug out:
361 $wgOut->fileDeleteError( $targetFile );
364 $dbw->delete( 'image', array( 'img_name' => $image ) );
365 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
367 # Purge archive URLs from the squid
369 while ( $s = $dbw->fetchObject( $res ) ) {
370 $this->doDeleteOldImage( $s->oi_archive_name
);
371 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name
);
374 # And also the HTML of all pages using this image
375 $linksTo = $this->img
->getLinksTo();
377 $u = SquidUpdate
::newFromTitles( $linksTo, $urlArr );
378 array_push( $wgPostCommitUpdateList, $u );
381 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
383 # Image itself is now gone, and database is cleaned.
384 # Now we remove the image description page.
386 $article = new Article( $this->mTitle
);
387 $article->doDeleteArticle( $reason ); # ignore errors
389 # Invalidate parser cache and client cache for pages using this image
390 # This is left until relatively late to reduce lock time
391 Title
::touchArray( $linksTo );
393 /* Delete thumbnails and refresh image metadata cache */
394 $this->img
->purgeCache();
400 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
401 $wgOut->setRobotpolicy( 'noindex,nofollow' );
403 $sk = $wgUser->getSkin();
404 $loglink = $sk->makeKnownLinkObj(
405 Title
::makeTitle( NS_SPECIAL
, 'Log/delete' ),
406 wfMsg( 'deletionlog' ) );
408 $text = wfMsg( 'deletedtext', $deleted, $loglink );
410 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
411 $wgOut->returnToMain( false, $this->mTitle
->getPrefixedText() );
414 function doDeleteOldImage( $oldimage )
418 $name = substr( $oldimage, 15 );
419 $archive = wfImageArchiveDir( $name );
421 # Delete the image if it exists. Sometimes the file will be missing
422 # due to manual intervention or weird sync problems; treat that
423 # condition gracefully and continue to delete the database entry.
424 # Also some records may end up with an empty oi_archive_name field
425 # if the original file was missing when a new upload was made;
426 # don't try to delete the directory then!
428 $targetFile = "{$archive}/{$oldimage}";
429 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink
( $targetFile ) ) {
430 # If we actually have a file and can't delete it, throw an error.
431 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
434 $log = new LogPage( 'delete' );
435 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
441 global $wgOut, $wgRequest, $wgUser;
442 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
444 $oldimage = $wgRequest->getText( 'oldimage' );
445 if ( strlen( $oldimage ) < 16 ) {
446 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
449 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
450 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
454 if ( wfReadOnly() ) {
455 $wgOut->readOnlyPage();
458 if( $wgUser->isAnon() ) {
459 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
462 if ( ! $this->mTitle
->userCanEdit() ) {
463 $wgOut->sysopRequired();
466 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
467 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
470 $name = substr( $oldimage, 15 );
472 $dest = wfImageDir( $name );
473 $archive = wfImageArchiveDir( $name );
474 $curfile = "{$dest}/{$name}";
476 if ( ! is_file( $curfile ) ) {
477 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
480 $oldver = wfTimestampNow() . "!{$name}";
482 $dbr =& wfGetDB( DB_SLAVE
);
483 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
485 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
486 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
489 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
490 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
493 # Record upload and update metadata cache
494 $img = Image
::newFromName( $name );
495 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
497 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
498 $wgOut->setRobotpolicy( 'noindex,nofollow' );
499 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
501 $descTitle = $img->getTitle();
502 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
510 class ImageHistoryList
{
511 function ImageHistoryList( &$skin ) {
512 $this->skin
=& $skin;
515 function beginImageHistoryList() {
516 $s = "\n<h2 id=\"filehistory\">" . wfMsg( 'imghistory' ) . "</h2>\n" .
517 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
521 function endImageHistoryList() {
526 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
527 global $wgUser, $wgLang, $wgContLang, $wgTitle;
529 $datetime = $wgLang->timeanddate( $timestamp, true );
530 $del = wfMsg( 'deleteimg' );
531 $delall = wfMsg( 'deleteimgcompletely' );
532 $cur = wfMsg( 'cur' );
535 $url = Image
::imageUrl( $img );
537 if ( $wgUser->isAllowed('delete') ) {
538 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
540 $style = $this->skin
->getInternalLinkAttributes( $link, $delall );
542 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
547 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
548 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
549 $token = urlencode( $wgUser->editToken( $img ) );
550 $rlink = $this->skin
->makeKnownLinkObj( $wgTitle,
551 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
552 urlencode( $img ) . "&wpEditToken=$token" );
553 $dlink = $this->skin
->makeKnownLinkObj( $wgTitle,
554 $del, 'action=delete&oldimage=' . urlencode( $img ) .
555 "&wpEditToken=$token" );
557 # Having live active links for non-logged in users
558 # means that bots and spiders crawling our site can
559 # inadvertently change content. Baaaad idea.
560 $rlink = wfMsg( 'revertimg' );
565 $userlink = $usertext;
567 $userlink = $this->skin
->makeLinkObj(
568 Title
::makeTitle( NS_USER
, $usertext ),
571 $nbytes = wfMsg( 'nbytes', $size );
572 $style = $this->skin
->getInternalLinkAttributes( $url, $datetime );
574 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
575 . " . . {$userlink} ({$nbytes})";
577 $s .= $this->skin
->commentBlock( $description, $wgTitle );