Cruft war continues: more uses of isAnon() and isLoggedIn()
[lhc/web/wiklou.git] / includes / ImagePage.php
1 <?php
2 /**
3 * @package MediaWiki
4 */
5
6 /**
7 *
8 */
9 if( !defined( 'MEDIAWIKI' ) )
10 die();
11
12 require_once( 'Image.php' );
13
14 /**
15 * Special handling for image description pages
16 * @package MediaWiki
17 */
18 class ImagePage extends Article {
19
20 /* private */ var $img; // Image object this page is shown for. Initilaized in openShowImage, not
21 // available in doDelete etc.
22
23 function view() {
24 if( $this->mTitle->getNamespace() == NS_IMAGE ) {
25 $this->openShowImage();
26 }
27
28 Article::view();
29
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
32 # it describes.
33
34 if( $this->mTitle->getNamespace() == NS_IMAGE ) {
35 $this->closeShowImage();
36 $this->imageHistory();
37 $this->imageLinks();
38 }
39 }
40
41 function openShowImage()
42 {
43 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
44 $wgUseImageResize, $wgRepositoryBaseUrl;
45 $this->img = Image::newFromTitle( $this->mTitle );
46 $full_url = $this->img->getViewURL();
47 $anchoropen = '';
48 $anchorclose = '';
49
50 if( $wgUser->getOption( 'imagesize' ) == '' ) {
51 $sizeSel = User::getDefaultOption( 'imagesize' );
52 } else {
53 $sizeSel = IntVal( $wgUser->getOption( 'imagesize' ) );
54 }
55 if( !isset( $wgImageLimits[$sizeSel] ) ) {
56 $sizeSel = User::getDefaultOption( 'imagesize' );
57 }
58 $max = $wgImageLimits[$sizeSel];
59 $maxWidth = $max[0];
60 $maxHeight = $max[1];
61
62
63 if ( $this->img->exists() ) {
64
65 $sk = $wgUser->getSkin();
66
67 if ( $this->img->getType() != '' ) {
68 # image
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 );
74 $width = $maxWidth;
75 }
76 if ( $height > $maxHeight ) {
77 $width = floor( $width * $maxHeight / $height );
78 $height = $maxHeight;
79 }
80 if ( $width != $this->img->getWidth() || $height != $this->img->getHeight() ) {
81 if( $wgUseImageResize ) {
82 $thumbnail = $this->img->getThumbnail( $width );
83
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.
88 $url = $full_url;
89 $width = $this->img->getWidth();
90 $height = $this->img->getHeight();
91 } else {
92 $url = $thumbnail->getUrl();
93 }
94 } else {
95 # No resize ability? Show the full image, but scale
96 # it down in the browser so it fits on the page.
97 $url = $full_url;
98 }
99 $anchoropen = "<a href=\"{$full_url}\">";
100 $anchorclose = "</a><br />\n$anchoropen{$msg}</a>";
101 } else {
102 $url = $full_url;
103 }
104 $s = '<div class="fullImageLink">' . $anchoropen .
105 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
106 htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>';
107 } else {
108 $s = "<div class=\"fullMedia\">" . $sk->makeMediaLink( $this->img->getName(),'' ) . '</div>';
109 }
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()));
115 }
116 $sharedtext.="</div>";
117 $wgOut->addWikiText($sharedtext);
118 }
119 }
120 }
121
122 function closeShowImage()
123 {
124 # For overloading
125 }
126
127 /**
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.
130 */
131 function imageHistory()
132 {
133 global $wgUser, $wgOut;
134
135 $sk = $wgUser->getSkin();
136
137 $line = $this->img->nextHistoryLine();
138
139 if ( $line ) {
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 );
145
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 );
150 }
151 $s .= $list->endImageHistoryList();
152 } else { $s=''; }
153 $wgOut->addHTML( $s );
154 }
155
156 function imageLinks()
157 {
158 global $wgUser, $wgOut;
159
160 $wgOut->addHTML( '<h2>' . wfMsg( 'imagelinks' ) . "</h2>\n" );
161
162 $dbr =& wfGetDB( DB_SLAVE );
163 $page = $dbr->tableName( 'page' );
164 $imagelinks = $dbr->tableName( 'imagelinks' );
165
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" );
170
171 if ( 0 == $dbr->numRows( $res ) ) {
172 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
173 return;
174 }
175 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
176
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" );
182 }
183 $wgOut->addHTML( "</ul>\n" );
184 }
185
186 function delete()
187 {
188 global $wgUser, $wgOut, $wgRequest;
189
190 $confirm = $wgRequest->getBool( 'wpConfirm' );
191 $image = $wgRequest->getVal( 'image' );
192 $oldimage = $wgRequest->getVal( 'oldimage' );
193
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();
198 return;
199 }
200 if ( wfReadOnly() ) {
201 $wgOut->readOnlyPage();
202 return;
203 }
204
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' ) );
210 return;
211 }
212
213 # Deleting old images doesn't require confirmation
214 if ( !is_null( $oldimage ) || $confirm ) {
215 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
216 $this->doDelete();
217 } else {
218 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
219 }
220 return;
221 }
222
223 if ( !is_null( $image ) ) {
224 $q = '&image=' . urlencode( $image );
225 } else if ( !is_null( $oldimage ) ) {
226 $q = '&oldimage=' . urlencode( $oldimage );
227 } else {
228 $q = '';
229 }
230 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
231 }
232
233 function doDelete()
234 {
235 global $wgOut, $wgUser, $wgContLang, $wgRequest;
236 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
237 $fname = 'ImagePage::doDelete';
238
239 $reason = $wgRequest->getVal( 'wpReason' );
240 $oldimage = $wgRequest->getVal( 'oldimage' );
241
242 $dbw =& wfGetDB( DB_MASTER );
243
244 if ( !is_null( $oldimage ) ) {
245 if ( strlen( $oldimage ) < 16 ) {
246 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
247 return;
248 }
249 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
250 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
251 return;
252 }
253 # Squid purging
254 if ( $wgUseSquid ) {
255 $urlArr = Array(
256 $wgInternalServer.wfImageArchiveUrl( $oldimage )
257 );
258 wfPurgeSquidServers($urlArr);
259 }
260 $this->doDeleteOldImage( $oldimage );
261 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
262 $deleted = $oldimage;
263 } else {
264 $image = $this->mTitle->getDBkey();
265 $dest = wfImageDir( $image );
266 $archive = wfImageDir( $image );
267
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 );
274 return;
275 }
276 $dbw->delete( 'image', array( 'img_name' => $image ) );
277 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
278
279 # Squid purging
280 if ( $wgUseSquid ) {
281 $urlArr = Array(
282 $wgInternalServer . Image::wfImageUrl( $image )
283 );
284 wfPurgeSquidServers($urlArr);
285 }
286
287
288 $urlArr = Array();
289 while ( $s = $dbw->fetchObject( $res ) ) {
290 $this->doDeleteOldImage( $s->oi_archive_name );
291 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name );
292 }
293
294 # Squid purging, part II
295 if ( $wgUseSquid ) {
296 /* this needs to be done after LinksUpdate */
297 $u = new SquidUpdate( $urlArr );
298 array_push( $wgDeferredUpdateList, $u );
299 }
300
301 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
302
303 # Image itself is now gone, and database is cleaned.
304 # Now we remove the image description page.
305
306 $nt = Title::makeTitleSafe( NS_IMAGE, $image );
307 $article = new Article( $nt );
308 $article->doDeleteArticle( $reason ); # ignore errors
309
310 $deleted = $image;
311 }
312
313 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
314 $wgOut->setRobotpolicy( 'noindex,nofollow' );
315
316 $sk = $wgUser->getSkin();
317 $loglink = $sk->makeKnownLinkObj(
318 Title::makeTitle( NS_SPECIAL, 'Delete/log' ),
319 wfMsg( 'deletionlog' ) );
320
321 $text = wfMsg( 'deletedtext', $deleted, $loglink );
322
323 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
324 $wgOut->returnToMain( false );
325 }
326
327 function doDeleteOldImage( $oldimage )
328 {
329 global $wgOut;
330
331 $name = substr( $oldimage, 15 );
332 $archive = wfImageArchiveDir( $name );
333
334 # Delete the image if it exists. Sometimes the file will be missing
335 # due to manual intervention or weird sync problems; treat that
336 # condition gracefully and continue to delete the database entry.
337 # Also some records may end up with an empty oi_archive_name field
338 # if the original file was missing when a new upload was made;
339 # don't try to delete the directory then!
340 #
341 $targetFile = "{$archive}/{$oldimage}";
342 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink( $targetFile ) ) {
343 # If we actually have a file and can't delete it, throw an error.
344 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
345 } else {
346 # Log the deletion
347 $log = new LogPage( 'delete' );
348 $log->addEntry( 'delete', $this->mTitle, wfMsg('deletedrevision',$oldimage) );
349 }
350 }
351
352 function revert()
353 {
354 global $wgOut, $wgRequest, $wgUser;
355 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
356
357 $oldimage = $wgRequest->getText( 'oldimage' );
358 if ( strlen( $oldimage ) < 16 ) {
359 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
360 return;
361 }
362 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
363 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
364 return;
365 }
366
367 if ( wfReadOnly() ) {
368 $wgOut->readOnlyPage();
369 return;
370 }
371 if( $wgUser->isAnon() ) {
372 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
373 return;
374 }
375 if ( ! $this->mTitle->userCanEdit() ) {
376 $wgOut->sysopRequired();
377 return;
378 }
379 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
380 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
381 return;
382 }
383 $name = substr( $oldimage, 15 );
384
385 $dest = wfImageDir( $name );
386 $archive = wfImageArchiveDir( $name );
387 $curfile = "{$dest}/{$name}";
388
389 if ( ! is_file( $curfile ) ) {
390 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
391 return;
392 }
393 $oldver = wfTimestampNow() . "!{$name}";
394
395 $dbr =& wfGetDB( DB_SLAVE );
396 $size = $dbr->selectField( 'oldimage', 'oi_size', 'oi_archive_name=\'' .
397 $dbr->strencode( $oldimage ) . "'" );
398
399 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
400 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
401 return;
402 }
403 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
404 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
405 }
406 wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
407 # Squid purging
408 if ( $wgUseSquid ) {
409 $urlArr = Array(
410 $wgInternalServer.wfImageArchiveUrl( $name ),
411 $wgInternalServer . Image::wfImageUrl( $name )
412 );
413 wfPurgeSquidServers($urlArr);
414 }
415
416 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
417 $wgOut->setRobotpolicy( 'noindex,nofollow' );
418 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
419 $wgOut->returnToMain( false );
420 }
421 }
422
423 /**
424 * @todo document
425 * @package MediaWiki
426 */
427 class ImageHistoryList {
428 function ImageHistoryList( &$skin ) {
429 $this->skin =& $skin;
430 }
431
432 function beginImageHistoryList() {
433 $s = "\n<h2>" . wfMsg( 'imghistory' ) . "</h2>\n" .
434 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
435 return $s;
436 }
437
438 function endImageHistoryList() {
439 $s = "</ul>\n";
440 return $s;
441 }
442
443 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
444 global $wgUser, $wgLang, $wgContLang, $wgTitle;
445
446 $datetime = $wgLang->timeanddate( $timestamp, true );
447 $del = wfMsg( 'deleteimg' );
448 $delall = wfMsg( 'deleteimgcompletely' );
449 $cur = wfMsg( 'cur' );
450
451 if ( $iscur ) {
452 $url = Image::wfImageUrl( $img );
453 $rlink = $cur;
454 if ( $wgUser->isAllowed('delete') ) {
455 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
456 '&action=delete' );
457 $style = $this->skin->getInternalLinkAttributes( $link, $delall );
458
459 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
460 } else {
461 $dlink = $del;
462 }
463 } else {
464 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
465 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
466 $token = urlencode( $wgUser->editToken( $img ) );
467 $rlink = $this->skin->makeKnownLink( $wgTitle->getPrefixedText(),
468 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
469 urlencode( $img ) . "&wpEditToken=$token" );
470 $dlink = $this->skin->makeKnownLink( $wgTitle->getPrefixedText(),
471 $del, 'action=delete&oldimage=' . urlencode( $img ) .
472 "&wpEditToken=$token" );
473 } else {
474 # Having live active links for non-logged in users
475 # means that bots and spiders crawling our site can
476 # inadvertently change content. Baaaad idea.
477 $rlink = wfMsg( 'revertimg' );
478 $dlink = $del;
479 }
480 }
481 if ( 0 == $user ) {
482 $userlink = $usertext;
483 } else {
484 $userlink = $this->skin->makeLinkObj(
485 Title::makeTitle( NS_USER, $usertext ),
486 $usertext );
487 }
488 $nbytes = wfMsg( 'nbytes', $size );
489 $style = $this->skin->getInternalLinkAttributes( $url, $datetime );
490
491 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
492 . " . . {$userlink} ({$nbytes})";
493
494 $s .= $this->skin->commentBlock( $description, $wgTitle );
495 $s .= "</li>\n";
496 return $s;
497 }
498
499 }
500
501
502 ?>