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