xref: /template/strap/ComboStrap/ModificationDate.php (revision 04fd306c7c155fa133ebb3669986875d65988276)
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            $createdMeta = $store->getCurrentFromName('date')['modified'];
44            if (empty($createdMeta)) {
45                $createdMeta = $store->getFromName('date')['modified'];
46                if (empty($createdMeta)) {
47                    return $this;
48                }
49            }
50            // the data in dokuwiki is saved as timestamp
51            $datetime = new DateTime();
52            if (!is_int($createdMeta)) {
53                LogUtility::msg("The modification time in the dokuwiki meta is not an integer");
54                return $this;
55            }
56            $datetime->setTimestamp($createdMeta);
57            $this->setValue($datetime);
58            return $this;
59
60        }
61
62
63    }
64
65
66    static public function getDescription(): string
67    {
68        return "The last modification date of the page"; // resource
69    }
70
71    static public function getLabel(): string
72    {
73        return "Modification Date";
74    }
75
76    static public function getName(): string
77    {
78        return self::PROPERTY_NAME;
79    }
80
81    static public function getPersistenceType(): string
82    {
83        return Metadata::DERIVED_METADATA;
84    }
85
86    static public function isMutable(): bool
87    {
88        return false;
89    }
90
91    /**
92     * @throws ExceptionNotFound - if the file does not exists
93     */
94    public function getDefaultValue(): DateTime
95    {
96
97        try {
98            return FileSystems::getModifiedTime($this->getResource()->getPathObject());
99        } catch (ExceptionNotFound $e) {
100            return CreationDate::createForPage($this->getResource())->getValue();
101        }
102
103    }
104
105    static public function getCanonical(): string
106    {
107        return Metadata::CANONICAL;
108    }
109
110
111    static public function isOnForm(): bool
112    {
113        return true;
114    }
115}
116