9 require_once( 'Image.php' );
12 * Special handling for image description pages
15 class ImagePage
extends Article
{
17 /* private */ var $img; // Image object this page is shown for. Initilaized in openShowImage, not
18 // available in doDelete etc.
21 if ( Namespace::getImage() == $this->mTitle
->getNamespace() ) {
22 $this->openShowImage();
27 # If the article we've just shown is in the "Image" namespace,
28 # follow it with the history list and link list for the image
31 if ( Namespace::getImage() == $this->mTitle
->getNamespace() ) {
32 $this->closeShowImage();
33 $this->imageHistory();
38 function openShowImage()
40 global $wgOut, $wgUser, $wgImageLimits, $wgRequest, $wgUseImageResize;
41 $this->img
= Image
::newFromTitle( $this->mTitle
);
42 $url = $this->img
->getViewURL();
45 if ( $wgUseImageResize && $wgUser->getOption( 'imagesize' ) != '' ) {
46 $max = $wgImageLimits[ intval( $wgUser->getOption( 'imagesize' ) ) ];
52 if ( $this->img
->exists() ) {
54 $sk = $wgUser->getSkin();
56 if ( $this->img
->getType() != '' ) {
58 $width = $this->img
->getWidth();
59 $height = $this->img
->getHeight();
60 $msg = wfMsg('showbigimage', $width, $height, intval( $this->img
->getSize()/1024 ) );
61 if ( $width > $maxWidth && $wgUseImageResize ) {
62 $anchoropen = "<a href=\"{$url}\">";
63 $anchorclose = "<br>{$msg}</a>";
65 $url = $this->img
->createThumb( $maxWidth );
66 $height = floor( $height * $maxWidth / $width );
69 if ( $height > $maxHeight && $wgUseImageResize ) {
70 $anchoropen = "<a href=\"{$url}\">";
71 $anchorclose = "<br>{$msg}</a>";
73 $width = floor( $width * $maxHeight / $height );
75 $url = $this->img
->createThumb( $width );
77 $s = "<div class=\"fullImageLink\">" . $anchoropen .
78 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
79 htmlspecialchars( $wgRequest->getVal( 'image' ) )."\" />" . $anchorclose . "</div>";
81 $s = "<div class=\"fullMedia\">".$sk->makeMediaLink($this->img
->getName(),"")."</div>";
83 if($this->img
->fromSharedDirectory
) {
84 $s.="<div class=\"sharedUploadNotice\">".wfMsg("sharedupload")."</div>";
86 $wgOut->addHTML( $s );
90 function closeShowImage()
96 * If the page we've just displayed is in the "Image" namespace,
97 * we follow it with an upload history of the image and its usage.
99 function imageHistory()
101 global $wgUser, $wgOut;
103 $sk = $wgUser->getSkin();
105 $line = $this->img
->nextHistoryLine();
108 $s = $sk->beginImageHistoryList() .
109 $sk->imageHistoryLine( true, $line->img_timestamp
,
110 $this->mTitle
->getDBkey(), $line->img_user
,
111 $line->img_user_text
, $line->img_size
, $line->img_description
);
113 while ( $line = $this->img
->nextHistoryLine() ) {
114 $s .= $sk->imageHistoryLine( false, $line->img_timestamp
,
115 $line->oi_archive_name
, $line->img_user
,
116 $line->img_user_text
, $line->img_size
, $line->img_description
);
118 $s .= $sk->endImageHistoryList();
120 $wgOut->addHTML( $s );
123 function imageLinks()
125 global $wgUser, $wgOut;
127 $wgOut->addHTML( '<h2>' . wfMsg( 'imagelinks' ) . "</h2>\n" );
129 $dbr =& wfGetDB( DB_SLAVE
);
130 $cur = $dbr->tableName( 'cur' );
131 $imagelinks = $dbr->tableName( 'imagelinks' );
133 $sql = "SELECT cur_namespace,cur_title FROM $imagelinks,$cur WHERE il_to=" .
134 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=cur_id";
135 $res = $dbr->query( $sql, DB_SLAVE
, "Article::imageLinks" );
137 if ( 0 == $dbr->numRows( $res ) ) {
138 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
141 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
143 $sk = $wgUser->getSkin();
144 while ( $s = $dbr->fetchObject( $res ) ) {
145 $name = Title
::MakeTitle( $s->cur_namespace
, $s->cur_title
);
146 $link = $sk->makeKnownLinkObj( $name, "" );
147 $wgOut->addHTML( "<li>{$link}</li>\n" );
149 $wgOut->addHTML( "</ul>\n" );
154 global $wgUser, $wgOut, $wgRequest;
156 $confirm = $wgRequest->getBool( 'wpConfirm' );
157 $image = $wgRequest->getVal( 'image' );
158 $oldimage = $wgRequest->getVal( 'oldimage' );
160 # Only sysops can delete images. Previously ordinary users could delete
161 # old revisions, but this is no longer the case.
162 if ( !$wgUser->isSysop() ) {
163 $wgOut->sysopRequired();
166 if ( wfReadOnly() ) {
167 $wgOut->readOnlyPage();
171 # Better double-check that it hasn't been deleted yet!
172 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
173 if ( !is_null( $image ) ) {
174 if ( '' == trim( $image ) ) {
175 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
180 # Deleting old images doesn't require confirmation
181 if ( !is_null( $oldimage ) ||
$confirm ) {
186 if ( !is_null( $image ) ) {
187 $q = '&image=' . urlencode( $image );
188 } else if ( !is_null( $oldimage ) ) {
189 $q = '&oldimage=' . urlencode( $oldimage );
193 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
198 global $wgOut, $wgUser, $wgContLang, $wgRequest;
199 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
200 $fname = 'ImagePage::doDelete';
202 $reason = $wgRequest->getVal( 'wpReason' );
203 $image = $wgRequest->getVal( 'image' );
204 $oldimage = $wgRequest->getVal( 'oldimage' );
206 $dbw =& wfGetDB( DB_MASTER
);
208 if ( !is_null( $oldimage ) ) {
212 $wgInternalServer.wfImageArchiveUrl( $oldimage )
214 wfPurgeSquidServers($urlArr);
216 $this->doDeleteOldImage( $oldimage );
217 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
218 $deleted = $oldimage;
220 if ( is_null ( $image ) ) {
221 $image = $this->mTitle
->getDBkey();
223 $dest = wfImageDir( $image );
224 $archive = wfImageDir( $image );
226 # Delete the image file if it exists; due to sync problems
227 # or manual trimming sometimes the file will be missing.
228 $targetFile = "{$dest}/{$image}";
229 if( file_exists( $targetFile ) && ! @unlink
( $targetFile ) ) {
230 # If the deletion operation actually failed, bug out:
231 $wgOut->fileDeleteError( $targetFile );
234 $dbw->delete( 'image', array( 'img_name' => $image ) );
235 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
240 $wgInternalServer . Image
::wfImageUrl( $image )
242 wfPurgeSquidServers($urlArr);
247 while ( $s = $dbw->fetchObject( $res ) ) {
248 $this->doDeleteOldImage( $s->oi_archive_name
);
249 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name
);
252 # Squid purging, part II
254 /* this needs to be done after LinksUpdate */
255 $u = new SquidUpdate( $urlArr );
256 array_push( $wgDeferredUpdateList, $u );
259 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
261 # Image itself is now gone, and database is cleaned.
262 # Now we remove the image description page.
264 $nt = Title
::newFromText( $wgContLang->getNsText( Namespace::getImage() ) . ":" . $image );
265 $article = new Article( $nt );
266 $article->doDeleteArticle( $reason ); # ignore errors
271 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
272 $wgOut->setRobotpolicy( 'noindex,nofollow' );
274 $sk = $wgUser->getSkin();
275 $loglink = $sk->makeKnownLink( $wgContLang->getNsText(
276 Namespace::getWikipedia() ) .
277 ':' . wfMsg( 'dellogpage' ), wfMsg( 'deletionlog' ) );
279 $text = wfMsg( 'deletedtext', $deleted, $loglink );
281 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
282 $wgOut->returnToMain( false );
285 function doDeleteOldImage( $oldimage )
289 $name = substr( $oldimage, 15 );
290 $archive = wfImageArchiveDir( $name );
292 # Delete the image if it exists. Sometimes the file will be missing
293 # due to manual intervention or weird sync problems; treat that
294 # condition gracefully and continue to delete the database entry.
295 # Also some records may end up with an empty oi_archive_name field
296 # if the original file was missing when a new upload was made;
297 # don't try to delete the directory then!
299 $targetFile = "{$archive}/{$oldimage}";
300 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink
( $targetFile ) ) {
301 # If we actually have a file and can't delete it, throw an error.
302 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
305 $log = new LogPage( 'delete' );
306 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
312 global $wgOut, $wgRequest;
313 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
315 $oldimage = $wgRequest->getText( 'oldimage' );
316 if ( strlen( $oldimage ) < 16 ) {
317 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
320 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
321 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
325 if ( wfReadOnly() ) {
326 $wgOut->readOnlyPage();
329 if ( ! $this->mTitle
->userCanEdit() ) {
330 $wgOut->sysopRequired();
333 $name = substr( $oldimage, 15 );
335 $dest = wfImageDir( $name );
336 $archive = wfImageArchiveDir( $name );
337 $curfile = "{$dest}/{$name}";
339 if ( ! is_file( $curfile ) ) {
340 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
343 $oldver = wfTimestampNow() . "!{$name}";
345 $dbr =& wfGetDB( DB_SLAVE
);
346 $size = $dbr->selectField( 'oldimage', 'oi_size', 'oi_archive_name=\'' .
347 $dbr->strencode( $oldimage ) . "'" );
349 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
350 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
353 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
354 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
356 wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
360 $wgInternalServer.wfImageArchiveUrl( $name ),
361 $wgInternalServer . Image
::wfImageUrl( $name )
363 wfPurgeSquidServers($urlArr);
366 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
367 $wgOut->setRobotpolicy( 'noindex,nofollow' );
368 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
369 $wgOut->returnToMain( false );