6abf063e859e0e4e08d9b9ca2dfa5f82959beb6d
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 $this->openShowImage();
29 if ( $this->img
->exists() ) $this->showEXIFdata();
31 # No need to display noarticletext, we use our own message, output in openShowImage()
32 if ( $this->getID() ) {
35 # Just need to set the right headers
36 $wgOut->setArticleFlag( true );
37 $wgOut->setRobotpolicy( 'index,follow' );
38 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
39 $wgOut->addMetaTags();
43 if ( $this->img
->exists() ) {
44 $this->uploadNewVersionLink();
45 if ( $wgUseExternalEditor && $this->img
->exists() ) {
46 $this->externalEditorLink();
49 $this->closeShowImage();
50 $this->imageHistory();
57 function showEXIFdata() {
58 global $wgOut, $wgShowEXIF;
59 if ( ! $wgShowEXIF ) return;
62 $exif = $this->img
->getExifData();
63 if ( count ( $exif ) == 0 ) return; # No EXIF data available
66 $r = "{| class=exif\n" ;
67 $r .= '|+ ' . wfMsg( 'exifdata' ) . "\n|-\n" ;
68 foreach ( $exif as $k => $v ) {
70 $r .= '| ' . htmlspecialchars($v) . "\n|-\n";
72 $wgOut->addWikiText( substr($r, 0, -3) . '|}' );
75 function openShowImage()
77 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
78 $wgUseImageResize, $wgRepositoryBaseUrl,
79 $wgUseExternalEditor, $wgServer;
80 $full_url = $this->img
->getViewURL();
84 if( $wgUser->getOption( 'imagesize' ) == '' ) {
85 $sizeSel = User
::getDefaultOption( 'imagesize' );
87 $sizeSel = IntVal( $wgUser->getOption( 'imagesize' ) );
89 if( !isset( $wgImageLimits[$sizeSel] ) ) {
90 $sizeSel = User
::getDefaultOption( 'imagesize' );
92 $max = $wgImageLimits[$sizeSel];
95 $sk = $wgUser->getSkin();
97 if ( $this->img
->exists() ) {
98 if ( $this->img
->getType() != '' ) {
100 $width = $this->img
->getWidth();
101 $height = $this->img
->getHeight();
102 $msg = wfMsg('showbigimage', $width, $height, intval( $this->img
->getSize()/1024 ) );
103 if ( $width > $maxWidth ) {
104 $height = floor( $height * $maxWidth / $width );
107 if ( $height > $maxHeight ) {
108 $width = floor( $width * $maxHeight / $height );
109 $height = $maxHeight;
111 if ( $width != $this->img
->getWidth() ||
$height != $this->img
->getHeight() ) {
112 if( $wgUseImageResize ) {
113 $thumbnail = $this->img
->getThumbnail( $width );
114 $url = $thumbnail->getUrl();
116 # No resize ability? Show the full image, but scale
117 # it down in the browser so it fits on the page.
120 $anchoropen = "<a href=\"{$full_url}\">";
121 $anchorclose = "</a><br />\n$anchoropen{$msg}</a>";
125 $s = '<div class="fullImageLink">' . $anchoropen .
126 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
127 htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>';
129 $s = "<div class=\"fullMedia\">" . $sk->makeMediaLink( $this->img
->getName(),'' ) . '</div>';
131 $wgOut->addHTML( $s );
132 if($this->img
->fromSharedDirectory
) {
133 $sharedtext="<div class=\"sharedUploadNotice\">" . wfMsg("sharedupload");
134 if($wgRepositoryBaseUrl) {
135 $sharedtext .= " ". wfMsg("shareduploadwiki",$wgRepositoryBaseUrl . urlencode($this->mTitle
->getDBkey()));
137 $sharedtext.="</div>";
138 $wgOut->addWikiText($sharedtext);
142 # Image does not exist
143 $wgOut->addWikiText( wfMsg( 'noimage', $this->getUploadUrl() ) );
147 function getUploadUrl() {
149 $uploadTitle = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
150 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img
->getName() ) );
154 function uploadNewVersionLink() {
156 $wgOut->addHTML("<div class='editExternally'>");
157 $wgOut->addWikiText( wfMsg( 'uploadnewversion', $this->getUploadUrl() ) );
158 $wgOut->addHTML("</div><br clear='both' />");
161 function externalEditorLink()
163 global $wgUser,$wgOut;
164 $sk = $wgUser->getSkin();
165 $wgOut->addHTML( '<div class="editExternally">' );
166 $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle
,
167 wfMsg( 'edit-externally' ),
168 "action=edit&externaledit=true&mode=file" ) );
169 $wgOut->addWikiText( '<div class="editExternallyHelp">' .
170 wfMsg('edit-externally-help') . '</div>' );
171 $wgOut->addHTML( '</div><br clear="all" />' );
174 function closeShowImage()
181 * If the page we've just displayed is in the "Image" namespace,
182 * we follow it with an upload history of the image and its usage.
184 function imageHistory()
186 global $wgUser, $wgOut;
188 $sk = $wgUser->getSkin();
190 $line = $this->img
->nextHistoryLine();
193 $list =& new ImageHistoryList( $sk );
194 $s = $list->beginImageHistoryList() .
195 $list->imageHistoryLine( true, $line->img_timestamp
,
196 $this->mTitle
->getDBkey(), $line->img_user
,
197 $line->img_user_text
, $line->img_size
, $line->img_description
);
199 while ( $line = $this->img
->nextHistoryLine() ) {
200 $s .= $list->imageHistoryLine( false, $line->img_timestamp
,
201 $line->oi_archive_name
, $line->img_user
,
202 $line->img_user_text
, $line->img_size
, $line->img_description
);
204 $s .= $list->endImageHistoryList();
206 $wgOut->addHTML( $s );
209 function imageLinks()
211 global $wgUser, $wgOut;
213 $wgOut->addHTML( '<h2>' . wfMsg( 'imagelinks' ) . "</h2>\n" );
215 $dbr =& wfGetDB( DB_SLAVE
);
216 $page = $dbr->tableName( 'page' );
217 $imagelinks = $dbr->tableName( 'imagelinks' );
219 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
220 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id"
221 . " LIMIT 500"; # quickie emergency brake
222 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
224 if ( 0 == $dbr->numRows( $res ) ) {
225 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
228 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
230 $sk = $wgUser->getSkin();
231 while ( $s = $dbr->fetchObject( $res ) ) {
232 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
233 $link = $sk->makeKnownLinkObj( $name, "" );
234 $wgOut->addHTML( "<li>{$link}</li>\n" );
236 $wgOut->addHTML( "</ul>\n" );
241 global $wgUser, $wgOut, $wgRequest;
243 $confirm = $wgRequest->getBool( 'wpConfirmB' );
244 $image = $wgRequest->getVal( 'image' );
245 $oldimage = $wgRequest->getVal( 'oldimage' );
247 # Only sysops can delete images. Previously ordinary users could delete
248 # old revisions, but this is no longer the case.
249 if ( !$wgUser->isAllowed('delete') ) {
250 $wgOut->sysopRequired();
253 if ( wfReadOnly() ) {
254 $wgOut->readOnlyPage();
258 # Better double-check that it hasn't been deleted yet!
259 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
260 if ( ( !is_null( $image ) )
261 && ( '' == trim( $image ) ) ) {
262 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
266 $this->img
= new Image( $this->mTitle
);
268 # Deleting old images doesn't require confirmation
269 if ( !is_null( $oldimage ) ||
$confirm ) {
270 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
273 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
278 if ( !is_null( $image ) ) {
279 $q = '&image=' . urlencode( $image );
280 } else if ( !is_null( $oldimage ) ) {
281 $q = '&oldimage=' . urlencode( $oldimage );
285 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
290 global $wgOut, $wgUser, $wgContLang, $wgRequest;
291 global $wgUseSquid, $wgInternalServer, $wgPostCommitUpdateList;
292 $fname = 'ImagePage::doDelete';
294 $reason = $wgRequest->getVal( 'wpReason' );
295 $oldimage = $wgRequest->getVal( 'oldimage' );
297 $dbw =& wfGetDB( DB_MASTER
);
299 if ( !is_null( $oldimage ) ) {
300 if ( strlen( $oldimage ) < 16 ) {
301 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
304 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
305 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
309 # Invalidate description page cache
310 $this->mTitle
->invalidateCache();
315 $wgInternalServer.wfImageArchiveUrl( $oldimage ),
316 $wgInternalServer.$this->mTitle
->getFullURL()
318 wfPurgeSquidServers($urlArr);
320 $this->doDeleteOldImage( $oldimage );
321 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
322 $deleted = $oldimage;
324 $image = $this->mTitle
->getDBkey();
325 $dest = wfImageDir( $image );
326 $archive = wfImageDir( $image );
328 # Delete the image file if it exists; due to sync problems
329 # or manual trimming sometimes the file will be missing.
330 $targetFile = "{$dest}/{$image}";
331 if( file_exists( $targetFile ) && ! @unlink
( $targetFile ) ) {
332 # If the deletion operation actually failed, bug out:
333 $wgOut->fileDeleteError( $targetFile );
336 $dbw->delete( 'image', array( 'img_name' => $image ) );
337 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
339 # Purge archive URLs from the squid
341 while ( $s = $dbw->fetchObject( $res ) ) {
342 $this->doDeleteOldImage( $s->oi_archive_name
);
343 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name
);
346 # And also the HTML of all pages using this image
347 $linksTo = $this->img
->getLinksTo();
349 $u = SquidUpdate
::newFromTitles( $linksTo, $urlArr );
350 array_push( $wgPostCommitUpdateList, $u );
353 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
355 # Image itself is now gone, and database is cleaned.
356 # Now we remove the image description page.
358 $article = new Article( $this->mTitle
);
359 $article->doDeleteArticle( $reason ); # ignore errors
361 # Invalidate parser cache and client cache for pages using this image
362 # This is left until relatively late to reduce lock time
363 Title
::touchArray( $linksTo );
365 /* Delete thumbnails and refresh image metadata cache */
366 $this->img
->purgeCache();
372 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
373 $wgOut->setRobotpolicy( 'noindex,nofollow' );
375 $sk = $wgUser->getSkin();
376 $loglink = $sk->makeKnownLinkObj(
377 Title
::makeTitle( NS_SPECIAL
, 'Log/delete' ),
378 wfMsg( 'deletionlog' ) );
380 $text = wfMsg( 'deletedtext', $deleted, $loglink );
382 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
383 $wgOut->returnToMain( false, $this->mTitle
->getPrefixedText() );
386 function doDeleteOldImage( $oldimage )
390 $name = substr( $oldimage, 15 );
391 $archive = wfImageArchiveDir( $name );
393 # Delete the image if it exists. Sometimes the file will be missing
394 # due to manual intervention or weird sync problems; treat that
395 # condition gracefully and continue to delete the database entry.
396 # Also some records may end up with an empty oi_archive_name field
397 # if the original file was missing when a new upload was made;
398 # don't try to delete the directory then!
400 $targetFile = "{$archive}/{$oldimage}";
401 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink
( $targetFile ) ) {
402 # If we actually have a file and can't delete it, throw an error.
403 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
406 $log = new LogPage( 'delete' );
407 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
413 global $wgOut, $wgRequest, $wgUser;
414 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
416 $oldimage = $wgRequest->getText( 'oldimage' );
417 if ( strlen( $oldimage ) < 16 ) {
418 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
421 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
422 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
426 if ( wfReadOnly() ) {
427 $wgOut->readOnlyPage();
430 if( $wgUser->isAnon() ) {
431 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
434 if ( ! $this->mTitle
->userCanEdit() ) {
435 $wgOut->sysopRequired();
438 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
439 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
442 $name = substr( $oldimage, 15 );
444 $dest = wfImageDir( $name );
445 $archive = wfImageArchiveDir( $name );
446 $curfile = "{$dest}/{$name}";
448 if ( ! is_file( $curfile ) ) {
449 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
452 $oldver = wfTimestampNow() . "!{$name}";
454 $dbr =& wfGetDB( DB_SLAVE
);
455 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
457 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
458 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
461 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
462 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
465 # Record upload and update metadata cache
466 $img = Image
::newFromName( $name );
467 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
469 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
470 $wgOut->setRobotpolicy( 'noindex,nofollow' );
471 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
473 $descTitle = $img->getTitle();
474 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
482 class ImageHistoryList
{
483 function ImageHistoryList( &$skin ) {
484 $this->skin
=& $skin;
487 function beginImageHistoryList() {
488 $s = "\n<h2>" . wfMsg( 'imghistory' ) . "</h2>\n" .
489 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
493 function endImageHistoryList() {
498 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
499 global $wgUser, $wgLang, $wgContLang, $wgTitle;
501 $datetime = $wgLang->timeanddate( $timestamp, true );
502 $del = wfMsg( 'deleteimg' );
503 $delall = wfMsg( 'deleteimgcompletely' );
504 $cur = wfMsg( 'cur' );
507 $url = Image
::imageUrl( $img );
509 if ( $wgUser->isAllowed('delete') ) {
510 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
512 $style = $this->skin
->getInternalLinkAttributes( $link, $delall );
514 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
519 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
520 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
521 $token = urlencode( $wgUser->editToken( $img ) );
522 $rlink = $this->skin
->makeKnownLinkObj( $wgTitle,
523 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
524 urlencode( $img ) . "&wpEditToken=$token" );
525 $dlink = $this->skin
->makeKnownLinkObj( $wgTitle,
526 $del, 'action=delete&oldimage=' . urlencode( $img ) .
527 "&wpEditToken=$token" );
529 # Having live active links for non-logged in users
530 # means that bots and spiders crawling our site can
531 # inadvertently change content. Baaaad idea.
532 $rlink = wfMsg( 'revertimg' );
537 $userlink = $usertext;
539 $userlink = $this->skin
->makeLinkObj(
540 Title
::makeTitle( NS_USER
, $usertext ),
543 $nbytes = wfMsg( 'nbytes', $size );
544 $style = $this->skin
->getInternalLinkAttributes( $url, $datetime );
546 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
547 . " . . {$userlink} ({$nbytes})";
549 $s .= $this->skin
->commentBlock( $description, $wgTitle );