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