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