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 # No need to display noarticletext, we use our own message, output in openShowImage()
42 if( $this->getID() ) {
45 # Just need to set the right headers
46 $wgOut->setArticleFlag( true );
47 $wgOut->setRobotpolicy( 'index,follow' );
48 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
49 $wgOut->addMetaTags();
53 $this->closeShowImage();
54 $this->imageHistory();
56 $wgOut->addHTML($this->makeMetadataTable($exif));
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 <li><a href="#filehistory">' . wfMsg( 'imghistory' ) . '</a></li>
76 <li><a href="#filelinks">' . wfMsg( 'imagelinks' ) . '</a></li>' .
77 ($metadata ?
'<li><a href="#metadata">' . wfMsg( 'metadata' ) . '</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 ) {
92 $r = '<h2 id="metadata">'. htmlspecialchars( wfMsg( 'metadata' ) ) . "</h2>\n";
93 $r .= "<table class='metadata'>\n" ;
95 foreach( $exif as $k => $v ) {
99 $r .= '<th>' . wfMsg( 'exif-' . strtolower( $k ) ) . "</th>\n";
100 $r .= '<td>' . htmlspecialchars( $v ) . "</td>\n";
104 $r .= "<td class='spacer'> </td>\n";
109 $r .= "<th></th><td></td></tr>\n";
112 return "$r</table>\n";
115 function openShowImage()
117 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
118 $wgUseImageResize, $wgRepositoryBaseUrl,
119 $wgUseExternalEditor, $wgServer;
120 $full_url = $this->img
->getViewURL();
124 if( $wgUser->getOption( 'imagesize' ) == '' ) {
125 $sizeSel = User
::getDefaultOption( 'imagesize' );
127 $sizeSel = IntVal( $wgUser->getOption( 'imagesize' ) );
129 if( !isset( $wgImageLimits[$sizeSel] ) ) {
130 $sizeSel = User
::getDefaultOption( 'imagesize' );
132 $max = $wgImageLimits[$sizeSel];
134 $maxHeight = $max[1];
135 $sk = $wgUser->getSkin();
137 if ( $this->img
->exists() ) {
138 if ( $this->img
->getType() ) {
140 $width = $this->img
->getWidth();
141 $height = $this->img
->getHeight();
142 # "Download high res version" link below the image
143 $msg = wfMsg('showbigimage', $width, $height, intval( $this->img
->getSize()/1024 ) );
144 if ( $width > $maxWidth ) {
145 $height = floor( $height * $maxWidth / $width );
148 if ( $height > $maxHeight ) {
149 $width = floor( $width * $maxHeight / $height );
150 $height = $maxHeight;
152 if ( $width != $this->img
->getWidth() ||
$height != $this->img
->getHeight() ) {
153 if( $wgUseImageResize ) {
154 $thumbnail = $this->img
->getThumbnail( $width );
155 $url = $thumbnail->getUrl();
157 # No resize ability? Show the full image, but scale
158 # it down in the browser so it fits on the page.
161 $anchoropen = "<a href=\"{$full_url}\">";
162 $anchorclose = "</a><br />\n$anchoropen{$msg}</a>";
166 $s = '<div class="fullImageLink" id="file">' . $anchoropen .
167 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
168 htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>';
170 $s = "<div class=\"fullMedia\">" . $sk->makeMediaLink( $this->img
->getName(),'' ) . '</div>';
172 $wgOut->addHTML( $s );
173 if($this->img
->fromSharedDirectory
) {
174 $sharedtext="<div class=\"sharedUploadNotice\">" . wfMsg("sharedupload");
175 if($wgRepositoryBaseUrl) {
176 $sharedtext .= " ". wfMsg("shareduploadwiki",$wgRepositoryBaseUrl . urlencode($this->mTitle
->getDBkey()));
178 $sharedtext.="</div>";
179 $wgOut->addWikiText($sharedtext);
183 # Image does not exist
184 $wgOut->addWikiText( wfMsg( 'noimage', $this->getUploadUrl() ) );
188 function getUploadUrl() {
190 $uploadTitle = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
191 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img
->getName() ) );
195 function uploadLinksBox()
197 global $wgUser,$wgOut;
198 $sk = $wgUser->getSkin();
199 $wgOut->addHTML( '<br /><ul><li>' );
200 $wgOut->addWikiText( '<div>'. wfMsg( 'uploadnewversion', $this->getUploadUrl() ) .'</div>' );
201 $wgOut->addHTML( '</li><li>' );
202 $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle
,
203 wfMsg( 'edit-externally' ), "action=edit&externaledit=true&mode=file" ) );
204 $wgOut->addWikiText( '<div>' . wfMsg('edit-externally-help') . '</div>' );
205 $wgOut->addHTML( '</li></ul>' );
208 function closeShowImage()
215 * If the page we've just displayed is in the "Image" namespace,
216 * we follow it with an upload history of the image and its usage.
218 function imageHistory()
220 global $wgUser, $wgOut, $wgUseExternalEditor;
222 $sk = $wgUser->getSkin();
224 $line = $this->img
->nextHistoryLine();
227 $list =& new ImageHistoryList( $sk );
228 $s = $list->beginImageHistoryList() .
229 $list->imageHistoryLine( true, $line->img_timestamp
,
230 $this->mTitle
->getDBkey(), $line->img_user
,
231 $line->img_user_text
, $line->img_size
, $line->img_description
);
233 while ( $line = $this->img
->nextHistoryLine() ) {
234 $s .= $list->imageHistoryLine( false, $line->img_timestamp
,
235 $line->oi_archive_name
, $line->img_user
,
236 $line->img_user_text
, $line->img_size
, $line->img_description
);
238 $s .= $list->endImageHistoryList();
240 $wgOut->addHTML( $s );
242 # Exist check because we don't want to show this on pages where an image
243 # doesn't exist along with the noimage message, that would suck. -ævar
244 if( $wgUseExternalEditor && $this->img
->exists() ) {
245 $this->uploadLinksBox();
250 function imageLinks()
252 global $wgUser, $wgOut;
254 $wgOut->addHTML( '<h2 id="filelinks">' . wfMsg( 'imagelinks' ) . "</h2>\n" );
256 $dbr =& wfGetDB( DB_SLAVE
);
257 $page = $dbr->tableName( 'page' );
258 $imagelinks = $dbr->tableName( 'imagelinks' );
260 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
261 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id"
262 . " LIMIT 500"; # quickie emergency brake
263 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
265 if ( 0 == $dbr->numRows( $res ) ) {
266 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
269 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
271 $sk = $wgUser->getSkin();
272 while ( $s = $dbr->fetchObject( $res ) ) {
273 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
274 $link = $sk->makeKnownLinkObj( $name, "" );
275 $wgOut->addHTML( "<li>{$link}</li>\n" );
277 $wgOut->addHTML( "</ul>\n" );
282 global $wgUser, $wgOut, $wgRequest;
284 $confirm = $wgRequest->getBool( 'wpConfirmB' );
285 $image = $wgRequest->getVal( 'image' );
286 $oldimage = $wgRequest->getVal( 'oldimage' );
288 # Only sysops can delete images. Previously ordinary users could delete
289 # old revisions, but this is no longer the case.
290 if ( !$wgUser->isAllowed('delete') ) {
291 $wgOut->sysopRequired();
294 if ( wfReadOnly() ) {
295 $wgOut->readOnlyPage();
299 # Better double-check that it hasn't been deleted yet!
300 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
301 if ( ( !is_null( $image ) )
302 && ( '' == trim( $image ) ) ) {
303 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
307 $this->img
= new Image( $this->mTitle
);
309 # Deleting old images doesn't require confirmation
310 if ( !is_null( $oldimage ) ||
$confirm ) {
311 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
314 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
319 if ( !is_null( $image ) ) {
320 $q = '&image=' . urlencode( $image );
321 } else if ( !is_null( $oldimage ) ) {
322 $q = '&oldimage=' . urlencode( $oldimage );
326 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
331 global $wgOut, $wgUser, $wgContLang, $wgRequest;
332 global $wgUseSquid, $wgInternalServer, $wgPostCommitUpdateList;
333 $fname = 'ImagePage::doDelete';
335 $reason = $wgRequest->getVal( 'wpReason' );
336 $oldimage = $wgRequest->getVal( 'oldimage' );
338 $dbw =& wfGetDB( DB_MASTER
);
340 if ( !is_null( $oldimage ) ) {
341 if ( strlen( $oldimage ) < 16 ) {
342 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
345 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
346 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
350 # Invalidate description page cache
351 $this->mTitle
->invalidateCache();
356 $wgInternalServer.wfImageArchiveUrl( $oldimage ),
357 $wgInternalServer.$this->mTitle
->getFullURL()
359 wfPurgeSquidServers($urlArr);
361 $this->doDeleteOldImage( $oldimage );
362 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
363 $deleted = $oldimage;
365 $image = $this->mTitle
->getDBkey();
366 $dest = wfImageDir( $image );
367 $archive = wfImageDir( $image );
369 # Delete the image file if it exists; due to sync problems
370 # or manual trimming sometimes the file will be missing.
371 $targetFile = "{$dest}/{$image}";
372 if( file_exists( $targetFile ) && ! @unlink
( $targetFile ) ) {
373 # If the deletion operation actually failed, bug out:
374 $wgOut->fileDeleteError( $targetFile );
377 $dbw->delete( 'image', array( 'img_name' => $image ) );
378 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
380 # Purge archive URLs from the squid
382 while ( $s = $dbw->fetchObject( $res ) ) {
383 $this->doDeleteOldImage( $s->oi_archive_name
);
384 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name
);
387 # And also the HTML of all pages using this image
388 $linksTo = $this->img
->getLinksTo();
390 $u = SquidUpdate
::newFromTitles( $linksTo, $urlArr );
391 array_push( $wgPostCommitUpdateList, $u );
394 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
396 # Image itself is now gone, and database is cleaned.
397 # Now we remove the image description page.
399 $article = new Article( $this->mTitle
);
400 $article->doDeleteArticle( $reason ); # ignore errors
402 # Invalidate parser cache and client cache for pages using this image
403 # This is left until relatively late to reduce lock time
404 Title
::touchArray( $linksTo );
406 /* Delete thumbnails and refresh image metadata cache */
407 $this->img
->purgeCache();
413 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
414 $wgOut->setRobotpolicy( 'noindex,nofollow' );
416 $sk = $wgUser->getSkin();
417 $loglink = $sk->makeKnownLinkObj(
418 Title
::makeTitle( NS_SPECIAL
, 'Log/delete' ),
419 wfMsg( 'deletionlog' ) );
421 $text = wfMsg( 'deletedtext', $deleted, $loglink );
423 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
424 $wgOut->returnToMain( false, $this->mTitle
->getPrefixedText() );
427 function doDeleteOldImage( $oldimage )
431 $name = substr( $oldimage, 15 );
432 $archive = wfImageArchiveDir( $name );
434 # Delete the image if it exists. Sometimes the file will be missing
435 # due to manual intervention or weird sync problems; treat that
436 # condition gracefully and continue to delete the database entry.
437 # Also some records may end up with an empty oi_archive_name field
438 # if the original file was missing when a new upload was made;
439 # don't try to delete the directory then!
441 $targetFile = "{$archive}/{$oldimage}";
442 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink
( $targetFile ) ) {
443 # If we actually have a file and can't delete it, throw an error.
444 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
447 $log = new LogPage( 'delete' );
448 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
454 global $wgOut, $wgRequest, $wgUser;
455 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
457 $oldimage = $wgRequest->getText( 'oldimage' );
458 if ( strlen( $oldimage ) < 16 ) {
459 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
462 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
463 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
467 if ( wfReadOnly() ) {
468 $wgOut->readOnlyPage();
471 if( $wgUser->isAnon() ) {
472 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
475 if ( ! $this->mTitle
->userCanEdit() ) {
476 $wgOut->sysopRequired();
479 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
480 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
483 $name = substr( $oldimage, 15 );
485 $dest = wfImageDir( $name );
486 $archive = wfImageArchiveDir( $name );
487 $curfile = "{$dest}/{$name}";
489 if ( ! is_file( $curfile ) ) {
490 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
493 $oldver = wfTimestampNow() . "!{$name}";
495 $dbr =& wfGetDB( DB_SLAVE
);
496 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
498 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
499 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
502 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
503 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
506 # Record upload and update metadata cache
507 $img = Image
::newFromName( $name );
508 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
510 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
511 $wgOut->setRobotpolicy( 'noindex,nofollow' );
512 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
514 $descTitle = $img->getTitle();
515 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
523 class ImageHistoryList
{
524 function ImageHistoryList( &$skin ) {
525 $this->skin
=& $skin;
528 function beginImageHistoryList() {
529 $s = "\n<h2 id=\"filehistory\">" . wfMsg( 'imghistory' ) . "</h2>\n" .
530 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
534 function endImageHistoryList() {
539 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
540 global $wgUser, $wgLang, $wgContLang, $wgTitle;
542 $datetime = $wgLang->timeanddate( $timestamp, true );
543 $del = wfMsg( 'deleteimg' );
544 $delall = wfMsg( 'deleteimgcompletely' );
545 $cur = wfMsg( 'cur' );
548 $url = Image
::imageUrl( $img );
550 if ( $wgUser->isAllowed('delete') ) {
551 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
553 $style = $this->skin
->getInternalLinkAttributes( $link, $delall );
555 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
560 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
561 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
562 $token = urlencode( $wgUser->editToken( $img ) );
563 $rlink = $this->skin
->makeKnownLinkObj( $wgTitle,
564 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
565 urlencode( $img ) . "&wpEditToken=$token" );
566 $dlink = $this->skin
->makeKnownLinkObj( $wgTitle,
567 $del, 'action=delete&oldimage=' . urlencode( $img ) .
568 "&wpEditToken=$token" );
570 # Having live active links for non-logged in users
571 # means that bots and spiders crawling our site can
572 # inadvertently change content. Baaaad idea.
573 $rlink = wfMsg( 'revertimg' );
578 $userlink = $usertext;
580 $userlink = $this->skin
->makeLinkObj(
581 Title
::makeTitle( NS_USER
, $usertext ),
584 $nbytes = wfMsg( 'nbytes', $size );
585 $style = $this->skin
->getInternalLinkAttributes( $url, $datetime );
587 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
588 . " . . {$userlink} ({$nbytes})";
590 $s .= $this->skin
->commentBlock( $description, $wgTitle );