xref: /template/ad-hominem/my_template.php (revision eb708266bcbc9ec5d164b4d4577a54b34fa02bd9)
1<?php
2/**
3 * Overwriting DokuWiki template functions
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Sascha Leib <sascha@leib.be>
7 * @author     Andreas Gohr <andi@splitbrain.org>
8 */
9
10use dokuwiki\Extension\Event;
11
12/**
13 * Print the specific HTML meta headers
14 *
15 * Overrides the original version by modifying the headers and the way it is printed
16 *
17 * @author Sascha Leib <sascha@leib.be>
18 * @author Andreas Gohr <andi@splitbrain.org>
19 *
20 * @triggers TPL_METAHEADER_OUTPUT
21 * @param  bool $alt Should feeds and alternative format links be added?
22 * @return bool
23 */
24function my_metaheaders($alt = true) {
25    global $ID;
26    global $REV;
27    global $INFO;
28    global $JSINFO;
29    global $ACT;
30    global $QUERY;
31    global $lang;
32    global $conf;
33    global $updateVersion;
34    /** @var Input $INPUT */
35    global $INPUT;
36
37    // prepare the head array
38    $head = array();
39
40    // prepare seed for js and css
41    $tseed   = $updateVersion;
42    $depends = getConfigFiles('main');
43    $depends[] = DOKU_CONF."tpl/".$conf['template']."/style.ini";
44    foreach($depends as $f) $tseed .= @filemtime($f);
45    $tseed   = md5($tseed);
46
47    // the usual stuff
48    $head['meta'][] = array('name'=> 'generator', 'content'=> 'DokuWiki');
49    if(actionOK('search')) {
50        $head['link'][] = array(
51            'rel' => 'search', 'type'=> 'application/opensearchdescription+xml',
52            'href'=> DOKU_BASE.'lib/exe/opensearch.php', 'title'=> $conf['title']
53        );
54    }
55
56    $head['link'][] = array('rel'=> 'start', 'href'=> DOKU_BASE);
57    if(actionOK('index')) {
58        $head['link'][] = array(
59            'rel'  => 'contents', 'href'=> wl($ID, 'do=index', false, '&'),
60            'title'=> $lang['btn_index']
61        );
62    }
63
64    if (actionOK('manifest')) {
65        $head['link'][] = array('rel'=> 'manifest', 'href'=> DOKU_BASE.'lib/exe/manifest.php');
66    }
67
68    $styleUtil = new \dokuwiki\StyleUtils();
69    $styleIni = $styleUtil->cssStyleini();
70    $replacements = $styleIni['replacements'];
71    if (!empty($replacements['__theme_color__'])) {
72        $head['meta'][] = array('name' => 'theme-color', 'content' => $replacements['__theme_color__']);
73    }
74
75    if($alt) {
76        if(actionOK('rss')) {
77            $head['link'][] = array(
78                'rel'  => 'alternate', 'type'=> 'application/rss+xml',
79                'title'=> $lang['btn_recent'], 'href'=> DOKU_BASE.'feed.php'
80            );
81            $head['link'][] = array(
82                'rel'  => 'alternate', 'type'=> 'application/rss+xml',
83                'title'=> $lang['currentns'],
84                'href' => DOKU_BASE.'feed.php?mode=list&ns='.(isset($INFO) ? $INFO['namespace'] : '')
85            );
86        }
87        if(($ACT == 'show' || $ACT == 'search') && $INFO['writable']) {
88            $head['link'][] = array(
89                'rel'  => 'edit',
90                'title'=> $lang['btn_edit'],
91                'href' => wl($ID, 'do=edit', false, '&')
92            );
93        }
94
95        if(actionOK('rss') && $ACT == 'search') {
96            $head['link'][] = array(
97                'rel'  => 'alternate', 'type'=> 'application/rss+xml',
98                'title'=> $lang['searchresult'],
99                'href' => DOKU_BASE.'feed.php?mode=search&q='.$QUERY
100            );
101        }
102
103        if(actionOK('export_xhtml')) {
104            $head['link'][] = array(
105                'rel' => 'alternate', 'type'=> 'text/html', 'title'=> $lang['plainhtml'],
106                'href'=> exportlink($ID, 'xhtml', '', false, '&')
107            );
108        }
109
110        if(actionOK('export_raw')) {
111            $head['link'][] = array(
112                'rel' => 'alternate', 'type'=> 'text/plain', 'title'=> $lang['wikimarkup'],
113                'href'=> exportlink($ID, 'raw', '', false, '&')
114            );
115        }
116    }
117
118    // setup robot tags apropriate for different modes
119    if(($ACT == 'show' || $ACT == 'export_xhtml') && !$REV) {
120        if($INFO['exists']) {
121            //delay indexing:
122            if((time() - $INFO['lastmod']) >= $conf['indexdelay'] && !isHiddenPage($ID) ) {
123                $head['meta'][] = array('name'=> 'robots', 'content'=> 'index,follow');
124            } else {
125                $head['meta'][] = array('name'=> 'robots', 'content'=> 'noindex,nofollow');
126            }
127            $canonicalUrl = wl($ID, '', true, '&');
128            if ($ID == $conf['start']) {
129                $canonicalUrl = DOKU_URL;
130            }
131            $head['link'][] = array('rel'=> 'canonical', 'href'=> $canonicalUrl);
132        } else {
133            $head['meta'][] = array('name'=> 'robots', 'content'=> 'noindex,follow');
134        }
135    } elseif(defined('DOKU_MEDIADETAIL')) {
136        $head['meta'][] = array('name'=> 'robots', 'content'=> 'index,follow');
137    } else {
138        $head['meta'][] = array('name'=> 'robots', 'content'=> 'noindex,nofollow');
139    }
140
141    // set metadata
142    if($ACT == 'show' || $ACT == 'export_xhtml') {
143        // keywords (explicit or implicit)
144        if(!empty($INFO['meta']['subject'])) {
145            $head['meta'][] = array('name'=> 'keywords', 'content'=> join(',', $INFO['meta']['subject']));
146        } else {
147            $head['meta'][] = array('name'=> 'keywords', 'content'=> str_replace(':', ',', $ID));
148        }
149    }
150
151    // load stylesheets
152    $head['link'][] = array(
153        'rel' => 'stylesheet',
154        'href'=> DOKU_BASE.'lib/exe/css.php?t='.rawurlencode($conf['template']).'&tseed='.$tseed
155    );
156
157    $script = "var NS='".(isset($INFO)?$INFO['namespace']:'')."';";
158    if($conf['useacl'] && $INPUT->server->str('REMOTE_USER')) {
159        $script .= "var SIG=".toolbar_signature().";";
160    }
161    jsinfo();
162    $script .= 'var JSINFO = ' . json_encode($JSINFO).';';
163    $head['script'][] = array('_data'=> $script);
164
165    // load jquery
166    $jquery = getCdnUrls();
167    foreach($jquery as $src) {
168        $head['script'][] = array(
169            'charset' => 'utf-8',
170            '_data' => '',
171            'src' => $src,
172        ) + ($conf['defer_js'] ? [ 'defer' => 'defer'] : []);
173    }
174
175    // load our javascript dispatcher
176    $head['script'][] = array(
177        'charset'=> 'utf-8', '_data'=> '',
178        'src' => DOKU_BASE.'lib/exe/js.php'.'?t='.rawurlencode($conf['template']).'&tseed='.$tseed,
179    ) + ($conf['defer_js'] ? [ 'defer' => 'defer'] : []);
180
181    // trigger event here
182    Event::createAndTrigger('TPL_METAHEADER_OUTPUT', $head, '_my_metaheaders_action', true);
183    return true;
184}
185
186
187/**
188 * prints the array build by my_metaheaders
189 *
190 * Overrides the original version by adding a tab before each line for neater HTML code
191 *
192 * @author Sascha Leib <sascha@leib.be>
193 * @author Andreas Gohr <andi@splitbrain.org>
194 *
195 * @param array $data
196 */
197function _my_metaheaders_action($data) {
198    foreach($data as $tag => $inst) {
199        /* if($tag == 'script') {
200            echo "\t<!--[if gte IE 9]><!-->\n"; // no scripts for old IE
201        } NO LONGER NEEDED */
202
203        foreach($inst as $attr) {
204            if ( empty($attr) ) { continue; }
205            echo "\t<", $tag, ' ', buildAttributes($attr);
206            if(isset($attr['_data']) || $tag == 'script') {
207                if($tag == 'script' && $attr['_data'])
208                    $attr['_data'] = "/*<![CDATA[*/".
209                        $attr['_data'].
210                        "\n/*!]]>*/";
211
212                echo '>', $attr['_data'], '</', $tag, '>';
213            } else {
214                echo '/>';
215            }
216            echo "\n";
217        }
218        /* if($tag == 'script') {
219            echo "\t<!--<![endif]-->\n";
220        } -- Not Needed Any Longer */
221    }
222}
223
224/**
225 * Print the breadcrumbs trace
226 *
227 * Cleanup of the original code to create neater and more accessible HTML
228 *
229 * @author Sascha Leib <sascha@leib.be>
230 * @author Andreas Gohr <andi@splitbrain.org>
231 *
232 * @param string $prefix inserted before each line
233 *
234 * @return void
235 */
236function my_breadcrumbs($prefix = '') {
237    global $lang;
238    global $conf;
239
240    //check if enabled
241    if(!$conf['breadcrumbs']) return false;
242
243    $crumbs = breadcrumbs(); //setup crumb trace
244
245	/* begin listing */
246	echo $prefix . "<nav id=\"navBreadCrumbs\">\n";
247	echo $prefix . "\t<h4 class=\"toggle\">" . $lang['breadcrumb'] . "</h4>\n";
248	echo $prefix . "\t<ol reversed>\n";
249
250
251    $last = count($crumbs);
252    $i    = 0;
253    foreach($crumbs as $id => $name) {
254        $i++;
255		echo $prefix . "\t\t<li" . ($i == $last ? ' class="current"' : '') . '><bdi>' . tpl_link(wl($id), hsc($name), '', true) .  "</bdi></li>\n";
256    }
257	echo $prefix . "\t</ol>\n";
258	echo $prefix . "</nav>\n";
259}
260
261
262/**
263 * Hierarchical breadcrumbs
264 *
265 * Cleanup of the original code to create neater and more accessible HTML
266 *
267 * @author Sascha Leib <sascha@leib.be>
268 * @author Andreas Gohr <andi@splitbrain.org>
269 * @author Nigel McNie <oracle.shinoda@gmail.com>
270 * @author Sean Coates <sean@caedmon.net>
271 * @author <fredrik@averpil.com>
272 *
273 * @param  string $prefix to be added before each line
274 *
275 */
276function my_youarehere($prefix = '') {
277    global $conf;
278    global $ID;
279    global $lang;
280
281    // check if enabled
282    if(!$conf['youarehere']) return false;
283
284    $parts = explode(':', $ID);
285    $count = count($parts);
286
287	echo $prefix . "<nav id=\"navYouAreHere\">\n";
288	echo $prefix . "\t<h4>" . $lang['youarehere'] . "</h4>\n";
289	echo $prefix . "\t<ol>\n";
290
291    // always print the startpage
292    echo $prefix . "\t\t<li class=\"home\">" . tpl_pagelink(':'.$conf['start'], null, true) . "</li>\n";
293
294    // print intermediate namespace links
295    $part = '';
296    for($i = 0; $i < $count - 1; $i++) {
297        $part .= $parts[$i].':';
298        $page = $part;
299        if($page == $conf['start']) continue; // Skip startpage
300
301        // output
302        echo $prefix . "\t\t<li>" . tpl_pagelink($page, null, true) . "</li>\n";
303    }
304
305    // print current page, skipping start page, skipping for namespace index
306    /* resolve_pageid('', $page, $exists);
307    if ( !(isset($page) && $page == $part.$parts[$i])
308		|| !($page == $conf['start']) ) {
309		echo "\t\t\t\t\t<li>" . tpl_pagelink($page, null, true) . "</li>\n";
310	} */
311
312	echo $prefix . "\t</ol>\n";
313	echo $prefix . "</nav>\n";
314}
315
316/**
317 * My implementation of the basic userinfo (in the global banner)
318 *
319 *
320 * @author Sascha Leib <sascha@leib.be>
321 *
322 * @param  string $prefix to be added before each line
323 *
324 * @return void
325 */
326function my_userinfo($prefix = '') {
327    global $lang;
328    global $INPUT;
329
330	// add login/logout button:
331	$items = array_reverse((new \dokuwiki\Menu\UserMenu())->getItems());
332	foreach($items as $it) {
333		$typ = $it->getType();
334		if ($typ === 'profile') {
335			echo $prefix . "<li class=\"action $typ\"><span class=\"sronly\">" . $lang['loggedinas'] . ' </span><a href="' . htmlentities($it->getLink()) . '" title="' . $it->getTitle() . '">' . userlink() . "</a></li>\n";
336		} else {
337			echo $prefix . "<li class=\"action $typ\"><a href=\"" . htmlentities($it->getLink()) . '" title="' . $it->getTitle() . '">' . $it->getLabel() . "</a></li>\n";
338		}
339	}
340
341}
342
343/**
344 *Inserts a cleaner version of the TOC
345 *
346 * This is an update of the original function that renders the TOC directly.
347 *
348 * @author Sascha Leib <sascha@leib.be>
349 * @author Andreas Gohr <andi@splitbrain.org>
350 *
351 * @param  string $prefix to be added before each line
352 *
353 * @return void
354 */
355function my_toc($prefix = '') {
356    global $TOC;
357    global $ACT;
358    global $ID;
359    global $REV;
360    global $INFO;
361    global $conf;
362    global $lang;
363    $toc = array();
364
365    if(is_array($TOC)) {
366        // if a TOC was prepared in global scope, always use it
367        $toc = $TOC;
368    } elseif(($ACT == 'show' || substr($ACT, 0, 6) == 'export') && !$REV && $INFO['exists']) {
369        // get TOC from metadata, render if neccessary
370        $meta = p_get_metadata($ID, '', METADATA_RENDER_USING_CACHE);
371        if(isset($meta['internal']['toc'])) {
372            $tocok = $meta['internal']['toc'];
373        } else {
374            $tocok = true;
375        }
376        $toc = isset($meta['description']['tableofcontents']) ? $meta['description']['tableofcontents'] : null;
377        if(!$tocok || !is_array($toc) || !$conf['tocminheads'] || count($toc) < $conf['tocminheads']) {
378            $toc = array();
379        }
380    } elseif($ACT == 'admin') {
381        // try to load admin plugin TOC
382        /** @var $plugin AdminPlugin */
383        if ($plugin = plugin_getRequestAdminPlugin()) {
384            $toc = $plugin->getTOC();
385            $TOC = $toc; // avoid later rebuild
386        }
387    }
388
389	/* Build the hierarchical list of headline links: */
390	if (count($toc) >= intval($conf['tocminheads'])) {
391		echo $prefix . "<aside id=\"dw__toc\" class=\"dw__toc\">\n";
392		echo $prefix . "\t<h3>" . $lang['toc'] . "</h3>\n" . $prefix . "\t<div>";
393		$level = intval("0");
394		foreach($toc as $it) {
395
396			$nl = intval($it['level']);
397			$cp = ($nl <=> $level);
398
399			if ($cp > 0) {
400				echo "\n" . $prefix . str_repeat("\t", $level*2 + 2) . "<ol>\n";
401			} else if ($cp < 0) {
402				echo "\n" . $prefix . str_repeat("\t", $level*2) . "</ol></li>\n";
403			} else {
404				echo "</li>\n";
405			}
406
407			echo $prefix . str_repeat("\t", $nl*2 + 1) . "<li><a href=\"#" . $it['hid'] . '">' . htmlentities($it['title']) . "</a>";
408			$level = $nl;
409		}
410
411		for ($i = $level-1; $i > 0; $i--) {
412			echo "</li>\n" . $prefix . str_repeat("\t", $i*2 + 1) . "</ol>";
413		}
414
415		echo "</li>\n" . $prefix . "\t\t</ol>\n" . $prefix . "\t</div>\n" . $prefix . "</aside>\n";
416	}
417}
418
419/**
420 * Print last change date
421 *
422 * @author Sascha Leib <sascha@leib.be>
423 *
424 * @param  string $prefix to be added before each line
425 *
426 * @return void
427 */
428function my_lastchange($prefix = '') {
429
430    global $lang;
431    global $INFO;
432
433	$format = '%Y-%m-%dT%T%z';	/* 2021-21-05T16:45:12+02:00 */
434
435	$date = $INFO['lastmod'];
436
437	echo $prefix . '<bdi>' . $lang['lastmod'] . "</bdi>\n";
438	echo $prefix . '<time itemprop="dateModified" datetime="' . strftime($format, $date) . '">' . dformat($date) . "</time>\n";
439	echo $prefix .'<span class="editorname" tabindex="0">' . $lang['by'] . ' <bdi itemprop="editor">' . editorinfo($INFO['editor']) . "</bdi></span>\n";
440}