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