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