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
24 $wgOut->setArticleBodyOnly(true);
25 $wgOut->addWikitext($this->getContent(true));
29 global $wgUseExternalEditor, $wgOut, $wgShowEXIF;
31 $this->img
= new Image( $this->mTitle
);
33 if( $this->mTitle
->getNamespace() == NS_IMAGE
) {
34 if ($wgShowEXIF && $this->img
->exists()) {
35 $exif = $this->img
->getExifData();
36 $showmeta = count($exif) ?
true : false;
42 if ($this->img
->exists())
43 $wgOut->addHTML($this->showTOC($showmeta));
45 $this->openShowImage();
47 $wgOut->addWikiText($this->makeMetadataTable($exif));
49 # No need to display noarticletext, we use our own message, output in openShowImage()
50 if( $this->getID() ) {
53 # Just need to set the right headers
54 $wgOut->setArticleFlag( true );
55 $wgOut->setRobotpolicy( 'index,follow' );
56 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
57 $wgOut->addMetaTags();
61 $this->closeShowImage();
62 $this->imageHistory();
74 * @param bool $metadata Whether or not to show the metadata link
77 function showTOC( $metadata ) {
79 $r = '<ul id="filetoc">
80 <li><a href="#file">' . $wgLang->getNsText( NS_IMAGE
) . '</a></li>' .
81 ($metadata ?
'<li><a href="#metadata">' . wfMsg( 'metadata' ) . '</a></li>' : '') . '
82 <li><a href="#filehistory">' . wfMsg( 'imghistory' ) . '</a></li>
83 <li><a href="#filelinks">' . wfMsg( 'imagelinks' ) . '</a></li>
89 * Make a table with metadata to be shown in the output page.
93 * @param array $exif The array containing the EXIF data
96 function makeMetadataTable( $exif ) {
97 $r = "{| class=metadata align=right width=250px\n";
98 $r .= '|+ id=metadata | '. htmlspecialchars( wfMsg( 'metadata' ) ) . "\n";
99 foreach( $exif as $k => $v ) {
100 $tag = strtolower( $k );
101 $r .= "! class=$tag |" . wfMsg( "exif-$tag" ) . "\n";
102 $r .= "| class=$tag |" . htmlspecialchars( $v ) . "\n";
105 return substr($r, 0, -3) . '|}';
109 * Overloading Article's getContent method.
110 * Omit noarticletext if sharedupload
112 * @param $noredir If true, do not follow redirects
114 function getContent( $noredir )
116 if ( $this->img
&& $this->img
->fromSharedDirectory
&& 0 == $this->getID() ) {
119 return Article
::getContent( $noredir );
122 function openShowImage()
124 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
125 $wgUseImageResize, $wgRepositoryBaseUrl,
126 $wgUseExternalEditor, $wgServer, $wgFetchCommonsDescriptions;
127 $full_url = $this->img
->getViewURL();
131 if( $wgUser->getOption( 'imagesize' ) == '' ) {
132 $sizeSel = User
::getDefaultOption( 'imagesize' );
134 $sizeSel = IntVal( $wgUser->getOption( 'imagesize' ) );
136 if( !isset( $wgImageLimits[$sizeSel] ) ) {
137 $sizeSel = User
::getDefaultOption( 'imagesize' );
139 $max = $wgImageLimits[$sizeSel];
141 $maxHeight = $max[1];
142 $sk = $wgUser->getSkin();
144 if ( $this->img
->exists() ) {
145 if($this->img
->fromSharedDirectory
146 && $wgRepositoryBaseUrl && $wgFetchCommonsDescriptions) {
147 $this->printSharedImageText();
152 $width = $this->img
->getWidth();
153 $height = $this->img
->getHeight();
156 if ( $this->img
->allowInlineDisplay() and $width and $height) {
159 # "Download high res version" link below the image
160 $msg = wfMsg('showbigimage', $width, $height, intval( $this->img
->getSize()/1024 ) );
161 if ( $width > $maxWidth ) {
162 $height = floor( $height * $maxWidth / $width );
165 if ( $height > $maxHeight ) {
166 $width = floor( $width * $maxHeight / $height );
167 $height = $maxHeight;
169 if ( !$this->img
->mustRender()
170 && ( $width != $this->img
->getWidth() ||
$height != $this->img
->getHeight() ) ) {
171 if( $wgUseImageResize ) {
172 $thumbnail = $this->img
->getThumbnail( $width );
173 if ( $thumbnail == null ) {
176 $url = $thumbnail->getUrl();
179 # No resize ability? Show the full image, but scale
180 # it down in the browser so it fits on the page.
183 $anchoropen = "<a href=\"{$full_url}\">";
184 $anchorclose = "</a><br />\n$anchoropen{$msg}</a>";
187 $showLink = $this->img
->mustRender();
189 $wgOut->addHTML( '<div class="fullImageLink" id="file">' . $anchoropen .
190 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
191 htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>' );
193 #if direct link is allowed but it's not a renderable image, show an icon.
194 if ($this->img
->isSafeFile()) {
195 $icon= $this->img
->iconThumb();
197 $wgOut->addHTML( '<div class="fullImageLink" id="file"><a href="' . $full_url . '">' .
207 $s= $sk->makeMediaLink( $this->img
->getName(), '', '', true );
208 $info= wfMsg( 'fileinfo', ceil($this->img
->getSize()/1024.0), $this->img
->getMimeType() );
210 if (!$this->img
->isSafeFile()) {
211 $wgOut->addHTML("<div class=\"fullMedia\">");
212 $wgOut->addHTML("<span class=\"dangerousLink\">");
214 $wgOut->addHTML("</span>");
216 $wgOut->addHTML("<span class=\"fileInfo\"> (");
217 $wgOut->addWikiText( $info, false );
218 $wgOut->addHTML(")</span>");
219 $wgOut->addHTML("</div>");
221 #this should be formated a little nicer. Is CSS sufficient?
222 $wgOut->addHTML("<div class=\"mediaWarning\">");
223 $wgOut->addWikiText( wfMsg( 'mediawarning' ) );
224 $wgOut->addHTML('</div>');
227 $wgOut->addHTML("<div class=\"fullMedia\">");
230 $wgOut->addHTML("<span class=\"fileInfo\"> (");
231 $wgOut->addWikiText( $info, false );
232 $wgOut->addHTML(")</span>");
234 $wgOut->addHTML("</div>");
238 if($this->img
->fromSharedDirectory
) {
239 $this->printSharedImageText();
242 # Image does not exist
243 $wgOut->addWikiText( wfMsg( 'noimage', $this->getUploadUrl() ) );
247 function printSharedImageText() {
248 global $wgRepositoryBaseUrl, $wgFetchCommonsDescriptions, $wgOut;
250 $url = $wgRepositoryBaseUrl . urlencode($this->mTitle
->getDBkey());
251 $sharedtext = "<div class='sharedUploadNotice'>" . wfMsg("sharedupload");
252 if ($wgRepositoryBaseUrl) {
253 $sharedtext .= " " . wfMsg("shareduploadwiki", $url);
255 $sharedtext .= "</div>";
256 $wgOut->addWikiText($sharedtext);
258 if ($wgRepositoryBaseUrl && $wgFetchCommonsDescriptions) {
259 $ur = ini_set('allow_url_fopen', true);
260 $text = @file_get_contents
($url);
261 ini_set('allow_url_fopen', $ur);
263 $wgOut->addHTML($text);
267 function getUploadUrl() {
269 $uploadTitle = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
270 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img
->getName() ) );
274 function uploadLinksBox()
276 global $wgUser,$wgOut;
277 $sk = $wgUser->getSkin();
278 $wgOut->addHTML( '<br /><ul><li>' );
279 $wgOut->addWikiText( '<div>'. wfMsg( 'uploadnewversion', $this->getUploadUrl() ) .'</div>' );
280 $wgOut->addHTML( '</li><li>' );
281 $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle
,
282 wfMsg( 'edit-externally' ), "action=edit&externaledit=true&mode=file" ) );
283 $wgOut->addWikiText( '<div>' . wfMsg('edit-externally-help') . '</div>' );
284 $wgOut->addHTML( '</li></ul>' );
287 function closeShowImage()
294 * If the page we've just displayed is in the "Image" namespace,
295 * we follow it with an upload history of the image and its usage.
297 function imageHistory()
299 global $wgUser, $wgOut, $wgUseExternalEditor;
301 $sk = $wgUser->getSkin();
303 $line = $this->img
->nextHistoryLine();
306 $list =& new ImageHistoryList( $sk );
307 $s = $list->beginImageHistoryList() .
308 $list->imageHistoryLine( true, $line->img_timestamp
,
309 $this->mTitle
->getDBkey(), $line->img_user
,
310 $line->img_user_text
, $line->img_size
, $line->img_description
);
312 while ( $line = $this->img
->nextHistoryLine() ) {
313 $s .= $list->imageHistoryLine( false, $line->img_timestamp
,
314 $line->oi_archive_name
, $line->img_user
,
315 $line->img_user_text
, $line->img_size
, $line->img_description
);
317 $s .= $list->endImageHistoryList();
319 $wgOut->addHTML( $s );
321 # Exist check because we don't want to show this on pages where an image
322 # doesn't exist along with the noimage message, that would suck. -ævar
323 if( $wgUseExternalEditor && $this->img
->exists() ) {
324 $this->uploadLinksBox();
329 function imageLinks()
331 global $wgUser, $wgOut;
333 $wgOut->addHTML( '<h2 id="filelinks">' . wfMsg( 'imagelinks' ) . "</h2>\n" );
335 $dbr =& wfGetDB( DB_SLAVE
);
336 $page = $dbr->tableName( 'page' );
337 $imagelinks = $dbr->tableName( 'imagelinks' );
339 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
340 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id"
341 . " LIMIT 500"; # quickie emergency brake
342 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
344 if ( 0 == $dbr->numRows( $res ) ) {
345 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
348 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
350 $sk = $wgUser->getSkin();
351 while ( $s = $dbr->fetchObject( $res ) ) {
352 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
353 $link = $sk->makeKnownLinkObj( $name, "" );
354 $wgOut->addHTML( "<li>{$link}</li>\n" );
356 $wgOut->addHTML( "</ul>\n" );
361 global $wgUser, $wgOut, $wgRequest;
363 $confirm = $wgRequest->getBool( 'wpConfirmB' );
364 $image = $wgRequest->getVal( 'image' );
365 $oldimage = $wgRequest->getVal( 'oldimage' );
367 # Only sysops can delete images. Previously ordinary users could delete
368 # old revisions, but this is no longer the case.
369 if ( !$wgUser->isAllowed('delete') ) {
370 $wgOut->sysopRequired();
373 if ( $wgUser->isBlocked() ) {
374 return $this->blockedIPpage();
376 if ( wfReadOnly() ) {
377 $wgOut->readOnlyPage();
381 # Better double-check that it hasn't been deleted yet!
382 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
383 if ( ( !is_null( $image ) )
384 && ( '' == trim( $image ) ) ) {
385 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
389 $this->img
= new Image( $this->mTitle
);
391 # Deleting old images doesn't require confirmation
392 if ( !is_null( $oldimage ) ||
$confirm ) {
393 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
396 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
401 if ( !is_null( $image ) ) {
402 $q = '&image=' . urlencode( $image );
403 } else if ( !is_null( $oldimage ) ) {
404 $q = '&oldimage=' . urlencode( $oldimage );
408 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
413 global $wgOut, $wgUser, $wgContLang, $wgRequest;
414 global $wgUseSquid, $wgInternalServer, $wgPostCommitUpdateList;
415 $fname = 'ImagePage::doDelete';
417 $reason = $wgRequest->getVal( 'wpReason' );
418 $oldimage = $wgRequest->getVal( 'oldimage' );
420 $dbw =& wfGetDB( DB_MASTER
);
422 if ( !is_null( $oldimage ) ) {
423 if ( strlen( $oldimage ) < 16 ) {
424 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
427 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
428 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
432 # Invalidate description page cache
433 $this->mTitle
->invalidateCache();
438 $wgInternalServer.wfImageArchiveUrl( $oldimage ),
439 $wgInternalServer.$this->mTitle
->getFullURL()
441 wfPurgeSquidServers($urlArr);
443 $this->doDeleteOldImage( $oldimage );
444 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
445 $deleted = $oldimage;
447 $image = $this->mTitle
->getDBkey();
448 $dest = wfImageDir( $image );
449 $archive = wfImageDir( $image );
451 # Delete the image file if it exists; due to sync problems
452 # or manual trimming sometimes the file will be missing.
453 $targetFile = "{$dest}/{$image}";
454 if( file_exists( $targetFile ) && ! @unlink
( $targetFile ) ) {
455 # If the deletion operation actually failed, bug out:
456 $wgOut->fileDeleteError( $targetFile );
459 $dbw->delete( 'image', array( 'img_name' => $image ) );
460 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
462 # Purge archive URLs from the squid
464 while ( $s = $dbw->fetchObject( $res ) ) {
465 $this->doDeleteOldImage( $s->oi_archive_name
);
466 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name
);
469 # And also the HTML of all pages using this image
470 $linksTo = $this->img
->getLinksTo();
472 $u = SquidUpdate
::newFromTitles( $linksTo, $urlArr );
473 array_push( $wgPostCommitUpdateList, $u );
476 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
478 # Image itself is now gone, and database is cleaned.
479 # Now we remove the image description page.
481 $article = new Article( $this->mTitle
);
482 $article->doDeleteArticle( $reason ); # ignore errors
484 # Invalidate parser cache and client cache for pages using this image
485 # This is left until relatively late to reduce lock time
486 Title
::touchArray( $linksTo );
488 /* Delete thumbnails and refresh image metadata cache */
489 $this->img
->purgeCache();
495 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
496 $wgOut->setRobotpolicy( 'noindex,nofollow' );
498 $loglink = '[[Special:Log/delete|' . wfMsg( 'deletionlog' ) . ']]';
499 $text = wfMsg( 'deletedtext', $deleted, $loglink );
501 $wgOut->addWikiText( $text );
503 $wgOut->returnToMain( false, $this->mTitle
->getPrefixedText() );
506 function doDeleteOldImage( $oldimage )
510 $name = substr( $oldimage, 15 );
511 $archive = wfImageArchiveDir( $name );
513 # Delete the image if it exists. Sometimes the file will be missing
514 # due to manual intervention or weird sync problems; treat that
515 # condition gracefully and continue to delete the database entry.
516 # Also some records may end up with an empty oi_archive_name field
517 # if the original file was missing when a new upload was made;
518 # don't try to delete the directory then!
520 $targetFile = "{$archive}/{$oldimage}";
521 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink
( $targetFile ) ) {
522 # If we actually have a file and can't delete it, throw an error.
523 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
526 $log = new LogPage( 'delete' );
527 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
533 global $wgOut, $wgRequest, $wgUser;
534 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
536 $oldimage = $wgRequest->getText( 'oldimage' );
537 if ( strlen( $oldimage ) < 16 ) {
538 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
541 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
542 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
546 if ( wfReadOnly() ) {
547 $wgOut->readOnlyPage();
550 if( $wgUser->isAnon() ) {
551 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
554 if ( ! $this->mTitle
->userCanEdit() ) {
555 $wgOut->sysopRequired();
558 if ( $wgUser->isBlocked() ) {
559 return $this->blockedIPpage();
561 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
562 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
565 $name = substr( $oldimage, 15 );
567 $dest = wfImageDir( $name );
568 $archive = wfImageArchiveDir( $name );
569 $curfile = "{$dest}/{$name}";
571 if ( ! is_file( $curfile ) ) {
572 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
575 $oldver = wfTimestampNow() . "!{$name}";
577 $dbr =& wfGetDB( DB_SLAVE
);
578 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
580 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
581 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
584 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
585 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
588 # Record upload and update metadata cache
589 $img = Image
::newFromName( $name );
590 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
592 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
593 $wgOut->setRobotpolicy( 'noindex,nofollow' );
594 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
596 $descTitle = $img->getTitle();
597 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
600 function blockedIPpage() {
601 require_once( 'EditPage.php' );
602 $edit = new EditPage( $this );
603 return $edit->blockedIPpage();
612 class ImageHistoryList
{
613 function ImageHistoryList( &$skin ) {
614 $this->skin
=& $skin;
617 function beginImageHistoryList() {
618 $s = "\n<h2 id=\"filehistory\">" . wfMsg( 'imghistory' ) . "</h2>\n" .
619 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
623 function endImageHistoryList() {
628 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
629 global $wgUser, $wgLang, $wgContLang, $wgTitle;
631 $datetime = $wgLang->timeanddate( $timestamp, true );
632 $del = wfMsg( 'deleteimg' );
633 $delall = wfMsg( 'deleteimgcompletely' );
634 $cur = wfMsg( 'cur' );
637 $url = Image
::imageUrl( $img );
639 if ( $wgUser->isAllowed('delete') ) {
640 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
642 $style = $this->skin
->getInternalLinkAttributes( $link, $delall );
644 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
649 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
650 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
651 $token = urlencode( $wgUser->editToken( $img ) );
652 $rlink = $this->skin
->makeKnownLinkObj( $wgTitle,
653 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
654 urlencode( $img ) . "&wpEditToken=$token" );
655 $dlink = $this->skin
->makeKnownLinkObj( $wgTitle,
656 $del, 'action=delete&oldimage=' . urlencode( $img ) .
657 "&wpEditToken=$token" );
659 # Having live active links for non-logged in users
660 # means that bots and spiders crawling our site can
661 # inadvertently change content. Baaaad idea.
662 $rlink = wfMsg( 'revertimg' );
667 $userlink = $usertext;
669 $userlink = $this->skin
->makeLinkObj(
670 Title
::makeTitle( NS_USER
, $usertext ),
673 $nbytes = wfMsg( 'nbytes', $size );
674 $style = $this->skin
->getInternalLinkAttributes( $url, $datetime );
676 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
677 . " . . {$userlink} ({$nbytes})";
679 $s .= $this->skin
->commentBlock( $description, $wgTitle );