Some changes to the link tables. They now all use a key on cur_id for the *_from...
[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 # Anybody can delete old revisions of images; only sysops
123 # can delete articles and current images
124
125 if ( !$wgUser->isSysop() ) {
126 $wgOut->sysopRequired();
127 return;
128 }
129 if ( wfReadOnly() ) {
130 $wgOut->readOnlyPage();
131 return;
132 }
133
134 # Better double-check that it hasn't been deleted yet!
135 $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
136 if ( $image ) {
137 if ( "" == trim( $image ) ) {
138 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
139 return;
140 }
141 }
142
143 # Likewise, deleting old images doesn't require confirmation
144 if ( $oldimage || 1 == $wpConfirm ) {
145 $this->doDelete();
146 return;
147 }
148
149 if ( $image ) {
150 $q = "&image={$image}";
151 } else if ( $oldimage ) {
152 $q = "&oldimage={$oldimage}";
153 }
154 return $this->confirmDelete( $q );
155 }
156
157 function doDelete()
158 {
159 global $wgOut, $wgUser, $wgLang;
160 global $image, $oldimage, $wpReason;
161 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
162 $fname = "Article::doDelete";
163
164 if ( $image ) {
165 $dest = wfImageDir( $image );
166 $archive = wfImageDir( $image );
167 if ( ! unlink( "{$dest}/{$image}" ) ) {
168 $wgOut->fileDeleteError( "{$dest}/{$image}" );
169 return;
170 }
171 $sql = "DELETE FROM image WHERE img_name='" .
172 wfStrencode( $image ) . "'";
173 wfQuery( $sql, DB_WRITE, $fname );
174
175 $sql = "SELECT oi_archive_name FROM oldimage WHERE oi_name='" .
176 wfStrencode( $image ) . "'";
177 $res = wfQuery( $sql, DB_READ, $fname );
178
179 # Squid purging
180 if ( $wgUseSquid ) {
181 $urlArr = Array(
182 $wgInternalServer.wfImageUrl( $image )
183 );
184 wfPurgeSquidServers($urlArr);
185 }
186
187
188 $urlArr = Array();
189 while ( $s = wfFetchObject( $res ) ) {
190 $this->doDeleteOldImage( $s->oi_archive_name );
191 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name );
192 }
193
194 # Squid purging, part II
195 if ( $wgUseSquid ) {
196 /* this needs to be done after LinksUpdate */
197 $u = new SquidUpdate($this->mTitle, $urlArr);
198 array_push( $wgDeferredUpdateList, $u );
199 }
200
201 $sql = "DELETE FROM oldimage WHERE oi_name='" .
202 wfStrencode( $image ) . "'";
203 wfQuery( $sql, DB_WRITE, $fname );
204
205 # Image itself is now gone, and database is cleaned.
206 # Now we remove the image description page.
207
208 $nt = Title::newFromText( $wgLang->getNsText( Namespace::getImage() ) . ":" . $image );
209 $this->doDeleteArticle( $nt );
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( $this->mTitle );
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 ?>