Image deletion for yssops only
[lhc/web/wiklou.git] / includes / ImagePage.php
1 <?
2
3 /*
4 Special handling for image description pages
5 */
6
7 class ImagePage extends Article {
8
9 function view() {
10 Article::view();
11
12 # If the article we've just shown is in the "Image" namespace,
13 # follow it with the history list and link list for the image
14 # it describes.
15
16 if ( Namespace::getImage() == $this->mTitle->getNamespace() ) {
17 $this->imageHistory();
18 $this->imageLinks();
19 }
20 }
21
22 # If the page we've just displayed is in the "Image" namespace,
23 # we follow it with an upload history of the image and its usage.
24
25 function imageHistory()
26 {
27 global $wgUser, $wgOut, $wgLang;
28 $fname = "Article::imageHistory";
29
30 $sql = "SELECT img_size,img_description,img_user," .
31 "img_user_text,img_timestamp FROM image WHERE " .
32 "img_name='" . wfStrencode( $this->mTitle->getDBkey() ) . "'";
33 $res = wfQuery( $sql, DB_READ, $fname );
34
35 if ( 0 == wfNumRows( $res ) ) { return; }
36
37 $sk = $wgUser->getSkin();
38 $s = $sk->beginImageHistoryList();
39
40 $line = wfFetchObject( $res );
41 $s .= $sk->imageHistoryLine( true, $line->img_timestamp,
42 $this->mTitle->getText(), $line->img_user,
43 $line->img_user_text, $line->img_size, $line->img_description );
44
45 $sql = "SELECT oi_size,oi_description,oi_user," .
46 "oi_user_text,oi_timestamp,oi_archive_name FROM oldimage WHERE " .
47 "oi_name='" . wfStrencode( $this->mTitle->getDBkey() ) . "' " .
48 "ORDER BY oi_timestamp DESC";
49 $res = wfQuery( $sql, DB_READ, $fname );
50
51 while ( $line = wfFetchObject( $res ) ) {
52 $s .= $sk->imageHistoryLine( false, $line->oi_timestamp,
53 $line->oi_archive_name, $line->oi_user,
54 $line->oi_user_text, $line->oi_size, $line->oi_description );
55 }
56 $s .= $sk->endImageHistoryList();
57 $wgOut->addHTML( $s );
58 }
59
60 function imageLinks()
61 {
62 global $wgUser, $wgOut;
63
64 $wgOut->addHTML( "<h2>" . wfMsg( "imagelinks" ) . "</h2>\n" );
65
66 $sql = "SELECT il_from FROM imagelinks WHERE il_to='" .
67 wfStrencode( $this->mTitle->getDBkey() ) . "'";
68 $res = wfQuery( $sql, DB_READ, "Article::imageLinks" );
69
70 if ( 0 == wfNumRows( $res ) ) {
71 $wgOut->addHtml( "<p>" . wfMsg( "nolinkstoimage" ) . "\n" );
72 return;
73 }
74 $wgOut->addHTML( "<p>" . wfMsg( "linkstoimage" ) . "\n<ul>" );
75
76 $sk = $wgUser->getSkin();
77 while ( $s = wfFetchObject( $res ) ) {
78 $name = $s->il_from;
79 $link = $sk->makeKnownLink( $name, "" );
80 $wgOut->addHTML( "<li>{$link}</li>\n" );
81 }
82 $wgOut->addHTML( "</ul>\n" );
83 }
84
85 function delete()
86 {
87 global $wgUser, $wgOut;
88 global $wpConfirm, $wpReason, $image, $oldimage;
89
90 # Anybody can delete old revisions of images; only sysops
91 # can delete articles and current images
92
93 if ( !$wgUser->isSysop() ) {
94 $wgOut->sysopRequired();
95 return;
96 }
97 if ( wfReadOnly() ) {
98 $wgOut->readOnlyPage();
99 return;
100 }
101
102 # Better double-check that it hasn't been deleted yet!
103 $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
104 if ( $image ) {
105 if ( "" == trim( $image ) ) {
106 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
107 return;
108 }
109 }
110
111 # Likewise, deleting old images doesn't require confirmation
112 if ( $oldimage || 1 == $wpConfirm ) {
113 $this->doDelete();
114 return;
115 }
116
117 if ( $image ) {
118 $q = "&image={$image}";
119 } else if ( $oldimage ) {
120 $q = "&oldimage={$oldimage}";
121 }
122 return $this->confirmDelete( $q );
123 }
124
125 function doDelete()
126 {
127 global $wgOut, $wgUser, $wgLang;
128 global $image, $oldimage, $wpReason;
129 $fname = "Article::doDelete";
130
131 if ( $image ) {
132 $dest = wfImageDir( $image );
133 $archive = wfImageDir( $image );
134 if ( ! unlink( "{$dest}/{$image}" ) ) {
135 $wgOut->fileDeleteError( "{$dest}/{$image}" );
136 return;
137 }
138 $sql = "DELETE FROM image WHERE img_name='" .
139 wfStrencode( $image ) . "'";
140 wfQuery( $sql, DB_WRITE, $fname );
141
142 $sql = "SELECT oi_archive_name FROM oldimage WHERE oi_name='" .
143 wfStrencode( $image ) . "'";
144 $res = wfQuery( $sql, DB_READ, $fname );
145
146 while ( $s = wfFetchObject( $res ) ) {
147 $this->doDeleteOldImage( $s->oi_archive_name );
148 }
149 $sql = "DELETE FROM oldimage WHERE oi_name='" .
150 wfStrencode( $image ) . "'";
151 wfQuery( $sql, DB_WRITE, $fname );
152
153 # Image itself is now gone, and database is cleaned.
154 # Now we remove the image description page.
155
156 $nt = Title::newFromText( $wgLang->getNsText( Namespace::getImage() ) . ":" . $image );
157 $this->doDeleteArticle( $nt );
158
159 $deleted = $image;
160 } else if ( $oldimage ) {
161 $this->doDeleteOldImage( $oldimage );
162 $sql = "DELETE FROM oldimage WHERE oi_archive_name='" .
163 wfStrencode( $oldimage ) . "'";
164 wfQuery( $sql, DB_WRITE, $fname );
165
166 $deleted = $oldimage;
167 } else {
168 $this->doDeleteArticle( $this->mTitle );
169 $deleted = $this->mTitle->getPrefixedText();
170 }
171 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
172 $wgOut->setRobotpolicy( "noindex,nofollow" );
173
174 $sk = $wgUser->getSkin();
175 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
176 Namespace::getWikipedia() ) .
177 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
178
179 $text = wfMsg( "deletedtext", $deleted, $loglink );
180
181 $wgOut->addHTML( "<p>" . $text );
182 $wgOut->returnToMain( false );
183 }
184
185 function doDeleteOldImage( $oldimage )
186 {
187 global $wgOut;
188
189 $name = substr( $oldimage, 15 );
190 $archive = wfImageArchiveDir( $name );
191 if ( ! unlink( "{$archive}/{$oldimage}" ) ) {
192 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
193 }
194 }
195
196 function revert()
197 {
198 global $wgOut;
199 global $oldimage;
200
201 if ( strlen( $oldimage ) < 16 ) {
202 $wgOut->unexpectedValueError( "oldimage", $oldimage );
203 return;
204 }
205 if ( wfReadOnly() ) {
206 $wgOut->readOnlyPage();
207 return;
208 }
209 $name = substr( $oldimage, 15 );
210
211 $dest = wfImageDir( $name );
212 $archive = wfImageArchiveDir( $name );
213 $curfile = "{$dest}/{$name}";
214
215 if ( ! is_file( $curfile ) ) {
216 $wgOut->fileNotFoundError( $curfile );
217 return;
218 }
219 $oldver = wfTimestampNow() . "!{$name}";
220 $size = wfGetSQL( "oldimage", "oi_size", "oi_archive_name='" .
221 wfStrencode( $oldimage ) . "'" );
222
223 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
224 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
225 return;
226 }
227 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
228 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
229 }
230 wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
231
232 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
233 $wgOut->setRobotpolicy( "noindex,nofollow" );
234 $wgOut->addHTML( wfMsg( "imagereverted" ) );
235 $wgOut->returnToMain( false );
236 }
237 }
238
239 ?>