1<?php
2
3
4namespace ComboStrap;
5
6
7use DateTime;
8
9/**
10 * Class CacheExpirationFrequencyMeta
11 * @package ComboStrap
12 * Represents the cache expiration date metadata
13 */
14class CacheExpirationDate extends MetadataDateTime
15{
16
17
18    /**
19     * The meta key that has the expiration date
20     */
21    public const PROPERTY_NAME = "date_cache_expiration";
22
23
24    public static function createForPage(ResourceCombo $page): CacheExpirationDate
25    {
26        return (new CacheExpirationDate())
27            ->setResource($page);
28    }
29
30    public function getDefaultValue(): ?DateTime
31    {
32        $resourceCombo = $this->getResource();
33        if (!($resourceCombo instanceof Page)) {
34            return null;
35        }
36        $path = $resourceCombo->getHtmlDocument()->getCachePath();
37        if (!FileSystems::exists($path)) {
38            return null;
39        }
40
41        $cacheIntervalInSecond = Site::getCacheTime();
42        if($cacheIntervalInSecond===-1){
43            return null;
44        }
45
46        /**
47         * Not the modified time (it's modified by a process when the cache is read
48         * for whatever reason)
49         */
50        $expirationTime = FileSystems::getCreationTime($path);
51        if ($cacheIntervalInSecond !== null) {
52            $expirationTime->modify('+' . $cacheIntervalInSecond . ' seconds');
53        }
54
55        return $expirationTime;
56
57    }
58
59
60    public function getValue(): ?DateTime
61    {
62
63        $value = parent::getValue();
64        if ($value === null) {
65            $cronExpression = $this->getResource()->getCacheExpirationFrequency();
66            if ($cronExpression !== null) {
67                try {
68                    $value = Cron::getDate($cronExpression);
69                    parent::setValue($value);
70                } catch (ExceptionCombo $e) {
71                    // nothing, the cron expression is tested when set
72                }
73            }
74        }
75        return $value;
76
77    }
78
79
80    public static function getName(): string
81    {
82        return self::PROPERTY_NAME;
83    }
84
85
86    public function getPersistenceType(): string
87    {
88        return MetadataDokuWikiStore::CURRENT_METADATA;
89    }
90
91
92    public function getCanonical(): string
93    {
94        return CacheExpirationFrequency::PROPERTY_NAME;
95    }
96
97    public function getTab(): string
98    {
99        return MetaManagerForm::TAB_CACHE_VALUE;
100    }
101
102    public function getDescription(): string
103    {
104        return "The next cache expiration date (calculated from the cache frequency expression)";
105    }
106
107    public function getLabel(): string
108    {
109        return "Cache Expiration Date";
110    }
111
112    public function getMutable(): bool
113    {
114        return false;
115    }
116}
117