Exemple #1
0
 function lines()
 {
     if (!empty($this->lines)) {
         return $this->lines;
     }
     $t1 = microtime(true);
     $content = cy_remove_html_attr($this->html, ['style']);
     $pattern = [];
     //$pattern[] = '/<!--.*?-->/s';
     $pattern[0] = '/<script.*?>.*?<\\/script>/si';
     $pattern[1] = '/<style.*?>.*?<\\/style>/si';
     $pattern[2] = '/<(\\w+)[^>]*?><\\/\\1>/si';
     $pattern[3] = '/<a.*?>|<\\/a>/si';
     $pattern[4] = '/<\\s*([^>]+?)\\s*>/s';
     $replace = array_fill(0, 4, '');
     $replace[] = '<$1>';
     $this->html_clean = preg_replace($pattern, $replace, $content);
     $texts = $this->textlines($this->html_clean);
     //$this->html_clean = preg_replace('/<\s*([^>]*?)\s*>/s', '$1', $content);
     foreach ($texts as $i => $line) {
         $this->lines[$i] = $line;
     }
     $cost = (microtime(true) - $t1) * 1000000;
     cy_stat('html-lines', $cost);
     return $this->lines;
 }
Exemple #2
0
 function get()
 {
     $active = null;
     $t1 = microtime(true);
     do {
         $mrc = curl_multi_exec($this->mh, $active);
     } while ($mrc == CURLM_CALL_MULTI_PERFORM);
     $select_func = function_exists('cy_curl_multi_select') ? 'cy_curl_multi_select' : 'curl_multi_select';
     while ($active && $mrc == CURLM_OK) {
         PHP_VERSION_ID < 50214 ? usleep(5000) : $select_func($this->mh);
         do {
             $mrc = curl_multi_exec($this->mh, $active);
         } while ($mrc == CURLM_CALL_MULTI_PERFORM);
     }
     $stat = array();
     $data = array();
     while ($r = curl_multi_info_read($this->mh, $msgs_in_queue)) {
         if ($r['msg'] == CURLMSG_DONE) {
             $ch = $r['handle'];
             list(, $c, $key) = $this->handles[(int) $ch];
             $data[$key] = $this->mix($ch, $c);
         }
     }
     /*
     foreach($this->handles as $id => list($ch, $c, $key))
     {
     	$data[$key] = $this->mix($ch, $c);
     }
     */
     $cost = (microtime(true) - $t1) * 1000000;
     cy_stat('Curl-get', $cost, array('c' => implode(',', $stat)));
     return array('errno' => 0, 'data' => $data);
 }
Exemple #3
0
 function query($sql, $options = [])
 {
     $timeout = isset($options['timeout']) ? $options['timeout'] : $_ENV['config']['timeout']['mysql_read'];
     if (!$this->is_avaliable()) {
         return cy_dt(CYE_DB_CONNECT_ERROR, 'mysql connect is not available.');
     }
     $this->db->query($sql, MYSQLI_ASYNC);
     $finished = 0;
     $t1 = microtime(true);
     while (!$finished) {
         $links = $errors = $reject = [$this->db];
         if (!mysqli_poll($links, $errors, $reject, 0, 100000)) {
             if (microtime(true) - $t1 < $timeout) {
                 continue;
             }
             $this->db->close();
             cy_log(CYE_ERROR, 'mysql query timeout, over %f second', $timeout);
             $dt = cy_dt(CYE_NET_TIMEOUT, 'mysql query timeout');
             goto end;
         }
         $finished = 1;
     }
     if (!($result = $this->db->reap_async_query())) {
         cy_log(CYE_ERROR, 'mysql query error [%d] [%s]', $this->db->errno, $this->db->error);
         $dt = cy_dt($this->db->errno, $this->db->error);
         goto end;
     }
     /* For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object */
     if (is_object($result)) {
         $rows = $result->fetch_all(MYSQLI_ASSOC);
         mysqli_free_result($result);
         $dt = cy_dt(0, $rows);
         goto end;
     }
     /* For other successful queries mysqli_query() will return TRUE. */
     $r = ['affected_rows' => $this->db->affected_rows, 'insert_id' => $this->db->insert_id];
     $dt = ['errno' => 0, 'data' => $r, 'info' => $this->db->info];
     end:
     $t2 = microtime(true);
     cy_stat('mysql-query', ($t2 - $t1) * 1000000);
     return $dt;
 }
