4 * DokuWiki template functions
6 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author Andreas Gohr <andi@splitbrain.org>
10 use dokuwiki\ActionRouter
;
11 use dokuwiki\Action\Exception\FatalException
;
12 use dokuwiki\Extension\PluginInterface
;
13 use dokuwiki\Ui\Admin
;
14 use dokuwiki\StyleUtils
;
15 use dokuwiki\Menu\Item\AbstractItem
;
16 use dokuwiki\Form\Form
;
17 use dokuwiki\Menu\MobileMenu
;
18 use dokuwiki\Ui\Subscribe
;
19 use dokuwiki\Extension\AdminPlugin
;
20 use dokuwiki\Extension\Event
;
21 use dokuwiki\File\PageResolver
;
24 * Access a template file
26 * Returns the path to the given file inside the current template, uses
27 * default template if the custom version doesn't exist.
32 * @author Andreas Gohr <andi@splitbrain.org>
34 function template($file)
38 if (@is_readable
(DOKU_INC
. 'lib/tpl/' . $conf['template'] . '/' . $file))
39 return DOKU_INC
. 'lib/tpl/' . $conf['template'] . '/' . $file;
41 return DOKU_INC
. 'lib/tpl/dokuwiki/' . $file;
45 * Convenience function to access template dir from local FS
47 * This replaces the deprecated DOKU_TPLINC constant
49 * @param string $tpl The template to use, default to current one
52 * @author Andreas Gohr <andi@splitbrain.org>
54 function tpl_incdir($tpl = '')
57 if (!$tpl) $tpl = $conf['template'];
58 return DOKU_INC
. 'lib/tpl/' . $tpl . '/';
62 * Convenience function to access template dir from web
64 * This replaces the deprecated DOKU_TPL constant
66 * @param string $tpl The template to use, default to current one
69 * @author Andreas Gohr <andi@splitbrain.org>
71 function tpl_basedir($tpl = '')
74 if (!$tpl) $tpl = $conf['template'];
75 return DOKU_BASE
. 'lib/tpl/' . $tpl . '/';
81 * This function is used for printing all the usual content
82 * (defined by the global $ACT var) by calling the appropriate
83 * outputfunction(s) from html.php
85 * Everything that doesn't use the main template file isn't
86 * handled by this function. ACL stuff is not done here either.
88 * @param bool $prependTOC should the TOC be displayed here?
89 * @return bool true if any output
91 * @triggers TPL_ACT_RENDER
92 * @triggers TPL_CONTENT_DISPLAY
93 * @author Andreas Gohr <andi@splitbrain.org>
95 function tpl_content($prependTOC = true)
99 $INFO['prependTOC'] = $prependTOC;
102 Event
::createAndTrigger('TPL_ACT_RENDER', $ACT, 'tpl_content_core');
103 $html_output = ob_get_clean();
104 Event
::createAndTrigger('TPL_CONTENT_DISPLAY', $html_output, function ($html_output) {
108 return !empty($html_output);
112 * Default Action of TPL_ACT_RENDER
116 function tpl_content_core()
118 $router = ActionRouter
::getInstance();
120 $router->getAction()->tplContent();
121 } catch (FatalException
$e) {
122 // there was no content for the action
123 msg(hsc($e->getMessage()), -1);
130 * Places the TOC where the function is called
132 * If you use this you most probably want to call tpl_content with
135 * @param bool $return Should the TOC be returned instead to be printed?
138 * @author Andreas Gohr <andi@splitbrain.org>
140 function tpl_toc($return = false)
150 if (is_array($TOC)) {
151 // if a TOC was prepared in global scope, always use it
153 } elseif (($ACT == 'show' ||
str_starts_with($ACT, 'export')) && !$REV && $INFO['exists']) {
154 // get TOC from metadata, render if neccessary
155 $meta = p_get_metadata($ID, '', METADATA_RENDER_USING_CACHE
);
156 $tocok = $meta['internal']['toc'] ??
true;
157 $toc = $meta['description']['tableofcontents'] ??
null;
158 if (!$tocok ||
!is_array($toc) ||
!$conf['tocminheads'] ||
count($toc) < $conf['tocminheads']) {
161 } elseif ($ACT == 'admin') {
162 // try to load admin plugin TOC
163 /** @var AdminPlugin $plugin */
164 if ($plugin = plugin_getRequestAdminPlugin()) {
165 $toc = $plugin->getTOC();
166 $TOC = $toc; // avoid later rebuild
170 Event
::createAndTrigger('TPL_TOC_RENDER', $toc, null, false);
171 $html = html_TOC($toc);
172 if ($return) return $html;
178 * Handle the admin page contents
182 * @author Andreas Gohr <andi@splitbrain.org>
191 $class = $INPUT->str('page');
192 if (!empty($class)) {
193 $pluginlist = plugin_list('admin');
195 if (in_array($class, $pluginlist)) {
196 // attempt to load the plugin
197 /** @var AdminPlugin $plugin */
198 $plugin = plugin_load('admin', $class);
202 if ($plugin instanceof PluginInterface
) {
203 if (!is_array($TOC)) $TOC = $plugin->getTOC(); //if TOC wasn't requested yet
204 if ($INFO['prependTOC']) tpl_toc();
207 $admin = new Admin();
214 * Print the correct HTML meta headers
216 * This has to go into the head section of your template.
218 * @param bool $alt Should feeds and alternative format links be added?
220 * @throws JsonException
222 * @author Andreas Gohr <andi@splitbrain.org>
223 * @triggers TPL_METAHEADER_OUTPUT
225 function tpl_metaheaders($alt = true)
235 global $updateVersion;
236 /** @var Input $INPUT */
239 // prepare the head array
242 // prepare seed for js and css
243 $tseed = $updateVersion;
244 $depends = getConfigFiles('main');
245 $depends[] = DOKU_CONF
. "tpl/" . $conf['template'] . "/style.ini";
246 foreach ($depends as $f) $tseed .= @filemtime
($f);
247 $tseed = md5($tseed);
250 $head['meta'][] = ['name' => 'generator', 'content' => 'DokuWiki'];
251 if (actionOK('search')) {
254 'type' => 'application/opensearchdescription+xml',
255 'href' => DOKU_BASE
. 'lib/exe/opensearch.php',
256 'title' => $conf['title']
260 $head['link'][] = ['rel' => 'start', 'href' => DOKU_BASE
];
261 if (actionOK('index')) {
264 'href' => wl($ID, 'do=index', false, '&'),
265 'title' => $lang['btn_index']
269 if (actionOK('manifest')) {
272 'href' => DOKU_BASE
. 'lib/exe/manifest.php'
276 $styleUtil = new StyleUtils();
277 $styleIni = $styleUtil->cssStyleini();
278 $replacements = $styleIni['replacements'];
279 if (!empty($replacements['__theme_color__'])) {
281 'name' => 'theme-color',
282 'content' => $replacements['__theme_color__']
287 if (actionOK('rss')) {
289 'rel' => 'alternate',
290 'type' => 'application/rss+xml',
291 'title' => $lang['btn_recent'],
292 'href' => DOKU_BASE
. 'feed.php'
295 'rel' => 'alternate',
296 'type' => 'application/rss+xml',
297 'title' => $lang['currentns'],
298 'href' => DOKU_BASE
. 'feed.php?mode=list&ns=' . (isset($INFO) ?
$INFO['namespace'] : '')
301 if (($ACT == 'show' ||
$ACT == 'search') && $INFO['writable']) {
304 'title' => $lang['btn_edit'],
305 'href' => wl($ID, 'do=edit', false, '&')
309 if (actionOK('rss') && $ACT == 'search') {
311 'rel' => 'alternate',
312 'type' => 'application/rss+xml',
313 'title' => $lang['searchresult'],
314 'href' => DOKU_BASE
. 'feed.php?mode=search&q=' . $QUERY
318 if (actionOK('export_xhtml')) {
320 'rel' => 'alternate',
321 'type' => 'text/html',
322 'title' => $lang['plainhtml'],
323 'href' => exportlink($ID, 'xhtml', '', false, '&')
327 if (actionOK('export_raw')) {
329 'rel' => 'alternate',
330 'type' => 'text/plain',
331 'title' => $lang['wikimarkup'],
332 'href' => exportlink($ID, 'raw', '', false, '&')
337 // setup robot tags appropriate for different modes
338 if (($ACT == 'show' ||
$ACT == 'export_xhtml') && !$REV) {
339 if ($INFO['exists']) {
341 if ((time() - $INFO['lastmod']) >= $conf['indexdelay'] && !isHiddenPage($ID)) {
342 $head['meta'][] = ['name' => 'robots', 'content' => 'index,follow'];
344 $head['meta'][] = ['name' => 'robots', 'content' => 'noindex,nofollow'];
346 $canonicalUrl = wl($ID, '', true, '&');
347 if ($ID == $conf['start']) {
348 $canonicalUrl = DOKU_URL
;
350 $head['link'][] = ['rel' => 'canonical', 'href' => $canonicalUrl];
352 $head['meta'][] = ['name' => 'robots', 'content' => 'noindex,follow'];
354 } elseif (defined('DOKU_MEDIADETAIL')) {
355 $head['meta'][] = ['name' => 'robots', 'content' => 'index,follow'];
357 $head['meta'][] = ['name' => 'robots', 'content' => 'noindex,nofollow'];
361 if ($ACT == 'show' ||
$ACT == 'export_xhtml') {
362 // keywords (explicit or implicit)
363 if (!empty($INFO['meta']['subject'])) {
364 $head['meta'][] = ['name' => 'keywords', 'content' => implode(',', $INFO['meta']['subject'])];
366 $head['meta'][] = ['name' => 'keywords', 'content' => str_replace(':', ',', $ID)];
372 'rel' => 'stylesheet',
373 'href' => DOKU_BASE
. 'lib/exe/css.php?t=' . rawurlencode($conf['template']) . '&tseed=' . $tseed
376 $script = "var NS='" . (isset($INFO) ?
$INFO['namespace'] : '') . "';";
377 if ($conf['useacl'] && $INPUT->server
->str('REMOTE_USER')) {
378 $script .= "var SIG=" . toolbar_signature() . ";";
381 $script .= 'var JSINFO = ' . json_encode($JSINFO, JSON_THROW_ON_ERROR
) . ';';
382 $script .= '(function(H){H.className=H.className.replace(/\bno-js\b/,\'js\')})(document.documentElement);';
383 $head['script'][] = ['_data' => $script];
386 $jquery = getCdnUrls();
387 foreach ($jquery as $src) {
388 $head['script'][] = [
391 ] +
($conf['defer_js'] ?
['defer' => 'defer'] : []);
394 // load our javascript dispatcher
395 $head['script'][] = [
397 'src' => DOKU_BASE
. 'lib/exe/js.php' . '?t=' . rawurlencode($conf['template']) . '&tseed=' . $tseed
398 ] +
($conf['defer_js'] ?
['defer' => 'defer'] : []);
400 // trigger event here
401 Event
::createAndTrigger('TPL_METAHEADER_OUTPUT', $head, '_tpl_metaheaders_action', true);
406 * prints the array build by tpl_metaheaders
408 * $data is an array of different header tags. Each tag can have multiple
409 * instances. Attributes are given as key value pairs. Values will be HTML
410 * encoded automatically so they should be provided as is in the $data array.
412 * For tags having a body attribute specify the body data in the special
413 * attribute '_data'. This field will NOT BE ESCAPED automatically.
415 * Inline scripts will use any nonce provided in the environment variable 'NONCE'.
419 * @author Andreas Gohr <andi@splitbrain.org>
421 function _tpl_metaheaders_action($data)
423 $nonce = getenv('NONCE');
424 foreach ($data as $tag => $inst) {
425 foreach ($inst as $attr) {
429 if ($nonce && $tag == 'script' && !empty($attr['_data'])) {
430 $attr['nonce'] = $nonce; // add nonce to inline script tags
432 echo '<', $tag, ' ', buildAttributes($attr);
433 if (isset($attr['_data']) ||
$tag == 'script') {
434 echo '>', $attr['_data'] ??
'', '</', $tag, '>';
444 * Output the given script as inline script tag
446 * This function will add the nonce attribute if a nonce is available.
448 * The script is NOT automatically escaped!
450 * @param string $script
451 * @param bool $return Return or print directly?
452 * @return string|void
454 function tpl_inlineScript($script, $return = false)
456 $nonce = getenv('NONCE');
458 $script = '<script nonce="' . $nonce . '">' . $script . '</script>';
460 $script = '<script>' . $script . '</script>';
463 if ($return) return $script;
470 * Just builds a link.
473 * @param string $name
474 * @param string $more
475 * @param bool $return if true return the link html, otherwise print
476 * @return bool|string html of the link, or true if printed
478 * @author Andreas Gohr <andi@splitbrain.org>
480 function tpl_link($url, $name, $more = '', $return = false)
482 $out = '<a href="' . $url . '" ';
483 if ($more) $out .= ' ' . $more;
484 $out .= ">$name</a>";
485 if ($return) return $out;
491 * Prints a link to a WikiPage
493 * Wrapper around html_wikilink
495 * @param string $id page id
496 * @param string|null $name the name of the link
497 * @param bool $return
498 * @return true|string
500 * @author Andreas Gohr <andi@splitbrain.org>
502 function tpl_pagelink($id, $name = null, $return = false)
504 $out = '<bdi>' . html_wikilink($id, $name) . '</bdi>';
505 if ($return) return $out;
511 * get the parent page
513 * Tries to find out which page is parent.
514 * returns false if none is available
516 * @param string $id page id
517 * @return false|string
519 * @author Andreas Gohr <andi@splitbrain.org>
521 function tpl_getparent($id)
523 $resolver = new PageResolver('root');
525 $parent = getNS($id) . ':';
526 $parent = $resolver->resolveId($parent);
527 if ($parent == $id) {
528 $pos = strrpos(getNS($id), ':');
529 $parent = substr($parent, 0, $pos) . ':';
530 $parent = $resolver->resolveId($parent);
531 if ($parent == $id) return false;
537 * Print one of the buttons
539 * @param string $type
540 * @param bool $return
541 * @return bool|string html, or false if no data, true if printed
542 * @see tpl_get_action
544 * @author Adrian Lang <mail@adrianlang.de>
545 * @deprecated 2017-09-01 see devel:menus
547 function tpl_button($type, $return = false)
549 dbg_deprecated('see devel:menus');
550 $data = tpl_get_action($type);
551 if ($data === false) {
553 } elseif (!is_array($data)) {
554 $out = sprintf($data, 'button');
557 * @var string $accesskey
559 * @var string $method
563 if ($id === '#dokuwiki__top') {
564 $out = html_topbtn();
566 $out = html_btn($type, $id, $accesskey, $params, $method);
569 if ($return) return $out;
575 * Like the action buttons but links
577 * @param string $type action command
578 * @param string $pre prefix of link
579 * @param string $suf suffix of link
580 * @param string $inner innerHML of link
581 * @param bool $return if true it returns html, otherwise prints
582 * @return bool|string html or false if no data, true if printed
584 * @see tpl_get_action
585 * @author Adrian Lang <mail@adrianlang.de>
586 * @deprecated 2017-09-01 see devel:menus
588 function tpl_actionlink($type, $pre = '', $suf = '', $inner = '', $return = false)
590 dbg_deprecated('see devel:menus');
592 $data = tpl_get_action($type);
593 if ($data === false) {
595 } elseif (!is_array($data)) {
596 $out = sprintf($data, 'link');
599 * @var string $accesskey
601 * @var string $method
602 * @var bool $nofollow
604 * @var string $replacement
607 if (strpos($id, '#') === 0) {
610 $linktarget = wl($id, $params);
612 $caption = $lang['btn_' . $type];
613 if (strpos($caption, '%s')) {
614 $caption = sprintf($caption, $replacement);
619 $akey = 'accesskey="' . $accesskey . '" ';
620 $addTitle = ' [' . strtoupper($accesskey) . ']';
622 $rel = $nofollow ?
'rel="nofollow" ' : '';
625 $pre . ($inner ?
: $caption) . $suf,
626 'class="action ' . $type . '" ' .
628 'title="' . hsc($caption) . $addTitle . '"',
632 if ($return) return $out;
638 * Check the actions and get data for buttons and links
640 * @param string $type
641 * @return array|bool|string
643 * @author Adrian Lang <mail@adrianlang.de>
644 * @author Andreas Gohr <andi@splitbrain.org>
645 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
646 * @deprecated 2017-09-01 see devel:menus
648 function tpl_get_action($type)
650 dbg_deprecated('see devel:menus');
651 if ($type == 'history') $type = 'revisions';
652 if ($type == 'subscription') $type = 'subscribe';
653 if ($type == 'img_backto') $type = 'imgBackto';
655 $class = '\\dokuwiki\\Menu\\Item\\' . ucfirst($type);
656 if (class_exists($class)) {
658 /** @var AbstractItem $item */
659 $item = new $class();
660 $data = $item->getLegacyData();
662 } catch (RuntimeException
$ignored) {
672 'params' => ['do' => $type],
679 $evt = new Event('TPL_ACTION_GET', $data);
680 if ($evt->advise_before()) {
681 //handle unknown types
683 $data = '[unknown %s type]';
686 $evt->advise_after();
693 * Wrapper around tpl_button() and tpl_actionlink()
695 * @param string $type action command
696 * @param bool $link link or form button?
697 * @param string|bool $wrapper HTML element wrapper
698 * @param bool $return return or print
699 * @param string $pre prefix for links
700 * @param string $suf suffix for links
701 * @param string $inner inner HTML for links
702 * @return bool|string
704 * @author Anika Henke <anika@selfthinker.org>
705 * @deprecated 2017-09-01 see devel:menus
707 function tpl_action($type, $link = false, $wrapper = false, $return = false, $pre = '', $suf = '', $inner = '')
709 dbg_deprecated('see devel:menus');
712 $out .= tpl_actionlink($type, $pre, $suf, $inner, true);
714 $out .= tpl_button($type, true);
716 if ($out && $wrapper) $out = "<$wrapper>$out</$wrapper>";
718 if ($return) return $out;
724 * Print the search form
726 * If the first parameter is given a div with the ID 'qsearch_out' will
727 * be added which instructs the ajax pagequicksearch to kick in and place
728 * its output into this div. The second parameter controls the propritary
729 * attribute autocomplete. If set to false this attribute will be set with an
730 * value of "off" to instruct the browser to disable it's own built in
731 * autocompletion feature (MSIE and Firefox)
734 * @param bool $autocomplete
737 * @author Andreas Gohr <andi@splitbrain.org>
739 function tpl_searchform($ajax = true, $autocomplete = true)
746 // don't print the search form if search action has been disabled
747 if (!actionOK('search')) return false;
749 $searchForm = new Form([
754 'id' => 'dw__search',
756 $searchForm->addTagOpen('div')->addClass('no');
757 $searchForm->setHiddenField('do', 'search');
758 $searchForm->setHiddenField('id', $ID);
759 $searchForm->addTextInput('q')
764 'placeholder' => $lang['btn_search'],
765 'autocomplete' => $autocomplete ?
'on' : 'off',
768 ->val($ACT === 'search' ?
$QUERY : '')
770 $searchForm->addButton('', $lang['btn_search'])->attrs([
772 'title' => $lang['btn_search'],
775 $searchForm->addTagOpen('div')->id('qsearch__out')->addClass('ajax_qsearch JSpopup');
776 $searchForm->addTagClose('div');
778 $searchForm->addTagClose('div');
780 echo $searchForm->toHTML('QuickSearch');
786 * Print the breadcrumbs trace
788 * @param string $sep Separator between entries
789 * @param bool $return return or print
790 * @return bool|string
792 * @author Andreas Gohr <andi@splitbrain.org>
794 function tpl_breadcrumbs($sep = null, $return = false)
800 if (!$conf['breadcrumbs']) return false;
803 if (is_null($sep)) $sep = '•';
807 $crumbs = breadcrumbs(); //setup crumb trace
809 $crumbs_sep = ' <span class="bcsep">' . $sep . '</span> ';
811 //render crumbs, highlight the last one
812 $out .= '<span class="bchead">' . $lang['breadcrumb'] . '</span>';
813 $last = count($crumbs);
815 foreach ($crumbs as $id => $name) {
818 if ($i == $last) $out .= '<span class="curid">';
819 $out .= '<bdi>' . tpl_link(wl($id), hsc($name), 'class="breadcrumbs" title="' . $id . '"', true) . '</bdi>';
820 if ($i == $last) $out .= '</span>';
822 if ($return) return $out;
828 * Hierarchical breadcrumbs
830 * This code was suggested as replacement for the usual breadcrumbs.
831 * It only makes sense with a deep site structure.
833 * @param string $sep Separator between entries
834 * @param bool $return return or print
835 * @return bool|string
837 * @todo May behave strangely in RTL languages
838 * @author <fredrik@averpil.com>
839 * @author Andreas Gohr <andi@splitbrain.org>
840 * @author Nigel McNie <oracle.shinoda@gmail.com>
841 * @author Sean Coates <sean@caedmon.net>
843 function tpl_youarehere($sep = null, $return = false)
850 if (!$conf['youarehere']) return false;
853 if (is_null($sep)) $sep = ' » ';
857 $parts = explode(':', $ID);
858 $count = count($parts);
860 $out .= '<span class="bchead">' . $lang['youarehere'] . ' </span>';
862 // always print the startpage
863 $out .= '<span class="home">' . tpl_pagelink(':' . $conf['start'], null, true) . '</span>';
865 // print intermediate namespace links
867 for ($i = 0; $i < $count - 1; $i++
) {
868 $part .= $parts[$i] . ':';
870 if ($page == $conf['start']) continue; // Skip startpage
873 $out .= $sep . tpl_pagelink($page, null, true);
876 // print current page, skipping start page, skipping for namespace index
878 $page = (new PageResolver('root'))->resolveId($page);
879 if ($page == $part . $parts[$i]) {
880 if ($return) return $out;
885 $page = $part . $parts[$i];
886 if ($page == $conf['start']) {
887 if ($return) return $out;
892 $out .= tpl_pagelink($page, null, true);
893 if ($return) return $out;
899 * Print info if the user is logged in
900 * and show full name in that case
902 * Could be enhanced with a profile link in future?
906 * @author Andreas Gohr <andi@splitbrain.org>
908 function tpl_userinfo()
911 /** @var Input $INPUT */
914 if ($INPUT->server
->str('REMOTE_USER')) {
915 echo $lang['loggedinas'] . ' ' . userlink();
922 * Print some info about the current page
924 * @param bool $ret return content instead of printing it
925 * @return bool|string
927 * @author Andreas Gohr <andi@splitbrain.org>
929 function tpl_pageinfo($ret = false)
936 // return if we are not allowed to view the page
937 if (!auth_quickaclcheck($ID)) {
941 // prepare date and path
942 $fn = $INFO['filepath'];
943 if (!$conf['fullpath']) {
945 $fn = str_replace($conf['olddir'] . '/', '', $fn);
947 $fn = str_replace($conf['datadir'] . '/', '', $fn);
950 $fn = utf8_decodeFN($fn);
951 $dateLocal = dformat($INFO['lastmod']);
952 $dateIso = date(DATE_ISO8601
, $INFO['lastmod']);
955 if ($INFO['exists']) {
956 $out = '<bdi>' . $fn . '</bdi>';
958 $out .= $lang['lastmod'];
960 $out .= '<time datetime="' . $dateIso . '">' . $dateLocal . '</time>';
961 if ($INFO['editor']) {
962 $out .= ' ' . $lang['by'] . ' ';
963 $out .= '<bdi>' . editorinfo($INFO['editor']) . '</bdi>';
965 $out .= ' (' . $lang['external_edit'] . ')';
967 if ($INFO['locked']) {
969 $out .= $lang['lockedby'];
971 $out .= '<bdi>' . editorinfo($INFO['locked']) . '</bdi>';
984 * Prints or returns the name of the given page (current one if none given).
986 * If useheading is enabled this will use the first headline else
987 * the given ID is used.
989 * @param string $id page id
990 * @param bool $ret return content instead of printing
991 * @return bool|string
993 * @author Andreas Gohr <andi@splitbrain.org>
995 function tpl_pagetitle($id = null, $ret = false)
997 global $ACT, $conf, $lang;
1005 if (useHeading('navigation')) {
1006 $first_heading = p_get_first_heading($id);
1007 if ($first_heading) $name = $first_heading;
1010 // default page title is the page name, modify with the current action
1014 $page_title = $lang['btn_admin'];
1015 // try to get the plugin name
1016 /** @var AdminPlugin $plugin */
1017 if ($plugin = plugin_getRequestAdminPlugin()) {
1018 $plugin_title = $plugin->getMenuText($conf['lang']);
1019 $page_title = $plugin_title ?
: $plugin->getPluginName();
1023 // show action as title
1030 $page_title = $lang['btn_' . $ACT];
1033 // add pen during editing
1036 $page_title = "✎ " . $name;
1039 // add action to page name
1041 $page_title = $name . ' - ' . $lang['btn_revs'];
1044 // add action to page name
1048 $page_title = $name . ' - ' . $lang['btn_' . $ACT];
1051 default: // SHOW and anything else not included
1052 $page_title = $name;
1056 return hsc($page_title);
1058 echo hsc($page_title);
1064 * Returns the requested EXIF/IPTC tag from the current image
1066 * If $tags is an array all given tags are tried until a
1067 * value is found. If no value is found $alt is returned.
1069 * Which texts are known is defined in the functions _exifTagNames
1070 * and _iptcTagNames() in inc/jpeg.php (You need to prepend IPTC
1071 * to the names of the latter one)
1073 * Only allowed in: detail.php
1075 * @param array|string $tags tag or array of tags to try
1076 * @param string $alt alternative output if no data was found
1077 * @param null|string $src the image src, uses global $SRC if not given
1080 * @author Andreas Gohr <andi@splitbrain.org>
1082 function tpl_img_getTag($tags, $alt = '', $src = null)
1085 global $SRC, $imgMeta;
1087 if (is_null($src)) $src = $SRC;
1088 if (is_null($src)) return $alt;
1090 if (!isset($imgMeta)) {
1091 $imgMeta = new JpegMeta($src);
1093 if ($imgMeta === false) return $alt;
1094 $info = cleanText($imgMeta->getField($tags));
1095 if (!$info) return $alt;
1101 * Garbage collects up the open JpegMeta object.
1103 function tpl_img_close()
1110 * Prints a html description list of the metatags of the current image
1112 function tpl_img_meta()
1116 $tags = tpl_get_img_meta();
1119 foreach ($tags as $tag) {
1120 $label = $lang[$tag['langkey']];
1121 if (!$label) $label = $tag['langkey'] . ':';
1123 echo '<dt>' . $label . '</dt><dd>';
1124 if ($tag['type'] == 'date') {
1125 echo dformat($tag['value']);
1127 echo hsc($tag['value']);
1135 * Returns metadata as configured in mediameta config file, ready for creating html
1137 * @return array with arrays containing the entries:
1138 * - string langkey key to lookup in the $lang var, if not found printed as is
1139 * - string type type of value
1140 * - string value tag value (unescaped)
1142 function tpl_get_img_meta()
1145 $config_files = getConfigFiles('mediameta');
1146 foreach ($config_files as $config_file) {
1147 if (file_exists($config_file)) {
1148 include($config_file);
1152 foreach ($fields as $tag) {
1154 if (!empty($tag[0])) {
1157 if (isset($tag[3]) && is_array($tag[3])) {
1158 $t = array_merge($t, $tag[3]);
1160 $value = tpl_img_getTag($t);
1162 $tags[] = ['langkey' => $tag[1], 'type' => $tag[2], 'value' => $value];
1169 * Prints the image with a link to the full sized version
1171 * Only allowed in: detail.php
1173 * @triggers TPL_IMG_DISPLAY
1174 * @param int $maxwidth - maximal width of the image
1175 * @param int $maxheight - maximal height of the image
1176 * @param bool $link - link to the orginal size?
1177 * @param array $params - additional image attributes
1178 * @return bool Result of TPL_IMG_DISPLAY
1180 function tpl_img($maxwidth = 0, $maxheight = 0, $link = true, $params = null)
1183 /** @var Input $INPUT */
1186 $w = (int)tpl_img_getTag('File.Width');
1187 $h = (int)tpl_img_getTag('File.Height');
1189 //resize to given max values
1192 if ($maxwidth && $w >= $maxwidth) {
1193 $ratio = $maxwidth / $w;
1194 } elseif ($maxheight && $h > $maxheight) {
1195 $ratio = $maxheight / $h;
1197 } elseif ($maxheight && $h >= $maxheight) {
1198 $ratio = $maxheight / $h;
1199 } elseif ($maxwidth && $w > $maxwidth) {
1200 $ratio = $maxwidth / $w;
1203 $w = floor($ratio * $w);
1204 $h = floor($ratio * $h);
1208 $url = ml($IMG, ['cache' => $INPUT->str('cache'), 'rev' => $REV], true, '&');
1209 $src = ml($IMG, ['cache' => $INPUT->str('cache'), 'rev' => $REV, 'w' => $w, 'h' => $h], true, '&');
1211 //prepare attributes
1212 $alt = tpl_img_getTag('Simple.Title');
1213 if (is_null($params)) {
1218 if ($w) $p['width'] = $w;
1219 if ($h) $p['height'] = $h;
1220 $p['class'] = 'img_detail';
1229 $data = ['url' => ($link ?
$url : null), 'params' => $p];
1230 return Event
::createAndTrigger('TPL_IMG_DISPLAY', $data, '_tpl_img_action', true);
1234 * Default action for TPL_IMG_DISPLAY
1236 * @param array $data
1239 function _tpl_img_action($data)
1242 $p = buildAttributes($data['params']);
1244 if ($data['url']) echo '<a href="' . hsc($data['url']) . '" title="' . $lang['mediaview'] . '">';
1245 echo '<img ' . $p . '/>';
1246 if ($data['url']) echo '</a>';
1251 * This function inserts a small gif which in reality is the indexer function.
1253 * Should be called somewhere at the very end of the main.php template
1257 function tpl_indexerWebBug()
1262 $p['src'] = DOKU_BASE
. 'lib/exe/taskrunner.php?id=' . rawurlencode($ID) .
1264 $p['width'] = 2; //no more 1x1 px image because we live in times of ad blockers...
1267 $att = buildAttributes($p);
1268 echo "<img $att />";
1275 * use this function to access template configuration variables
1277 * @param string $id name of the value to access
1278 * @param mixed $notset what to return if the setting is not available
1281 function tpl_getConf($id, $notset = false)
1284 static $tpl_configloaded = false;
1286 $tpl = $conf['template'];
1288 if (!$tpl_configloaded) {
1289 $tconf = tpl_loadConfig();
1290 if ($tconf !== false) {
1291 foreach ($tconf as $key => $value) {
1292 if (isset($conf['tpl'][$tpl][$key])) continue;
1293 $conf['tpl'][$tpl][$key] = $value;
1295 $tpl_configloaded = true;
1299 return $conf['tpl'][$tpl][$id] ??
$notset;
1305 * reads all template configuration variables
1306 * this function is automatically called by tpl_getConf()
1308 * @return false|array
1310 function tpl_loadConfig()
1313 $file = tpl_incdir() . '/conf/default.php';
1316 if (!file_exists($file)) return false;
1318 // load default config file
1329 * use this function to access template language variables
1331 * @param string $id key of language string
1334 function tpl_getLang($id)
1338 if (count($lang) === 0) {
1339 global $conf, $config_cascade; // definitely don't invoke "global $lang"
1341 $path = tpl_incdir() . 'lang/';
1345 // don't include once
1346 @include
($path . 'en/lang.php');
1347 foreach ($config_cascade['lang']['template'] as $config_file) {
1348 if (file_exists($config_file . $conf['template'] . '/en/lang.php')) {
1349 include($config_file . $conf['template'] . '/en/lang.php');
1353 if ($conf['lang'] != 'en') {
1354 @include
($path . $conf['lang'] . '/lang.php');
1355 foreach ($config_cascade['lang']['template'] as $config_file) {
1356 if (file_exists($config_file . $conf['template'] . '/' . $conf['lang'] . '/lang.php')) {
1357 include($config_file . $conf['template'] . '/' . $conf['lang'] . '/lang.php');
1362 return $lang[$id] ??
'';
1366 * Retrieve a language dependent file and pass to xhtml renderer for display
1367 * template equivalent of p_locale_xhtml()
1369 * @param string $id id of language dependent wiki page
1370 * @return string parsed contents of the wiki page in xhtml format
1372 function tpl_locale_xhtml($id)
1374 return p_cached_output(tpl_localeFN($id));
1378 * Prepends appropriate path for a language dependent filename
1380 * @param string $id id of localized text
1381 * @return string wiki text
1383 function tpl_localeFN($id)
1385 $path = tpl_incdir() . 'lang/';
1387 $file = DOKU_CONF
. 'template_lang/' . $conf['template'] . '/' . $conf['lang'] . '/' . $id . '.txt';
1388 if (!file_exists($file)) {
1389 $file = $path . $conf['lang'] . '/' . $id . '.txt';
1390 if (!file_exists($file)) {
1391 //fall back to english
1392 $file = $path . 'en/' . $id . '.txt';
1399 * prints the "main content" in the mediamanager popup
1401 * Depending on the user's actions this may be a list of
1402 * files in a namespace, the meta editing dialog or
1403 * a message of referencing pages
1405 * Only allowed in mediamanager.php
1407 * @triggers MEDIAMANAGER_CONTENT_OUTPUT
1408 * @param bool $fromajax - set true when calling this function via ajax
1409 * @param string $sort
1411 * @author Andreas Gohr <andi@splitbrain.org>
1413 function tpl_mediaContent($fromajax = false, $sort = 'natural')
1420 /** @var Input $INPUT */
1423 $do = $INPUT->extract('do')->str('do');
1424 if (in_array($do, ['save', 'cancel'])) $do = '';
1427 if ($INPUT->bool('edit')) {
1429 } elseif (is_array($INUSE)) {
1436 // output the content pane, wrapped in an event.
1437 if (!$fromajax) echo '<div id="media__content">';
1438 $data = ['do' => $do];
1439 $evt = new Event('MEDIAMANAGER_CONTENT_OUTPUT', $data);
1440 if ($evt->advise_before()) {
1442 if ($do == 'filesinuse') {
1443 media_filesinuse($INUSE, $IMG);
1444 } elseif ($do == 'filelist') {
1445 media_filelist($NS, $AUTH, $JUMPTO, false, $sort);
1446 } elseif ($do == 'searchlist') {
1447 media_searchlist($INPUT->str('q'), $NS, $AUTH);
1449 msg('Unknown action ' . hsc($do), -1);
1452 $evt->advise_after();
1454 if (!$fromajax) echo '</div>';
1458 * Prints the central column in full-screen media manager
1459 * Depending on the opened tab this may be a list of
1460 * files in a namespace, upload form or search form
1462 * @author Kate Arzamastseva <pshns@ukr.net>
1464 function tpl_mediaFileList()
1470 /** @var Input $INPUT */
1473 $opened_tab = $INPUT->str('tab_files');
1474 if (!$opened_tab ||
!in_array($opened_tab, ['files', 'upload', 'search'])) $opened_tab = 'files';
1475 if ($INPUT->str('mediado') == 'update') $opened_tab = 'upload';
1477 echo '<h2 class="a11y">' . $lang['mediaselect'] . '</h2>' . NL
;
1479 media_tabs_files($opened_tab);
1481 echo '<div class="panelHeader">' . NL
;
1483 $tabTitle = $NS ?
: '[' . $lang['mediaroot'] . ']';
1484 printf($lang['media_' . $opened_tab], '<strong>' . hsc($tabTitle) . '</strong>');
1486 if ($opened_tab === 'search' ||
$opened_tab === 'files') {
1487 media_tab_files_options();
1491 echo '<div class="panelContent">' . NL
;
1492 if ($opened_tab == 'files') {
1493 media_tab_files($NS, $AUTH, $JUMPTO);
1494 } elseif ($opened_tab == 'upload') {
1495 media_tab_upload($NS, $AUTH, $JUMPTO);
1496 } elseif ($opened_tab == 'search') {
1497 media_tab_search($NS, $AUTH);
1503 * Prints the third column in full-screen media manager
1504 * Depending on the opened tab this may be details of the
1505 * selected file, the meta editing dialog or
1506 * list of file revisions
1508 * @param string $image
1509 * @param boolean $rev
1511 * @author Kate Arzamastseva <pshns@ukr.net>
1513 function tpl_mediaFileDetails($image, $rev)
1515 global $conf, $DEL, $lang;
1516 /** @var Input $INPUT */
1520 !file_exists(mediaFN($image)) &&
1521 file_exists(mediaMetaFN($image, '.changes')) &&
1522 $conf['mediarevisions']
1524 if (!$image ||
(!file_exists(mediaFN($image)) && !$removed) ||
$DEL) return;
1525 if ($rev && !file_exists(mediaFN($image, $rev))) $rev = false;
1526 $ns = getNS($image);
1527 $do = $INPUT->str('mediado');
1529 $opened_tab = $INPUT->str('tab_details');
1531 $tab_array = ['view'];
1532 [, $mime] = mimetype($image);
1533 if ($mime == 'image/jpeg') {
1534 $tab_array[] = 'edit';
1536 if ($conf['mediarevisions']) {
1537 $tab_array[] = 'history';
1540 if (!$opened_tab ||
!in_array($opened_tab, $tab_array)) $opened_tab = 'view';
1541 if ($INPUT->bool('edit')) $opened_tab = 'edit';
1542 if ($do == 'restore') $opened_tab = 'view';
1544 media_tabs_details($image, $opened_tab);
1546 echo '<div class="panelHeader"><h3>';
1547 [$ext] = mimetype($image, false);
1548 $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
1549 $class = 'select mediafile mf_' . $class;
1551 $attributes = $rev ?
['rev' => $rev] : [];
1552 $tabTitle = sprintf(
1553 '<strong><a href="%s" class="%s" title="%s">%s</a></strong>',
1554 ml($image, $attributes),
1559 if ($opened_tab === 'view' && $rev) {
1560 printf($lang['media_viewold'], $tabTitle, dformat($rev));
1562 printf($lang['media_' . $opened_tab], $tabTitle);
1565 echo '</h3></div>' . NL
;
1567 echo '<div class="panelContent">' . NL
;
1569 if ($opened_tab == 'view') {
1570 media_tab_view($image, $ns, null, $rev);
1571 } elseif ($opened_tab == 'edit' && !$removed) {
1572 media_tab_edit($image, $ns);
1573 } elseif ($opened_tab == 'history' && $conf['mediarevisions']) {
1574 media_tab_history($image, $ns);
1581 * prints the namespace tree in the mediamanager popup
1583 * Only allowed in mediamanager.php
1585 * @author Andreas Gohr <andi@splitbrain.org>
1587 function tpl_mediaTree()
1590 echo '<div id="media__tree">';
1596 * Print a dropdown menu with all DokuWiki actions
1598 * Note: this will not use any pretty URLs
1600 * @param string $empty empty option label
1601 * @param string $button submit button label
1603 * @author Andreas Gohr <andi@splitbrain.org>
1604 * @deprecated 2017-09-01 see devel:menus
1606 function tpl_actiondropdown($empty = '', $button = '>')
1608 dbg_deprecated('see devel:menus');
1609 $menu = new MobileMenu();
1610 echo $menu->getDropdown($empty, $button);
1614 * Print a informational line about the used license
1616 * @param string $img print image? (|button|badge)
1617 * @param bool $imgonly skip the textual description?
1618 * @param bool $return when true don't print, but return HTML
1619 * @param bool $wrap wrap in div with class="license"?
1622 * @author Andreas Gohr <andi@splitbrain.org>
1624 function tpl_license($img = 'badge', $imgonly = false, $return = false, $wrap = true)
1629 if (!$conf['license']) return '';
1630 if (!is_array($license[$conf['license']])) return '';
1631 $lic = $license[$conf['license']];
1632 $target = ($conf['target']['extern']) ?
' target="' . $conf['target']['extern'] . '"' : '';
1635 if ($wrap) $out .= '<div class="license">';
1637 $src = license_img($img);
1639 $out .= '<a href="' . $lic['url'] . '" rel="license"' . $target;
1640 $out .= '><img src="' . DOKU_BASE
. $src . '" alt="' . $lic['name'] . '" /></a>';
1641 if (!$imgonly) $out .= ' ';
1645 $out .= $lang['license'] . ' ';
1646 $out .= '<bdi><a href="' . $lic['url'] . '" rel="license" class="urlextern"' . $target;
1647 $out .= '>' . $lic['name'] . '</a></bdi>';
1649 if ($wrap) $out .= '</div>';
1651 if ($return) return $out;
1657 * Includes the rendered HTML of a given page
1659 * This function is useful to populate sidebars or similar features in a
1662 * @param string $pageid The page name you want to include
1663 * @param bool $print Should the content be printed or returned only
1664 * @param bool $propagate Search higher namespaces, too?
1665 * @param bool $useacl Include the page only if the ACLs check out?
1666 * @return bool|null|string
1668 function tpl_include_page($pageid, $print = true, $propagate = false, $useacl = true)
1671 $pageid = page_findnearest($pageid, $useacl);
1672 } elseif ($useacl && auth_quickaclcheck($pageid) == AUTH_NONE
) {
1675 if (!$pageid) return false;
1679 $html = p_wiki_xhtml($pageid, '', false);
1682 if ($print) echo $html;
1687 * Display the subscribe form
1689 * @author Adrian Lang <lang@cosmocode.de>
1690 * @deprecated 2020-07-23
1692 function tpl_subscribe()
1694 dbg_deprecated(Subscribe
::class . '::show()');
1695 (new Subscribe())->show();
1699 * Tries to send already created content right to the browser
1701 * Wraps around ob_flush() and flush()
1703 * @author Andreas Gohr <andi@splitbrain.org>
1705 function tpl_flush()
1707 if (ob_get_level() > 0) ob_flush();
1712 * Tries to find a ressource file in the given locations.
1714 * If a given location starts with a colon it is assumed to be a media
1715 * file, otherwise it is assumed to be relative to the current template
1717 * @param string[] $search locations to look at
1718 * @param bool $abs if to use absolute URL
1719 * @param array &$imginfo filled with getimagesize()
1720 * @param bool $fallback use fallback image if target isn't found or return 'false' if potential
1721 * false result is required
1724 * @author Andreas Gohr <andi@splitbrain.org>
1726 function tpl_getMediaFile($search, $abs = false, &$imginfo = null, $fallback = true)
1731 // loop through candidates until a match was found:
1732 foreach ($search as $img) {
1733 if (str_starts_with($img, ':')) {
1734 $file = mediaFN($img);
1737 $file = tpl_incdir() . $img;
1741 if (file_exists($file)) break;
1744 // manage non existing target
1745 if (!file_exists($file)) {
1746 // give result for fallback image
1748 $file = DOKU_INC
. 'lib/images/blank.gif';
1749 // stop process if false result is required (if $fallback is false)
1755 // fetch image data if requested
1756 if (!is_null($imginfo)) {
1757 $imginfo = getimagesize($file);
1762 $url = ml($img, '', true, '', $abs);
1764 $url = tpl_basedir() . $img;
1765 if ($abs) $url = DOKU_URL
. substr($url, strlen(DOKU_REL
));
1772 * PHP include a file
1774 * either from the conf directory if it exists, otherwise use
1775 * file in the template's root directory.
1777 * The function honours config cascade settings and looks for the given
1778 * file next to the ´main´ config files, in the order protected, local,
1781 * Note: no escaping or sanity checking is done here. Never pass user input
1784 * @param string $file
1786 * @author Andreas Gohr <andi@splitbrain.org>
1787 * @author Anika Henke <anika@selfthinker.org>
1789 function tpl_includeFile($file)
1791 global $config_cascade;
1792 foreach (['protected', 'local', 'default'] as $config_group) {
1793 if (empty($config_cascade['main'][$config_group])) continue;
1794 foreach ($config_cascade['main'][$config_group] as $conf_file) {
1795 $dir = dirname($conf_file);
1796 if (file_exists("$dir/$file")) {
1797 include("$dir/$file");
1803 // still here? try the template dir
1804 $file = tpl_incdir() . $file;
1805 if (file_exists($file)) {
1811 * Returns <link> tag for various icon types (favicon|mobile|generic)
1813 * @param array $types - list of icon types to display (favicon|mobile|generic)
1816 * @author Anika Henke <anika@selfthinker.org>
1818 function tpl_favicon($types = ['favicon'])
1823 foreach ($types as $type) {
1826 $look = [':wiki:favicon.ico', ':favicon.ico', 'images/favicon.ico'];
1827 $return .= '<link rel="shortcut icon" href="' . tpl_getMediaFile($look) . '" />' . NL
;
1830 $look = [':wiki:apple-touch-icon.png', ':apple-touch-icon.png', 'images/apple-touch-icon.png'];
1831 $return .= '<link rel="apple-touch-icon" href="' . tpl_getMediaFile($look) . '" />' . NL
;
1834 // ideal world solution, which doesn't work in any browser yet
1835 $look = [':wiki:favicon.svg', ':favicon.svg', 'images/favicon.svg'];
1836 $return .= '<link rel="icon" href="' . tpl_getMediaFile($look) . '" type="image/svg+xml" />' . NL
;
1845 * Prints full-screen media manager
1847 * @author Kate Arzamastseva <pshns@ukr.net>
1849 function tpl_media()
1851 global $NS, $IMG, $JUMPTO, $REV, $lang, $fullscreen, $INPUT;
1853 require_once DOKU_INC
. 'lib/exe/mediamanager.php';
1856 $image = cleanID($INPUT->str('image'));
1857 if (isset($IMG)) $image = $IMG;
1858 if (isset($JUMPTO)) $image = $JUMPTO;
1859 if (isset($REV) && !$JUMPTO) $rev = $REV;
1861 echo '<div id="mediamanager__page">' . NL
;
1862 echo '<h1>' . $lang['btn_media'] . '</h1>' . NL
;
1865 echo '<div class="panel namespaces">' . NL
;
1866 echo '<h2>' . $lang['namespaces'] . '</h2>' . NL
;
1867 echo '<div class="panelHeader">';
1868 echo $lang['media_namespaces'];
1871 echo '<div class="panelContent" id="media__tree">' . NL
;
1876 echo '<div class="panel filelist">' . NL
;
1877 tpl_mediaFileList();
1880 echo '<div class="panel file">' . NL
;
1881 echo '<h2 class="a11y">' . $lang['media_file'] . '</h2>' . NL
;
1882 tpl_mediaFileDetails($image, $rev);
1889 * Return useful layout classes
1893 * @author Anika Henke <anika@selfthinker.org>
1895 function tpl_classes()
1897 global $ACT, $conf, $ID, $INFO;
1898 /** @var Input $INPUT */
1904 'tpl_' . $conf['template'],
1905 $INPUT->server
->bool('REMOTE_USER') ?
'loggedIn' : '',
1906 (isset($INFO['exists']) && $INFO['exists']) ?
'' : 'notFound',
1907 ($ID == $conf['start']) ?
'home' : ''
1909 return implode(' ', $classes);
1913 * Create event for tools menues
1915 * @param string $toolsname name of menu
1916 * @param array $items
1917 * @param string $view e.g. 'main', 'detail', ...
1919 * @author Anika Henke <anika@selfthinker.org>
1920 * @deprecated 2017-09-01 see devel:menus
1922 function tpl_toolsevent($toolsname, $items, $view = 'main')
1924 dbg_deprecated('see devel:menus');
1925 $data = ['view' => $view, 'items' => $items];
1927 $hook = 'TEMPLATE_' . strtoupper($toolsname) . '_DISPLAY';
1928 $evt = new Event($hook, $data);
1929 if ($evt->advise_before()) {
1930 foreach ($evt->data
['items'] as $html) echo $html;
1932 $evt->advise_after();