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