Merge "Add cache versioning to InfoAction."
[lhc/web/wiklou.git] / includes / ZhClient.php
index 7807319..c5955ae 100644 (file)
@@ -1,4 +1,24 @@
 <?php
+/**
+ * Client for querying zhdaemon.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
 
 /**
  * Client for querying zhdaemon
@@ -9,7 +29,10 @@ class ZhClient {
        /**
         * Constructor
         *
-        * @access private
+        * @param $host
+        * @param $port
+        *
+        * @return ZhClient
         */
        function __construct( $host, $port ) {
                $this->mHost = $host;
@@ -27,7 +50,7 @@ class ZhClient {
        }
 
        /**
-        * Establish conncetion
+        * Establish connection
         *
         * @access private
         *
@@ -38,16 +61,15 @@ class ZhClient {
                $errno = $errstr = '';
                $this->mFP = fsockopen( $this->mHost, $this->mPort, $errno, $errstr, 30 );
                wfRestoreWarnings();
-               if ( !$this->mFP ) {
-                       return false;
-               }
-               return true;
+               return !$this->mFP;
        }
 
        /**
         * Query the daemon and return the result
         *
         * @access private
+        *
+        * @return string
         */
        function query( $request ) {
                if ( !$this->mConnected ) {
@@ -59,30 +81,27 @@ class ZhClient {
                $result = fgets( $this->mFP, 1024 );
 
                list( $status, $len ) = explode( ' ', $result );
-               if( $status == 'ERROR' ) {
+               if ( $status == 'ERROR' ) {
                        // $len is actually the error code...
                        print "zhdaemon error $len<br />\n";
                        return false;
                }
                $bytesread = 0;
                $data = '';
-               while( !feof( $this->mFP ) && $bytesread < $len ) {
+               while ( !feof( $this->mFP ) && $bytesread < $len ) {
                        $str = fread( $this->mFP, $len - $bytesread );
                        $bytesread += strlen( $str );
                        $data .= $str;
                }
                // data should be of length $len. otherwise something is wrong
-               if ( strlen( $data ) != $len ) {
-                       return false;
-               }
-               return $data;
+               return strlen( $data ) == $len;
        }
 
        /**
         * Convert the input to a different language variant
         *
-        * @param $text String: input text
-        * @param $tolang String: language variant
+        * @param string $text input text
+        * @param string $tolang language variant
         * @return string the converted text
         */
        function convert( $text, $tolang ) {
@@ -98,7 +117,7 @@ class ZhClient {
        /**
         * Convert the input to all possible variants
         *
-        * @param $text String: input text
+        * @param string $text input text
         * @return array langcode => converted_string
         */
        function convertToAllVariants( $text ) {
@@ -112,17 +131,18 @@ class ZhClient {
                $info = explode( ';', $infoline );
                $ret = array();
                $i = 0;
-               foreach( $info as $variant ) {
+               foreach ( $info as $variant ) {
                        list( $code, $len ) = explode( ' ', $variant );
                        $ret[strtolower( $code )] = substr( $data, $i, $len );
                        $i += $len;
                }
                return $ret;
        }
+
        /**
         * Perform word segmentation
         *
-        * @param $text String: input text
+        * @param string $text input text
         * @return string segmented text
         */
        function segment( $text ) {