1<?php
2/**
3 * Created by PhpStorm.
4 * User: z97
5 * Date: 15-6-3
6 * Time: 下午9:14
7 */
8if(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/../../');
9require_once(DOKU_INC.'inc/init.php');
10require_once(DOKU_INC."inc/RemoteAPICore.php");
11require_once(DOKU_INC."inc/remote.php");
12require_once(DOKU_INC."inc/remoteAPI_l.php");
13require_once (DOKU_INC . 'inc/parserutils.php');
14
15class remote_plugin_pagestat extends DokuWiki_Remote_Plugin {
16    protected $helper;
17    protected $remoteapi;
18    function __construct(){
19        $this->helper=new helper_plugin_pagestat();
20        $this->remoteapi=new remoteAPI_l();
21    }
22
23    public function _getMethods() {
24        return array(
25            'test' => array(
26                'args' => array('string'),
27                'return' => 'string'
28            ),
29            'getUser' => array(
30                'args' => array(),
31                'return' => 'string'
32            ),
33        );
34    }
35
36    function test($msg){
37        return "i am pagestat!! ($msg) .";
38    }
39
40    public function getUser(){
41        global $INPUT;
42        return $INPUT->server->str('REMOTE_USER') ;
43    }
44
45    function rawPage($id,$rev=''){
46        $id = $this->resolvePageId($id);
47        if(auth_quickaclcheck($id) < AUTH_READ){
48            throw new RemoteAccessDeniedException('You are not allowed to read this file', 111);
49        }
50        $text = rawWiki($id,$rev);
51        if(!$text) {
52            $text='';
53        }
54        return $text;
55    }
56
57    private function resolvePageId($id)
58    {
59        $id = cleanID($id);
60        if (empty($id)) {
61            global $conf;
62            $id = cleanID($conf['start']);
63        }
64        return $id;
65    }
66
67    public function get_wordlist($fullid){
68        $txtstr=$this->rawPage($fullid);
69        if(strlen($txtstr)<20){
70            return '';
71        }
72        $outlist="";
73        $rt = preg_match("@<WORDLIST\b(.*?)>(.*?)</WORDLIST>@",$txtstr,$match);
74        if($rt){
75            $outlist=$match[2];
76        }
77        return $outlist;
78    }
79
80    function make_sql_artxt($arr){
81        $sql_w_arr=array();
82        foreach($arr as $wd){
83            $sql_w_arr[]="'$wd'";
84        }
85        return implode(",",$sql_w_arr);
86    }
87
88
89    //because i am jsonrpc ,so its allready array
90    function check_word($words_arr,$return_inf="WORD"){ //$return_inf = 'WORD' or 'MORE' or 'FULL'
91        $sql_hw_col="word";
92        $sql_rw_col="word,headword";
93        switch($return_inf){
94            case "MORE":
95                $sql_hw_col="word,rank,rankRange";
96                $sql_rw_col="word,headword,headwordId";
97                break;
98            case "FULL":
99                $sql_hw_col="word,rank,rankRange,relatedWords";
100                $sql_rw_col="word,headword,headwordId";
101                break;
102        }
103
104        $sqli = new mysqli("localhost", "www-data", "135790", "wordlist");
105
106        $headwords=array();
107
108        $sql_s=$this->make_sql_artxt($words_arr);
109
110        $sql_h_sel=<<<HSEL
111SELECT $sql_hw_col FROM headwords WHERE word IN ($sql_s)
112HSEL;
113
114        $rt=$sqli->query($sql_h_sel);
115        while($row=$rt->fetch_assoc()){
116            $headwords[]=$row['word'];
117            $headwords_fm[]=$row;
118        }
119
120        $remain_arr = array_diff($words_arr,$headwords);
121        if(count($remain_arr)>0) {
122            $sql_s = $this->make_sql_artxt($remain_arr);
123
124            $sql_rr_sel = <<<HSEL
125SELECT $sql_rw_col FROM rwords WHERE word IN ($sql_s)
126HSEL;
127            $rt = $sqli->query($sql_rr_sel);
128            $rwords = $rt->fetch_all();
129
130
131            $rel_arr = array();
132            foreach ($rwords as $dt) {
133                $rel_arr[] = $dt[0];
134            }
135            $n_arr = array_diff($remain_arr, $rel_arr);
136            sort($n_arr);
137
138        }
139
140        if($return_inf=="WORD") {
141            $out_ob = array("headwords" => $headwords,
142                "rwords" => $rwords,
143                "unwords" => $n_arr
144            );
145
146        }else{
147            if($n_arr) {
148                $sql_ext = $this->make_sql_artxt($n_arr);
149
150                $sql_ext_sel = <<<EXTSEL
151SELECT word FROM extwords WHERE word IN ($sql_ext)
152EXTSEL;
153                $rt = $sqli->query($sql_ext_sel);
154                while ($row = $rt->fetch_assoc()) {
155                    $extwords[] = $row['word'];
156                }
157
158
159                $un_arr = array_diff($n_arr, $extwords);
160
161                sort($un_arr);
162            }
163
164            $out_ob = array("headwords" => $headwords_fm,
165                "rwords" => $rwords,
166                "extwords" => $extwords,
167                "unwords" => $un_arr
168            );
169        }
170
171        $sqli->close();
172        return $out_ob;
173    }
174
175    function get_defs($words_arr,$return_inf="SIMPLE"){//$return_inf = 'WORD' or 'SIMPLE'
176        if(is_array( $words_arr)!=true||count($words_arr)<1){
177            throw new \xx_jsonrpc\E_Invalid_params("params error. invalid words array. ");
178        }
179        $sqli = new mysqli("localhost", "www-data", "135790", "gldic");
180        $sql_arr_txt = $this->make_sql_artxt($words_arr);
181
182        switch($return_inf){
183            case "MORE":
184                $sql_defs_sel=<<<DEFSMORE
185SELECT word,pron,defsimp,defen FROM more WHERE word IN ($sql_arr_txt)
186DEFSMORE;
187                break;
188            case "SIMPLE":
189                $sql_defs_sel=<<<DEFSSIMP
190SELECT word,pron,defsimp FROM simple WHERE word IN ($sql_arr_txt)
191DEFSSIMP;
192                break;
193            default:
194                throw new \xx_jsonrpc\E_Internal_error("get_defs wrong return_inf=$return_inf");
195        }
196        $rt=$sqli->query($sql_defs_sel);
197
198        $defs=$rt->fetch_all();
199        $sqli->close();
200        return $defs;
201
202    }
203
204    public function Format_def_more($word_def){
205        $word = $word_def[0];
206        $pron = $word_def[1];
207        $defsimp = $word_def[2];
208        $defen_txt = $word_def[3];
209        $out_txt="";
210        $out_txt.="===== $word =====\n";
211        $out_txt.="  * <wrap vo>$word</wrap> ($pron) \\\\ **$defsimp**\n";
212        if($defen_txt){
213            $defen=json_decode($defen_txt,TRUE);
214
215            foreach($defen as $pos=>$subdefs){
216                $out_txt.="    - $pos\n";
217                foreach($subdefs as $index=>$subd){
218                    $out_txt.="      - ''{$subd['mean']}''\\\\ {$subd['sen']}\n";
219                }
220            }
221        }
222        return $out_txt;
223    }
224
225    public function Build_defmore_list($word_list){
226        $word_def_list=$this->get_defs($word_list,"MORE");
227        $def_map=array();
228        foreach($word_def_list as $index=>$word_def){
229            $def_map[$word_def[0]]=$word_def;
230        }
231
232        $out_txt="";
233        foreach($word_list as $i=>$word){
234
235            if(isset($def_map[$word])){
236                $def = $def_map[$word];
237
238                $out_txt.=$this->Format_def_more($def);
239            }else{
240                $out_txt.="===== $word !!! =====\n";
241                $out_txt.= "  - $word : !!!!!NO DEF!!!!!\n";
242            }
243        }
244        return $out_txt;
245
246    }
247
248
249    public function Cp_subtitle($pageid,$subtitle_txt){
250
251
252        $filename=end(explode(":",$pageid));
253
254        $subid="$pageid:{$this->getConf('subtitle_dst')}";
255        $save_txt=<<<SAVETXT
256<code - $filename.srt>
257$subtitle_txt
258</code>
259SAVETXT;
260
261        $rt=$this->remoteapi->putPage($subid,$save_txt,["sum"=>"generate by Cp_subtitle"]);
262        return $pageid;
263    }
264
265    public function Cp_deflist($pageid,$word_list){
266
267        $def_txt=$this->Build_defmore_list($word_list);
268        $defid="$pageid:{$this->getConf('def_dst')}";
269        $rt=$this->remoteapi->putPage($defid,$def_txt,["sum"=>"generate by Cp_deflist"]);
270        return $pageid;
271    }
272
273    public function Cp_wordlist($pageid,$txt){
274        $wordlistid="$pageid:{$this->getConf('wordlist_dst')}";
275        $rt=$this->remoteapi->putPage($wordlistid,$txt,["sum"=>"generate by Cp_wordlist"]);
276        return $pageid;
277    }
278
279    public function Cp_stable($pageid){
280
281    }
282
283
284
285}