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