1<?php
2// This file tells DokuWiki that this plugin has a helper module
3// It's optional in modern DW versions but still recommended
4define('BEGIN_REPLACE_DELIMITER', '@');
5define('END_REPLACE_DELIMITER', '@');
6
7class helper_plugin_yatp extends DokuWiki_Plugin
8{
9    function getTemplateFile($name) {
10        return (substr($name, 0, 1) == ":") || ($this->getConf('namespace') == '') ? substr($name, 1) : $this->getConf('namespace') . ":" . $name;
11    }
12
13    function getTemplate($pageName) {
14		/**
15		 * by default, a page from namespace specified in $conf['namespace'] will be loaded
16		 * To override this, prepend a colon to $name
17		**/
18		$template = rawWiki($pageName);
19        if (!$template) return false;
20		$template = preg_replace('/<noinclude>.*?<\/noinclude>/s', '', $template);
21		$template = preg_replace('/<includeonly>|<\/includeonly>/', '', $template);
22		return $template;
23	}
24
25    /**
26     * Handles the replacement array
27     */
28    function messageReplacers($replacers) {
29        $r = [];
30        if (is_null($replacers)) {
31            $r['keys'] = null;
32            $r['vals'] = null;
33        } else if (is_string($replacers)) {
34            if ( str_contains($replacers, '=') && (substr(trim($replacers), -1) != '=') ){
35                list($k, $v) = explode('=', $replacers, 2);
36                $r['keys'] = BEGIN_REPLACE_DELIMITER.trim($k).END_REPLACE_DELIMITER;
37                $r['vals'] = trim(str_replace('\|', '|', $v));
38            }
39        } else if (is_array($replacers) ) {
40            foreach($replacers as $rep) {
41                if (str_contains($rep, '=') && (substr(trim($rep), -1) != '=') ){
42                    list($k, $v) = explode('=', $rep, 2);
43                    $r['keys'][] = BEGIN_REPLACE_DELIMITER.trim($k).END_REPLACE_DELIMITER;
44                    if (trim($v)[0] == '"' and trim($v)[-1] == '"') {
45                        $r['vals'][] = substr(trim(str_replace('\|','|',$v)), 1, -1);
46                    } else {
47                        $r['vals'][] = trim(str_replace('\|','|',$v));
48                    }
49                }
50            }
51        } else {
52            // This is an assertion failure. We should NEVER get here.
53            //die("FATAL ERROR!  Unknown type passed to syntax_plugin_yatp_template::massageReplaceMentArray() can't message syntax_plugin_yatp_template::\$replacers!  Type is:".gettype($r)." Value is:".$r);
54            $r['keys'] = null;
55            $r['vals'] = null;
56        }
57        return $r;
58    }
59}