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