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. Initilaized in openShowImage, not
21 // available in doDelete etc.
24 if( $this->mTitle
->getNamespace() == NS_IMAGE
) {
25 $this->openShowImage();
30 # If the article we've just shown is in the "Image" namespace,
31 # follow it with the history list and link list for the image
34 if( $this->mTitle
->getNamespace() == NS_IMAGE
) {
35 $this->closeShowImage();
36 $this->imageHistory();
41 function openShowImage()
43 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
44 $wgUseImageResize, $wgRepositoryBaseUrl;
45 $this->img
= Image
::newFromTitle( $this->mTitle
);
46 $full_url = $this->img
->getViewURL();
50 if( $wgUser->getOption( 'imagesize' ) == '' ) {
51 $sizeSel = User
::getDefaultOption( 'imagesize' );
53 $sizeSel = IntVal( $wgUser->getOption( 'imagesize' ) );
55 if( !isset( $wgImageLimits[$sizeSel] ) ) {
56 $sizeSel = User
::getDefaultOption( 'imagesize' );
58 $max = $wgImageLimits[$sizeSel];
63 if ( $this->img
->exists() ) {
65 $sk = $wgUser->getSkin();
67 if ( $this->img
->getType() != '' ) {
69 $width = $this->img
->getWidth();
70 $height = $this->img
->getHeight();
71 $msg = wfMsg('showbigimage', $width, $height, intval( $this->img
->getSize()/1024 ) );
72 if ( $width > $maxWidth ) {
73 $height = floor( $height * $maxWidth / $width );
76 if ( $height > $maxHeight ) {
77 $width = floor( $width * $maxHeight / $height );
80 if ( $width != $this->img
->getWidth() ||
$height != $this->img
->getHeight() ) {
81 if( $wgUseImageResize ) {
82 $thumbnail = $this->img
->getThumbnail( $width );
84 if ( ( ! $this->img
->mustRender() )
85 && ( $thumbnail->getSize() > $this->img
->getSize() ) ) {
86 # the thumbnail is bigger thatn the original image.
87 # show the original image instead of the thumb.
89 $width = $this->img
->getWidth();
90 $height = $this->img
->getHeight();
92 $url = $thumbnail->getUrl();
95 # No resize ability? Show the full image, but scale
96 # it down in the browser so it fits on the page.
99 $anchoropen = "<a href=\"{$full_url}\">";
100 $anchorclose = "</a><br />\n$anchoropen{$msg}</a>";
104 $s = '<div class="fullImageLink">' . $anchoropen .
105 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
106 htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>';
108 $s = "<div class=\"fullMedia\">" . $sk->makeMediaLink( $this->img
->getName(),'' ) . '</div>';
110 $wgOut->addHTML( $s );
111 if($this->img
->fromSharedDirectory
) {
112 $sharedtext="<div class=\"sharedUploadNotice\">" . wfMsg("sharedupload");
113 if($wgRepositoryBaseUrl) {
114 $sharedtext .= " ". wfMsg("shareduploadwiki",$wgRepositoryBaseUrl . urlencode($this->mTitle
->getDBkey()));
116 $sharedtext.="</div>";
117 $wgOut->addWikiText($sharedtext);
122 function closeShowImage()
128 * If the page we've just displayed is in the "Image" namespace,
129 * we follow it with an upload history of the image and its usage.
131 function imageHistory()
133 global $wgUser, $wgOut;
135 $sk = $wgUser->getSkin();
137 $line = $this->img
->nextHistoryLine();
140 $list =& new ImageHistoryList( $sk );
141 $s = $list->beginImageHistoryList() .
142 $list->imageHistoryLine( true, $line->img_timestamp
,
143 $this->mTitle
->getDBkey(), $line->img_user
,
144 $line->img_user_text
, $line->img_size
, $line->img_description
);
146 while ( $line = $this->img
->nextHistoryLine() ) {
147 $s .= $list->imageHistoryLine( false, $line->img_timestamp
,
148 $line->oi_archive_name
, $line->img_user
,
149 $line->img_user_text
, $line->img_size
, $line->img_description
);
151 $s .= $list->endImageHistoryList();
153 $wgOut->addHTML( $s );
156 function imageLinks()
158 global $wgUser, $wgOut;
160 $wgOut->addHTML( '<h2>' . wfMsg( 'imagelinks' ) . "</h2>\n" );
162 $dbr =& wfGetDB( DB_SLAVE
);
163 $page = $dbr->tableName( 'page' );
164 $imagelinks = $dbr->tableName( 'imagelinks' );
166 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
167 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id"
168 . " LIMIT 500"; # quickie emergency brake
169 $res = $dbr->query( $sql, DB_SLAVE
, "Article::imageLinks" );
171 if ( 0 == $dbr->numRows( $res ) ) {
172 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
175 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
177 $sk = $wgUser->getSkin();
178 while ( $s = $dbr->fetchObject( $res ) ) {
179 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
180 $link = $sk->makeKnownLinkObj( $name, "" );
181 $wgOut->addHTML( "<li>{$link}</li>\n" );
183 $wgOut->addHTML( "</ul>\n" );
188 global $wgUser, $wgOut, $wgRequest;
190 $confirm = $wgRequest->getBool( 'wpConfirmB' );
191 $image = $wgRequest->getVal( 'image' );
192 $oldimage = $wgRequest->getVal( 'oldimage' );
194 # Only sysops can delete images. Previously ordinary users could delete
195 # old revisions, but this is no longer the case.
196 if ( !$wgUser->isAllowed('delete') ) {
197 $wgOut->sysopRequired();
200 if ( wfReadOnly() ) {
201 $wgOut->readOnlyPage();
205 # Better double-check that it hasn't been deleted yet!
206 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
207 if ( ( !is_null( $image ) )
208 && ( '' == trim( $image ) ) ) {
209 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
213 # Deleting old images doesn't require confirmation
214 if ( !is_null( $oldimage ) ||
$confirm ) {
215 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
218 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
223 if ( !is_null( $image ) ) {
224 $q = '&image=' . urlencode( $image );
225 } else if ( !is_null( $oldimage ) ) {
226 $q = '&oldimage=' . urlencode( $oldimage );
230 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
235 global $wgOut, $wgUser, $wgContLang, $wgRequest;
236 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
237 $fname = 'ImagePage::doDelete';
239 $reason = $wgRequest->getVal( 'wpReason' );
240 $oldimage = $wgRequest->getVal( 'oldimage' );
242 $dbw =& wfGetDB( DB_MASTER
);
244 if ( !is_null( $oldimage ) ) {
245 if ( strlen( $oldimage ) < 16 ) {
246 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
249 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
250 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
256 $wgInternalServer.wfImageArchiveUrl( $oldimage )
258 wfPurgeSquidServers($urlArr);
260 $this->doDeleteOldImage( $oldimage );
261 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
262 $deleted = $oldimage;
264 $image = $this->mTitle
->getDBkey();
265 $dest = wfImageDir( $image );
266 $archive = wfImageDir( $image );
268 # Delete the image file if it exists; due to sync problems
269 # or manual trimming sometimes the file will be missing.
270 $targetFile = "{$dest}/{$image}";
271 if( file_exists( $targetFile ) && ! @unlink
( $targetFile ) ) {
272 # If the deletion operation actually failed, bug out:
273 $wgOut->fileDeleteError( $targetFile );
276 $dbw->delete( 'image', array( 'img_name' => $image ) );
277 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
282 $wgInternalServer . Image
::wfImageUrl( $image )
284 wfPurgeSquidServers($urlArr);
289 while ( $s = $dbw->fetchObject( $res ) ) {
290 $this->doDeleteOldImage( $s->oi_archive_name
);
291 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name
);
294 # Squid purging, part II
296 /* this needs to be done after LinksUpdate */
297 $u = new SquidUpdate( $urlArr );
298 array_push( $wgDeferredUpdateList, $u );
301 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
303 # Image itself is now gone, and database is cleaned.
304 # Now we remove the image description page.
306 $nt = Title
::makeTitleSafe( NS_IMAGE
, $image );
307 $article = new Article( $nt );
308 $article->doDeleteArticle( $reason ); # ignore errors
310 /* refresh image metadata cache */
311 new Image( $image, true );
316 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
317 $wgOut->setRobotpolicy( 'noindex,nofollow' );
319 $sk = $wgUser->getSkin();
320 $loglink = $sk->makeKnownLinkObj(
321 Title
::makeTitle( NS_SPECIAL
, 'Delete/log' ),
322 wfMsg( 'deletionlog' ) );
324 $text = wfMsg( 'deletedtext', $deleted, $loglink );
326 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
327 $wgOut->returnToMain( false );
330 function doDeleteOldImage( $oldimage )
334 $name = substr( $oldimage, 15 );
335 $archive = wfImageArchiveDir( $name );
337 # Delete the image if it exists. Sometimes the file will be missing
338 # due to manual intervention or weird sync problems; treat that
339 # condition gracefully and continue to delete the database entry.
340 # Also some records may end up with an empty oi_archive_name field
341 # if the original file was missing when a new upload was made;
342 # don't try to delete the directory then!
344 $targetFile = "{$archive}/{$oldimage}";
345 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink
( $targetFile ) ) {
346 # If we actually have a file and can't delete it, throw an error.
347 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
350 $log = new LogPage( 'delete' );
351 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
357 global $wgOut, $wgRequest, $wgUser;
358 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
360 $oldimage = $wgRequest->getText( 'oldimage' );
361 if ( strlen( $oldimage ) < 16 ) {
362 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
365 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
366 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
370 if ( wfReadOnly() ) {
371 $wgOut->readOnlyPage();
374 if( $wgUser->isAnon() ) {
375 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
378 if ( ! $this->mTitle
->userCanEdit() ) {
379 $wgOut->sysopRequired();
382 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
383 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
386 $name = substr( $oldimage, 15 );
388 $dest = wfImageDir( $name );
389 $archive = wfImageArchiveDir( $name );
390 $curfile = "{$dest}/{$name}";
392 if ( ! is_file( $curfile ) ) {
393 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
396 $oldver = wfTimestampNow() . "!{$name}";
398 $dbr =& wfGetDB( DB_SLAVE
);
399 $size = $dbr->selectField( 'oldimage', 'oi_size', 'oi_archive_name=\'' .
400 $dbr->strencode( $oldimage ) . "'" );
402 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
403 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
406 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
407 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
409 wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
411 /* refresh image metadata cache */
412 new Image( $name, true );
417 $wgInternalServer.wfImageArchiveUrl( $name ),
418 $wgInternalServer . Image
::wfImageUrl( $name )
420 wfPurgeSquidServers($urlArr);
423 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
424 $wgOut->setRobotpolicy( 'noindex,nofollow' );
425 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
426 $wgOut->returnToMain( false );
434 class ImageHistoryList
{
435 function ImageHistoryList( &$skin ) {
436 $this->skin
=& $skin;
439 function beginImageHistoryList() {
440 $s = "\n<h2>" . wfMsg( 'imghistory' ) . "</h2>\n" .
441 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
445 function endImageHistoryList() {
450 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
451 global $wgUser, $wgLang, $wgContLang, $wgTitle;
453 $datetime = $wgLang->timeanddate( $timestamp, true );
454 $del = wfMsg( 'deleteimg' );
455 $delall = wfMsg( 'deleteimgcompletely' );
456 $cur = wfMsg( 'cur' );
459 $url = Image
::wfImageUrl( $img );
461 if ( $wgUser->isAllowed('delete') ) {
462 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
464 $style = $this->skin
->getInternalLinkAttributes( $link, $delall );
466 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
471 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
472 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
473 $token = urlencode( $wgUser->editToken( $img ) );
474 $rlink = $this->skin
->makeKnownLink( $wgTitle->getPrefixedText(),
475 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
476 urlencode( $img ) . "&wpEditToken=$token" );
477 $dlink = $this->skin
->makeKnownLink( $wgTitle->getPrefixedText(),
478 $del, 'action=delete&oldimage=' . urlencode( $img ) .
479 "&wpEditToken=$token" );
481 # Having live active links for non-logged in users
482 # means that bots and spiders crawling our site can
483 # inadvertently change content. Baaaad idea.
484 $rlink = wfMsg( 'revertimg' );
489 $userlink = $usertext;
491 $userlink = $this->skin
->makeLinkObj(
492 Title
::makeTitle( NS_USER
, $usertext ),
495 $nbytes = wfMsg( 'nbytes', $size );
496 $style = $this->skin
->getInternalLinkAttributes( $url, $datetime );
498 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
499 . " . . {$userlink} ({$nbytes})";
501 $s .= $this->skin
->commentBlock( $description, $wgTitle );