9 if( !defined( 'MEDIAWIKI' ) )
12 require_once( 'Image.php' );
15 * Special handling for image description pages
18 class ImagePage
extends Article
{
20 /* private */ var $img; // Image object this page is shown for. Initilaized in openShowImage, not
21 // available in doDelete etc.
24 if ( Namespace::getImage() == $this->mTitle
->getNamespace() ) {
25 $this->openShowImage();
30 # If the article we've just shown is in the "Image" namespace,
31 # follow it with the history list and link list for the image
34 if ( Namespace::getImage() == $this->mTitle
->getNamespace() ) {
35 $this->closeShowImage();
36 $this->imageHistory();
41 function openShowImage()
43 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
44 $wgUseImageResize, $wgRepositoryBaseUrl;
45 $this->img
= Image
::newFromTitle( $this->mTitle
);
46 $url = $this->img
->getViewURL();
49 if ( $wgUseImageResize ) {
50 if( $wgUser->getOption( 'imagesize' ) == '' ) {
51 $sizeSel = User
::getDefaultOption( 'imagesize' );
53 $sizeSel = IntVal( $wgUser->getOption( 'imagesize' ) );
55 if( !isset( $wgImageLimits[$sizeSel] ) ) {
56 $sizeSel = User
::getDefaultOption( 'imagesize' );
58 $max = $wgImageLimits[$sizeSel];
64 if ( $this->img
->exists() ) {
66 $sk = $wgUser->getSkin();
68 if ( $this->img
->getType() != '' ) {
70 $width = $this->img
->getWidth();
71 $height = $this->img
->getHeight();
72 $msg = wfMsg('showbigimage', $width, $height, intval( $this->img
->getSize()/1024 ) );
73 if ( $width > $maxWidth && $wgUseImageResize ) {
74 $anchoropen = "<a href=\"{$url}\">";
75 $anchorclose = "<br>{$msg}</a>";
77 $url = $this->img
->createThumb( $maxWidth );
78 $height = floor( $height * $maxWidth / $width );
81 if ( $height > $maxHeight && $wgUseImageResize ) {
82 $anchoropen = "<a href=\"{$url}\">";
83 $anchorclose = "<br>{$msg}</a>";
85 $width = floor( $width * $maxHeight / $height );
87 $url = $this->img
->createThumb( $width );
89 $s = "<div class=\"fullImageLink\">" . $anchoropen .
90 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
91 htmlspecialchars( $wgRequest->getVal( 'image' ) )."\" />" . $anchorclose . "</div>";
93 $s = "<div class=\"fullMedia\">".$sk->makeMediaLink($this->img
->getName(),"")."</div>";
95 $wgOut->addHTML( $s );
96 if($this->img
->fromSharedDirectory
) {
97 $sharedtext="<div class=\"sharedUploadNotice\">" . wfMsg("sharedupload");
98 if($wgRepositoryBaseUrl) {
99 $sharedtext .= " ". wfMsg("shareduploadwiki",$wgRepositoryBaseUrl . urlencode($this->mTitle
->getDBkey()));
101 $sharedtext.="</div>";
102 $wgOut->addWikiText($sharedtext);
107 function closeShowImage()
113 * If the page we've just displayed is in the "Image" namespace,
114 * we follow it with an upload history of the image and its usage.
116 function imageHistory()
118 global $wgUser, $wgOut;
120 $sk = $wgUser->getSkin();
122 $line = $this->img
->nextHistoryLine();
125 $list =& new ImageHistoryList( $sk );
126 $s = $list->beginImageHistoryList() .
127 $list->imageHistoryLine( true, $line->img_timestamp
,
128 $this->mTitle
->getDBkey(), $line->img_user
,
129 $line->img_user_text
, $line->img_size
, $line->img_description
);
131 while ( $line = $this->img
->nextHistoryLine() ) {
132 $s .= $list->imageHistoryLine( false, $line->img_timestamp
,
133 $line->oi_archive_name
, $line->img_user
,
134 $line->img_user_text
, $line->img_size
, $line->img_description
);
136 $s .= $list->endImageHistoryList();
138 $wgOut->addHTML( $s );
141 function imageLinks()
143 global $wgUser, $wgOut;
145 $wgOut->addHTML( '<h2>' . wfMsg( 'imagelinks' ) . "</h2>\n" );
147 $dbr =& wfGetDB( DB_SLAVE
);
148 $cur = $dbr->tableName( 'cur' );
149 $imagelinks = $dbr->tableName( 'imagelinks' );
151 $sql = "SELECT cur_namespace,cur_title FROM $imagelinks,$cur WHERE il_to=" .
152 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=cur_id"
153 . " LIMIT 500"; # quickie emergency brake
154 $res = $dbr->query( $sql, DB_SLAVE
, "Article::imageLinks" );
156 if ( 0 == $dbr->numRows( $res ) ) {
157 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
160 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
162 $sk = $wgUser->getSkin();
163 while ( $s = $dbr->fetchObject( $res ) ) {
164 $name = Title
::MakeTitle( $s->cur_namespace
, $s->cur_title
);
165 $link = $sk->makeKnownLinkObj( $name, "" );
166 $wgOut->addHTML( "<li>{$link}</li>\n" );
168 $wgOut->addHTML( "</ul>\n" );
173 global $wgUser, $wgOut, $wgRequest;
175 $confirm = $wgRequest->getBool( 'wpConfirm' );
176 $image = $wgRequest->getVal( 'image' );
177 $oldimage = $wgRequest->getVal( 'oldimage' );
179 # Only sysops can delete images. Previously ordinary users could delete
180 # old revisions, but this is no longer the case.
181 if ( !$wgUser->isAllowed('delete') ) {
182 $wgOut->sysopRequired();
185 if ( wfReadOnly() ) {
186 $wgOut->readOnlyPage();
190 # Better double-check that it hasn't been deleted yet!
191 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
192 if ( !is_null( $image ) ) {
193 if ( '' == trim( $image ) ) {
194 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
199 # Deleting old images doesn't require confirmation
200 if ( !is_null( $oldimage ) ||
$confirm ) {
205 if ( !is_null( $image ) ) {
206 $q = '&image=' . urlencode( $image );
207 } else if ( !is_null( $oldimage ) ) {
208 $q = '&oldimage=' . urlencode( $oldimage );
212 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
217 global $wgOut, $wgUser, $wgContLang, $wgRequest;
218 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
219 $fname = 'ImagePage::doDelete';
221 $reason = $wgRequest->getVal( 'wpReason' );
222 $image = $wgRequest->getVal( 'image' );
223 $oldimage = $wgRequest->getVal( 'oldimage' );
225 $dbw =& wfGetDB( DB_MASTER
);
227 if ( !is_null( $oldimage ) ) {
231 $wgInternalServer.wfImageArchiveUrl( $oldimage )
233 wfPurgeSquidServers($urlArr);
235 $this->doDeleteOldImage( $oldimage );
236 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
237 $deleted = $oldimage;
239 if ( is_null ( $image ) ) {
240 $image = $this->mTitle
->getDBkey();
242 $dest = wfImageDir( $image );
243 $archive = wfImageDir( $image );
245 # Delete the image file if it exists; due to sync problems
246 # or manual trimming sometimes the file will be missing.
247 $targetFile = "{$dest}/{$image}";
248 if( file_exists( $targetFile ) && ! @unlink
( $targetFile ) ) {
249 # If the deletion operation actually failed, bug out:
250 $wgOut->fileDeleteError( $targetFile );
253 $dbw->delete( 'image', array( 'img_name' => $image ) );
254 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
259 $wgInternalServer . Image
::wfImageUrl( $image )
261 wfPurgeSquidServers($urlArr);
266 while ( $s = $dbw->fetchObject( $res ) ) {
267 $this->doDeleteOldImage( $s->oi_archive_name
);
268 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name
);
271 # Squid purging, part II
273 /* this needs to be done after LinksUpdate */
274 $u = new SquidUpdate( $urlArr );
275 array_push( $wgDeferredUpdateList, $u );
278 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
280 # Image itself is now gone, and database is cleaned.
281 # Now we remove the image description page.
283 $nt = Title
::newFromText( $wgContLang->getNsText( Namespace::getImage() ) . ":" . $image );
284 $article = new Article( $nt );
285 $article->doDeleteArticle( $reason ); # ignore errors
290 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
291 $wgOut->setRobotpolicy( 'noindex,nofollow' );
293 $sk = $wgUser->getSkin();
294 $loglink = $sk->makeKnownLink( $wgContLang->getNsText(
295 Namespace::getWikipedia() ) .
296 ':' . wfMsg( 'dellogpage' ), wfMsg( 'deletionlog' ) );
298 $text = wfMsg( 'deletedtext', $deleted, $loglink );
300 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
301 $wgOut->returnToMain( false );
304 function doDeleteOldImage( $oldimage )
308 $name = substr( $oldimage, 15 );
309 $archive = wfImageArchiveDir( $name );
311 # Delete the image if it exists. Sometimes the file will be missing
312 # due to manual intervention or weird sync problems; treat that
313 # condition gracefully and continue to delete the database entry.
314 # Also some records may end up with an empty oi_archive_name field
315 # if the original file was missing when a new upload was made;
316 # don't try to delete the directory then!
318 $targetFile = "{$archive}/{$oldimage}";
319 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink
( $targetFile ) ) {
320 # If we actually have a file and can't delete it, throw an error.
321 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
324 $log = new LogPage( 'delete' );
325 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
331 global $wgOut, $wgRequest;
332 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
334 $oldimage = $wgRequest->getText( 'oldimage' );
335 if ( strlen( $oldimage ) < 16 ) {
336 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
339 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
340 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
344 if ( wfReadOnly() ) {
345 $wgOut->readOnlyPage();
348 if ( ! $this->mTitle
->userCanEdit() ) {
349 $wgOut->sysopRequired();
352 $name = substr( $oldimage, 15 );
354 $dest = wfImageDir( $name );
355 $archive = wfImageArchiveDir( $name );
356 $curfile = "{$dest}/{$name}";
358 if ( ! is_file( $curfile ) ) {
359 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
362 $oldver = wfTimestampNow() . "!{$name}";
364 $dbr =& wfGetDB( DB_SLAVE
);
365 $size = $dbr->selectField( 'oldimage', 'oi_size', 'oi_archive_name=\'' .
366 $dbr->strencode( $oldimage ) . "'" );
368 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
369 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
372 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
373 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
375 wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
379 $wgInternalServer.wfImageArchiveUrl( $name ),
380 $wgInternalServer . Image
::wfImageUrl( $name )
382 wfPurgeSquidServers($urlArr);
385 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
386 $wgOut->setRobotpolicy( 'noindex,nofollow' );
387 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
388 $wgOut->returnToMain( false );
392 class ImageHistoryList
{
393 function ImageHistoryList( &$skin ) {
394 $this->skin
=& $skin;
397 function beginImageHistoryList() {
398 $s = "\n<h2>" . wfMsg( 'imghistory' ) . "</h2>\n" .
399 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
403 function endImageHistoryList() {
408 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
409 global $wgUser, $wgLang, $wgContLang, $wgTitle;
411 $datetime = $wgLang->timeanddate( $timestamp, true );
412 $del = wfMsg( 'deleteimg' );
413 $delall = wfMsg( 'deleteimgcompletely' );
414 $cur = wfMsg( 'cur' );
417 $url = Image
::wfImageUrl( $img );
419 if ( $wgUser->isAllowed('delete') ) {
420 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
422 $style = $this->skin
->getInternalLinkAttributes( $link, $delall );
424 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
429 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
430 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
431 $rlink = $this->skin
->makeKnownLink( $wgTitle->getPrefixedText(),
432 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
434 $dlink = $this->skin
->makeKnownLink( $wgTitle->getPrefixedText(),
435 $del, 'action=delete&oldimage=' . urlencode( $img ) );
437 # Having live active links for non-logged in users
438 # means that bots and spiders crawling our site can
439 # inadvertently change content. Baaaad idea.
440 $rlink = wfMsg( 'revertimg' );
445 $userlink = $usertext;
447 $userlink = $this->skin
->makeLink( $wgContLang->getNsText( Namespace::getUser() ) .
448 ':'.$usertext, $usertext );
450 $nbytes = wfMsg( 'nbytes', $size );
451 $style = $this->skin
->getInternalLinkAttributes( $url, $datetime );
453 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
454 . " . . {$userlink} ({$nbytes})";
456 if ( '' != $description && '*' != $description ) {
457 $sk=$wgUser->getSkin();
458 $s .= $wgContLang->emphasize(' (' . $sk->formatComment($description,$wgTitle) . ')');