1<?php
2/**
3 * User: z9764
4 * Date: 2015/3/8
5 * Time: 2:29
6 */
7if (!defined('DOKU_INC')) die();
8if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
9require_once (DOKU_PLUGIN . 'action.php');
10require_once (DOKU_INC . 'inc/html.php');
11require_once (DOKU_INC . 'inc/parserutils.php');
12require_once(DOKU_INC.'inc/search.php');
13
14class  action_plugin_ajaxpeon extends DokuWiki_Action_Plugin{
15
16    var $helper;
17
18    function action_plugin_ajaxpeon(){
19        $this->helper = $this->loadHelper('ajaxpeon', false);
20    }
21
22    function register( Doku_Event_Handler $controller) {
23        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE',  $this, '_ajax_call');
24    }
25
26    /**
27     * handle ajax requests
28     */
29    function _ajax_call(&$event, $param)
30    {
31        if ($event->data !== 'ajaxpeon') {
32            return;
33        }
34        //no other ajax call handlers needed
35        $event->stopPropagation();
36        $event->preventDefault();
37
38        //e.g. access additional request variables
39        global $INPUT; //available since release 2012-10-13 "Adora Belle"
40
41        $pageid =$INPUT->str('pageid');
42
43        $target = $INPUT->str("target");
44
45        $reflect = $INPUT->str("reflect");
46
47        $data = array();
48        $out="";
49        if($target=="page"){
50            $out=$this->get_page($pageid);
51        }
52        if($target=="toc"){
53            $out=$this->get_toc($pageid);
54        }
55        if($target=="rawpage"){
56            if($INPUT->str('rev')=='ori'){
57                $orev_list= $this->helper->get_learnorev();
58                $out = rawWiki($pageid,$orev_list[$pageid]);
59            }else {
60                $out = rawWiki($pageid);
61            }
62        }
63        if($target=="writeraw"){
64            $ori_txt = rawWiki($pageid);
65            $ori_len = strlen($ori_txt);
66            $subtarget = $INPUT->str('sub');
67            $rec_txt = $INPUT->str('txt');
68            $store_txt=null;
69            switch($subtarget){
70                case "add":
71                    $store_txt=$ori_txt.$rec_txt;
72                    break;
73                case "wordlist":
74                    $store_txt = $this->helper->merge_wordlist($ori_txt,$rec_txt);
75                    break;
76                case "write":
77                    $store_txt = $rec_txt;
78                    break;
79            }
80            if($store_txt!=null){
81                saveWikiText($pageid,$store_txt,"L".strlen($store_txt));
82                $out="origin txt len:".$ori_len.",you add:".(strlen($store_txt)-$ori_len);
83            }else{
84                $out="do nothing,please ensure set the mdata['sub']";
85            }
86        }
87        if($target=="catalog"){
88            $ns=$INPUT->str('ns');
89            if($ns==null){
90                $ns="";
91            }
92            $out = $this->get_catalog($ns);
93        }
94        if($target=="page_wordlists"){
95            $wdstr=$INPUT->str('pglist');
96            $pglist=json_decode($wdstr,true);
97            $out=$this->helper->get_page_wordlists($pglist);
98        }
99        if($target=="learnlist"){
100            $out=$this->helper->get_learnlist();
101        }
102        if($target=="user"){
103            $out=$_SERVER['REMOTE_USER'];
104            if($out==null){
105                $data["inf"]="not login";
106            }
107
108        }
109
110
111
112        $data["content"]=$out;
113        if($reflect!=null) {
114            $data["reflect"] = $reflect;
115        }
116        //json library of DokuWiki
117        require_once DOKU_INC . 'inc/JSON.php';
118        $json = new JSON();
119        //set content type
120        header('Content-Type: application/json');
121        if($_GET["callback"]){
122            echo $_GET["callback"]."(".$json->encode($data).")";
123        }else {
124            echo $json->encode($data);
125        }
126    }
127
128
129
130
131
132
133    function get_catalog($ns){
134        $data=array();
135        global $conf;
136        $ns = utf8_encodeFN(str_replace(':', '/', $ns));
137        search($data,$conf['datadir'], 'search_allpages', array(),$ns);
138
139        $out=array();
140        foreach($data as $ff){
141            $out[]=$ff["id"];
142        }
143        return $out;
144    }
145
146
147
148
149
150
151    function get_toc22($pageid){
152        global $ID;
153        global $TOC;
154        $ID=$pageid;
155        $oldtoc = $TOC;
156        $html   = p_wiki_xhtml($pageid, '', false);
157        $outtoc=tpl_toc(true);
158        $TOC    = $oldtoc;
159        return $outtoc;
160    }
161
162    function get_toc($pageid){
163        global $conf;
164        $meta = p_get_metadata($pageid, false, METADATA_RENDER_USING_CACHE);
165        if(isset($meta['internal']['toc'])) {
166            $tocok = $meta['internal']['toc'];
167        } else {
168            $tocok = true;
169        }
170        $toc = isset($meta['description']['tableofcontents']) ? $meta['description']['tableofcontents'] : null;
171        if(!$tocok || !is_array($toc) || !$conf['tocminheads'] || count($toc) < $conf['tocminheads']) {
172            $toc = array();
173        }
174
175        trigger_event('TPL_TOC_RENDER', $toc, null, false);
176        $html = html_TOC($toc);
177        return $html;
178
179    }
180
181    function get_page($pageid){
182        return tpl_include_page($pageid,false);
183    }
184
185    function myrecord(){
186  //      saveWikiText("zh:fftest",$origin."\\\\ \n".$name,"fftest"); //this is save to zh/fftest.txt
187 //          $origin = rawWiki("zh:fftest");    // read from         zh/fftest.txt
188        //tpl_content
189
190        //data
191        $data = array("avg1" => "you are success");
192        //json library of DokuWiki
193        require_once DOKU_INC . 'inc/JSON.php';
194        $json = new JSON();
195        //set content type
196        header('Content-Type: application/json');
197        echo $json->encode($data);
198    }
199
200    function tpl_include_page($pageid, $print = true, $propagate = false) {
201        if (!$pageid) return false;
202        if ($propagate) $pageid = page_findnearest($pageid);
203
204        global $TOC;
205        $oldtoc = $TOC;
206        $html   = p_wiki_xhtml($pageid, '', false);
207        $TOC    = $oldtoc;
208
209        if(!$print) return $html;
210        echo $html;
211        return $html;
212    }
213
214    function html_TOC($toc){
215        if(!count($toc)) return '';
216        global $lang;
217        $out  = '<!-- TOC START -->'.DOKU_LF;
218        $out .= '<div id="dw__toc">'.DOKU_LF;
219        $out .= '<h3 class="toggle">';
220        $out .= $lang['toc'];
221        $out .= '</h3>'.DOKU_LF;
222        $out .= '<div>'.DOKU_LF;
223        $out .= html_buildlist($toc,'toc','html_list_toc','html_li_default',true);
224        $out .= '</div>'.DOKU_LF.'</div>'.DOKU_LF;
225        $out .= '<!-- TOC END -->'.DOKU_LF;
226        return $out;
227    }
228
229}