Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
page.php
1<?php
3
10
15class Page
16{
20 protected static $instance = null;
24 private $cacheKey = null;
28 private $canCache = true;
29
33 private $storage = null;
34
38 private $cacheProvider = null;
39
40 private $voting = true;
47 public function __construct($requestUri = null, $host = null, $privateKey = null)
48 {
49 if (func_num_args())
50 {
51 $cacheKey = static::convertUriToPath($requestUri, $host, $privateKey);
52 $this->init($cacheKey);
53 }
54 }
55
56 protected function init($cacheKey)
57 {
58 if (is_string($cacheKey) && mb_strlen($cacheKey))
59 {
60 $this->cacheKey = $cacheKey;
61 $this->storage = $this->getStaticHtmlStorage($this->cacheKey);
62 }
63 }
64
65 public static function createFromCacheKey($cacheKey)
66 {
67 $storage = new static();
68 $storage->init($cacheKey);
69
70 return $storage;
71 }
72
78 public static function getInstance()
79 {
80 if (!isset(static::$instance))
81 {
82 $cacheProvider = static::getCacheProvider();
83 $privateKey = $cacheProvider !== null ? $cacheProvider->getCachePrivateKey() : null;
84
85 static::$instance = new static(
86 Helper::getRequestUri(),
87 Helper::getHttpHost(),
88 Helper::getRealPrivateKey($privateKey)
89 );
90
91 if ($cacheProvider !== null)
92 {
93 static::$instance->setCacheProvider($cacheProvider);
94 }
95 }
96
97 return static::$instance;
98 }
99
105 public function getStorage()
106 {
107 return $this->storage;
108 }
109
110 public function setCacheProvider(CacheProvider $provider)
111 {
112 $this->cacheProvider = $provider;
113 }
114
118 private static function getCacheProvider()
119 {
120 foreach (GetModuleEvents("main", "OnGetStaticCacheProvider", true) as $arEvent)
121 {
122 $provider = ExecuteModuleEventEx($arEvent);
123 if (is_object($provider) && $provider instanceof CacheProvider)
124 {
125 return $provider;
126 }
127 }
128
129 return null;
130 }
131
132 /*
133 * Returns private cache key
134 */
135 public static function getPrivateKey()
136 {
137 $cacheProvider = static::getCacheProvider();
138 return $cacheProvider !== null ? $cacheProvider->getCachePrivateKey() : null;
139 }
148 public static function convertUriToPath($uri, $host = null, $privateKey = null)
149 {
150 return Helper::convertUriToPath($uri, $host, $privateKey);
151 }
152
157 public function getCacheKey()
158 {
159 return $this->cacheKey;
160 }
168 public function write($content, $md5)
169 {
170 if ($this->storage === null)
171 {
172 return false;
173 }
174
175 $cacheSize = $this->storage->getSize();
176 $written = $this->storage->write($content."<!--".$md5."-->", $md5);
177 if ($written !== false && $this->storage->shouldCountQuota())
178 {
179 $delta = $cacheSize !== false ? $written - $cacheSize : $written;
180 if ($delta !== 0)
181 {
182 Helper::updateCacheFileSize($delta);
183 }
184 }
185
186 return $written;
187 }
188
194 public function read()
195 {
196 if ($this->storage !== null)
197 {
198 return $this->storage->read();
199 }
200
201 return false;
202 }
203
209 public function delete()
210 {
211 if ($this->storage === null)
212 {
213 return false;
214 }
215
216 $deletedSize = $this->storage->delete();
217 if ($deletedSize !== false && $this->storage->shouldCountQuota())
218 {
219 Helper::updateCacheFileSize(-$deletedSize);
220 }
221
222 PageManager::deleteByCacheKey($this->cacheKey);
223
224 return $deletedSize;
225 }
226
231 public function deleteAll()
232 {
233 if ($this->storage === null)
234 {
235 return false;
236 }
237
238 $this->storage->deleteAll();
239
240 if ($this->storage->shouldCountQuota())
241 {
242 Helper::updateCacheFileSize(false);
243 }
244
245 PageTable::deleteAll();
246
247 return true;
248 }
249
254 public function getLastModified()
255 {
256 if ($this->storage !== null)
257 {
258 return $this->storage->getLastModified();
259 }
260
261 return false;
262 }
263
269 public function exists()
270 {
271 if ($this->storage !== null)
272 {
273 return $this->storage->exists();
274 }
275
276 return false;
277 }
278
283 public function getMd5()
284 {
285 if ($this->storage !== null)
286 {
287 return $this->storage->getMd5();
288 }
289
290 return false;
291 }
292
297 public function getSize()
298 {
299 if ($this->storage !== null)
300 {
301 return $this->storage->getSize();
302 }
303
304 return false;
305 }
306
312 public function isCacheable()
313 {
314 if ($this->storage === null)
315 {
316 return false;
317 }
318
319 if ($this->cacheProvider !== null && $this->cacheProvider->isCacheable() === false)
320 {
321 return false;
322 }
323
324 if (isset(\Bitrix\Main\Application::getInstance()->getKernelSession()["SESS_SHOW_TIME_EXEC"]) && (\Bitrix\Main\Application::getInstance()->getKernelSession()["SESS_SHOW_TIME_EXEC"] == 'Y'))
325 {
326 return false;
327 }
328 elseif (isset(\Bitrix\Main\Application::getInstance()->getKernelSession()["SHOW_SQL_STAT"]) && (\Bitrix\Main\Application::getInstance()->getKernelSession()["SHOW_SQL_STAT"] == 'Y'))
329 {
330 return false;
331 }
332 elseif (isset(\Bitrix\Main\Application::getInstance()->getKernelSession()["SHOW_CACHE_STAT"]) && (\Bitrix\Main\Application::getInstance()->getKernelSession()["SHOW_CACHE_STAT"] == 'Y'))
333 {
334 return false;
335 }
336
337 $httpStatus = intval(\CHTTP::GetLastStatus());
338 if ($httpStatus == 200 || $httpStatus === 0)
339 {
340 return $this->canCache;
341 }
342
343 return false;
344 }
345
351 public function markNonCacheable()
352 {
353 $this->canCache = false;
354
355 if (Logger::isOn())
356 {
357 $debugBacktrace = debug_backtrace();
358
359 Logger::log(array(
360 "TYPE" => Debug\Logger::TYPE_PAGE_NOT_CACHEABLE,
361 "MESSAGE" => "File: ".$debugBacktrace[0]["file"].":".$debugBacktrace[0]["line"]
362 ));
363 }
364 }
365
366 public function setUserPrivateKey()
367 {
368 if ($this->cacheProvider !== null)
369 {
370 $this->cacheProvider->setUserPrivateKey();
371 }
372 }
373
374 public function onBeforeEndBufferContent()
375 {
376 if ($this->cacheProvider !== null)
377 {
378 $this->cacheProvider->onBeforeEndBufferContent();
379 }
380 }
381
388 public static function getStaticHtmlStorage($cacheKey)
389 {
390 $configuration = array();
391 $htmlCacheOptions = Helper::getOptions();
392 $storage = $htmlCacheOptions["STORAGE"] ?? false;
393
394 if (in_array($storage, array("memcached", "memcached_cluster")))
395 {
396 if (extension_loaded("memcache"))
397 {
398 return new Data\MemcachedStorage($cacheKey, $configuration, $htmlCacheOptions);
399 }
400 else
401 {
402 return null;
403 }
404 }
405 else
406 {
407 return new Data\FileStorage($cacheKey, $configuration, $htmlCacheOptions);
408 }
409 }
410
411 public function enableVoting()
412 {
413 $this->voting = true;
414 }
415
416 public function disableVoting()
417 {
418 $this->voting = false;
419 }
420
421 public function isVotingEnabled()
422 {
423 return $this->voting;
424 }
425
431 public function giveNegativeComponentVote($context = "")
432 {
433 if (
434 defined("USE_HTML_STATIC_CACHE")
435 && USE_HTML_STATIC_CACHE === true
436 && StaticArea::getCurrentDynamicId() === false //Voting doesn't work inside a dynamic area
437 )
438 {
439 if (!$this->isVotingEnabled())
440 {
441 return;
442 }
443
444 $this->canCache = false;
445
446 Debug\Logger::log(array(
447 "TYPE" => Debug\Logger::TYPE_COMPONENT_VOTING,
448 "MESSAGE" => $context
449 ));
450 }
451 }
452}
453
454class_alias("Bitrix\\Main\\Composite\\Page", "Bitrix\\Main\\Data\\StaticHtmlCache");
static convertUriToPath($uri, $host=null, $privateKey=null)
Definition page.php:148
static createFromCacheKey($cacheKey)
Definition page.php:65
write($content, $md5)
Definition page.php:168
__construct($requestUri=null, $host=null, $privateKey=null)
Definition page.php:47
setCacheProvider(CacheProvider $provider)
Definition page.php:110
giveNegativeComponentVote($context="")
Definition page.php:431
static getStaticHtmlStorage($cacheKey)
Definition page.php:388