MediaWiki  REL1_22
ZhClient.php
Go to the documentation of this file.
00001 <?php
00026 class ZhClient {
00027     var $mHost, $mPort, $mFP, $mConnected;
00028 
00037     function __construct( $host, $port ) {
00038         $this->mHost = $host;
00039         $this->mPort = $port;
00040         $this->mConnected = $this->connect();
00041     }
00042 
00048     function isconnected() {
00049         return $this->mConnected;
00050     }
00051 
00059     function connect() {
00060         wfSuppressWarnings();
00061         $errno = $errstr = '';
00062         $this->mFP = fsockopen( $this->mHost, $this->mPort, $errno, $errstr, 30 );
00063         wfRestoreWarnings();
00064         return !$this->mFP;
00065     }
00066 
00074     function query( $request ) {
00075         if ( !$this->mConnected ) {
00076             return false;
00077         }
00078 
00079         fwrite( $this->mFP, $request );
00080 
00081         $result = fgets( $this->mFP, 1024 );
00082 
00083         list( $status, $len ) = explode( ' ', $result );
00084         if ( $status == 'ERROR' ) {
00085             // $len is actually the error code...
00086             print "zhdaemon error $len<br />\n";
00087             return false;
00088         }
00089         $bytesread = 0;
00090         $data = '';
00091         while ( !feof( $this->mFP ) && $bytesread < $len ) {
00092             $str = fread( $this->mFP, $len - $bytesread );
00093             $bytesread += strlen( $str );
00094             $data .= $str;
00095         }
00096         // data should be of length $len. otherwise something is wrong
00097         return strlen( $data ) == $len;
00098     }
00099 
00107     function convert( $text, $tolang ) {
00108         $len = strlen( $text );
00109         $q = "CONV $tolang $len\n$text";
00110         $result = $this->query( $q );
00111         if ( !$result ) {
00112             $result = $text;
00113         }
00114         return $result;
00115     }
00116 
00123     function convertToAllVariants( $text ) {
00124         $len = strlen( $text );
00125         $q = "CONV ALL $len\n$text";
00126         $result = $this->query( $q );
00127         if ( !$result ) {
00128             return false;
00129         }
00130         list( $infoline, $data ) = explode( '|', $result, 2 );
00131         $info = explode( ';', $infoline );
00132         $ret = array();
00133         $i = 0;
00134         foreach ( $info as $variant ) {
00135             list( $code, $len ) = explode( ' ', $variant );
00136             $ret[strtolower( $code )] = substr( $data, $i, $len );
00137             $i += $len;
00138         }
00139         return $ret;
00140     }
00141 
00148     function segment( $text ) {
00149         $len = strlen( $text );
00150         $q = "SEG $len\n$text";
00151         $result = $this->query( $q );
00152         if ( !$result ) { // fallback to character based segmentation
00153             $result = $this->segment( $text );
00154         }
00155         return $result;
00156     }
00157 
00161     function close() {
00162         fclose( $this->mFP );
00163     }
00164 }