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 && $wgUser->getOption( 'imagesize' ) != '' ) {
50 $max = $wgImageLimits[ intval( $wgUser->getOption( 'imagesize' ) ) ];
56 if ( $this->img
->exists() ) {
58 $sk = $wgUser->getSkin();
60 if ( $this->img
->getType() != '' ) {
62 $width = $this->img
->getWidth();
63 $height = $this->img
->getHeight();
64 $msg = wfMsg('showbigimage', $width, $height, intval( $this->img
->getSize()/1024 ) );
65 if ( $width > $maxWidth && $wgUseImageResize ) {
66 $anchoropen = "<a href=\"{$url}\">";
67 $anchorclose = "<br>{$msg}</a>";
69 $url = $this->img
->createThumb( $maxWidth );
70 $height = floor( $height * $maxWidth / $width );
73 if ( $height > $maxHeight && $wgUseImageResize ) {
74 $anchoropen = "<a href=\"{$url}\">";
75 $anchorclose = "<br>{$msg}</a>";
77 $width = floor( $width * $maxHeight / $height );
79 $url = $this->img
->createThumb( $width );
81 $s = "<div class=\"fullImageLink\">" . $anchoropen .
82 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
83 htmlspecialchars( $wgRequest->getVal( 'image' ) )."\" />" . $anchorclose . "</div>";
85 $s = "<div class=\"fullMedia\">".$sk->makeMediaLink($this->img
->getName(),"")."</div>";
87 $wgOut->addHTML( $s );
88 if($this->img
->fromSharedDirectory
) {
89 $sharedtext="<div class=\"sharedUploadNotice\">" . wfMsg("sharedupload");
90 if($wgRepositoryBaseUrl) {
91 $sharedtext .= " ". wfMsg("shareduploadwiki",$wgRepositoryBaseUrl . urlencode($this->mTitle
->getDBkey()));
93 $sharedtext.="</div>";
94 $wgOut->addWikiText($sharedtext);
99 function closeShowImage()
105 * If the page we've just displayed is in the "Image" namespace,
106 * we follow it with an upload history of the image and its usage.
108 function imageHistory()
110 global $wgUser, $wgOut;
112 $sk = $wgUser->getSkin();
114 $line = $this->img
->nextHistoryLine();
117 $list =& new ImageHistoryList( $sk );
118 $s = $list->beginImageHistoryList() .
119 $list->imageHistoryLine( true, $line->img_timestamp
,
120 $this->mTitle
->getDBkey(), $line->img_user
,
121 $line->img_user_text
, $line->img_size
, $line->img_description
);
123 while ( $line = $this->img
->nextHistoryLine() ) {
124 $s .= $list->imageHistoryLine( false, $line->img_timestamp
,
125 $line->oi_archive_name
, $line->img_user
,
126 $line->img_user_text
, $line->img_size
, $line->img_description
);
128 $s .= $list->endImageHistoryList();
130 $wgOut->addHTML( $s );
133 function imageLinks()
135 global $wgUser, $wgOut;
137 $wgOut->addHTML( '<h2>' . wfMsg( 'imagelinks' ) . "</h2>\n" );
139 $dbr =& wfGetDB( DB_SLAVE
);
140 $cur = $dbr->tableName( 'cur' );
141 $imagelinks = $dbr->tableName( 'imagelinks' );
143 $sql = "SELECT cur_namespace,cur_title FROM $imagelinks,$cur WHERE il_to=" .
144 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=cur_id"
145 . " LIMIT 500"; # quickie emergency brake
146 $res = $dbr->query( $sql, DB_SLAVE
, "Article::imageLinks" );
148 if ( 0 == $dbr->numRows( $res ) ) {
149 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
152 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
154 $sk = $wgUser->getSkin();
155 while ( $s = $dbr->fetchObject( $res ) ) {
156 $name = Title
::MakeTitle( $s->cur_namespace
, $s->cur_title
);
157 $link = $sk->makeKnownLinkObj( $name, "" );
158 $wgOut->addHTML( "<li>{$link}</li>\n" );
160 $wgOut->addHTML( "</ul>\n" );
165 global $wgUser, $wgOut, $wgRequest;
167 $confirm = $wgRequest->getBool( 'wpConfirm' );
168 $image = $wgRequest->getVal( 'image' );
169 $oldimage = $wgRequest->getVal( 'oldimage' );
171 # Only sysops can delete images. Previously ordinary users could delete
172 # old revisions, but this is no longer the case.
173 if ( !$wgUser->isAllowed('delete') ) {
174 $wgOut->sysopRequired();
177 if ( wfReadOnly() ) {
178 $wgOut->readOnlyPage();
182 # Better double-check that it hasn't been deleted yet!
183 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
184 if ( !is_null( $image ) ) {
185 if ( '' == trim( $image ) ) {
186 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
191 # Deleting old images doesn't require confirmation
192 if ( !is_null( $oldimage ) ||
$confirm ) {
197 if ( !is_null( $image ) ) {
198 $q = '&image=' . urlencode( $image );
199 } else if ( !is_null( $oldimage ) ) {
200 $q = '&oldimage=' . urlencode( $oldimage );
204 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
209 global $wgOut, $wgUser, $wgContLang, $wgRequest;
210 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
211 $fname = 'ImagePage::doDelete';
213 $reason = $wgRequest->getVal( 'wpReason' );
214 $image = $wgRequest->getVal( 'image' );
215 $oldimage = $wgRequest->getVal( 'oldimage' );
217 $dbw =& wfGetDB( DB_MASTER
);
219 if ( !is_null( $oldimage ) ) {
223 $wgInternalServer.wfImageArchiveUrl( $oldimage )
225 wfPurgeSquidServers($urlArr);
227 $this->doDeleteOldImage( $oldimage );
228 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
229 $deleted = $oldimage;
231 if ( is_null ( $image ) ) {
232 $image = $this->mTitle
->getDBkey();
234 $dest = wfImageDir( $image );
235 $archive = wfImageDir( $image );
237 # Delete the image file if it exists; due to sync problems
238 # or manual trimming sometimes the file will be missing.
239 $targetFile = "{$dest}/{$image}";
240 if( file_exists( $targetFile ) && ! @unlink
( $targetFile ) ) {
241 # If the deletion operation actually failed, bug out:
242 $wgOut->fileDeleteError( $targetFile );
245 $dbw->delete( 'image', array( 'img_name' => $image ) );
246 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
251 $wgInternalServer . Image
::wfImageUrl( $image )
253 wfPurgeSquidServers($urlArr);
258 while ( $s = $dbw->fetchObject( $res ) ) {
259 $this->doDeleteOldImage( $s->oi_archive_name
);
260 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name
);
263 # Squid purging, part II
265 /* this needs to be done after LinksUpdate */
266 $u = new SquidUpdate( $urlArr );
267 array_push( $wgDeferredUpdateList, $u );
270 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
272 # Image itself is now gone, and database is cleaned.
273 # Now we remove the image description page.
275 $nt = Title
::newFromText( $wgContLang->getNsText( Namespace::getImage() ) . ":" . $image );
276 $article = new Article( $nt );
277 $article->doDeleteArticle( $reason ); # ignore errors
282 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
283 $wgOut->setRobotpolicy( 'noindex,nofollow' );
285 $sk = $wgUser->getSkin();
286 $loglink = $sk->makeKnownLink( $wgContLang->getNsText(
287 Namespace::getWikipedia() ) .
288 ':' . wfMsg( 'dellogpage' ), wfMsg( 'deletionlog' ) );
290 $text = wfMsg( 'deletedtext', $deleted, $loglink );
292 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
293 $wgOut->returnToMain( false );
296 function doDeleteOldImage( $oldimage )
300 $name = substr( $oldimage, 15 );
301 $archive = wfImageArchiveDir( $name );
303 # Delete the image if it exists. Sometimes the file will be missing
304 # due to manual intervention or weird sync problems; treat that
305 # condition gracefully and continue to delete the database entry.
306 # Also some records may end up with an empty oi_archive_name field
307 # if the original file was missing when a new upload was made;
308 # don't try to delete the directory then!
310 $targetFile = "{$archive}/{$oldimage}";
311 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink
( $targetFile ) ) {
312 # If we actually have a file and can't delete it, throw an error.
313 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
316 $log = new LogPage( 'delete' );
317 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
323 global $wgOut, $wgRequest;
324 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
326 $oldimage = $wgRequest->getText( 'oldimage' );
327 if ( strlen( $oldimage ) < 16 ) {
328 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
331 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
332 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
336 if ( wfReadOnly() ) {
337 $wgOut->readOnlyPage();
340 if ( ! $this->mTitle
->userCanEdit() ) {
341 $wgOut->sysopRequired();
344 $name = substr( $oldimage, 15 );
346 $dest = wfImageDir( $name );
347 $archive = wfImageArchiveDir( $name );
348 $curfile = "{$dest}/{$name}";
350 if ( ! is_file( $curfile ) ) {
351 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
354 $oldver = wfTimestampNow() . "!{$name}";
356 $dbr =& wfGetDB( DB_SLAVE
);
357 $size = $dbr->selectField( 'oldimage', 'oi_size', 'oi_archive_name=\'' .
358 $dbr->strencode( $oldimage ) . "'" );
360 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
361 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
364 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
365 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
367 wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
371 $wgInternalServer.wfImageArchiveUrl( $name ),
372 $wgInternalServer . Image
::wfImageUrl( $name )
374 wfPurgeSquidServers($urlArr);
377 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
378 $wgOut->setRobotpolicy( 'noindex,nofollow' );
379 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
380 $wgOut->returnToMain( false );
384 class ImageHistoryList
{
385 function ImageHistoryList( &$skin ) {
386 $this->skin
=& $skin;
389 function beginImageHistoryList() {
390 $s = "\n<h2>" . wfMsg( 'imghistory' ) . "</h2>\n" .
391 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
395 function endImageHistoryList() {
400 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
401 global $wgUser, $wgLang, $wgContLang, $wgTitle;
403 $datetime = $wgLang->timeanddate( $timestamp, true );
404 $del = wfMsg( 'deleteimg' );
405 $delall = wfMsg( 'deleteimgcompletely' );
406 $cur = wfMsg( 'cur' );
409 $url = Image
::wfImageUrl( $img );
411 if ( $wgUser->isAllowed('delete') ) {
412 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
414 $style = $this->skin
->getInternalLinkAttributes( $link, $delall );
416 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
421 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
422 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
423 $rlink = $this->skin
->makeKnownLink( $wgTitle->getPrefixedText(),
424 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
426 $dlink = $this->skin
->makeKnownLink( $wgTitle->getPrefixedText(),
427 $del, 'action=delete&oldimage=' . urlencode( $img ) );
429 # Having live active links for non-logged in users
430 # means that bots and spiders crawling our site can
431 # inadvertently change content. Baaaad idea.
432 $rlink = wfMsg( 'revertimg' );
437 $userlink = $usertext;
439 $userlink = $this->skin
->makeLink( $wgContLang->getNsText( Namespace::getUser() ) .
440 ':'.$usertext, $usertext );
442 $nbytes = wfMsg( 'nbytes', $size );
443 $style = $this->skin
->getInternalLinkAttributes( $url, $datetime );
445 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
446 . " . . {$userlink} ({$nbytes})";
448 if ( '' != $description && '*' != $description ) {
449 $sk=$wgUser->getSkin();
450 $s .= $wgContLang->emphasize(' (' . $sk->formatComment($description,$wgTitle) . ')');