91521c6d8293ed73332fb0a8a5db35b3f94a2853
[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 il_from FROM imagelinks WHERE il_to='" .
99 wfStrencode( $this->mTitle->getDBkey() ) . "'";
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 = $s->il_from;
111 $link = $sk->makeKnownLink( $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 $fname = "Article::doDelete";
162
163 if ( $image ) {
164 $dest = wfImageDir( $image );
165 $archive = wfImageDir( $image );
166 if ( ! unlink( "{$dest}/{$image}" ) ) {
167 $wgOut->fileDeleteError( "{$dest}/{$image}" );
168 return;
169 }
170 $sql = "DELETE FROM image WHERE img_name='" .
171 wfStrencode( $image ) . "'";
172 wfQuery( $sql, DB_WRITE, $fname );
173
174 $sql = "SELECT oi_archive_name FROM oldimage WHERE oi_name='" .
175 wfStrencode( $image ) . "'";
176 $res = wfQuery( $sql, DB_READ, $fname );
177
178 while ( $s = wfFetchObject( $res ) ) {
179 $this->doDeleteOldImage( $s->oi_archive_name );
180 }
181 $sql = "DELETE FROM oldimage WHERE oi_name='" .
182 wfStrencode( $image ) . "'";
183 wfQuery( $sql, DB_WRITE, $fname );
184
185 # Image itself is now gone, and database is cleaned.
186 # Now we remove the image description page.
187
188 $nt = Title::newFromText( $wgLang->getNsText( Namespace::getImage() ) . ":" . $image );
189 $this->doDeleteArticle( $nt );
190
191 $deleted = $image;
192 } else if ( $oldimage ) {
193 $this->doDeleteOldImage( $oldimage );
194 $sql = "DELETE FROM oldimage WHERE oi_archive_name='" .
195 wfStrencode( $oldimage ) . "'";
196 wfQuery( $sql, DB_WRITE, $fname );
197
198 $deleted = $oldimage;
199 } else {
200 $this->doDeleteArticle( $this->mTitle );
201 $deleted = $this->mTitle->getPrefixedText();
202 }
203 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
204 $wgOut->setRobotpolicy( "noindex,nofollow" );
205
206 $sk = $wgUser->getSkin();
207 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
208 Namespace::getWikipedia() ) .
209 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
210
211 $text = wfMsg( "deletedtext", $deleted, $loglink );
212
213 $wgOut->addHTML( "<p>" . $text );
214 $wgOut->returnToMain( false );
215 }
216
217 function doDeleteOldImage( $oldimage )
218 {
219 global $wgOut;
220
221 $name = substr( $oldimage, 15 );
222 $archive = wfImageArchiveDir( $name );
223 if ( ! unlink( "{$archive}/{$oldimage}" ) ) {
224 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
225 }
226 }
227
228 function revert()
229 {
230 global $wgOut;
231 global $oldimage;
232
233 if ( strlen( $oldimage ) < 16 ) {
234 $wgOut->unexpectedValueError( "oldimage", $oldimage );
235 return;
236 }
237 if ( wfReadOnly() ) {
238 $wgOut->readOnlyPage();
239 return;
240 }
241 $name = substr( $oldimage, 15 );
242
243 $dest = wfImageDir( $name );
244 $archive = wfImageArchiveDir( $name );
245 $curfile = "{$dest}/{$name}";
246
247 if ( ! is_file( $curfile ) ) {
248 $wgOut->fileNotFoundError( $curfile );
249 return;
250 }
251 $oldver = wfTimestampNow() . "!{$name}";
252 $size = wfGetSQL( "oldimage", "oi_size", "oi_archive_name='" .
253 wfStrencode( $oldimage ) . "'" );
254
255 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
256 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
257 return;
258 }
259 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
260 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
261 }
262 wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
263
264 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
265 $wgOut->setRobotpolicy( "noindex,nofollow" );
266 $wgOut->addHTML( wfMsg( "imagereverted" ) );
267 $wgOut->returnToMain( false );
268 }
269 }
270
271 ?>