1<?php 2 3 4namespace ComboStrap; 5 6 7use dokuwiki\Cache\CacheInstructions; 8 9class InstructionsDocument extends PageCompilerDocument 10{ 11 12 13 14 /** 15 * @var CacheInstructions 16 */ 17 private $cache; 18 19 20 21 /** 22 * InstructionsDocument constructor. 23 * @var Page $page 24 */ 25 public function __construct(Page $page) 26 { 27 parent::__construct($page); 28 29 30 $path = $page->getPath(); 31 $id = $path->getDokuwikiId(); 32 /** 33 * The local path is part of the key cache and should be the same 34 * than dokuwiki 35 * 36 * For whatever reason, Dokuwiki uses: 37 * * `/` as separator on Windows 38 * * and Windows short path `GERARD~1` not gerardnico 39 * See {@link wikiFN()} 40 * There is also a cache in the function 41 * 42 * We can't use our {@link Path} class because the 43 * path is on windows format without the short path format 44 */ 45 $localFile = wikiFN($id); 46 $this->cache = new CacheInstructions($id, $localFile); 47 48 49 } 50 51 52 53 54 function getExtension(): string 55 { 56 return "i"; 57 } 58 59 function process(): CachedDocument 60 { 61 62 if (!$this->shouldProcess()) { 63 return $this; 64 } 65 66 /** 67 * The id is not passed while on handler 68 * Therefore the global id should be set 69 */ 70 global $ID; 71 $oldId = $ID; 72 $ID = $this->getPage()->getPath()->getDokuwikiId(); 73 74 /** 75 * Get the instructions 76 * Adapted from {@link p_cached_instructions()} 77 */ 78 try { 79 $text = $this->getPage()->getTextContent(); 80 $instructions = p_get_instructions($text); 81 } finally { 82 // close restore ID 83 $ID = $oldId; 84 } 85 86 if (!$this->cache->storeCache($instructions)) { 87 $message = 'Unable to save the parsed instructions cache file. Hint: disk full; file permissions; safe_mode setting ?'; 88 LogUtility::msg($message, LogUtility::LVL_MSG_ERROR); 89 $this->setContent([]); 90 return $this; 91 } 92 93 // the parsing may have set new metadata values 94 $this->getPage()->rebuild(); 95 96 $this->setContent($instructions); 97 return $this; 98 99 } 100 101 102 public function getFileContent() 103 { 104 /** 105 * The data is {@link serialize serialized} for instructions 106 * we can't use the parent method that retrieve text by default 107 */ 108 return $this->cache->retrieveCache(); 109 110 } 111 112 113 function getRendererName(): string 114 { 115 return "i"; 116 } 117 118 public function getCachePath(): LocalPath 119 { 120 return LocalPath::create($this->cache->cache); 121 } 122 123 public function shouldProcess(): bool 124 { 125 126 global $ID; 127 $keep = $ID; 128 try { 129 $ID = $this->getPage()->getDokuwikiId(); 130 return $this->cache->useCache() === false; 131 } finally { 132 $ID = $keep; 133 } 134 } 135 136 137 /** 138 */ 139 public function storeContent($content) 140 { 141 142 /** 143 * Store the content 144 */ 145 $this->cache->storeCache($content); 146 return $this; 147 148 } 149 150 151 152 153} 154