* (bug 1765) Tidy causes corruption inside <gallery>
[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->makeKnownLink($this->mTitle->getPrefixedDBkey(),wfMsg("edit-externally"),
134 "action=edit&externaledit=true&mode=file"));
135 $wgOut->addWikiText("<div class=\"editExternallyHelp\">".wfMsg("edit-externally-help"));
136 $wgOut->addHTML("</div><br clear=\"all\"/>");
137
138 }
139 function closeShowImage()
140 {
141 # For overloading
142
143 }
144
145 /**
146 * If the page we've just displayed is in the "Image" namespace,
147 * we follow it with an upload history of the image and its usage.
148 */
149 function imageHistory()
150 {
151 global $wgUser, $wgOut;
152
153 $sk = $wgUser->getSkin();
154
155 $line = $this->img->nextHistoryLine();
156
157 if ( $line ) {
158 $list =& new ImageHistoryList( $sk );
159 $s = $list->beginImageHistoryList() .
160 $list->imageHistoryLine( true, $line->img_timestamp,
161 $this->mTitle->getDBkey(), $line->img_user,
162 $line->img_user_text, $line->img_size, $line->img_description );
163
164 while ( $line = $this->img->nextHistoryLine() ) {
165 $s .= $list->imageHistoryLine( false, $line->img_timestamp,
166 $line->oi_archive_name, $line->img_user,
167 $line->img_user_text, $line->img_size, $line->img_description );
168 }
169 $s .= $list->endImageHistoryList();
170 } else { $s=''; }
171 $wgOut->addHTML( $s );
172 }
173
174 function imageLinks()
175 {
176 global $wgUser, $wgOut;
177
178 $wgOut->addHTML( '<h2>' . wfMsg( 'imagelinks' ) . "</h2>\n" );
179
180 $dbr =& wfGetDB( DB_SLAVE );
181 $page = $dbr->tableName( 'page' );
182 $imagelinks = $dbr->tableName( 'imagelinks' );
183
184 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
185 $dbr->addQuotes( $this->mTitle->getDBkey() ) . " AND il_from=page_id"
186 . " LIMIT 500"; # quickie emergency brake
187 $res = $dbr->query( $sql, DB_SLAVE, "Article::imageLinks" );
188
189 if ( 0 == $dbr->numRows( $res ) ) {
190 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
191 return;
192 }
193 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
194
195 $sk = $wgUser->getSkin();
196 while ( $s = $dbr->fetchObject( $res ) ) {
197 $name = Title::MakeTitle( $s->page_namespace, $s->page_title );
198 $link = $sk->makeKnownLinkObj( $name, "" );
199 $wgOut->addHTML( "<li>{$link}</li>\n" );
200 }
201 $wgOut->addHTML( "</ul>\n" );
202 }
203
204 function delete()
205 {
206 global $wgUser, $wgOut, $wgRequest;
207
208 $confirm = $wgRequest->getBool( 'wpConfirmB' );
209 $image = $wgRequest->getVal( 'image' );
210 $oldimage = $wgRequest->getVal( 'oldimage' );
211
212 # Only sysops can delete images. Previously ordinary users could delete
213 # old revisions, but this is no longer the case.
214 if ( !$wgUser->isAllowed('delete') ) {
215 $wgOut->sysopRequired();
216 return;
217 }
218 if ( wfReadOnly() ) {
219 $wgOut->readOnlyPage();
220 return;
221 }
222
223 # Better double-check that it hasn't been deleted yet!
224 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
225 if ( ( !is_null( $image ) )
226 && ( '' == trim( $image ) ) ) {
227 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
228 return;
229 }
230
231 # Deleting old images doesn't require confirmation
232 if ( !is_null( $oldimage ) || $confirm ) {
233 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
234 $this->doDelete();
235 } else {
236 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
237 }
238 return;
239 }
240
241 if ( !is_null( $image ) ) {
242 $q = '&image=' . urlencode( $image );
243 } else if ( !is_null( $oldimage ) ) {
244 $q = '&oldimage=' . urlencode( $oldimage );
245 } else {
246 $q = '';
247 }
248 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
249 }
250
251 function doDelete()
252 {
253 global $wgOut, $wgUser, $wgContLang, $wgRequest;
254 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
255 $fname = 'ImagePage::doDelete';
256
257 $reason = $wgRequest->getVal( 'wpReason' );
258 $oldimage = $wgRequest->getVal( 'oldimage' );
259
260 $dbw =& wfGetDB( DB_MASTER );
261
262 if ( !is_null( $oldimage ) ) {
263 if ( strlen( $oldimage ) < 16 ) {
264 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
265 return;
266 }
267 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
268 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
269 return;
270 }
271 # Squid purging
272 if ( $wgUseSquid ) {
273 $urlArr = Array(
274 $wgInternalServer.wfImageArchiveUrl( $oldimage )
275 );
276 wfPurgeSquidServers($urlArr);
277 }
278 $this->doDeleteOldImage( $oldimage );
279 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
280 $deleted = $oldimage;
281 } else {
282 $image = $this->mTitle->getDBkey();
283 $dest = wfImageDir( $image );
284 $archive = wfImageDir( $image );
285
286 # Delete the image file if it exists; due to sync problems
287 # or manual trimming sometimes the file will be missing.
288 $targetFile = "{$dest}/{$image}";
289 if( file_exists( $targetFile ) && ! @unlink( $targetFile ) ) {
290 # If the deletion operation actually failed, bug out:
291 $wgOut->fileDeleteError( $targetFile );
292 return;
293 }
294 $dbw->delete( 'image', array( 'img_name' => $image ) );
295 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
296
297 # Squid purging
298 if ( $wgUseSquid ) {
299 $urlArr = Array(
300 $wgInternalServer . Image::wfImageUrl( $image )
301 );
302 wfPurgeSquidServers($urlArr);
303 }
304
305
306 $urlArr = Array();
307 while ( $s = $dbw->fetchObject( $res ) ) {
308 $this->doDeleteOldImage( $s->oi_archive_name );
309 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name );
310 }
311
312 # Squid purging, part II
313 if ( $wgUseSquid ) {
314 /* this needs to be done after LinksUpdate */
315 $u = new SquidUpdate( $urlArr );
316 array_push( $wgDeferredUpdateList, $u );
317 }
318
319 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
320
321 # Image itself is now gone, and database is cleaned.
322 # Now we remove the image description page.
323
324 $nt = Title::makeTitleSafe( NS_IMAGE, $image );
325 $article = new Article( $nt );
326 $article->doDeleteArticle( $reason ); # ignore errors
327
328 /* refresh image metadata cache */
329 new Image( $image, true );
330
331 $deleted = $image;
332 }
333
334 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
335 $wgOut->setRobotpolicy( 'noindex,nofollow' );
336
337 $sk = $wgUser->getSkin();
338 $loglink = $sk->makeKnownLinkObj(
339 Title::makeTitle( NS_SPECIAL, 'Delete/log' ),
340 wfMsg( 'deletionlog' ) );
341
342 $text = wfMsg( 'deletedtext', $deleted, $loglink );
343
344 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
345 $wgOut->returnToMain( false );
346 }
347
348 function doDeleteOldImage( $oldimage )
349 {
350 global $wgOut;
351
352 $name = substr( $oldimage, 15 );
353 $archive = wfImageArchiveDir( $name );
354
355 # Delete the image if it exists. Sometimes the file will be missing
356 # due to manual intervention or weird sync problems; treat that
357 # condition gracefully and continue to delete the database entry.
358 # Also some records may end up with an empty oi_archive_name field
359 # if the original file was missing when a new upload was made;
360 # don't try to delete the directory then!
361 #
362 $targetFile = "{$archive}/{$oldimage}";
363 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink( $targetFile ) ) {
364 # If we actually have a file and can't delete it, throw an error.
365 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
366 } else {
367 # Log the deletion
368 $log = new LogPage( 'delete' );
369 $log->addEntry( 'delete', $this->mTitle, wfMsg('deletedrevision',$oldimage) );
370 }
371 }
372
373 function revert()
374 {
375 global $wgOut, $wgRequest, $wgUser;
376 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
377
378 $oldimage = $wgRequest->getText( 'oldimage' );
379 if ( strlen( $oldimage ) < 16 ) {
380 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
381 return;
382 }
383 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
384 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
385 return;
386 }
387
388 if ( wfReadOnly() ) {
389 $wgOut->readOnlyPage();
390 return;
391 }
392 if( $wgUser->isAnon() ) {
393 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
394 return;
395 }
396 if ( ! $this->mTitle->userCanEdit() ) {
397 $wgOut->sysopRequired();
398 return;
399 }
400 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
401 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
402 return;
403 }
404 $name = substr( $oldimage, 15 );
405
406 $dest = wfImageDir( $name );
407 $archive = wfImageArchiveDir( $name );
408 $curfile = "{$dest}/{$name}";
409
410 if ( ! is_file( $curfile ) ) {
411 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
412 return;
413 }
414 $oldver = wfTimestampNow() . "!{$name}";
415
416 $dbr =& wfGetDB( DB_SLAVE );
417 $size = $dbr->selectField( 'oldimage', 'oi_size', 'oi_archive_name=\'' .
418 $dbr->strencode( $oldimage ) . "'" );
419
420 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
421 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
422 return;
423 }
424 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
425 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
426 }
427 wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
428
429 /* refresh image metadata cache */
430 new Image( $name, true );
431
432 # Squid purging
433 if ( $wgUseSquid ) {
434 $urlArr = Array(
435 $wgInternalServer.wfImageArchiveUrl( $name ),
436 $wgInternalServer . Image::wfImageUrl( $name )
437 );
438 wfPurgeSquidServers($urlArr);
439 }
440
441 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
442 $wgOut->setRobotpolicy( 'noindex,nofollow' );
443 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
444 $wgOut->returnToMain( false );
445 }
446 }
447
448 /**
449 * @todo document
450 * @package MediaWiki
451 */
452 class ImageHistoryList {
453 function ImageHistoryList( &$skin ) {
454 $this->skin =& $skin;
455 }
456
457 function beginImageHistoryList() {
458 $s = "\n<h2>" . wfMsg( 'imghistory' ) . "</h2>\n" .
459 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
460 return $s;
461 }
462
463 function endImageHistoryList() {
464 $s = "</ul>\n";
465 return $s;
466 }
467
468 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
469 global $wgUser, $wgLang, $wgContLang, $wgTitle;
470
471 $datetime = $wgLang->timeanddate( $timestamp, true );
472 $del = wfMsg( 'deleteimg' );
473 $delall = wfMsg( 'deleteimgcompletely' );
474 $cur = wfMsg( 'cur' );
475
476 if ( $iscur ) {
477 $url = Image::wfImageUrl( $img );
478 $rlink = $cur;
479 if ( $wgUser->isAllowed('delete') ) {
480 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
481 '&action=delete' );
482 $style = $this->skin->getInternalLinkAttributes( $link, $delall );
483
484 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
485 } else {
486 $dlink = $del;
487 }
488 } else {
489 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
490 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
491 $token = urlencode( $wgUser->editToken( $img ) );
492 $rlink = $this->skin->makeKnownLink( $wgTitle->getPrefixedText(),
493 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
494 urlencode( $img ) . "&wpEditToken=$token" );
495 $dlink = $this->skin->makeKnownLink( $wgTitle->getPrefixedText(),
496 $del, 'action=delete&oldimage=' . urlencode( $img ) .
497 "&wpEditToken=$token" );
498 } else {
499 # Having live active links for non-logged in users
500 # means that bots and spiders crawling our site can
501 # inadvertently change content. Baaaad idea.
502 $rlink = wfMsg( 'revertimg' );
503 $dlink = $del;
504 }
505 }
506 if ( 0 == $user ) {
507 $userlink = $usertext;
508 } else {
509 $userlink = $this->skin->makeLinkObj(
510 Title::makeTitle( NS_USER, $usertext ),
511 $usertext );
512 }
513 $nbytes = wfMsg( 'nbytes', $size );
514 $style = $this->skin->getInternalLinkAttributes( $url, $datetime );
515
516 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
517 . " . . {$userlink} ({$nbytes})";
518
519 $s .= $this->skin->commentBlock( $description, $wgTitle );
520 $s .= "</li>\n";
521 return $s;
522 }
523
524 }
525
526
527 ?>