| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
namespace MediaWiki\Cache; |
| 25 |
|
| 26 |
use MediaWiki\Config\ServiceOptions; |
| 27 |
use MediaWiki\MainConfigNames; |
| 28 |
use MediaWiki\MediaWikiServices; |
| 29 |
use MediaWiki\Request\WebRequest; |
| 30 |
use Wikimedia\AtEase\AtEase; |
| 31 |
use Wikimedia\IPUtils; |
| 32 |
use Wikimedia\ObjectCache\BagOStuff; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
abstract class FileCacheBase { |
| 40 |
|
| 41 |
private const CONSTRUCTOR_OPTIONS = [ |
| 42 |
MainConfigNames::CacheEpoch, |
| 43 |
MainConfigNames::FileCacheDepth, |
| 44 |
MainConfigNames::FileCacheDirectory, |
| 45 |
MainConfigNames::MimeType, |
| 46 |
MainConfigNames::UseGzip, |
| 47 |
]; |
| 48 |
|
| 49 |
|
| 50 |
protected $mKey; |
| 51 |
|
| 52 |
protected $mType = 'object'; |
| 53 |
|
| 54 |
protected $mExt = 'cache'; |
| 55 |
|
| 56 |
protected $mFilePath; |
| 57 |
|
| 58 |
protected $mUseGzip; |
| 59 |
|
| 60 |
protected $mCached; |
| 61 |
|
| 62 |
protected $options; |
| 63 |
|
| 64 |
|
| 65 |
private const MISS_FACTOR = 15; |
| 66 |
private const MISS_TTL_SEC = 3600; |
| 67 |
|
| 68 |
protected function __construct() { |
| 69 |
$this->options = new ServiceOptions( |
| 70 |
self::CONSTRUCTOR_OPTIONS, |
| 71 |
MediaWikiServices::getInstance()->getMainConfig() |
| 72 |
); |
| 73 |
$this->mUseGzip = (bool)$this->options->get( MainConfigNames::UseGzip ); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
final protected function baseCacheDirectory() { |
| 81 |
return $this->options->get( MainConfigNames::FileCacheDirectory ); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
abstract protected function cacheDirectory(); |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
protected function cachePath() { |
| 95 |
if ( $this->mFilePath !== null ) { |
| 96 |
return $this->mFilePath; |
| 97 |
} |
| 98 |
|
| 99 |
$dir = $this->cacheDirectory(); |
| 100 |
|
| 101 |
$subDirs = $this->typeSubdirectory() . $this->hashSubdirectory(); |
| 102 |
|
| 103 |
$key = str_replace( '.', '%2E', urlencode( $this->mKey ) ); |
| 104 |
|
| 105 |
$this->mFilePath = "{$dir}/{$subDirs}{$key}.{$this->mExt}"; |
| 106 |
if ( $this->useGzip() ) { |
| 107 |
$this->mFilePath .= '.gz'; |
| 108 |
} |
| 109 |
|
| 110 |
return $this->mFilePath; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public function isCached() { |
| 118 |
$this->mCached ??= is_file( $this->cachePath() ); |
| 119 |
|
| 120 |
return $this->mCached; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public function cacheTimestamp() { |
| 128 |
$timestamp = filemtime( $this->cachePath() ); |
| 129 |
|
| 130 |
return ( $timestamp !== false ) |
| 131 |
? wfTimestamp( TS_MW, $timestamp ) |
| 132 |
: false; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public function isCacheGood( $timestamp = '' ) { |
| 142 |
$cacheEpoch = $this->options->get( MainConfigNames::CacheEpoch ); |
| 143 |
|
| 144 |
if ( !$this->isCached() ) { |
| 145 |
return false; |
| 146 |
} |
| 147 |
|
| 148 |
$cachetime = $this->cacheTimestamp(); |
| 149 |
$good = ( $timestamp <= $cachetime && $cacheEpoch <= $cachetime ); |
| 150 |
wfDebug( __METHOD__ . |
| 151 |
": cachetime $cachetime, touched '{$timestamp}' epoch {$cacheEpoch}, good " . wfBoolToStr( $good ) ); |
| 152 |
|
| 153 |
return $good; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
protected function useGzip() { |
| 161 |
return $this->mUseGzip; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
public function fetchText() { |
| 169 |
if ( $this->useGzip() ) { |
| 170 |
$fh = gzopen( $this->cachePath(), 'rb' ); |
| 171 |
|
| 172 |
return stream_get_contents( $fh ); |
| 173 |
} else { |
| 174 |
return file_get_contents( $this->cachePath() ); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public function saveText( $text ) { |
| 184 |
if ( $this->useGzip() ) { |
| 185 |
$text = gzencode( $text ); |
| 186 |
} |
| 187 |
|
| 188 |
$this->checkCacheDirs(); |
| 189 |
if ( !file_put_contents( $this->cachePath(), $text, LOCK_EX ) ) { |
| 190 |
wfDebug( __METHOD__ . "() failed saving " . $this->cachePath() ); |
| 191 |
$this->mCached = null; |
| 192 |
|
| 193 |
return false; |
| 194 |
} |
| 195 |
|
| 196 |
$this->mCached = true; |
| 197 |
|
| 198 |
return $text; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public function clearCache() { |
| 206 |
AtEase::suppressWarnings(); |
| 207 |
unlink( $this->cachePath() ); |
| 208 |
AtEase::restoreWarnings(); |
| 209 |
$this->mCached = false; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
protected function checkCacheDirs() { |
| 217 |
wfMkdirParents( dirname( $this->cachePath() ), null, __METHOD__ ); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
protected function typeSubdirectory() { |
| 228 |
return $this->mType . '/'; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
protected function hashSubdirectory() { |
| 237 |
$fileCacheDepth = $this->options->get( MainConfigNames::FileCacheDepth ); |
| 238 |
|
| 239 |
$subdir = ''; |
| 240 |
if ( $fileCacheDepth > 0 ) { |
| 241 |
$hash = md5( $this->mKey ); |
| 242 |
for ( $i = 1; $i <= $fileCacheDepth; $i++ ) { |
| 243 |
$subdir .= substr( $hash, 0, $i ) . '/'; |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
return $subdir; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
public function incrMissesRecent( WebRequest $request ) { |
| 256 |
if ( mt_rand( 1, self::MISS_FACTOR ) == 1 ) { |
| 257 |
|
| 258 |
|
| 259 |
$ip = $request->getIP(); |
| 260 |
if ( !IPUtils::isValid( $ip ) ) { |
| 261 |
return; |
| 262 |
} |
| 263 |
|
| 264 |
$ip = IPUtils::isIPv6( $ip ) |
| 265 |
? IPUtils::sanitizeRange( "$ip/32" ) |
| 266 |
: IPUtils::sanitizeRange( "$ip/16" ); |
| 267 |
|
| 268 |
|
| 269 |
$cache = MediaWikiServices::getInstance()->getObjectCacheFactory() |
| 270 |
->getLocalClusterInstance(); |
| 271 |
$key = $cache->makeKey( static::class, 'attempt', $this->mType, $this->mKey, $ip ); |
| 272 |
if ( !$cache->add( $key, 1, self::MISS_TTL_SEC ) ) { |
| 273 |
return; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
$cache->incrWithInit( $this->cacheMissKey( $cache ), self::MISS_TTL_SEC ); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
public function getMissesRecent() { |
| 286 |
$cache = MediaWikiServices::getInstance()->getObjectCacheFactory() |
| 287 |
->getLocalClusterInstance(); |
| 288 |
|
| 289 |
return self::MISS_FACTOR * $cache->get( $this->cacheMissKey( $cache ) ); |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
protected function cacheMissKey( BagOStuff $cache ) { |
| 297 |
return $cache->makeKey( static::class, 'misses', $this->mType, $this->mKey ); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
class_alias( FileCacheBase::class, 'FileCacheBase' ); |