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