RC patrol fixlet: include rcid in 'diff' link in enhanced mode, for individually...
[mediawiki.git] / includes / ImagePage.php
blobdc7c67fbf09fd8a00e355b814e108655e415a33b
1 <?php
2 /**
3 * @package MediaWiki
4 */
6 /**
8 */
9 require_once( 'Image.php' );
11 /**
12 * Special handling for image description pages
13 * @package MediaWiki
15 class ImagePage extends Article {
17 /* private */ var $img; // Image object this page is shown for. Initilaized in openShowImage, not
18 // available in doDelete etc.
20 function view() {
21 if ( Namespace::getImage() == $this->mTitle->getNamespace() ) {
22 $this->openShowImage();
25 Article::view();
27 # If the article we've just shown is in the "Image" namespace,
28 # follow it with the history list and link list for the image
29 # it describes.
31 if ( Namespace::getImage() == $this->mTitle->getNamespace() ) {
32 $this->closeShowImage();
33 $this->imageHistory();
34 $this->imageLinks();
38 function openShowImage()
40 global $wgOut, $wgUser, $wgImageLimits, $wgRequest, $wgUseImageResize;
41 $this->img = Image::newFromTitle( $this->mTitle );
42 $url = $this->img->getViewURL();
43 $anchoropen = '';
44 $anchorclose = '';
45 if ( $wgUseImageResize && $wgUser->getOption( 'imagesize' ) != '' ) {
46 $max = $wgImageLimits[ intval( $wgUser->getOption( 'imagesize' ) ) ];
47 $maxWidth = $max[0];
48 $maxHeight = $max[1];
52 if ( $this->img->exists() ) {
54 $sk = $wgUser->getSkin();
56 if ( $this->img->getType() != '' ) {
57 # image
58 $width = $this->img->getWidth();
59 $height = $this->img->getHeight();
60 $msg = wfMsg('showbigimage', $width, $height, intval( $this->img->getSize()/1024 ) );
61 if ( $width > $maxWidth && $wgUseImageResize ) {
62 $anchoropen = "<a href=\"{$url}\">";
63 $anchorclose = "<br>{$msg}</a>";
65 $url = $this->img->createThumb( $maxWidth );
66 $height = floor( $height * $maxWidth / $width );
67 $width = $maxWidth;
69 if ( $height > $maxHeight && $wgUseImageResize ) {
70 $anchoropen = "<a href=\"{$url}\">";
71 $anchorclose = "<br>{$msg}</a>";
73 $width = floor( $width * $maxHeight / $height );
74 $height = $maxHeight;
75 $url = $this->img->createThumb( $width );
77 $s = "<div class=\"fullImageLink\">" . $anchoropen .
78 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
79 htmlspecialchars( $wgRequest->getVal( 'image' ) )."\" />" . $anchorclose . "</div>";
80 } else {
81 $s = "<div class=\"fullMedia\">".$sk->makeMediaLink($this->img->getName(),"")."</div>";
83 $wgOut->addHTML( $s );
84 if($this->img->fromSharedDirectory) {
85 $wgOut->addWikiText("<div class=\"sharedUploadNotice\">".wfMsg("sharedupload")."</div>");
90 function closeShowImage()
92 # For overloading
95 /**
96 * If the page we've just displayed is in the "Image" namespace,
97 * we follow it with an upload history of the image and its usage.
99 function imageHistory()
101 global $wgUser, $wgOut;
103 $sk = $wgUser->getSkin();
105 $line = $this->img->nextHistoryLine();
107 if ( $line ) {
108 $list =& new ImageHistoryList( $sk );
109 $s = $list->beginImageHistoryList() .
110 $list->imageHistoryLine( true, $line->img_timestamp,
111 $this->mTitle->getDBkey(), $line->img_user,
112 $line->img_user_text, $line->img_size, $line->img_description );
114 while ( $line = $this->img->nextHistoryLine() ) {
115 $s .= $list->imageHistoryLine( false, $line->img_timestamp,
116 $line->oi_archive_name, $line->img_user,
117 $line->img_user_text, $line->img_size, $line->img_description );
119 $s .= $list->endImageHistoryList();
120 } else { $s=''; }
121 $wgOut->addHTML( $s );
124 function imageLinks()
126 global $wgUser, $wgOut;
128 $wgOut->addHTML( '<h2>' . wfMsg( 'imagelinks' ) . "</h2>\n" );
130 $dbr =& wfGetDB( DB_SLAVE );
131 $cur = $dbr->tableName( 'cur' );
132 $imagelinks = $dbr->tableName( 'imagelinks' );
134 $sql = "SELECT cur_namespace,cur_title FROM $imagelinks,$cur WHERE il_to=" .
135 $dbr->addQuotes( $this->mTitle->getDBkey() ) . " AND il_from=cur_id";
136 $res = $dbr->query( $sql, DB_SLAVE, "Article::imageLinks" );
138 if ( 0 == $dbr->numRows( $res ) ) {
139 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
140 return;
142 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
144 $sk = $wgUser->getSkin();
145 while ( $s = $dbr->fetchObject( $res ) ) {
146 $name = Title::MakeTitle( $s->cur_namespace, $s->cur_title );
147 $link = $sk->makeKnownLinkObj( $name, "" );
148 $wgOut->addHTML( "<li>{$link}</li>\n" );
150 $wgOut->addHTML( "</ul>\n" );
153 function delete()
155 global $wgUser, $wgOut, $wgRequest;
157 $confirm = $wgRequest->getBool( 'wpConfirm' );
158 $image = $wgRequest->getVal( 'image' );
159 $oldimage = $wgRequest->getVal( 'oldimage' );
161 # Only sysops can delete images. Previously ordinary users could delete
162 # old revisions, but this is no longer the case.
163 if ( !$wgUser->isAllowed('delete') ) {
164 $wgOut->sysopRequired();
165 return;
167 if ( wfReadOnly() ) {
168 $wgOut->readOnlyPage();
169 return;
172 # Better double-check that it hasn't been deleted yet!
173 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
174 if ( !is_null( $image ) ) {
175 if ( '' == trim( $image ) ) {
176 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
177 return;
181 # Deleting old images doesn't require confirmation
182 if ( !is_null( $oldimage ) || $confirm ) {
183 $this->doDelete();
184 return;
187 if ( !is_null( $image ) ) {
188 $q = '&image=' . urlencode( $image );
189 } else if ( !is_null( $oldimage ) ) {
190 $q = '&oldimage=' . urlencode( $oldimage );
191 } else {
192 $q = '';
194 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
197 function doDelete()
199 global $wgOut, $wgUser, $wgContLang, $wgRequest;
200 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
201 $fname = 'ImagePage::doDelete';
203 $reason = $wgRequest->getVal( 'wpReason' );
204 $image = $wgRequest->getVal( 'image' );
205 $oldimage = $wgRequest->getVal( 'oldimage' );
207 $dbw =& wfGetDB( DB_MASTER );
209 if ( !is_null( $oldimage ) ) {
210 # Squid purging
211 if ( $wgUseSquid ) {
212 $urlArr = Array(
213 $wgInternalServer.wfImageArchiveUrl( $oldimage )
215 wfPurgeSquidServers($urlArr);
217 $this->doDeleteOldImage( $oldimage );
218 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
219 $deleted = $oldimage;
220 } else {
221 if ( is_null ( $image ) ) {
222 $image = $this->mTitle->getDBkey();
224 $dest = wfImageDir( $image );
225 $archive = wfImageDir( $image );
227 # Delete the image file if it exists; due to sync problems
228 # or manual trimming sometimes the file will be missing.
229 $targetFile = "{$dest}/{$image}";
230 if( file_exists( $targetFile ) && ! @unlink( $targetFile ) ) {
231 # If the deletion operation actually failed, bug out:
232 $wgOut->fileDeleteError( $targetFile );
233 return;
235 $dbw->delete( 'image', array( 'img_name' => $image ) );
236 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
238 # Squid purging
239 if ( $wgUseSquid ) {
240 $urlArr = Array(
241 $wgInternalServer . Image::wfImageUrl( $image )
243 wfPurgeSquidServers($urlArr);
247 $urlArr = Array();
248 while ( $s = $dbw->fetchObject( $res ) ) {
249 $this->doDeleteOldImage( $s->oi_archive_name );
250 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name );
253 # Squid purging, part II
254 if ( $wgUseSquid ) {
255 /* this needs to be done after LinksUpdate */
256 $u = new SquidUpdate( $urlArr );
257 array_push( $wgDeferredUpdateList, $u );
260 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
262 # Image itself is now gone, and database is cleaned.
263 # Now we remove the image description page.
265 $nt = Title::newFromText( $wgContLang->getNsText( Namespace::getImage() ) . ":" . $image );
266 $article = new Article( $nt );
267 $article->doDeleteArticle( $reason ); # ignore errors
269 $deleted = $image;
272 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
273 $wgOut->setRobotpolicy( 'noindex,nofollow' );
275 $sk = $wgUser->getSkin();
276 $loglink = $sk->makeKnownLink( $wgContLang->getNsText(
277 Namespace::getWikipedia() ) .
278 ':' . wfMsg( 'dellogpage' ), wfMsg( 'deletionlog' ) );
280 $text = wfMsg( 'deletedtext', $deleted, $loglink );
282 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
283 $wgOut->returnToMain( false );
286 function doDeleteOldImage( $oldimage )
288 global $wgOut;
290 $name = substr( $oldimage, 15 );
291 $archive = wfImageArchiveDir( $name );
293 # Delete the image if it exists. Sometimes the file will be missing
294 # due to manual intervention or weird sync problems; treat that
295 # condition gracefully and continue to delete the database entry.
296 # Also some records may end up with an empty oi_archive_name field
297 # if the original file was missing when a new upload was made;
298 # don't try to delete the directory then!
300 $targetFile = "{$archive}/{$oldimage}";
301 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink( $targetFile ) ) {
302 # If we actually have a file and can't delete it, throw an error.
303 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
304 } else {
305 # Log the deletion
306 $log = new LogPage( 'delete' );
307 $log->addEntry( 'delete', $this->mTitle, wfMsg('deletedrevision',$oldimage) );
311 function revert()
313 global $wgOut, $wgRequest;
314 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
316 $oldimage = $wgRequest->getText( 'oldimage' );
317 if ( strlen( $oldimage ) < 16 ) {
318 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
319 return;
321 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
322 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
323 return;
326 if ( wfReadOnly() ) {
327 $wgOut->readOnlyPage();
328 return;
330 if ( ! $this->mTitle->userCanEdit() ) {
331 $wgOut->sysopRequired();
332 return;
334 $name = substr( $oldimage, 15 );
336 $dest = wfImageDir( $name );
337 $archive = wfImageArchiveDir( $name );
338 $curfile = "{$dest}/{$name}";
340 if ( ! is_file( $curfile ) ) {
341 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
342 return;
344 $oldver = wfTimestampNow() . "!{$name}";
346 $dbr =& wfGetDB( DB_SLAVE );
347 $size = $dbr->selectField( 'oldimage', 'oi_size', 'oi_archive_name=\'' .
348 $dbr->strencode( $oldimage ) . "'" );
350 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
351 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
352 return;
354 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
355 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
357 wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
358 # Squid purging
359 if ( $wgUseSquid ) {
360 $urlArr = Array(
361 $wgInternalServer.wfImageArchiveUrl( $name ),
362 $wgInternalServer . Image::wfImageUrl( $name )
364 wfPurgeSquidServers($urlArr);
367 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
368 $wgOut->setRobotpolicy( 'noindex,nofollow' );
369 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
370 $wgOut->returnToMain( false );
374 class ImageHistoryList {
375 function ImageHistoryList( &$skin ) {
376 $this->skin =& $skin;
379 function beginImageHistoryList() {
380 $s = "\n<h2>" . wfMsg( 'imghistory' ) . "</h2>\n" .
381 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
382 return $s;
385 function endImageHistoryList() {
386 $s = "</ul>\n";
387 return $s;
390 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
391 global $wgUser, $wgLang, $wgContLang, $wgTitle;
393 $datetime = $wgLang->timeanddate( $timestamp, true );
394 $del = wfMsg( 'deleteimg' );
395 $delall = wfMsg( 'deleteimgcompletely' );
396 $cur = wfMsg( 'cur' );
398 if ( $iscur ) {
399 $url = Image::wfImageUrl( $img );
400 $rlink = $cur;
401 if ( $wgUser->isAllowed('delete') ) {
402 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
403 '&action=delete' );
404 $style = $this->skin->getInternalLinkAttributes( $link, $delall );
406 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
407 } else {
408 $dlink = $del;
410 } else {
411 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
412 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
413 $rlink = $this->skin->makeKnownLink( $wgTitle->getPrefixedText(),
414 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
415 urlencode( $img ) );
416 $dlink = $this->skin->makeKnownLink( $wgTitle->getPrefixedText(),
417 $del, 'action=delete&oldimage=' . urlencode( $img ) );
418 } else {
419 # Having live active links for non-logged in users
420 # means that bots and spiders crawling our site can
421 # inadvertently change content. Baaaad idea.
422 $rlink = wfMsg( 'revertimg' );
423 $dlink = $del;
426 if ( 0 == $user ) {
427 $userlink = $usertext;
428 } else {
429 $userlink = $this->skin->makeLink( $wgContLang->getNsText( Namespace::getUser() ) .
430 ':'.$usertext, $usertext );
432 $nbytes = wfMsg( 'nbytes', $size );
433 $style = $this->skin->getInternalLinkAttributes( $url, $datetime );
435 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
436 . " . . {$userlink} ({$nbytes})";
438 if ( '' != $description && '*' != $description ) {
439 $sk=$wgUser->getSkin();
440 $s .= $wgContLang->emphasize(' (' . $sk->formatComment($description,$wgTitle) . ')');
442 $s .= "</li>\n";
443 return $s;