The return of the validation feature (caveat: new table scheme!)
[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( 'wpConfirmB' );
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 /* refresh image metadata cache */
311 new Image( $image, true );
312
313 $deleted = $image;
314 }
315
316 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
317 $wgOut->setRobotpolicy( 'noindex,nofollow' );
318
319 $sk = $wgUser->getSkin();
320 $loglink = $sk->makeKnownLinkObj(
321 Title::makeTitle( NS_SPECIAL, 'Delete/log' ),
322 wfMsg( 'deletionlog' ) );
323
324 $text = wfMsg( 'deletedtext', $deleted, $loglink );
325
326 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
327 $wgOut->returnToMain( false );
328 }
329
330 function doDeleteOldImage( $oldimage )
331 {
332 global $wgOut;
333
334 $name = substr( $oldimage, 15 );
335 $archive = wfImageArchiveDir( $name );
336
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!
343 #
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}" );
348 } else {
349 # Log the deletion
350 $log = new LogPage( 'delete' );
351 $log->addEntry( 'delete', $this->mTitle, wfMsg('deletedrevision',$oldimage) );
352 }
353 }
354
355 function revert()
356 {
357 global $wgOut, $wgRequest, $wgUser;
358 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
359
360 $oldimage = $wgRequest->getText( 'oldimage' );
361 if ( strlen( $oldimage ) < 16 ) {
362 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
363 return;
364 }
365 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
366 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
367 return;
368 }
369
370 if ( wfReadOnly() ) {
371 $wgOut->readOnlyPage();
372 return;
373 }
374 if( $wgUser->isAnon() ) {
375 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
376 return;
377 }
378 if ( ! $this->mTitle->userCanEdit() ) {
379 $wgOut->sysopRequired();
380 return;
381 }
382 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
383 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
384 return;
385 }
386 $name = substr( $oldimage, 15 );
387
388 $dest = wfImageDir( $name );
389 $archive = wfImageArchiveDir( $name );
390 $curfile = "{$dest}/{$name}";
391
392 if ( ! is_file( $curfile ) ) {
393 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
394 return;
395 }
396 $oldver = wfTimestampNow() . "!{$name}";
397
398 $dbr =& wfGetDB( DB_SLAVE );
399 $size = $dbr->selectField( 'oldimage', 'oi_size', 'oi_archive_name=\'' .
400 $dbr->strencode( $oldimage ) . "'" );
401
402 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
403 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
404 return;
405 }
406 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
407 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
408 }
409 wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
410
411 /* refresh image metadata cache */
412 new Image( $name, true );
413
414 # Squid purging
415 if ( $wgUseSquid ) {
416 $urlArr = Array(
417 $wgInternalServer.wfImageArchiveUrl( $name ),
418 $wgInternalServer . Image::wfImageUrl( $name )
419 );
420 wfPurgeSquidServers($urlArr);
421 }
422
423 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
424 $wgOut->setRobotpolicy( 'noindex,nofollow' );
425 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
426 $wgOut->returnToMain( false );
427 }
428 }
429
430 /**
431 * @todo document
432 * @package MediaWiki
433 */
434 class ImageHistoryList {
435 function ImageHistoryList( &$skin ) {
436 $this->skin =& $skin;
437 }
438
439 function beginImageHistoryList() {
440 $s = "\n<h2>" . wfMsg( 'imghistory' ) . "</h2>\n" .
441 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
442 return $s;
443 }
444
445 function endImageHistoryList() {
446 $s = "</ul>\n";
447 return $s;
448 }
449
450 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
451 global $wgUser, $wgLang, $wgContLang, $wgTitle;
452
453 $datetime = $wgLang->timeanddate( $timestamp, true );
454 $del = wfMsg( 'deleteimg' );
455 $delall = wfMsg( 'deleteimgcompletely' );
456 $cur = wfMsg( 'cur' );
457
458 if ( $iscur ) {
459 $url = Image::wfImageUrl( $img );
460 $rlink = $cur;
461 if ( $wgUser->isAllowed('delete') ) {
462 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
463 '&action=delete' );
464 $style = $this->skin->getInternalLinkAttributes( $link, $delall );
465
466 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
467 } else {
468 $dlink = $del;
469 }
470 } else {
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" );
480 } else {
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' );
485 $dlink = $del;
486 }
487 }
488 if ( 0 == $user ) {
489 $userlink = $usertext;
490 } else {
491 $userlink = $this->skin->makeLinkObj(
492 Title::makeTitle( NS_USER, $usertext ),
493 $usertext );
494 }
495 $nbytes = wfMsg( 'nbytes', $size );
496 $style = $this->skin->getInternalLinkAttributes( $url, $datetime );
497
498 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
499 . " . . {$userlink} ({$nbytes})";
500
501 $s .= $this->skin->commentBlock( $description, $wgTitle );
502 $s .= "</li>\n";
503 return $s;
504 }
505
506 }
507
508
509 ?>