Clean up a few scriptlets
[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 $wgRequest->getVal( 'image' )."\" />" . $anchorclose . "</div>";
80 } else {
81 $s = "<div class=\"fullMedia\">".$sk->makeMediaLink($this->img->getName(),"")."</div>";
82 }
83 $wgOut->addHTML( $s );
84 }
85 }
86
87 function closeShowImage()
88 {
89 # For overloading
90 }
91
92 /**
93 * If the page we've just displayed is in the "Image" namespace,
94 * we follow it with an upload history of the image and its usage.
95 */
96 function imageHistory()
97 {
98 global $wgUser, $wgOut;
99
100 $sk = $wgUser->getSkin();
101
102 $line = $this->img->nextHistoryLine();
103
104 if ( $line ) {
105 $s = $sk->beginImageHistoryList() .
106 $sk->imageHistoryLine( true, $line->img_timestamp,
107 $this->mTitle->getDBkey(), $line->img_user,
108 $line->img_user_text, $line->img_size, $line->img_description );
109
110 while ( $line = $this->img->nextHistoryLine() ) {
111 $s .= $sk->imageHistoryLine( false, $line->img_timestamp,
112 $line->oi_archive_name, $line->img_user,
113 $line->img_user_text, $line->img_size, $line->img_description );
114 }
115 $s .= $sk->endImageHistoryList();
116 } else { $s=''; }
117 $wgOut->addHTML( $s );
118 }
119
120 function imageLinks()
121 {
122 global $wgUser, $wgOut;
123
124 $wgOut->addHTML( '<h2>' . wfMsg( 'imagelinks' ) . "</h2>\n" );
125
126 $dbr =& wfGetDB( DB_SLAVE );
127 $cur = $dbr->tableName( 'cur' );
128 $imagelinks = $dbr->tableName( 'imagelinks' );
129
130 $sql = "SELECT cur_namespace,cur_title FROM $imagelinks,$cur WHERE il_to=" .
131 $dbr->addQuotes( $this->mTitle->getDBkey() ) . " AND il_from=cur_id";
132 $res = $dbr->query( $sql, DB_SLAVE, "Article::imageLinks" );
133
134 if ( 0 == $dbr->numRows( $res ) ) {
135 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
136 return;
137 }
138 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
139
140 $sk = $wgUser->getSkin();
141 while ( $s = $dbr->fetchObject( $res ) ) {
142 $name = Title::MakeTitle( $s->cur_namespace, $s->cur_title );
143 $link = $sk->makeKnownLinkObj( $name, "" );
144 $wgOut->addHTML( "<li>{$link}</li>\n" );
145 }
146 $wgOut->addHTML( "</ul>\n" );
147 }
148
149 function delete()
150 {
151 global $wgUser, $wgOut, $wgRequest;
152
153 $confirm = $wgRequest->getBool( 'wpConfirm' );
154 $image = $wgRequest->getVal( 'image' );
155 $oldimage = $wgRequest->getVal( 'oldimage' );
156
157 # Only sysops can delete images. Previously ordinary users could delete
158 # old revisions, but this is no longer the case.
159 if ( !$wgUser->isSysop() ) {
160 $wgOut->sysopRequired();
161 return;
162 }
163 if ( wfReadOnly() ) {
164 $wgOut->readOnlyPage();
165 return;
166 }
167
168 # Better double-check that it hasn't been deleted yet!
169 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
170 if ( !is_null( $image ) ) {
171 if ( '' == trim( $image ) ) {
172 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
173 return;
174 }
175 }
176
177 # Deleting old images doesn't require confirmation
178 if ( !is_null( $oldimage ) || $confirm ) {
179 $this->doDelete();
180 return;
181 }
182
183 if ( !is_null( $image ) ) {
184 $q = '&image=' . urlencode( $image );
185 } else if ( !is_null( $oldimage ) ) {
186 $q = '&oldimage=' . urlencode( $oldimage );
187 } else {
188 $q = '';
189 }
190 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
191 }
192
193 function doDelete()
194 {
195 global $wgOut, $wgUser, $wgContLang, $wgRequest;
196 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
197 $fname = 'ImagePage::doDelete';
198
199 $reason = $wgRequest->getVal( 'wpReason' );
200 $image = $wgRequest->getVal( 'image' );
201 $oldimage = $wgRequest->getVal( 'oldimage' );
202
203 $dbw =& wfGetDB( DB_MASTER );
204
205 if ( !is_null( $oldimage ) ) {
206 # Squid purging
207 if ( $wgUseSquid ) {
208 $urlArr = Array(
209 $wgInternalServer.wfImageArchiveUrl( $oldimage )
210 );
211 wfPurgeSquidServers($urlArr);
212 }
213 $this->doDeleteOldImage( $oldimage );
214 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
215 $deleted = $oldimage;
216 } else {
217 if ( is_null ( $image ) ) {
218 $image = $this->mTitle->getDBkey();
219 }
220 $dest = wfImageDir( $image );
221 $archive = wfImageDir( $image );
222
223 # Delete the image file if it exists; due to sync problems
224 # or manual trimming sometimes the file will be missing.
225 $targetFile = "{$dest}/{$image}";
226 if( file_exists( $targetFile ) && ! @unlink( $targetFile ) ) {
227 # If the deletion operation actually failed, bug out:
228 $wgOut->fileDeleteError( $targetFile );
229 return;
230 }
231 $dbw->delete( 'image', array( 'img_name' => $image ) );
232 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
233
234 # Squid purging
235 if ( $wgUseSquid ) {
236 $urlArr = Array(
237 $wgInternalServer . Image::wfImageUrl( $image )
238 );
239 wfPurgeSquidServers($urlArr);
240 }
241
242
243 $urlArr = Array();
244 while ( $s = $dbw->fetchObject( $res ) ) {
245 $this->doDeleteOldImage( $s->oi_archive_name );
246 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name );
247 }
248
249 # Squid purging, part II
250 if ( $wgUseSquid ) {
251 /* this needs to be done after LinksUpdate */
252 $u = new SquidUpdate( $urlArr );
253 array_push( $wgDeferredUpdateList, $u );
254 }
255
256 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
257
258 # Image itself is now gone, and database is cleaned.
259 # Now we remove the image description page.
260
261 $nt = Title::newFromText( $wgContLang->getNsText( Namespace::getImage() ) . ":" . $image );
262 $article = new Article( $nt );
263 $article->doDeleteArticle( $reason ); # ignore errors
264
265 $deleted = $image;
266 }
267
268 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
269 $wgOut->setRobotpolicy( 'noindex,nofollow' );
270
271 $sk = $wgUser->getSkin();
272 $loglink = $sk->makeKnownLink( $wgContLang->getNsText(
273 Namespace::getWikipedia() ) .
274 ':' . wfMsg( 'dellogpage' ), wfMsg( 'deletionlog' ) );
275
276 $text = wfMsg( 'deletedtext', $deleted, $loglink );
277
278 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
279 $wgOut->returnToMain( false );
280 }
281
282 function doDeleteOldImage( $oldimage )
283 {
284 global $wgOut;
285
286 $name = substr( $oldimage, 15 );
287 $archive = wfImageArchiveDir( $name );
288
289 # Delete the image if it exists. Sometimes the file will be missing
290 # due to manual intervention or weird sync problems; treat that
291 # condition gracefully and continue to delete the database entry.
292 # Also some records may end up with an empty oi_archive_name field
293 # if the original file was missing when a new upload was made;
294 # don't try to delete the directory then!
295 #
296 $targetFile = "{$archive}/{$oldimage}";
297 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink( $targetFile ) ) {
298 # If we actually have a file and can't delete it, throw an error.
299 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
300 } else {
301 # Log the deletion
302 $log = new LogPage( 'delete' );
303 $log->addEntry( 'delete', $this->mTitle, wfMsg('deletedrevision',$oldimage) );
304 }
305 }
306
307 function revert()
308 {
309 global $wgOut, $wgRequest;
310 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
311
312 $oldimage = $wgRequest->getText( 'oldimage' );
313
314 if ( strlen( $oldimage ) < 16 ) {
315 $wgOut->unexpectedValueError( 'oldimage', $oldimage );
316 return;
317 }
318 if ( wfReadOnly() ) {
319 $wgOut->readOnlyPage();
320 return;
321 }
322 if ( ! $this->mTitle->userCanEdit() ) {
323 $wgOut->sysopRequired();
324 return;
325 }
326 $name = substr( $oldimage, 15 );
327
328 $dest = wfImageDir( $name );
329 $archive = wfImageArchiveDir( $name );
330 $curfile = "{$dest}/{$name}";
331
332 if ( ! is_file( $curfile ) ) {
333 $wgOut->fileNotFoundError( $curfile );
334 return;
335 }
336 $oldver = wfTimestampNow() . "!{$name}";
337
338 $dbr =& wfGetDB( DB_SLAVE );
339 $size = $dbr->getField( 'oldimage', 'oi_size', 'oi_archive_name=\'' .
340 $dbr->strencode( $oldimage ) . "'" );
341
342 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
343 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
344 return;
345 }
346 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
347 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
348 }
349 wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
350 # Squid purging
351 if ( $wgUseSquid ) {
352 $urlArr = Array(
353 $wgInternalServer.wfImageArchiveUrl( $name ),
354 $wgInternalServer . Image::wfImageUrl( $name )
355 );
356 wfPurgeSquidServers($urlArr);
357 }
358
359 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
360 $wgOut->setRobotpolicy( 'noindex,nofollow' );
361 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
362 $wgOut->returnToMain( false );
363 }
364 }
365
366
367 ?>