Implement #1206
[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 $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 = "<br />{$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 $this->doDelete();
216 return;
217 }
218
219 if ( !is_null( $image ) ) {
220 $q = '&image=' . urlencode( $image );
221 } else if ( !is_null( $oldimage ) ) {
222 $q = '&oldimage=' . urlencode( $oldimage );
223 } else {
224 $q = '';
225 }
226 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
227 }
228
229 function doDelete()
230 {
231 global $wgOut, $wgUser, $wgContLang, $wgRequest;
232 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
233 $fname = 'ImagePage::doDelete';
234
235 $reason = $wgRequest->getVal( 'wpReason' );
236 $image = $wgRequest->getVal( 'image' );
237 $oldimage = $wgRequest->getVal( 'oldimage' );
238
239 $dbw =& wfGetDB( DB_MASTER );
240
241 if ( !is_null( $oldimage ) ) {
242 # Squid purging
243 if ( $wgUseSquid ) {
244 $urlArr = Array(
245 $wgInternalServer.wfImageArchiveUrl( $oldimage )
246 );
247 wfPurgeSquidServers($urlArr);
248 }
249 $this->doDeleteOldImage( $oldimage );
250 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
251 $deleted = $oldimage;
252 } else {
253 if ( is_null ( $image ) ) {
254 $image = $this->mTitle->getDBkey();
255 }
256 $dest = wfImageDir( $image );
257 $archive = wfImageDir( $image );
258
259 # Delete the image file if it exists; due to sync problems
260 # or manual trimming sometimes the file will be missing.
261 $targetFile = "{$dest}/{$image}";
262 if( file_exists( $targetFile ) && ! @unlink( $targetFile ) ) {
263 # If the deletion operation actually failed, bug out:
264 $wgOut->fileDeleteError( $targetFile );
265 return;
266 }
267 $dbw->delete( 'image', array( 'img_name' => $image ) );
268 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
269
270 # Squid purging
271 if ( $wgUseSquid ) {
272 $urlArr = Array(
273 $wgInternalServer . Image::wfImageUrl( $image )
274 );
275 wfPurgeSquidServers($urlArr);
276 }
277
278
279 $urlArr = Array();
280 while ( $s = $dbw->fetchObject( $res ) ) {
281 $this->doDeleteOldImage( $s->oi_archive_name );
282 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name );
283 }
284
285 # Squid purging, part II
286 if ( $wgUseSquid ) {
287 /* this needs to be done after LinksUpdate */
288 $u = new SquidUpdate( $urlArr );
289 array_push( $wgDeferredUpdateList, $u );
290 }
291
292 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
293
294 # Image itself is now gone, and database is cleaned.
295 # Now we remove the image description page.
296
297 $nt = Title::newFromText( $wgContLang->getNsText( Namespace::getImage() ) . ":" . $image );
298 $article = new Article( $nt );
299 $article->doDeleteArticle( $reason ); # ignore errors
300
301 $deleted = $image;
302 }
303
304 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
305 $wgOut->setRobotpolicy( 'noindex,nofollow' );
306
307 $sk = $wgUser->getSkin();
308 $loglink = $sk->makeKnownLink( $wgContLang->getNsText(
309 Namespace::getWikipedia() ) .
310 ':' . wfMsg( 'dellogpage' ), wfMsg( 'deletionlog' ) );
311
312 $text = wfMsg( 'deletedtext', $deleted, $loglink );
313
314 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
315 $wgOut->returnToMain( false );
316 }
317
318 function doDeleteOldImage( $oldimage )
319 {
320 global $wgOut;
321
322 $name = substr( $oldimage, 15 );
323 $archive = wfImageArchiveDir( $name );
324
325 # Delete the image if it exists. Sometimes the file will be missing
326 # due to manual intervention or weird sync problems; treat that
327 # condition gracefully and continue to delete the database entry.
328 # Also some records may end up with an empty oi_archive_name field
329 # if the original file was missing when a new upload was made;
330 # don't try to delete the directory then!
331 #
332 $targetFile = "{$archive}/{$oldimage}";
333 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink( $targetFile ) ) {
334 # If we actually have a file and can't delete it, throw an error.
335 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
336 } else {
337 # Log the deletion
338 $log = new LogPage( 'delete' );
339 $log->addEntry( 'delete', $this->mTitle, wfMsg('deletedrevision',$oldimage) );
340 }
341 }
342
343 function revert()
344 {
345 global $wgOut, $wgRequest;
346 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
347
348 $oldimage = $wgRequest->getText( 'oldimage' );
349 if ( strlen( $oldimage ) < 16 ) {
350 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
351 return;
352 }
353 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
354 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
355 return;
356 }
357
358 if ( wfReadOnly() ) {
359 $wgOut->readOnlyPage();
360 return;
361 }
362 if ( ! $this->mTitle->userCanEdit() ) {
363 $wgOut->sysopRequired();
364 return;
365 }
366 $name = substr( $oldimage, 15 );
367
368 $dest = wfImageDir( $name );
369 $archive = wfImageArchiveDir( $name );
370 $curfile = "{$dest}/{$name}";
371
372 if ( ! is_file( $curfile ) ) {
373 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
374 return;
375 }
376 $oldver = wfTimestampNow() . "!{$name}";
377
378 $dbr =& wfGetDB( DB_SLAVE );
379 $size = $dbr->selectField( 'oldimage', 'oi_size', 'oi_archive_name=\'' .
380 $dbr->strencode( $oldimage ) . "'" );
381
382 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
383 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
384 return;
385 }
386 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
387 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
388 }
389 wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
390 # Squid purging
391 if ( $wgUseSquid ) {
392 $urlArr = Array(
393 $wgInternalServer.wfImageArchiveUrl( $name ),
394 $wgInternalServer . Image::wfImageUrl( $name )
395 );
396 wfPurgeSquidServers($urlArr);
397 }
398
399 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
400 $wgOut->setRobotpolicy( 'noindex,nofollow' );
401 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
402 $wgOut->returnToMain( false );
403 }
404 }
405
406 class ImageHistoryList {
407 function ImageHistoryList( &$skin ) {
408 $this->skin =& $skin;
409 }
410
411 function beginImageHistoryList() {
412 $s = "\n<h2>" . wfMsg( 'imghistory' ) . "</h2>\n" .
413 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
414 return $s;
415 }
416
417 function endImageHistoryList() {
418 $s = "</ul>\n";
419 return $s;
420 }
421
422 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
423 global $wgUser, $wgLang, $wgContLang, $wgTitle;
424
425 $datetime = $wgLang->timeanddate( $timestamp, true );
426 $del = wfMsg( 'deleteimg' );
427 $delall = wfMsg( 'deleteimgcompletely' );
428 $cur = wfMsg( 'cur' );
429
430 if ( $iscur ) {
431 $url = Image::wfImageUrl( $img );
432 $rlink = $cur;
433 if ( $wgUser->isAllowed('delete') ) {
434 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
435 '&action=delete' );
436 $style = $this->skin->getInternalLinkAttributes( $link, $delall );
437
438 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
439 } else {
440 $dlink = $del;
441 }
442 } else {
443 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
444 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
445 $rlink = $this->skin->makeKnownLink( $wgTitle->getPrefixedText(),
446 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
447 urlencode( $img ) );
448 $dlink = $this->skin->makeKnownLink( $wgTitle->getPrefixedText(),
449 $del, 'action=delete&oldimage=' . urlencode( $img ) );
450 } else {
451 # Having live active links for non-logged in users
452 # means that bots and spiders crawling our site can
453 # inadvertently change content. Baaaad idea.
454 $rlink = wfMsg( 'revertimg' );
455 $dlink = $del;
456 }
457 }
458 if ( 0 == $user ) {
459 $userlink = $usertext;
460 } else {
461 $userlink = $this->skin->makeLink( $wgContLang->getNsText( Namespace::getUser() ) .
462 ':'.$usertext, $usertext );
463 }
464 $nbytes = wfMsg( 'nbytes', $size );
465 $style = $this->skin->getInternalLinkAttributes( $url, $datetime );
466
467 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
468 . " . . {$userlink} ({$nbytes})";
469
470 if ( '' != $description && '*' != $description ) {
471 $sk=$wgUser->getSkin();
472 $s .= $wgContLang->emphasize(' (' . $sk->formatComment($description,$wgTitle) . ')');
473 }
474 $s .= "</li>\n";
475 return $s;
476 }
477
478 }
479
480
481 ?>