Exemple #4
0
 function __call($method, $args)
 {
     $data = [];
     $call = ['mGet' => 'find', 'save' => 'save', 'mInsert' => 'batchInsert', 'delete' => 'remove', 'update' => 'update'];
     $getid = ['mGet' => false, 'save' => 'save', 'mInsert' => true, 'delete' => false, 'update' => true];
     if (empty($call[$method])) {
         return cy_dt(-1, 'unkown method.');
     }
     $t1 = microtime(true);
     $table = array_shift($args);
     $dbname = $this->c['database'];
     try {
         if (!self::$mongo) {
             self::$mongo = $this->connect();
         }
         $db = self::$mongo->{$dbname};
         $c10n = $table == 'file' ? $db->getGridFS() : $db->{$table};
         $back = call_user_func_array([$c10n, $call[$method]], $args);
         if (is_object($back)) {
             $back = iterator_to_array($back);
         } else {
             if ($getid[$method] && $back) {
                 $list = array();
                 foreach ($args as $k => $v) {
                     $list[$k] = (string) $v['_id'];
                 }
                 $back = $list;
             }
         }
         $data = cy_dt(0, $back);
         //self::$mongo->close();
     } catch (MongoCursorException $e) {
         $error = substr($e->getMessage(), 0, 512);
         cy_log(CYE_ERROR, "mongo-{$method} " . $error);
         $data = cy_dt(CYE_SYSTEM_ERROR, $error);
     } catch (MongoCursorTimeoutException $e) {
         $error = substr($e->getMessage(), 0, 512);
         cy_log(CYE_ERROR, "mongo-{$method} " . $error);
         cy_log(CYE_ERROR, "mongo-{$method} close=%d, reconnect=%d", self::$mongo->close(), self::$mongo->connect);
         $data = cy_dt(CYE_SYSTEM_ERROR, $error);
     } catch (MongoConnectionException $e) {
         $error = substr($e->getMessage(), 0, 512);
         cy_log(CYE_ERROR, "mongo-{$method} " . $error);
         $data = cy_dt(CYE_SYSTEM_ERROR, $error);
     } catch (Exception $e) {
         $error = substr($e->getMessage(), 0, 512);
         cy_log(CYE_ERROR, "mongo-{$method} " . $error);
         $data = cy_dt(CYE_SYSTEM_ERROR, $error);
     }
     // end process.
     $cost = (microtime(true) - $t1) * 1000000;
     cy_stat('mongo-' . $method, $cost);
     return $data;
 }
Exemple #5
0
 function connect($config)
 {
     if (empty($config)) {
         $mysql_conf = $_ENV['config']['db'];
     } else {
         $mysql_conf = $config;
     }
     if (empty($mysql_conf)) {
         $mysql_conf = [['host' => '127.0.0.1', 'port' => '3306', 'user' => '', 'password' => '', 'database' => '']];
     }
     $cfg_count = count($mysql_conf);
     $i = array_rand($mysql_conf, 1);
     $re = 0;
     do {
         $i = ($i + 1) % $cfg_count;
         $config = $mysql_conf[$i];
         $server = $config['host'] . ':' . $config['port'];
     } while (0);
     //while(!cy_ctl_check($server, $_SERVER['REQUEST_TIME']) && $re++ < $cfg_count);
     if (empty($config)) {
         cy_log(CYE_ERROR, 'Mysqlc::connect DB config is not found.');
         return NULL;
     }
     $t1 = microtime(true);
     $conn = new CY_Mysqli();
     $to = $_ENV['config']['timeout'];
     $conn->options(MYSQLI_OPT_CONNECT_TIMEOUT, $to['mysql_connect']);
     $conn->options(MYSQLI_OPT_READ_TIMEOUT, $to['mysql_read']);
     $conn->options(MYSQLI_OPT_WRITE_TIMEOUT, 1);
     $ret = $conn->real_connect($config['host'], $config['user'], $config['password'], $config['database'], $config['port']);
     if (!$ret) {
         //cy_log(CYE_WARNING, "connect to [%s] at port [%s] failed, %s", $config['host'], $config['port'], $conn->connect_error);
         //cy_stat('mysql-connect', (microtime(true) - $t1)*1000000, ['errno' => $conn->connect_errno]);
         //cy_ctl_fail($server, $_SERVER['REQUEST_TIME']);
         return NULL;
     }
     $conn->setcfg($config);
     $conn->set_charset("utf8");
     $conn->query("set names utf8,character_set_client=binary");
     //cy_ctl_succ($server, $_SERVER['REQUEST_TIME']);
     cy_stat('mysql-connect', (microtime(true) - $t1) * 1000000);
     return $conn;
 }
Exemple #6
0
 function mGet($maps, $options = array())
 {
     $indexs = array();
     $partitions = $this->partitions($maps, $options);
     foreach ($partitions as $i => &$part) {
         $index = array();
         $pieces = ['*', 1, "\r\n\$4\r\nMGET\r\n"];
         $count =& $pieces[1];
         foreach ($part['maps'] as $k => $v) {
             $pieces[] = "\$" . strlen($k) . "\r\n" . $k . "\r\n";
             $index[] = $v;
             $count++;
         }
         $part['body'] = implode($pieces);
         $part['read_func'] = array($this, 'rc_read');
         unset($part['maps']);
         $indexs[$i] = $index;
         $this->net->prepare($i, array_merge($part, $options));
     }
     $dt = $this->net->get();
     if ($dt['errno'] !== 0) {
         cy_log(CYE_ERROR, "rc-mGet net get error.");
         cy_stat('rc-mGet', $dt['costs'], array('errno' => $dt['errno']));
         return $dt;
     }
     cy_stat('rc-mGet', $dt['costs']);
     $data = array();
     foreach ($dt['data'] as $i => $d) {
         if ($d['errno'] !== 0) {
             cy_log(CYE_ERROR, 'rc-mGet ' . $partitions[$i]['server'] . ' failed.');
             continue;
         }
         $idx = $indexs[$i];
         foreach ($d['data'] as $n => $val) {
             $data[$idx[$n]] = $val;
         }
     }
     krsort($data);
     return array('errno' => 0, 'data' => $data);
 }