1<?php 2 3use ComboStrap\DokuPath; 4use ComboStrap\LogUtility; 5use ComboStrap\PluginUtility; 6use ComboStrap\Resources; 7use ComboStrap\Site; 8use dokuwiki\Cache\CacheRenderer; 9 10if (!defined('DOKU_INC')) die(); 11 12/** 13 * 14 * 15 * Add the snippet needed by the components 16 * 17 */ 18class action_plugin_combo_snippets extends DokuWiki_Action_Plugin 19{ 20 21 const COMBO_CACHE_PREFIX = "combo:cache:"; 22 23 /** 24 * @var bool - to trace if the header output was called 25 */ 26 private $headerOutputWasCalled = false; 27 28 function __construct() 29 { 30 // enable direct access to language strings 31 // ie $this->lang 32 $this->setupLocale(); 33 } 34 35 public function register(Doku_Event_Handler $controller) 36 { 37 38 /** 39 * To add the snippets in the header 40 */ 41 $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'componentSnippetHead', array()); 42 43 /** 44 * To add the snippets in the content 45 * if they have not been added to the header 46 * 47 * Not https://www.dokuwiki.org/devel:event:tpl_content_display TPL_ACT_RENDER 48 * or https://www.dokuwiki.org/devel:event:tpl_act_render 49 * because it works only for the main content 50 * in {@link tpl_content()} 51 * 52 * We use 53 * https://www.dokuwiki.org/devel:event:renderer_content_postprocess 54 * that is in {@link p_render()} and takes into account also the slot page. 55 */ 56 $controller->register_hook('RENDERER_CONTENT_POSTPROCESS', 'AFTER', $this, 'componentSnippetContent', array()); 57 58 /** 59 * To reset the value 60 */ 61 $controller->register_hook('DOKUWIKI_DONE', 'BEFORE', $this, 'close', array()); 62 63 64 /** 65 * To log the cache used by bar 66 */ 67 $controller->register_hook('PARSER_CACHE_USE', 'AFTER', $this, 'barParsed', array()); 68 69 } 70 71 /** 72 * Reset variable 73 * Otherwise in test, when we call it two times, it just fail 74 */ 75 function close() 76 { 77 78 $this->headerOutputWasCalled = false; 79 80 /** 81 * Fighting the fact that in 7.2, 82 * there is still a cache 83 */ 84 PluginUtility::initSnippetManager(); 85 86 } 87 88 /** 89 * Dokuwiki has already a canonical methodology 90 * https://www.dokuwiki.org/canonical 91 * 92 * @param $event 93 */ 94 function componentSnippetHead($event) 95 { 96 97 98 global $ID; 99 if (empty($ID)) { 100 global $_SERVER; 101 $requestUri = $_SERVER['REQUEST_URI']; 102 if (!strpos($requestUri, "/lib/exe/ajax.php") !== false) { 103 global $_REQUEST; 104 $call = $_REQUEST['call']; 105 if ($call != action_plugin_combo_webcode::CALL_ID) { 106 return; 107 } 108 } 109 } 110 111 /** 112 * Advertise that the header output was called 113 * If the user is using another template 114 * than strap that does not put the component snippet 115 * in the head 116 * Used in 117 */ 118 $this->headerOutputWasCalled = true; 119 120 $snippetManager = PluginUtility::getSnippetManager(); 121 122 /** 123 * For each processed bar in the page 124 * * retrieve the snippets from the cache or store the process one 125 * * add the cache information in meta 126 */ 127 $bars = $snippetManager->getBarsOfPage(); 128 foreach ($bars as $barId => $servedFromCache) { 129 130 // Add cache information into the head meta 131 // to test 132 $event->data["meta"][] = array("name" => self::COMBO_CACHE_PREFIX . $barId, "content" => var_export($servedFromCache, true)); 133 134 // Get or store the data 135 $cache = new \dokuwiki\Cache\Cache($barId, "snippet"); 136 $barFileSystemPath = DokuPath::createPagePathFromPath(DokuPath::PATH_SEPARATOR . $barId)->getFileSystemPath(); 137 $dependencies = array( 138 "files" => [ 139 $barFileSystemPath, 140 Resources::getComboHome() . "/plugin.info.txt" 141 ] 142 ); 143 144 // if the bar was served from the cache 145 if ($servedFromCache && $cache->useCache($dependencies)) { 146 147 // Retrieve snippets from previous run 148 $data = $cache->retrieveCache(); 149 150 if (!empty($data)) { 151 $snippets = unserialize($data); 152 $snippetManager->addSnippetsFromCacheForBar($barId, $snippets); 153 154 if (Site::debugIsOn()) { 155 LogUtility::log2file("Snippet cache file {$cache->cache} used", LogUtility::LVL_MSG_DEBUG); 156 $event->data['script'][] = array( 157 "type" => "application/json", 158 "_data" => json_encode($snippets), 159 "class" => "combo-snippet-cache-" . str_replace(":", "-", $barId)); 160 } 161 162 } 163 } else { 164 $snippets = $snippetManager->getSnippetsForBar($barId); 165 if (!empty($snippets)) { 166 $cache->storeCache(serialize($snippets)); 167 } 168 } 169 170 } 171 172 /** 173 * Snippets 174 */ 175 foreach ($snippetManager->getSnippets() as $tagType => $tags) { 176 177 foreach ($tags as $tag) { 178 $event->data[$tagType][] = $tag; 179 } 180 181 } 182 183 184 $snippetManager->close(); 185 186 } 187 188 /** 189 * Used if the template does not run the content 190 * before the calling of the header as strap does. 191 * 192 * In this case, the {@link \ComboStrap\SnippetManager::close()} has 193 * not run, and the snippets are still in memory. 194 * 195 * We store them in the HTML and they 196 * follows then the HTML cache of DokuWiki 197 * @param $event 198 */ 199 function componentSnippetContent($event) 200 { 201 202 $format = $event->data[0]; 203 if($format!=="xhtml"){ 204 return; 205 } 206 207 /** 208 * Run only if the header output was already called 209 */ 210 if ($this->headerOutputWasCalled) { 211 212 $snippetManager = PluginUtility::getSnippetManager(); 213 214 $xhtmlContent = &$event->data[1]; 215 $snippets = $snippetManager->getSnippets(); 216 foreach ($snippets as $tagType => $tags) { 217 218 foreach ($tags as $tag) { 219 $xhtmlContent .= DOKU_LF . "<$tagType"; 220 $attributes = ""; 221 $content = null; 222 foreach ($tag as $attributeName => $attributeValue) { 223 if ($attributeName != "_data") { 224 $attributes .= " $attributeName=\"$attributeValue\""; 225 } else { 226 $content = $attributeValue; 227 } 228 } 229 $xhtmlContent .= "$attributes>"; 230 if (!empty($content)) { 231 $xhtmlContent .= $content; 232 } 233 $xhtmlContent .= "</$tagType>" . DOKU_LF; 234 } 235 236 } 237 238 $snippetManager->close(); 239 240 } 241 242 } 243 244 245 /** 246 * 247 * @param $event 248 */ 249 function barParsed($event) 250 { 251 $data = $event->data; 252 if ($data->mode == "xhtml") { 253 254 /* @var CacheRenderer $data */ 255 $pageId = $data->page; 256 $cached = $event->result; 257 PluginUtility::getSnippetManager()->addBar($pageId, $cached); 258 259 } 260 261 262 } 263 264 265} 266