1<?php
2/**
3 * Helper Component for the xxx Plugin
4 *
5 */
6
7// must be run within Dokuwiki
8if(!defined('DOKU_INC')) die();
9require_once (DOKU_INC . 'inc/parserutils.php');
10class helper_plugin_ajaxpeon extends DokuWiki_Plugin
11{
12    function make_searchbox(){
13        $out="";
14
15        $out=$out.'<div class="xxtest">';
16        $out=$out.'<input id="xxpageid" type="text"/>';
17
18        $out=$out.'<input id="xxckpage"  name="Page" type="checkbox"/><label for="xxckpage">Page</label> ';
19        $out=$out.'<input id="xxcktoc" name="Toc" type="checkbox"/><label for="xxcktoc">Toc</label>';
20        $out=$out.'<input id="xxckvoice" name="Voice" type="checkbox"/><label for="xxckvoice">Voice</label>';
21
22        $out=$out.'<input id="xxsearch" title="Search" type="button" value="Search"/>';
23        $out=$out.'<div class="xxresult" id="xxdirectrt"></div>';
24        $out=$out.'</div>';
25        echo $out;
26        return $out;
27    }
28
29    function make_learn_cata(){
30        global $conf;
31        $metadir = $conf["metadir"];
32        $learndir = $metadir."/learn/";
33        $filelist = scandir($learndir);
34        $learnlist = array();
35        foreach($filelist as $fname){
36            if(strpos($fname,".changes")){
37                $tmphf = fopen($learndir.$fname,"r");
38                $tmpstr= fgets($tmphf);
39                $namelen =strlen($fname);
40                $rawnlen = $namelen-8;
41                if(strlen($tmpstr)>10){
42                    $learnlist[]=substr($tmpstr,0,10)."\t".substr($fname,0,$rawnlen);
43                }
44            }
45        }
46        $learnlstr = join("\n",$learnlist);
47        $outfile = fopen($metadir."/learn.list","w");
48        fwrite($outfile,$learnlstr);
49        fclose($outfile);
50    }
51
52    function get_learn_list(){
53
54    }
55
56    function get_learn_words($learn_list){
57
58    }
59
60
61    function get_file_list($dir){
62        $file_array=array();
63        $file_array["files"]=array();
64        $filelist = scandir($dir);
65
66        $predir= getcwd();
67        chdir($dir);
68        foreach($filelist as $fname){
69            if(is_dir($fname)){
70                if($fname!="." and $fname!=".."){
71                    $file_array["dir"][$fname]=$this->get_file_list($fname);
72                }
73            }else{
74                if(is_file($fname)) {
75                    $file_array["files"][] = substr($fname,0,strpos($fname,"."));
76                }
77            }
78        }
79        chdir($predir);
80        return $file_array;
81    }
82
83
84    function get_changes_list($file_list,$dirnm,$changes_dir,&$out_filedata,&$out_fileorev){
85        $changes_ar=array();
86        $changes_ar["files"]=array();
87
88        if(isset($file_list["dir"])){
89            foreach($file_list["dir"] as $dirname=>$dirvalue){
90                $changes_ar["ns"][$dirnm.$dirname.":"]=$this->get_changes_list($dirvalue,$dirnm.$dirname.":",$changes_dir,$out_filedata,$out_fileorev);
91            }
92        }
93        if(isset($file_list["files"])){
94            foreach($file_list["files"] as $file){
95                $nsfile = $dirnm.$file;
96                $dirfile = utf8_encodeFN(str_replace(':', '/', $nsfile));
97                $hfile=fopen($changes_dir."/".$dirfile.'.changes',"r");
98                $tmpstr= fgets($hfile);
99                $orev =substr($tmpstr,0,10);
100                fclose($hfile);
101
102                $hwordf = fopen($dirfile.'.txt',"r");
103                $txtstr = fread($hwordf,filesize($dirfile.'.txt'));
104                $rt = preg_match("@<WORDLIST\b(.*?)>(.*?)</WORDLIST>@",$txtstr,$match);
105                $wordlist="";
106                fclose($hwordf);
107                if($rt===1){
108                    $wordlist=$match[2];
109                }
110
111                $nsdata = array("orev"=>$orev,"wordlist"=>$wordlist);
112                $out_filedata[$dirnm.$file]=$nsdata;
113                $out_fileorev[$dirnm.$file]=$orev;
114                $changes_ar["files"][]=$dirnm.$file;
115
116            }
117        }
118        return $changes_ar;
119    }
120
121    function build_learn_list($update_span){
122        global $conf;
123        $metadir = $conf["metadir"];
124        $pagedir = $conf['datadir'];
125        $pretime=0;
126        if(is_readable($metadir."/learn.log"))
127        {
128            $htmp=fopen($metadir."/learn.log","r");
129            $pretime = intval(fgets($htmp));
130            fclose($htmp);
131        }
132        if($pretime){
133            if((time()-$pretime)<$update_span){
134                return;
135            }
136        }
137
138        $predir= getcwd();
139        chdir($pagedir);
140        $learndir_ls = $this->get_file_list("learn");
141        $flat_data=array();
142        $orev_data=array();
143        $learn_ns_ls = $this->get_changes_list($learndir_ls,"learn:",$metadir,$flat_data,$orev_data);
144
145        chdir($predir);
146
147        $flearnlist = fopen($metadir."/learn.list","w");
148        fwrite($flearnlist,json_encode($learn_ns_ls));
149        fclose($flearnlist);
150        $flearndata= fopen($metadir."/learn.flatdata","w");
151        fwrite($flearndata,json_encode($flat_data));
152        fclose($flearndata);
153        $forev= fopen($metadir."/learn.orev","w");
154        fwrite($forev,json_encode($orev_data));
155        fclose($forev);
156        $this->learnlog($metadir."/learn.log");
157//    $flearnlog= fopen($metadir."/learn.data","w+");
158    }
159
160
161    function learnlog($logname){
162        $outlog=null;
163        $pretime=0;
164        if(is_readable($logname)==false){
165            $outlog=fopen($logname,"w");
166        }else {
167            $outlog=fopen($logname,"r+");
168            $pretime = fgets($outlog);
169
170            $orgtime = fgets($outlog);
171
172            if ($orgtime) {
173                if ((time() - intval($orgtime)) > 60 * 60 * 24) {
174                    fclose($outlog);
175                    $outlog = fopen($logname, "w");
176                }
177            }
178        }
179        fseek($outlog,0,SEEK_SET);
180        fwrite($outlog,time()."\n");
181        fseek($outlog,0,SEEK_END);
182        fwrite($outlog,time()."\n");
183        fclose($outlog);
184        return intval($pretime);
185    }
186
187
188    function get_page_wordlists($page_list){
189        $this->build_learn_list(60*10);
190        global $conf;
191        $metadir = $conf["metadir"];
192        $pagedir = $conf['datadir'];
193        $fstr = file_get_contents($metadir."/learn.flatdata");
194        $flat_wlist = json_decode($fstr,true);
195        $data=array();
196        foreach($page_list as $page){
197            $data[$page]=$flat_wlist[$page]["wordlist"];
198        }
199        return $data;
200    }
201
202    function get_learnlist(){
203        $this->build_learn_list(60*10);
204        global $conf;
205        $metadir = $conf["metadir"];
206        $pagedir = $conf['datadir'];
207        $fstr = file_get_contents($metadir."/learn.list");
208        return $fstr;
209    }
210
211    function get_learnorev(){
212        global $conf;
213        $metadir = $conf["metadir"];
214        $filen= $metadir."/learn.orev";
215        $fstr = file_get_contents($filen);
216        $orev_list = json_decode($fstr,true);
217        return $orev_list;
218    }
219
220    function merge_wordlist($w_txt1,$w_txt2){
221        $list1 = explode(",",$w_txt1);
222        $list2 = explode(",",$w_txt2);
223        $list3 = array_merge($list1,$list2);
224        sort($list3);
225        $list_rt = array_unique($list3);
226        return implode(",",$list_rt);
227    }
228    /*
229    function get_wordlistss($page_list){
230        $data=array();
231        foreach($page_list as $page){
232            $data[$page]=$this->get_wordlist($page);
233        }
234        return $data;
235    }
236
237    function get_wordlist($pageid){
238        $filestr=rawWiki($pageid);
239        $rt = preg_match("@<WORDLIST\b(.*?)>(.*?)</WORDLIST>@",$filestr,$match);
240        if($rt!==1){
241            return null;
242        }
243        return $match[2];
244    }
245    */
246
247
248}
249