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