Removed dependence on register_globals from everything except the special pages....
[lhc/web/wiklou.git] / includes / ImagePage.php
1 <?php
2
3 /*
4 Special handling for image description pages
5 */
6
7 class ImagePage extends Article {
8
9 function view() {
10 if ( Namespace::getImage() == $this->mTitle->getNamespace() ) {
11 $this->openShowImage();
12 }
13
14 Article::view();
15
16 # If the article we've just shown is in the "Image" namespace,
17 # follow it with the history list and link list for the image
18 # it describes.
19
20 if ( Namespace::getImage() == $this->mTitle->getNamespace() ) {
21 $this->closeShowImage();
22 $this->imageHistory();
23 $this->imageLinks();
24 }
25 }
26
27 function openShowImage()
28 {
29 global $wgOut, $wgUser;
30 $name = $this->mTitle->getText();
31 $path = wfImagePath( $name );
32 $url = wfImageUrl( $name );
33
34 if ( file_exists( $path ) ) {
35 list($width, $height, $type, $attr) = getimagesize( $path );
36
37 $sk = $wgUser->getSkin();
38
39 if ( $type != "" ) {
40 # image
41 $s .= "<center><img src=\"{$url}\" width=\"{$width}\" height=\"{$height}\"></center>";
42 } else {
43 $s .= "<center>".$sk->makeMediaLink($name,"")."</center>";
44 }
45 $wgOut->addHTML( $s );
46 }
47 }
48
49 function closeShowImage()
50 {
51 # For overloading
52 }
53
54 # If the page we've just displayed is in the "Image" namespace,
55 # we follow it with an upload history of the image and its usage.
56
57 function imageHistory()
58 {
59 global $wgUser, $wgOut, $wgLang;
60 $fname = "Article::imageHistory";
61
62 $sql = "SELECT img_size,img_description,img_user," .
63 "img_user_text,img_timestamp FROM image WHERE " .
64 "img_name='" . wfStrencode( $this->mTitle->getDBkey() ) . "'";
65 $res = wfQuery( $sql, DB_READ, $fname );
66
67 if ( 0 == wfNumRows( $res ) ) { return; }
68
69 $sk = $wgUser->getSkin();
70 $s = $sk->beginImageHistoryList();
71
72 $line = wfFetchObject( $res );
73 $s .= $sk->imageHistoryLine( true, $line->img_timestamp,
74 $this->mTitle->getText(), $line->img_user,
75 $line->img_user_text, $line->img_size, $line->img_description );
76
77 $sql = "SELECT oi_size,oi_description,oi_user," .
78 "oi_user_text,oi_timestamp,oi_archive_name FROM oldimage WHERE " .
79 "oi_name='" . wfStrencode( $this->mTitle->getDBkey() ) . "' " .
80 "ORDER BY oi_timestamp DESC";
81 $res = wfQuery( $sql, DB_READ, $fname );
82
83 while ( $line = wfFetchObject( $res ) ) {
84 $s .= $sk->imageHistoryLine( false, $line->oi_timestamp,
85 $line->oi_archive_name, $line->oi_user,
86 $line->oi_user_text, $line->oi_size, $line->oi_description );
87 }
88 $s .= $sk->endImageHistoryList();
89 $wgOut->addHTML( $s );
90 }
91
92 function imageLinks()
93 {
94 global $wgUser, $wgOut;
95
96 $wgOut->addHTML( "<h2>" . wfMsg( "imagelinks" ) . "</h2>\n" );
97
98 $sql = "SELECT cur_namespace,cur_title FROM imagelinks,cur WHERE il_to='" .
99 wfStrencode( $this->mTitle->getDBkey() ) . "' AND il_from=cur_id";
100 $res = wfQuery( $sql, DB_READ, "Article::imageLinks" );
101
102 if ( 0 == wfNumRows( $res ) ) {
103 $wgOut->addHtml( "<p>" . wfMsg( "nolinkstoimage" ) . "\n" );
104 return;
105 }
106 $wgOut->addHTML( "<p>" . wfMsg( "linkstoimage" ) . "\n<ul>" );
107
108 $sk = $wgUser->getSkin();
109 while ( $s = wfFetchObject( $res ) ) {
110 $name = Title::MakeTitle( $s->cur_namespace, $s->cur_title );
111 $link = $sk->makeKnownLinkObj( $name, "" );
112 $wgOut->addHTML( "<li>{$link}</li>\n" );
113 }
114 $wgOut->addHTML( "</ul>\n" );
115 }
116
117 function delete()
118 {
119 global $wgUser, $wgOut, $wgRequest;
120
121 $confirm = $wgRequest->getBool( 'wpConfirm' );
122 $image = $wgRequest->getVal( 'image' );
123 $oldimage = $wgRequest->getVal( 'oldimage' );
124
125 # Only sysops can delete images. Previously ordinary users could delete
126 # old revisions, but this is no longer the case.
127 if ( !$wgUser->isSysop() ) {
128 $wgOut->sysopRequired();
129 return;
130 }
131 if ( wfReadOnly() ) {
132 $wgOut->readOnlyPage();
133 return;
134 }
135
136 # Better double-check that it hasn't been deleted yet!
137 $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
138 if ( !is_null( $image ) ) {
139 if ( "" == trim( $image ) ) {
140 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
141 return;
142 }
143 }
144
145 # Deleting old images doesn't require confirmation
146 if ( !is_null( $oldimage ) || $confirm ) {
147 $this->doDelete();
148 return;
149 }
150
151 if ( !is_null( $image ) ) {
152 $q = "&image={$image}";
153 } else if ( !is_null( $oldimage ) ) {
154 $q = "&oldimage={$oldimage}";
155 }
156 return $this->confirmDelete( $q );
157 }
158
159 function doDelete()
160 {
161 global $wgOut, $wgUser, $wgLang, $wgRequest;
162 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
163 $fname = "Article::doDelete";
164
165 $reason = $wgRequest->getVal( 'wpReason' );
166 $image = $wgRequest->getVal( 'image' );
167 $oldimage = $wgRequest->getVal( 'oldimage' );
168
169 if ( !is_null( $image ) ) {
170 $dest = wfImageDir( $image );
171 $archive = wfImageDir( $image );
172 if ( ! unlink( "{$dest}/{$image}" ) ) {
173 $wgOut->fileDeleteError( "{$dest}/{$image}" );
174 return;
175 }
176 $sql = "DELETE FROM image WHERE img_name='" .
177 wfStrencode( $image ) . "'";
178 wfQuery( $sql, DB_WRITE, $fname );
179
180 $sql = "SELECT oi_archive_name FROM oldimage WHERE oi_name='" .
181 wfStrencode( $image ) . "'";
182 $res = wfQuery( $sql, DB_READ, $fname );
183
184 # Squid purging
185 if ( $wgUseSquid ) {
186 $urlArr = Array(
187 $wgInternalServer.wfImageUrl( $image )
188 );
189 wfPurgeSquidServers($urlArr);
190 }
191
192
193 $urlArr = Array();
194 while ( $s = wfFetchObject( $res ) ) {
195 $this->doDeleteOldImage( $s->oi_archive_name );
196 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name );
197 }
198
199 # Squid purging, part II
200 if ( $wgUseSquid ) {
201 /* this needs to be done after LinksUpdate */
202 $u = new SquidUpdate( $urlArr );
203 array_push( $wgDeferredUpdateList, $u );
204 }
205
206 $sql = "DELETE FROM oldimage WHERE oi_name='" .
207 wfStrencode( $image ) . "'";
208 wfQuery( $sql, DB_WRITE, $fname );
209
210 # Image itself is now gone, and database is cleaned.
211 # Now we remove the image description page.
212
213 $nt = Title::newFromText( $wgLang->getNsText( Namespace::getImage() ) . ":" . $image );
214 $article = new Article( $nt );
215 $article->doDeleteArticle( $reason ); # ignore errors
216
217 $deleted = $image;
218 } else if ( !is_null( $oldimage ) ) {
219 # Squid purging
220 if ( $wgUseSquid ) {
221 $urlArr = Array(
222 $wgInternalServer.wfImageArchiveUrl( $oldimage )
223 );
224 wfPurgeSquidServers($urlArr);
225 }
226 $this->doDeleteOldImage( $oldimage );
227 $sql = "DELETE FROM oldimage WHERE oi_archive_name='" .
228 wfStrencode( $oldimage ) . "'";
229 wfQuery( $sql, DB_WRITE, $fname );
230
231 $deleted = $oldimage;
232 } else {
233 $this->doDeleteArticle( $reason ); # ignore errors
234 $deleted = $this->mTitle->getPrefixedText();
235 }
236 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
237 $wgOut->setRobotpolicy( "noindex,nofollow" );
238
239 $sk = $wgUser->getSkin();
240 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
241 Namespace::getWikipedia() ) .
242 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
243
244 $text = wfMsg( "deletedtext", $deleted, $loglink );
245
246 $wgOut->addHTML( "<p>" . $text );
247 $wgOut->returnToMain( false );
248 }
249
250 function doDeleteOldImage( $oldimage )
251 {
252 global $wgOut;
253
254 $name = substr( $oldimage, 15 );
255 $archive = wfImageArchiveDir( $name );
256 if ( ! unlink( "{$archive}/{$oldimage}" ) ) {
257 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
258 }
259 }
260
261 function revert()
262 {
263 global $wgOut, $wgRequest;
264 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
265
266 $oldimage = $wgRequest->getText( 'oldimage' );
267
268 if ( strlen( $oldimage ) < 16 ) {
269 $wgOut->unexpectedValueError( "oldimage", $oldimage );
270 return;
271 }
272 if ( wfReadOnly() ) {
273 $wgOut->readOnlyPage();
274 return;
275 }
276 $name = substr( $oldimage, 15 );
277
278 $dest = wfImageDir( $name );
279 $archive = wfImageArchiveDir( $name );
280 $curfile = "{$dest}/{$name}";
281
282 if ( ! is_file( $curfile ) ) {
283 $wgOut->fileNotFoundError( $curfile );
284 return;
285 }
286 $oldver = wfTimestampNow() . "!{$name}";
287 $size = wfGetSQL( "oldimage", "oi_size", "oi_archive_name='" .
288 wfStrencode( $oldimage ) . "'" );
289
290 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
291 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
292 return;
293 }
294 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
295 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
296 }
297 wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
298 # Squid purging
299 if ( $wgUseSquid ) {
300 $urlArr = Array(
301 $wgInternalServer.wfImageArchiveUrl( $name ),
302 $wgInternalServer.wfImageUrl( $name )
303 );
304 wfPurgeSquidServers($urlArr);
305 }
306
307 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
308 $wgOut->setRobotpolicy( "noindex,nofollow" );
309 $wgOut->addHTML( wfMsg( "imagereverted" ) );
310 $wgOut->returnToMain( false );
311 }
312 }
313
314 ?>