JeLuF's image resizing code
[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 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 list($width, $height, $type, $attr) = getimagesize( $path );
35
36 $sk = $wgUser->getSkin();
37
38 if ( $type != "" ) {
39 # image
40 $s .= "<center><img src=\"{$url}\" width=\"{$width}\" height=\"{$height}\"></center>";
41 } else {
42 $s .= "<center>".$sk->makeMediaLink($name,"")."</center>";
43 }
44 $wgOut->AddHTML( $s );
45 }
46
47 function closeShowImage()
48 {
49 global $wgOut, $wgUser;
50 $sk = $wgUser->getSkin();
51 $s = "</center>";
52 $wgOut->AddHTML( $s );
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 il_from FROM imagelinks WHERE il_to='" .
100 wfStrencode( $this->mTitle->getDBkey() ) . "'";
101 $res = wfQuery( $sql, DB_READ, "Article::imageLinks" );
102
103 if ( 0 == wfNumRows( $res ) ) {
104 $wgOut->addHtml( "<p>" . wfMsg( "nolinkstoimage" ) . "\n" );
105 return;
106 }
107 $wgOut->addHTML( "<p>" . wfMsg( "linkstoimage" ) . "\n<ul>" );
108
109 $sk = $wgUser->getSkin();
110 while ( $s = wfFetchObject( $res ) ) {
111 $name = $s->il_from;
112 $link = $sk->makeKnownLink( $name, "" );
113 $wgOut->addHTML( "<li>{$link}</li>\n" );
114 }
115 $wgOut->addHTML( "</ul>\n" );
116 }
117
118 function delete()
119 {
120 global $wgUser, $wgOut;
121 global $wpConfirm, $wpReason, $image, $oldimage;
122
123 # Anybody can delete old revisions of images; only sysops
124 # can delete articles and current images
125
126 if ( !$wgUser->isSysop() ) {
127 $wgOut->sysopRequired();
128 return;
129 }
130 if ( wfReadOnly() ) {
131 $wgOut->readOnlyPage();
132 return;
133 }
134
135 # Better double-check that it hasn't been deleted yet!
136 $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
137 if ( $image ) {
138 if ( "" == trim( $image ) ) {
139 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
140 return;
141 }
142 }
143
144 # Likewise, deleting old images doesn't require confirmation
145 if ( $oldimage || 1 == $wpConfirm ) {
146 $this->doDelete();
147 return;
148 }
149
150 if ( $image ) {
151 $q = "&image={$image}";
152 } else if ( $oldimage ) {
153 $q = "&oldimage={$oldimage}";
154 }
155 return $this->confirmDelete( $q );
156 }
157
158 function doDelete()
159 {
160 global $wgOut, $wgUser, $wgLang;
161 global $image, $oldimage, $wpReason;
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 while ( $s = wfFetchObject( $res ) ) {
180 $this->doDeleteOldImage( $s->oi_archive_name );
181 }
182 $sql = "DELETE FROM oldimage WHERE oi_name='" .
183 wfStrencode( $image ) . "'";
184 wfQuery( $sql, DB_WRITE, $fname );
185
186 # Image itself is now gone, and database is cleaned.
187 # Now we remove the image description page.
188
189 $nt = Title::newFromText( $wgLang->getNsText( Namespace::getImage() ) . ":" . $image );
190 $this->doDeleteArticle( $nt );
191
192 $deleted = $image;
193 } else if ( $oldimage ) {
194 $this->doDeleteOldImage( $oldimage );
195 $sql = "DELETE FROM oldimage WHERE oi_archive_name='" .
196 wfStrencode( $oldimage ) . "'";
197 wfQuery( $sql, DB_WRITE, $fname );
198
199 $deleted = $oldimage;
200 } else {
201 $this->doDeleteArticle( $this->mTitle );
202 $deleted = $this->mTitle->getPrefixedText();
203 }
204 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
205 $wgOut->setRobotpolicy( "noindex,nofollow" );
206
207 $sk = $wgUser->getSkin();
208 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
209 Namespace::getWikipedia() ) .
210 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
211
212 $text = wfMsg( "deletedtext", $deleted, $loglink );
213
214 $wgOut->addHTML( "<p>" . $text );
215 $wgOut->returnToMain( false );
216 }
217
218 function doDeleteOldImage( $oldimage )
219 {
220 global $wgOut;
221
222 $name = substr( $oldimage, 15 );
223 $archive = wfImageArchiveDir( $name );
224 if ( ! unlink( "{$archive}/{$oldimage}" ) ) {
225 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
226 }
227 }
228
229 function revert()
230 {
231 global $wgOut;
232 global $oldimage;
233
234 if ( strlen( $oldimage ) < 16 ) {
235 $wgOut->unexpectedValueError( "oldimage", $oldimage );
236 return;
237 }
238 if ( wfReadOnly() ) {
239 $wgOut->readOnlyPage();
240 return;
241 }
242 $name = substr( $oldimage, 15 );
243
244 $dest = wfImageDir( $name );
245 $archive = wfImageArchiveDir( $name );
246 $curfile = "{$dest}/{$name}";
247
248 if ( ! is_file( $curfile ) ) {
249 $wgOut->fileNotFoundError( $curfile );
250 return;
251 }
252 $oldver = wfTimestampNow() . "!{$name}";
253 $size = wfGetSQL( "oldimage", "oi_size", "oi_archive_name='" .
254 wfStrencode( $oldimage ) . "'" );
255
256 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
257 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
258 return;
259 }
260 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
261 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
262 }
263 wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
264
265 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
266 $wgOut->setRobotpolicy( "noindex,nofollow" );
267 $wgOut->addHTML( wfMsg( "imagereverted" ) );
268 $wgOut->returnToMain( false );
269 }
270 }
271
272 ?>