3 * DokuWiki template functions
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Andreas Gohr <andi@splitbrain.org>
9 use dokuwiki\Extension\AdminPlugin
;
10 use dokuwiki\Extension\Event
;
13 * Access a template file
15 * Returns the path to the given file inside the current template, uses
16 * default template if the custom version doesn't exist.
18 * @author Andreas Gohr <andi@splitbrain.org>
22 function template($file) {
25 if(@is_readable
(DOKU_INC
.'lib/tpl/'.$conf['template'].'/'.$file))
26 return DOKU_INC
.'lib/tpl/'.$conf['template'].'/'.$file;
28 return DOKU_INC
.'lib/tpl/dokuwiki/'.$file;
32 * Convenience function to access template dir from local FS
34 * This replaces the deprecated DOKU_TPLINC constant
36 * @author Andreas Gohr <andi@splitbrain.org>
37 * @param string $tpl The template to use, default to current one
40 function tpl_incdir($tpl='') {
42 if(!$tpl) $tpl = $conf['template'];
43 return DOKU_INC
.'lib/tpl/'.$tpl.'/';
47 * Convenience function to access template dir from web
49 * This replaces the deprecated DOKU_TPL constant
51 * @author Andreas Gohr <andi@splitbrain.org>
52 * @param string $tpl The template to use, default to current one
55 function tpl_basedir($tpl='') {
57 if(!$tpl) $tpl = $conf['template'];
58 return DOKU_BASE
.'lib/tpl/'.$tpl.'/';
64 * This function is used for printing all the usual content
65 * (defined by the global $ACT var) by calling the appropriate
66 * outputfunction(s) from html.php
68 * Everything that doesn't use the main template file isn't
69 * handled by this function. ACL stuff is not done here either.
71 * @author Andreas Gohr <andi@splitbrain.org>
73 * @triggers TPL_ACT_RENDER
74 * @triggers TPL_CONTENT_DISPLAY
75 * @param bool $prependTOC should the TOC be displayed here?
76 * @return bool true if any output
78 function tpl_content($prependTOC = true) {
81 $INFO['prependTOC'] = $prependTOC;
84 Event
::createAndTrigger('TPL_ACT_RENDER', $ACT, 'tpl_content_core');
85 $html_output = ob_get_clean();
86 Event
::createAndTrigger('TPL_CONTENT_DISPLAY', $html_output, 'ptln');
88 return !empty($html_output);
92 * Default Action of TPL_ACT_RENDER
96 function tpl_content_core() {
97 $router = \dokuwiki\ActionRouter
::getInstance();
99 $router->getAction()->tplContent();
100 } catch(\dokuwiki\Action\Exception\FatalException
$e) {
101 // there was no content for the action
102 msg(hsc($e->getMessage()), -1);
109 * Places the TOC where the function is called
111 * If you use this you most probably want to call tpl_content with
114 * @author Andreas Gohr <andi@splitbrain.org>
116 * @param bool $return Should the TOC be returned instead to be printed?
119 function tpl_toc($return = false) {
130 // if a TOC was prepared in global scope, always use it
132 } elseif(($ACT == 'show' ||
substr($ACT, 0, 6) == 'export') && !$REV && $INFO['exists']) {
133 // get TOC from metadata, render if neccessary
134 $meta = p_get_metadata($ID, '', METADATA_RENDER_USING_CACHE
);
135 if(isset($meta['internal']['toc'])) {
136 $tocok = $meta['internal']['toc'];
140 $toc = isset($meta['description']['tableofcontents']) ?
$meta['description']['tableofcontents'] : null;
141 if(!$tocok ||
!is_array($toc) ||
!$conf['tocminheads'] ||
count($toc) < $conf['tocminheads']) {
144 } elseif($ACT == 'admin') {
145 // try to load admin plugin TOC
146 /** @var $plugin AdminPlugin */
147 if ($plugin = plugin_getRequestAdminPlugin()) {
148 $toc = $plugin->getTOC();
149 $TOC = $toc; // avoid later rebuild
153 Event
::createAndTrigger('TPL_TOC_RENDER', $toc, null, false);
154 $html = html_TOC($toc);
155 if($return) return $html;
161 * Handle the admin page contents
163 * @author Andreas Gohr <andi@splitbrain.org>
167 function tpl_admin() {
173 $class = $INPUT->str('page');
175 $pluginlist = plugin_list('admin');
177 if(in_array($class, $pluginlist)) {
178 // attempt to load the plugin
179 /** @var $plugin AdminPlugin */
180 $plugin = plugin_load('admin', $class);
184 if($plugin !== null) {
185 if(!is_array($TOC)) $TOC = $plugin->getTOC(); //if TOC wasn't requested yet
186 if($INFO['prependTOC']) tpl_toc();
189 $admin = new dokuwiki\Ui\
Admin();
196 * Print the correct HTML meta headers
198 * This has to go into the head section of your template.
200 * @author Andreas Gohr <andi@splitbrain.org>
202 * @triggers TPL_METAHEADER_OUTPUT
203 * @param bool $alt Should feeds and alternative format links be added?
206 function tpl_metaheaders($alt = true) {
215 global $updateVersion;
216 /** @var Input $INPUT */
219 // prepare the head array
222 // prepare seed for js and css
223 $tseed = $updateVersion;
224 $depends = getConfigFiles('main');
225 $depends[] = DOKU_CONF
."tpl/".$conf['template']."/style.ini";
226 foreach($depends as $f) $tseed .= @filemtime
($f);
227 $tseed = md5($tseed);
230 $head['meta'][] = array('name'=> 'generator', 'content'=> 'DokuWiki');
231 if(actionOK('search')) {
232 $head['link'][] = array(
233 'rel' => 'search', 'type'=> 'application/opensearchdescription+xml',
234 'href'=> DOKU_BASE
.'lib/exe/opensearch.php', 'title'=> $conf['title']
238 $head['link'][] = array('rel'=> 'start', 'href'=> DOKU_BASE
);
239 if(actionOK('index')) {
240 $head['link'][] = array(
241 'rel' => 'contents', 'href'=> wl($ID, 'do=index', false, '&'),
242 'title'=> $lang['btn_index']
246 if (actionOK('manifest')) {
247 $head['link'][] = array('rel'=> 'manifest', 'href'=> DOKU_BASE
.'lib/exe/manifest.php');
250 $styleUtil = new \dokuwiki\
StyleUtils();
251 $styleIni = $styleUtil->cssStyleini();
252 $replacements = $styleIni['replacements'];
253 if (!empty($replacements['__theme_color__'])) {
254 $head['meta'][] = array('name' => 'theme-color', 'content' => $replacements['__theme_color__']);
258 if(actionOK('rss')) {
259 $head['link'][] = array(
260 'rel' => 'alternate', 'type'=> 'application/rss+xml',
261 'title'=> $lang['btn_recent'], 'href'=> DOKU_BASE
.'feed.php'
263 $head['link'][] = array(
264 'rel' => 'alternate', 'type'=> 'application/rss+xml',
265 'title'=> $lang['currentns'],
266 'href' => DOKU_BASE
.'feed.php?mode=list&ns='.(isset($INFO) ?
$INFO['namespace'] : '')
269 if(($ACT == 'show' ||
$ACT == 'search') && $INFO['writable']) {
270 $head['link'][] = array(
272 'title'=> $lang['btn_edit'],
273 'href' => wl($ID, 'do=edit', false, '&')
277 if(actionOK('rss') && $ACT == 'search') {
278 $head['link'][] = array(
279 'rel' => 'alternate', 'type'=> 'application/rss+xml',
280 'title'=> $lang['searchresult'],
281 'href' => DOKU_BASE
.'feed.php?mode=search&q='.$QUERY
285 if(actionOK('export_xhtml')) {
286 $head['link'][] = array(
287 'rel' => 'alternate', 'type'=> 'text/html', 'title'=> $lang['plainhtml'],
288 'href'=> exportlink($ID, 'xhtml', '', false, '&')
292 if(actionOK('export_raw')) {
293 $head['link'][] = array(
294 'rel' => 'alternate', 'type'=> 'text/plain', 'title'=> $lang['wikimarkup'],
295 'href'=> exportlink($ID, 'raw', '', false, '&')
300 // setup robot tags apropriate for different modes
301 if(($ACT == 'show' ||
$ACT == 'export_xhtml') && !$REV) {
302 if($INFO['exists']) {
304 if((time() - $INFO['lastmod']) >= $conf['indexdelay'] && !isHiddenPage($ID) ) {
305 $head['meta'][] = array('name'=> 'robots', 'content'=> 'index,follow');
307 $head['meta'][] = array('name'=> 'robots', 'content'=> 'noindex,nofollow');
309 $canonicalUrl = wl($ID, '', true, '&');
310 if ($ID == $conf['start']) {
311 $canonicalUrl = DOKU_URL
;
313 $head['link'][] = array('rel'=> 'canonical', 'href'=> $canonicalUrl);
315 $head['meta'][] = array('name'=> 'robots', 'content'=> 'noindex,follow');
317 } elseif(defined('DOKU_MEDIADETAIL')) {
318 $head['meta'][] = array('name'=> 'robots', 'content'=> 'index,follow');
320 $head['meta'][] = array('name'=> 'robots', 'content'=> 'noindex,nofollow');
324 if($ACT == 'show' ||
$ACT == 'export_xhtml') {
325 // keywords (explicit or implicit)
326 if(!empty($INFO['meta']['subject'])) {
327 $head['meta'][] = array('name'=> 'keywords', 'content'=> join(',', $INFO['meta']['subject']));
329 $head['meta'][] = array('name'=> 'keywords', 'content'=> str_replace(':', ',', $ID));
334 $head['link'][] = array(
335 'rel' => 'stylesheet',
336 'href'=> DOKU_BASE
.'lib/exe/css.php?t='.rawurlencode($conf['template']).'&tseed='.$tseed
339 $script = "var NS='".(isset($INFO)?
$INFO['namespace']:'')."';";
340 if($conf['useacl'] && $INPUT->server
->str('REMOTE_USER')) {
341 $script .= "var SIG=".toolbar_signature().";";
344 $script .= 'var JSINFO = ' . json_encode($JSINFO).';';
345 $head['script'][] = array('_data'=> $script);
348 $jquery = getCdnUrls();
349 foreach($jquery as $src) {
350 $head['script'][] = array(
351 'charset' => 'utf-8',
354 ) +
($conf['defer_js'] ?
[ 'defer' => 'defer'] : []);
357 // load our javascript dispatcher
358 $head['script'][] = array(
359 'charset'=> 'utf-8', '_data'=> '',
360 'src' => DOKU_BASE
.'lib/exe/js.php'.'?t='.rawurlencode($conf['template']).'&tseed='.$tseed,
361 ) +
($conf['defer_js'] ?
[ 'defer' => 'defer'] : []);
363 // trigger event here
364 Event
::createAndTrigger('TPL_METAHEADER_OUTPUT', $head, '_tpl_metaheaders_action', true);
369 * prints the array build by tpl_metaheaders
371 * $data is an array of different header tags. Each tag can have multiple
372 * instances. Attributes are given as key value pairs. Values will be HTML
373 * encoded automatically so they should be provided as is in the $data array.
375 * For tags having a body attribute specify the body data in the special
376 * attribute '_data'. This field will NOT BE ESCAPED automatically.
378 * @author Andreas Gohr <andi@splitbrain.org>
382 function _tpl_metaheaders_action($data) {
383 foreach($data as $tag => $inst) {
384 if($tag == 'script') {
385 echo "<!--[if gte IE 9]><!-->\n"; // no scripts for old IE
387 foreach($inst as $attr) {
388 if ( empty($attr) ) { continue; }
389 echo '<', $tag, ' ', buildAttributes($attr);
390 if(isset($attr['_data']) ||
$tag == 'script') {
391 if($tag == 'script' && $attr['_data'])
392 $attr['_data'] = "/*<![CDATA[*/".
396 echo '>', $attr['_data'], '</', $tag, '>';
402 if($tag == 'script') {
403 echo "<!--<![endif]-->\n";
411 * Just builds a link.
413 * @author Andreas Gohr <andi@splitbrain.org>
416 * @param string $name
417 * @param string $more
418 * @param bool $return if true return the link html, otherwise print
419 * @return bool|string html of the link, or true if printed
421 function tpl_link($url, $name, $more = '', $return = false) {
422 $out = '<a href="'.$url.'" ';
423 if($more) $out .= ' '.$more;
424 $out .= ">$name</a>";
425 if($return) return $out;
431 * Prints a link to a WikiPage
433 * Wrapper around html_wikilink
435 * @author Andreas Gohr <andi@splitbrain.org>
437 * @param string $id page id
438 * @param string|null $name the name of the link
439 * @param bool $return
440 * @return true|string
442 function tpl_pagelink($id, $name = null, $return = false) {
443 $out = '<bdi>'.html_wikilink($id, $name).'</bdi>';
444 if($return) return $out;
450 * get the parent page
452 * Tries to find out which page is parent.
453 * returns false if none is available
455 * @author Andreas Gohr <andi@splitbrain.org>
457 * @param string $id page id
458 * @return false|string
460 function tpl_getparent($id) {
461 $parent = getNS($id).':';
462 resolve_pageid('', $parent, $exists);
464 $pos = strrpos(getNS($id), ':');
465 $parent = substr($parent, 0, $pos).':';
466 resolve_pageid('', $parent, $exists);
467 if($parent == $id) return false;
473 * Print one of the buttons
475 * @author Adrian Lang <mail@adrianlang.de>
476 * @see tpl_get_action
478 * @param string $type
479 * @param bool $return
480 * @return bool|string html, or false if no data, true if printed
481 * @deprecated 2017-09-01 see devel:menus
483 function tpl_button($type, $return = false) {
484 dbg_deprecated('see devel:menus');
485 $data = tpl_get_action($type);
486 if($data === false) {
488 } elseif(!is_array($data)) {
489 $out = sprintf($data, 'button');
492 * @var string $accesskey
494 * @var string $method
498 if($id === '#dokuwiki__top') {
499 $out = html_topbtn();
501 $out = html_btn($type, $id, $accesskey, $params, $method);
504 if($return) return $out;
510 * Like the action buttons but links
512 * @author Adrian Lang <mail@adrianlang.de>
513 * @see tpl_get_action
515 * @param string $type action command
516 * @param string $pre prefix of link
517 * @param string $suf suffix of link
518 * @param string $inner innerHML of link
519 * @param bool $return if true it returns html, otherwise prints
520 * @return bool|string html or false if no data, true if printed
521 * @deprecated 2017-09-01 see devel:menus
523 function tpl_actionlink($type, $pre = '', $suf = '', $inner = '', $return = false) {
524 dbg_deprecated('see devel:menus');
526 $data = tpl_get_action($type);
527 if($data === false) {
529 } elseif(!is_array($data)) {
530 $out = sprintf($data, 'link');
533 * @var string $accesskey
535 * @var string $method
536 * @var bool $nofollow
538 * @var string $replacement
541 if(strpos($id, '#') === 0) {
544 $linktarget = wl($id, $params);
546 $caption = $lang['btn_'.$type];
547 if(strpos($caption, '%s')){
548 $caption = sprintf($caption, $replacement);
550 $akey = $addTitle = '';
552 $akey = 'accesskey="'.$accesskey.'" ';
553 $addTitle = ' ['.strtoupper($accesskey).']';
555 $rel = $nofollow ?
'rel="nofollow" ' : '';
557 $linktarget, $pre.(($inner) ?
$inner : $caption).$suf,
558 'class="action '.$type.'" '.
560 'title="'.hsc($caption).$addTitle.'"', true
563 if($return) return $out;
569 * Check the actions and get data for buttons and links
571 * @author Andreas Gohr <andi@splitbrain.org>
572 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
573 * @author Adrian Lang <mail@adrianlang.de>
575 * @param string $type
576 * @return array|bool|string
577 * @deprecated 2017-09-01 see devel:menus
579 function tpl_get_action($type) {
580 dbg_deprecated('see devel:menus');
581 if($type == 'history') $type = 'revisions';
582 if($type == 'subscription') $type = 'subscribe';
583 if($type == 'img_backto') $type = 'imgBackto';
585 $class = '\\dokuwiki\\Menu\\Item\\' . ucfirst($type);
586 if(class_exists($class)) {
588 /** @var \dokuwiki\Menu\Item\AbstractItem $item */
590 $data = $item->getLegacyData();
592 } catch(\RuntimeException
$ignored) {
602 'params' => array('do' => $type),
609 $evt = new Event('TPL_ACTION_GET', $data);
610 if($evt->advise_before()) {
611 //handle unknown types
613 $data = '[unknown %s type]';
616 $evt->advise_after();
623 * Wrapper around tpl_button() and tpl_actionlink()
625 * @author Anika Henke <anika@selfthinker.org>
627 * @param string $type action command
628 * @param bool $link link or form button?
629 * @param string|bool $wrapper HTML element wrapper
630 * @param bool $return return or print
631 * @param string $pre prefix for links
632 * @param string $suf suffix for links
633 * @param string $inner inner HTML for links
634 * @return bool|string
635 * @deprecated 2017-09-01 see devel:menus
637 function tpl_action($type, $link = false, $wrapper = false, $return = false, $pre = '', $suf = '', $inner = '') {
638 dbg_deprecated('see devel:menus');
641 $out .= tpl_actionlink($type, $pre, $suf, $inner, true);
643 $out .= tpl_button($type, true);
645 if($out && $wrapper) $out = "<$wrapper>$out</$wrapper>";
647 if($return) return $out;
649 return $out ?
true : false;
653 * Print the search form
655 * If the first parameter is given a div with the ID 'qsearch_out' will
656 * be added which instructs the ajax pagequicksearch to kick in and place
657 * its output into this div. The second parameter controls the propritary
658 * attribute autocomplete. If set to false this attribute will be set with an
659 * value of "off" to instruct the browser to disable it's own built in
660 * autocompletion feature (MSIE and Firefox)
662 * @author Andreas Gohr <andi@splitbrain.org>
665 * @param bool $autocomplete
668 function tpl_searchform($ajax = true, $autocomplete = true) {
674 // don't print the search form if search action has been disabled
675 if(!actionOK('search')) return false;
677 $searchForm = new dokuwiki\Form\
Form([
682 'id' => 'dw__search',
684 $searchForm->addTagOpen('div')->addClass('no');
685 $searchForm->setHiddenField('do', 'search');
686 $searchForm->setHiddenField('id', $ID);
687 $searchForm->addTextInput('q')
692 'placeholder' => $lang['btn_search'],
693 'autocomplete' => $autocomplete ?
'on' : 'off',
696 ->val($ACT === 'search' ?
$QUERY : '')
699 $searchForm->addButton('', $lang['btn_search'])->attrs([
701 'title' => $lang['btn_search'],
704 $searchForm->addTagOpen('div')->id('qsearch__out')->addClass('ajax_qsearch JSpopup');
705 $searchForm->addTagClose('div');
707 $searchForm->addTagClose('div');
709 echo $searchForm->toHTML('QuickSearch');
715 * Print the breadcrumbs trace
717 * @author Andreas Gohr <andi@splitbrain.org>
719 * @param string $sep Separator between entries
720 * @param bool $return return or print
721 * @return bool|string
723 function tpl_breadcrumbs($sep = null, $return = false) {
728 if(!$conf['breadcrumbs']) return false;
731 if(is_null($sep)) $sep = '•';
735 $crumbs = breadcrumbs(); //setup crumb trace
737 $crumbs_sep = ' <span class="bcsep">'.$sep.'</span> ';
739 //render crumbs, highlight the last one
740 $out .= '<span class="bchead">'.$lang['breadcrumb'].'</span>';
741 $last = count($crumbs);
743 foreach($crumbs as $id => $name) {
746 if($i == $last) $out .= '<span class="curid">';
747 $out .= '<bdi>' . tpl_link(wl($id), hsc($name), 'class="breadcrumbs" title="'.$id.'"', true) . '</bdi>';
748 if($i == $last) $out .= '</span>';
750 if($return) return $out;
752 return $out ?
true : false;
756 * Hierarchical breadcrumbs
758 * This code was suggested as replacement for the usual breadcrumbs.
759 * It only makes sense with a deep site structure.
761 * @author Andreas Gohr <andi@splitbrain.org>
762 * @author Nigel McNie <oracle.shinoda@gmail.com>
763 * @author Sean Coates <sean@caedmon.net>
764 * @author <fredrik@averpil.com>
765 * @todo May behave strangely in RTL languages
767 * @param string $sep Separator between entries
768 * @param bool $return return or print
769 * @return bool|string
771 function tpl_youarehere($sep = null, $return = false) {
777 if(!$conf['youarehere']) return false;
780 if(is_null($sep)) $sep = ' » ';
784 $parts = explode(':', $ID);
785 $count = count($parts);
787 $out .= '<span class="bchead">'.$lang['youarehere'].' </span>';
789 // always print the startpage
790 $out .= '<span class="home">' . tpl_pagelink(':'.$conf['start'], null, true) . '</span>';
792 // print intermediate namespace links
794 for($i = 0; $i < $count - 1; $i++
) {
795 $part .= $parts[$i].':';
797 if($page == $conf['start']) continue; // Skip startpage
800 $out .= $sep . tpl_pagelink($page, null, true);
803 // print current page, skipping start page, skipping for namespace index
804 resolve_pageid('', $page, $exists);
805 if (isset($page) && $page == $part.$parts[$i]) {
806 if($return) return $out;
810 $page = $part.$parts[$i];
811 if($page == $conf['start']) {
812 if($return) return $out;
817 $out .= tpl_pagelink($page, null, true);
818 if($return) return $out;
820 return $out ?
true : false;
824 * Print info if the user is logged in
825 * and show full name in that case
827 * Could be enhanced with a profile link in future?
829 * @author Andreas Gohr <andi@splitbrain.org>
833 function tpl_userinfo() {
835 /** @var Input $INPUT */
838 if($INPUT->server
->str('REMOTE_USER')) {
839 print $lang['loggedinas'].' '.userlink();
846 * Print some info about the current page
848 * @author Andreas Gohr <andi@splitbrain.org>
850 * @param bool $ret return content instead of printing it
851 * @return bool|string
853 function tpl_pageinfo($ret = false) {
859 // return if we are not allowed to view the page
860 if(!auth_quickaclcheck($ID)) {
864 // prepare date and path
865 $fn = $INFO['filepath'];
866 if(!$conf['fullpath']) {
868 $fn = str_replace($conf['olddir'].'/', '', $fn);
870 $fn = str_replace($conf['datadir'].'/', '', $fn);
873 $fn = utf8_decodeFN($fn);
874 $date = dformat($INFO['lastmod']);
877 if($INFO['exists']) {
879 $out .= '<bdi>'.$fn.'</bdi>';
881 $out .= $lang['lastmod'];
884 if($INFO['editor']) {
885 $out .= ' '.$lang['by'].' ';
886 $out .= '<bdi>'.editorinfo($INFO['editor']).'</bdi>';
888 $out .= ' ('.$lang['external_edit'].')';
890 if($INFO['locked']) {
892 $out .= $lang['lockedby'];
894 $out .= '<bdi>'.editorinfo($INFO['locked']).'</bdi>';
907 * Prints or returns the name of the given page (current one if none given).
909 * If useheading is enabled this will use the first headline else
910 * the given ID is used.
912 * @author Andreas Gohr <andi@splitbrain.org>
914 * @param string $id page id
915 * @param bool $ret return content instead of printing
916 * @return bool|string
918 function tpl_pagetitle($id = null, $ret = false) {
919 global $ACT, $INPUT, $conf, $lang;
927 if(useHeading('navigation')) {
928 $first_heading = p_get_first_heading($id);
929 if($first_heading) $name = $first_heading;
932 // default page title is the page name, modify with the current action
936 $page_title = $lang['btn_admin'];
937 // try to get the plugin name
938 /** @var $plugin AdminPlugin */
939 if ($plugin = plugin_getRequestAdminPlugin()){
940 $plugin_title = $plugin->getMenuText($conf['lang']);
941 $page_title = $plugin_title ?
$plugin_title : $plugin->getPluginName();
950 $page_title = $lang['btn_'.$ACT];
956 $page_title = $lang['btn_'.$ACT];
962 $page_title = "✎ ".$name;
966 $page_title = $name . ' - ' . $lang['btn_revs'];
972 $page_title = $name . ' - ' . $lang['btn_'.$ACT];
975 default : // SHOW and anything else not included
980 return hsc($page_title);
982 print hsc($page_title);
988 * Returns the requested EXIF/IPTC tag from the current image
990 * If $tags is an array all given tags are tried until a
991 * value is found. If no value is found $alt is returned.
993 * Which texts are known is defined in the functions _exifTagNames
994 * and _iptcTagNames() in inc/jpeg.php (You need to prepend IPTC
995 * to the names of the latter one)
997 * Only allowed in: detail.php
999 * @author Andreas Gohr <andi@splitbrain.org>
1001 * @param array|string $tags tag or array of tags to try
1002 * @param string $alt alternative output if no data was found
1003 * @param null|string $src the image src, uses global $SRC if not given
1006 function tpl_img_getTag($tags, $alt = '', $src = null) {
1010 if(is_null($src)) $src = $SRC;
1012 static $meta = null;
1013 if(is_null($meta)) $meta = new JpegMeta($src);
1014 if($meta === false) return $alt;
1015 $info = cleanText($meta->getField($tags));
1016 if($info == false) return $alt;
1021 * Returns a description list of the metatags of the current image
1023 * @return string html of description list
1025 function tpl_img_meta() {
1028 $tags = tpl_get_img_meta();
1031 foreach($tags as $tag) {
1032 $label = $lang[$tag['langkey']];
1033 if(!$label) $label = $tag['langkey'] . ':';
1035 echo '<dt>'.$label.'</dt><dd>';
1036 if ($tag['type'] == 'date') {
1037 echo dformat($tag['value']);
1039 echo hsc($tag['value']);
1047 * Returns metadata as configured in mediameta config file, ready for creating html
1049 * @return array with arrays containing the entries:
1050 * - string langkey key to lookup in the $lang var, if not found printed as is
1051 * - string type type of value
1052 * - string value tag value (unescaped)
1054 function tpl_get_img_meta() {
1056 $config_files = getConfigFiles('mediameta');
1057 foreach ($config_files as $config_file) {
1058 if(file_exists($config_file)) {
1059 include($config_file);
1062 /** @var array $fields the included array with metadata */
1065 foreach($fields as $tag){
1067 if (!empty($tag[0])) {
1068 $t = array($tag[0]);
1070 if(is_array($tag[3])) {
1071 $t = array_merge($t,$tag[3]);
1073 $value = tpl_img_getTag($t);
1075 $tags[] = array('langkey' => $tag[1], 'type' => $tag[2], 'value' => $value);
1082 * Prints the image with a link to the full sized version
1084 * Only allowed in: detail.php
1086 * @triggers TPL_IMG_DISPLAY
1087 * @param $maxwidth int - maximal width of the image
1088 * @param $maxheight int - maximal height of the image
1089 * @param $link bool - link to the orginal size?
1090 * @param $params array - additional image attributes
1091 * @return bool Result of TPL_IMG_DISPLAY
1093 function tpl_img($maxwidth = 0, $maxheight = 0, $link = true, $params = null) {
1095 /** @var Input $INPUT */
1098 $w = (int) tpl_img_getTag('File.Width');
1099 $h = (int) tpl_img_getTag('File.Height');
1101 //resize to given max values
1104 if($maxwidth && $w >= $maxwidth) {
1105 $ratio = $maxwidth / $w;
1106 } elseif($maxheight && $h > $maxheight) {
1107 $ratio = $maxheight / $h;
1110 if($maxheight && $h >= $maxheight) {
1111 $ratio = $maxheight / $h;
1112 } elseif($maxwidth && $w > $maxwidth) {
1113 $ratio = $maxwidth / $w;
1117 $w = floor($ratio * $w);
1118 $h = floor($ratio * $h);
1122 $url = ml($IMG, array('cache'=> $INPUT->str('cache'),'rev'=>$REV), true, '&');
1123 $src = ml($IMG, array('cache'=> $INPUT->str('cache'),'rev'=>$REV, 'w'=> $w, 'h'=> $h), true, '&');
1125 //prepare attributes
1126 $alt = tpl_img_getTag('Simple.Title');
1127 if(is_null($params)) {
1132 if($w) $p['width'] = $w;
1133 if($h) $p['height'] = $h;
1134 $p['class'] = 'img_detail';
1143 $data = array('url'=> ($link ?
$url : null), 'params'=> $p);
1144 return Event
::createAndTrigger('TPL_IMG_DISPLAY', $data, '_tpl_img_action', true);
1148 * Default action for TPL_IMG_DISPLAY
1150 * @param array $data
1153 function _tpl_img_action($data) {
1155 $p = buildAttributes($data['params']);
1157 if($data['url']) print '<a href="'.hsc($data['url']).'" title="'.$lang['mediaview'].'">';
1158 print '<img '.$p.'/>';
1159 if($data['url']) print '</a>';
1164 * This function inserts a small gif which in reality is the indexer function.
1166 * Should be called somewhere at the very end of the main.php
1171 function tpl_indexerWebBug() {
1175 $p['src'] = DOKU_BASE
.'lib/exe/taskrunner.php?id='.rawurlencode($ID).
1177 $p['width'] = 2; //no more 1x1 px image because we live in times of ad blockers...
1180 $att = buildAttributes($p);
1181 print "<img $att />";
1188 * use this function to access template configuration variables
1190 * @param string $id name of the value to access
1191 * @param mixed $notset what to return if the setting is not available
1194 function tpl_getConf($id, $notset=false) {
1196 static $tpl_configloaded = false;
1198 $tpl = $conf['template'];
1200 if(!$tpl_configloaded) {
1201 $tconf = tpl_loadConfig();
1202 if($tconf !== false) {
1203 foreach($tconf as $key => $value) {
1204 if(isset($conf['tpl'][$tpl][$key])) continue;
1205 $conf['tpl'][$tpl][$key] = $value;
1207 $tpl_configloaded = true;
1211 if(isset($conf['tpl'][$tpl][$id])){
1212 return $conf['tpl'][$tpl][$id];
1221 * reads all template configuration variables
1222 * this function is automatically called by tpl_getConf()
1226 function tpl_loadConfig() {
1228 $file = tpl_incdir().'/conf/default.php';
1231 if(!file_exists($file)) return false;
1233 // load default config file
1243 * use this function to access template language variables
1245 * @param string $id key of language string
1248 function tpl_getLang($id) {
1249 static $lang = array();
1251 if(count($lang) === 0) {
1252 global $conf, $config_cascade; // definitely don't invoke "global $lang"
1254 $path = tpl_incdir() . 'lang/';
1258 // don't include once
1259 @include
($path . 'en/lang.php');
1260 foreach($config_cascade['lang']['template'] as $config_file) {
1261 if(file_exists($config_file . $conf['template'] . '/en/lang.php')) {
1262 include($config_file . $conf['template'] . '/en/lang.php');
1266 if($conf['lang'] != 'en') {
1267 @include
($path . $conf['lang'] . '/lang.php');
1268 foreach($config_cascade['lang']['template'] as $config_file) {
1269 if(file_exists($config_file . $conf['template'] . '/' . $conf['lang'] . '/lang.php')) {
1270 include($config_file . $conf['template'] . '/' . $conf['lang'] . '/lang.php');
1279 * Retrieve a language dependent file and pass to xhtml renderer for display
1280 * template equivalent of p_locale_xhtml()
1282 * @param string $id id of language dependent wiki page
1283 * @return string parsed contents of the wiki page in xhtml format
1285 function tpl_locale_xhtml($id) {
1286 return p_cached_output(tpl_localeFN($id));
1290 * Prepends appropriate path for a language dependent filename
1292 * @param string $id id of localized text
1293 * @return string wiki text
1295 function tpl_localeFN($id) {
1296 $path = tpl_incdir().'lang/';
1298 $file = DOKU_CONF
.'template_lang/'.$conf['template'].'/'.$conf['lang'].'/'.$id.'.txt';
1299 if (!file_exists($file)){
1300 $file = $path.$conf['lang'].'/'.$id.'.txt';
1301 if(!file_exists($file)){
1302 //fall back to english
1303 $file = $path.'en/'.$id.'.txt';
1310 * prints the "main content" in the mediamanager popup
1312 * Depending on the user's actions this may be a list of
1313 * files in a namespace, the meta editing dialog or
1314 * a message of referencing pages
1316 * Only allowed in mediamanager.php
1318 * @triggers MEDIAMANAGER_CONTENT_OUTPUT
1319 * @param bool $fromajax - set true when calling this function via ajax
1320 * @param string $sort
1322 * @author Andreas Gohr <andi@splitbrain.org>
1324 function tpl_mediaContent($fromajax = false, $sort='natural') {
1330 /** @var Input $INPUT */
1333 $do = $INPUT->extract('do')->str('do');
1334 if(in_array($do, array('save', 'cancel'))) $do = '';
1337 if($INPUT->bool('edit')) {
1339 } elseif(is_array($INUSE)) {
1346 // output the content pane, wrapped in an event.
1347 if(!$fromajax) ptln('<div id="media__content">');
1348 $data = array('do' => $do);
1349 $evt = new Event('MEDIAMANAGER_CONTENT_OUTPUT', $data);
1350 if($evt->advise_before()) {
1352 if($do == 'filesinuse') {
1353 media_filesinuse($INUSE, $IMG);
1354 } elseif($do == 'filelist') {
1355 media_filelist($NS, $AUTH, $JUMPTO,false,$sort);
1356 } elseif($do == 'searchlist') {
1357 media_searchlist($INPUT->str('q'), $NS, $AUTH);
1359 msg('Unknown action '.hsc($do), -1);
1362 $evt->advise_after();
1364 if(!$fromajax) ptln('</div>');
1369 * Prints the central column in full-screen media manager
1370 * Depending on the opened tab this may be a list of
1371 * files in a namespace, upload form or search form
1373 * @author Kate Arzamastseva <pshns@ukr.net>
1375 function tpl_mediaFileList() {
1380 /** @var Input $INPUT */
1383 $opened_tab = $INPUT->str('tab_files');
1384 if(!$opened_tab ||
!in_array($opened_tab, array('files', 'upload', 'search'))) $opened_tab = 'files';
1385 if($INPUT->str('mediado') == 'update') $opened_tab = 'upload';
1387 echo '<h2 class="a11y">'.$lang['mediaselect'].'</h2>'.NL
;
1389 media_tabs_files($opened_tab);
1391 echo '<div class="panelHeader">'.NL
;
1393 $tabTitle = ($NS) ?
$NS : '['.$lang['mediaroot'].']';
1394 printf($lang['media_'.$opened_tab], '<strong>'.hsc($tabTitle).'</strong>');
1396 if($opened_tab === 'search' ||
$opened_tab === 'files') {
1397 media_tab_files_options();
1401 echo '<div class="panelContent">'.NL
;
1402 if($opened_tab == 'files') {
1403 media_tab_files($NS, $AUTH, $JUMPTO);
1404 } elseif($opened_tab == 'upload') {
1405 media_tab_upload($NS, $AUTH, $JUMPTO);
1406 } elseif($opened_tab == 'search') {
1407 media_tab_search($NS, $AUTH);
1413 * Prints the third column in full-screen media manager
1414 * Depending on the opened tab this may be details of the
1415 * selected file, the meta editing dialog or
1416 * list of file revisions
1418 * @author Kate Arzamastseva <pshns@ukr.net>
1420 * @param string $image
1421 * @param boolean $rev
1423 function tpl_mediaFileDetails($image, $rev) {
1424 global $conf, $DEL, $lang;
1425 /** @var Input $INPUT */
1429 !file_exists(mediaFN($image)) &&
1430 file_exists(mediaMetaFN($image, '.changes')) &&
1431 $conf['mediarevisions']
1433 if(!$image ||
(!file_exists(mediaFN($image)) && !$removed) ||
$DEL) return;
1434 if($rev && !file_exists(mediaFN($image, $rev))) $rev = false;
1435 $ns = getNS($image);
1436 $do = $INPUT->str('mediado');
1438 $opened_tab = $INPUT->str('tab_details');
1440 $tab_array = array('view');
1441 list(, $mime) = mimetype($image);
1442 if($mime == 'image/jpeg') {
1443 $tab_array[] = 'edit';
1445 if($conf['mediarevisions']) {
1446 $tab_array[] = 'history';
1449 if(!$opened_tab ||
!in_array($opened_tab, $tab_array)) $opened_tab = 'view';
1450 if($INPUT->bool('edit')) $opened_tab = 'edit';
1451 if($do == 'restore') $opened_tab = 'view';
1453 media_tabs_details($image, $opened_tab);
1455 echo '<div class="panelHeader"><h3>';
1456 list($ext) = mimetype($image, false);
1457 $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
1458 $class = 'select mediafile mf_'.$class;
1459 $attributes = $rev ?
['rev' => $rev] : [];
1460 $tabTitle = '<strong><a href="'.ml($image, $attributes).'" class="'.$class.'" title="'.$lang['mediaview'].'">'.
1461 $image.'</a>'.'</strong>';
1462 if($opened_tab === 'view' && $rev) {
1463 printf($lang['media_viewold'], $tabTitle, dformat($rev));
1465 printf($lang['media_'.$opened_tab], $tabTitle);
1468 echo '</h3></div>'.NL
;
1470 echo '<div class="panelContent">'.NL
;
1472 if($opened_tab == 'view') {
1473 media_tab_view($image, $ns, null, $rev);
1475 } elseif($opened_tab == 'edit' && !$removed) {
1476 media_tab_edit($image, $ns);
1478 } elseif($opened_tab == 'history' && $conf['mediarevisions']) {
1479 media_tab_history($image, $ns);
1486 * prints the namespace tree in the mediamanager popup
1488 * Only allowed in mediamanager.php
1490 * @author Andreas Gohr <andi@splitbrain.org>
1492 function tpl_mediaTree() {
1494 ptln('<div id="media__tree">');
1500 * Print a dropdown menu with all DokuWiki actions
1502 * Note: this will not use any pretty URLs
1504 * @author Andreas Gohr <andi@splitbrain.org>
1506 * @param string $empty empty option label
1507 * @param string $button submit button label
1508 * @deprecated 2017-09-01 see devel:menus
1510 function tpl_actiondropdown($empty = '', $button = '>') {
1511 dbg_deprecated('see devel:menus');
1512 $menu = new \dokuwiki\Menu\
MobileMenu();
1513 echo $menu->getDropdown($empty, $button);
1517 * Print a informational line about the used license
1519 * @author Andreas Gohr <andi@splitbrain.org>
1520 * @param string $img print image? (|button|badge)
1521 * @param bool $imgonly skip the textual description?
1522 * @param bool $return when true don't print, but return HTML
1523 * @param bool $wrap wrap in div with class="license"?
1526 function tpl_license($img = 'badge', $imgonly = false, $return = false, $wrap = true) {
1530 if(!$conf['license']) return '';
1531 if(!is_array($license[$conf['license']])) return '';
1532 $lic = $license[$conf['license']];
1533 $target = ($conf['target']['extern']) ?
' target="'.$conf['target']['extern'].'"' : '';
1536 if($wrap) $out .= '<div class="license">';
1538 $src = license_img($img);
1540 $out .= '<a href="'.$lic['url'].'" rel="license"'.$target;
1541 $out .= '><img src="'.DOKU_BASE
.$src.'" alt="'.$lic['name'].'" /></a>';
1542 if(!$imgonly) $out .= ' ';
1546 $out .= $lang['license'].' ';
1547 $out .= '<bdi><a href="'.$lic['url'].'" rel="license" class="urlextern"'.$target;
1548 $out .= '>'.$lic['name'].'</a></bdi>';
1550 if($wrap) $out .= '</div>';
1552 if($return) return $out;
1558 * Includes the rendered HTML of a given page
1560 * This function is useful to populate sidebars or similar features in a
1563 * @param string $pageid The page name you want to include
1564 * @param bool $print Should the content be printed or returned only
1565 * @param bool $propagate Search higher namespaces, too?
1566 * @param bool $useacl Include the page only if the ACLs check out?
1567 * @return bool|null|string
1569 function tpl_include_page($pageid, $print = true, $propagate = false, $useacl = true) {
1571 $pageid = page_findnearest($pageid, $useacl);
1572 } elseif($useacl && auth_quickaclcheck($pageid) == AUTH_NONE
) {
1575 if(!$pageid) return false;
1579 $html = p_wiki_xhtml($pageid, '', false);
1582 if($print) echo $html;
1587 * Display the subscribe form
1589 * @author Adrian Lang <lang@cosmocode.de>
1590 * @deprecated 2020-07-23
1592 function tpl_subscribe() {
1593 dbg_deprecated(\dokuwiki\Ui\Subscribe
::class .'::show()');
1594 (new \dokuwiki\Ui\Subscribe
)->show();
1598 * Tries to send already created content right to the browser
1600 * Wraps around ob_flush() and flush()
1602 * @author Andreas Gohr <andi@splitbrain.org>
1604 function tpl_flush() {
1605 if( ob_get_level() > 0 ) ob_flush();
1610 * Tries to find a ressource file in the given locations.
1612 * If a given location starts with a colon it is assumed to be a media
1613 * file, otherwise it is assumed to be relative to the current template
1615 * @param string[] $search locations to look at
1616 * @param bool $abs if to use absolute URL
1617 * @param array &$imginfo filled with getimagesize()
1618 * @param bool $fallback use fallback image if target isn't found or return 'false' if potential
1619 * false result is required
1622 * @author Andreas Gohr <andi@splitbrain.org>
1624 function tpl_getMediaFile($search, $abs = false, &$imginfo = null, $fallback = true) {
1628 // loop through candidates until a match was found:
1629 foreach($search as $img) {
1630 if(substr($img, 0, 1) == ':') {
1631 $file = mediaFN($img);
1634 $file = tpl_incdir().$img;
1638 if(file_exists($file)) break;
1641 // manage non existing target
1642 if (!file_exists($file)) {
1643 // give result for fallback image
1644 if ($fallback === true) {
1645 $file = DOKU_INC
. 'lib/images/blank.gif';
1646 // stop process if false result is required (if $fallback is false)
1652 // fetch image data if requested
1653 if(!is_null($imginfo)) {
1654 $imginfo = getimagesize($file);
1659 $url = ml($img, '', true, '', $abs);
1661 $url = tpl_basedir().$img;
1662 if($abs) $url = DOKU_URL
.substr($url, strlen(DOKU_REL
));
1669 * PHP include a file
1671 * either from the conf directory if it exists, otherwise use
1672 * file in the template's root directory.
1674 * The function honours config cascade settings and looks for the given
1675 * file next to the ´main´ config files, in the order protected, local,
1678 * Note: no escaping or sanity checking is done here. Never pass user input
1681 * @author Anika Henke <anika@selfthinker.org>
1682 * @author Andreas Gohr <andi@splitbrain.org>
1684 * @param string $file
1686 function tpl_includeFile($file) {
1687 global $config_cascade;
1688 foreach(array('protected', 'local', 'default') as $config_group) {
1689 if(empty($config_cascade['main'][$config_group])) continue;
1690 foreach($config_cascade['main'][$config_group] as $conf_file) {
1691 $dir = dirname($conf_file);
1692 if(file_exists("$dir/$file")) {
1693 include("$dir/$file");
1699 // still here? try the template dir
1700 $file = tpl_incdir().$file;
1701 if(file_exists($file)) {
1707 * Returns <link> tag for various icon types (favicon|mobile|generic)
1709 * @author Anika Henke <anika@selfthinker.org>
1711 * @param array $types - list of icon types to display (favicon|mobile|generic)
1714 function tpl_favicon($types = array('favicon')) {
1718 foreach($types as $type) {
1721 $look = array(':wiki:favicon.ico', ':favicon.ico', 'images/favicon.ico');
1722 $return .= '<link rel="shortcut icon" href="'.tpl_getMediaFile($look).'" />'.NL
;
1725 $look = array(':wiki:apple-touch-icon.png', ':apple-touch-icon.png', 'images/apple-touch-icon.png');
1726 $return .= '<link rel="apple-touch-icon" href="'.tpl_getMediaFile($look).'" />'.NL
;
1729 // ideal world solution, which doesn't work in any browser yet
1730 $look = array(':wiki:favicon.svg', ':favicon.svg', 'images/favicon.svg');
1731 $return .= '<link rel="icon" href="'.tpl_getMediaFile($look).'" type="image/svg+xml" />'.NL
;
1740 * Prints full-screen media manager
1742 * @author Kate Arzamastseva <pshns@ukr.net>
1744 function tpl_media() {
1745 global $NS, $IMG, $JUMPTO, $REV, $lang, $fullscreen, $INPUT;
1747 require_once DOKU_INC
.'lib/exe/mediamanager.php';
1750 $image = cleanID($INPUT->str('image'));
1751 if(isset($IMG)) $image = $IMG;
1752 if(isset($JUMPTO)) $image = $JUMPTO;
1753 if(isset($REV) && !$JUMPTO) $rev = $REV;
1755 echo '<div id="mediamanager__page">'.NL
;
1756 echo '<h1>'.$lang['btn_media'].'</h1>'.NL
;
1759 echo '<div class="panel namespaces">'.NL
;
1760 echo '<h2>'.$lang['namespaces'].'</h2>'.NL
;
1761 echo '<div class="panelHeader">';
1762 echo $lang['media_namespaces'];
1765 echo '<div class="panelContent" id="media__tree">'.NL
;
1770 echo '<div class="panel filelist">'.NL
;
1771 tpl_mediaFileList();
1774 echo '<div class="panel file">'.NL
;
1775 echo '<h2 class="a11y">'.$lang['media_file'].'</h2>'.NL
;
1776 tpl_mediaFileDetails($image, $rev);
1783 * Return useful layout classes
1785 * @author Anika Henke <anika@selfthinker.org>
1789 function tpl_classes() {
1790 global $ACT, $conf, $ID, $INFO;
1791 /** @var Input $INPUT */
1797 'tpl_'.$conf['template'],
1798 $INPUT->server
->bool('REMOTE_USER') ?
'loggedIn' : '',
1799 (isset($INFO) && $INFO['exists']) ?
'' : 'notFound',
1800 ($ID == $conf['start']) ?
'home' : '',
1802 return join(' ', $classes);
1806 * Create event for tools menues
1808 * @author Anika Henke <anika@selfthinker.org>
1809 * @param string $toolsname name of menu
1810 * @param array $items
1811 * @param string $view e.g. 'main', 'detail', ...
1812 * @deprecated 2017-09-01 see devel:menus
1814 function tpl_toolsevent($toolsname, $items, $view = 'main') {
1815 dbg_deprecated('see devel:menus');
1821 $hook = 'TEMPLATE_' . strtoupper($toolsname) . '_DISPLAY';
1822 $evt = new Event($hook, $data);
1823 if($evt->advise_before()) {
1824 foreach($evt->data
['items'] as $k => $html) echo $html;
1826 $evt->advise_after();
1829 //Setup VIM: ex: et ts=4 :