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. Initialized in openShowImage, not
21 // available in doDelete etc.
24 global $wgUseExternalEditor;
25 if( $this->mTitle
->getNamespace() == NS_IMAGE
) {
26 $this->openShowImage();
30 if($wgUseExternalEditor) {
31 $this->externalEditorLink();
34 # If the article we've just shown is in the "Image" namespace,
35 # follow it with the history list and link list for the image
38 if( $this->mTitle
->getNamespace() == NS_IMAGE
) {
39 $this->closeShowImage();
40 $this->imageHistory();
45 function openShowImage()
47 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
48 $wgUseImageResize, $wgRepositoryBaseUrl,
50 $this->img
= new Image( $this->mTitle
);
51 $full_url = $this->img
->getViewURL();
55 if( $wgUser->getOption( 'imagesize' ) == '' ) {
56 $sizeSel = User
::getDefaultOption( 'imagesize' );
58 $sizeSel = IntVal( $wgUser->getOption( 'imagesize' ) );
60 if( !isset( $wgImageLimits[$sizeSel] ) ) {
61 $sizeSel = User
::getDefaultOption( 'imagesize' );
63 $max = $wgImageLimits[$sizeSel];
68 if ( $this->img
->exists() ) {
70 $sk = $wgUser->getSkin();
72 if ( $this->img
->getType() != '' ) {
74 $width = $this->img
->getWidth();
75 $height = $this->img
->getHeight();
76 $msg = wfMsg('showbigimage', $width, $height, intval( $this->img
->getSize()/1024 ) );
77 if ( $width > $maxWidth ) {
78 $height = floor( $height * $maxWidth / $width );
81 if ( $height > $maxHeight ) {
82 $width = floor( $width * $maxHeight / $height );
85 if ( $width != $this->img
->getWidth() ||
$height != $this->img
->getHeight() ) {
86 if( $wgUseImageResize ) {
87 $thumbnail = $this->img
->getThumbnail( $width );
88 $url = $thumbnail->getUrl();
90 # No resize ability? Show the full image, but scale
91 # it down in the browser so it fits on the page.
94 $anchoropen = "<a href=\"{$full_url}\">";
95 $anchorclose = "</a><br />\n$anchoropen{$msg}</a>";
99 $s = '<div class="fullImageLink">' . $anchoropen .
100 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
101 htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>';
103 $s = "<div class=\"fullMedia\">" . $sk->makeMediaLink( $this->img
->getName(),'' ) . '</div>';
105 $wgOut->addHTML( $s );
106 if($this->img
->fromSharedDirectory
) {
107 $sharedtext="<div class=\"sharedUploadNotice\">" . wfMsg("sharedupload");
108 if($wgRepositoryBaseUrl) {
109 $sharedtext .= " ". wfMsg("shareduploadwiki",$wgRepositoryBaseUrl . urlencode($this->mTitle
->getDBkey()));
111 $sharedtext.="</div>";
112 $wgOut->addWikiText($sharedtext);
118 function externalEditorLink()
120 global $wgUser,$wgOut;
121 $sk = $wgUser->getSkin();
122 $wgOut->addHTML( '<div class="editExternally">' );
123 $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle
,
124 wfMsg( 'edit-externally' ),
125 "action=edit&externaledit=true&mode=file" ) );
126 $wgOut->addWikiText( '<div class="editExternallyHelp">' .
127 wfMsg('edit-externally-help') . '</div>' );
128 $wgOut->addHTML( '</div><br clear="all" />' );
131 function closeShowImage()
138 * If the page we've just displayed is in the "Image" namespace,
139 * we follow it with an upload history of the image and its usage.
141 function imageHistory()
143 global $wgUser, $wgOut;
145 $sk = $wgUser->getSkin();
147 $line = $this->img
->nextHistoryLine();
150 $list =& new ImageHistoryList( $sk );
151 $s = $list->beginImageHistoryList() .
152 $list->imageHistoryLine( true, $line->img_timestamp
,
153 $this->mTitle
->getDBkey(), $line->img_user
,
154 $line->img_user_text
, $line->img_size
, $line->img_description
);
156 while ( $line = $this->img
->nextHistoryLine() ) {
157 $s .= $list->imageHistoryLine( false, $line->img_timestamp
,
158 $line->oi_archive_name
, $line->img_user
,
159 $line->img_user_text
, $line->img_size
, $line->img_description
);
161 $s .= $list->endImageHistoryList();
163 $wgOut->addHTML( $s );
166 function imageLinks()
168 global $wgUser, $wgOut;
170 $wgOut->addHTML( '<h2>' . wfMsg( 'imagelinks' ) . "</h2>\n" );
172 $dbr =& wfGetDB( DB_SLAVE
);
173 $page = $dbr->tableName( 'page' );
174 $imagelinks = $dbr->tableName( 'imagelinks' );
176 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
177 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id"
178 . " LIMIT 500"; # quickie emergency brake
179 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
181 if ( 0 == $dbr->numRows( $res ) ) {
182 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
185 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
187 $sk = $wgUser->getSkin();
188 while ( $s = $dbr->fetchObject( $res ) ) {
189 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
190 $link = $sk->makeKnownLinkObj( $name, "" );
191 $wgOut->addHTML( "<li>{$link}</li>\n" );
193 $wgOut->addHTML( "</ul>\n" );
198 global $wgUser, $wgOut, $wgRequest;
200 $confirm = $wgRequest->getBool( 'wpConfirmB' );
201 $image = $wgRequest->getVal( 'image' );
202 $oldimage = $wgRequest->getVal( 'oldimage' );
204 # Only sysops can delete images. Previously ordinary users could delete
205 # old revisions, but this is no longer the case.
206 if ( !$wgUser->isAllowed('delete') ) {
207 $wgOut->sysopRequired();
210 if ( wfReadOnly() ) {
211 $wgOut->readOnlyPage();
215 # Better double-check that it hasn't been deleted yet!
216 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
217 if ( ( !is_null( $image ) )
218 && ( '' == trim( $image ) ) ) {
219 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
223 # Deleting old images doesn't require confirmation
224 if ( !is_null( $oldimage ) ||
$confirm ) {
225 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
228 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
233 if ( !is_null( $image ) ) {
234 $q = '&image=' . urlencode( $image );
235 } else if ( !is_null( $oldimage ) ) {
236 $q = '&oldimage=' . urlencode( $oldimage );
240 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
245 global $wgOut, $wgUser, $wgContLang, $wgRequest;
246 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
247 $fname = 'ImagePage::doDelete';
249 $reason = $wgRequest->getVal( 'wpReason' );
250 $oldimage = $wgRequest->getVal( 'oldimage' );
252 $dbw =& wfGetDB( DB_MASTER
);
254 if ( !is_null( $oldimage ) ) {
255 if ( strlen( $oldimage ) < 16 ) {
256 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
259 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
260 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
264 # Invalidate description page cache
265 $this->mTitle
->invalidateCache();
270 $wgInternalServer.wfImageArchiveUrl( $oldimage ),
271 $wgInternalServer.$this->mTitle
->getFullURL()
273 wfPurgeSquidServers($urlArr);
275 $this->doDeleteOldImage( $oldimage );
276 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
277 $deleted = $oldimage;
279 $image = $this->mTitle
->getDBkey();
280 $dest = wfImageDir( $image );
281 $archive = wfImageDir( $image );
283 # Delete the image file if it exists; due to sync problems
284 # or manual trimming sometimes the file will be missing.
285 $targetFile = "{$dest}/{$image}";
286 if( file_exists( $targetFile ) && ! @unlink
( $targetFile ) ) {
287 # If the deletion operation actually failed, bug out:
288 $wgOut->fileDeleteError( $targetFile );
291 $dbw->delete( 'image', array( 'img_name' => $image ) );
292 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
295 while ( $s = $dbw->fetchObject( $res ) ) {
296 $this->doDeleteOldImage( $s->oi_archive_name
);
297 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name
);
300 # Defer purging of archived URLs
302 /* this needs to be done after LinksUpdate */
303 $u = new SquidUpdate( $urlArr );
304 array_push( $wgDeferredUpdateList, $u );
307 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
309 # Image itself is now gone, and database is cleaned.
310 # Now we remove the image description page.
312 $article = new Article( $this->mTitle
);
313 $article->doDeleteArticle( $reason ); # ignore errors
315 /* Delete thumbnails and refresh image metadata cache */
316 $imgObj = new Image( $this->mTitle
);
317 $imgObj->purgeCache();
322 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
323 $wgOut->setRobotpolicy( 'noindex,nofollow' );
325 $sk = $wgUser->getSkin();
326 $loglink = $sk->makeKnownLinkObj(
327 Title
::makeTitle( NS_SPECIAL
, 'Log/delete' ),
328 wfMsg( 'deletionlog' ) );
330 $text = wfMsg( 'deletedtext', $deleted, $loglink );
332 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
333 $wgOut->returnToMain( false, $this->mTitle
->getPrefixedText() );
336 function doDeleteOldImage( $oldimage )
340 $name = substr( $oldimage, 15 );
341 $archive = wfImageArchiveDir( $name );
343 # Delete the image if it exists. Sometimes the file will be missing
344 # due to manual intervention or weird sync problems; treat that
345 # condition gracefully and continue to delete the database entry.
346 # Also some records may end up with an empty oi_archive_name field
347 # if the original file was missing when a new upload was made;
348 # don't try to delete the directory then!
350 $targetFile = "{$archive}/{$oldimage}";
351 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink
( $targetFile ) ) {
352 # If we actually have a file and can't delete it, throw an error.
353 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
356 $log = new LogPage( 'delete' );
357 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
363 global $wgOut, $wgRequest, $wgUser;
364 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
366 $oldimage = $wgRequest->getText( 'oldimage' );
367 if ( strlen( $oldimage ) < 16 ) {
368 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
371 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
372 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
376 if ( wfReadOnly() ) {
377 $wgOut->readOnlyPage();
380 if( $wgUser->isAnon() ) {
381 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
384 if ( ! $this->mTitle
->userCanEdit() ) {
385 $wgOut->sysopRequired();
388 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
389 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
392 $name = substr( $oldimage, 15 );
394 $dest = wfImageDir( $name );
395 $archive = wfImageArchiveDir( $name );
396 $curfile = "{$dest}/{$name}";
398 if ( ! is_file( $curfile ) ) {
399 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
402 $oldver = wfTimestampNow() . "!{$name}";
404 $dbr =& wfGetDB( DB_SLAVE
);
405 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
407 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
408 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
411 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
412 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
415 # Record upload and update metadata cache
416 $img = Image
::newFromName( $name );
417 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
419 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
420 $wgOut->setRobotpolicy( 'noindex,nofollow' );
421 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
423 $descTitle = $img->getTitle();
424 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
432 class ImageHistoryList
{
433 function ImageHistoryList( &$skin ) {
434 $this->skin
=& $skin;
437 function beginImageHistoryList() {
438 $s = "\n<h2>" . wfMsg( 'imghistory' ) . "</h2>\n" .
439 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
443 function endImageHistoryList() {
448 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
449 global $wgUser, $wgLang, $wgContLang, $wgTitle;
451 $datetime = $wgLang->timeanddate( $timestamp, true );
452 $del = wfMsg( 'deleteimg' );
453 $delall = wfMsg( 'deleteimgcompletely' );
454 $cur = wfMsg( 'cur' );
457 $url = Image
::imageUrl( $img );
459 if ( $wgUser->isAllowed('delete') ) {
460 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
462 $style = $this->skin
->getInternalLinkAttributes( $link, $delall );
464 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
469 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
470 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
471 $token = urlencode( $wgUser->editToken( $img ) );
472 $rlink = $this->skin
->makeKnownLinkObj( $wgTitle,
473 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
474 urlencode( $img ) . "&wpEditToken=$token" );
475 $dlink = $this->skin
->makeKnownLinkObj( $wgTitle,
476 $del, 'action=delete&oldimage=' . urlencode( $img ) .
477 "&wpEditToken=$token" );
479 # Having live active links for non-logged in users
480 # means that bots and spiders crawling our site can
481 # inadvertently change content. Baaaad idea.
482 $rlink = wfMsg( 'revertimg' );
487 $userlink = $usertext;
489 $userlink = $this->skin
->makeLinkObj(
490 Title
::makeTitle( NS_USER
, $usertext ),
493 $nbytes = wfMsg( 'nbytes', $size );
494 $style = $this->skin
->getInternalLinkAttributes( $url, $datetime );
496 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
497 . " . . {$userlink} ({$nbytes})";
499 $s .= $this->skin
->commentBlock( $description, $wgTitle );