fixing notices and syncing with refactoring in Article.php
[mediawiki.git] / includes / ImagePage.php
blobdab95ac9022d66e257fe690a062a9f4ba93f20eb
1 <?php
3 /*
4 Special handling for image description pages
5 */
7 class ImagePage extends Article {
9 function view() {
10 if ( Namespace::getImage() == $this->mTitle->getNamespace() ) {
11 $this->openShowImage();
14 Article::view();
16 # If the article we've just shown is in the "Image" namespace,
17 # follow it with the history list and link list for the image
18 # it describes.
20 if ( Namespace::getImage() == $this->mTitle->getNamespace() ) {
21 $this->closeShowImage();
22 $this->imageHistory();
23 $this->imageLinks();
27 function openShowImage()
29 global $wgOut, $wgUser;
30 $name = $this->mTitle->getText();
31 $path = wfImagePath( $name );
32 $url = wfImageUrl( $name );
34 if ( file_exists( $path ) ) {
35 list($width, $height, $type, $attr) = getimagesize( $path );
37 $sk = $wgUser->getSkin();
39 if ( $type != "" ) {
40 # image
41 $s = "<center><img src=\"{$url}\" width=\"{$width}\" height=\"{$height}\"></center>";
42 } else {
43 $s = "<center>".$sk->makeMediaLink($name,"")."</center>";
45 $wgOut->addHTML( $s );
49 function closeShowImage()
51 # For overloading
54 # If the page we've just displayed is in the "Image" namespace,
55 # we follow it with an upload history of the image and its usage.
57 function imageHistory()
59 global $wgUser, $wgOut, $wgLang;
60 $fname = "Article::imageHistory";
62 $sql = "SELECT img_size,img_description,img_user," .
63 "img_user_text,img_timestamp FROM image WHERE " .
64 "img_name='" . wfStrencode( $this->mTitle->getDBkey() ) . "'";
65 $res = wfQuery( $sql, DB_READ, $fname );
67 if ( 0 == wfNumRows( $res ) ) { return; }
69 $sk = $wgUser->getSkin();
70 $s = $sk->beginImageHistoryList();
72 $line = wfFetchObject( $res );
73 $s .= $sk->imageHistoryLine( true, $line->img_timestamp,
74 $this->mTitle->getText(), $line->img_user,
75 $line->img_user_text, $line->img_size, $line->img_description );
77 $sql = "SELECT oi_size,oi_description,oi_user," .
78 "oi_user_text,oi_timestamp,oi_archive_name FROM oldimage WHERE " .
79 "oi_name='" . wfStrencode( $this->mTitle->getDBkey() ) . "' " .
80 "ORDER BY oi_timestamp DESC";
81 $res = wfQuery( $sql, DB_READ, $fname );
83 while ( $line = wfFetchObject( $res ) ) {
84 $s .= $sk->imageHistoryLine( false, $line->oi_timestamp,
85 $line->oi_archive_name, $line->oi_user,
86 $line->oi_user_text, $line->oi_size, $line->oi_description );
88 $s .= $sk->endImageHistoryList();
89 $wgOut->addHTML( $s );
92 function imageLinks()
94 global $wgUser, $wgOut;
96 $wgOut->addHTML( "<h2>" . wfMsg( "imagelinks" ) . "</h2>\n" );
98 $sql = "SELECT cur_namespace,cur_title FROM imagelinks,cur WHERE il_to='" .
99 wfStrencode( $this->mTitle->getDBkey() ) . "' AND il_from=cur_id";
100 $res = wfQuery( $sql, DB_READ, "Article::imageLinks" );
102 if ( 0 == wfNumRows( $res ) ) {
103 $wgOut->addHtml( "<p>" . wfMsg( "nolinkstoimage" ) . "\n" );
104 return;
106 $wgOut->addHTML( "<p>" . wfMsg( "linkstoimage" ) . "\n<ul>" );
108 $sk = $wgUser->getSkin();
109 while ( $s = wfFetchObject( $res ) ) {
110 $name = Title::MakeTitle( $s->cur_namespace, $s->cur_title );
111 $link = $sk->makeKnownLinkObj( $name, "" );
112 $wgOut->addHTML( "<li>{$link}</li>\n" );
114 $wgOut->addHTML( "</ul>\n" );
117 function delete()
119 global $wgUser, $wgOut, $wgRequest;
121 $confirm = $wgRequest->getBool( 'wpConfirm' );
122 $image = $wgRequest->getVal( 'image' );
123 $oldimage = $wgRequest->getVal( 'oldimage' );
125 # Only sysops can delete images. Previously ordinary users could delete
126 # old revisions, but this is no longer the case.
127 if ( !$wgUser->isSysop() ) {
128 $wgOut->sysopRequired();
129 return;
131 if ( wfReadOnly() ) {
132 $wgOut->readOnlyPage();
133 return;
136 # Better double-check that it hasn't been deleted yet!
137 $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
138 if ( !is_null( $image ) ) {
139 if ( "" == trim( $image ) ) {
140 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
141 return;
145 # Deleting old images doesn't require confirmation
146 if ( !is_null( $oldimage ) || $confirm ) {
147 $this->doDelete();
148 return;
151 if ( !is_null( $image ) ) {
152 $q = "&image={$image}";
153 } else if ( !is_null( $oldimage ) ) {
154 $q = "&oldimage={$oldimage}";
155 } else {
156 $q = "";
158 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
161 function doDelete()
163 global $wgOut, $wgUser, $wgLang, $wgRequest;
164 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
165 $fname = "Article::doDelete";
167 $reason = $wgRequest->getVal( 'wpReason' );
168 $image = $wgRequest->getVal( 'image' );
169 $oldimage = $wgRequest->getVal( 'oldimage' );
171 if ( !is_null( $image ) ) {
172 $dest = wfImageDir( $image );
173 $archive = wfImageDir( $image );
174 if ( ! @unlink( "{$dest}/{$image}" ) ) {
175 $wgOut->fileDeleteError( "{$dest}/{$image}" );
176 return;
178 $sql = "DELETE FROM image WHERE img_name='" .
179 wfStrencode( $image ) . "'";
180 wfQuery( $sql, DB_WRITE, $fname );
182 $sql = "SELECT oi_archive_name FROM oldimage WHERE oi_name='" .
183 wfStrencode( $image ) . "'";
184 $res = wfQuery( $sql, DB_READ, $fname );
186 # Squid purging
187 if ( $wgUseSquid ) {
188 $urlArr = Array(
189 $wgInternalServer.wfImageUrl( $image )
191 wfPurgeSquidServers($urlArr);
195 $urlArr = Array();
196 while ( $s = wfFetchObject( $res ) ) {
197 $this->doDeleteOldImage( $s->oi_archive_name );
198 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name );
201 # Squid purging, part II
202 if ( $wgUseSquid ) {
203 /* this needs to be done after LinksUpdate */
204 $u = new SquidUpdate( $urlArr );
205 array_push( $wgDeferredUpdateList, $u );
208 $sql = "DELETE FROM oldimage WHERE oi_name='" .
209 wfStrencode( $image ) . "'";
210 wfQuery( $sql, DB_WRITE, $fname );
212 # Image itself is now gone, and database is cleaned.
213 # Now we remove the image description page.
215 $nt = Title::newFromText( $wgLang->getNsText( Namespace::getImage() ) . ":" . $image );
216 $article = new Article( $nt );
217 $article->doDeleteArticle( $reason ); # ignore errors
219 $deleted = $image;
220 } else if ( !is_null( $oldimage ) ) {
221 # Squid purging
222 if ( $wgUseSquid ) {
223 $urlArr = Array(
224 $wgInternalServer.wfImageArchiveUrl( $oldimage )
226 wfPurgeSquidServers($urlArr);
228 $this->doDeleteOldImage( $oldimage );
229 $sql = "DELETE FROM oldimage WHERE oi_archive_name='" .
230 wfStrencode( $oldimage ) . "'";
231 wfQuery( $sql, DB_WRITE, $fname );
233 $deleted = $oldimage;
234 } else {
235 $this->doDeleteArticle( $reason ); # ignore errors
236 $deleted = $this->mTitle->getPrefixedText();
238 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
239 $wgOut->setRobotpolicy( "noindex,nofollow" );
241 $sk = $wgUser->getSkin();
242 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
243 Namespace::getWikipedia() ) .
244 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
246 $text = wfMsg( "deletedtext", $deleted, $loglink );
248 $wgOut->addHTML( "<p>" . $text );
249 $wgOut->returnToMain( false );
252 function doDeleteOldImage( $oldimage )
254 global $wgOut;
256 $name = substr( $oldimage, 15 );
257 $archive = wfImageArchiveDir( $name );
258 if ( ! unlink( "{$archive}/{$oldimage}" ) ) {
259 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
263 function revert()
265 global $wgOut, $wgRequest;
266 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
268 $oldimage = $wgRequest->getText( 'oldimage' );
270 if ( strlen( $oldimage ) < 16 ) {
271 $wgOut->unexpectedValueError( "oldimage", $oldimage );
272 return;
274 if ( wfReadOnly() ) {
275 $wgOut->readOnlyPage();
276 return;
278 $name = substr( $oldimage, 15 );
280 $dest = wfImageDir( $name );
281 $archive = wfImageArchiveDir( $name );
282 $curfile = "{$dest}/{$name}";
284 if ( ! is_file( $curfile ) ) {
285 $wgOut->fileNotFoundError( $curfile );
286 return;
288 $oldver = wfTimestampNow() . "!{$name}";
289 $size = wfGetSQL( "oldimage", "oi_size", "oi_archive_name='" .
290 wfStrencode( $oldimage ) . "'" );
292 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
293 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
294 return;
296 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
297 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
299 wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
300 # Squid purging
301 if ( $wgUseSquid ) {
302 $urlArr = Array(
303 $wgInternalServer.wfImageArchiveUrl( $name ),
304 $wgInternalServer.wfImageUrl( $name )
306 wfPurgeSquidServers($urlArr);
309 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
310 $wgOut->setRobotpolicy( "noindex,nofollow" );
311 $wgOut->addHTML( wfMsg( "imagereverted" ) );
312 $wgOut->returnToMain( false );