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