Various live patches ported from REL1_4
[mediawiki.git] / includes / ImagePage.php
blobb117d42c1765fb9e44224f3e65d65207866f495c
1 <?php
2 /**
3 * @package MediaWiki
4 */
6 /**
8 */
9 if( !defined( 'MEDIAWIKI' ) )
10 die();
12 require_once( 'Image.php' );
14 /**
15 * Special handling for image description pages
16 * @package MediaWiki
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.
23 function view() {
24 global $wgUseExternalEditor;
25 if( $this->mTitle->getNamespace() == NS_IMAGE ) {
26 $this->openShowImage();
29 Article::view();
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
36 # it describes.
38 if( $this->mTitle->getNamespace() == NS_IMAGE ) {
39 $this->closeShowImage();
40 $this->imageHistory();
41 $this->imageLinks();
45 function openShowImage()
47 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
48 $wgUseImageResize, $wgRepositoryBaseUrl,
49 $wgUseExternalEditor;
50 $this->img = new Image( $this->mTitle );
51 $full_url = $this->img->getViewURL();
52 $anchoropen = '';
53 $anchorclose = '';
55 if( $wgUser->getOption( 'imagesize' ) == '' ) {
56 $sizeSel = User::getDefaultOption( 'imagesize' );
57 } else {
58 $sizeSel = IntVal( $wgUser->getOption( 'imagesize' ) );
60 if( !isset( $wgImageLimits[$sizeSel] ) ) {
61 $sizeSel = User::getDefaultOption( 'imagesize' );
63 $max = $wgImageLimits[$sizeSel];
64 $maxWidth = $max[0];
65 $maxHeight = $max[1];
68 if ( $this->img->exists() ) {
70 $sk = $wgUser->getSkin();
72 if ( $this->img->getType() != '' ) {
73 # image
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 );
79 $width = $maxWidth;
81 if ( $height > $maxHeight ) {
82 $width = floor( $width * $maxHeight / $height );
83 $height = $maxHeight;
85 if ( $width != $this->img->getWidth() || $height != $this->img->getHeight() ) {
86 if( $wgUseImageResize ) {
87 $thumbnail = $this->img->getThumbnail( $width );
89 if ( ( ! $this->img->mustRender() )
90 && ( $thumbnail->getSize() > $this->img->getSize() ) ) {
91 # the thumbnail is bigger thatn the original image.
92 # show the original image instead of the thumb.
93 $url = $full_url;
94 $width = $this->img->getWidth();
95 $height = $this->img->getHeight();
96 } else {
97 $url = $thumbnail->getUrl();
99 } else {
100 # No resize ability? Show the full image, but scale
101 # it down in the browser so it fits on the page.
102 $url = $full_url;
104 $anchoropen = "<a href=\"{$full_url}\">";
105 $anchorclose = "</a><br />\n$anchoropen{$msg}</a>";
106 } else {
107 $url = $full_url;
109 $s = '<div class="fullImageLink">' . $anchoropen .
110 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
111 htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>';
112 } else {
113 $s = "<div class=\"fullMedia\">" . $sk->makeMediaLink( $this->img->getName(),'' ) . '</div>';
115 $wgOut->addHTML( $s );
116 if($this->img->fromSharedDirectory) {
117 $sharedtext="<div class=\"sharedUploadNotice\">" . wfMsg("sharedupload");
118 if($wgRepositoryBaseUrl) {
119 $sharedtext .= " ". wfMsg("shareduploadwiki",$wgRepositoryBaseUrl . urlencode($this->mTitle->getDBkey()));
121 $sharedtext.="</div>";
122 $wgOut->addWikiText($sharedtext);
128 function externalEditorLink()
130 global $wgUser,$wgOut;
131 $sk = $wgUser->getSkin();
132 $wgOut->addHTML( '<div class="editExternally">' );
133 $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle,
134 wfMsg( 'edit-externally' ),
135 "action=edit&externaledit=true&mode=file" ) );
136 $wgOut->addWikiText( '<div class="editExternallyHelp">' .
137 wfMsg('edit-externally-help') . '</div>' );
138 $wgOut->addHTML( '</div><br clear="all" />' );
141 function closeShowImage()
143 # For overloading
148 * If the page we've just displayed is in the "Image" namespace,
149 * we follow it with an upload history of the image and its usage.
151 function imageHistory()
153 global $wgUser, $wgOut;
155 $sk = $wgUser->getSkin();
157 $line = $this->img->nextHistoryLine();
159 if ( $line ) {
160 $list =& new ImageHistoryList( $sk );
161 $s = $list->beginImageHistoryList() .
162 $list->imageHistoryLine( true, $line->img_timestamp,
163 $this->mTitle->getDBkey(), $line->img_user,
164 $line->img_user_text, $line->img_size, $line->img_description );
166 while ( $line = $this->img->nextHistoryLine() ) {
167 $s .= $list->imageHistoryLine( false, $line->img_timestamp,
168 $line->oi_archive_name, $line->img_user,
169 $line->img_user_text, $line->img_size, $line->img_description );
171 $s .= $list->endImageHistoryList();
172 } else { $s=''; }
173 $wgOut->addHTML( $s );
176 function imageLinks()
178 global $wgUser, $wgOut;
180 $wgOut->addHTML( '<h2>' . wfMsg( 'imagelinks' ) . "</h2>\n" );
182 $dbr =& wfGetDB( DB_SLAVE );
183 $page = $dbr->tableName( 'page' );
184 $imagelinks = $dbr->tableName( 'imagelinks' );
186 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
187 $dbr->addQuotes( $this->mTitle->getDBkey() ) . " AND il_from=page_id"
188 . " LIMIT 500"; # quickie emergency brake
189 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
191 if ( 0 == $dbr->numRows( $res ) ) {
192 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
193 return;
195 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
197 $sk = $wgUser->getSkin();
198 while ( $s = $dbr->fetchObject( $res ) ) {
199 $name = Title::MakeTitle( $s->page_namespace, $s->page_title );
200 $link = $sk->makeKnownLinkObj( $name, "" );
201 $wgOut->addHTML( "<li>{$link}</li>\n" );
203 $wgOut->addHTML( "</ul>\n" );
206 function delete()
208 global $wgUser, $wgOut, $wgRequest;
210 $confirm = $wgRequest->getBool( 'wpConfirmB' );
211 $image = $wgRequest->getVal( 'image' );
212 $oldimage = $wgRequest->getVal( 'oldimage' );
214 # Only sysops can delete images. Previously ordinary users could delete
215 # old revisions, but this is no longer the case.
216 if ( !$wgUser->isAllowed('delete') ) {
217 $wgOut->sysopRequired();
218 return;
220 if ( wfReadOnly() ) {
221 $wgOut->readOnlyPage();
222 return;
225 # Better double-check that it hasn't been deleted yet!
226 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
227 if ( ( !is_null( $image ) )
228 && ( '' == trim( $image ) ) ) {
229 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
230 return;
233 # Deleting old images doesn't require confirmation
234 if ( !is_null( $oldimage ) || $confirm ) {
235 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
236 $this->doDelete();
237 } else {
238 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
240 return;
243 if ( !is_null( $image ) ) {
244 $q = '&image=' . urlencode( $image );
245 } else if ( !is_null( $oldimage ) ) {
246 $q = '&oldimage=' . urlencode( $oldimage );
247 } else {
248 $q = '';
250 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
253 function doDelete()
255 global $wgOut, $wgUser, $wgContLang, $wgRequest;
256 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
257 $fname = 'ImagePage::doDelete';
259 $reason = $wgRequest->getVal( 'wpReason' );
260 $oldimage = $wgRequest->getVal( 'oldimage' );
262 $dbw =& wfGetDB( DB_MASTER );
264 if ( !is_null( $oldimage ) ) {
265 if ( strlen( $oldimage ) < 16 ) {
266 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
267 return;
269 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
270 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
271 return;
274 # Invalidate description page cache
275 $this->mTitle->invalidateCache();
277 # Squid purging
278 if ( $wgUseSquid ) {
279 $urlArr = Array(
280 $wgInternalServer.wfImageArchiveUrl( $oldimage ),
281 $wgInternalServer.$this->mTitle->getFullURL()
283 wfPurgeSquidServers($urlArr);
285 $this->doDeleteOldImage( $oldimage );
286 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
287 $deleted = $oldimage;
288 } else {
289 $image = $this->mTitle->getDBkey();
290 $dest = wfImageDir( $image );
291 $archive = wfImageDir( $image );
293 # Delete the image file if it exists; due to sync problems
294 # or manual trimming sometimes the file will be missing.
295 $targetFile = "{$dest}/{$image}";
296 if( file_exists( $targetFile ) && ! @unlink( $targetFile ) ) {
297 # If the deletion operation actually failed, bug out:
298 $wgOut->fileDeleteError( $targetFile );
299 return;
301 $dbw->delete( 'image', array( 'img_name' => $image ) );
302 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
304 # Squid purging
305 if ( $wgUseSquid ) {
306 $urlArr = Array(
307 $wgInternalServer . Image::imageUrl( $image )
309 wfPurgeSquidServers($urlArr);
313 $urlArr = Array();
314 while ( $s = $dbw->fetchObject( $res ) ) {
315 $this->doDeleteOldImage( $s->oi_archive_name );
316 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name );
319 # Squid purging, part II
320 if ( $wgUseSquid ) {
321 /* this needs to be done after LinksUpdate */
322 $u = new SquidUpdate( $urlArr );
323 array_push( $wgDeferredUpdateList, $u );
326 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
328 # Image itself is now gone, and database is cleaned.
329 # Now we remove the image description page.
331 $article = new Article( $this->mTitle );
332 $article->doDeleteArticle( $reason ); # ignore errors
334 /* refresh image metadata cache */
335 $imgObj = new Image( $this->mTitle );
336 $imgObj->loadFromFile();
337 $imgObj->saveToCache();
339 $deleted = $image;
342 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
343 $wgOut->setRobotpolicy( 'noindex,nofollow' );
345 $sk = $wgUser->getSkin();
346 $loglink = $sk->makeKnownLinkObj(
347 Title::makeTitle( NS_SPECIAL, 'Log/delete' ),
348 wfMsg( 'deletionlog' ) );
350 $text = wfMsg( 'deletedtext', $deleted, $loglink );
352 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
353 $wgOut->returnToMain( false, $this->mTitle->getPrefixedText() );
356 function doDeleteOldImage( $oldimage )
358 global $wgOut;
360 $name = substr( $oldimage, 15 );
361 $archive = wfImageArchiveDir( $name );
363 # Delete the image if it exists. Sometimes the file will be missing
364 # due to manual intervention or weird sync problems; treat that
365 # condition gracefully and continue to delete the database entry.
366 # Also some records may end up with an empty oi_archive_name field
367 # if the original file was missing when a new upload was made;
368 # don't try to delete the directory then!
370 $targetFile = "{$archive}/{$oldimage}";
371 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink( $targetFile ) ) {
372 # If we actually have a file and can't delete it, throw an error.
373 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
374 } else {
375 # Log the deletion
376 $log = new LogPage( 'delete' );
377 $log->addEntry( 'delete', $this->mTitle, wfMsg('deletedrevision',$oldimage) );
381 function revert()
383 global $wgOut, $wgRequest, $wgUser;
384 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
386 $oldimage = $wgRequest->getText( 'oldimage' );
387 if ( strlen( $oldimage ) < 16 ) {
388 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
389 return;
391 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
392 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
393 return;
396 if ( wfReadOnly() ) {
397 $wgOut->readOnlyPage();
398 return;
400 if( $wgUser->isAnon() ) {
401 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
402 return;
404 if ( ! $this->mTitle->userCanEdit() ) {
405 $wgOut->sysopRequired();
406 return;
408 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
409 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
410 return;
412 $name = substr( $oldimage, 15 );
414 $dest = wfImageDir( $name );
415 $archive = wfImageArchiveDir( $name );
416 $curfile = "{$dest}/{$name}";
418 if ( ! is_file( $curfile ) ) {
419 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
420 return;
422 $oldver = wfTimestampNow() . "!{$name}";
424 $dbr =& wfGetDB( DB_SLAVE );
425 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
427 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
428 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
429 return;
431 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
432 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
435 # Record upload and update metadata cache
436 $img = Image::newFromName( $name );
437 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
439 # Squid purging
440 if ( $wgUseSquid ) {
441 $urlArr = Array(
442 $wgInternalServer.wfImageArchiveUrl( $name ),
443 $wgInternalServer . Image::imageUrl( $name ),
445 wfPurgeSquidServers($urlArr);
448 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
449 $wgOut->setRobotpolicy( 'noindex,nofollow' );
450 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
452 $descTitle = $img->getTitle();
453 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
458 * @todo document
459 * @package MediaWiki
461 class ImageHistoryList {
462 function ImageHistoryList( &$skin ) {
463 $this->skin =& $skin;
466 function beginImageHistoryList() {
467 $s = "\n<h2>" . wfMsg( 'imghistory' ) . "</h2>\n" .
468 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
469 return $s;
472 function endImageHistoryList() {
473 $s = "</ul>\n";
474 return $s;
477 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
478 global $wgUser, $wgLang, $wgContLang, $wgTitle;
480 $datetime = $wgLang->timeanddate( $timestamp, true );
481 $del = wfMsg( 'deleteimg' );
482 $delall = wfMsg( 'deleteimgcompletely' );
483 $cur = wfMsg( 'cur' );
485 if ( $iscur ) {
486 $url = Image::imageUrl( $img );
487 $rlink = $cur;
488 if ( $wgUser->isAllowed('delete') ) {
489 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
490 '&action=delete' );
491 $style = $this->skin->getInternalLinkAttributes( $link, $delall );
493 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
494 } else {
495 $dlink = $del;
497 } else {
498 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
499 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
500 $token = urlencode( $wgUser->editToken( $img ) );
501 $rlink = $this->skin->makeKnownLinkObj( $wgTitle,
502 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
503 urlencode( $img ) . "&wpEditToken=$token" );
504 $dlink = $this->skin->makeKnownLinkObj( $wgTitle,
505 $del, 'action=delete&oldimage=' . urlencode( $img ) .
506 "&wpEditToken=$token" );
507 } else {
508 # Having live active links for non-logged in users
509 # means that bots and spiders crawling our site can
510 # inadvertently change content. Baaaad idea.
511 $rlink = wfMsg( 'revertimg' );
512 $dlink = $del;
515 if ( 0 == $user ) {
516 $userlink = $usertext;
517 } else {
518 $userlink = $this->skin->makeLinkObj(
519 Title::makeTitle( NS_USER, $usertext ),
520 $usertext );
522 $nbytes = wfMsg( 'nbytes', $size );
523 $style = $this->skin->getInternalLinkAttributes( $url, $datetime );
525 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
526 . " . . {$userlink} ({$nbytes})";
528 $s .= $this->skin->commentBlock( $description, $wgTitle );
529 $s .= "</li>\n";
530 return $s;