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