1<?php
2
3
4use ComboStrap\CacheExpirationFrequency;
5use ComboStrap\ExceptionCombo;
6use ComboStrap\LogUtility;
7use ComboStrap\Page;
8use ComboStrap\PluginUtility;
9use ComboStrap\TagAttributes;
10
11
12require_once(__DIR__ . '/../ComboStrap/PluginUtility.php');
13
14/**
15 * Set the cache of the bar
16 * Ie add the possibility to add a time
17 * over {@link \dokuwiki\Parsing\ParserMode\Nocache}
18 *
19 * Depend on the cron dependency
20 * https://github.com/dragonmantank/cron-expression
21 * @deprecated
22 */
23class syntax_plugin_combo_cache extends DokuWiki_Syntax_Plugin
24{
25
26
27    const TAG = "cache";
28
29    const PARSING_STATUS = "status";
30    const PARSING_STATE_SUCCESSFUL = "successful";
31    const PARSING_STATE_UNSUCCESSFUL = "unsuccessful";
32
33    const EXPIRATION_ATTRIBUTE = "expiration";
34
35
36    function getType()
37    {
38        return 'protected';
39    }
40
41    /**
42     * How Dokuwiki will add P element
43     *
44     *  * 'normal' - The plugin can be used inside paragraphs (inline)
45     *  * 'block'  - Open paragraphs need to be closed before plugin output - block should not be inside paragraphs
46     *  * 'stack'  - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs
47     *
48     * @see DokuWiki_Syntax_Plugin::getPType()
49     */
50    function getPType()
51    {
52        return 'normal';
53    }
54
55    function getAllowedTypes()
56    {
57        return array();
58    }
59
60    function getSort()
61    {
62        return 201;
63    }
64
65
66    function connectTo($mode)
67    {
68
69        $this->Lexer->addSpecialPattern(PluginUtility::getVoidElementTagPattern(self::TAG), $mode, PluginUtility::getModeFromTag($this->getPluginComponent()));
70
71    }
72
73
74    function handle($match, $state, $pos, Doku_Handler $handler)
75    {
76
77        switch ($state) {
78
79
80            case DOKU_LEXER_SPECIAL :
81
82                $attributes = TagAttributes::createFromTagMatch($match);
83                $value = $attributes->getValue(self::EXPIRATION_ATTRIBUTE);
84                $status = self::PARSING_STATE_SUCCESSFUL;
85
86
87                $requestPage = Page::createPageFromRequestedPage();
88
89                try {
90                    CacheExpirationFrequency::createForPage($requestPage)
91                        ->setValue($value)
92                        ->sendToWriteStore();
93                } catch (ExceptionCombo $e) {
94                    $status = self::PARSING_STATE_UNSUCCESSFUL;
95                }
96
97                LogUtility::msg("The cache syntax component has been deprecated for the cache frequency metadata", LogUtility::LVL_MSG_INFO, CacheExpirationFrequency::PROPERTY_NAME);
98
99                return array(
100                    PluginUtility::STATE => $state,
101                    self::PARSING_STATUS => $status,
102                    PluginUtility::PAYLOAD => $value
103                );
104
105
106        }
107        return array();
108
109    }
110
111    /**
112     * Render the output
113     * @param string $format
114     * @param Doku_Renderer $renderer
115     * @param array $data - what the function handle() return'ed
116     * @return boolean - rendered correctly? (however, returned value is not used at the moment)
117     * @see DokuWiki_Syntax_Plugin::render()
118     *
119     *
120     */
121    function render($format, Doku_Renderer $renderer, $data): bool
122    {
123
124        switch ($format) {
125
126            case 'xhtml':
127                if ($data[self::PARSING_STATUS] !== self::PARSING_STATE_SUCCESSFUL) {
128                    $cronExpression = $data[PluginUtility::PAYLOAD];
129                    LogUtility::msg("The expression ($cronExpression) is not a valid expression", LogUtility::LVL_MSG_ERROR, CacheExpirationFrequency::PROPERTY_NAME);
130                }
131                break;
132
133            case "metadata":
134                if ($data[self::PARSING_STATUS] === self::PARSING_STATE_SUCCESSFUL) {
135                    $cronExpression = $data[PluginUtility::PAYLOAD];
136                    $requestPage = Page::createPageFromRequestedPage();
137                    try {
138                        CacheExpirationFrequency::createForPage($requestPage)
139                            ->setValue($cronExpression)
140                            ->sendToWriteStore();
141                    } catch (ExceptionCombo $e) {
142                        // should not happen as we test for its validity
143                    }
144
145                }
146
147
148        }
149        // unsupported $mode
150        return false;
151    }
152
153
154}
155
156