1 <?php
2 
3 namespace ComboStrap;
4 
5 /**
6  * @deprecated - use the metadata/page properties instead
7  */
8 class CacheTag
9 {
10 
11     public const MARKUP = "cache";
12     public const EXPIRATION_ATTRIBUTE = "expiration";
13     public const PARSING_STATE_UNSUCCESSFUL = "unsuccessful";
14     public const PARSING_STATUS = "status";
15     public const PARSING_STATE_SUCCESSFUL = "successful";
16 
17 
18     public static function handle(TagAttributes $attributes): array
19     {
20 
21         $cronExpression = $attributes->getValue(self::EXPIRATION_ATTRIBUTE);
22         $returnedArray = array(
23             PluginUtility::PAYLOAD => $cronExpression
24         );
25 
26         try {
27             $requestPage = MarkupPath::createPageFromPathObject(
28                 ExecutionContext::getActualOrCreateFromEnv()->getExecutingWikiPath()
29             );
30         } catch (ExceptionNotFound $e) {
31             $message = "No markup executing. Cache expiration date could not be set";
32             LogUtility::error($message, self::MARKUP);
33             return array(
34                 PluginUtility::EXIT_CODE => self::PARSING_STATE_UNSUCCESSFUL,
35                 PluginUtility::EXIT_MESSAGE => $message
36             );
37         }
38 
39         try {
40             CacheExpirationFrequency::createForPage($requestPage)
41                 ->setValue($cronExpression)
42                 ->sendToWriteStore();
43         } catch (ExceptionBadSyntax $e) {
44             $returnedArray[PluginUtility::EXIT_CODE] = self::PARSING_STATE_UNSUCCESSFUL;
45             $returnedArray[PluginUtility::EXIT_MESSAGE] = "The expression ($cronExpression) is not a valid expression";
46             return $returnedArray;
47         } catch (ExceptionBadArgument $e) {
48             // It should not happen
49             $message = "Internal Error: The cache expiration date could not be stored: {$e->getMessage()}";
50             LogUtility::error($message, self::MARKUP, $e);
51             $returnedArray[PluginUtility::EXIT_CODE] = self::PARSING_STATE_UNSUCCESSFUL;
52             $returnedArray[PluginUtility::EXIT_MESSAGE] = $message;
53             return $returnedArray;
54         }
55 
56         LogUtility::warning("The cache syntax component has been deprecated for the cache frequency metadata", CacheExpirationFrequency::PROPERTY_NAME);
57 
58         $returnedArray[PluginUtility::EXIT_CODE] = self::PARSING_STATE_SUCCESSFUL;
59         $returnedArray[PluginUtility::PAYLOAD] = $cronExpression;
60         return $returnedArray;
61 
62     }
63 
64     public static function renderXhtml(array $data): string
65     {
66 
67         if ($data[PluginUtility::EXIT_CODE] !== CacheTag::PARSING_STATE_SUCCESSFUL) {
68             $message = $data[PluginUtility::EXIT_MESSAGE];
69             LogUtility::error($message, CacheExpirationFrequency::PROPERTY_NAME);
70         }
71         return "";
72 
73     }
74 
75     public static function metadata($data)
76     {
77 
78         if ($data[PluginUtility::EXIT_CODE] === CacheTag::PARSING_STATE_SUCCESSFUL) {
79             $cronExpression = $data[PluginUtility::PAYLOAD];
80             try {
81                 $requestPage = MarkupPath::createFromRequestedPage();
82             } catch (ExceptionNotFound $e) {
83                 LogUtility::error("Unable to store the cache expiration date because no requested page", self::MARKUP, $e);
84                 return;
85             }
86             try {
87                 CacheExpirationFrequency::createForPage($requestPage)
88                     ->setValue($cronExpression)
89                     ->sendToWriteStore();
90             } catch (ExceptionCompile $e) {
91                 // should not happen as we have test for its validity
92             }
93 
94         }
95 
96     }
97 }
98