xref: /plugin/footerv2/action.php (revision 5d12497364e4dcb8dc14a321b43cc3f44e742f7f)
1<?php
2
3/**
4
5 * DokuWiki Plugin footer (Action Component)
6
7 *
8
9 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
10
11 *
12
13 * Original: from Plugin footer, author Juergen <H-J-Schuemmer@Web.de>
14
15 * Modified by Scott Lee Chua scottleechua@icloud.com
16
17 *
18
19 * Original: from Plugin headerfooter, author Li Zheng <lzpublic@qq.com>
20
21 * Modified by Juergen H-J-Schuemmer@Web.de
22
23 * Only the footer component is supported in this plugin because the header functionality breaks the section edit mode
24
25 */
26
27
28// must be run within Dokuwiki
29
30if(!defined('DOKU_INC')) die();
31
32
33class action_plugin_footerv2 extends DokuWiki_Action_Plugin {
34
35   public function register(Doku_Event_Handler $controller) {
36
37
38      $controller->register_hook('PARSER_WIKITEXT_PREPROCESS', 'AFTER', $this, 'handle_parser_wikitext_preprocess');
39
40     // aus Seite "https://github.com/MrBertie/pagequery/commit/6cae014dc7cc779c0be8d0a660af42407b414806":
41
42     $controller->register_hook('PARSER_CACHE_USE', 'BEFORE', $this, '_purgecache');
43
44   }
45
46   public function handle_parser_wikitext_preprocess(Doku_Event &$event, $param) {
47
48       global $INFO;
49
50       global $ID;
51
52       global $conf;
53
54       // Check if $INFO is set and has an 'id' key before accessing it
55       if (isset($INFO) && isset($INFO['id']) && $INFO['id'] != '') return;
56
57
58       //helper array needed for parsePageTemplate
59
60       //so that replacement like shown here is possible: https://www.dokuwiki.org/namespace_templates#replacement_patterns
61
62       $data = array(
63
64           'id'       => $ID, // the id of the page to be created
65
66           'tpl'      => '', // the text used as template
67
68       );
69
70
71      // Auslesen der Konfiguration für das Präfix der Vorlage-Dateien:
72
73      $pre_nsp = $this->getConf('prefix_namespace');
74
75      if ($pre_nsp != '') {
76
77         $pre_nsp = '/'.$pre_nsp.'_';
78
79      } else {
80
81         $pre_nsp = '/_';   // Defaultwert 1 Unterstrich für Namespace
82
83      };
84
85      $pre_sub = $this->getConf('prefix_subnamespace');
86
87      if ($pre_sub != '') {
88
89         $pre_sub = '/'.$pre_sub.'_';
90
91      } else {
92
93         $pre_sub = '/__';  // Defaultwert 2 Unterstriche für Sub-Namespace
94
95      };
96
97
98       $footerpath = '';
99
100      $templatename = 'footer.txt';   // Name der Vorlage
101
102       $path = dirname(wikiFN($ID));
103
104       if (@file_exists($path.$pre_nsp.$templatename)) {
105
106           $footerpath = $path.$pre_nsp.$templatename;
107
108       } else {
109
110           // search upper namespaces for templates
111
112           $len = strlen(rtrim($conf['datadir'], '/'));
113
114           while (strlen($path) >= $len) {
115
116               if (@file_exists($path.$pre_sub.$templatename)) {
117
118                   $footerpath = $path.$pre_sub.$templatename;
119
120                   break;
121
122               }
123
124               $path = substr($path, 0, strrpos($path, '/'));
125
126           }
127
128       }
129
130
131       if (!empty($footerpath)) {
132
133         $content = $event->data;
134
135         if(strpos($content,"~~NOFOOTER~~") == false) {
136
137            // Prüfung. ob der Befehl "~~NOFOOTER~~" im Quelltext enthalten ist
138
139            $footer = file_get_contents($footerpath);
140
141            if ($footer !== false) {
142
143               $data['tpl'] = cleanText($footer);
144
145               $footer = parsePageTemplate($data);
146
147               if ($this->getConf('separation') == 'paragraph') {
148
149                  // Wenn Absätze zum Teilen verwendet werden
150
151                  $footer = rtrim($footer, " \r\n\\") . "\n\n";
152
153               }
154
155               $event->data .= $footer;
156
157            }
158
159            /*
160
161            // Code übernommen von Seite "https://www.dokuwiki.org/devel:event_handlers_code#caching":
162
163            $event->preventDefault();  // stop dokuwiki carrying out its own checks
164
165            $event->stopPropagation(); // avoid other handlers of this event, changing our decision here
166
167            $event->result = false;    // don't use the cached version
168
169            */
170
171         } else {
172
173            $event->data = str_replace('~~NOFOOTER~~','',$content);
174
175            // Befehl "~~NOFOOTER~~" soll nicht angezeigt werden
176
177         }
178
179      }
180
181   }
182
183
184   // Codeschnipsel aus Seite "https://github.com/MrBertie/pagequery/commit/6cae014dc7cc779c0be8d0a660af42407b414806":
185
186   /**
187
188    * Check for pages changes and eventually purge cache.
189
190    *
191
192    * @author Samuele Tognini <samuele@samuele.netsons.org>
193
194    *
195
196    * @param Doku_Event $event
197
198    * @param mixed     $param not defined
199
200    */
201
202   function _purgecache(&$event, $param) {
203
204       global $ID;
205
206       global $conf;
207
208       /** @var cache_parser $cache */
209
210       $cache = &$event->data;
211
212
213       if(!isset($cache->page)) return;
214
215       //purge only xhtml cache
216
217       if($cache->mode != "xhtml") return;
218
219       //Check if it is an pagequery page
220
221       if(!p_get_metadata($ID, 'pagequery')) return;
222
223       $aclcache = $this->getConf('aclcache');
224
225       if($conf['useacl']) {
226
227           $newkey = false;
228
229           if($aclcache == 'user') {
230
231               //Cache per user
232
233               if($_SERVER['REMOTE_USER']) $newkey = $_SERVER['REMOTE_USER'];
234
235           } else if($aclcache == 'groups') {
236
237               //Cache per groups
238
239               global $INFO;
240
241               if($INFO['userinfo']['grps']) $newkey = implode('#', $INFO['userinfo']['grps']);
242
243           }
244
245           if($newkey) {
246
247               $cache->key .= "#".$newkey;
248
249               $cache->cache = getCacheName($cache->key, $cache->ext);
250
251           }
252
253       }
254
255       //Check if a page is more recent than purgefile.
256
257       if(@filemtime($cache->cache) < @filemtime($conf['cachedir'].'/purgefile')) {
258
259           $event->preventDefault();
260
261           $event->stopPropagation();
262
263           $event->result = false;
264
265       }
266   }
267}