Stop loading feed.php every time by moving available feeds classes in defines.php...
[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->getUrl();
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, $wgLang, $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 if ( ! @unlink( "{$dest}/{$image}" ) ) {
223 $wgOut->fileDeleteError( "{$dest}/{$image}" );
224 return;
225 }
226 $dbw->delete( 'image', array( 'img_name' => $image ) );
227 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
228
229 # Squid purging
230 if ( $wgUseSquid ) {
231 $urlArr = Array(
232 $wgInternalServer . Image::wfImageUrl( $image )
233 );
234 wfPurgeSquidServers($urlArr);
235 }
236
237
238 $urlArr = Array();
239 while ( $s = $dbw->fetchObject( $res ) ) {
240 $this->doDeleteOldImage( $s->oi_archive_name );
241 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name );
242 }
243
244 # Squid purging, part II
245 if ( $wgUseSquid ) {
246 /* this needs to be done after LinksUpdate */
247 $u = new SquidUpdate( $urlArr );
248 array_push( $wgDeferredUpdateList, $u );
249 }
250
251 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
252
253 # Image itself is now gone, and database is cleaned.
254 # Now we remove the image description page.
255
256 $nt = Title::newFromText( $wgLang->getNsText( Namespace::getImage() ) . ":" . $image );
257 $article = new Article( $nt );
258 $article->doDeleteArticle( $reason ); # ignore errors
259
260 $deleted = $image;
261 }
262
263 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
264 $wgOut->setRobotpolicy( 'noindex,nofollow' );
265
266 $sk = $wgUser->getSkin();
267 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
268 Namespace::getWikipedia() ) .
269 ':' . wfMsg( 'dellogpage' ), wfMsg( 'deletionlog' ) );
270
271 $text = wfMsg( 'deletedtext', $deleted, $loglink );
272
273 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
274 $wgOut->returnToMain( false );
275 }
276
277 function doDeleteOldImage( $oldimage )
278 {
279 global $wgOut;
280
281 $name = substr( $oldimage, 15 );
282 $archive = wfImageArchiveDir( $name );
283 if ( ! @unlink( "{$archive}/{$oldimage}" ) ) {
284 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
285 } else {
286 # Log the deletion
287 $log = new LogPage( 'delete' );
288 $log->addEntry( 'delete', $this->mTitle, wfMsg('deletedrevision',$oldimage) );
289 }
290 }
291
292 function revert()
293 {
294 global $wgOut, $wgRequest;
295 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
296
297 $oldimage = $wgRequest->getText( 'oldimage' );
298
299 if ( strlen( $oldimage ) < 16 ) {
300 $wgOut->unexpectedValueError( 'oldimage', $oldimage );
301 return;
302 }
303 if ( wfReadOnly() ) {
304 $wgOut->readOnlyPage();
305 return;
306 }
307 if ( ! $this->mTitle->userCanEdit() ) {
308 $wgOut->sysopRequired();
309 return;
310 }
311 $name = substr( $oldimage, 15 );
312
313 $dest = wfImageDir( $name );
314 $archive = wfImageArchiveDir( $name );
315 $curfile = "{$dest}/{$name}";
316
317 if ( ! is_file( $curfile ) ) {
318 $wgOut->fileNotFoundError( $curfile );
319 return;
320 }
321 $oldver = wfTimestampNow() . "!{$name}";
322
323 $dbr =& wfGetDB( DB_SLAVE );
324 $size = $dbr->getField( 'oldimage', 'oi_size', 'oi_archive_name=\'' .
325 $dbr->strencode( $oldimage ) . "'" );
326
327 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
328 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
329 return;
330 }
331 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
332 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
333 }
334 wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
335 # Squid purging
336 if ( $wgUseSquid ) {
337 $urlArr = Array(
338 $wgInternalServer.wfImageArchiveUrl( $name ),
339 $wgInternalServer . Image::wfImageUrl( $name )
340 );
341 wfPurgeSquidServers($urlArr);
342 }
343
344 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
345 $wgOut->setRobotpolicy( 'noindex,nofollow' );
346 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
347 $wgOut->returnToMain( false );
348 }
349 }
350
351
352 ?>