massive double to single quotes conversion. I have not noticed any bug after a lot...
[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
80 $line = $this->img->nextHistoryLine();
81
82 if ( $line ) {
83 $s = $sk->beginImageHistoryList() .
84 $sk->imageHistoryLine( true, $line->img_timestamp,
85 $this->mTitle->getDBkey(), $line->img_user,
86 $line->img_user_text, $line->img_size, $line->img_description );
87
88 while ( $line = $this->img->nextHistoryLine() ) {
89 $s .= $sk->imageHistoryLine( false, $line->img_timestamp,
90 $line->oi_archive_name, $line->img_user,
91 $line->img_user_text, $line->img_size, $line->img_description );
92 }
93 $s .= $sk->endImageHistoryList();
94 } else { $s=''; }
95 $wgOut->addHTML( $s );
96 }
97
98 function imageLinks()
99 {
100 global $wgUser, $wgOut;
101
102 $wgOut->addHTML( '<h2>' . wfMsg( 'imagelinks' ) . "</h2>\n" );
103
104 $dbr =& wfGetDB( DB_SLAVE );
105 $cur = $dbr->tableName( 'cur' );
106 $imagelinks = $dbr->tableName( 'imagelinks' );
107
108 $sql = "SELECT cur_namespace,cur_title FROM $imagelinks,$cur WHERE il_to=" .
109 $dbr->addQuotes( $this->mTitle->getDBkey() ) . " AND il_from=cur_id";
110 $res = $dbr->query( $sql, DB_SLAVE, "Article::imageLinks" );
111
112 if ( 0 == $dbr->numRows( $res ) ) {
113 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
114 return;
115 }
116 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
117
118 $sk = $wgUser->getSkin();
119 while ( $s = $dbr->fetchObject( $res ) ) {
120 $name = Title::MakeTitle( $s->cur_namespace, $s->cur_title );
121 $link = $sk->makeKnownLinkObj( $name, "" );
122 $wgOut->addHTML( "<li>{$link}</li>\n" );
123 }
124 $wgOut->addHTML( "</ul>\n" );
125 }
126
127 function delete()
128 {
129 global $wgUser, $wgOut, $wgRequest;
130
131 $confirm = $wgRequest->getBool( 'wpConfirm' );
132 $image = $wgRequest->getVal( 'image' );
133 $oldimage = $wgRequest->getVal( 'oldimage' );
134
135 # Only sysops can delete images. Previously ordinary users could delete
136 # old revisions, but this is no longer the case.
137 if ( !$wgUser->isSysop() ) {
138 $wgOut->sysopRequired();
139 return;
140 }
141 if ( wfReadOnly() ) {
142 $wgOut->readOnlyPage();
143 return;
144 }
145
146 # Better double-check that it hasn't been deleted yet!
147 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
148 if ( !is_null( $image ) ) {
149 if ( '' == trim( $image ) ) {
150 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
151 return;
152 }
153 }
154
155 # Deleting old images doesn't require confirmation
156 if ( !is_null( $oldimage ) || $confirm ) {
157 $this->doDelete();
158 return;
159 }
160
161 if ( !is_null( $image ) ) {
162 $q = '&image=' . urlencode( $image );
163 } else if ( !is_null( $oldimage ) ) {
164 $q = '&oldimage=' . urlencode( $oldimage );
165 } else {
166 $q = '';
167 }
168 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
169 }
170
171 function doDelete()
172 {
173 global $wgOut, $wgUser, $wgLang, $wgRequest;
174 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
175 $fname = 'Article::doDelete';
176
177 $reason = $wgRequest->getVal( 'wpReason' );
178 $image = $wgRequest->getVal( 'image' );
179 $oldimage = $wgRequest->getVal( 'oldimage' );
180
181 $dbw =& wfGetDB( DB_MASTER );
182
183 if ( !is_null( $image ) ) {
184 $dest = wfImageDir( $image );
185 $archive = wfImageDir( $image );
186 if ( ! @unlink( "{$dest}/{$image}" ) ) {
187 $wgOut->fileDeleteError( "{$dest}/{$image}" );
188 return;
189 }
190 $dbw->delete( 'image', array( 'img_name' => $image ) );
191 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
192
193 # Squid purging
194 if ( $wgUseSquid ) {
195 $urlArr = Array(
196 $wgInternalServer . Image::wfImageUrl( $image )
197 );
198 wfPurgeSquidServers($urlArr);
199 }
200
201
202 $urlArr = Array();
203 while ( $s = $dbr->fetchObject( $res ) ) {
204 $this->doDeleteOldImage( $s->oi_archive_name );
205 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name );
206 }
207
208 # Squid purging, part II
209 if ( $wgUseSquid ) {
210 /* this needs to be done after LinksUpdate */
211 $u = new SquidUpdate( $urlArr );
212 array_push( $wgDeferredUpdateList, $u );
213 }
214
215 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
216
217 # Image itself is now gone, and database is cleaned.
218 # Now we remove the image description page.
219
220 $nt = Title::newFromText( $wgLang->getNsText( Namespace::getImage() ) . ":" . $image );
221 $article = new Article( $nt );
222 $article->doDeleteArticle( $reason ); # ignore errors
223
224 $deleted = $image;
225 } else if ( !is_null( $oldimage ) ) {
226 # Squid purging
227 if ( $wgUseSquid ) {
228 $urlArr = Array(
229 $wgInternalServer.wfImageArchiveUrl( $oldimage )
230 );
231 wfPurgeSquidServers($urlArr);
232 }
233 $this->doDeleteOldImage( $oldimage );
234 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
235 $deleted = $oldimage;
236 } else {
237 $this->doDeleteArticle( $reason ); # ignore errors
238 $deleted = $this->mTitle->getPrefixedText();
239 }
240 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
241 $wgOut->setRobotpolicy( 'noindex,nofollow' );
242
243 $sk = $wgUser->getSkin();
244 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
245 Namespace::getWikipedia() ) .
246 ':' . wfMsg( 'dellogpage' ), wfMsg( 'deletionlog' ) );
247
248 $text = wfMsg( 'deletedtext', $deleted, $loglink );
249
250 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
251 $wgOut->returnToMain( false );
252 }
253
254 function doDeleteOldImage( $oldimage )
255 {
256 global $wgOut;
257
258 $name = substr( $oldimage, 15 );
259 $archive = wfImageArchiveDir( $name );
260 if ( ! unlink( "{$archive}/{$oldimage}" ) ) {
261 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
262 }
263 }
264
265 function revert()
266 {
267 global $wgOut, $wgRequest;
268 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
269
270 $oldimage = $wgRequest->getText( 'oldimage' );
271
272 if ( strlen( $oldimage ) < 16 ) {
273 $wgOut->unexpectedValueError( 'oldimage', $oldimage );
274 return;
275 }
276 if ( wfReadOnly() ) {
277 $wgOut->readOnlyPage();
278 return;
279 }
280 if ( ! $this->mTitle->userCanEdit() ) {
281 $wgOut->sysopRequired();
282 return;
283 }
284 $name = substr( $oldimage, 15 );
285
286 $dest = wfImageDir( $name );
287 $archive = wfImageArchiveDir( $name );
288 $curfile = "{$dest}/{$name}";
289
290 if ( ! is_file( $curfile ) ) {
291 $wgOut->fileNotFoundError( $curfile );
292 return;
293 }
294 $oldver = wfTimestampNow() . "!{$name}";
295
296 $dbr =& wfGetDB( DB_SLAVE );
297 $size = $dbr->getField( 'oldimage', 'oi_size', 'oi_archive_name=\'' .
298 $dbr->strencode( $oldimage ) . "'" );
299
300 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
301 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
302 return;
303 }
304 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
305 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
306 }
307 wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
308 # Squid purging
309 if ( $wgUseSquid ) {
310 $urlArr = Array(
311 $wgInternalServer.wfImageArchiveUrl( $name ),
312 $wgInternalServer . Image::wfImageUrl( $name )
313 );
314 wfPurgeSquidServers($urlArr);
315 }
316
317 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
318 $wgOut->setRobotpolicy( 'noindex,nofollow' );
319 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
320 $wgOut->returnToMain( false );
321 }
322 }
323
324
325 ?>