1<?php
2/**
3 */
4
5// must be run within Dokuwiki
6if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
7if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
8require_once(DOKU_INC.'inc/fulltext.php');
9require_once(DOKU_INC.'inc/pageutils.php');
10require_once(DOKU_INC.'inc/common.php');
11
12
13class action_plugin_dyncontent extends DokuWiki_Action_Plugin {
14
15    public function register(Doku_Event_Handler $controller) {
16
17       $controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'insert_button', array());
18       $controller->register_hook('PARSER_WIKITEXT_PREPROCESS', 'AFTER', $this, 'handle_parser_wikitext_preprocess');
19
20    }
21
22	 /**
23     * Add list with buttons to toolbar
24     *
25     * @param Doku_Event $event
26     * @param            $param
27     */
28    public function insert_button(Doku_Event $event, $param) {
29
30		$button = array();
31		$ico = '../../plugins/dyncontent/toolbar_ico.png';
32
33        $button[] =
34				array(
35                'type'   => 'linkwizv2',
36                'title'  => 'Dynamic Content',
37                'icon'   => $ico,
38				'open' => '<dyn ',
39				'close' => '</dyn>',
40                'block'  => FALSE
41                );
42
43		$event->data = array_merge($event->data, $button);
44
45
46    }
47
48
49    public function handle_parser_wikitext_preprocess(Doku_Event &$event, $param) {
50        global $INFO;
51        if ($INFO['id'] != '') return;
52        $inf = pageinfo();
53		$pagename = $inf["id"];
54
55		/*
56        $inf['namespace'] = urlencode(str_replace(array(' ', '%', '&'), '_', $inf['namespace']));
57        $ns = str_replace(':', '/', $inf['namespace']) . '/';
58        $base = str_replace('\\', '/', DOKU_INC) . 'data/pages/' . $ns; // 得到文件绝对路径
59		*/
60
61		$squery = $pagename.'*|'.$pagename.'">"';
62		$search = $this->get_search_results($squery);
63
64		if(count($search)){
65            foreach($search as $pagid) {
66				$dynamic_content .= "\n\n\n\n".$this->get_page_dynamic_content($pagid, $pagename); //the content of entire
67			}
68		}
69
70        $event->data .= $dynamic_content;
71
72    }
73
74	function get_search_results($pagename){
75
76		$search = ft_pageSearch($pagename,$poswords);
77		$search = array_keys($search);
78		$search = array_unique($search);
79		return $search;
80
81    }
82
83	function get_page_dynamic_content($page_id, $pagename){
84		$content = rawWiki($page_id);
85
86		//https://www.phpliveregex.com/#tab-preg-match-all
87		/*
88		$regex = '/(?<=<dyn ).*?(?=<\/b>)/';
89        preg_match_all($regex, $data, $matched);
90		*/
91		$regex = '/<dyn \s*?.*?'.$pagename.'\s*?.*?>(.*?|[\s\S]*?)<\/dyn>/';
92		preg_match_all($regex,$content,$out);
93
94/*
95		foreach($out as $match){
96			echo json_encode($match);
97			echo "<br/>";
98			if (!empty($match[0]))
99				echo "m0:".$match[0];
100			if (!empty($match[1]))
101				echo "m1:".$match[1];
102			echo "<br/>";
103
104		}
105		echo "fine page: ".$page_id."<br />";
106		*/
107
108		foreach($out[1] as $match)
109			if (!empty($match))
110				$output .= '[['.$page_id.'|→]] '.$match;
111
112		return $output;
113	}
114	//'<span style=\"background-color: azure; padding: 2px 0px 2px 0px;\">'.
115
116}
117