From f696971eba84feadab524eb6e563cfa3d064b951 Mon Sep 17 00:00:00 2001 From: Erik Moeller Date: Thu, 2 Dec 2004 19:56:00 +0000 Subject: [PATCH] - Refactor Image class for better maintainability - Add $wgRepositoryBaseUrl for backlinks to repository wiki from local image pages --- includes/DefaultSettings.php | 7 ++ includes/Image.php | 132 +++++++++++++++++++++-------------- includes/ImagePage.php | 10 ++- languages/Language.php | 1 + 4 files changed, 95 insertions(+), 55 deletions(-) diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index c4eb85c462..48f513472d 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -139,6 +139,13 @@ $wgSharedUploadDirectory = "/var/www/wiki3/images"; $wgHashedSharedUploadDirectory = true; # set true if the repository uses latin1 filenames $wgSharedLatin1=false; +# Base URL for a repository wiki. Leave this blank if uploads are just +# stored in a shared directory and not meant to be accessible throug +# a separate wiki. Otherwise the image description pages on the local +# wiki will link to the image description page on this wiki. +# +# Please specify the namespace, as in the example below. +$wgRepositoryBaseUrl="http://commons.wikimedia.org/wiki/Image:"; # Email settings # diff --git a/includes/Image.php b/includes/Image.php index 2209c4a053..52bca6e9e1 100644 --- a/includes/Image.php +++ b/includes/Image.php @@ -40,47 +40,38 @@ class Image */ function Image( $name ) { - global $wgUploadDirectory,$wgHashedUploadDirectory, - $wgUseSharedUploads, $wgSharedUploadDirectory, - $wgHashedSharedUploadDirectory,$wgUseLatin1, - $wgSharedLatin1,$wgLang; + + global $wgUseSharedUploads, $wgUseLatin1, $wgSharedLatin1, $wgLang; $this->name = $name; $this->title = Title::makeTitleSafe( NS_IMAGE, $this->name ); - $this->fromSharedDirectory = false; - if ($wgHashedUploadDirectory) { - $hash = md5( $this->title->getDBkey() ); - $this->imagePath = $wgUploadDirectory . '/' . $hash{0} . '/' . - substr( $hash, 0, 2 ) . "/{$name}"; - } else { - $this->imagePath = $wgUploadDirectory . '/' . $name; - } + $this->fromSharedDirectory = false; + $this->imagePath = $this->getFullPath(); $this->fileExists = file_exists( $this->imagePath); # If the file is not found, and a shared upload directory # like the Wikimedia Commons is used, look for it there. - if (!$this->fileExists && $wgUseSharedUploads) { - # in case we're running a capitallinks=false wiki - $sharedname=$wgLang->ucfirst($name); - $sharedtitle=$wgLang->ucfirst($this->title->getDBkey()); - if($wgUseLatin1 && !$wgSharedLatin1) { - $sharedname=utf8_encode($sharedname); - $sharedtitle=utf8_encode($sharedtitle); + if (!$this->fileExists && $wgUseSharedUploads) { + + # In case we're on a wgCapitalLinks=false wiki, we + # capitalize the first letter of the filename before + # looking it up in the shared repository. + $this->name= $wgLang->ucfirst($name); + + # Encode the filename if we're on a Latin1 wiki and the + # shared repository is UTF-8 + if($wgUseLatin1 && !$wgSharedLatin1) { + $this->name = utf8_encode($name); } - if($wgHashedSharedUploadDirectory) { - $hash = md5( $sharedtitle ); - $this->imagePath = $wgSharedUploadDirectory . '/' . $hash{0} . '/' . - substr( $hash, 0, 2 ) . "/".$sharedname; - } else { - $this->imagePath = $wgSharedUploadDirectory . '/' . $sharedname; - } + $this->imagePath = $this->getFullPath(true); $this->fileExists = file_exists( $this->imagePath); - $this->fromSharedDirectory = true; - $name=$sharedname; - } + $this->fromSharedDirectory = true; + $name=$this->name; + + } if($this->fileExists) { - $this->url = $this->wfImageUrl( $name, $this->fromSharedDirectory ); + $this->url = $this->wfImageUrl( $this->name, $this->fromSharedDirectory ); } else { $this->url=''; } @@ -244,24 +235,15 @@ class Image */ function wfImageUrl( $name, $fromSharedDirectory = false ) { - global $wgUploadPath,$wgUploadBaseUrl,$wgHashedUploadDirectory, - $wgHashedSharedUploadDirectory,$wgSharedUploadPath; + global $wgUploadPath,$wgUploadBaseUrl,$wgSharedUploadPath; if($fromSharedDirectory) { - $hash = $wgHashedSharedUploadDirectory; $base = ''; $path = $wgSharedUploadPath; } else { - $hash = $wgHashedUploadDirectory; $base = $wgUploadBaseUrl; $path = $wgUploadPath; } - if ( $hash ) { - $hash = md5( $name ); - $url = "{$base}{$path}/" . $hash{0} . "/" . - substr( $hash, 0, 2 ) . "/{$name}"; - } else { - $url = "{$base}{$path}/{$name}"; - } + $url = "{$base}{$path}" . wfGetHashPath($name, $fromSharedDirectory) . "{$name}"; return wfUrlencode( $url ); } @@ -280,30 +262,23 @@ class Image * @access private */ function thumbUrl( $width, $subdir='thumb') { - global $wgUploadPath,$wgHashedUploadDirectory, $wgUploadBaseUrl, + global $wgUploadPath, $wgUploadBaseUrl, $wgSharedUploadPath,$wgSharedUploadDirectory, - $wgHashedSharedUploadDirectory,$wgUseLatin1,$wgSharedLatin1; + $wgUseLatin1,$wgSharedLatin1; $name = $this->thumbName( $width ); if($this->fromSharedDirectory) { - $hashdir = $wgHashedSharedUploadDirectory; $base = ''; $path = $wgSharedUploadPath; if($wgUseLatin1 && !$wgSharedLatin1) { $name=utf8_encode($name); } } else { - $hashdir = $wgHashedUploadDirectory; $base = $wgUploadBaseUrl; $path = $wgUploadPath; } - if ($hashdir) { - $hash = md5( $name ); - $url = "{$base}{$path}/{$subdir}/" . $hash{0} . "/" . - substr( $hash, 0, 2 ) . "/{$name}"; - } else { - $url = "{$base}{$path}/{$subdir}/{$name}"; - } - + $url = "{$base}{$path}/{$subdir}" . + wfGetHashPath($name, $this->fromSharedDirectory) + . "{$name}"; return wfUrlencode($url); } @@ -562,6 +537,34 @@ class Image return ( $this->extension == 'svg' ); } + /** + * Return the full filesystem path to the file. Note that this does + * not mean that a file actually exists under that location. + * + * This path depends on whether directory hashing is active or not, + * i.e. whether the images are all found in the same directory, + * or in hashed paths like /images/3/3c. + * + * @access public + * @param boolean $fromSharedDirectory Return the path to the file + * in a shared repository (see $wgUseSharedRepository and related + * options in DefaultSettings.php) instead of a local one. + * + */ + function getFullPath( $fromSharedRepository = false ) + { + global $wgUploadDirectory, $wgSharedUploadDirectory; + + $dir = $fromSharedRepository ? $wgSharedUploadDirectory : + $wgUploadDirectory; + $ishashed = $fromSharedRepository ? $wgHashedSharedUploadDirectory : + $wgHashedUploadDirectory; + $name = $this->name; + $fullpath = $dir . wfGetHashPath($name) . $name; + return $fullpath; + } + + } //class @@ -637,6 +640,29 @@ function wfImageArchiveDir( $fname , $subdir='archive', $shared=false ) return $archive; } + +/* + * Return the hash path component of an image path (URL or filesystem), + * e.g. "/3/3c/", or just "/" if hashing is not used. + * + * @param $dbkey The filesystem / database name of the file + * @param $fromSharedDirectory Use the shared file repository? It may + * use different hash settings from the local one. + */ +function wfGetHashPath ( $dbkey, $fromSharedDirectory = false ) { + global $wgHashedSharedUploadDirectory, $wgSharedUploadDirectory; + + $ishashed = $fromSharedDirectory ? $wgHashedSharedUploadDirectory : + $wgSharedUploadDirectory; + if($ishashed) { + $hash = md5($dbkey); + return '/' . $hash{0} . '/' . substr( $hash, 0, 2 ) . "/"; + } else { + return '/'; + } +} + + /** * Record an image upload in the upload log. */ diff --git a/includes/ImagePage.php b/includes/ImagePage.php index 139f0247c2..d29a431180 100644 --- a/includes/ImagePage.php +++ b/includes/ImagePage.php @@ -37,7 +37,8 @@ class ImagePage extends Article { function openShowImage() { - global $wgOut, $wgUser, $wgImageLimits, $wgRequest, $wgUseImageResize; + global $wgOut, $wgUser, $wgImageLimits, $wgRequest, + $wgUseImageResize, $wgRepositoryBaseUrl; $this->img = Image::newFromTitle( $this->mTitle ); $url = $this->img->getViewURL(); $anchoropen = ''; @@ -82,7 +83,12 @@ class ImagePage extends Article { } $wgOut->addHTML( $s ); if($this->img->fromSharedDirectory) { - $wgOut->addWikiText("
".wfMsg("sharedupload")."
"); + $sharedtext="
" . wfMsg("sharedupload"); + if($wgRepositoryBaseUrl) { + $sharedtext .= " ". wfMsg("shareduploadwiki",$wgRepositoryBaseUrl . urlencode($this->mTitle->getDBkey())); + } + $sharedtext.="
"; + $wgOut->addWikiText($sharedtext); } } } diff --git a/languages/Language.php b/languages/Language.php index d61d3d504f..55e37eae69 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -969,6 +969,7 @@ this old version, (rev) = revert to this old version. 'linkstoimage' => 'The following pages link to this image:', 'nolinkstoimage' => 'There are no pages that link to this image.', 'sharedupload' => 'This file is a shared upload and may be used by other projects.', +'shareduploadwiki' => 'Please see the [$1 image description page] for further information.', # Statistics # -- 2.20.1