Some documentation. Let's talk about it in wikitech-l.
[mediawiki.git] / includes / ImagePage.php
blob42c219e0ed96cf54499085a02b7ebe94fa8ab1d6
1 <?php
2 /**
4 */
6 /**
8 */
9 require_once( 'Image.php' );
11 /**
12 * Special handling for image description pages
14 class ImagePage extends Article {
16 /* private */ var $img; // Image object this page is shown for. Initilaized in openShowImage, not
17 // available in doDelete etc.
19 function view() {
20 if ( Namespace::getImage() == $this->mTitle->getNamespace() ) {
21 $this->openShowImage();
24 Article::view();
26 # If the article we've just shown is in the "Image" namespace,
27 # follow it with the history list and link list for the image
28 # it describes.
30 if ( Namespace::getImage() == $this->mTitle->getNamespace() ) {
31 $this->closeShowImage();
32 $this->imageHistory();
33 $this->imageLinks();
37 function openShowImage()
39 global $wgOut, $wgUser, $wgRequest, $wgMaxImageWidth, $wgUseImageResize;
40 $this->img = Image::newFromTitle( $this->mTitle );
41 $url = $this->img->getUrl();
42 $anchoropen = '';
43 $anchorclose = '';
46 if ( $this->img->exists() ) {
48 $sk = $wgUser->getSkin();
50 if ( $this->img->getType() != '' ) {
51 # image
52 $width = $this->img->getWidth();
53 $height = $this->img->getHeight();
54 if ( $width > $wgMaxImageWidth && $wgUseImageResize ) {
55 $anchoropen = "<a href=\"{$url}\">";
56 $anchorclose = '</a>';
57 $url=$this->img->createThumb( $wgMaxImageWidth );
58 $height = floor( $height * $wgMaxImageWidth / $width );
59 $width = $wgMaxImageWidth;
61 $s = "<div class=\"fullImage\">" . $anchoropen .
62 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
63 $wgRequest->getVal( 'image' )."\" />" . $anchorclose . "</div>";
64 } else {
65 $s = "<div class=\"fullMedia\">".$sk->makeMediaLink($this->img->getName(),"")."</div>";
67 $wgOut->addHTML( $s );
71 function closeShowImage()
73 # For overloading
76 /**
77 * If the page we've just displayed is in the "Image" namespace,
78 * we follow it with an upload history of the image and its usage.
80 function imageHistory()
82 global $wgUser, $wgOut;
84 $sk = $wgUser->getSkin();
86 $line = $this->img->nextHistoryLine();
88 if ( $line ) {
89 $s = $sk->beginImageHistoryList() .
90 $sk->imageHistoryLine( true, $line->img_timestamp,
91 $this->mTitle->getDBkey(), $line->img_user,
92 $line->img_user_text, $line->img_size, $line->img_description );
94 while ( $line = $this->img->nextHistoryLine() ) {
95 $s .= $sk->imageHistoryLine( false, $line->img_timestamp,
96 $line->oi_archive_name, $line->img_user,
97 $line->img_user_text, $line->img_size, $line->img_description );
99 $s .= $sk->endImageHistoryList();
100 } else { $s=''; }
101 $wgOut->addHTML( $s );
104 function imageLinks()
106 global $wgUser, $wgOut;
108 $wgOut->addHTML( '<h2>' . wfMsg( 'imagelinks' ) . "</h2>\n" );
110 $dbr =& wfGetDB( DB_SLAVE );
111 $cur = $dbr->tableName( 'cur' );
112 $imagelinks = $dbr->tableName( 'imagelinks' );
114 $sql = "SELECT cur_namespace,cur_title FROM $imagelinks,$cur WHERE il_to=" .
115 $dbr->addQuotes( $this->mTitle->getDBkey() ) . " AND il_from=cur_id";
116 $res = $dbr->query( $sql, DB_SLAVE, "Article::imageLinks" );
118 if ( 0 == $dbr->numRows( $res ) ) {
119 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
120 return;
122 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
124 $sk = $wgUser->getSkin();
125 while ( $s = $dbr->fetchObject( $res ) ) {
126 $name = Title::MakeTitle( $s->cur_namespace, $s->cur_title );
127 $link = $sk->makeKnownLinkObj( $name, "" );
128 $wgOut->addHTML( "<li>{$link}</li>\n" );
130 $wgOut->addHTML( "</ul>\n" );
133 function delete()
135 global $wgUser, $wgOut, $wgRequest;
137 $confirm = $wgRequest->getBool( 'wpConfirm' );
138 $image = $wgRequest->getVal( 'image' );
139 $oldimage = $wgRequest->getVal( 'oldimage' );
141 # Only sysops can delete images. Previously ordinary users could delete
142 # old revisions, but this is no longer the case.
143 if ( !$wgUser->isSysop() ) {
144 $wgOut->sysopRequired();
145 return;
147 if ( wfReadOnly() ) {
148 $wgOut->readOnlyPage();
149 return;
152 # Better double-check that it hasn't been deleted yet!
153 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
154 if ( !is_null( $image ) ) {
155 if ( '' == trim( $image ) ) {
156 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
157 return;
161 # Deleting old images doesn't require confirmation
162 if ( !is_null( $oldimage ) || $confirm ) {
163 $this->doDelete();
164 return;
167 if ( !is_null( $image ) ) {
168 $q = '&image=' . urlencode( $image );
169 } else if ( !is_null( $oldimage ) ) {
170 $q = '&oldimage=' . urlencode( $oldimage );
171 } else {
172 $q = '';
174 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
177 function doDelete()
179 global $wgOut, $wgUser, $wgLang, $wgRequest;
180 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
181 $fname = 'ImagePage::doDelete';
183 $reason = $wgRequest->getVal( 'wpReason' );
184 $image = $wgRequest->getVal( 'image' );
185 $oldimage = $wgRequest->getVal( 'oldimage' );
187 $dbw =& wfGetDB( DB_MASTER );
189 if ( !is_null( $oldimage ) ) {
190 # Squid purging
191 if ( $wgUseSquid ) {
192 $urlArr = Array(
193 $wgInternalServer.wfImageArchiveUrl( $oldimage )
195 wfPurgeSquidServers($urlArr);
197 $this->doDeleteOldImage( $oldimage );
198 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
199 $deleted = $oldimage;
200 } else {
201 if ( is_null ( $image ) ) {
202 $image = $this->mTitle->getDBkey();
204 $dest = wfImageDir( $image );
205 $archive = wfImageDir( $image );
206 if ( ! @unlink( "{$dest}/{$image}" ) ) {
207 $wgOut->fileDeleteError( "{$dest}/{$image}" );
208 return;
210 $dbw->delete( 'image', array( 'img_name' => $image ) );
211 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
213 # Squid purging
214 if ( $wgUseSquid ) {
215 $urlArr = Array(
216 $wgInternalServer . Image::wfImageUrl( $image )
218 wfPurgeSquidServers($urlArr);
222 $urlArr = Array();
223 while ( $s = $dbw->fetchObject( $res ) ) {
224 $this->doDeleteOldImage( $s->oi_archive_name );
225 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name );
228 # Squid purging, part II
229 if ( $wgUseSquid ) {
230 /* this needs to be done after LinksUpdate */
231 $u = new SquidUpdate( $urlArr );
232 array_push( $wgDeferredUpdateList, $u );
235 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
237 # Image itself is now gone, and database is cleaned.
238 # Now we remove the image description page.
240 $nt = Title::newFromText( $wgLang->getNsText( Namespace::getImage() ) . ":" . $image );
241 $article = new Article( $nt );
242 $article->doDeleteArticle( $reason ); # ignore errors
244 $deleted = $image;
247 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
248 $wgOut->setRobotpolicy( 'noindex,nofollow' );
250 $sk = $wgUser->getSkin();
251 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
252 Namespace::getWikipedia() ) .
253 ':' . wfMsg( 'dellogpage' ), wfMsg( 'deletionlog' ) );
255 $text = wfMsg( 'deletedtext', $deleted, $loglink );
257 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
258 $wgOut->returnToMain( false );
261 function doDeleteOldImage( $oldimage )
263 global $wgOut;
265 $name = substr( $oldimage, 15 );
266 $archive = wfImageArchiveDir( $name );
267 if ( ! @unlink( "{$archive}/{$oldimage}" ) ) {
268 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
269 } else {
270 # Log the deletion
271 $log = new LogPage( 'delete' );
272 $log->addEntry( 'delete', $this->mTitle, wfMsg('deletedrevision',$oldimage) );
276 function revert()
278 global $wgOut, $wgRequest;
279 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
281 $oldimage = $wgRequest->getText( 'oldimage' );
283 if ( strlen( $oldimage ) < 16 ) {
284 $wgOut->unexpectedValueError( 'oldimage', $oldimage );
285 return;
287 if ( wfReadOnly() ) {
288 $wgOut->readOnlyPage();
289 return;
291 if ( ! $this->mTitle->userCanEdit() ) {
292 $wgOut->sysopRequired();
293 return;
295 $name = substr( $oldimage, 15 );
297 $dest = wfImageDir( $name );
298 $archive = wfImageArchiveDir( $name );
299 $curfile = "{$dest}/{$name}";
301 if ( ! is_file( $curfile ) ) {
302 $wgOut->fileNotFoundError( $curfile );
303 return;
305 $oldver = wfTimestampNow() . "!{$name}";
307 $dbr =& wfGetDB( DB_SLAVE );
308 $size = $dbr->getField( 'oldimage', 'oi_size', 'oi_archive_name=\'' .
309 $dbr->strencode( $oldimage ) . "'" );
311 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
312 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
313 return;
315 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
316 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
318 wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
319 # Squid purging
320 if ( $wgUseSquid ) {
321 $urlArr = Array(
322 $wgInternalServer.wfImageArchiveUrl( $name ),
323 $wgInternalServer . Image::wfImageUrl( $name )
325 wfPurgeSquidServers($urlArr);
328 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
329 $wgOut->setRobotpolicy( 'noindex,nofollow' );
330 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
331 $wgOut->returnToMain( false );