Page move undo feature
[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 global $wgUseExternalEditor;
25 if( $this->mTitle->getNamespace() == NS_IMAGE ) {
26 $this->openShowImage();
27 }
28
29 Article::view();
30 if($wgUseExternalEditor) {
31 $this->externalEditorLink();
32 }
33
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.
37
38 if( $this->mTitle->getNamespace() == NS_IMAGE ) {
39 $this->closeShowImage();
40 $this->imageHistory();
41 $this->imageLinks();
42 }
43 }
44
45 function openShowImage()
46 {
47 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
48 $wgUseImageResize, $wgRepositoryBaseUrl,
49 $wgUseExternalEditor;
50 $this->img = Image::newFromTitle( $this->mTitle );
51 $full_url = $this->img->getViewURL();
52 $anchoropen = '';
53 $anchorclose = '';
54
55 if( $wgUser->getOption( 'imagesize' ) == '' ) {
56 $sizeSel = User::getDefaultOption( 'imagesize' );
57 } else {
58 $sizeSel = IntVal( $wgUser->getOption( 'imagesize' ) );
59 }
60 if( !isset( $wgImageLimits[$sizeSel] ) ) {
61 $sizeSel = User::getDefaultOption( 'imagesize' );
62 }
63 $max = $wgImageLimits[$sizeSel];
64 $maxWidth = $max[0];
65 $maxHeight = $max[1];
66
67
68 if ( $this->img->exists() ) {
69
70 $sk = $wgUser->getSkin();
71
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;
80 }
81 if ( $height > $maxHeight ) {
82 $width = floor( $width * $maxHeight / $height );
83 $height = $maxHeight;
84 }
85 if ( $width != $this->img->getWidth() || $height != $this->img->getHeight() ) {
86 if( $wgUseImageResize ) {
87 $thumbnail = $this->img->getThumbnail( $width );
88
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();
98 }
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;
103 }
104 $anchoropen = "<a href=\"{$full_url}\">";
105 $anchorclose = "</a><br />\n$anchoropen{$msg}</a>";
106 } else {
107 $url = $full_url;
108 }
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>';
114 }
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()));
120 }
121 $sharedtext.="</div>";
122 $wgOut->addWikiText($sharedtext);
123 }
124
125 }
126 }
127
128 function externalEditorLink()
129 {
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" />' );
139 }
140
141 function closeShowImage()
142 {
143 # For overloading
144
145 }
146
147 /**
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.
150 */
151 function imageHistory()
152 {
153 global $wgUser, $wgOut;
154
155 $sk = $wgUser->getSkin();
156
157 $line = $this->img->nextHistoryLine();
158
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 );
165
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 );
170 }
171 $s .= $list->endImageHistoryList();
172 } else { $s=''; }
173 $wgOut->addHTML( $s );
174 }
175
176 function imageLinks()
177 {
178 global $wgUser, $wgOut;
179
180 $wgOut->addHTML( '<h2>' . wfMsg( 'imagelinks' ) . "</h2>\n" );
181
182 $dbr =& wfGetDB( DB_SLAVE );
183 $page = $dbr->tableName( 'page' );
184 $imagelinks = $dbr->tableName( 'imagelinks' );
185
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" );
190
191 if ( 0 == $dbr->numRows( $res ) ) {
192 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
193 return;
194 }
195 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
196
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" );
202 }
203 $wgOut->addHTML( "</ul>\n" );
204 }
205
206 function delete()
207 {
208 global $wgUser, $wgOut, $wgRequest;
209
210 $confirm = $wgRequest->getBool( 'wpConfirmB' );
211 $image = $wgRequest->getVal( 'image' );
212 $oldimage = $wgRequest->getVal( 'oldimage' );
213
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;
219 }
220 if ( wfReadOnly() ) {
221 $wgOut->readOnlyPage();
222 return;
223 }
224
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;
231 }
232
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' ) );
239 }
240 return;
241 }
242
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 = '';
249 }
250 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
251 }
252
253 function doDelete()
254 {
255 global $wgOut, $wgUser, $wgContLang, $wgRequest;
256 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
257 $fname = 'ImagePage::doDelete';
258
259 $reason = $wgRequest->getVal( 'wpReason' );
260 $oldimage = $wgRequest->getVal( 'oldimage' );
261
262 $dbw =& wfGetDB( DB_MASTER );
263
264 if ( !is_null( $oldimage ) ) {
265 if ( strlen( $oldimage ) < 16 ) {
266 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
267 return;
268 }
269 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
270 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
271 return;
272 }
273 # Squid purging
274 if ( $wgUseSquid ) {
275 $urlArr = Array(
276 $wgInternalServer.wfImageArchiveUrl( $oldimage )
277 );
278 wfPurgeSquidServers($urlArr);
279 }
280 $this->doDeleteOldImage( $oldimage );
281 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
282 $deleted = $oldimage;
283 } else {
284 $image = $this->mTitle->getDBkey();
285 $dest = wfImageDir( $image );
286 $archive = wfImageDir( $image );
287
288 # Delete the image file if it exists; due to sync problems
289 # or manual trimming sometimes the file will be missing.
290 $targetFile = "{$dest}/{$image}";
291 if( file_exists( $targetFile ) && ! @unlink( $targetFile ) ) {
292 # If the deletion operation actually failed, bug out:
293 $wgOut->fileDeleteError( $targetFile );
294 return;
295 }
296 $dbw->delete( 'image', array( 'img_name' => $image ) );
297 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
298
299 # Squid purging
300 if ( $wgUseSquid ) {
301 $urlArr = Array(
302 $wgInternalServer . Image::wfImageUrl( $image )
303 );
304 wfPurgeSquidServers($urlArr);
305 }
306
307
308 $urlArr = Array();
309 while ( $s = $dbw->fetchObject( $res ) ) {
310 $this->doDeleteOldImage( $s->oi_archive_name );
311 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name );
312 }
313
314 # Squid purging, part II
315 if ( $wgUseSquid ) {
316 /* this needs to be done after LinksUpdate */
317 $u = new SquidUpdate( $urlArr );
318 array_push( $wgDeferredUpdateList, $u );
319 }
320
321 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
322
323 # Image itself is now gone, and database is cleaned.
324 # Now we remove the image description page.
325
326 $nt = Title::makeTitleSafe( NS_IMAGE, $image );
327 $article = new Article( $nt );
328 $article->doDeleteArticle( $reason ); # ignore errors
329
330 /* refresh image metadata cache */
331 new Image( $image, true );
332
333 $deleted = $image;
334 }
335
336 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
337 $wgOut->setRobotpolicy( 'noindex,nofollow' );
338
339 $sk = $wgUser->getSkin();
340 $loglink = $sk->makeKnownLinkObj(
341 Title::makeTitle( NS_SPECIAL, 'Delete/log' ),
342 wfMsg( 'deletionlog' ) );
343
344 $text = wfMsg( 'deletedtext', $deleted, $loglink );
345
346 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
347 $wgOut->returnToMain( false );
348 }
349
350 function doDeleteOldImage( $oldimage )
351 {
352 global $wgOut;
353
354 $name = substr( $oldimage, 15 );
355 $archive = wfImageArchiveDir( $name );
356
357 # Delete the image if it exists. Sometimes the file will be missing
358 # due to manual intervention or weird sync problems; treat that
359 # condition gracefully and continue to delete the database entry.
360 # Also some records may end up with an empty oi_archive_name field
361 # if the original file was missing when a new upload was made;
362 # don't try to delete the directory then!
363 #
364 $targetFile = "{$archive}/{$oldimage}";
365 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink( $targetFile ) ) {
366 # If we actually have a file and can't delete it, throw an error.
367 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
368 } else {
369 # Log the deletion
370 $log = new LogPage( 'delete' );
371 $log->addEntry( 'delete', $this->mTitle, wfMsg('deletedrevision',$oldimage) );
372 }
373 }
374
375 function revert()
376 {
377 global $wgOut, $wgRequest, $wgUser;
378 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
379
380 $oldimage = $wgRequest->getText( 'oldimage' );
381 if ( strlen( $oldimage ) < 16 ) {
382 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
383 return;
384 }
385 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
386 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
387 return;
388 }
389
390 if ( wfReadOnly() ) {
391 $wgOut->readOnlyPage();
392 return;
393 }
394 if( $wgUser->isAnon() ) {
395 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
396 return;
397 }
398 if ( ! $this->mTitle->userCanEdit() ) {
399 $wgOut->sysopRequired();
400 return;
401 }
402 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
403 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
404 return;
405 }
406 $name = substr( $oldimage, 15 );
407
408 $dest = wfImageDir( $name );
409 $archive = wfImageArchiveDir( $name );
410 $curfile = "{$dest}/{$name}";
411
412 if ( ! is_file( $curfile ) ) {
413 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
414 return;
415 }
416 $oldver = wfTimestampNow() . "!{$name}";
417
418 $dbr =& wfGetDB( DB_SLAVE );
419 $size = $dbr->selectField( 'oldimage', 'oi_size', 'oi_archive_name=\'' .
420 $dbr->strencode( $oldimage ) . "'" );
421
422 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
423 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
424 return;
425 }
426 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
427 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
428 }
429 wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
430
431 /* refresh image metadata cache */
432 new Image( $name, true );
433
434 # Squid purging
435 if ( $wgUseSquid ) {
436 $urlArr = Array(
437 $wgInternalServer.wfImageArchiveUrl( $name ),
438 $wgInternalServer . Image::wfImageUrl( $name )
439 );
440 wfPurgeSquidServers($urlArr);
441 }
442
443 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
444 $wgOut->setRobotpolicy( 'noindex,nofollow' );
445 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
446 $wgOut->returnToMain( false );
447 }
448 }
449
450 /**
451 * @todo document
452 * @package MediaWiki
453 */
454 class ImageHistoryList {
455 function ImageHistoryList( &$skin ) {
456 $this->skin =& $skin;
457 }
458
459 function beginImageHistoryList() {
460 $s = "\n<h2>" . wfMsg( 'imghistory' ) . "</h2>\n" .
461 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
462 return $s;
463 }
464
465 function endImageHistoryList() {
466 $s = "</ul>\n";
467 return $s;
468 }
469
470 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
471 global $wgUser, $wgLang, $wgContLang, $wgTitle;
472
473 $datetime = $wgLang->timeanddate( $timestamp, true );
474 $del = wfMsg( 'deleteimg' );
475 $delall = wfMsg( 'deleteimgcompletely' );
476 $cur = wfMsg( 'cur' );
477
478 if ( $iscur ) {
479 $url = Image::wfImageUrl( $img );
480 $rlink = $cur;
481 if ( $wgUser->isAllowed('delete') ) {
482 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
483 '&action=delete' );
484 $style = $this->skin->getInternalLinkAttributes( $link, $delall );
485
486 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
487 } else {
488 $dlink = $del;
489 }
490 } else {
491 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
492 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
493 $token = urlencode( $wgUser->editToken( $img ) );
494 $rlink = $this->skin->makeKnownLinkObj( $wgTitle,
495 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
496 urlencode( $img ) . "&wpEditToken=$token" );
497 $dlink = $this->skin->makeKnownLinkObj( $wgTitle,
498 $del, 'action=delete&oldimage=' . urlencode( $img ) .
499 "&wpEditToken=$token" );
500 } else {
501 # Having live active links for non-logged in users
502 # means that bots and spiders crawling our site can
503 # inadvertently change content. Baaaad idea.
504 $rlink = wfMsg( 'revertimg' );
505 $dlink = $del;
506 }
507 }
508 if ( 0 == $user ) {
509 $userlink = $usertext;
510 } else {
511 $userlink = $this->skin->makeLinkObj(
512 Title::makeTitle( NS_USER, $usertext ),
513 $usertext );
514 }
515 $nbytes = wfMsg( 'nbytes', $size );
516 $style = $this->skin->getInternalLinkAttributes( $url, $datetime );
517
518 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
519 . " . . {$userlink} ({$nbytes})";
520
521 $s .= $this->skin->commentBlock( $description, $wgTitle );
522 $s .= "</li>\n";
523 return $s;
524 }
525
526 }
527
528
529 ?>