comment update, stupid cvs commit bot test..
[mediawiki.git] / includes / ImagePage.php
blob6e8d72b6c20ef299909dee0131cbab2518183e70
1 <?php
3 /*
4 Special handling for image description pages
5 */
7 class ImagePage extends Article {
9 /* private */ var $img; // Image object this page is shown for. Initilaized in openShowImage, not
10 // available in doDelete etc.
12 function view() {
13 if ( Namespace::getImage() == $this->mTitle->getNamespace() ) {
14 $this->openShowImage();
17 Article::view();
19 # If the article we've just shown is in the "Image" namespace,
20 # follow it with the history list and link list for the image
21 # it describes.
23 if ( Namespace::getImage() == $this->mTitle->getNamespace() ) {
24 $this->closeShowImage();
25 $this->imageHistory();
26 $this->imageLinks();
30 function openShowImage()
32 global $wgOut, $wgUser,$wgRequest;
33 $this->img = Image::newFromTitle( $this->mTitle );
34 $url = $this->img->getUrl();
36 if ( $this->img->exists() ) {
38 $sk = $wgUser->getSkin();
40 if ( $this->img->getType() != "" ) {
41 # image
42 $s = "<div class=\"fullImage\">" .
43 "<img src=\"{$url}\" width=\"" . $this->img->getWidth() . "\" height=\"" . $this->img->getHeight() .
44 "\" alt=\"".$wgRequest->getVal( 'image' )."\" /></div>";
45 } else {
46 $s = "<div class=\"fullMedia\">".$sk->makeMediaLink($this->img->getName(),"")."</div>";
48 $wgOut->addHTML( $s );
52 function closeShowImage()
54 # For overloading
57 # If the page we've just displayed is in the "Image" namespace,
58 # we follow it with an upload history of the image and its usage.
60 function imageHistory()
62 global $wgUser, $wgOut;
64 $sk = $wgUser->getSkin();
65 $s = $sk->beginImageHistoryList();
67 $line = $this->img->nextHistoryLine();
69 $s .= $sk->imageHistoryLine( true, $line->img_timestamp,
70 $this->mTitle->getDBkey(), $line->img_user,
71 $line->img_user_text, $line->img_size, $line->img_description );
73 while ( $line = $this->img->nextHistoryLine() ) {
74 $s .= $sk->imageHistoryLine( false, $line->img_timestamp,
75 $line->oi_archive_name, $line->img_user,
76 $line->img_user_text, $line->img_size, $line->img_description );
78 $s .= $sk->endImageHistoryList();
79 $wgOut->addHTML( $s );
82 function imageLinks()
84 global $wgUser, $wgOut;
86 $wgOut->addHTML( "<h2>" . wfMsg( "imagelinks" ) . "</h2>\n" );
88 $sql = "SELECT cur_namespace,cur_title FROM imagelinks,cur WHERE il_to='" .
89 wfStrencode( $this->mTitle->getDBkey() ) . "' AND il_from=cur_id";
90 $res = wfQuery( $sql, DB_READ, "Article::imageLinks" );
92 if ( 0 == wfNumRows( $res ) ) {
93 $wgOut->addHtml( "<p>" . wfMsg( "nolinkstoimage" ) . "</p>\n" );
94 return;
96 $wgOut->addHTML( "<p>" . wfMsg( "linkstoimage" ) . "</p>\n<ul>" );
98 $sk = $wgUser->getSkin();
99 while ( $s = wfFetchObject( $res ) ) {
100 $name = Title::MakeTitle( $s->cur_namespace, $s->cur_title );
101 $link = $sk->makeKnownLinkObj( $name, "" );
102 $wgOut->addHTML( "<li>{$link}</li>\n" );
104 $wgOut->addHTML( "</ul>\n" );
107 function delete()
109 global $wgUser, $wgOut, $wgRequest;
111 $confirm = $wgRequest->getBool( 'wpConfirm' );
112 $image = $wgRequest->getVal( 'image' );
113 $oldimage = $wgRequest->getVal( 'oldimage' );
115 # Only sysops can delete images. Previously ordinary users could delete
116 # old revisions, but this is no longer the case.
117 if ( !$wgUser->isSysop() ) {
118 $wgOut->sysopRequired();
119 return;
121 if ( wfReadOnly() ) {
122 $wgOut->readOnlyPage();
123 return;
126 # Better double-check that it hasn't been deleted yet!
127 $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
128 if ( !is_null( $image ) ) {
129 if ( "" == trim( $image ) ) {
130 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
131 return;
135 # Deleting old images doesn't require confirmation
136 if ( !is_null( $oldimage ) || $confirm ) {
137 $this->doDelete();
138 return;
141 if ( !is_null( $image ) ) {
142 $q = "&image=" . urlencode( $image );
143 } else if ( !is_null( $oldimage ) ) {
144 $q = "&oldimage=" . urlencode( $oldimage );
145 } else {
146 $q = "";
148 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
151 function doDelete()
153 global $wgOut, $wgUser, $wgLang, $wgRequest;
154 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
155 $fname = "Article::doDelete";
157 $reason = $wgRequest->getVal( 'wpReason' );
158 $image = $wgRequest->getVal( 'image' );
159 $oldimage = $wgRequest->getVal( 'oldimage' );
161 if ( !is_null( $image ) ) {
162 $dest = wfImageDir( $image );
163 $archive = wfImageDir( $image );
164 if ( ! @unlink( "{$dest}/{$image}" ) ) {
165 $wgOut->fileDeleteError( "{$dest}/{$image}" );
166 return;
168 $sql = "DELETE FROM image WHERE img_name='" .
169 wfStrencode( $image ) . "'";
170 wfQuery( $sql, DB_WRITE, $fname );
172 $sql = "SELECT oi_archive_name FROM oldimage WHERE oi_name='" .
173 wfStrencode( $image ) . "'";
174 $res = wfQuery( $sql, DB_READ, $fname );
176 # Squid purging
177 if ( $wgUseSquid ) {
178 $urlArr = Array(
179 $wgInternalServer . Image::wfImageUrl( $image )
181 wfPurgeSquidServers($urlArr);
185 $urlArr = Array();
186 while ( $s = wfFetchObject( $res ) ) {
187 $this->doDeleteOldImage( $s->oi_archive_name );
188 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name );
191 # Squid purging, part II
192 if ( $wgUseSquid ) {
193 /* this needs to be done after LinksUpdate */
194 $u = new SquidUpdate( $urlArr );
195 array_push( $wgDeferredUpdateList, $u );
198 $sql = "DELETE FROM oldimage WHERE oi_name='" .
199 wfStrencode( $image ) . "'";
200 wfQuery( $sql, DB_WRITE, $fname );
202 # Image itself is now gone, and database is cleaned.
203 # Now we remove the image description page.
205 $nt = Title::newFromText( $wgLang->getNsText( Namespace::getImage() ) . ":" . $image );
206 $article = new Article( $nt );
207 $article->doDeleteArticle( $reason ); # ignore errors
209 $deleted = $image;
210 } else if ( !is_null( $oldimage ) ) {
211 # Squid purging
212 if ( $wgUseSquid ) {
213 $urlArr = Array(
214 $wgInternalServer.wfImageArchiveUrl( $oldimage )
216 wfPurgeSquidServers($urlArr);
218 $this->doDeleteOldImage( $oldimage );
219 $sql = "DELETE FROM oldimage WHERE oi_archive_name='" .
220 wfStrencode( $oldimage ) . "'";
221 wfQuery( $sql, DB_WRITE, $fname );
223 $deleted = $oldimage;
224 } else {
225 $this->doDeleteArticle( $reason ); # ignore errors
226 $deleted = $this->mTitle->getPrefixedText();
228 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
229 $wgOut->setRobotpolicy( "noindex,nofollow" );
231 $sk = $wgUser->getSkin();
232 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
233 Namespace::getWikipedia() ) .
234 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
236 $text = wfMsg( "deletedtext", $deleted, $loglink );
238 $wgOut->addHTML( "<p>" . $text . "</p>\n" );
239 $wgOut->returnToMain( false );
242 function doDeleteOldImage( $oldimage )
244 global $wgOut;
246 $name = substr( $oldimage, 15 );
247 $archive = wfImageArchiveDir( $name );
248 if ( ! unlink( "{$archive}/{$oldimage}" ) ) {
249 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
253 function revert()
255 global $wgOut, $wgRequest;
256 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
258 $oldimage = $wgRequest->getText( 'oldimage' );
260 if ( strlen( $oldimage ) < 16 ) {
261 $wgOut->unexpectedValueError( "oldimage", $oldimage );
262 return;
264 if ( wfReadOnly() ) {
265 $wgOut->readOnlyPage();
266 return;
268 $name = substr( $oldimage, 15 );
270 $dest = wfImageDir( $name );
271 $archive = wfImageArchiveDir( $name );
272 $curfile = "{$dest}/{$name}";
274 if ( ! is_file( $curfile ) ) {
275 $wgOut->fileNotFoundError( $curfile );
276 return;
278 $oldver = wfTimestampNow() . "!{$name}";
279 $size = wfGetSQL( "oldimage", "oi_size", "oi_archive_name='" .
280 wfStrencode( $oldimage ) . "'" );
282 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
283 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
284 return;
286 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
287 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
289 wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
290 # Squid purging
291 if ( $wgUseSquid ) {
292 $urlArr = Array(
293 $wgInternalServer.wfImageArchiveUrl( $name ),
294 $wgInternalServer . Image::wfImageUrl( $name )
296 wfPurgeSquidServers($urlArr);
299 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
300 $wgOut->setRobotpolicy( "noindex,nofollow" );
301 $wgOut->addHTML( wfMsg( "imagereverted" ) );
302 $wgOut->returnToMain( false );