1<?php
2
3namespace ComboStrap;
4
5use ComboStrap\Meta\Api\Metadata;
6use ComboStrap\Meta\Api\MetadataDateTime;
7use ComboStrap\Meta\Store\MetadataDokuWikiStore;
8use DateTime;
9
10class ModificationDate extends MetadataDateTime
11{
12
13    public const PROPERTY_NAME = 'date_modified';
14
15    public static function createForPage(MarkupPath $page)
16    {
17        return (new ModificationDate())
18            ->setResource($page);
19    }
20
21    static public function getTab(): string
22    {
23        return MetaManagerForm::TAB_PAGE_VALUE;
24    }
25
26    public function buildFromReadStore(): MetadataDateTime
27    {
28        $store = $this->getReadStore();
29        if (!($store instanceof MetadataDokuWikiStore)) {
30            return parent::buildFromReadStore();
31        }
32
33        try {
34            $modificationTime = FileSystems::getModifiedTime($this->getResource()->getPathObject());
35            $this->setValue($modificationTime);
36            return $this;
37        } catch (ExceptionNotFound $e) {
38
39            /**
40             * Dokuwiki
41             * Why do they store the date of the file while it's in the file system ?
42             */
43            $currentDateMeta = $store->getCurrentFromName('date');
44            $createdMeta = null;
45            if ($currentDateMeta !== null) {
46                $createdMeta = $currentDateMeta['modified'] ?? null;
47            }
48            if (empty($createdMeta)) {
49                $createdMeta = $currentDateMeta['modified'] ?? null;
50                if (empty($createdMeta)) {
51                    return $this;
52                }
53            }
54            // the data in dokuwiki is saved as timestamp
55            $datetime = new DateTime();
56            if (!is_int($createdMeta)) {
57                LogUtility::msg("The modification time in the dokuwiki meta is not an integer");
58                return $this;
59            }
60            $datetime->setTimestamp($createdMeta);
61            $this->setValue($datetime);
62            return $this;
63
64        }
65
66
67    }
68
69
70    static public function getDescription(): string
71    {
72        return "The last modification date of the page"; // resource
73    }
74
75    static public function getLabel(): string
76    {
77        return "Modification Date";
78    }
79
80    static public function getName(): string
81    {
82        return self::PROPERTY_NAME;
83    }
84
85    static public function getPersistenceType(): string
86    {
87        return Metadata::DERIVED_METADATA;
88    }
89
90    static public function isMutable(): bool
91    {
92        return false;
93    }
94
95    /**
96     * @throws ExceptionNotFound - if the file does not exists
97     */
98    public function getDefaultValue(): DateTime
99    {
100
101        try {
102            return FileSystems::getModifiedTime($this->getResource()->getPathObject());
103        } catch (ExceptionNotFound $e) {
104            return CreationDate::createForPage($this->getResource())->getValue();
105        }
106
107    }
108
109    static public function getCanonical(): string
110    {
111        return Metadata::CANONICAL;
112    }
113
114
115    static public function isOnForm(): bool
116    {
117        return true;
118    }
119}
120