Remove unneeded commented code, that I accidently added in r82461
[mediawiki.git] / includes / ZhClient.php
bloba04220c6a195d3696dc6911bf054c3712e6772a6
1 <?php
3 /**
4 * Client for querying zhdaemon
6 */
7 class ZhClient {
8 var $mHost, $mPort, $mFP, $mConnected;
10 /**
11 * Constructor
13 * @access private
15 function __construct($host, $port) {
16 $this->mHost = $host;
17 $this->mPort = $port;
18 $this->mConnected = $this->connect();
21 /**
22 * Check if connection to zhdaemon is successful
24 function isconnected() {
25 return $this->mConnected;
28 /**
29 * Establish conncetion
31 * @access private
33 function connect() {
34 wfSuppressWarnings();
35 $errno = $errstr = '';
36 $this->mFP = fsockopen($this->mHost, $this->mPort, $errno, $errstr, 30);
37 wfRestoreWarnings();
38 if ( !$this->mFP ) {
39 return false;
41 return true;
44 /**
45 * Query the daemon and return the result
47 * @access private
49 function query($request) {
50 if ( !$this->mConnected ) {
51 return false;
54 fwrite($this->mFP, $request);
56 $result=fgets($this->mFP, 1024);
58 list($status, $len) = explode(" ", $result);
59 if($status == 'ERROR') {
60 //$len is actually the error code...
61 print "zhdaemon error $len<br />\n";
62 return false;
64 $bytesread=0;
65 $data='';
66 while(!feof($this->mFP) && $bytesread<$len) {
67 $str= fread($this->mFP, $len-$bytesread);
68 $bytesread += strlen($str);
69 $data .= $str;
71 //data should be of length $len. otherwise something is wrong
72 if ( strlen($data) != $len ) {
73 return false;
75 return $data;
78 /**
79 * Convert the input to a different language variant
81 * @param $text string: input text
82 * @param $tolang string: language variant
83 * @return string the converted text
85 function convert($text, $tolang) {
86 $len = strlen($text);
87 $q = "CONV $tolang $len\n$text";
88 $result = $this->query($q);
89 if ( !$result ) {
90 $result = $text;
92 return $result;
95 /**
96 * Convert the input to all possible variants
98 * @param $text string: input text
99 * @return array langcode => converted_string
101 function convertToAllVariants($text) {
102 $len = strlen($text);
103 $q = "CONV ALL $len\n$text";
104 $result = $this->query($q);
105 if ( !$result ) {
106 return false;
108 list($infoline, $data) = explode('|', $result, 2);
109 $info = explode(";", $infoline);
110 $ret = array();
111 $i=0;
112 foreach($info as $variant) {
113 list($code, $len) = explode(' ', $variant);
114 $ret[strtolower($code)] = substr($data, $i, $len);
115 $i+=$len;
117 return $ret;
120 * Perform word segmentation
122 * @param $text string: input text
123 * @return string segmented text
125 function segment($text) {
126 $len = strlen($text);
127 $q = "SEG $len\n$text";
128 $result = $this->query($q);
129 if ( !$result ) {// fallback to character based segmentation
130 $result = $this->segment($text);
132 return $result;
136 * Close the connection
138 function close() {
139 fclose($this->mFP);