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
21 var $mExtraDescription = false;
25 $wgOut->setArticleBodyOnly(true);
26 $wgOut->addWikitext($this->getContent(true));
30 global $wgUseExternalEditor, $wgOut, $wgShowEXIF;
32 $this->img
= new Image( $this->mTitle
);
34 if( $this->mTitle
->getNamespace() == NS_IMAGE
) {
35 if ($wgShowEXIF && $this->img
->exists()) {
36 $exif = $this->img
->getExifData();
37 $showmeta = count($exif) ?
true : false;
43 if ($this->img
->exists())
44 $wgOut->addHTML($this->showTOC($showmeta));
46 $this->openShowImage();
48 $wgOut->addWikiText($this->makeMetadataTable($exif));
50 # No need to display noarticletext, we use our own message, output in openShowImage()
51 if( $this->getID() ) {
54 # Just need to set the right headers
55 $wgOut->setArticleFlag( true );
56 $wgOut->setRobotpolicy( 'index,follow' );
57 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
58 $wgOut->addMetaTags();
62 if ($this->mExtraDescription
) {
63 $fol = wfMsg('shareddescriptionfollows');
65 $wgOut->addWikiText(wfMsg('shareddescriptionfollows'));
66 $wgOut->addHTML($this->mExtraDescription
);
69 $this->closeShowImage();
70 $this->imageHistory();
82 * @param bool $metadata Whether or not to show the metadata link
85 function showTOC( $metadata ) {
87 $r = '<ul id="filetoc">
88 <li><a href="#file">' . $wgLang->getNsText( NS_IMAGE
) . '</a></li>' .
89 ($metadata ?
'<li><a href="#metadata">' . wfMsg( 'metadata' ) . '</a></li>' : '') . '
90 <li><a href="#filehistory">' . wfMsg( 'imghistory' ) . '</a></li>
91 <li><a href="#filelinks">' . wfMsg( 'imagelinks' ) . '</a></li>
97 * Make a table with metadata to be shown in the output page.
101 * @param array $exif The array containing the EXIF data
104 function makeMetadataTable( $exif ) {
105 $r = "{| class=metadata align=right width=250px\n";
106 $r .= '|+ id=metadata | '. htmlspecialchars( wfMsg( 'metadata' ) ) . "\n";
107 foreach( $exif as $k => $v ) {
108 $tag = strtolower( $k );
109 $r .= "! class=$tag |" . wfMsg( "exif-$tag" ) . "\n";
110 $r .= "| class=$tag |" . htmlspecialchars( $v ) . "\n";
113 return substr($r, 0, -3) . '|}';
117 * Overloading Article's getContent method.
118 * Omit noarticletext if sharedupload
120 * @param $noredir If true, do not follow redirects
122 function getContent( $noredir )
124 if ( $this->img
&& $this->img
->fromSharedDirectory
&& 0 == $this->getID() ) {
127 return Article
::getContent( $noredir );
130 function openShowImage()
132 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
133 $wgUseImageResize, $wgRepositoryBaseUrl,
134 $wgUseExternalEditor, $wgServer, $wgFetchCommonsDescriptions;
135 $full_url = $this->img
->getViewURL();
139 if( $wgUser->getOption( 'imagesize' ) == '' ) {
140 $sizeSel = User
::getDefaultOption( 'imagesize' );
142 $sizeSel = IntVal( $wgUser->getOption( 'imagesize' ) );
144 if( !isset( $wgImageLimits[$sizeSel] ) ) {
145 $sizeSel = User
::getDefaultOption( 'imagesize' );
147 $max = $wgImageLimits[$sizeSel];
149 $maxHeight = $max[1];
150 $sk = $wgUser->getSkin();
152 if ( $this->img
->exists() ) {
154 $width = $this->img
->getWidth();
155 $height = $this->img
->getHeight();
158 if ( $this->img
->allowInlineDisplay() and $width and $height) {
161 # "Download high res version" link below the image
162 $msg = wfMsg('showbigimage', $width, $height, intval( $this->img
->getSize()/1024 ) );
163 if ( $width > $maxWidth ) {
164 $height = floor( $height * $maxWidth / $width );
167 if ( $height > $maxHeight ) {
168 $width = floor( $width * $maxHeight / $height );
169 $height = $maxHeight;
171 if ( !$this->img
->mustRender()
172 && ( $width != $this->img
->getWidth() ||
$height != $this->img
->getHeight() ) ) {
173 if( $wgUseImageResize ) {
174 $thumbnail = $this->img
->getThumbnail( $width );
175 if ( $thumbnail == null ) {
178 $url = $thumbnail->getUrl();
181 # No resize ability? Show the full image, but scale
182 # it down in the browser so it fits on the page.
185 $anchoropen = "<a href=\"{$full_url}\">";
186 $anchorclose = "</a><br />\n$anchoropen{$msg}</a>";
189 $showLink = $this->img
->mustRender();
191 $wgOut->addHTML( '<div class="fullImageLink" id="file">' . $anchoropen .
192 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
193 htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>' );
195 #if direct link is allowed but it's not a renderable image, show an icon.
196 if ($this->img
->isSafeFile()) {
197 $icon= $this->img
->iconThumb();
199 $wgOut->addHTML( '<div class="fullImageLink" id="file"><a href="' . $full_url . '">' .
209 $s= $sk->makeMediaLink( $this->img
->getName(), '', '', true );
210 $info= wfMsg( 'fileinfo', ceil($this->img
->getSize()/1024.0), $this->img
->getMimeType() );
212 if (!$this->img
->isSafeFile()) {
213 $wgOut->addHTML("<div class=\"fullMedia\">");
214 $wgOut->addHTML("<span class=\"dangerousLink\">");
216 $wgOut->addHTML("</span>");
218 $wgOut->addHTML("<span class=\"fileInfo\"> (");
219 $wgOut->addWikiText( $info, false );
220 $wgOut->addHTML(")</span>");
221 $wgOut->addHTML("</div>");
223 #this should be formated a little nicer. Is CSS sufficient?
224 $wgOut->addHTML("<div class=\"mediaWarning\">");
225 $wgOut->addWikiText( wfMsg( 'mediawarning' ) );
226 $wgOut->addHTML('</div>');
229 $wgOut->addHTML("<div class=\"fullMedia\">");
232 $wgOut->addHTML("<span class=\"fileInfo\"> (");
233 $wgOut->addWikiText( $info, false );
234 $wgOut->addHTML(")</span>");
236 $wgOut->addHTML("</div>");
240 if($this->img
->fromSharedDirectory
) {
241 $this->printSharedImageText();
244 # Image does not exist
245 $wgOut->addWikiText( wfMsg( 'noimage', $this->getUploadUrl() ) );
249 function printSharedImageText() {
250 global $wgRepositoryBaseUrl, $wgFetchCommonsDescriptions, $wgOut;
252 $url = $wgRepositoryBaseUrl . urlencode($this->mTitle
->getDBkey());
253 $sharedtext = "<div class='sharedUploadNotice'>" . wfMsg("sharedupload");
254 if ($wgRepositoryBaseUrl && !$wgFetchCommonsDescriptions) {
255 $sharedtext .= " " . wfMsg("shareduploadwiki", $url);
257 $sharedtext .= "</div>";
258 $wgOut->addWikiText($sharedtext);
260 if ($wgRepositoryBaseUrl && $wgFetchCommonsDescriptions) {
261 $ur = ini_set('allow_url_fopen', true);
262 $text = wfGetHTTP($url . '?action=render');
263 ini_set('allow_url_fopen', $ur);
265 $this->mExtraDescription
= $text;
269 function getUploadUrl() {
271 $uploadTitle = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
272 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img
->getName() ) );
276 function uploadLinksBox()
278 global $wgUser, $wgOut;
280 if ($this->img
->fromSharedDirectory
)
283 $sk = $wgUser->getSkin();
284 $wgOut->addHTML( '<br /><ul><li>' );
285 $wgOut->addWikiText( '<div>'. wfMsg( 'uploadnewversion', $this->getUploadUrl() ) .'</div>' );
286 $wgOut->addHTML( '</li><li>' );
287 $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle
,
288 wfMsg( 'edit-externally' ), "action=edit&externaledit=true&mode=file" ) );
289 $wgOut->addWikiText( '<div>' . wfMsg('edit-externally-help') . '</div>' );
290 $wgOut->addHTML( '</li></ul>' );
293 function closeShowImage()
300 * If the page we've just displayed is in the "Image" namespace,
301 * we follow it with an upload history of the image and its usage.
303 function imageHistory()
305 global $wgUser, $wgOut, $wgUseExternalEditor;
307 $sk = $wgUser->getSkin();
309 $line = $this->img
->nextHistoryLine();
312 $list =& new ImageHistoryList( $sk );
313 $s = $list->beginImageHistoryList() .
314 $list->imageHistoryLine( true, $line->img_timestamp
,
315 $this->mTitle
->getDBkey(), $line->img_user
,
316 $line->img_user_text
, $line->img_size
, $line->img_description
);
318 while ( $line = $this->img
->nextHistoryLine() ) {
319 $s .= $list->imageHistoryLine( false, $line->img_timestamp
,
320 $line->oi_archive_name
, $line->img_user
,
321 $line->img_user_text
, $line->img_size
, $line->img_description
);
323 $s .= $list->endImageHistoryList();
325 $wgOut->addHTML( $s );
327 # Exist check because we don't want to show this on pages where an image
328 # doesn't exist along with the noimage message, that would suck. -ævar
329 if( $wgUseExternalEditor && $this->img
->exists() ) {
330 $this->uploadLinksBox();
335 function imageLinks()
337 global $wgUser, $wgOut;
339 $wgOut->addHTML( '<h2 id="filelinks">' . wfMsg( 'imagelinks' ) . "</h2>\n" );
341 $dbr =& wfGetDB( DB_SLAVE
);
342 $page = $dbr->tableName( 'page' );
343 $imagelinks = $dbr->tableName( 'imagelinks' );
345 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
346 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id"
347 . " LIMIT 500"; # quickie emergency brake
348 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
350 if ( 0 == $dbr->numRows( $res ) ) {
351 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
354 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
356 $sk = $wgUser->getSkin();
357 while ( $s = $dbr->fetchObject( $res ) ) {
358 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
359 $link = $sk->makeKnownLinkObj( $name, "" );
360 $wgOut->addHTML( "<li>{$link}</li>\n" );
362 $wgOut->addHTML( "</ul>\n" );
367 global $wgUser, $wgOut, $wgRequest;
369 $confirm = $wgRequest->getBool( 'wpConfirmB' );
370 $image = $wgRequest->getVal( 'image' );
371 $oldimage = $wgRequest->getVal( 'oldimage' );
373 # Only sysops can delete images. Previously ordinary users could delete
374 # old revisions, but this is no longer the case.
375 if ( !$wgUser->isAllowed('delete') ) {
376 $wgOut->sysopRequired();
379 if ( $wgUser->isBlocked() ) {
380 return $this->blockedIPpage();
382 if ( wfReadOnly() ) {
383 $wgOut->readOnlyPage();
387 # Better double-check that it hasn't been deleted yet!
388 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
389 if ( ( !is_null( $image ) )
390 && ( '' == trim( $image ) ) ) {
391 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
395 $this->img
= new Image( $this->mTitle
);
397 # Deleting old images doesn't require confirmation
398 if ( !is_null( $oldimage ) ||
$confirm ) {
399 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
402 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
407 if ( !is_null( $image ) ) {
408 $q = '&image=' . urlencode( $image );
409 } else if ( !is_null( $oldimage ) ) {
410 $q = '&oldimage=' . urlencode( $oldimage );
414 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
419 global $wgOut, $wgUser, $wgContLang, $wgRequest;
420 global $wgUseSquid, $wgInternalServer, $wgPostCommitUpdateList;
421 $fname = 'ImagePage::doDelete';
423 $reason = $wgRequest->getVal( 'wpReason' );
424 $oldimage = $wgRequest->getVal( 'oldimage' );
426 $dbw =& wfGetDB( DB_MASTER
);
428 if ( !is_null( $oldimage ) ) {
429 if ( strlen( $oldimage ) < 16 ) {
430 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
433 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
434 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
438 # Invalidate description page cache
439 $this->mTitle
->invalidateCache();
444 $wgInternalServer.wfImageArchiveUrl( $oldimage ),
445 $wgInternalServer.$this->mTitle
->getFullURL()
447 wfPurgeSquidServers($urlArr);
449 $this->doDeleteOldImage( $oldimage );
450 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
451 $deleted = $oldimage;
453 $image = $this->mTitle
->getDBkey();
454 $dest = wfImageDir( $image );
455 $archive = wfImageDir( $image );
457 # Delete the image file if it exists; due to sync problems
458 # or manual trimming sometimes the file will be missing.
459 $targetFile = "{$dest}/{$image}";
460 if( file_exists( $targetFile ) && ! @unlink
( $targetFile ) ) {
461 # If the deletion operation actually failed, bug out:
462 $wgOut->fileDeleteError( $targetFile );
465 $dbw->delete( 'image', array( 'img_name' => $image ) );
466 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
468 # Purge archive URLs from the squid
470 while ( $s = $dbw->fetchObject( $res ) ) {
471 $this->doDeleteOldImage( $s->oi_archive_name
);
472 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name
);
475 # And also the HTML of all pages using this image
476 $linksTo = $this->img
->getLinksTo();
478 $u = SquidUpdate
::newFromTitles( $linksTo, $urlArr );
479 array_push( $wgPostCommitUpdateList, $u );
482 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
484 # Image itself is now gone, and database is cleaned.
485 # Now we remove the image description page.
487 $article = new Article( $this->mTitle
);
488 $article->doDeleteArticle( $reason ); # ignore errors
490 # Invalidate parser cache and client cache for pages using this image
491 # This is left until relatively late to reduce lock time
492 Title
::touchArray( $linksTo );
494 /* Delete thumbnails and refresh image metadata cache */
495 $this->img
->purgeCache();
501 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
502 $wgOut->setRobotpolicy( 'noindex,nofollow' );
504 $loglink = '[[Special:Log/delete|' . wfMsg( 'deletionlog' ) . ']]';
505 $text = wfMsg( 'deletedtext', $deleted, $loglink );
507 $wgOut->addWikiText( $text );
509 $wgOut->returnToMain( false, $this->mTitle
->getPrefixedText() );
512 function doDeleteOldImage( $oldimage )
516 $name = substr( $oldimage, 15 );
517 $archive = wfImageArchiveDir( $name );
519 # Delete the image if it exists. Sometimes the file will be missing
520 # due to manual intervention or weird sync problems; treat that
521 # condition gracefully and continue to delete the database entry.
522 # Also some records may end up with an empty oi_archive_name field
523 # if the original file was missing when a new upload was made;
524 # don't try to delete the directory then!
526 $targetFile = "{$archive}/{$oldimage}";
527 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink
( $targetFile ) ) {
528 # If we actually have a file and can't delete it, throw an error.
529 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
532 $log = new LogPage( 'delete' );
533 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
539 global $wgOut, $wgRequest, $wgUser;
540 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
542 $oldimage = $wgRequest->getText( 'oldimage' );
543 if ( strlen( $oldimage ) < 16 ) {
544 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
547 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
548 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
552 if ( wfReadOnly() ) {
553 $wgOut->readOnlyPage();
556 if( $wgUser->isAnon() ) {
557 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
560 if ( ! $this->mTitle
->userCanEdit() ) {
561 $wgOut->sysopRequired();
564 if ( $wgUser->isBlocked() ) {
565 return $this->blockedIPpage();
567 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
568 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
571 $name = substr( $oldimage, 15 );
573 $dest = wfImageDir( $name );
574 $archive = wfImageArchiveDir( $name );
575 $curfile = "{$dest}/{$name}";
577 if ( ! is_file( $curfile ) ) {
578 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
581 $oldver = wfTimestampNow() . "!{$name}";
583 $dbr =& wfGetDB( DB_SLAVE
);
584 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
586 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
587 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
590 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
591 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
594 # Record upload and update metadata cache
595 $img = Image
::newFromName( $name );
596 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
598 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
599 $wgOut->setRobotpolicy( 'noindex,nofollow' );
600 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
602 $descTitle = $img->getTitle();
603 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
606 function blockedIPpage() {
607 require_once( 'EditPage.php' );
608 $edit = new EditPage( $this );
609 return $edit->blockedIPpage();
618 class ImageHistoryList
{
619 function ImageHistoryList( &$skin ) {
620 $this->skin
=& $skin;
623 function beginImageHistoryList() {
624 $s = "\n<h2 id=\"filehistory\">" . wfMsg( 'imghistory' ) . "</h2>\n" .
625 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
629 function endImageHistoryList() {
634 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
635 global $wgUser, $wgLang, $wgContLang, $wgTitle;
637 $datetime = $wgLang->timeanddate( $timestamp, true );
638 $del = wfMsg( 'deleteimg' );
639 $delall = wfMsg( 'deleteimgcompletely' );
640 $cur = wfMsg( 'cur' );
643 $url = Image
::imageUrl( $img );
645 if ( $wgUser->isAllowed('delete') ) {
646 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
648 $style = $this->skin
->getInternalLinkAttributes( $link, $delall );
650 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
655 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
656 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
657 $token = urlencode( $wgUser->editToken( $img ) );
658 $rlink = $this->skin
->makeKnownLinkObj( $wgTitle,
659 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
660 urlencode( $img ) . "&wpEditToken=$token" );
661 $dlink = $this->skin
->makeKnownLinkObj( $wgTitle,
662 $del, 'action=delete&oldimage=' . urlencode( $img ) .
663 "&wpEditToken=$token" );
665 # Having live active links for non-logged in users
666 # means that bots and spiders crawling our site can
667 # inadvertently change content. Baaaad idea.
668 $rlink = wfMsg( 'revertimg' );
673 $userlink = $usertext;
675 $userlink = $this->skin
->makeLinkObj(
676 Title
::makeTitle( NS_USER
, $usertext ),
679 $nbytes = wfMsg( 'nbytes', $size );
680 $style = $this->skin
->getInternalLinkAttributes( $url, $datetime );
682 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
683 . " . . {$userlink} ({$nbytes})";
685 $s .= $this->skin
->commentBlock( $description, $wgTitle );