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