1<?php
2
3
4namespace ComboStrap;
5
6
7use ComboStrap\Meta\Api\MetadataText;
8use ComboStrap\Meta\Field\PageH1;
9use ComboStrap\Meta\Store\MetadataDokuWikiStore;
10
11class PageTitle extends MetadataText
12{
13
14    public const PROPERTY_NAME = 'title';
15    public const TITLE = 'title';
16
17    public static function createForMarkup($page): PageTitle
18    {
19        return (new PageTitle())
20            ->setResource($page);
21    }
22
23    public static function getTab(): string
24    {
25        return MetaManagerForm::TAB_PAGE_VALUE;
26    }
27
28    public static function getDescription(): string
29    {
30        return "The page title is a description advertised to external application such as search engine and browser.";
31    }
32
33    public static function getLabel(): string
34    {
35        return "Title";
36    }
37
38    static public function getName(): string
39    {
40        return self::PROPERTY_NAME;
41    }
42
43    public static function getPersistenceType(): string
44    {
45        return MetadataDokuWikiStore::PERSISTENT_DOKUWIKI_KEY;
46    }
47
48    public static function isMutable(): bool
49    {
50        return true;
51    }
52
53    /**
54     * `title` is created by DokuWiki
55     * in current but not persistent
56     * and hold the heading 1, see {@link p_get_first_heading}
57     */
58    public function getDefaultValue(): string
59    {
60
61        $resource = $this->getResource();
62        if (!($resource instanceof MarkupPath)) {
63            LogUtility::internalError("Resource that are not page have no title");
64            return ResourceName::getFromPath($resource->getPathObject());
65        }
66        if ($resource->isRootHomePage() && !empty(Site::getTagLine())) {
67            return Site::getTagLine();
68        }
69        return PageH1::createForPage($this->getResource())
70            ->getValueOrDefault();
71
72    }
73
74    public function buildFromReadStore()
75    {
76        $metadataStore = $this->getReadStore();
77        /**
78         * We got a conflict Dokuwiki stores a `title` meta in the current
79         * See first line of {@link \Doku_Renderer_metadata::header()}
80         */
81        $isWikiDisabled = ExecutionContext::getActualOrCreateFromEnv()
82            ->getConfig()
83            ->isHeadingWikiComponentDisabled();
84        if ($isWikiDisabled && $metadataStore instanceof MetadataDokuWikiStore) {
85            $this->wasBuild = true;
86            $dataCurrentAndPersistent = $metadataStore->getDataCurrentAndPersistent();
87            $value = $dataCurrentAndPersistent[MetadataDokuWikiStore::PERSISTENT_DOKUWIKI_KEY][$this->getName()] ?? null;
88            $this->setFromStoreValueWithoutException($value);
89            return $this;
90        }
91        return parent::buildFromReadStore();
92    }
93
94
95
96    /**
97     * @return string
98     */
99    public function getValueOrDefault(): string
100    {
101        try {
102            return $this->getValue();
103        } catch (ExceptionNotFound $e) {
104            return $this->getDefaultValue();
105        }
106    }
107
108
109    public static function getCanonical(): string
110    {
111        return self::TITLE;
112    }
113
114
115    public static function isOnForm(): bool
116    {
117        return true;
118    }
119
120}
121