| 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 |
use MediaWiki\Cache\CacheKeyHelper; |
| 25 |
use MediaWiki\Cache\FileCacheBase; |
| 26 |
use MediaWiki\Context\IContextSource; |
| 27 |
use MediaWiki\HookContainer\HookRunner; |
| 28 |
use MediaWiki\MainConfigNames; |
| 29 |
use MediaWiki\MediaWikiServices; |
| 30 |
use MediaWiki\Page\PageIdentity; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class HTMLFileCache extends FileCacheBase { |
| 40 |
public const MODE_NORMAL = 0; |
| 41 |
public const MODE_OUTAGE = 1; |
| 42 |
public const MODE_REBUILD = 2; |
| 43 |
|
| 44 |
private const CACHEABLE_ACTIONS = [ |
| 45 |
'view', |
| 46 |
'history', |
| 47 |
]; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public function __construct( $page, $action ) { |
| 54 |
parent::__construct(); |
| 55 |
|
| 56 |
if ( !in_array( $action, self::CACHEABLE_ACTIONS ) ) { |
| 57 |
throw new InvalidArgumentException( 'Invalid file cache type given.' ); |
| 58 |
} |
| 59 |
|
| 60 |
$this->mKey = CacheKeyHelper::getKeyForPage( $page ); |
| 61 |
$this->mType = (string)$action; |
| 62 |
$this->mExt = 'html'; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
protected function cacheDirectory() { |
| 70 |
return $this->baseCacheDirectory(); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
protected function typeSubdirectory() { |
| 80 |
if ( $this->mType === 'view' ) { |
| 81 |
return ''; |
| 82 |
} else { |
| 83 |
return $this->mType . '/'; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public static function useFileCache( IContextSource $context, $mode = self::MODE_NORMAL ) { |
| 94 |
$services = MediaWikiServices::getInstance(); |
| 95 |
$config = $services->getMainConfig(); |
| 96 |
|
| 97 |
if ( !$config->get( MainConfigNames::UseFileCache ) && $mode !== self::MODE_REBUILD ) { |
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
$queryVals = $context->getRequest()->getValues(); |
| 103 |
foreach ( $queryVals as $query => $val ) { |
| 104 |
if ( $query === 'title' || $query === 'curid' ) { |
| 105 |
continue; |
| 106 |
|
| 107 |
} elseif ( $query === 'action' && in_array( $val, self::CACHEABLE_ACTIONS ) ) { |
| 108 |
continue; |
| 109 |
|
| 110 |
} elseif ( $query === 'maxage' || $query === 'smaxage' ) { |
| 111 |
continue; |
| 112 |
|
| 113 |
} elseif ( $query === 'uselang' ) { |
| 114 |
continue; |
| 115 |
} |
| 116 |
|
| 117 |
return false; |
| 118 |
} |
| 119 |
|
| 120 |
$user = $context->getUser(); |
| 121 |
|
| 122 |
|
| 123 |
$ulang = $context->getLanguage(); |
| 124 |
|
| 125 |
|
| 126 |
if ( $user->isRegistered() || |
| 127 |
!$ulang->equals( $services->getContentLanguage() ) ) { |
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
$userHasNewMessages = $services->getTalkPageNotificationManager()->userHasNewMessages( $user ); |
| 132 |
if ( ( $mode === self::MODE_NORMAL ) && $userHasNewMessages ) { |
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
return ( new HookRunner( $services->getHookContainer() ) )->onHTMLFileCache__useFileCache( $context ); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public function loadFromFileCache( IContextSource $context, $mode = self::MODE_NORMAL ) { |
| 147 |
wfDebug( __METHOD__ . "()" ); |
| 148 |
$filename = $this->cachePath(); |
| 149 |
|
| 150 |
if ( $mode === self::MODE_OUTAGE ) { |
| 151 |
|
| 152 |
$context->getTitle()->resetArticleID( 0 ); |
| 153 |
} |
| 154 |
|
| 155 |
$context->getOutput()->sendCacheControl(); |
| 156 |
header( "Content-Type: {$this->options->get( MainConfigNames::MimeType )}; charset=UTF-8" ); |
| 157 |
header( 'Content-Language: ' . |
| 158 |
MediaWikiServices::getInstance()->getContentLanguage()->getHtmlCode() ); |
| 159 |
if ( $this->useGzip() ) { |
| 160 |
if ( wfClientAcceptsGzip() ) { |
| 161 |
header( 'Content-Encoding: gzip' ); |
| 162 |
readfile( $filename ); |
| 163 |
} else { |
| 164 |
|
| 165 |
wfDebug( __METHOD__ . " uncompressing cache file and sending it" ); |
| 166 |
readgzfile( $filename ); |
| 167 |
} |
| 168 |
} else { |
| 169 |
readfile( $filename ); |
| 170 |
} |
| 171 |
|
| 172 |
$context->getOutput()->disable(); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
public function saveToFileCache( $text ) { |
| 188 |
if ( strlen( $text ) < 512 ) { |
| 189 |
|
| 190 |
return $text; |
| 191 |
} |
| 192 |
|
| 193 |
wfDebug( __METHOD__ . "()\n", 'private' ); |
| 194 |
|
| 195 |
$now = wfTimestampNow(); |
| 196 |
if ( $this->useGzip() ) { |
| 197 |
$text = str_replace( |
| 198 |
'</html>', '<!-- Cached/compressed ' . $now . " -->\n</html>", $text ); |
| 199 |
} else { |
| 200 |
$text = str_replace( |
| 201 |
'</html>', '<!-- Cached ' . $now . " -->\n</html>", $text ); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
$compressed = $this->saveText( $text ); |
| 206 |
if ( $compressed === false ) { |
| 207 |
return $text; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
if ( $this->useGzip() && wfClientAcceptsGzip() ) { |
| 213 |
header( 'Content-Encoding: gzip' ); |
| 214 |
|
| 215 |
return $compressed; |
| 216 |
} |
| 217 |
|
| 218 |
return $text; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
public static function clearFileCache( $page ) { |
| 228 |
$config = MediaWikiServices::getInstance()->getMainConfig(); |
| 229 |
if ( !$config->get( MainConfigNames::UseFileCache ) ) { |
| 230 |
return false; |
| 231 |
} |
| 232 |
|
| 233 |
foreach ( self::CACHEABLE_ACTIONS as $type ) { |
| 234 |
$fc = new self( $page, $type ); |
| 235 |
$fc->clearCache(); |
| 236 |
} |
| 237 |
|
| 238 |
return true; |
| 239 |
} |
| 240 |
} |