Add some conditional die()s to suppress fatal error messages if certain files are...
[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 ( Namespace::getImage() == $this->mTitle->getNamespace() ) {
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 ( Namespace::getImage() == $this->mTitle->getNamespace() ) {
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 $url = $this->img->getViewURL();
47 $anchoropen = '';
48 $anchorclose = '';
49 if ( $wgUseImageResize && $wgUser->getOption( 'imagesize' ) != '' ) {
50 $max = $wgImageLimits[ intval( $wgUser->getOption( 'imagesize' ) ) ];
51 $maxWidth = $max[0];
52 $maxHeight = $max[1];
53 }
54
55
56 if ( $this->img->exists() ) {
57
58 $sk = $wgUser->getSkin();
59
60 if ( $this->img->getType() != '' ) {
61 # image
62 $width = $this->img->getWidth();
63 $height = $this->img->getHeight();
64 $msg = wfMsg('showbigimage', $width, $height, intval( $this->img->getSize()/1024 ) );
65 if ( $width > $maxWidth && $wgUseImageResize ) {
66 $anchoropen = "<a href=\"{$url}\">";
67 $anchorclose = "<br>{$msg}</a>";
68
69 $url = $this->img->createThumb( $maxWidth );
70 $height = floor( $height * $maxWidth / $width );
71 $width = $maxWidth;
72 }
73 if ( $height > $maxHeight && $wgUseImageResize ) {
74 $anchoropen = "<a href=\"{$url}\">";
75 $anchorclose = "<br>{$msg}</a>";
76
77 $width = floor( $width * $maxHeight / $height );
78 $height = $maxHeight;
79 $url = $this->img->createThumb( $width );
80 }
81 $s = "<div class=\"fullImageLink\">" . $anchoropen .
82 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
83 htmlspecialchars( $wgRequest->getVal( 'image' ) )."\" />" . $anchorclose . "</div>";
84 } else {
85 $s = "<div class=\"fullMedia\">".$sk->makeMediaLink($this->img->getName(),"")."</div>";
86 }
87 $wgOut->addHTML( $s );
88 if($this->img->fromSharedDirectory) {
89 $sharedtext="<div class=\"sharedUploadNotice\">" . wfMsg("sharedupload");
90 if($wgRepositoryBaseUrl) {
91 $sharedtext .= " ". wfMsg("shareduploadwiki",$wgRepositoryBaseUrl . urlencode($this->mTitle->getDBkey()));
92 }
93 $sharedtext.="</div>";
94 $wgOut->addWikiText($sharedtext);
95 }
96 }
97 }
98
99 function closeShowImage()
100 {
101 # For overloading
102 }
103
104 /**
105 * If the page we've just displayed is in the "Image" namespace,
106 * we follow it with an upload history of the image and its usage.
107 */
108 function imageHistory()
109 {
110 global $wgUser, $wgOut;
111
112 $sk = $wgUser->getSkin();
113
114 $line = $this->img->nextHistoryLine();
115
116 if ( $line ) {
117 $list =& new ImageHistoryList( $sk );
118 $s = $list->beginImageHistoryList() .
119 $list->imageHistoryLine( true, $line->img_timestamp,
120 $this->mTitle->getDBkey(), $line->img_user,
121 $line->img_user_text, $line->img_size, $line->img_description );
122
123 while ( $line = $this->img->nextHistoryLine() ) {
124 $s .= $list->imageHistoryLine( false, $line->img_timestamp,
125 $line->oi_archive_name, $line->img_user,
126 $line->img_user_text, $line->img_size, $line->img_description );
127 }
128 $s .= $list->endImageHistoryList();
129 } else { $s=''; }
130 $wgOut->addHTML( $s );
131 }
132
133 function imageLinks()
134 {
135 global $wgUser, $wgOut;
136
137 $wgOut->addHTML( '<h2>' . wfMsg( 'imagelinks' ) . "</h2>\n" );
138
139 $dbr =& wfGetDB( DB_SLAVE );
140 $cur = $dbr->tableName( 'cur' );
141 $imagelinks = $dbr->tableName( 'imagelinks' );
142
143 $sql = "SELECT cur_namespace,cur_title FROM $imagelinks,$cur WHERE il_to=" .
144 $dbr->addQuotes( $this->mTitle->getDBkey() ) . " AND il_from=cur_id"
145 . " LIMIT 500"; # quickie emergency brake
146 $res = $dbr->query( $sql, DB_SLAVE, "Article::imageLinks" );
147
148 if ( 0 == $dbr->numRows( $res ) ) {
149 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
150 return;
151 }
152 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
153
154 $sk = $wgUser->getSkin();
155 while ( $s = $dbr->fetchObject( $res ) ) {
156 $name = Title::MakeTitle( $s->cur_namespace, $s->cur_title );
157 $link = $sk->makeKnownLinkObj( $name, "" );
158 $wgOut->addHTML( "<li>{$link}</li>\n" );
159 }
160 $wgOut->addHTML( "</ul>\n" );
161 }
162
163 function delete()
164 {
165 global $wgUser, $wgOut, $wgRequest;
166
167 $confirm = $wgRequest->getBool( 'wpConfirm' );
168 $image = $wgRequest->getVal( 'image' );
169 $oldimage = $wgRequest->getVal( 'oldimage' );
170
171 # Only sysops can delete images. Previously ordinary users could delete
172 # old revisions, but this is no longer the case.
173 if ( !$wgUser->isAllowed('delete') ) {
174 $wgOut->sysopRequired();
175 return;
176 }
177 if ( wfReadOnly() ) {
178 $wgOut->readOnlyPage();
179 return;
180 }
181
182 # Better double-check that it hasn't been deleted yet!
183 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
184 if ( !is_null( $image ) ) {
185 if ( '' == trim( $image ) ) {
186 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
187 return;
188 }
189 }
190
191 # Deleting old images doesn't require confirmation
192 if ( !is_null( $oldimage ) || $confirm ) {
193 $this->doDelete();
194 return;
195 }
196
197 if ( !is_null( $image ) ) {
198 $q = '&image=' . urlencode( $image );
199 } else if ( !is_null( $oldimage ) ) {
200 $q = '&oldimage=' . urlencode( $oldimage );
201 } else {
202 $q = '';
203 }
204 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
205 }
206
207 function doDelete()
208 {
209 global $wgOut, $wgUser, $wgContLang, $wgRequest;
210 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
211 $fname = 'ImagePage::doDelete';
212
213 $reason = $wgRequest->getVal( 'wpReason' );
214 $image = $wgRequest->getVal( 'image' );
215 $oldimage = $wgRequest->getVal( 'oldimage' );
216
217 $dbw =& wfGetDB( DB_MASTER );
218
219 if ( !is_null( $oldimage ) ) {
220 # Squid purging
221 if ( $wgUseSquid ) {
222 $urlArr = Array(
223 $wgInternalServer.wfImageArchiveUrl( $oldimage )
224 );
225 wfPurgeSquidServers($urlArr);
226 }
227 $this->doDeleteOldImage( $oldimage );
228 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
229 $deleted = $oldimage;
230 } else {
231 if ( is_null ( $image ) ) {
232 $image = $this->mTitle->getDBkey();
233 }
234 $dest = wfImageDir( $image );
235 $archive = wfImageDir( $image );
236
237 # Delete the image file if it exists; due to sync problems
238 # or manual trimming sometimes the file will be missing.
239 $targetFile = "{$dest}/{$image}";
240 if( file_exists( $targetFile ) && ! @unlink( $targetFile ) ) {
241 # If the deletion operation actually failed, bug out:
242 $wgOut->fileDeleteError( $targetFile );
243 return;
244 }
245 $dbw->delete( 'image', array( 'img_name' => $image ) );
246 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
247
248 # Squid purging
249 if ( $wgUseSquid ) {
250 $urlArr = Array(
251 $wgInternalServer . Image::wfImageUrl( $image )
252 );
253 wfPurgeSquidServers($urlArr);
254 }
255
256
257 $urlArr = Array();
258 while ( $s = $dbw->fetchObject( $res ) ) {
259 $this->doDeleteOldImage( $s->oi_archive_name );
260 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name );
261 }
262
263 # Squid purging, part II
264 if ( $wgUseSquid ) {
265 /* this needs to be done after LinksUpdate */
266 $u = new SquidUpdate( $urlArr );
267 array_push( $wgDeferredUpdateList, $u );
268 }
269
270 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
271
272 # Image itself is now gone, and database is cleaned.
273 # Now we remove the image description page.
274
275 $nt = Title::newFromText( $wgContLang->getNsText( Namespace::getImage() ) . ":" . $image );
276 $article = new Article( $nt );
277 $article->doDeleteArticle( $reason ); # ignore errors
278
279 $deleted = $image;
280 }
281
282 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
283 $wgOut->setRobotpolicy( 'noindex,nofollow' );
284
285 $sk = $wgUser->getSkin();
286 $loglink = $sk->makeKnownLink( $wgContLang->getNsText(
287 Namespace::getWikipedia() ) .
288 ':' . wfMsg( 'dellogpage' ), wfMsg( 'deletionlog' ) );
289
290 $text = wfMsg( 'deletedtext', $deleted, $loglink );
291
292 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
293 $wgOut->returnToMain( false );
294 }
295
296 function doDeleteOldImage( $oldimage )
297 {
298 global $wgOut;
299
300 $name = substr( $oldimage, 15 );
301 $archive = wfImageArchiveDir( $name );
302
303 # Delete the image if it exists. Sometimes the file will be missing
304 # due to manual intervention or weird sync problems; treat that
305 # condition gracefully and continue to delete the database entry.
306 # Also some records may end up with an empty oi_archive_name field
307 # if the original file was missing when a new upload was made;
308 # don't try to delete the directory then!
309 #
310 $targetFile = "{$archive}/{$oldimage}";
311 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink( $targetFile ) ) {
312 # If we actually have a file and can't delete it, throw an error.
313 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
314 } else {
315 # Log the deletion
316 $log = new LogPage( 'delete' );
317 $log->addEntry( 'delete', $this->mTitle, wfMsg('deletedrevision',$oldimage) );
318 }
319 }
320
321 function revert()
322 {
323 global $wgOut, $wgRequest;
324 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
325
326 $oldimage = $wgRequest->getText( 'oldimage' );
327 if ( strlen( $oldimage ) < 16 ) {
328 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
329 return;
330 }
331 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
332 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
333 return;
334 }
335
336 if ( wfReadOnly() ) {
337 $wgOut->readOnlyPage();
338 return;
339 }
340 if ( ! $this->mTitle->userCanEdit() ) {
341 $wgOut->sysopRequired();
342 return;
343 }
344 $name = substr( $oldimage, 15 );
345
346 $dest = wfImageDir( $name );
347 $archive = wfImageArchiveDir( $name );
348 $curfile = "{$dest}/{$name}";
349
350 if ( ! is_file( $curfile ) ) {
351 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
352 return;
353 }
354 $oldver = wfTimestampNow() . "!{$name}";
355
356 $dbr =& wfGetDB( DB_SLAVE );
357 $size = $dbr->selectField( 'oldimage', 'oi_size', 'oi_archive_name=\'' .
358 $dbr->strencode( $oldimage ) . "'" );
359
360 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
361 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
362 return;
363 }
364 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
365 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
366 }
367 wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
368 # Squid purging
369 if ( $wgUseSquid ) {
370 $urlArr = Array(
371 $wgInternalServer.wfImageArchiveUrl( $name ),
372 $wgInternalServer . Image::wfImageUrl( $name )
373 );
374 wfPurgeSquidServers($urlArr);
375 }
376
377 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
378 $wgOut->setRobotpolicy( 'noindex,nofollow' );
379 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
380 $wgOut->returnToMain( false );
381 }
382 }
383
384 class ImageHistoryList {
385 function ImageHistoryList( &$skin ) {
386 $this->skin =& $skin;
387 }
388
389 function beginImageHistoryList() {
390 $s = "\n<h2>" . wfMsg( 'imghistory' ) . "</h2>\n" .
391 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
392 return $s;
393 }
394
395 function endImageHistoryList() {
396 $s = "</ul>\n";
397 return $s;
398 }
399
400 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
401 global $wgUser, $wgLang, $wgContLang, $wgTitle;
402
403 $datetime = $wgLang->timeanddate( $timestamp, true );
404 $del = wfMsg( 'deleteimg' );
405 $delall = wfMsg( 'deleteimgcompletely' );
406 $cur = wfMsg( 'cur' );
407
408 if ( $iscur ) {
409 $url = Image::wfImageUrl( $img );
410 $rlink = $cur;
411 if ( $wgUser->isAllowed('delete') ) {
412 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
413 '&action=delete' );
414 $style = $this->skin->getInternalLinkAttributes( $link, $delall );
415
416 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
417 } else {
418 $dlink = $del;
419 }
420 } else {
421 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
422 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
423 $rlink = $this->skin->makeKnownLink( $wgTitle->getPrefixedText(),
424 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
425 urlencode( $img ) );
426 $dlink = $this->skin->makeKnownLink( $wgTitle->getPrefixedText(),
427 $del, 'action=delete&oldimage=' . urlencode( $img ) );
428 } else {
429 # Having live active links for non-logged in users
430 # means that bots and spiders crawling our site can
431 # inadvertently change content. Baaaad idea.
432 $rlink = wfMsg( 'revertimg' );
433 $dlink = $del;
434 }
435 }
436 if ( 0 == $user ) {
437 $userlink = $usertext;
438 } else {
439 $userlink = $this->skin->makeLink( $wgContLang->getNsText( Namespace::getUser() ) .
440 ':'.$usertext, $usertext );
441 }
442 $nbytes = wfMsg( 'nbytes', $size );
443 $style = $this->skin->getInternalLinkAttributes( $url, $datetime );
444
445 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
446 . " . . {$userlink} ({$nbytes})";
447
448 if ( '' != $description && '*' != $description ) {
449 $sk=$wgUser->getSkin();
450 $s .= $wgContLang->emphasize(' (' . $sk->formatComment($description,$wgTitle) . ')');
451 }
452 $s .= "</li>\n";
453 return $s;
454 }
455
456 }
457
458
459 ?>