2011-09-29 08:18:20 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2012-05-08 12:51:21 +00:00
|
|
|
* Data storage in the file system.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2011-09-29 08:18:20 +00:00
|
|
|
* @file
|
|
|
|
|
* @ingroup Cache
|
|
|
|
|
*/
|
2012-05-08 12:51:21 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Base class for data storage in the file system.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Cache
|
|
|
|
|
*/
|
2011-09-29 08:18:20 +00:00
|
|
|
abstract class FileCacheBase {
|
|
|
|
|
protected $mKey;
|
2011-10-02 17:53:33 +00:00
|
|
|
protected $mType = 'object';
|
|
|
|
|
protected $mExt = 'cache';
|
2011-09-29 08:18:20 +00:00
|
|
|
protected $mFilePath;
|
|
|
|
|
protected $mUseGzip;
|
2011-10-02 19:44:31 +00:00
|
|
|
/* lazy loaded */
|
|
|
|
|
protected $mCached;
|
2011-09-29 08:18:20 +00:00
|
|
|
|
2013-05-15 01:12:35 +00:00
|
|
|
/* @todo configurable? */
|
2011-10-02 19:44:31 +00:00
|
|
|
const MISS_FACTOR = 15; // log 1 every MISS_FACTOR cache misses
|
|
|
|
|
const MISS_TTL_SEC = 3600; // how many seconds ago is "recent"
|
2011-10-02 17:53:33 +00:00
|
|
|
|
2011-09-29 08:18:20 +00:00
|
|
|
protected function __construct() {
|
|
|
|
|
global $wgUseGzip;
|
|
|
|
|
|
|
|
|
|
$this->mUseGzip = (bool)$wgUseGzip;
|
2011-10-02 17:53:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the base file cache directory
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
final protected function baseCacheDirectory() {
|
2011-10-03 06:54:39 +00:00
|
|
|
global $wgFileCacheDirectory;
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2011-10-03 06:54:39 +00:00
|
|
|
return $wgFileCacheDirectory;
|
2011-09-29 08:18:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-12-27 22:38:44 +00:00
|
|
|
* Get the base cache directory (not specific to this file)
|
2011-09-29 08:18:20 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
abstract protected function cacheDirectory();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the path to the cache file
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected function cachePath() {
|
|
|
|
|
if ( $this->mFilePath !== null ) {
|
|
|
|
|
return $this->mFilePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$dir = $this->cacheDirectory();
|
2011-10-02 17:53:33 +00:00
|
|
|
# Build directories (methods include the trailing "/")
|
|
|
|
|
$subDirs = $this->typeSubdirectory() . $this->hashSubdirectory();
|
2011-09-29 08:18:20 +00:00
|
|
|
# Avoid extension confusion
|
|
|
|
|
$key = str_replace( '.', '%2E', urlencode( $this->mKey ) );
|
|
|
|
|
# Build the full file path
|
|
|
|
|
$this->mFilePath = "{$dir}/{$subDirs}{$key}.{$this->mExt}";
|
|
|
|
|
if ( $this->useGzip() ) {
|
|
|
|
|
$this->mFilePath .= '.gz';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->mFilePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if the cache file exists
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isCached() {
|
2011-10-02 19:44:31 +00:00
|
|
|
if ( $this->mCached === null ) {
|
|
|
|
|
$this->mCached = file_exists( $this->cachePath() );
|
|
|
|
|
}
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2011-10-02 19:44:31 +00:00
|
|
|
return $this->mCached;
|
2011-09-29 08:18:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the last-modified timestamp of the cache file
|
2012-02-09 17:42:35 +00:00
|
|
|
* @return string|bool TS_MW timestamp
|
2011-09-29 08:18:20 +00:00
|
|
|
*/
|
|
|
|
|
public function cacheTimestamp() {
|
|
|
|
|
$timestamp = filemtime( $this->cachePath() );
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2011-09-29 08:18:20 +00:00
|
|
|
return ( $timestamp !== false )
|
|
|
|
|
? wfTimestamp( TS_MW, $timestamp )
|
|
|
|
|
: false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if up to date cache file exists
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $timestamp MW_TS timestamp
|
2011-09-29 08:18:20 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isCacheGood( $timestamp = '' ) {
|
|
|
|
|
global $wgCacheEpoch;
|
|
|
|
|
|
|
|
|
|
if ( !$this->isCached() ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$cachetime = $this->cacheTimestamp();
|
|
|
|
|
$good = ( $timestamp <= $cachetime && $wgCacheEpoch <= $cachetime );
|
2011-12-27 22:38:44 +00:00
|
|
|
wfDebug( __METHOD__ . ": cachetime $cachetime, touched '{$timestamp}' epoch {$wgCacheEpoch}, good $good\n" );
|
2011-09-29 08:18:20 +00:00
|
|
|
|
|
|
|
|
return $good;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if the cache is gzipped
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
protected function useGzip() {
|
|
|
|
|
return $this->mUseGzip;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the uncompressed text from the cache
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function fetchText() {
|
2013-04-20 17:18:13 +00:00
|
|
|
if ( $this->useGzip() ) {
|
2012-02-12 19:51:03 +00:00
|
|
|
$fh = gzopen( $this->cachePath(), 'rb' );
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2012-02-12 19:51:03 +00:00
|
|
|
return stream_get_contents( $fh );
|
|
|
|
|
} else {
|
2012-02-16 02:34:59 +00:00
|
|
|
return file_get_contents( $this->cachePath() );
|
2012-02-12 19:51:03 +00:00
|
|
|
}
|
2011-09-29 08:18:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Save and compress text to the cache
|
|
|
|
|
* @return string compressed text
|
|
|
|
|
*/
|
|
|
|
|
public function saveText( $text ) {
|
|
|
|
|
global $wgUseFileCache;
|
2011-10-02 17:53:33 +00:00
|
|
|
|
2011-09-29 08:18:20 +00:00
|
|
|
if ( !$wgUseFileCache ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $this->useGzip() ) {
|
|
|
|
|
$text = gzencode( $text );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->checkCacheDirs(); // build parent dir
|
2011-10-02 17:53:33 +00:00
|
|
|
if ( !file_put_contents( $this->cachePath(), $text, LOCK_EX ) ) {
|
2013-04-13 11:36:24 +00:00
|
|
|
wfDebug( __METHOD__ . "() failed saving " . $this->cachePath() . "\n" );
|
2011-10-02 19:44:31 +00:00
|
|
|
$this->mCached = null;
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2011-09-29 08:18:20 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-02 19:44:31 +00:00
|
|
|
$this->mCached = true;
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2011-09-29 08:18:20 +00:00
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-29 21:17:43 +00:00
|
|
|
/**
|
2011-09-29 08:18:20 +00:00
|
|
|
* Clear the cache for this page
|
2012-01-12 19:41:18 +00:00
|
|
|
* @return void
|
2011-09-29 08:18:20 +00:00
|
|
|
*/
|
|
|
|
|
public function clearCache() {
|
|
|
|
|
wfSuppressWarnings();
|
|
|
|
|
unlink( $this->cachePath() );
|
|
|
|
|
wfRestoreWarnings();
|
2011-10-02 19:44:31 +00:00
|
|
|
$this->mCached = false;
|
2011-09-29 08:18:20 +00:00
|
|
|
}
|
|
|
|
|
|
2011-09-29 21:17:43 +00:00
|
|
|
/**
|
2011-09-29 08:18:20 +00:00
|
|
|
* Create parent directors of $this->cachePath()
|
2012-01-12 19:41:18 +00:00
|
|
|
* @return void
|
2011-09-29 08:18:20 +00:00
|
|
|
*/
|
|
|
|
|
protected function checkCacheDirs() {
|
2011-10-02 17:53:33 +00:00
|
|
|
wfMkdirParents( dirname( $this->cachePath() ), null, __METHOD__ );
|
|
|
|
|
}
|
2011-09-29 08:18:20 +00:00
|
|
|
|
2011-10-02 17:53:33 +00:00
|
|
|
/**
|
2012-02-07 17:56:32 +00:00
|
|
|
* Get the cache type subdirectory (with trailing slash)
|
|
|
|
|
* An extending class could use that method to alter the type -> directory
|
|
|
|
|
* mapping. @see HTMLFileCache::typeSubdirectory() for an example.
|
|
|
|
|
*
|
2011-10-02 17:53:33 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected function typeSubdirectory() {
|
|
|
|
|
return $this->mType . '/';
|
2011-09-29 08:18:20 +00:00
|
|
|
}
|
|
|
|
|
|
2011-09-29 21:17:43 +00:00
|
|
|
/**
|
2011-10-02 17:53:33 +00:00
|
|
|
* Return relative multi-level hash subdirectory (with trailing slash)
|
|
|
|
|
* or the empty string if not $wgFileCacheDepth
|
2011-09-29 08:18:20 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected function hashSubdirectory() {
|
|
|
|
|
global $wgFileCacheDepth;
|
|
|
|
|
|
|
|
|
|
$subdir = '';
|
|
|
|
|
if ( $wgFileCacheDepth > 0 ) {
|
|
|
|
|
$hash = md5( $this->mKey );
|
|
|
|
|
for ( $i = 1; $i <= $wgFileCacheDepth; $i++ ) {
|
|
|
|
|
$subdir .= substr( $hash, 0, $i ) . '/';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $subdir;
|
|
|
|
|
}
|
2011-10-02 17:53:33 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Roughly increments the cache misses in the last hour by unique visitors
|
|
|
|
|
* @param $request WebRequest
|
2012-01-12 19:41:18 +00:00
|
|
|
* @return void
|
2011-10-02 17:53:33 +00:00
|
|
|
*/
|
|
|
|
|
public function incrMissesRecent( WebRequest $request ) {
|
|
|
|
|
global $wgMemc;
|
|
|
|
|
if ( mt_rand( 0, self::MISS_FACTOR - 1 ) == 0 ) {
|
2012-10-10 18:13:40 +00:00
|
|
|
# Get a large IP range that should include the user even if that
|
2011-12-27 22:38:44 +00:00
|
|
|
# person's IP address changes
|
2011-10-02 17:53:33 +00:00
|
|
|
$ip = $request->getIP();
|
|
|
|
|
if ( !IP::isValid( $ip ) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$ip = IP::isIPv6( $ip )
|
2011-12-23 07:41:23 +00:00
|
|
|
? IP::sanitizeRange( "$ip/32" )
|
2011-10-02 17:53:33 +00:00
|
|
|
: IP::sanitizeRange( "$ip/16" );
|
|
|
|
|
|
|
|
|
|
# Bail out if a request already came from this range...
|
|
|
|
|
$key = wfMemcKey( get_class( $this ), 'attempt', $this->mType, $this->mKey, $ip );
|
|
|
|
|
if ( $wgMemc->get( $key ) ) {
|
|
|
|
|
return; // possibly the same user
|
|
|
|
|
}
|
2011-10-02 19:44:31 +00:00
|
|
|
$wgMemc->set( $key, 1, self::MISS_TTL_SEC );
|
2011-10-02 17:53:33 +00:00
|
|
|
|
|
|
|
|
# Increment the number of cache misses...
|
|
|
|
|
$key = $this->cacheMissKey();
|
|
|
|
|
if ( $wgMemc->get( $key ) === false ) {
|
2011-10-02 19:44:31 +00:00
|
|
|
$wgMemc->set( $key, 1, self::MISS_TTL_SEC );
|
2011-10-02 17:53:33 +00:00
|
|
|
} else {
|
|
|
|
|
$wgMemc->incr( $key );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Roughly gets the cache misses in the last hour by unique visitors
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function getMissesRecent() {
|
|
|
|
|
global $wgMemc;
|
2013-11-17 20:36:27 +00:00
|
|
|
|
2011-10-02 17:53:33 +00:00
|
|
|
return self::MISS_FACTOR * $wgMemc->get( $this->cacheMissKey() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected function cacheMissKey() {
|
|
|
|
|
return wfMemcKey( get_class( $this ), 'misses', $this->mType, $this->mKey );
|
|
|
|
|
}
|
2011-09-29 08:18:20 +00:00
|
|
|
}
|