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
23 global $wgUseExternalEditor, $wgOut, $wgShowEXIF;
25 $this->img
= new Image( $this->mTitle
);
27 if( $this->mTitle
->getNamespace() == NS_IMAGE
) {
28 if ($wgShowEXIF && $this->img
->exists()) {
29 $exif = $this->img
->getExifData();
30 $showmeta = count($exif) ?
true : false;
36 if ($this->img
->exists())
37 $wgOut->addHTML($this->showTOC($showmeta));
39 $this->openShowImage();
41 $wgOut->addWikiText($this->makeMetadataTable($exif));
43 # No need to display noarticletext, we use our own message, output in openShowImage()
44 if( $this->getID() ) {
47 # Just need to set the right headers
48 $wgOut->setArticleFlag( true );
49 $wgOut->setRobotpolicy( 'index,follow' );
50 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
51 $wgOut->addMetaTags();
55 $this->closeShowImage();
56 $this->imageHistory();
68 * @param bool $metadata Whether or not to show the metadata link
71 function showTOC( $metadata ) {
73 $r = '<ul id="filetoc">
74 <li><a href="#file">' . $wgLang->getNsText( NS_IMAGE
) . '</a></li>' .
75 ($metadata ?
'<li><a href="#metadata">' . wfMsg( 'metadata' ) . '</a></li>' : '') . '
76 <li><a href="#filehistory">' . wfMsg( 'imghistory' ) . '</a></li>
77 <li><a href="#filelinks">' . wfMsg( 'imagelinks' ) . '</a></li>
83 * Make a table with metadata to be shown in the output page.
87 * @param array $exif The array containing the EXIF data
90 function makeMetadataTable( $exif ) {
91 $r = "{| class=metadata align=right width=250px\n";
92 $r .= '|+ id=metadata | '. htmlspecialchars( wfMsg( 'metadata' ) ) . "\n";
93 foreach( $exif as $k => $v ) {
94 $tag = strtolower( $k );
95 $r .= "! class=$tag |" . wfMsg( "exif-$tag" ) . "\n";
96 $r .= "| class=$tag |" . htmlspecialchars( $v ) . "\n";
99 return substr($r, 0, -3) . '|}';
103 * Overloading Article's getContent method.
104 * Omit noarticletext if sharedupload
106 * @param $noredir If true, do not follow redirects
108 function getContent( $noredir )
110 if ( $this->img
&& $this->img
->fromSharedDirectory
&& 0 == $this->getID() ) {
113 return Article
::getContent( $noredir );
116 function openShowImage()
118 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
119 $wgUseImageResize, $wgRepositoryBaseUrl,
120 $wgUseExternalEditor, $wgServer, $wgFetchCommonsDescriptions;
121 $full_url = $this->img
->getViewURL();
125 if( $wgUser->getOption( 'imagesize' ) == '' ) {
126 $sizeSel = User
::getDefaultOption( 'imagesize' );
128 $sizeSel = IntVal( $wgUser->getOption( 'imagesize' ) );
130 if( !isset( $wgImageLimits[$sizeSel] ) ) {
131 $sizeSel = User
::getDefaultOption( 'imagesize' );
133 $max = $wgImageLimits[$sizeSel];
135 $maxHeight = $max[1];
136 $sk = $wgUser->getSkin();
138 if ( $this->img
->exists() ) {
139 if($this->img
->fromSharedDirectory
140 && $wgRepositoryBaseUrl && $wgFetchCommonsDescriptions) {
141 $this->printSharedImageText();
146 $width = $this->img
->getWidth();
147 $height = $this->img
->getHeight();
150 if ( $this->img
->allowInlineDisplay() and $width and $height) {
153 # "Download high res version" link below the image
154 $msg = wfMsg('showbigimage', $width, $height, intval( $this->img
->getSize()/1024 ) );
155 if ( $width > $maxWidth ) {
156 $height = floor( $height * $maxWidth / $width );
159 if ( $height > $maxHeight ) {
160 $width = floor( $width * $maxHeight / $height );
161 $height = $maxHeight;
163 if ( !$this->img
->mustRender()
164 && ( $width != $this->img
->getWidth() ||
$height != $this->img
->getHeight() ) ) {
165 if( $wgUseImageResize ) {
166 $thumbnail = $this->img
->getThumbnail( $width );
167 if ( $thumbnail == null ) {
170 $url = $thumbnail->getUrl();
173 # No resize ability? Show the full image, but scale
174 # it down in the browser so it fits on the page.
177 $anchoropen = "<a href=\"{$full_url}\">";
178 $anchorclose = "</a><br />\n$anchoropen{$msg}</a>";
181 $showLink = $this->img
->mustRender();
183 $wgOut->addHTML( '<div class="fullImageLink" id="file">' . $anchoropen .
184 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
185 htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>' );
187 #if direct link is allowed but it's not a renderable image, show an icon.
188 if ($this->img
->isSafeFile()) {
189 $icon= $this->img
->iconThumb();
191 $wgOut->addHTML( '<div class="fullImageLink" id="file"><a href="' . $full_url . '">' .
201 $s= $sk->makeMediaLink( $this->img
->getName(), '', '', true );
202 $info= wfMsg( 'fileinfo', ceil($this->img
->getSize()/1024.0), $this->img
->getMimeType() );
204 if (!$this->img
->isSafeFile()) {
205 $wgOut->addHTML("<div class=\"fullMedia\">");
206 $wgOut->addHTML("<span class=\"dangerousLink\">");
208 $wgOut->addHTML("</span>");
210 $wgOut->addHTML("<span class=\"fileInfo\"> (");
211 $wgOut->addWikiText( $info, false );
212 $wgOut->addHTML(")</span>");
213 $wgOut->addHTML("</div>");
215 #this should be formated a little nicer. Is CSS sufficient?
216 $wgOut->addHTML("<div class=\"mediaWarning\">");
217 $wgOut->addWikiText( wfMsg( 'mediawarning' ) );
218 $wgOut->addHTML('</div>');
221 $wgOut->addHTML("<div class=\"fullMedia\">");
224 $wgOut->addHTML("<span class=\"fileInfo\"> (");
225 $wgOut->addWikiText( $info, false );
226 $wgOut->addHTML(")</span>");
228 $wgOut->addHTML("</div>");
232 if($this->img
->fromSharedDirectory
) {
233 $this->printSharedImageText();
236 # Image does not exist
237 $wgOut->addWikiText( wfMsg( 'noimage', $this->getUploadUrl() ) );
241 function getSharedImageText() {
242 global $wgRepositoryBaseUrl, $wgFetchCommonsDescriptions, $wgOut;
244 $url = $wgRepositoryBaseUrl . urlencode($this->mTitle
->getDBkey());
245 $sharedtext = "<div class='sharedUploadNotice'>" . wfMsg("sharedupload");
246 if ($wgRepositoryBaseUrl) {
247 $sharedtext .= " " . wfMsg("shareduploadwiki", $url);
249 $sharedtext .= "</div>";
250 $wgOut->addWikiText($sharedtext);
252 if ($wgRepositoryBaseUrl && $wgFetchCommonsDescriptions) {
253 $ur = ini_set('allow_url_fopen', true);
255 ini_set('allow_url_fopen', false);
257 $wgOut->addHTML($text);
261 function getUploadUrl() {
263 $uploadTitle = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
264 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img
->getName() ) );
268 function uploadLinksBox()
270 global $wgUser,$wgOut;
271 $sk = $wgUser->getSkin();
272 $wgOut->addHTML( '<br /><ul><li>' );
273 $wgOut->addWikiText( '<div>'. wfMsg( 'uploadnewversion', $this->getUploadUrl() ) .'</div>' );
274 $wgOut->addHTML( '</li><li>' );
275 $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle
,
276 wfMsg( 'edit-externally' ), "action=edit&externaledit=true&mode=file" ) );
277 $wgOut->addWikiText( '<div>' . wfMsg('edit-externally-help') . '</div>' );
278 $wgOut->addHTML( '</li></ul>' );
281 function closeShowImage()
288 * If the page we've just displayed is in the "Image" namespace,
289 * we follow it with an upload history of the image and its usage.
291 function imageHistory()
293 global $wgUser, $wgOut, $wgUseExternalEditor;
295 $sk = $wgUser->getSkin();
297 $line = $this->img
->nextHistoryLine();
300 $list =& new ImageHistoryList( $sk );
301 $s = $list->beginImageHistoryList() .
302 $list->imageHistoryLine( true, $line->img_timestamp
,
303 $this->mTitle
->getDBkey(), $line->img_user
,
304 $line->img_user_text
, $line->img_size
, $line->img_description
);
306 while ( $line = $this->img
->nextHistoryLine() ) {
307 $s .= $list->imageHistoryLine( false, $line->img_timestamp
,
308 $line->oi_archive_name
, $line->img_user
,
309 $line->img_user_text
, $line->img_size
, $line->img_description
);
311 $s .= $list->endImageHistoryList();
313 $wgOut->addHTML( $s );
315 # Exist check because we don't want to show this on pages where an image
316 # doesn't exist along with the noimage message, that would suck. -ævar
317 if( $wgUseExternalEditor && $this->img
->exists() ) {
318 $this->uploadLinksBox();
323 function imageLinks()
325 global $wgUser, $wgOut;
327 $wgOut->addHTML( '<h2 id="filelinks">' . wfMsg( 'imagelinks' ) . "</h2>\n" );
329 $dbr =& wfGetDB( DB_SLAVE
);
330 $page = $dbr->tableName( 'page' );
331 $imagelinks = $dbr->tableName( 'imagelinks' );
333 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
334 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id"
335 . " LIMIT 500"; # quickie emergency brake
336 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
338 if ( 0 == $dbr->numRows( $res ) ) {
339 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
342 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
344 $sk = $wgUser->getSkin();
345 while ( $s = $dbr->fetchObject( $res ) ) {
346 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
347 $link = $sk->makeKnownLinkObj( $name, "" );
348 $wgOut->addHTML( "<li>{$link}</li>\n" );
350 $wgOut->addHTML( "</ul>\n" );
355 global $wgUser, $wgOut, $wgRequest;
357 $confirm = $wgRequest->getBool( 'wpConfirmB' );
358 $image = $wgRequest->getVal( 'image' );
359 $oldimage = $wgRequest->getVal( 'oldimage' );
361 # Only sysops can delete images. Previously ordinary users could delete
362 # old revisions, but this is no longer the case.
363 if ( !$wgUser->isAllowed('delete') ) {
364 $wgOut->sysopRequired();
367 if ( $wgUser->isBlocked() ) {
368 return $this->blockedIPpage();
370 if ( wfReadOnly() ) {
371 $wgOut->readOnlyPage();
375 # Better double-check that it hasn't been deleted yet!
376 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
377 if ( ( !is_null( $image ) )
378 && ( '' == trim( $image ) ) ) {
379 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
383 $this->img
= new Image( $this->mTitle
);
385 # Deleting old images doesn't require confirmation
386 if ( !is_null( $oldimage ) ||
$confirm ) {
387 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
390 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
395 if ( !is_null( $image ) ) {
396 $q = '&image=' . urlencode( $image );
397 } else if ( !is_null( $oldimage ) ) {
398 $q = '&oldimage=' . urlencode( $oldimage );
402 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
407 global $wgOut, $wgUser, $wgContLang, $wgRequest;
408 global $wgUseSquid, $wgInternalServer, $wgPostCommitUpdateList;
409 $fname = 'ImagePage::doDelete';
411 $reason = $wgRequest->getVal( 'wpReason' );
412 $oldimage = $wgRequest->getVal( 'oldimage' );
414 $dbw =& wfGetDB( DB_MASTER
);
416 if ( !is_null( $oldimage ) ) {
417 if ( strlen( $oldimage ) < 16 ) {
418 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
421 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
422 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
426 # Invalidate description page cache
427 $this->mTitle
->invalidateCache();
432 $wgInternalServer.wfImageArchiveUrl( $oldimage ),
433 $wgInternalServer.$this->mTitle
->getFullURL()
435 wfPurgeSquidServers($urlArr);
437 $this->doDeleteOldImage( $oldimage );
438 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
439 $deleted = $oldimage;
441 $image = $this->mTitle
->getDBkey();
442 $dest = wfImageDir( $image );
443 $archive = wfImageDir( $image );
445 # Delete the image file if it exists; due to sync problems
446 # or manual trimming sometimes the file will be missing.
447 $targetFile = "{$dest}/{$image}";
448 if( file_exists( $targetFile ) && ! @unlink
( $targetFile ) ) {
449 # If the deletion operation actually failed, bug out:
450 $wgOut->fileDeleteError( $targetFile );
453 $dbw->delete( 'image', array( 'img_name' => $image ) );
454 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
456 # Purge archive URLs from the squid
458 while ( $s = $dbw->fetchObject( $res ) ) {
459 $this->doDeleteOldImage( $s->oi_archive_name
);
460 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name
);
463 # And also the HTML of all pages using this image
464 $linksTo = $this->img
->getLinksTo();
466 $u = SquidUpdate
::newFromTitles( $linksTo, $urlArr );
467 array_push( $wgPostCommitUpdateList, $u );
470 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
472 # Image itself is now gone, and database is cleaned.
473 # Now we remove the image description page.
475 $article = new Article( $this->mTitle
);
476 $article->doDeleteArticle( $reason ); # ignore errors
478 # Invalidate parser cache and client cache for pages using this image
479 # This is left until relatively late to reduce lock time
480 Title
::touchArray( $linksTo );
482 /* Delete thumbnails and refresh image metadata cache */
483 $this->img
->purgeCache();
489 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
490 $wgOut->setRobotpolicy( 'noindex,nofollow' );
492 $loglink = '[[Special:Log/delete|' . wfMsg( 'deletionlog' ) . ']]';
493 $text = wfMsg( 'deletedtext', $deleted, $loglink );
495 $wgOut->addWikiText( $text );
497 $wgOut->returnToMain( false, $this->mTitle
->getPrefixedText() );
500 function doDeleteOldImage( $oldimage )
504 $name = substr( $oldimage, 15 );
505 $archive = wfImageArchiveDir( $name );
507 # Delete the image if it exists. Sometimes the file will be missing
508 # due to manual intervention or weird sync problems; treat that
509 # condition gracefully and continue to delete the database entry.
510 # Also some records may end up with an empty oi_archive_name field
511 # if the original file was missing when a new upload was made;
512 # don't try to delete the directory then!
514 $targetFile = "{$archive}/{$oldimage}";
515 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink
( $targetFile ) ) {
516 # If we actually have a file and can't delete it, throw an error.
517 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
520 $log = new LogPage( 'delete' );
521 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
527 global $wgOut, $wgRequest, $wgUser;
528 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
530 $oldimage = $wgRequest->getText( 'oldimage' );
531 if ( strlen( $oldimage ) < 16 ) {
532 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
535 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
536 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
540 if ( wfReadOnly() ) {
541 $wgOut->readOnlyPage();
544 if( $wgUser->isAnon() ) {
545 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
548 if ( ! $this->mTitle
->userCanEdit() ) {
549 $wgOut->sysopRequired();
552 if ( $wgUser->isBlocked() ) {
553 return $this->blockedIPpage();
555 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
556 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
559 $name = substr( $oldimage, 15 );
561 $dest = wfImageDir( $name );
562 $archive = wfImageArchiveDir( $name );
563 $curfile = "{$dest}/{$name}";
565 if ( ! is_file( $curfile ) ) {
566 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
569 $oldver = wfTimestampNow() . "!{$name}";
571 $dbr =& wfGetDB( DB_SLAVE
);
572 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
574 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
575 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
578 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
579 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
582 # Record upload and update metadata cache
583 $img = Image
::newFromName( $name );
584 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
586 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
587 $wgOut->setRobotpolicy( 'noindex,nofollow' );
588 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
590 $descTitle = $img->getTitle();
591 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
594 function blockedIPpage() {
595 require_once( 'EditPage.php' );
596 $edit = new EditPage( $this );
597 return $edit->blockedIPpage();
606 class ImageHistoryList
{
607 function ImageHistoryList( &$skin ) {
608 $this->skin
=& $skin;
611 function beginImageHistoryList() {
612 $s = "\n<h2 id=\"filehistory\">" . wfMsg( 'imghistory' ) . "</h2>\n" .
613 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
617 function endImageHistoryList() {
622 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
623 global $wgUser, $wgLang, $wgContLang, $wgTitle;
625 $datetime = $wgLang->timeanddate( $timestamp, true );
626 $del = wfMsg( 'deleteimg' );
627 $delall = wfMsg( 'deleteimgcompletely' );
628 $cur = wfMsg( 'cur' );
631 $url = Image
::imageUrl( $img );
633 if ( $wgUser->isAllowed('delete') ) {
634 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
636 $style = $this->skin
->getInternalLinkAttributes( $link, $delall );
638 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
643 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
644 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
645 $token = urlencode( $wgUser->editToken( $img ) );
646 $rlink = $this->skin
->makeKnownLinkObj( $wgTitle,
647 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
648 urlencode( $img ) . "&wpEditToken=$token" );
649 $dlink = $this->skin
->makeKnownLinkObj( $wgTitle,
650 $del, 'action=delete&oldimage=' . urlencode( $img ) .
651 "&wpEditToken=$token" );
653 # Having live active links for non-logged in users
654 # means that bots and spiders crawling our site can
655 # inadvertently change content. Baaaad idea.
656 $rlink = wfMsg( 'revertimg' );
661 $userlink = $usertext;
663 $userlink = $this->skin
->makeLinkObj(
664 Title
::makeTitle( NS_USER
, $usertext ),
667 $nbytes = wfMsg( 'nbytes', $size );
668 $style = $this->skin
->getInternalLinkAttributes( $url, $datetime );
670 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
671 . " . . {$userlink} ({$nbytes})";
673 $s .= $this->skin
->commentBlock( $description, $wgTitle );