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