Forgot to add this file.
[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 } else {
156 $q = "";
157 }
158 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
159 }
160
161 function doDelete()
162 {
163 global $wgOut, $wgUser, $wgLang, $wgRequest;
164 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
165 $fname = "Article::doDelete";
166
167 $reason = $wgRequest->getVal( 'wpReason' );
168 $image = $wgRequest->getVal( 'image' );
169 $oldimage = $wgRequest->getVal( 'oldimage' );
170
171 if ( !is_null( $image ) ) {
172 $dest = wfImageDir( $image );
173 $archive = wfImageDir( $image );
174 if ( ! @unlink( "{$dest}/{$image}" ) ) {
175 $wgOut->fileDeleteError( "{$dest}/{$image}" );
176 return;
177 }
178 $sql = "DELETE FROM image WHERE img_name='" .
179 wfStrencode( $image ) . "'";
180 wfQuery( $sql, DB_WRITE, $fname );
181
182 $sql = "SELECT oi_archive_name FROM oldimage WHERE oi_name='" .
183 wfStrencode( $image ) . "'";
184 $res = wfQuery( $sql, DB_READ, $fname );
185
186 # Squid purging
187 if ( $wgUseSquid ) {
188 $urlArr = Array(
189 $wgInternalServer.wfImageUrl( $image )
190 );
191 wfPurgeSquidServers($urlArr);
192 }
193
194
195 $urlArr = Array();
196 while ( $s = wfFetchObject( $res ) ) {
197 $this->doDeleteOldImage( $s->oi_archive_name );
198 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name );
199 }
200
201 # Squid purging, part II
202 if ( $wgUseSquid ) {
203 /* this needs to be done after LinksUpdate */
204 $u = new SquidUpdate( $urlArr );
205 array_push( $wgDeferredUpdateList, $u );
206 }
207
208 $sql = "DELETE FROM oldimage WHERE oi_name='" .
209 wfStrencode( $image ) . "'";
210 wfQuery( $sql, DB_WRITE, $fname );
211
212 # Image itself is now gone, and database is cleaned.
213 # Now we remove the image description page.
214
215 $nt = Title::newFromText( $wgLang->getNsText( Namespace::getImage() ) . ":" . $image );
216 $article = new Article( $nt );
217 $article->doDeleteArticle( $reason ); # ignore errors
218
219 $deleted = $image;
220 } else if ( !is_null( $oldimage ) ) {
221 # Squid purging
222 if ( $wgUseSquid ) {
223 $urlArr = Array(
224 $wgInternalServer.wfImageArchiveUrl( $oldimage )
225 );
226 wfPurgeSquidServers($urlArr);
227 }
228 $this->doDeleteOldImage( $oldimage );
229 $sql = "DELETE FROM oldimage WHERE oi_archive_name='" .
230 wfStrencode( $oldimage ) . "'";
231 wfQuery( $sql, DB_WRITE, $fname );
232
233 $deleted = $oldimage;
234 } else {
235 $this->doDeleteArticle( $reason ); # ignore errors
236 $deleted = $this->mTitle->getPrefixedText();
237 }
238 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
239 $wgOut->setRobotpolicy( "noindex,nofollow" );
240
241 $sk = $wgUser->getSkin();
242 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
243 Namespace::getWikipedia() ) .
244 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
245
246 $text = wfMsg( "deletedtext", $deleted, $loglink );
247
248 $wgOut->addHTML( "<p>" . $text );
249 $wgOut->returnToMain( false );
250 }
251
252 function doDeleteOldImage( $oldimage )
253 {
254 global $wgOut;
255
256 $name = substr( $oldimage, 15 );
257 $archive = wfImageArchiveDir( $name );
258 if ( ! unlink( "{$archive}/{$oldimage}" ) ) {
259 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
260 }
261 }
262
263 function revert()
264 {
265 global $wgOut, $wgRequest;
266 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
267
268 $oldimage = $wgRequest->getText( 'oldimage' );
269
270 if ( strlen( $oldimage ) < 16 ) {
271 $wgOut->unexpectedValueError( "oldimage", $oldimage );
272 return;
273 }
274 if ( wfReadOnly() ) {
275 $wgOut->readOnlyPage();
276 return;
277 }
278 $name = substr( $oldimage, 15 );
279
280 $dest = wfImageDir( $name );
281 $archive = wfImageArchiveDir( $name );
282 $curfile = "{$dest}/{$name}";
283
284 if ( ! is_file( $curfile ) ) {
285 $wgOut->fileNotFoundError( $curfile );
286 return;
287 }
288 $oldver = wfTimestampNow() . "!{$name}";
289 $size = wfGetSQL( "oldimage", "oi_size", "oi_archive_name='" .
290 wfStrencode( $oldimage ) . "'" );
291
292 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
293 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
294 return;
295 }
296 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
297 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
298 }
299 wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
300 # Squid purging
301 if ( $wgUseSquid ) {
302 $urlArr = Array(
303 $wgInternalServer.wfImageArchiveUrl( $name ),
304 $wgInternalServer.wfImageUrl( $name )
305 );
306 wfPurgeSquidServers($urlArr);
307 }
308
309 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
310 $wgOut->setRobotpolicy( "noindex,nofollow" );
311 $wgOut->addHTML( wfMsg( "imagereverted" ) );
312 $wgOut->returnToMain( false );
313 }
314 }
315
316 ?>