xref: /plugin/combo/ComboStrap/CacheExpirationDate.php (revision 04fd306c7c155fa133ebb3669986875d65988276)
1c3437056SNickeau<?php
2c3437056SNickeau
3c3437056SNickeau
4c3437056SNickeaunamespace ComboStrap;
5c3437056SNickeau
6c3437056SNickeau
7*04fd306cSNickeauuse ComboStrap\Meta\Api\MetadataDateTime;
8*04fd306cSNickeauuse ComboStrap\Meta\Store\MetadataDokuWikiStore;
9c3437056SNickeauuse DateTime;
10c3437056SNickeau
11c3437056SNickeau/**
12c3437056SNickeau * Class CacheExpirationFrequencyMeta
13c3437056SNickeau * @package ComboStrap
14c3437056SNickeau * Represents the cache expiration date metadata
15c3437056SNickeau */
16c3437056SNickeauclass CacheExpirationDate extends MetadataDateTime
17c3437056SNickeau{
18c3437056SNickeau
19c3437056SNickeau
20c3437056SNickeau    /**
21c3437056SNickeau     * The meta key that has the expiration date
22c3437056SNickeau     */
23c3437056SNickeau    public const PROPERTY_NAME = "date_cache_expiration";
24c3437056SNickeau
25c3437056SNickeau
26c3437056SNickeau    public static function createForPage(ResourceCombo $page): CacheExpirationDate
27c3437056SNickeau    {
28c3437056SNickeau        return (new CacheExpirationDate())
29c3437056SNickeau            ->setResource($page);
30c3437056SNickeau    }
31c3437056SNickeau
32*04fd306cSNickeau    /**
33*04fd306cSNickeau     *
34*04fd306cSNickeau     * @throws ExceptionNotFound - if their is no default value, the HTML document does not exists, the cache is disabled
35*04fd306cSNickeau     *
36*04fd306cSNickeau     */
37*04fd306cSNickeau    public function getDefaultValue(): DateTime
38c3437056SNickeau    {
39c3437056SNickeau        $resourceCombo = $this->getResource();
40*04fd306cSNickeau        if (!($resourceCombo instanceof MarkupPath)) {
41*04fd306cSNickeau            throw new ExceptionNotFound("Cache expiration is only available for page fragment");
42c3437056SNickeau        }
43c3437056SNickeau
44*04fd306cSNickeau        /**
45*04fd306cSNickeau         * We use {@link FetcherMarkup::getContentCachePath()}
46*04fd306cSNickeau         * and not {@link FetcherMarkup::processIfNeededAndGetFetchPath()}
47*04fd306cSNickeau         * to not create the HTML
48*04fd306cSNickeau         */
49*04fd306cSNickeau        try {
50*04fd306cSNickeau            $fetcherMarkup = $resourceCombo->createHtmlFetcherWithItselfAsContextPath();
51*04fd306cSNickeau        } catch (ExceptionNotExists $e) {
52*04fd306cSNickeau            throw new ExceptionNotFound("The executing path does not exist.");
53*04fd306cSNickeau        }
54*04fd306cSNickeau        $path = $fetcherMarkup->getContentCachePath();
55*04fd306cSNickeau        if (!FileSystems::exists($path)) {
56*04fd306cSNickeau            throw new ExceptionNotFound("There is no HTML document created to expire.");
57*04fd306cSNickeau        }
58*04fd306cSNickeau
59*04fd306cSNickeau
60*04fd306cSNickeau        $cacheIntervalInSecond = Site::getXhtmlCacheTime();
61c3437056SNickeau        if ($cacheIntervalInSecond === -1) {
62*04fd306cSNickeau            throw new ExceptionNotFound("Cache has been disabled globally on the site");
63c3437056SNickeau        }
64c3437056SNickeau
65c3437056SNickeau        /**
66c3437056SNickeau         * Not the modified time (it's modified by a process when the cache is read
67c3437056SNickeau         * for whatever reason)
68c3437056SNickeau         */
69c3437056SNickeau        $expirationTime = FileSystems::getCreationTime($path);
70c3437056SNickeau        if ($cacheIntervalInSecond !== null) {
71c3437056SNickeau            $expirationTime->modify('+' . $cacheIntervalInSecond . ' seconds');
72c3437056SNickeau        }
73c3437056SNickeau
74c3437056SNickeau        return $expirationTime;
75c3437056SNickeau
76c3437056SNickeau    }
77c3437056SNickeau
78c3437056SNickeau
79*04fd306cSNickeau    /**
80*04fd306cSNickeau     * @throws ExceptionNotFound
81*04fd306cSNickeau     */
82*04fd306cSNickeau    public function getValue(): DateTime
83c3437056SNickeau    {
84c3437056SNickeau
85*04fd306cSNickeau        try {
86*04fd306cSNickeau            return parent::getValue();
87*04fd306cSNickeau        } catch (ExceptionNotFound $e) {
88*04fd306cSNickeau            $cronExpression = CacheExpirationFrequency::createForPage($this->getResource())->getValue();
89c3437056SNickeau            try {
90c3437056SNickeau                $value = Cron::getDate($cronExpression);
91c3437056SNickeau                parent::setValue($value);
92c3437056SNickeau                return $value;
93*04fd306cSNickeau            } catch (ExceptionBadArgument $badArgument) {
94*04fd306cSNickeau                throw $e;
95*04fd306cSNickeau            }
96*04fd306cSNickeau        }
97c3437056SNickeau
98c3437056SNickeau    }
99c3437056SNickeau
100c3437056SNickeau
101c3437056SNickeau    public static function getName(): string
102c3437056SNickeau    {
103c3437056SNickeau        return self::PROPERTY_NAME;
104c3437056SNickeau    }
105c3437056SNickeau
106c3437056SNickeau
107*04fd306cSNickeau    public static function getPersistenceType(): string
108c3437056SNickeau    {
109c3437056SNickeau        return MetadataDokuWikiStore::CURRENT_METADATA;
110c3437056SNickeau    }
111c3437056SNickeau
112c3437056SNickeau
113*04fd306cSNickeau    public static function getCanonical(): string
114c3437056SNickeau    {
115c3437056SNickeau        return CacheExpirationFrequency::PROPERTY_NAME;
116c3437056SNickeau    }
117c3437056SNickeau
118*04fd306cSNickeau    public static function getTab(): string
119c3437056SNickeau    {
120c3437056SNickeau        return MetaManagerForm::TAB_CACHE_VALUE;
121c3437056SNickeau    }
122c3437056SNickeau
123*04fd306cSNickeau    public static function getDescription(): string
124c3437056SNickeau    {
125c3437056SNickeau        return "The next cache expiration date (calculated from the cache frequency expression)";
126c3437056SNickeau    }
127c3437056SNickeau
128*04fd306cSNickeau    public static function getLabel(): string
129c3437056SNickeau    {
130c3437056SNickeau        return "Cache Expiration Date";
131c3437056SNickeau    }
132c3437056SNickeau
133*04fd306cSNickeau    public static function isMutable(): bool
134c3437056SNickeau    {
135c3437056SNickeau        return false;
136c3437056SNickeau    }
137*04fd306cSNickeau
138*04fd306cSNickeau    public static function isOnForm(): bool
139*04fd306cSNickeau    {
140*04fd306cSNickeau        return true;
141*04fd306cSNickeau    }
142c3437056SNickeau}
143