revert unintended replacement of wiki.png
[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;
120 global $wpConfirm, $wpReason, $image, $oldimage;
121
122 # Only sysops can delete images. Previously ordinary users could delete
123 # old revisions, but this is no longer the case.
124 if ( !$wgUser->isSysop() ) {
125 $wgOut->sysopRequired();
126 return;
127 }
128 if ( wfReadOnly() ) {
129 $wgOut->readOnlyPage();
130 return;
131 }
132
133 # Better double-check that it hasn't been deleted yet!
134 $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
135 if ( $image ) {
136 if ( "" == trim( $image ) ) {
137 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
138 return;
139 }
140 }
141
142 # Deleting old images doesn't require confirmation
143 if ( $oldimage || 1 == $wpConfirm ) {
144 $this->doDelete();
145 return;
146 }
147
148 if ( $image ) {
149 $q = "&image={$image}";
150 } else if ( $oldimage ) {
151 $q = "&oldimage={$oldimage}";
152 }
153 return $this->confirmDelete( $q );
154 }
155
156 function doDelete()
157 {
158 global $wgOut, $wgUser, $wgLang;
159 global $image, $oldimage, $wpReason;
160 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
161 $fname = "Article::doDelete";
162
163 if ( $image ) {
164 $dest = wfImageDir( $image );
165 $archive = wfImageDir( $image );
166 if ( ! unlink( "{$dest}/{$image}" ) ) {
167 $wgOut->fileDeleteError( "{$dest}/{$image}" );
168 return;
169 }
170 $sql = "DELETE FROM image WHERE img_name='" .
171 wfStrencode( $image ) . "'";
172 wfQuery( $sql, DB_WRITE, $fname );
173
174 $sql = "SELECT oi_archive_name FROM oldimage WHERE oi_name='" .
175 wfStrencode( $image ) . "'";
176 $res = wfQuery( $sql, DB_READ, $fname );
177
178 # Squid purging
179 if ( $wgUseSquid ) {
180 $urlArr = Array(
181 $wgInternalServer.wfImageUrl( $image )
182 );
183 wfPurgeSquidServers($urlArr);
184 }
185
186
187 $urlArr = Array();
188 while ( $s = wfFetchObject( $res ) ) {
189 $this->doDeleteOldImage( $s->oi_archive_name );
190 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name );
191 }
192
193 # Squid purging, part II
194 if ( $wgUseSquid ) {
195 /* this needs to be done after LinksUpdate */
196 $u = new SquidUpdate( $urlArr );
197 array_push( $wgDeferredUpdateList, $u );
198 }
199
200 $sql = "DELETE FROM oldimage WHERE oi_name='" .
201 wfStrencode( $image ) . "'";
202 wfQuery( $sql, DB_WRITE, $fname );
203
204 # Image itself is now gone, and database is cleaned.
205 # Now we remove the image description page.
206
207 $nt = Title::newFromText( $wgLang->getNsText( Namespace::getImage() ) . ":" . $image );
208 $article = new Article( $nt );
209 $article->doDeleteArticle(); # ignore errors
210
211 $deleted = $image;
212 } else if ( $oldimage ) {
213 # Squid purging
214 if ( $wgUseSquid ) {
215 $urlArr = Array(
216 $wgInternalServer.wfImageArchiveUrl( $oldimage )
217 );
218 wfPurgeSquidServers($urlArr);
219 }
220 $this->doDeleteOldImage( $oldimage );
221 $sql = "DELETE FROM oldimage WHERE oi_archive_name='" .
222 wfStrencode( $oldimage ) . "'";
223 wfQuery( $sql, DB_WRITE, $fname );
224
225 $deleted = $oldimage;
226 } else {
227 $this->doDeleteArticle(); # ignore errors
228 $deleted = $this->mTitle->getPrefixedText();
229 }
230 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
231 $wgOut->setRobotpolicy( "noindex,nofollow" );
232
233 $sk = $wgUser->getSkin();
234 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
235 Namespace::getWikipedia() ) .
236 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
237
238 $text = wfMsg( "deletedtext", $deleted, $loglink );
239
240 $wgOut->addHTML( "<p>" . $text );
241 $wgOut->returnToMain( false );
242 }
243
244 function doDeleteOldImage( $oldimage )
245 {
246 global $wgOut;
247
248 $name = substr( $oldimage, 15 );
249 $archive = wfImageArchiveDir( $name );
250 if ( ! unlink( "{$archive}/{$oldimage}" ) ) {
251 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
252 }
253 }
254
255 function revert()
256 {
257 global $wgOut;
258 global $oldimage;
259 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
260
261 if ( strlen( $oldimage ) < 16 ) {
262 $wgOut->unexpectedValueError( "oldimage", $oldimage );
263 return;
264 }
265 if ( wfReadOnly() ) {
266 $wgOut->readOnlyPage();
267 return;
268 }
269 $name = substr( $oldimage, 15 );
270
271 $dest = wfImageDir( $name );
272 $archive = wfImageArchiveDir( $name );
273 $curfile = "{$dest}/{$name}";
274
275 if ( ! is_file( $curfile ) ) {
276 $wgOut->fileNotFoundError( $curfile );
277 return;
278 }
279 $oldver = wfTimestampNow() . "!{$name}";
280 $size = wfGetSQL( "oldimage", "oi_size", "oi_archive_name='" .
281 wfStrencode( $oldimage ) . "'" );
282
283 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
284 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
285 return;
286 }
287 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
288 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
289 }
290 wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
291 # Squid purging
292 if ( $wgUseSquid ) {
293 $urlArr = Array(
294 $wgInternalServer.wfImageArchiveUrl( $name ),
295 $wgInternalServer.wfImageUrl( $name )
296 );
297 wfPurgeSquidServers($urlArr);
298 }
299
300 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
301 $wgOut->setRobotpolicy( "noindex,nofollow" );
302 $wgOut->addHTML( wfMsg( "imagereverted" ) );
303 $wgOut->returnToMain( false );
304 }
305 }
306
307 ?>