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