Replaced old memcached client with new one
[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 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 global $wgOut, $wgUser;
52 $sk = $wgUser->getSkin();
53 $s = "</center>";
54 $wgOut->AddHTML( $s );
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, $wgLang;
63 $fname = "Article::imageHistory";
64
65 $sql = "SELECT img_size,img_description,img_user," .
66 "img_user_text,img_timestamp FROM image WHERE " .
67 "img_name='" . wfStrencode( $this->mTitle->getDBkey() ) . "'";
68 $res = wfQuery( $sql, DB_READ, $fname );
69
70 if ( 0 == wfNumRows( $res ) ) { return; }
71
72 $sk = $wgUser->getSkin();
73 $s = $sk->beginImageHistoryList();
74
75 $line = wfFetchObject( $res );
76 $s .= $sk->imageHistoryLine( true, $line->img_timestamp,
77 $this->mTitle->getText(), $line->img_user,
78 $line->img_user_text, $line->img_size, $line->img_description );
79
80 $sql = "SELECT oi_size,oi_description,oi_user," .
81 "oi_user_text,oi_timestamp,oi_archive_name FROM oldimage WHERE " .
82 "oi_name='" . wfStrencode( $this->mTitle->getDBkey() ) . "' " .
83 "ORDER BY oi_timestamp DESC";
84 $res = wfQuery( $sql, DB_READ, $fname );
85
86 while ( $line = wfFetchObject( $res ) ) {
87 $s .= $sk->imageHistoryLine( false, $line->oi_timestamp,
88 $line->oi_archive_name, $line->oi_user,
89 $line->oi_user_text, $line->oi_size, $line->oi_description );
90 }
91 $s .= $sk->endImageHistoryList();
92 $wgOut->addHTML( $s );
93 }
94
95 function imageLinks()
96 {
97 global $wgUser, $wgOut;
98
99 $wgOut->addHTML( "<h2>" . wfMsg( "imagelinks" ) . "</h2>\n" );
100
101 $sql = "SELECT il_from FROM imagelinks WHERE il_to='" .
102 wfStrencode( $this->mTitle->getDBkey() ) . "'";
103 $res = wfQuery( $sql, DB_READ, "Article::imageLinks" );
104
105 if ( 0 == wfNumRows( $res ) ) {
106 $wgOut->addHtml( "<p>" . wfMsg( "nolinkstoimage" ) . "\n" );
107 return;
108 }
109 $wgOut->addHTML( "<p>" . wfMsg( "linkstoimage" ) . "\n<ul>" );
110
111 $sk = $wgUser->getSkin();
112 while ( $s = wfFetchObject( $res ) ) {
113 $name = $s->il_from;
114 $link = $sk->makeKnownLink( $name, "" );
115 $wgOut->addHTML( "<li>{$link}</li>\n" );
116 }
117 $wgOut->addHTML( "</ul>\n" );
118 }
119
120 function delete()
121 {
122 global $wgUser, $wgOut;
123 global $wpConfirm, $wpReason, $image, $oldimage;
124
125 # Anybody can delete old revisions of images; only sysops
126 # can delete articles and current images
127
128 if ( !$wgUser->isSysop() ) {
129 $wgOut->sysopRequired();
130 return;
131 }
132 if ( wfReadOnly() ) {
133 $wgOut->readOnlyPage();
134 return;
135 }
136
137 # Better double-check that it hasn't been deleted yet!
138 $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
139 if ( $image ) {
140 if ( "" == trim( $image ) ) {
141 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
142 return;
143 }
144 }
145
146 # Likewise, deleting old images doesn't require confirmation
147 if ( $oldimage || 1 == $wpConfirm ) {
148 $this->doDelete();
149 return;
150 }
151
152 if ( $image ) {
153 $q = "&image={$image}";
154 } else if ( $oldimage ) {
155 $q = "&oldimage={$oldimage}";
156 }
157 return $this->confirmDelete( $q );
158 }
159
160 function doDelete()
161 {
162 global $wgOut, $wgUser, $wgLang;
163 global $image, $oldimage, $wpReason;
164 $fname = "Article::doDelete";
165
166 if ( $image ) {
167 $dest = wfImageDir( $image );
168 $archive = wfImageDir( $image );
169 if ( ! unlink( "{$dest}/{$image}" ) ) {
170 $wgOut->fileDeleteError( "{$dest}/{$image}" );
171 return;
172 }
173 $sql = "DELETE FROM image WHERE img_name='" .
174 wfStrencode( $image ) . "'";
175 wfQuery( $sql, DB_WRITE, $fname );
176
177 $sql = "SELECT oi_archive_name FROM oldimage WHERE oi_name='" .
178 wfStrencode( $image ) . "'";
179 $res = wfQuery( $sql, DB_READ, $fname );
180
181 while ( $s = wfFetchObject( $res ) ) {
182 $this->doDeleteOldImage( $s->oi_archive_name );
183 }
184 $sql = "DELETE FROM oldimage WHERE oi_name='" .
185 wfStrencode( $image ) . "'";
186 wfQuery( $sql, DB_WRITE, $fname );
187
188 # Image itself is now gone, and database is cleaned.
189 # Now we remove the image description page.
190
191 $nt = Title::newFromText( $wgLang->getNsText( Namespace::getImage() ) . ":" . $image );
192 $this->doDeleteArticle( $nt );
193
194 $deleted = $image;
195 } else if ( $oldimage ) {
196 $this->doDeleteOldImage( $oldimage );
197 $sql = "DELETE FROM oldimage WHERE oi_archive_name='" .
198 wfStrencode( $oldimage ) . "'";
199 wfQuery( $sql, DB_WRITE, $fname );
200
201 $deleted = $oldimage;
202 } else {
203 $this->doDeleteArticle( $this->mTitle );
204 $deleted = $this->mTitle->getPrefixedText();
205 }
206 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
207 $wgOut->setRobotpolicy( "noindex,nofollow" );
208
209 $sk = $wgUser->getSkin();
210 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
211 Namespace::getWikipedia() ) .
212 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
213
214 $text = wfMsg( "deletedtext", $deleted, $loglink );
215
216 $wgOut->addHTML( "<p>" . $text );
217 $wgOut->returnToMain( false );
218 }
219
220 function doDeleteOldImage( $oldimage )
221 {
222 global $wgOut;
223
224 $name = substr( $oldimage, 15 );
225 $archive = wfImageArchiveDir( $name );
226 if ( ! unlink( "{$archive}/{$oldimage}" ) ) {
227 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
228 }
229 }
230
231 function revert()
232 {
233 global $wgOut;
234 global $oldimage;
235
236 if ( strlen( $oldimage ) < 16 ) {
237 $wgOut->unexpectedValueError( "oldimage", $oldimage );
238 return;
239 }
240 if ( wfReadOnly() ) {
241 $wgOut->readOnlyPage();
242 return;
243 }
244 $name = substr( $oldimage, 15 );
245
246 $dest = wfImageDir( $name );
247 $archive = wfImageArchiveDir( $name );
248 $curfile = "{$dest}/{$name}";
249
250 if ( ! is_file( $curfile ) ) {
251 $wgOut->fileNotFoundError( $curfile );
252 return;
253 }
254 $oldver = wfTimestampNow() . "!{$name}";
255 $size = wfGetSQL( "oldimage", "oi_size", "oi_archive_name='" .
256 wfStrencode( $oldimage ) . "'" );
257
258 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
259 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
260 return;
261 }
262 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
263 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
264 }
265 wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
266
267 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
268 $wgOut->setRobotpolicy( "noindex,nofollow" );
269 $wgOut->addHTML( wfMsg( "imagereverted" ) );
270 $wgOut->returnToMain( false );
271 }
272 }
273
274 ?>