@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | |
| 16 | 16 | namespace phpspider\core; |
| 17 | 17 | // 引入PATH_DATA |
| 18 | -require_once __DIR__ . '/constants.php'; |
|
| 18 | +require_once __DIR__.'/constants.php'; |
|
| 19 | 19 | |
| 20 | 20 | class log |
| 21 | 21 | { |
@@ -82,13 +82,13 @@ discard block |
||
| 82 | 82 | |
| 83 | 83 | if ($log_type == 'note') |
| 84 | 84 | { |
| 85 | - $msg = self::$out_sta. $msg . "\n".self::$out_end; |
|
| 85 | + $msg = self::$out_sta.$msg."\n".self::$out_end; |
|
| 86 | 86 | } |
| 87 | 87 | else |
| 88 | 88 | { |
| 89 | - $msg = self::$out_sta.date("Y-m-d H:i:s")." [{$log_type}] " . $msg .self::$out_end. "\n"; |
|
| 89 | + $msg = self::$out_sta.date("Y-m-d H:i:s")." [{$log_type}] ".$msg.self::$out_end."\n"; |
|
| 90 | 90 | } |
| 91 | - if(self::$log_show) |
|
| 91 | + if (self::$log_show) |
|
| 92 | 92 | { |
| 93 | 93 | echo $msg; |
| 94 | 94 | } |
@@ -105,9 +105,9 @@ discard block |
||
| 105 | 105 | { |
| 106 | 106 | if ($log_type != '') |
| 107 | 107 | { |
| 108 | - $msg = date("Y-m-d H:i:s")." [{$log_type}] " . $msg . "\n"; |
|
| 108 | + $msg = date("Y-m-d H:i:s")." [{$log_type}] ".$msg."\n"; |
|
| 109 | 109 | } |
| 110 | - if(self::$log_show) |
|
| 110 | + if (self::$log_show) |
|
| 111 | 111 | { |
| 112 | 112 | echo $msg; |
| 113 | 113 | } |
@@ -83,8 +83,7 @@ |
||
| 83 | 83 | if ($log_type == 'note') |
| 84 | 84 | { |
| 85 | 85 | $msg = self::$out_sta. $msg . "\n".self::$out_end; |
| 86 | - } |
|
| 87 | - else |
|
| 86 | + } else |
|
| 88 | 87 | { |
| 89 | 88 | $msg = self::$out_sta.date("Y-m-d H:i:s")." [{$log_type}] " . $msg .self::$out_end. "\n"; |
| 90 | 89 | } |
@@ -376,20 +376,20 @@ discard block |
||
| 376 | 376 | |
| 377 | 377 | $ids = array(); |
| 378 | 378 | foreach ($set as $val) |
| 379 | - { |
|
| 379 | + { |
|
| 380 | 380 | ksort($val); |
| 381 | 381 | // 去重,其实不去也可以,因为相同的when只会执行第一个,后面的就直接跳过不执行了 |
| 382 | 382 | $key = md5($val[$index]); |
| 383 | - $ids[$key] = $val[$index]; |
|
| 384 | - |
|
| 385 | - foreach (array_keys($val) as $field) |
|
| 386 | - { |
|
| 387 | - if ($field != $index) |
|
| 388 | - { |
|
| 389 | - $final[$field][$key] = 'When `'.$index.'` = "'.$val[$index].'" Then "'.$val[$field].'"'; |
|
| 390 | - } |
|
| 391 | - } |
|
| 392 | - } |
|
| 383 | + $ids[$key] = $val[$index]; |
|
| 384 | + |
|
| 385 | + foreach (array_keys($val) as $field) |
|
| 386 | + { |
|
| 387 | + if ($field != $index) |
|
| 388 | + { |
|
| 389 | + $final[$field][$key] = 'When `'.$index.'` = "'.$val[$index].'" Then "'.$val[$field].'"'; |
|
| 390 | + } |
|
| 391 | + } |
|
| 392 | + } |
|
| 393 | 393 | //$ids = array_values($ids); |
| 394 | 394 | |
| 395 | 395 | // 如果不是数组而且不为空,就转数组 |
@@ -400,29 +400,29 @@ discard block |
||
| 400 | 400 | $where[] = $index.' In ("'.implode('","', $ids).'")'; |
| 401 | 401 | $where = empty($where) ? "" : " Where ".implode(" And ", $where); |
| 402 | 402 | |
| 403 | - $sql = "Update `".$table."` Set "; |
|
| 404 | - $cases = ''; |
|
| 403 | + $sql = "Update `".$table."` Set "; |
|
| 404 | + $cases = ''; |
|
| 405 | 405 | |
| 406 | - foreach ($final as $k => $v) |
|
| 407 | - { |
|
| 406 | + foreach ($final as $k => $v) |
|
| 407 | + { |
|
| 408 | 408 | // 过滤掉数据库没有的字段 |
| 409 | 409 | if (!in_array($k, $fields)) |
| 410 | 410 | { |
| 411 | 411 | continue; |
| 412 | 412 | } |
| 413 | - $cases .= '`'.$k.'` = Case '."\n"; |
|
| 414 | - foreach ($v as $row) |
|
| 415 | - { |
|
| 416 | - $cases .= $row."\n"; |
|
| 417 | - } |
|
| 413 | + $cases .= '`'.$k.'` = Case '."\n"; |
|
| 414 | + foreach ($v as $row) |
|
| 415 | + { |
|
| 416 | + $cases .= $row."\n"; |
|
| 417 | + } |
|
| 418 | 418 | |
| 419 | - $cases .= 'Else `'.$k.'` End, '; |
|
| 420 | - } |
|
| 419 | + $cases .= 'Else `'.$k.'` End, '; |
|
| 420 | + } |
|
| 421 | 421 | |
| 422 | - $sql .= substr($cases, 0, -2); |
|
| 422 | + $sql .= substr($cases, 0, -2); |
|
| 423 | 423 | |
| 424 | 424 | // 其实不带 Where In ($index) 的条件也可以的 |
| 425 | - $sql .= $where; |
|
| 425 | + $sql .= $where; |
|
| 426 | 426 | |
| 427 | 427 | if ($return_sql) return $sql; |
| 428 | 428 | |
@@ -39,11 +39,11 @@ discard block |
||
| 39 | 39 | //echo "progress[".self::$links[self::$link_name]['pid']."] create db connect[".self::$link_name."]\n"; |
| 40 | 40 | } |
| 41 | 41 | self::$links[self::$link_name]['conn'] = mysqli_connect($config['host'], $config['user'], $config['pass'], $config['name'], $config['port']); |
| 42 | - if(mysqli_connect_errno()) |
|
| 42 | + if (mysqli_connect_errno()) |
|
| 43 | 43 | { |
| 44 | 44 | self::$links[self::$link_name]['fail']++; |
| 45 | - $errmsg = 'Mysql Connect failed['.self::$links[self::$link_name]['fail'].']: ' . mysqli_connect_error(); |
|
| 46 | - echo util::colorize(date("H:i:s") . " {$errmsg}\n\n", 'fail'); |
|
| 45 | + $errmsg = 'Mysql Connect failed['.self::$links[self::$link_name]['fail'].']: '.mysqli_connect_error(); |
|
| 46 | + echo util::colorize(date("H:i:s")." {$errmsg}\n\n", 'fail'); |
|
| 47 | 47 | log::add($errmsg, "Error"); |
| 48 | 48 | // 连接失败5次,中断进程 |
| 49 | 49 | if (self::$links[self::$link_name]['fail'] >= 5) |
@@ -81,9 +81,9 @@ discard block |
||
| 81 | 81 | */ |
| 82 | 82 | public static function clear_link() |
| 83 | 83 | { |
| 84 | - if(self::$links) |
|
| 84 | + if (self::$links) |
|
| 85 | 85 | { |
| 86 | - foreach(self::$links as $k=>$v) |
|
| 86 | + foreach (self::$links as $k=>$v) |
|
| 87 | 87 | { |
| 88 | 88 | @mysqli_close($v['conn']); |
| 89 | 89 | unset(self::$links[$k]); |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | { |
| 139 | 139 | if (!is_array($GLOBALS['config']['db'])) |
| 140 | 140 | { |
| 141 | - exit('db.php _get_default_config()' . '没有mysql配置'); |
|
| 141 | + exit('db.php _get_default_config()'.'没有mysql配置'); |
|
| 142 | 142 | } |
| 143 | 143 | self::$configs['default'] = $GLOBALS['config']['db']; |
| 144 | 144 | } |
@@ -156,7 +156,7 @@ discard block |
||
| 156 | 156 | |
| 157 | 157 | public static function autocommit($mode = false) |
| 158 | 158 | { |
| 159 | - if ( self::$autocommiting ) |
|
| 159 | + if (self::$autocommiting) |
|
| 160 | 160 | { |
| 161 | 161 | return true; |
| 162 | 162 | } |
@@ -218,18 +218,18 @@ discard block |
||
| 218 | 218 | array_shift($backtrace); |
| 219 | 219 | $narr = array('class', 'type', 'function', 'file', 'line'); |
| 220 | 220 | $err = "debug_backtrace:\n"; |
| 221 | - foreach($backtrace as $i => $l) |
|
| 221 | + foreach ($backtrace as $i => $l) |
|
| 222 | 222 | { |
| 223 | - foreach($narr as $k) |
|
| 223 | + foreach ($narr as $k) |
|
| 224 | 224 | { |
| 225 | - if( !isset($l[$k]) ) |
|
| 225 | + if (!isset($l[$k])) |
|
| 226 | 226 | { |
| 227 | 227 | $l[$k] = ''; |
| 228 | 228 | } |
| 229 | 229 | } |
| 230 | 230 | $err .= "[$i] in function {$l['class']}{$l['type']}{$l['function']} "; |
| 231 | - if($l['file']) $err .= " in {$l['file']} "; |
|
| 232 | - if($l['line']) $err .= " on line {$l['line']} "; |
|
| 231 | + if ($l['file']) $err .= " in {$l['file']} "; |
|
| 232 | + if ($l['line']) $err .= " on line {$l['line']} "; |
|
| 233 | 233 | $err .= "\n"; |
| 234 | 234 | } |
| 235 | 235 | log::add($err); |
@@ -253,7 +253,7 @@ discard block |
||
| 253 | 253 | { |
| 254 | 254 | if (!preg_match("/limit/i", $sql)) |
| 255 | 255 | { |
| 256 | - $sql = preg_replace("/[,;]$/i", '', trim($sql)) . " limit 1 "; |
|
| 256 | + $sql = preg_replace("/[,;]$/i", '', trim($sql))." limit 1 "; |
|
| 257 | 257 | } |
| 258 | 258 | $rsid = self::query($sql); |
| 259 | 259 | if ($rsid === false) |
@@ -272,7 +272,7 @@ discard block |
||
| 272 | 272 | { |
| 273 | 273 | return array(); |
| 274 | 274 | } |
| 275 | - while ( $row = self::fetch($rsid) ) |
|
| 275 | + while ($row = self::fetch($rsid)) |
|
| 276 | 276 | { |
| 277 | 277 | $rows[] = $row; |
| 278 | 278 | } |
@@ -305,7 +305,7 @@ discard block |
||
| 305 | 305 | $items_sql .= "`$k`,"; |
| 306 | 306 | $values_sql .= "\"$v\","; |
| 307 | 307 | } |
| 308 | - $sql = "Insert Ignore Into `{$table}` (" . substr($items_sql, 0, -1) . ") Values (" . substr($values_sql, 0, -1) . ")"; |
|
| 308 | + $sql = "Insert Ignore Into `{$table}` (".substr($items_sql, 0, -1).") Values (".substr($values_sql, 0, -1).")"; |
|
| 309 | 309 | if ($return_sql) |
| 310 | 310 | { |
| 311 | 311 | return $sql; |
@@ -386,7 +386,7 @@ discard block |
||
| 386 | 386 | { |
| 387 | 387 | if ($field != $index) |
| 388 | 388 | { |
| 389 | - $final[$field][$key] = 'When `'.$index.'` = "'.$val[$index].'" Then "'.$val[$field].'"'; |
|
| 389 | + $final[$field][$key] = 'When `'.$index.'` = "'.$val[$index].'" Then "'.$val[$field].'"'; |
|
| 390 | 390 | } |
| 391 | 391 | } |
| 392 | 392 | } |
@@ -453,8 +453,8 @@ discard block |
||
| 453 | 453 | unset($where[$k]); |
| 454 | 454 | } |
| 455 | 455 | } |
| 456 | - $where = empty($where) ? "" : " Where " . implode(" And ", $where); |
|
| 457 | - $sql = substr($sql, 0, -1) . $where; |
|
| 456 | + $where = empty($where) ? "" : " Where ".implode(" And ", $where); |
|
| 457 | + $sql = substr($sql, 0, -1).$where; |
|
| 458 | 458 | if ($return_sql) |
| 459 | 459 | { |
| 460 | 460 | return $sql; |
@@ -479,7 +479,7 @@ discard block |
||
| 479 | 479 | { |
| 480 | 480 | return false; |
| 481 | 481 | } |
| 482 | - $where = 'Where ' . (!is_array($where) ? $where : implode(' And ', $where)); |
|
| 482 | + $where = 'Where '.(!is_array($where) ? $where : implode(' And ', $where)); |
|
| 483 | 483 | $sql = "Delete From `{$table}` {$where}"; |
| 484 | 484 | if ($return_sql) |
| 485 | 485 | { |
@@ -511,11 +511,11 @@ discard block |
||
| 511 | 511 | public static function strsafe($array) |
| 512 | 512 | { |
| 513 | 513 | $arrays = array(); |
| 514 | - if(is_array($array)===true) |
|
| 514 | + if (is_array($array) === true) |
|
| 515 | 515 | { |
| 516 | 516 | foreach ($array as $key => $val) |
| 517 | 517 | { |
| 518 | - if(is_array($val)===true) |
|
| 518 | + if (is_array($val) === true) |
|
| 519 | 519 | { |
| 520 | 520 | $arrays[$key] = self::strsafe($val); |
| 521 | 521 | } |
@@ -561,7 +561,7 @@ discard block |
||
| 561 | 561 | |
| 562 | 562 | public static function table_exists($table_name) |
| 563 | 563 | { |
| 564 | - $sql = "SHOW TABLES LIKE '" . $table_name . "'"; |
|
| 564 | + $sql = "SHOW TABLES LIKE '".$table_name."'"; |
|
| 565 | 565 | $rsid = self::query($sql); |
| 566 | 566 | $table = self::fetch($rsid); |
| 567 | 567 | if (empty($table)) |
@@ -51,13 +51,11 @@ discard block |
||
| 51 | 51 | exit(250); |
| 52 | 52 | } |
| 53 | 53 | self::_init($config); |
| 54 | - } |
|
| 55 | - else |
|
| 54 | + } else |
|
| 56 | 55 | { |
| 57 | 56 | mysqli_query(self::$links[self::$link_name]['conn'], " SET character_set_connection=utf8, character_set_results=utf8, character_set_client=binary, sql_mode='' "); |
| 58 | 57 | } |
| 59 | - } |
|
| 60 | - else |
|
| 58 | + } else |
|
| 61 | 59 | { |
| 62 | 60 | $curr_pid = function_exists('posix_getpid') ? posix_getpid() : 0; |
| 63 | 61 | // 如果父进程已经生成资源就释放重新生成,因为多进程不能共享连接资源 |
@@ -106,8 +104,7 @@ discard block |
||
| 106 | 104 | if (!empty($config)) |
| 107 | 105 | { |
| 108 | 106 | self::$configs[self::$link_name] = $config; |
| 109 | - } |
|
| 110 | - else |
|
| 107 | + } else |
|
| 111 | 108 | { |
| 112 | 109 | if (empty(self::$configs[self::$link_name])) |
| 113 | 110 | { |
@@ -228,15 +225,18 @@ discard block |
||
| 228 | 225 | } |
| 229 | 226 | } |
| 230 | 227 | $err .= "[$i] in function {$l['class']}{$l['type']}{$l['function']} "; |
| 231 | - if($l['file']) $err .= " in {$l['file']} "; |
|
| 232 | - if($l['line']) $err .= " on line {$l['line']} "; |
|
| 228 | + if($l['file']) { |
|
| 229 | + $err .= " in {$l['file']} "; |
|
| 230 | + } |
|
| 231 | + if($l['line']) { |
|
| 232 | + $err .= " on line {$l['line']} "; |
|
| 233 | + } |
|
| 233 | 234 | $err .= "\n"; |
| 234 | 235 | } |
| 235 | 236 | log::add($err); |
| 236 | 237 | |
| 237 | 238 | return false; |
| 238 | - } |
|
| 239 | - else |
|
| 239 | + } else |
|
| 240 | 240 | { |
| 241 | 241 | return self::$rsid; |
| 242 | 242 | } |
@@ -309,14 +309,12 @@ discard block |
||
| 309 | 309 | if ($return_sql) |
| 310 | 310 | { |
| 311 | 311 | return $sql; |
| 312 | - } |
|
| 313 | - else |
|
| 312 | + } else |
|
| 314 | 313 | { |
| 315 | 314 | if (self::query($sql)) |
| 316 | 315 | { |
| 317 | 316 | return mysqli_insert_id(self::$links[self::$link_name]['conn']); |
| 318 | - } |
|
| 319 | - else |
|
| 317 | + } else |
|
| 320 | 318 | { |
| 321 | 319 | return false; |
| 322 | 320 | } |
@@ -356,7 +354,9 @@ discard block |
||
| 356 | 354 | |
| 357 | 355 | $sql = "Insert Ignore Into `{$table}`(".implode(", ", $keys_sql).") Values (".implode("), (", $vals_sql).")"; |
| 358 | 356 | |
| 359 | - if ($return_sql) return $sql; |
|
| 357 | + if ($return_sql) { |
|
| 358 | + return $sql; |
|
| 359 | + } |
|
| 360 | 360 | |
| 361 | 361 | $rt = self::query($sql); |
| 362 | 362 | $insert_id = self::insert_id(); |
@@ -424,7 +424,9 @@ discard block |
||
| 424 | 424 | // 其实不带 Where In ($index) 的条件也可以的 |
| 425 | 425 | $sql .= $where; |
| 426 | 426 | |
| 427 | - if ($return_sql) return $sql; |
|
| 427 | + if ($return_sql) { |
|
| 428 | + return $sql; |
|
| 429 | + } |
|
| 428 | 430 | |
| 429 | 431 | $rt = self::query($sql); |
| 430 | 432 | $insert_id = self::affected_rows(); |
@@ -458,14 +460,12 @@ discard block |
||
| 458 | 460 | if ($return_sql) |
| 459 | 461 | { |
| 460 | 462 | return $sql; |
| 461 | - } |
|
| 462 | - else |
|
| 463 | + } else |
|
| 463 | 464 | { |
| 464 | 465 | if (self::query($sql)) |
| 465 | 466 | { |
| 466 | 467 | return mysqli_affected_rows(self::$links[self::$link_name]['conn']); |
| 467 | - } |
|
| 468 | - else |
|
| 468 | + } else |
|
| 469 | 469 | { |
| 470 | 470 | return false; |
| 471 | 471 | } |
@@ -484,14 +484,12 @@ discard block |
||
| 484 | 484 | if ($return_sql) |
| 485 | 485 | { |
| 486 | 486 | return $sql; |
| 487 | - } |
|
| 488 | - else |
|
| 487 | + } else |
|
| 489 | 488 | { |
| 490 | 489 | if (self::query($sql)) |
| 491 | 490 | { |
| 492 | 491 | return mysqli_affected_rows(self::$links[self::$link_name]['conn']); |
| 493 | - } |
|
| 494 | - else |
|
| 492 | + } else |
|
| 495 | 493 | { |
| 496 | 494 | return false; |
| 497 | 495 | } |
@@ -518,8 +516,7 @@ discard block |
||
| 518 | 516 | if(is_array($val)===true) |
| 519 | 517 | { |
| 520 | 518 | $arrays[$key] = self::strsafe($val); |
| 521 | - } |
|
| 522 | - else |
|
| 519 | + } else |
|
| 523 | 520 | { |
| 524 | 521 | //先去掉转义,避免下面重复转义了 |
| 525 | 522 | $val = stripslashes($val); |
@@ -531,8 +528,7 @@ discard block |
||
| 531 | 528 | } |
| 532 | 529 | } |
| 533 | 530 | return $arrays; |
| 534 | - } |
|
| 535 | - else |
|
| 531 | + } else |
|
| 536 | 532 | { |
| 537 | 533 | $array = stripslashes($array); |
| 538 | 534 | $array = addslashes($array); |
@@ -29,7 +29,7 @@ |
||
| 29 | 29 | ini_set('memory_limit', '1024M'); |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | -if( PHP_SAPI != 'cli' ) |
|
| 32 | +if (PHP_SAPI != 'cli') |
|
| 33 | 33 | { |
| 34 | 34 | exit("You must run the CLI environment\n"); |
| 35 | 35 | } |
@@ -14,7 +14,7 @@ discard block |
||
| 14 | 14 | //---------------------------------- |
| 15 | 15 | |
| 16 | 16 | // 严格开发模式 |
| 17 | -error_reporting( E_ALL ); |
|
| 17 | +error_reporting(E_ALL); |
|
| 18 | 18 | //ini_set('display_errors', 1); |
| 19 | 19 | |
| 20 | 20 | // 永不超时 |
@@ -26,7 +26,7 @@ discard block |
||
| 26 | 26 | ini_set('memory_limit', '1024M'); |
| 27 | 27 | } |
| 28 | 28 | |
| 29 | -if( PHP_SAPI != 'cli' ) |
|
| 29 | +if (PHP_SAPI != 'cli') |
|
| 30 | 30 | { |
| 31 | 31 | exit("You must run the CLI environment\n"); |
| 32 | 32 | } |
@@ -35,7 +35,7 @@ discard block |
||
| 35 | 35 | date_default_timezone_set('Asia/Shanghai'); |
| 36 | 36 | |
| 37 | 37 | // 引入PATH_DATA |
| 38 | -require_once __DIR__ . '/constants.php'; |
|
| 38 | +require_once __DIR__.'/constants.php'; |
|
| 39 | 39 | // 核心库目录 |
| 40 | 40 | define('CORE', dirname(__FILE__)); |
| 41 | 41 | define('PATH_ROOT', CORE."/../"); |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | define('PATH_LIBRARY', CORE."/../library"); |
| 44 | 44 | |
| 45 | 45 | // 系统配置 |
| 46 | -if( file_exists( PATH_ROOT."/config/inc_config.php" ) ) |
|
| 46 | +if (file_exists(PATH_ROOT."/config/inc_config.php")) |
|
| 47 | 47 | { |
| 48 | 48 | require PATH_ROOT."/config/inc_config.php"; |
| 49 | 49 | } |
@@ -139,8 +139,8 @@ |
||
| 139 | 139 | } |
| 140 | 140 | |
| 141 | 141 | /** |
| 142 | - * 获取默认配置 |
|
| 143 | - */ |
|
| 142 | + * 获取默认配置 |
|
| 143 | + */ |
|
| 144 | 144 | protected static function _get_default_config() |
| 145 | 145 | { |
| 146 | 146 | if (empty(self::$configs['default'])) |
@@ -23,7 +23,7 @@ discard block |
||
| 23 | 23 | /** |
| 24 | 24 | * redis链接标识符号 |
| 25 | 25 | */ |
| 26 | - protected static $redis = NULL; |
|
| 26 | + protected static $redis = NULL; |
|
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | 29 | * redis配置数组 |
@@ -41,7 +41,7 @@ discard block |
||
| 41 | 41 | |
| 42 | 42 | public static function init() |
| 43 | 43 | { |
| 44 | - if ( ! extension_loaded('redis')) |
|
| 44 | + if (!extension_loaded('redis')) |
|
| 45 | 45 | { |
| 46 | 46 | self::$error = 'The redis extension was not found'; |
| 47 | 47 | return false; |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | self::$links[self::$link_name] = new Redis(); |
| 58 | 58 | if (strstr($config['host'], '.sock')) |
| 59 | 59 | { |
| 60 | - if ( ! self::$links[self::$link_name]->connect($config['host'])) |
|
| 60 | + if (!self::$links[self::$link_name]->connect($config['host'])) |
|
| 61 | 61 | { |
| 62 | 62 | self::$error = 'Unable to connect to redis server'; |
| 63 | 63 | unset(self::$links[self::$link_name]); |
@@ -66,7 +66,7 @@ discard block |
||
| 66 | 66 | } |
| 67 | 67 | else |
| 68 | 68 | { |
| 69 | - if ( ! self::$links[self::$link_name]->connect($config['host'], $config['port'], $config['timeout'])) |
|
| 69 | + if (!self::$links[self::$link_name]->connect($config['host'], $config['port'], $config['timeout'])) |
|
| 70 | 70 | { |
| 71 | 71 | self::$error = 'Unable to connect to redis server'; |
| 72 | 72 | unset(self::$links[self::$link_name]); |
@@ -77,7 +77,7 @@ discard block |
||
| 77 | 77 | // 验证 |
| 78 | 78 | if ($config['pass']) |
| 79 | 79 | { |
| 80 | - if ( ! self::$links[self::$link_name]->auth($config['pass'])) |
|
| 80 | + if (!self::$links[self::$link_name]->auth($config['pass'])) |
|
| 81 | 81 | { |
| 82 | 82 | self::$error = 'Redis Server authentication failed'; |
| 83 | 83 | unset(self::$links[self::$link_name]); |
@@ -98,9 +98,9 @@ discard block |
||
| 98 | 98 | |
| 99 | 99 | public static function clear_link() |
| 100 | 100 | { |
| 101 | - if(self::$links) |
|
| 101 | + if (self::$links) |
|
| 102 | 102 | { |
| 103 | - foreach(self::$links as $k=>$v) |
|
| 103 | + foreach (self::$links as $k=>$v) |
|
| 104 | 104 | { |
| 105 | 105 | $v->close(); |
| 106 | 106 | unset(self::$links[$k]); |
@@ -147,7 +147,7 @@ discard block |
||
| 147 | 147 | { |
| 148 | 148 | if (!is_array($GLOBALS['config']['redis'])) |
| 149 | 149 | { |
| 150 | - exit('cls_redis.php _get_default_config()' . '没有redis配置'); |
|
| 150 | + exit('cls_redis.php _get_default_config()'.'没有redis配置'); |
|
| 151 | 151 | // You not set a config array for connect\nPlease check the configuration file config/inc_config.php |
| 152 | 152 | } |
| 153 | 153 | self::$configs['default'] = $GLOBALS['config']['redis']; |
@@ -170,7 +170,7 @@ discard block |
||
| 170 | 170 | self::init(); |
| 171 | 171 | try |
| 172 | 172 | { |
| 173 | - if ( self::$links[self::$link_name] ) |
|
| 173 | + if (self::$links[self::$link_name]) |
|
| 174 | 174 | { |
| 175 | 175 | if ($expire > 0) |
| 176 | 176 | { |
@@ -213,7 +213,7 @@ discard block |
||
| 213 | 213 | self::init(); |
| 214 | 214 | try |
| 215 | 215 | { |
| 216 | - if ( self::$links[self::$link_name] ) |
|
| 216 | + if (self::$links[self::$link_name]) |
|
| 217 | 217 | { |
| 218 | 218 | if ($expire > 0) |
| 219 | 219 | { |
@@ -267,7 +267,7 @@ discard block |
||
| 267 | 267 | self::init(); |
| 268 | 268 | try |
| 269 | 269 | { |
| 270 | - if ( self::$links[self::$link_name] ) |
|
| 270 | + if (self::$links[self::$link_name]) |
|
| 271 | 271 | { |
| 272 | 272 | $key = "Lock:{$name}"; |
| 273 | 273 | while (true) |
@@ -320,7 +320,7 @@ discard block |
||
| 320 | 320 | self::init(); |
| 321 | 321 | try |
| 322 | 322 | { |
| 323 | - if ( self::$links[self::$link_name] ) |
|
| 323 | + if (self::$links[self::$link_name]) |
|
| 324 | 324 | { |
| 325 | 325 | return self::$links[self::$link_name]->get($key); |
| 326 | 326 | } |
@@ -353,7 +353,7 @@ discard block |
||
| 353 | 353 | self::init(); |
| 354 | 354 | try |
| 355 | 355 | { |
| 356 | - if ( self::$links[self::$link_name] ) |
|
| 356 | + if (self::$links[self::$link_name]) |
|
| 357 | 357 | { |
| 358 | 358 | return self::$links[self::$link_name]->del($key); |
| 359 | 359 | } |
@@ -393,7 +393,7 @@ discard block |
||
| 393 | 393 | |
| 394 | 394 | try |
| 395 | 395 | { |
| 396 | - if ( self::$links[self::$link_name] ) |
|
| 396 | + if (self::$links[self::$link_name]) |
|
| 397 | 397 | { |
| 398 | 398 | $type = self::$links[self::$link_name]->type($key); |
| 399 | 399 | if (isset($types[$type])) |
@@ -431,7 +431,7 @@ discard block |
||
| 431 | 431 | self::init(); |
| 432 | 432 | try |
| 433 | 433 | { |
| 434 | - if ( self::$links[self::$link_name] ) |
|
| 434 | + if (self::$links[self::$link_name]) |
|
| 435 | 435 | { |
| 436 | 436 | if (empty($integer)) |
| 437 | 437 | { |
@@ -472,7 +472,7 @@ discard block |
||
| 472 | 472 | self::init(); |
| 473 | 473 | try |
| 474 | 474 | { |
| 475 | - if ( self::$links[self::$link_name] ) |
|
| 475 | + if (self::$links[self::$link_name]) |
|
| 476 | 476 | { |
| 477 | 477 | if (empty($integer)) |
| 478 | 478 | { |
@@ -513,7 +513,7 @@ discard block |
||
| 513 | 513 | self::init(); |
| 514 | 514 | try |
| 515 | 515 | { |
| 516 | - if ( self::$links[self::$link_name] ) |
|
| 516 | + if (self::$links[self::$link_name]) |
|
| 517 | 517 | { |
| 518 | 518 | return self::$links[self::$link_name]->append($key, $value); |
| 519 | 519 | } |
@@ -548,7 +548,7 @@ discard block |
||
| 548 | 548 | self::init(); |
| 549 | 549 | try |
| 550 | 550 | { |
| 551 | - if ( self::$links[self::$link_name] ) |
|
| 551 | + if (self::$links[self::$link_name]) |
|
| 552 | 552 | { |
| 553 | 553 | return self::$links[self::$link_name]->substr($key, $start, $end); |
| 554 | 554 | } |
@@ -581,7 +581,7 @@ discard block |
||
| 581 | 581 | self::init(); |
| 582 | 582 | try |
| 583 | 583 | { |
| 584 | - if ( self::$links[self::$link_name] ) |
|
| 584 | + if (self::$links[self::$link_name]) |
|
| 585 | 585 | { |
| 586 | 586 | return self::$links[self::$link_name]->select($index); |
| 587 | 587 | } |
@@ -614,7 +614,7 @@ discard block |
||
| 614 | 614 | self::init(); |
| 615 | 615 | try |
| 616 | 616 | { |
| 617 | - if ( self::$links[self::$link_name] ) |
|
| 617 | + if (self::$links[self::$link_name]) |
|
| 618 | 618 | { |
| 619 | 619 | return self::$links[self::$link_name]->dbsize(); |
| 620 | 620 | } |
@@ -646,7 +646,7 @@ discard block |
||
| 646 | 646 | self::init(); |
| 647 | 647 | try |
| 648 | 648 | { |
| 649 | - if ( self::$links[self::$link_name] ) |
|
| 649 | + if (self::$links[self::$link_name]) |
|
| 650 | 650 | { |
| 651 | 651 | return self::$links[self::$link_name]->flushdb(); |
| 652 | 652 | } |
@@ -678,7 +678,7 @@ discard block |
||
| 678 | 678 | self::init(); |
| 679 | 679 | try |
| 680 | 680 | { |
| 681 | - if ( self::$links[self::$link_name] ) |
|
| 681 | + if (self::$links[self::$link_name]) |
|
| 682 | 682 | { |
| 683 | 683 | return self::$links[self::$link_name]->flushall(); |
| 684 | 684 | } |
@@ -711,7 +711,7 @@ discard block |
||
| 711 | 711 | self::init(); |
| 712 | 712 | try |
| 713 | 713 | { |
| 714 | - if ( self::$links[self::$link_name] ) |
|
| 714 | + if (self::$links[self::$link_name]) |
|
| 715 | 715 | { |
| 716 | 716 | if (!$is_bgsave) |
| 717 | 717 | { |
@@ -750,7 +750,7 @@ discard block |
||
| 750 | 750 | self::init(); |
| 751 | 751 | try |
| 752 | 752 | { |
| 753 | - if ( self::$links[self::$link_name] ) |
|
| 753 | + if (self::$links[self::$link_name]) |
|
| 754 | 754 | { |
| 755 | 755 | return self::$links[self::$link_name]->info(); |
| 756 | 756 | } |
@@ -782,7 +782,7 @@ discard block |
||
| 782 | 782 | self::init(); |
| 783 | 783 | try |
| 784 | 784 | { |
| 785 | - if ( self::$links[self::$link_name] ) |
|
| 785 | + if (self::$links[self::$link_name]) |
|
| 786 | 786 | { |
| 787 | 787 | if (!empty($len)) |
| 788 | 788 | { |
@@ -821,7 +821,7 @@ discard block |
||
| 821 | 821 | self::init(); |
| 822 | 822 | try |
| 823 | 823 | { |
| 824 | - if ( self::$links[self::$link_name] ) |
|
| 824 | + if (self::$links[self::$link_name]) |
|
| 825 | 825 | { |
| 826 | 826 | return self::$links[self::$link_name]->lastsave(); |
| 827 | 827 | } |
@@ -855,7 +855,7 @@ discard block |
||
| 855 | 855 | self::init(); |
| 856 | 856 | try |
| 857 | 857 | { |
| 858 | - if ( self::$links[self::$link_name] ) |
|
| 858 | + if (self::$links[self::$link_name]) |
|
| 859 | 859 | { |
| 860 | 860 | return self::$links[self::$link_name]->lpush($key, $value); |
| 861 | 861 | } |
@@ -889,7 +889,7 @@ discard block |
||
| 889 | 889 | self::init(); |
| 890 | 890 | try |
| 891 | 891 | { |
| 892 | - if ( self::$links[self::$link_name] ) |
|
| 892 | + if (self::$links[self::$link_name]) |
|
| 893 | 893 | { |
| 894 | 894 | return self::$links[self::$link_name]->rpush($key, $value); |
| 895 | 895 | } |
@@ -922,7 +922,7 @@ discard block |
||
| 922 | 922 | self::init(); |
| 923 | 923 | try |
| 924 | 924 | { |
| 925 | - if ( self::$links[self::$link_name] ) |
|
| 925 | + if (self::$links[self::$link_name]) |
|
| 926 | 926 | { |
| 927 | 927 | return self::$links[self::$link_name]->lpop($key); |
| 928 | 928 | } |
@@ -955,7 +955,7 @@ discard block |
||
| 955 | 955 | self::init(); |
| 956 | 956 | try |
| 957 | 957 | { |
| 958 | - if ( self::$links[self::$link_name] ) |
|
| 958 | + if (self::$links[self::$link_name]) |
|
| 959 | 959 | { |
| 960 | 960 | return self::$links[self::$link_name]->rpop($key); |
| 961 | 961 | } |
@@ -988,7 +988,7 @@ discard block |
||
| 988 | 988 | self::init(); |
| 989 | 989 | try |
| 990 | 990 | { |
| 991 | - if ( self::$links[self::$link_name] ) |
|
| 991 | + if (self::$links[self::$link_name]) |
|
| 992 | 992 | { |
| 993 | 993 | return self::$links[self::$link_name]->lSize($key); |
| 994 | 994 | } |
@@ -1022,7 +1022,7 @@ discard block |
||
| 1022 | 1022 | self::init(); |
| 1023 | 1023 | try |
| 1024 | 1024 | { |
| 1025 | - if ( self::$links[self::$link_name] ) |
|
| 1025 | + if (self::$links[self::$link_name]) |
|
| 1026 | 1026 | { |
| 1027 | 1027 | return self::$links[self::$link_name]->lget($key, $index); |
| 1028 | 1028 | } |
@@ -1057,7 +1057,7 @@ discard block |
||
| 1057 | 1057 | self::init(); |
| 1058 | 1058 | try |
| 1059 | 1059 | { |
| 1060 | - if ( self::$links[self::$link_name] ) |
|
| 1060 | + if (self::$links[self::$link_name]) |
|
| 1061 | 1061 | { |
| 1062 | 1062 | return self::$links[self::$link_name]->lRange($key, $start, $end); |
| 1063 | 1063 | } |
@@ -1133,7 +1133,7 @@ discard block |
||
| 1133 | 1133 | self::init(); |
| 1134 | 1134 | try |
| 1135 | 1135 | { |
| 1136 | - if ( self::$links[self::$link_name] ) |
|
| 1136 | + if (self::$links[self::$link_name]) |
|
| 1137 | 1137 | { |
| 1138 | 1138 | return self::$links[self::$link_name]->keys($key); |
| 1139 | 1139 | } |
@@ -1168,7 +1168,7 @@ discard block |
||
| 1168 | 1168 | self::init(); |
| 1169 | 1169 | try |
| 1170 | 1170 | { |
| 1171 | - if ( self::$links[self::$link_name] ) |
|
| 1171 | + if (self::$links[self::$link_name]) |
|
| 1172 | 1172 | { |
| 1173 | 1173 | return self::$links[self::$link_name]->ttl($key); |
| 1174 | 1174 | } |
@@ -1202,7 +1202,7 @@ discard block |
||
| 1202 | 1202 | self::init(); |
| 1203 | 1203 | try |
| 1204 | 1204 | { |
| 1205 | - if ( self::$links[self::$link_name] ) |
|
| 1205 | + if (self::$links[self::$link_name]) |
|
| 1206 | 1206 | { |
| 1207 | 1207 | return self::$links[self::$link_name]->expire($key, $expire); |
| 1208 | 1208 | } |
@@ -1235,7 +1235,7 @@ discard block |
||
| 1235 | 1235 | self::init(); |
| 1236 | 1236 | try |
| 1237 | 1237 | { |
| 1238 | - if ( self::$links[self::$link_name] ) |
|
| 1238 | + if (self::$links[self::$link_name]) |
|
| 1239 | 1239 | { |
| 1240 | 1240 | return self::$links[self::$link_name]->exists($key); |
| 1241 | 1241 | } |
@@ -63,8 +63,7 @@ discard block |
||
| 63 | 63 | unset(self::$links[self::$link_name]); |
| 64 | 64 | return false; |
| 65 | 65 | } |
| 66 | - } |
|
| 67 | - else |
|
| 66 | + } else |
|
| 68 | 67 | { |
| 69 | 68 | if ( ! self::$links[self::$link_name]->connect($config['host'], $config['port'], $config['timeout'])) |
| 70 | 69 | { |
@@ -114,8 +113,7 @@ discard block |
||
| 114 | 113 | if (!empty($config)) |
| 115 | 114 | { |
| 116 | 115 | self::$configs[self::$link_name] = $config; |
| 117 | - } |
|
| 118 | - else |
|
| 116 | + } else |
|
| 119 | 117 | { |
| 120 | 118 | if (empty(self::$configs[self::$link_name])) |
| 121 | 119 | { |
@@ -175,14 +173,12 @@ discard block |
||
| 175 | 173 | if ($expire > 0) |
| 176 | 174 | { |
| 177 | 175 | return self::$links[self::$link_name]->setex($key, $expire, $value); |
| 178 | - } |
|
| 179 | - else |
|
| 176 | + } else |
|
| 180 | 177 | { |
| 181 | 178 | return self::$links[self::$link_name]->set($key, $value); |
| 182 | 179 | } |
| 183 | 180 | } |
| 184 | - } |
|
| 185 | - catch (Exception $e) |
|
| 181 | + } catch (Exception $e) |
|
| 186 | 182 | { |
| 187 | 183 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
| 188 | 184 | log::warn($msg); |
@@ -223,14 +219,12 @@ discard block |
||
| 223 | 219 | //self::$links[self::$link_name]->expire($key, $expire); |
| 224 | 220 | //self::$links[self::$link_name]->exec(); |
| 225 | 221 | //return true; |
| 226 | - } |
|
| 227 | - else |
|
| 222 | + } else |
|
| 228 | 223 | { |
| 229 | 224 | return self::$links[self::$link_name]->setnx($key, $value); |
| 230 | 225 | } |
| 231 | 226 | } |
| 232 | - } |
|
| 233 | - catch (Exception $e) |
|
| 227 | + } catch (Exception $e) |
|
| 234 | 228 | { |
| 235 | 229 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
| 236 | 230 | log::warn($msg); |
@@ -284,8 +278,7 @@ discard block |
||
| 284 | 278 | } |
| 285 | 279 | return false; |
| 286 | 280 | } |
| 287 | - } |
|
| 288 | - catch (Exception $e) |
|
| 281 | + } catch (Exception $e) |
|
| 289 | 282 | { |
| 290 | 283 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
| 291 | 284 | log::warn($msg); |
@@ -324,8 +317,7 @@ discard block |
||
| 324 | 317 | { |
| 325 | 318 | return self::$links[self::$link_name]->get($key); |
| 326 | 319 | } |
| 327 | - } |
|
| 328 | - catch (Exception $e) |
|
| 320 | + } catch (Exception $e) |
|
| 329 | 321 | { |
| 330 | 322 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
| 331 | 323 | log::warn($msg); |
@@ -357,8 +349,7 @@ discard block |
||
| 357 | 349 | { |
| 358 | 350 | return self::$links[self::$link_name]->del($key); |
| 359 | 351 | } |
| 360 | - } |
|
| 361 | - catch (Exception $e) |
|
| 352 | + } catch (Exception $e) |
|
| 362 | 353 | { |
| 363 | 354 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
| 364 | 355 | log::warn($msg); |
@@ -401,8 +392,7 @@ discard block |
||
| 401 | 392 | return $types[$type]; |
| 402 | 393 | } |
| 403 | 394 | } |
| 404 | - } |
|
| 405 | - catch (Exception $e) |
|
| 395 | + } catch (Exception $e) |
|
| 406 | 396 | { |
| 407 | 397 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
| 408 | 398 | log::warn($msg); |
@@ -436,14 +426,12 @@ discard block |
||
| 436 | 426 | if (empty($integer)) |
| 437 | 427 | { |
| 438 | 428 | return self::$links[self::$link_name]->incr($key); |
| 439 | - } |
|
| 440 | - else |
|
| 429 | + } else |
|
| 441 | 430 | { |
| 442 | 431 | return self::$links[self::$link_name]->incrby($key, $integer); |
| 443 | 432 | } |
| 444 | 433 | } |
| 445 | - } |
|
| 446 | - catch (Exception $e) |
|
| 434 | + } catch (Exception $e) |
|
| 447 | 435 | { |
| 448 | 436 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
| 449 | 437 | log::warn($msg); |
@@ -477,14 +465,12 @@ discard block |
||
| 477 | 465 | if (empty($integer)) |
| 478 | 466 | { |
| 479 | 467 | return self::$links[self::$link_name]->decr($key); |
| 480 | - } |
|
| 481 | - else |
|
| 468 | + } else |
|
| 482 | 469 | { |
| 483 | 470 | return self::$links[self::$link_name]->decrby($key, $integer); |
| 484 | 471 | } |
| 485 | 472 | } |
| 486 | - } |
|
| 487 | - catch (Exception $e) |
|
| 473 | + } catch (Exception $e) |
|
| 488 | 474 | { |
| 489 | 475 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
| 490 | 476 | log::warn($msg); |
@@ -517,8 +503,7 @@ discard block |
||
| 517 | 503 | { |
| 518 | 504 | return self::$links[self::$link_name]->append($key, $value); |
| 519 | 505 | } |
| 520 | - } |
|
| 521 | - catch (Exception $e) |
|
| 506 | + } catch (Exception $e) |
|
| 522 | 507 | { |
| 523 | 508 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
| 524 | 509 | log::warn($msg); |
@@ -552,8 +537,7 @@ discard block |
||
| 552 | 537 | { |
| 553 | 538 | return self::$links[self::$link_name]->substr($key, $start, $end); |
| 554 | 539 | } |
| 555 | - } |
|
| 556 | - catch (Exception $e) |
|
| 540 | + } catch (Exception $e) |
|
| 557 | 541 | { |
| 558 | 542 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
| 559 | 543 | log::warn($msg); |
@@ -585,8 +569,7 @@ discard block |
||
| 585 | 569 | { |
| 586 | 570 | return self::$links[self::$link_name]->select($index); |
| 587 | 571 | } |
| 588 | - } |
|
| 589 | - catch (Exception $e) |
|
| 572 | + } catch (Exception $e) |
|
| 590 | 573 | { |
| 591 | 574 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
| 592 | 575 | log::warn($msg); |
@@ -618,8 +601,7 @@ discard block |
||
| 618 | 601 | { |
| 619 | 602 | return self::$links[self::$link_name]->dbsize(); |
| 620 | 603 | } |
| 621 | - } |
|
| 622 | - catch (Exception $e) |
|
| 604 | + } catch (Exception $e) |
|
| 623 | 605 | { |
| 624 | 606 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
| 625 | 607 | log::warn($msg); |
@@ -650,8 +632,7 @@ discard block |
||
| 650 | 632 | { |
| 651 | 633 | return self::$links[self::$link_name]->flushdb(); |
| 652 | 634 | } |
| 653 | - } |
|
| 654 | - catch (Exception $e) |
|
| 635 | + } catch (Exception $e) |
|
| 655 | 636 | { |
| 656 | 637 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
| 657 | 638 | log::warn($msg); |
@@ -682,8 +663,7 @@ discard block |
||
| 682 | 663 | { |
| 683 | 664 | return self::$links[self::$link_name]->flushall(); |
| 684 | 665 | } |
| 685 | - } |
|
| 686 | - catch (Exception $e) |
|
| 666 | + } catch (Exception $e) |
|
| 687 | 667 | { |
| 688 | 668 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
| 689 | 669 | log::warn($msg); |
@@ -716,14 +696,12 @@ discard block |
||
| 716 | 696 | if (!$is_bgsave) |
| 717 | 697 | { |
| 718 | 698 | return self::$links[self::$link_name]->save(); |
| 719 | - } |
|
| 720 | - else |
|
| 699 | + } else |
|
| 721 | 700 | { |
| 722 | 701 | return self::$links[self::$link_name]->bgsave(); |
| 723 | 702 | } |
| 724 | 703 | } |
| 725 | - } |
|
| 726 | - catch (Exception $e) |
|
| 704 | + } catch (Exception $e) |
|
| 727 | 705 | { |
| 728 | 706 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
| 729 | 707 | log::warn($msg); |
@@ -754,8 +732,7 @@ discard block |
||
| 754 | 732 | { |
| 755 | 733 | return self::$links[self::$link_name]->info(); |
| 756 | 734 | } |
| 757 | - } |
|
| 758 | - catch (Exception $e) |
|
| 735 | + } catch (Exception $e) |
|
| 759 | 736 | { |
| 760 | 737 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
| 761 | 738 | log::warn($msg); |
@@ -787,14 +764,12 @@ discard block |
||
| 787 | 764 | if (!empty($len)) |
| 788 | 765 | { |
| 789 | 766 | return $redis->slowlog($command, $len); |
| 790 | - } |
|
| 791 | - else |
|
| 767 | + } else |
|
| 792 | 768 | { |
| 793 | 769 | return $redis->slowlog($command); |
| 794 | 770 | } |
| 795 | 771 | } |
| 796 | - } |
|
| 797 | - catch (Exception $e) |
|
| 772 | + } catch (Exception $e) |
|
| 798 | 773 | { |
| 799 | 774 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
| 800 | 775 | log::warn($msg); |
@@ -825,8 +800,7 @@ discard block |
||
| 825 | 800 | { |
| 826 | 801 | return self::$links[self::$link_name]->lastsave(); |
| 827 | 802 | } |
| 828 | - } |
|
| 829 | - catch (Exception $e) |
|
| 803 | + } catch (Exception $e) |
|
| 830 | 804 | { |
| 831 | 805 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
| 832 | 806 | log::warn($msg); |
@@ -859,8 +833,7 @@ discard block |
||
| 859 | 833 | { |
| 860 | 834 | return self::$links[self::$link_name]->lpush($key, $value); |
| 861 | 835 | } |
| 862 | - } |
|
| 863 | - catch (Exception $e) |
|
| 836 | + } catch (Exception $e) |
|
| 864 | 837 | { |
| 865 | 838 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
| 866 | 839 | log::warn($msg); |
@@ -893,8 +866,7 @@ discard block |
||
| 893 | 866 | { |
| 894 | 867 | return self::$links[self::$link_name]->rpush($key, $value); |
| 895 | 868 | } |
| 896 | - } |
|
| 897 | - catch (Exception $e) |
|
| 869 | + } catch (Exception $e) |
|
| 898 | 870 | { |
| 899 | 871 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
| 900 | 872 | log::warn($msg); |
@@ -926,8 +898,7 @@ discard block |
||
| 926 | 898 | { |
| 927 | 899 | return self::$links[self::$link_name]->lpop($key); |
| 928 | 900 | } |
| 929 | - } |
|
| 930 | - catch (Exception $e) |
|
| 901 | + } catch (Exception $e) |
|
| 931 | 902 | { |
| 932 | 903 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
| 933 | 904 | log::warn($msg); |
@@ -959,8 +930,7 @@ discard block |
||
| 959 | 930 | { |
| 960 | 931 | return self::$links[self::$link_name]->rpop($key); |
| 961 | 932 | } |
| 962 | - } |
|
| 963 | - catch (Exception $e) |
|
| 933 | + } catch (Exception $e) |
|
| 964 | 934 | { |
| 965 | 935 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
| 966 | 936 | log::warn($msg); |
@@ -992,8 +962,7 @@ discard block |
||
| 992 | 962 | { |
| 993 | 963 | return self::$links[self::$link_name]->lSize($key); |
| 994 | 964 | } |
| 995 | - } |
|
| 996 | - catch (Exception $e) |
|
| 965 | + } catch (Exception $e) |
|
| 997 | 966 | { |
| 998 | 967 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
| 999 | 968 | log::warn($msg); |
@@ -1026,8 +995,7 @@ discard block |
||
| 1026 | 995 | { |
| 1027 | 996 | return self::$links[self::$link_name]->lget($key, $index); |
| 1028 | 997 | } |
| 1029 | - } |
|
| 1030 | - catch (Exception $e) |
|
| 998 | + } catch (Exception $e) |
|
| 1031 | 999 | { |
| 1032 | 1000 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
| 1033 | 1001 | log::warn($msg); |
@@ -1061,8 +1029,7 @@ discard block |
||
| 1061 | 1029 | { |
| 1062 | 1030 | return self::$links[self::$link_name]->lRange($key, $start, $end); |
| 1063 | 1031 | } |
| 1064 | - } |
|
| 1065 | - catch (Exception $e) |
|
| 1032 | + } catch (Exception $e) |
|
| 1066 | 1033 | { |
| 1067 | 1034 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
| 1068 | 1035 | log::warn($msg); |
@@ -1105,8 +1072,7 @@ discard block |
||
| 1105 | 1072 | $list[] = $data; |
| 1106 | 1073 | } |
| 1107 | 1074 | return $list; |
| 1108 | - } |
|
| 1109 | - else |
|
| 1075 | + } else |
|
| 1110 | 1076 | { |
| 1111 | 1077 | // 没有数据返回NULL |
| 1112 | 1078 | return NULL; |
@@ -1137,8 +1103,7 @@ discard block |
||
| 1137 | 1103 | { |
| 1138 | 1104 | return self::$links[self::$link_name]->keys($key); |
| 1139 | 1105 | } |
| 1140 | - } |
|
| 1141 | - catch (Exception $e) |
|
| 1106 | + } catch (Exception $e) |
|
| 1142 | 1107 | { |
| 1143 | 1108 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
| 1144 | 1109 | log::warn($msg); |
@@ -1172,8 +1137,7 @@ discard block |
||
| 1172 | 1137 | { |
| 1173 | 1138 | return self::$links[self::$link_name]->ttl($key); |
| 1174 | 1139 | } |
| 1175 | - } |
|
| 1176 | - catch (Exception $e) |
|
| 1140 | + } catch (Exception $e) |
|
| 1177 | 1141 | { |
| 1178 | 1142 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
| 1179 | 1143 | log::warn($msg); |
@@ -1206,8 +1170,7 @@ discard block |
||
| 1206 | 1170 | { |
| 1207 | 1171 | return self::$links[self::$link_name]->expire($key, $expire); |
| 1208 | 1172 | } |
| 1209 | - } |
|
| 1210 | - catch (Exception $e) |
|
| 1173 | + } catch (Exception $e) |
|
| 1211 | 1174 | { |
| 1212 | 1175 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
| 1213 | 1176 | log::warn($msg); |
@@ -1239,8 +1202,7 @@ discard block |
||
| 1239 | 1202 | { |
| 1240 | 1203 | return self::$links[self::$link_name]->exists($key); |
| 1241 | 1204 | } |
| 1242 | - } |
|
| 1243 | - catch (Exception $e) |
|
| 1205 | + } catch (Exception $e) |
|
| 1244 | 1206 | { |
| 1245 | 1207 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
| 1246 | 1208 | log::warn($msg); |
@@ -1303,8 +1265,7 @@ discard block |
||
| 1303 | 1265 | { |
| 1304 | 1266 | return self::$links[self::$link_name]->sadd($key, $value); |
| 1305 | 1267 | } |
| 1306 | - } |
|
| 1307 | - catch (Exception $e) |
|
| 1268 | + } catch (Exception $e) |
|
| 1308 | 1269 | { |
| 1309 | 1270 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
| 1310 | 1271 | log::warn($msg); |
@@ -1336,8 +1297,7 @@ discard block |
||
| 1336 | 1297 | { |
| 1337 | 1298 | return self::$links[self::$link_name]->spop($key); |
| 1338 | 1299 | } |
| 1339 | - } |
|
| 1340 | - catch (Exception $e) |
|
| 1300 | + } catch (Exception $e) |
|
| 1341 | 1301 | { |
| 1342 | 1302 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
| 1343 | 1303 | log::warn($msg); |
@@ -1369,8 +1329,7 @@ discard block |
||
| 1369 | 1329 | { |
| 1370 | 1330 | return self::$links[self::$link_name]->scard($key); |
| 1371 | 1331 | } |
| 1372 | - } |
|
| 1373 | - catch (Exception $e) |
|
| 1332 | + } catch (Exception $e) |
|
| 1374 | 1333 | { |
| 1375 | 1334 | $msg = "PHP Fatal error: Uncaught exception 'RedisException' with message '".$e->getMessage()."'\n"; |
| 1376 | 1335 | log::warn($msg); |
@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | |
| 16 | 16 | namespace phpspider\core; |
| 17 | 17 | // 引入PATH_DATA |
| 18 | -require_once __DIR__ . '/constants.php'; |
|
| 18 | +require_once __DIR__.'/constants.php'; |
|
| 19 | 19 | |
| 20 | 20 | class util |
| 21 | 21 | { |
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | $lock = util::get_file(PATH_DATA."/lock/{$lock_name}.lock"); |
| 46 | 46 | if ($lock) |
| 47 | 47 | { |
| 48 | - $time = time() - $lock; |
|
| 48 | + $time = time()-$lock; |
|
| 49 | 49 | // 还没到10分钟,说明进程还活着 |
| 50 | 50 | if ($time < $lock_timeout) |
| 51 | 51 | { |
@@ -64,42 +64,42 @@ discard block |
||
| 64 | 64 | |
| 65 | 65 | public static function time2second($time, $is_log = true) |
| 66 | 66 | { |
| 67 | - if(is_numeric($time)) |
|
| 67 | + if (is_numeric($time)) |
|
| 68 | 68 | { |
| 69 | 69 | $value = array( |
| 70 | 70 | "years" => 0, "days" => 0, "hours" => 0, |
| 71 | 71 | "minutes" => 0, "seconds" => 0, |
| 72 | 72 | ); |
| 73 | - if($time >= 31556926) |
|
| 73 | + if ($time >= 31556926) |
|
| 74 | 74 | { |
| 75 | - $value["years"] = floor($time/31556926); |
|
| 76 | - $time = ($time%31556926); |
|
| 75 | + $value["years"] = floor($time / 31556926); |
|
| 76 | + $time = ($time % 31556926); |
|
| 77 | 77 | } |
| 78 | - if($time >= 86400) |
|
| 78 | + if ($time >= 86400) |
|
| 79 | 79 | { |
| 80 | - $value["days"] = floor($time/86400); |
|
| 81 | - $time = ($time%86400); |
|
| 80 | + $value["days"] = floor($time / 86400); |
|
| 81 | + $time = ($time % 86400); |
|
| 82 | 82 | } |
| 83 | - if($time >= 3600) |
|
| 83 | + if ($time >= 3600) |
|
| 84 | 84 | { |
| 85 | - $value["hours"] = floor($time/3600); |
|
| 86 | - $time = ($time%3600); |
|
| 85 | + $value["hours"] = floor($time / 3600); |
|
| 86 | + $time = ($time % 3600); |
|
| 87 | 87 | } |
| 88 | - if($time >= 60) |
|
| 88 | + if ($time >= 60) |
|
| 89 | 89 | { |
| 90 | - $value["minutes"] = floor($time/60); |
|
| 91 | - $time = ($time%60); |
|
| 90 | + $value["minutes"] = floor($time / 60); |
|
| 91 | + $time = ($time % 60); |
|
| 92 | 92 | } |
| 93 | 93 | $value["seconds"] = floor($time); |
| 94 | 94 | //return (array) $value; |
| 95 | 95 | //$t = $value["years"] ."y ". $value["days"] ."d ". $value["hours"] ."h ". $value["minutes"] ."m ".$value["seconds"]."s"; |
| 96 | 96 | if ($is_log) |
| 97 | 97 | { |
| 98 | - $t = $value["days"] ."d ". $value["hours"] ."h ". $value["minutes"] ."m ".$value["seconds"]."s"; |
|
| 98 | + $t = $value["days"]."d ".$value["hours"]."h ".$value["minutes"]."m ".$value["seconds"]."s"; |
|
| 99 | 99 | } |
| 100 | 100 | else |
| 101 | 101 | { |
| 102 | - $t = $value["days"] ." days ". $value["hours"] ." hours ". $value["minutes"] ." minutes"; |
|
| 102 | + $t = $value["days"]." days ".$value["hours"]." hours ".$value["minutes"]." minutes"; |
|
| 103 | 103 | } |
| 104 | 104 | return $t; |
| 105 | 105 | |
@@ -114,7 +114,7 @@ discard block |
||
| 114 | 114 | { |
| 115 | 115 | if ($day_end === true) $day_end = date('Y-m-d'); |
| 116 | 116 | |
| 117 | - return array_map(function ($time) { |
|
| 117 | + return array_map(function($time) { |
|
| 118 | 118 | return date('Y-m-d', $time); |
| 119 | 119 | }, range(strtotime($day_sta), strtotime($day_end), $range)); |
| 120 | 120 | } |
@@ -129,17 +129,17 @@ discard block |
||
| 129 | 129 | */ |
| 130 | 130 | public static function get_file_line($filepath) |
| 131 | 131 | { |
| 132 | - $line = 0 ; |
|
| 133 | - $fp = fopen($filepath , 'r'); |
|
| 132 | + $line = 0; |
|
| 133 | + $fp = fopen($filepath, 'r'); |
|
| 134 | 134 | if (!$fp) |
| 135 | 135 | { |
| 136 | 136 | return 0; |
| 137 | 137 | } |
| 138 | 138 | //获取文件的一行内容,注意:需要php5才支持该函数; |
| 139 | - while( stream_get_line($fp,8192,"\n") ){ |
|
| 139 | + while (stream_get_line($fp, 8192, "\n")) { |
|
| 140 | 140 | $line++; |
| 141 | 141 | } |
| 142 | - fclose($fp);//关闭文件 |
|
| 142 | + fclose($fp); //关闭文件 |
|
| 143 | 143 | return $line; |
| 144 | 144 | } |
| 145 | 145 | |
@@ -203,7 +203,7 @@ discard block |
||
| 203 | 203 | public static function format_bytes($size) |
| 204 | 204 | { |
| 205 | 205 | $unit = array('b', 'kb', 'mb', 'gb', 'tb', 'pb'); |
| 206 | - return @round($size / pow(1024, ($i = floor(log($size, 1024)))), 2) . ' ' . $unit[$i]; |
|
| 206 | + return @round($size / pow(1024, ($i = floor(log($size, 1024)))), 2).' '.$unit[$i]; |
|
| 207 | 207 | } |
| 208 | 208 | |
| 209 | 209 | /** |
@@ -234,7 +234,7 @@ discard block |
||
| 234 | 234 | public static function rand_num($num = 7) |
| 235 | 235 | { |
| 236 | 236 | $rand = ""; |
| 237 | - for ($i = 0; $i < $num; $i ++) |
|
| 237 | + for ($i = 0; $i < $num; $i++) |
|
| 238 | 238 | { |
| 239 | 239 | $rand .= mt_rand(0, 9); |
| 240 | 240 | } |
@@ -253,7 +253,7 @@ discard block |
||
| 253 | 253 | { |
| 254 | 254 | $chars = 'abcdefghijklmnopqrstuvwxyz0123456789'; |
| 255 | 255 | $string = ""; |
| 256 | - for ($i = 0; $i < $num; $i ++) |
|
| 256 | + for ($i = 0; $i < $num; $i++) |
|
| 257 | 257 | { |
| 258 | 258 | $string .= substr($chars, rand(0, strlen($chars)), 1); |
| 259 | 259 | } |
@@ -284,20 +284,20 @@ discard block |
||
| 284 | 284 | } |
| 285 | 285 | if (count($pinyins) == 0) |
| 286 | 286 | { |
| 287 | - $fp = fopen(PATH_DATA . '/pinyin.dat', 'r'); |
|
| 287 | + $fp = fopen(PATH_DATA.'/pinyin.dat', 'r'); |
|
| 288 | 288 | while (!feof($fp)) |
| 289 | 289 | { |
| 290 | 290 | $line = trim(fgets($fp)); |
| 291 | - $pinyins[$line[0] . $line[1]] = substr($line, 3, strlen($line) - 3); |
|
| 291 | + $pinyins[$line[0].$line[1]] = substr($line, 3, strlen($line)-3); |
|
| 292 | 292 | } |
| 293 | 293 | fclose($fp); |
| 294 | 294 | } |
| 295 | - for ($i = 0; $i < $slen; $i ++) |
|
| 295 | + for ($i = 0; $i < $slen; $i++) |
|
| 296 | 296 | { |
| 297 | 297 | if (ord($str[$i]) > 0x80) |
| 298 | 298 | { |
| 299 | - $c = $str[$i] . $str[$i + 1]; |
|
| 300 | - $i ++; |
|
| 299 | + $c = $str[$i].$str[$i+1]; |
|
| 300 | + $i++; |
|
| 301 | 301 | if (isset($pinyins[$c])) |
| 302 | 302 | { |
| 303 | 303 | if ($ishead == 0) |
@@ -344,7 +344,7 @@ discard block |
||
| 344 | 344 | if (($firstchar_ord >= 65 and $firstchar_ord <= 91) or ($firstchar_ord >= 48 and $firstchar_ord <= 57)) return $s0{0}; |
| 345 | 345 | // $s = iconv("utf-8", "gbk//ignore", $s0); |
| 346 | 346 | $s = mb_convert_encoding($s0, "gbk", "utf-8"); |
| 347 | - $asc = ord($s{0}) * 256 + ord($s{1}) - 65536; |
|
| 347 | + $asc = ord($s{0}) * 256+ord($s{1})-65536; |
|
| 348 | 348 | if ($asc >= -20319 and $asc <= -20284) return "A"; |
| 349 | 349 | if ($asc >= -20283 and $asc <= -19776) return "B"; |
| 350 | 350 | if ($asc >= -19775 and $asc <= -19219) return "C"; |
@@ -382,7 +382,7 @@ discard block |
||
| 382 | 382 | public static function getxtime($day) |
| 383 | 383 | { |
| 384 | 384 | $day = intval($day); |
| 385 | - return mktime(23, 59, 59, date("m"), date("d") - $day, date("y")); |
|
| 385 | + return mktime(23, 59, 59, date("m"), date("d")-$day, date("y")); |
|
| 386 | 386 | } |
| 387 | 387 | |
| 388 | 388 | /** |
@@ -446,7 +446,7 @@ discard block |
||
| 446 | 446 | */ |
| 447 | 447 | public static function path_exists($path) |
| 448 | 448 | { |
| 449 | - $pathinfo = pathinfo($path . '/tmp.txt'); |
|
| 449 | + $pathinfo = pathinfo($path.'/tmp.txt'); |
|
| 450 | 450 | if (!empty($pathinfo['dirname'])) |
| 451 | 451 | { |
| 452 | 452 | if (file_exists($pathinfo['dirname']) === false) |
@@ -474,10 +474,10 @@ discard block |
||
| 474 | 474 | $dh = opendir($dir); |
| 475 | 475 | while ($file = readdir($dh)) |
| 476 | 476 | { |
| 477 | - if($file!="." && $file!="..") |
|
| 477 | + if ($file != "." && $file != "..") |
|
| 478 | 478 | { |
| 479 | 479 | $fullpath = $dir."/".$file; |
| 480 | - if(!is_dir($fullpath)) |
|
| 480 | + if (!is_dir($fullpath)) |
|
| 481 | 481 | { |
| 482 | 482 | unlink($fullpath); |
| 483 | 483 | } |
@@ -490,7 +490,7 @@ discard block |
||
| 490 | 490 | |
| 491 | 491 | closedir($dh); |
| 492 | 492 | //删除当前文件夹: |
| 493 | - if(rmdir($dir)) |
|
| 493 | + if (rmdir($dir)) |
|
| 494 | 494 | { |
| 495 | 495 | return true; |
| 496 | 496 | } |
@@ -519,7 +519,7 @@ discard block |
||
| 519 | 519 | { |
| 520 | 520 | if ($file != '.' && $file != '..') |
| 521 | 521 | { |
| 522 | - $fullpath = $path . '/' . $file; |
|
| 522 | + $fullpath = $path.'/'.$file; |
|
| 523 | 523 | if (is_link($fullpath)) |
| 524 | 524 | { |
| 525 | 525 | return FALSE; |
@@ -603,7 +603,7 @@ discard block |
||
| 603 | 603 | */ |
| 604 | 604 | public static function array_iconv($arr, $from_encoding, $to_encoding) |
| 605 | 605 | { |
| 606 | - eval('$arr = '.iconv($from_encoding, $to_encoding.'//IGNORE', var_export($arr,TRUE)).';'); |
|
| 606 | + eval('$arr = '.iconv($from_encoding, $to_encoding.'//IGNORE', var_export($arr, TRUE)).';'); |
|
| 607 | 607 | return $arr; |
| 608 | 608 | } |
| 609 | 609 | |
@@ -638,7 +638,7 @@ discard block |
||
| 638 | 638 | } |
| 639 | 639 | if (isset($ymd[1])) $dt[1] = $ymd[1]; |
| 640 | 640 | if (isset($ymd[2])) $dt[2] = $ymd[2]; |
| 641 | - if (strlen($dt[0]) == 2) $dt[0] = '20' . $dt[0]; |
|
| 641 | + if (strlen($dt[0]) == 2) $dt[0] = '20'.$dt[0]; |
|
| 642 | 642 | if (isset($ds[1])) |
| 643 | 643 | { |
| 644 | 644 | $hms = explode(":", $ds[1]); |
@@ -675,25 +675,25 @@ discard block |
||
| 675 | 675 | { |
| 676 | 676 | $j = 0; |
| 677 | 677 | $wordscut = ""; |
| 678 | - for ($i = 0; $i < count($info[0]); $i ++) |
|
| 678 | + for ($i = 0; $i < count($info[0]); $i++) |
|
| 679 | 679 | { |
| 680 | 680 | $wordscut .= $info[0][$i]; |
| 681 | 681 | if (ord($info[0][$i]) >= 128) |
| 682 | 682 | { |
| 683 | - $j = $j + 2; |
|
| 683 | + $j = $j+2; |
|
| 684 | 684 | } |
| 685 | 685 | else |
| 686 | 686 | { |
| 687 | - $j = $j + 1; |
|
| 687 | + $j = $j+1; |
|
| 688 | 688 | } |
| 689 | 689 | if ($j >= $length) |
| 690 | 690 | { |
| 691 | - return $wordscut . $etc; |
|
| 691 | + return $wordscut.$etc; |
|
| 692 | 692 | } |
| 693 | 693 | } |
| 694 | 694 | return join('', $info[0]); |
| 695 | 695 | } |
| 696 | - return join("", array_slice($info[0], 0, $length)) . $etc; |
|
| 696 | + return join("", array_slice($info[0], 0, $length)).$etc; |
|
| 697 | 697 | } |
| 698 | 698 | |
| 699 | 699 | /** |
@@ -732,7 +732,7 @@ discard block |
||
| 732 | 732 | } |
| 733 | 733 | if (is_array($header['Location'])) |
| 734 | 734 | { |
| 735 | - return $header['Location'][count($header['Location']) - 1]; |
|
| 735 | + return $header['Location'][count($header['Location'])-1]; |
|
| 736 | 736 | } |
| 737 | 737 | else |
| 738 | 738 | { |
@@ -756,12 +756,12 @@ discard block |
||
| 756 | 756 | { |
| 757 | 757 | $extralen = unpack('v', substr($data, 10, 2)); |
| 758 | 758 | $extralen = $extralen[1]; |
| 759 | - $headerlen += 2 + $extralen; |
|
| 759 | + $headerlen += 2+$extralen; |
|
| 760 | 760 | } |
| 761 | 761 | if ($flags & 8) // Filename |
| 762 | - $headerlen = strpos($data, chr(0), $headerlen) + 1; |
|
| 762 | + $headerlen = strpos($data, chr(0), $headerlen)+1; |
|
| 763 | 763 | if ($flags & 16) // Comment |
| 764 | - $headerlen = strpos($data, chr(0), $headerlen) + 1; |
|
| 764 | + $headerlen = strpos($data, chr(0), $headerlen)+1; |
|
| 765 | 765 | if ($flags & 2) // CRC at end of file |
| 766 | 766 | $headerlen += 2; |
| 767 | 767 | $unpacked = @gzinflate(substr($data, $headerlen)); |
@@ -799,10 +799,10 @@ discard block |
||
| 799 | 799 | // 整数部分 |
| 800 | 800 | if ($num > 0) |
| 801 | 801 | { |
| 802 | - $retval = "元" . $retval; |
|
| 802 | + $retval = "元".$retval; |
|
| 803 | 803 | $f = 1; |
| 804 | 804 | $str = strrev(intval($num)); |
| 805 | - for ($i = 0, $c = strlen($str); $i < $c; $i ++) |
|
| 805 | + for ($i = 0, $c = strlen($str); $i < $c; $i++) |
|
| 806 | 806 | { |
| 807 | 807 | if ($str[$i] > 0) |
| 808 | 808 | { |
@@ -817,16 +817,16 @@ discard block |
||
| 817 | 817 | $out[$i] = $char[$str[$i]]; |
| 818 | 818 | } |
| 819 | 819 | $out[$i] .= $str[$i] != '0' ? $unit[$i % 4] : ''; |
| 820 | - if ($i > 1 and $str[$i] + $str[$i - 1] == 0) |
|
| 820 | + if ($i > 1 and $str[$i]+$str[$i-1] == 0) |
|
| 821 | 821 | { |
| 822 | 822 | $out[$i] = ''; |
| 823 | 823 | } |
| 824 | 824 | if ($i % 4 == 0) |
| 825 | 825 | { |
| 826 | - $out[$i] .= $unit[4 + floor($i / 4)]; |
|
| 826 | + $out[$i] .= $unit[4+floor($i / 4)]; |
|
| 827 | 827 | } |
| 828 | 828 | } |
| 829 | - $retval = join('', array_reverse($out)) . $retval; |
|
| 829 | + $retval = join('', array_reverse($out)).$retval; |
|
| 830 | 830 | } |
| 831 | 831 | return $retval; |
| 832 | 832 | } |
@@ -837,22 +837,22 @@ discard block |
||
| 837 | 837 | switch ($status) |
| 838 | 838 | { |
| 839 | 839 | case 'succ': |
| 840 | - $out = "\033[32m"; // Blue |
|
| 840 | + $out = "\033[32m"; // Blue |
|
| 841 | 841 | break; |
| 842 | 842 | case "error": |
| 843 | - $out = "\033[31m"; // Red |
|
| 843 | + $out = "\033[31m"; // Red |
|
| 844 | 844 | break; |
| 845 | 845 | case "warn": |
| 846 | - $out = "\033[33m"; // Yellow |
|
| 846 | + $out = "\033[33m"; // Yellow |
|
| 847 | 847 | break; |
| 848 | 848 | case "note": |
| 849 | - $out = "\033[34m"; // Green |
|
| 849 | + $out = "\033[34m"; // Green |
|
| 850 | 850 | break; |
| 851 | 851 | case "debug": |
| 852 | - $out = "\033[36m"; // Green |
|
| 852 | + $out = "\033[36m"; // Green |
|
| 853 | 853 | break; |
| 854 | 854 | default: |
| 855 | - $out = "\033[0m"; // info |
|
| 855 | + $out = "\033[0m"; // info |
|
| 856 | 856 | break; |
| 857 | 857 | } |
| 858 | 858 | return $out.$str."\033[0m"; |
@@ -860,19 +860,19 @@ discard block |
||
| 860 | 860 | |
| 861 | 861 | public static function node_to_array($dom, $node) |
| 862 | 862 | { |
| 863 | - if(!is_a( $dom, 'DOMDocument' ) || !is_a( $node, 'DOMNode' )) |
|
| 863 | + if (!is_a($dom, 'DOMDocument') || !is_a($node, 'DOMNode')) |
|
| 864 | 864 | { |
| 865 | 865 | return false; |
| 866 | 866 | } |
| 867 | 867 | |
| 868 | 868 | $array = array(); |
| 869 | 869 | // Discard empty nodes |
| 870 | - $localName = trim( $node->localName ); |
|
| 871 | - if( empty($localName)) |
|
| 870 | + $localName = trim($node->localName); |
|
| 871 | + if (empty($localName)) |
|
| 872 | 872 | { |
| 873 | 873 | return false; |
| 874 | 874 | } |
| 875 | - if( XML_TEXT_NODE == $node->nodeType ) |
|
| 875 | + if (XML_TEXT_NODE == $node->nodeType) |
|
| 876 | 876 | { |
| 877 | 877 | return $node->nodeValue; |
| 878 | 878 | } |
@@ -882,14 +882,14 @@ discard block |
||
| 882 | 882 | } |
| 883 | 883 | foreach ($node->childNodes as $childNode) |
| 884 | 884 | { |
| 885 | - if ( (isset($childNode->childNodes->length) && 1 == $childNode->childNodes->length) && |
|
| 886 | - XML_TEXT_NODE == $childNode->firstChild->nodeType ) |
|
| 885 | + if ((isset($childNode->childNodes->length) && 1 == $childNode->childNodes->length) && |
|
| 886 | + XML_TEXT_NODE == $childNode->firstChild->nodeType) |
|
| 887 | 887 | { |
| 888 | 888 | $array[$childNode->localName] = $childNode->nodeValue; |
| 889 | 889 | } |
| 890 | 890 | else |
| 891 | 891 | { |
| 892 | - if( false !== ($a = self::node_to_array( $dom, $childNode))) |
|
| 892 | + if (false !== ($a = self::node_to_array($dom, $childNode))) |
|
| 893 | 893 | { |
| 894 | 894 | $array[$childNode->localName] = $a; |
| 895 | 895 | } |
@@ -900,7 +900,7 @@ discard block |
||
| 900 | 900 | |
| 901 | 901 | public static function is_win() |
| 902 | 902 | { |
| 903 | - return strtoupper(substr(PHP_OS,0,3))==="WIN"; |
|
| 903 | + return strtoupper(substr(PHP_OS, 0, 3)) === "WIN"; |
|
| 904 | 904 | } |
| 905 | 905 | |
| 906 | 906 | /** |
@@ -96,15 +96,13 @@ discard block |
||
| 96 | 96 | if ($is_log) |
| 97 | 97 | { |
| 98 | 98 | $t = $value["days"] ."d ". $value["hours"] ."h ". $value["minutes"] ."m ".$value["seconds"]."s"; |
| 99 | - } |
|
| 100 | - else |
|
| 99 | + } else |
|
| 101 | 100 | { |
| 102 | 101 | $t = $value["days"] ." days ". $value["hours"] ." hours ". $value["minutes"] ." minutes"; |
| 103 | 102 | } |
| 104 | 103 | return $t; |
| 105 | 104 | |
| 106 | - } |
|
| 107 | - else |
|
| 105 | + } else |
|
| 108 | 106 | { |
| 109 | 107 | return false; |
| 110 | 108 | } |
@@ -112,7 +110,9 @@ discard block |
||
| 112 | 110 | |
| 113 | 111 | public static function get_days($day_sta, $day_end = true, $range = 86400) |
| 114 | 112 | { |
| 115 | - if ($day_end === true) $day_end = date('Y-m-d'); |
|
| 113 | + if ($day_end === true) { |
|
| 114 | + $day_end = date('Y-m-d'); |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | 117 | return array_map(function ($time) { |
| 118 | 118 | return date('Y-m-d', $time); |
@@ -303,22 +303,18 @@ discard block |
||
| 303 | 303 | if ($ishead == 0) |
| 304 | 304 | { |
| 305 | 305 | $restr .= $pinyins[$c]; |
| 306 | - } |
|
| 307 | - else |
|
| 306 | + } else |
|
| 308 | 307 | { |
| 309 | 308 | $restr .= $pinyins[$c][0]; |
| 310 | 309 | } |
| 311 | - } |
|
| 312 | - else |
|
| 310 | + } else |
|
| 313 | 311 | { |
| 314 | 312 | // $restr .= "_"; |
| 315 | 313 | } |
| 316 | - } |
|
| 317 | - else if (preg_match("/[a-z0-9]/i", $str[$i])) |
|
| 314 | + } else if (preg_match("/[a-z0-9]/i", $str[$i])) |
|
| 318 | 315 | { |
| 319 | 316 | $restr .= $str[$i]; |
| 320 | - } |
|
| 321 | - else |
|
| 317 | + } else |
|
| 322 | 318 | { |
| 323 | 319 | // $restr .= "_"; |
| 324 | 320 | } |
@@ -341,33 +337,81 @@ discard block |
||
| 341 | 337 | public static function letter_first($s0) |
| 342 | 338 | { |
| 343 | 339 | $firstchar_ord = ord(strtoupper($s0{0})); |
| 344 | - if (($firstchar_ord >= 65 and $firstchar_ord <= 91) or ($firstchar_ord >= 48 and $firstchar_ord <= 57)) return $s0{0}; |
|
| 340 | + if (($firstchar_ord >= 65 and $firstchar_ord <= 91) or ($firstchar_ord >= 48 and $firstchar_ord <= 57)) { |
|
| 341 | + return $s0{0}; |
|
| 342 | + } |
|
| 345 | 343 | // $s = iconv("utf-8", "gbk//ignore", $s0); |
| 346 | 344 | $s = mb_convert_encoding($s0, "gbk", "utf-8"); |
| 347 | 345 | $asc = ord($s{0}) * 256 + ord($s{1}) - 65536; |
| 348 | - if ($asc >= -20319 and $asc <= -20284) return "A"; |
|
| 349 | - if ($asc >= -20283 and $asc <= -19776) return "B"; |
|
| 350 | - if ($asc >= -19775 and $asc <= -19219) return "C"; |
|
| 351 | - if ($asc >= -19218 and $asc <= -18711) return "D"; |
|
| 352 | - if ($asc >= -18710 and $asc <= -18527) return "E"; |
|
| 353 | - if ($asc >= -18526 and $asc <= -18240) return "F"; |
|
| 354 | - if ($asc >= -18239 and $asc <= -17923) return "G"; |
|
| 355 | - if ($asc >= -17922 and $asc <= -17418) return "H"; |
|
| 356 | - if ($asc >= -17417 and $asc <= -16475) return "J"; |
|
| 357 | - if ($asc >= -16474 and $asc <= -16213) return "K"; |
|
| 358 | - if ($asc >= -16212 and $asc <= -15641) return "L"; |
|
| 359 | - if ($asc >= -15640 and $asc <= -15166) return "M"; |
|
| 360 | - if ($asc >= -15165 and $asc <= -14923) return "N"; |
|
| 361 | - if ($asc >= -14922 and $asc <= -14915) return "O"; |
|
| 362 | - if ($asc >= -14914 and $asc <= -14631) return "P"; |
|
| 363 | - if ($asc >= -14630 and $asc <= -14150) return "Q"; |
|
| 364 | - if ($asc >= -14149 and $asc <= -14091) return "R"; |
|
| 365 | - if ($asc >= -14090 and $asc <= -13319) return "S"; |
|
| 366 | - if ($asc >= -13318 and $asc <= -12839) return "T"; |
|
| 367 | - if ($asc >= -12838 and $asc <= -12557) return "W"; |
|
| 368 | - if ($asc >= -12556 and $asc <= -11848) return "X"; |
|
| 369 | - if ($asc >= -11847 and $asc <= -11056) return "Y"; |
|
| 370 | - if ($asc >= -11055 and $asc <= -10247) return "Z"; |
|
| 346 | + if ($asc >= -20319 and $asc <= -20284) { |
|
| 347 | + return "A"; |
|
| 348 | + } |
|
| 349 | + if ($asc >= -20283 and $asc <= -19776) { |
|
| 350 | + return "B"; |
|
| 351 | + } |
|
| 352 | + if ($asc >= -19775 and $asc <= -19219) { |
|
| 353 | + return "C"; |
|
| 354 | + } |
|
| 355 | + if ($asc >= -19218 and $asc <= -18711) { |
|
| 356 | + return "D"; |
|
| 357 | + } |
|
| 358 | + if ($asc >= -18710 and $asc <= -18527) { |
|
| 359 | + return "E"; |
|
| 360 | + } |
|
| 361 | + if ($asc >= -18526 and $asc <= -18240) { |
|
| 362 | + return "F"; |
|
| 363 | + } |
|
| 364 | + if ($asc >= -18239 and $asc <= -17923) { |
|
| 365 | + return "G"; |
|
| 366 | + } |
|
| 367 | + if ($asc >= -17922 and $asc <= -17418) { |
|
| 368 | + return "H"; |
|
| 369 | + } |
|
| 370 | + if ($asc >= -17417 and $asc <= -16475) { |
|
| 371 | + return "J"; |
|
| 372 | + } |
|
| 373 | + if ($asc >= -16474 and $asc <= -16213) { |
|
| 374 | + return "K"; |
|
| 375 | + } |
|
| 376 | + if ($asc >= -16212 and $asc <= -15641) { |
|
| 377 | + return "L"; |
|
| 378 | + } |
|
| 379 | + if ($asc >= -15640 and $asc <= -15166) { |
|
| 380 | + return "M"; |
|
| 381 | + } |
|
| 382 | + if ($asc >= -15165 and $asc <= -14923) { |
|
| 383 | + return "N"; |
|
| 384 | + } |
|
| 385 | + if ($asc >= -14922 and $asc <= -14915) { |
|
| 386 | + return "O"; |
|
| 387 | + } |
|
| 388 | + if ($asc >= -14914 and $asc <= -14631) { |
|
| 389 | + return "P"; |
|
| 390 | + } |
|
| 391 | + if ($asc >= -14630 and $asc <= -14150) { |
|
| 392 | + return "Q"; |
|
| 393 | + } |
|
| 394 | + if ($asc >= -14149 and $asc <= -14091) { |
|
| 395 | + return "R"; |
|
| 396 | + } |
|
| 397 | + if ($asc >= -14090 and $asc <= -13319) { |
|
| 398 | + return "S"; |
|
| 399 | + } |
|
| 400 | + if ($asc >= -13318 and $asc <= -12839) { |
|
| 401 | + return "T"; |
|
| 402 | + } |
|
| 403 | + if ($asc >= -12838 and $asc <= -12557) { |
|
| 404 | + return "W"; |
|
| 405 | + } |
|
| 406 | + if ($asc >= -12556 and $asc <= -11848) { |
|
| 407 | + return "X"; |
|
| 408 | + } |
|
| 409 | + if ($asc >= -11847 and $asc <= -11056) { |
|
| 410 | + return "Y"; |
|
| 411 | + } |
|
| 412 | + if ($asc >= -11055 and $asc <= -10247) { |
|
| 413 | + return "Z"; |
|
| 414 | + } |
|
| 371 | 415 | return 0; // null |
| 372 | 416 | } |
| 373 | 417 | |
@@ -400,11 +444,15 @@ discard block |
||
| 400 | 444 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); |
| 401 | 445 | $content = curl_exec($ch); |
| 402 | 446 | curl_close($ch); |
| 403 | - if ($content) return $content; |
|
| 447 | + if ($content) { |
|
| 448 | + return $content; |
|
| 449 | + } |
|
| 404 | 450 | } |
| 405 | 451 | $ctx = stream_context_create(array('http' => array('timeout' => $timeout))); |
| 406 | 452 | $content = @file_get_contents($url, 0, $ctx); |
| 407 | - if ($content) return $content; |
|
| 453 | + if ($content) { |
|
| 454 | + return $content; |
|
| 455 | + } |
|
| 408 | 456 | return false; |
| 409 | 457 | } |
| 410 | 458 | |
@@ -429,8 +477,7 @@ discard block |
||
| 429 | 477 | // 多个php-fpm写一个文件的时候容易丢失,要加锁 |
| 430 | 478 | //return @file_put_contents($file, $content, FILE_APPEND|LOCK_EX); |
| 431 | 479 | return @file_put_contents($file, $content, FILE_APPEND); |
| 432 | - } |
|
| 433 | - else |
|
| 480 | + } else |
|
| 434 | 481 | { |
| 435 | 482 | return @file_put_contents($file, $content, LOCK_EX); |
| 436 | 483 | } |
@@ -480,8 +527,7 @@ discard block |
||
| 480 | 527 | if(!is_dir($fullpath)) |
| 481 | 528 | { |
| 482 | 529 | unlink($fullpath); |
| 483 | - } |
|
| 484 | - else |
|
| 530 | + } else |
|
| 485 | 531 | { |
| 486 | 532 | self::deldir($fullpath); |
| 487 | 533 | } |
@@ -493,8 +539,7 @@ discard block |
||
| 493 | 539 | if(rmdir($dir)) |
| 494 | 540 | { |
| 495 | 541 | return true; |
| 496 | - } |
|
| 497 | - else |
|
| 542 | + } else |
|
| 498 | 543 | { |
| 499 | 544 | return false; |
| 500 | 545 | } |
@@ -523,12 +568,10 @@ discard block |
||
| 523 | 568 | if (is_link($fullpath)) |
| 524 | 569 | { |
| 525 | 570 | return FALSE; |
| 526 | - } |
|
| 527 | - elseif (!is_dir($fullpath) && !@chmod($fullpath, $filemode)) |
|
| 571 | + } elseif (!is_dir($fullpath) && !@chmod($fullpath, $filemode)) |
|
| 528 | 572 | { |
| 529 | 573 | return FALSE; |
| 530 | - } |
|
| 531 | - elseif (!self::chmodr($fullpath, $filemode)) |
|
| 574 | + } elseif (!self::chmodr($fullpath, $filemode)) |
|
| 532 | 575 | { |
| 533 | 576 | return FALSE; |
| 534 | 577 | } |
@@ -540,8 +583,7 @@ discard block |
||
| 540 | 583 | if (@chmod($path, $filemode)) |
| 541 | 584 | { |
| 542 | 585 | return TRUE; |
| 543 | - } |
|
| 544 | - else |
|
| 586 | + } else |
|
| 545 | 587 | { |
| 546 | 588 | return FALSE; |
| 547 | 589 | } |
@@ -576,8 +618,7 @@ discard block |
||
| 576 | 618 | if ($str === mb_convert_encoding(mb_convert_encoding($str, "UTF-32", "UTF-8"), "UTF-8", "UTF-32")) |
| 577 | 619 | { |
| 578 | 620 | return true; |
| 579 | - } |
|
| 580 | - else |
|
| 621 | + } else |
|
| 581 | 622 | { |
| 582 | 623 | return false; |
| 583 | 624 | } |
@@ -636,15 +677,27 @@ discard block |
||
| 636 | 677 | { |
| 637 | 678 | $dt[0] = $ymd[0]; |
| 638 | 679 | } |
| 639 | - if (isset($ymd[1])) $dt[1] = $ymd[1]; |
|
| 640 | - if (isset($ymd[2])) $dt[2] = $ymd[2]; |
|
| 641 | - if (strlen($dt[0]) == 2) $dt[0] = '20' . $dt[0]; |
|
| 680 | + if (isset($ymd[1])) { |
|
| 681 | + $dt[1] = $ymd[1]; |
|
| 682 | + } |
|
| 683 | + if (isset($ymd[2])) { |
|
| 684 | + $dt[2] = $ymd[2]; |
|
| 685 | + } |
|
| 686 | + if (strlen($dt[0]) == 2) { |
|
| 687 | + $dt[0] = '20' . $dt[0]; |
|
| 688 | + } |
|
| 642 | 689 | if (isset($ds[1])) |
| 643 | 690 | { |
| 644 | 691 | $hms = explode(":", $ds[1]); |
| 645 | - if (isset($hms[0])) $dt[3] = $hms[0]; |
|
| 646 | - if (isset($hms[1])) $dt[4] = $hms[1]; |
|
| 647 | - if (isset($hms[2])) $dt[5] = $hms[2]; |
|
| 692 | + if (isset($hms[0])) { |
|
| 693 | + $dt[3] = $hms[0]; |
|
| 694 | + } |
|
| 695 | + if (isset($hms[1])) { |
|
| 696 | + $dt[4] = $hms[1]; |
|
| 697 | + } |
|
| 698 | + if (isset($hms[2])) { |
|
| 699 | + $dt[5] = $hms[2]; |
|
| 700 | + } |
|
| 648 | 701 | } |
| 649 | 702 | foreach ($dt as $k => $v) |
| 650 | 703 | { |
@@ -658,8 +711,7 @@ discard block |
||
| 658 | 711 | if (!empty($mt)) |
| 659 | 712 | { |
| 660 | 713 | return $mt; |
| 661 | - } |
|
| 662 | - else |
|
| 714 | + } else |
|
| 663 | 715 | { |
| 664 | 716 | return strtotime($dtime); |
| 665 | 717 | } |
@@ -668,8 +720,12 @@ discard block |
||
| 668 | 720 | public static function cn_substr($string, $length = 80, $etc = '...', $count_words = true) |
| 669 | 721 | { |
| 670 | 722 | mb_internal_encoding("UTF-8"); |
| 671 | - if ($length == 0) return ''; |
|
| 672 | - if (strlen($string) <= $length) return $string; |
|
| 723 | + if ($length == 0) { |
|
| 724 | + return ''; |
|
| 725 | + } |
|
| 726 | + if (strlen($string) <= $length) { |
|
| 727 | + return $string; |
|
| 728 | + } |
|
| 673 | 729 | preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $string, $info); |
| 674 | 730 | if ($count_words) |
| 675 | 731 | { |
@@ -681,8 +737,7 @@ discard block |
||
| 681 | 737 | if (ord($info[0][$i]) >= 128) |
| 682 | 738 | { |
| 683 | 739 | $j = $j + 2; |
| 684 | - } |
|
| 685 | - else |
|
| 740 | + } else |
|
| 686 | 741 | { |
| 687 | 742 | $j = $j + 1; |
| 688 | 743 | } |
@@ -733,13 +788,11 @@ discard block |
||
| 733 | 788 | if (is_array($header['Location'])) |
| 734 | 789 | { |
| 735 | 790 | return $header['Location'][count($header['Location']) - 1]; |
| 736 | - } |
|
| 737 | - else |
|
| 791 | + } else |
|
| 738 | 792 | { |
| 739 | 793 | return $header['Location']; |
| 740 | 794 | } |
| 741 | - } |
|
| 742 | - else |
|
| 795 | + } else |
|
| 743 | 796 | { |
| 744 | 797 | return $url; |
| 745 | 798 | } |
@@ -758,14 +811,22 @@ discard block |
||
| 758 | 811 | $extralen = $extralen[1]; |
| 759 | 812 | $headerlen += 2 + $extralen; |
| 760 | 813 | } |
| 761 | - if ($flags & 8) // Filename |
|
| 814 | + if ($flags & 8) { |
|
| 815 | + // Filename |
|
| 762 | 816 | $headerlen = strpos($data, chr(0), $headerlen) + 1; |
| 763 | - if ($flags & 16) // Comment |
|
| 817 | + } |
|
| 818 | + if ($flags & 16) { |
|
| 819 | + // Comment |
|
| 764 | 820 | $headerlen = strpos($data, chr(0), $headerlen) + 1; |
| 765 | - if ($flags & 2) // CRC at end of file |
|
| 821 | + } |
|
| 822 | + if ($flags & 2) { |
|
| 823 | + // CRC at end of file |
|
| 766 | 824 | $headerlen += 2; |
| 825 | + } |
|
| 767 | 826 | $unpacked = @gzinflate(substr($data, $headerlen)); |
| 768 | - if ($unpacked === FALSE) $unpacked = $data; |
|
| 827 | + if ($unpacked === FALSE) { |
|
| 828 | + $unpacked = $data; |
|
| 829 | + } |
|
| 769 | 830 | return $unpacked; |
| 770 | 831 | } |
| 771 | 832 | |
@@ -777,7 +838,9 @@ discard block |
||
| 777 | 838 | */ |
| 778 | 839 | public static function number2chinese($num, $sim = FALSE) |
| 779 | 840 | { |
| 780 | - if (!is_numeric($num)) return '含有非数字非小数点字符!'; |
|
| 841 | + if (!is_numeric($num)) { |
|
| 842 | + return '含有非数字非小数点字符!'; |
|
| 843 | + } |
|
| 781 | 844 | $char = $sim ? array('零', '一', '二', '三', '四', '五', '六', '七', '八', '九') : array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'); |
| 782 | 845 | $unit = $sim ? array('', '十', '百', '千', '', '万', '亿', '兆') : array('', '拾', '佰', '仟', '', '萬', '億', '兆'); |
| 783 | 846 | $retval = ''; |
@@ -811,8 +874,7 @@ discard block |
||
| 811 | 874 | if ($f == 1 && $str[$i] == 0) |
| 812 | 875 | { |
| 813 | 876 | $out[$i] = ""; |
| 814 | - } |
|
| 815 | - else |
|
| 877 | + } else |
|
| 816 | 878 | { |
| 817 | 879 | $out[$i] = $char[$str[$i]]; |
| 818 | 880 | } |
@@ -886,8 +948,7 @@ discard block |
||
| 886 | 948 | XML_TEXT_NODE == $childNode->firstChild->nodeType ) |
| 887 | 949 | { |
| 888 | 950 | $array[$childNode->localName] = $childNode->nodeValue; |
| 889 | - } |
|
| 890 | - else |
|
| 951 | + } else |
|
| 891 | 952 | { |
| 892 | 953 | if( false !== ($a = self::node_to_array( $dom, $childNode))) |
| 893 | 954 | { |
@@ -758,35 +758,35 @@ |
||
| 758 | 758 | switch($command) |
| 759 | 759 | { |
| 760 | 760 | // 启动 phpspider |
| 761 | - case 'start': |
|
| 762 | - if ($command2 === '-d') |
|
| 763 | - { |
|
| 764 | - self::$daemonize = true; |
|
| 765 | - } |
|
| 766 | - break; |
|
| 767 | - case 'stop': |
|
| 768 | - exec("ps aux | grep $start_file | grep -v grep | awk '{print $2}'", $info); |
|
| 769 | - if (count($info) <= 1) |
|
| 770 | - { |
|
| 771 | - echo "PHPSpider[$start_file] not run\n"; |
|
| 772 | - } |
|
| 773 | - else |
|
| 774 | - { |
|
| 775 | - //echo "PHPSpider[$start_file] is stoping ...\n"; |
|
| 776 | - echo "PHPSpider[$start_file] stop success"; |
|
| 777 | - exec("ps aux | grep $start_file | grep -v grep | awk '{print $2}' |xargs kill -SIGINT", $info); |
|
| 778 | - } |
|
| 779 | - exit; |
|
| 780 | - break; |
|
| 781 | - case 'kill': |
|
| 782 | - exec("ps aux | grep $start_file | grep -v grep | awk '{print $2}' |xargs kill -SIGKILL"); |
|
| 783 | - break; |
|
| 784 | - // 显示 phpspider 运行状态 |
|
| 785 | - case 'status': |
|
| 786 | - exit(0); |
|
| 787 | - // 未知命令 |
|
| 788 | - default : |
|
| 789 | - exit("Usage: php yourfile.php {start|stop|status|kill}\n"); |
|
| 761 | + case 'start': |
|
| 762 | + if ($command2 === '-d') |
|
| 763 | + { |
|
| 764 | + self::$daemonize = true; |
|
| 765 | + } |
|
| 766 | + break; |
|
| 767 | + case 'stop': |
|
| 768 | + exec("ps aux | grep $start_file | grep -v grep | awk '{print $2}'", $info); |
|
| 769 | + if (count($info) <= 1) |
|
| 770 | + { |
|
| 771 | + echo "PHPSpider[$start_file] not run\n"; |
|
| 772 | + } |
|
| 773 | + else |
|
| 774 | + { |
|
| 775 | + //echo "PHPSpider[$start_file] is stoping ...\n"; |
|
| 776 | + echo "PHPSpider[$start_file] stop success"; |
|
| 777 | + exec("ps aux | grep $start_file | grep -v grep | awk '{print $2}' |xargs kill -SIGINT", $info); |
|
| 778 | + } |
|
| 779 | + exit; |
|
| 780 | + break; |
|
| 781 | + case 'kill': |
|
| 782 | + exec("ps aux | grep $start_file | grep -v grep | awk '{print $2}' |xargs kill -SIGKILL"); |
|
| 783 | + break; |
|
| 784 | + // 显示 phpspider 运行状态 |
|
| 785 | + case 'status': |
|
| 786 | + exit(0); |
|
| 787 | + // 未知命令 |
|
| 788 | + default : |
|
| 789 | + exit("Usage: php yourfile.php {start|stop|status|kill}\n"); |
|
| 790 | 790 | } |
| 791 | 791 | } |
| 792 | 792 | |
@@ -399,7 +399,7 @@ discard block |
||
| 399 | 399 | public function __construct($configs = array()) |
| 400 | 400 | { |
| 401 | 401 | // 产生时钟云,解决php7下面ctrl+c无法停止bug |
| 402 | - declare(ticks = 1); |
|
| 402 | + declare(ticks=1); |
|
| 403 | 403 | |
| 404 | 404 | // 先打开以显示验证报错内容 |
| 405 | 405 | log::$log_show = true; |
@@ -416,16 +416,16 @@ discard block |
||
| 416 | 416 | exit; |
| 417 | 417 | } |
| 418 | 418 | |
| 419 | - $configs['name'] = isset($configs['name']) ? $configs['name'] : 'phpspider'; |
|
| 420 | - $configs['proxy'] = isset($configs['proxy']) ? $configs['proxy'] : false; |
|
| 421 | - $configs['user_agent'] = isset($configs['user_agent']) ? $configs['user_agent'] : self::AGENT_PC; |
|
| 422 | - $configs['client_ip'] = isset($configs['client_ip']) ? $configs['client_ip'] : array(); |
|
| 423 | - $configs['interval'] = isset($configs['interval']) ? $configs['interval'] : self::INTERVAL; |
|
| 424 | - $configs['timeout'] = isset($configs['timeout']) ? $configs['timeout'] : self::TIMEOUT; |
|
| 425 | - $configs['max_try'] = isset($configs['max_try']) ? $configs['max_try'] : self::MAX_TRY; |
|
| 426 | - $configs['max_depth'] = isset($configs['max_depth']) ? $configs['max_depth'] : 0; |
|
| 427 | - $configs['max_fields'] = isset($configs['max_fields']) ? $configs['max_fields'] : 0; |
|
| 428 | - $configs['export'] = isset($configs['export']) ? $configs['export'] : array(); |
|
| 419 | + $configs['name'] = isset($configs['name']) ? $configs['name'] : 'phpspider'; |
|
| 420 | + $configs['proxy'] = isset($configs['proxy']) ? $configs['proxy'] : false; |
|
| 421 | + $configs['user_agent'] = isset($configs['user_agent']) ? $configs['user_agent'] : self::AGENT_PC; |
|
| 422 | + $configs['client_ip'] = isset($configs['client_ip']) ? $configs['client_ip'] : array(); |
|
| 423 | + $configs['interval'] = isset($configs['interval']) ? $configs['interval'] : self::INTERVAL; |
|
| 424 | + $configs['timeout'] = isset($configs['timeout']) ? $configs['timeout'] : self::TIMEOUT; |
|
| 425 | + $configs['max_try'] = isset($configs['max_try']) ? $configs['max_try'] : self::MAX_TRY; |
|
| 426 | + $configs['max_depth'] = isset($configs['max_depth']) ? $configs['max_depth'] : 0; |
|
| 427 | + $configs['max_fields'] = isset($configs['max_fields']) ? $configs['max_fields'] : 0; |
|
| 428 | + $configs['export'] = isset($configs['export']) ? $configs['export'] : array(); |
|
| 429 | 429 | //新增参数 BY KEN <[email protected]> |
| 430 | 430 | $configs['max_pages'] = isset($configs['max_pages']) ? $configs['max_pages'] : self::$max_pages; |
| 431 | 431 | $configs['max_duration'] = isset($configs['max_duration']) ? $configs['max_duration'] : self::$max_duration; |
@@ -443,11 +443,11 @@ discard block |
||
| 443 | 443 | } |
| 444 | 444 | |
| 445 | 445 | // csv、sql、db |
| 446 | - self::$export_type = isset($configs['export']['type']) ? $configs['export']['type'] : ''; |
|
| 447 | - self::$export_file = isset($configs['export']['file']) ? $configs['export']['file'] : ''; |
|
| 446 | + self::$export_type = isset($configs['export']['type']) ? $configs['export']['type'] : ''; |
|
| 447 | + self::$export_file = isset($configs['export']['file']) ? $configs['export']['file'] : ''; |
|
| 448 | 448 | self::$export_table = isset($configs['export']['table']) ? $configs['export']['table'] : ''; |
| 449 | - self::$db_config = isset($configs['db_config']) ? $configs['db_config'] : array(); |
|
| 450 | - self::$queue_config = isset($configs['queue_config']) ? $configs['queue_config'] : array(); |
|
| 449 | + self::$db_config = isset($configs['db_config']) ? $configs['db_config'] : array(); |
|
| 450 | + self::$queue_config = isset($configs['queue_config']) ? $configs['queue_config'] : array(); |
|
| 451 | 451 | |
| 452 | 452 | // 是否设置了并发任务数, 并且大于1, 而且不是windows环境 |
| 453 | 453 | if (isset($configs['tasknum']) && $configs['tasknum'] > 1 && !util::is_win()) |
@@ -492,7 +492,7 @@ discard block |
||
| 492 | 492 | // 投递状态 |
| 493 | 493 | $status = false; |
| 494 | 494 | //限制最大子域名数量 |
| 495 | - if ( ! empty(self::$configs['max_sub_num'])) |
|
| 495 | + if (!empty(self::$configs['max_sub_num'])) |
|
| 496 | 496 | { |
| 497 | 497 | //抓取到的子域名超过指定数量,就丢掉此域名 |
| 498 | 498 | $sub_domain_count = $this->sub_domain_count($url); |
@@ -557,7 +557,7 @@ discard block |
||
| 557 | 557 | // 投递状态 |
| 558 | 558 | $status = false; |
| 559 | 559 | //限制最大子域名数量 |
| 560 | - if ( ! empty(self::$configs['max_sub_num'])) |
|
| 560 | + if (!empty(self::$configs['max_sub_num'])) |
|
| 561 | 561 | { |
| 562 | 562 | //抓取超过 max_sub_num 子域名的,就丢掉 |
| 563 | 563 | $sub_domain_count = $this->sub_domain_count($url); |
@@ -615,12 +615,12 @@ discard block |
||
| 615 | 615 | { |
| 616 | 616 | $parse_url = parse_url($url); |
| 617 | 617 | //2018-1-3 通配所有域名 |
| 618 | - if ( ! empty($parse_url['host']) and self::$configs['domains'][0] == '*') |
|
| 618 | + if (!empty($parse_url['host']) and self::$configs['domains'][0] == '*') |
|
| 619 | 619 | { |
| 620 | 620 | return true; |
| 621 | 621 | } |
| 622 | 622 | //限定域名 |
| 623 | - if (empty($parse_url['host']) || ! in_array($parse_url['host'], self::$configs['domains'])) |
|
| 623 | + if (empty($parse_url['host']) || !in_array($parse_url['host'], self::$configs['domains'])) |
|
| 624 | 624 | { |
| 625 | 625 | return false; |
| 626 | 626 | } |
@@ -645,7 +645,7 @@ discard block |
||
| 645 | 645 | } |
| 646 | 646 | |
| 647 | 647 | //增加 要排除的列表页特征正则 BY KEN <[email protected]> |
| 648 | - if ( ! empty(self::$configs['list_url_regexes_remove'])) |
|
| 648 | + if (!empty(self::$configs['list_url_regexes_remove'])) |
|
| 649 | 649 | { |
| 650 | 650 | foreach (self::$configs['list_url_regexes_remove'] as $regex) |
| 651 | 651 | { |
@@ -668,7 +668,7 @@ discard block |
||
| 668 | 668 | return true; |
| 669 | 669 | } |
| 670 | 670 | |
| 671 | - if ( ! empty(self::$configs['list_url_regexes'])) |
|
| 671 | + if (!empty(self::$configs['list_url_regexes'])) |
|
| 672 | 672 | { |
| 673 | 673 | foreach (self::$configs['list_url_regexes'] as $regex) |
| 674 | 674 | { |
@@ -700,7 +700,7 @@ discard block |
||
| 700 | 700 | } |
| 701 | 701 | |
| 702 | 702 | //增加 要排除的内容页特征正则 BY KEN <[email protected]> |
| 703 | - if ( ! empty(self::$configs['content_url_regexes_remove'])) |
|
| 703 | + if (!empty(self::$configs['content_url_regexes_remove'])) |
|
| 704 | 704 | { |
| 705 | 705 | foreach (self::$configs['content_url_regexes_remove'] as $regex) |
| 706 | 706 | { |
@@ -722,7 +722,7 @@ discard block |
||
| 722 | 722 | return false; |
| 723 | 723 | } |
| 724 | 724 | |
| 725 | - if ( ! empty(self::$configs['content_url_regexes'])) |
|
| 725 | + if (!empty(self::$configs['content_url_regexes'])) |
|
| 726 | 726 | { |
| 727 | 727 | foreach (self::$configs['content_url_regexes'] as $regex) |
| 728 | 728 | { |
@@ -755,7 +755,7 @@ discard block |
||
| 755 | 755 | $command2 = isset($argv[2]) ? $argv[2] : ''; |
| 756 | 756 | |
| 757 | 757 | // 根据命令做相应处理 |
| 758 | - switch($command) |
|
| 758 | + switch ($command) |
|
| 759 | 759 | { |
| 760 | 760 | // 启动 phpspider |
| 761 | 761 | case 'start': |
@@ -920,7 +920,7 @@ discard block |
||
| 920 | 920 | // 显示最后结果 |
| 921 | 921 | log::$log_show = true; |
| 922 | 922 | |
| 923 | - $spider_time_run = util::time2second(intval(microtime(true) - self::$time_start)); |
|
| 923 | + $spider_time_run = util::time2second(intval(microtime(true)-self::$time_start)); |
|
| 924 | 924 | log::note("Spider finished in {$spider_time_run}"); |
| 925 | 925 | |
| 926 | 926 | $get_collected_url_num = $this->get_collected_url_num(); |
@@ -949,12 +949,12 @@ discard block |
||
| 949 | 949 | // 检查PHP版本 |
| 950 | 950 | if (version_compare(PHP_VERSION, '5.3.0', 'lt')) |
| 951 | 951 | { |
| 952 | - log::error('PHP 5.3+ is required, currently installed version is: ' . phpversion()); |
|
| 952 | + log::error('PHP 5.3+ is required, currently installed version is: '.phpversion()); |
|
| 953 | 953 | exit; |
| 954 | 954 | } |
| 955 | 955 | |
| 956 | 956 | // 检查CURL扩展 |
| 957 | - if(!function_exists('curl_init')) |
|
| 957 | + if (!function_exists('curl_init')) |
|
| 958 | 958 | { |
| 959 | 959 | log::error('The curl extension was not found'); |
| 960 | 960 | exit; |
@@ -975,26 +975,26 @@ discard block |
||
| 975 | 975 | } |
| 976 | 976 | |
| 977 | 977 | // 集群、保存运行状态、多任务都需要Redis支持 |
| 978 | - if ( self::$multiserver || self::$save_running_state || self::$tasknum > 1 ) |
|
| 978 | + if (self::$multiserver || self::$save_running_state || self::$tasknum > 1) |
|
| 979 | 979 | { |
| 980 | 980 | self::$use_redis = true; |
| 981 | 981 | |
| 982 | 982 | queue::set_connect('default', self::$queue_config); |
| 983 | 983 | if (!queue::init()) |
| 984 | 984 | { |
| 985 | - if ( self::$multiserver ) |
|
| 985 | + if (self::$multiserver) |
|
| 986 | 986 | { |
| 987 | 987 | log::error('Multiserver needs Redis support, '.queue::$error); |
| 988 | 988 | exit; |
| 989 | 989 | } |
| 990 | 990 | |
| 991 | - if ( self::$tasknum > 1 ) |
|
| 991 | + if (self::$tasknum > 1) |
|
| 992 | 992 | { |
| 993 | 993 | log::error('Multitasking needs Redis support, '.queue::$error); |
| 994 | 994 | exit; |
| 995 | 995 | } |
| 996 | 996 | |
| 997 | - if ( self::$save_running_state ) |
|
| 997 | + if (self::$save_running_state) |
|
| 998 | 998 | { |
| 999 | 999 | log::error('Spider kept running state needs Redis support, '.queue::$error); |
| 1000 | 1000 | exit; |
@@ -1015,7 +1015,7 @@ discard block |
||
| 1015 | 1015 | exit; |
| 1016 | 1016 | } |
| 1017 | 1017 | |
| 1018 | - foreach ( self::$configs['scan_urls'] as $url ) |
|
| 1018 | + foreach (self::$configs['scan_urls'] as $url) |
|
| 1019 | 1019 | { |
| 1020 | 1020 | // 只检查配置中的入口URL, 通过 add_scan_url 添加的不检查了. |
| 1021 | 1021 | if (!$this->is_scan_page($url)) |
@@ -1047,7 +1047,7 @@ discard block |
||
| 1047 | 1047 | $start_file = $argv[0]; |
| 1048 | 1048 | |
| 1049 | 1049 | $header = ''; |
| 1050 | - if ( ! util::is_win()) |
|
| 1050 | + if (!util::is_win()) |
|
| 1051 | 1051 | { |
| 1052 | 1052 | $header .= "\033[33m"; |
| 1053 | 1053 | } |
@@ -1057,7 +1057,7 @@ discard block |
||
| 1057 | 1057 | $header .= " * Documentation: https://doc.phpspider.org\n"; |
| 1058 | 1058 | $header .= ' * Task Number: '.self::$tasknum."\n\n"; |
| 1059 | 1059 | $header .= "Input \"php $start_file stop\" to quit. Start success.\n"; |
| 1060 | - if ( ! util::is_win()) |
|
| 1060 | + if (!util::is_win()) |
|
| 1061 | 1061 | { |
| 1062 | 1062 | $header .= "\033[0m"; |
| 1063 | 1063 | } |
@@ -1079,7 +1079,7 @@ discard block |
||
| 1079 | 1079 | //-------------------------------------------------------------------------------- |
| 1080 | 1080 | |
| 1081 | 1081 | // 添加入口URL到队列 |
| 1082 | - foreach ( self::$configs['scan_urls'] as $url ) |
|
| 1082 | + foreach (self::$configs['scan_urls'] as $url) |
|
| 1083 | 1083 | { |
| 1084 | 1084 | // false 表示不允许重复 |
| 1085 | 1085 | $this->add_scan_url($url, null, false); |
@@ -1127,7 +1127,7 @@ discard block |
||
| 1127 | 1127 | $pid = pcntl_fork(); |
| 1128 | 1128 | |
| 1129 | 1129 | // 主进程记录子进程pid |
| 1130 | - if($pid > 0) |
|
| 1130 | + if ($pid > 0) |
|
| 1131 | 1131 | { |
| 1132 | 1132 | // 暂时没用 |
| 1133 | 1133 | //self::$taskpids[$taskid] = $pid; |
@@ -1173,7 +1173,7 @@ discard block |
||
| 1173 | 1173 | |
| 1174 | 1174 | public function do_collect_page() |
| 1175 | 1175 | { |
| 1176 | - while( $queue_lsize = $this->queue_lsize() ) |
|
| 1176 | + while ($queue_lsize = $this->queue_lsize()) |
|
| 1177 | 1177 | { |
| 1178 | 1178 | // 如果是主任务 |
| 1179 | 1179 | if (self::$taskmaster) |
@@ -1182,7 +1182,7 @@ discard block |
||
| 1182 | 1182 | if (self::$tasknum > 1 && !self::$fork_task_complete) |
| 1183 | 1183 | { |
| 1184 | 1184 | // 主进程采集到多于任务数2个时, 生成子任务一起采集 |
| 1185 | - if ($queue_lsize > self::$tasknum + 2) |
|
| 1185 | + if ($queue_lsize > self::$tasknum+2) |
|
| 1186 | 1186 | { |
| 1187 | 1187 | self::$fork_task_complete = true; |
| 1188 | 1188 | |
@@ -1196,7 +1196,7 @@ discard block |
||
| 1196 | 1196 | } |
| 1197 | 1197 | } |
| 1198 | 1198 | //在主进程中,保存当前配置到缓存,以使子进程可实时读取动态修改后的配置 20180209 |
| 1199 | - if (self::$use_redis and ! empty(self::$configs)) |
|
| 1199 | + if (self::$use_redis and !empty(self::$configs)) |
|
| 1200 | 1200 | { |
| 1201 | 1201 | queue::set('configs_'.self::$configs['name'], json_encode(self::$configs)); |
| 1202 | 1202 | } |
@@ -1215,10 +1215,10 @@ discard block |
||
| 1215 | 1215 | else |
| 1216 | 1216 | { |
| 1217 | 1217 | // 主进程采集到多于任务数2个时, 子任务可以采集, 否则等待... |
| 1218 | - if ($queue_lsize > self::$taskid + 2) |
|
| 1218 | + if ($queue_lsize > self::$taskid+2) |
|
| 1219 | 1219 | { |
| 1220 | 1220 | //在子进程中,从内存中实时读取当前最新配置,用于适应主进程常驻内存模式,无限循环后的配置变动 20180209 |
| 1221 | - if (self::$use_redis and ! empty(self::$configs)) |
|
| 1221 | + if (self::$use_redis and !empty(self::$configs)) |
|
| 1222 | 1222 | { |
| 1223 | 1223 | if ($configs_active = queue::get('configs_'.self::$configs['name'])) |
| 1224 | 1224 | { |
@@ -1345,7 +1345,7 @@ discard block |
||
| 1345 | 1345 | $html = $this->request_url($url, $link); |
| 1346 | 1346 | |
| 1347 | 1347 | //记录速度较慢域名花费抓取时间 20180213 |
| 1348 | - $time_run = round(microtime(true) - $page_time_start); |
|
| 1348 | + $time_run = round(microtime(true)-$page_time_start); |
|
| 1349 | 1349 | if ($time_run > 1) |
| 1350 | 1350 | { |
| 1351 | 1351 | $this->incr_duration_num($url, $time_run); |
@@ -1453,7 +1453,7 @@ discard block |
||
| 1453 | 1453 | if (self::$configs['max_depth'] == 0 || $link['depth'] < self::$configs['max_depth']) |
| 1454 | 1454 | { |
| 1455 | 1455 | // 分析提取HTML页面中的URL |
| 1456 | - $this->get_urls($page['raw'], $url, $link['depth'] + 1); |
|
| 1456 | + $this->get_urls($page['raw'], $url, $link['depth']+1); |
|
| 1457 | 1457 | } |
| 1458 | 1458 | } |
| 1459 | 1459 | |
@@ -1468,10 +1468,10 @@ discard block |
||
| 1468 | 1468 | $this->incr_depth_num($link['depth']); |
| 1469 | 1469 | |
| 1470 | 1470 | // 处理页面耗时时间 |
| 1471 | - $time_run = round(microtime(true) - $page_time_start, 3); |
|
| 1471 | + $time_run = round(microtime(true)-$page_time_start, 3); |
|
| 1472 | 1472 | log::debug('task id: '.self::$taskid." Success process page {$url} in {$time_run} s"); |
| 1473 | 1473 | |
| 1474 | - $spider_time_run = util::time2second(intval(microtime(true) - self::$time_start)); |
|
| 1474 | + $spider_time_run = util::time2second(intval(microtime(true)-self::$time_start)); |
|
| 1475 | 1475 | log::info('task id: '.self::$taskid." Spider running in {$spider_time_run}"); |
| 1476 | 1476 | |
| 1477 | 1477 | // 爬虫爬取每个网页的时间间隔, 单位: 毫秒 |
@@ -1559,7 +1559,7 @@ discard block |
||
| 1559 | 1559 | $html = $return; |
| 1560 | 1560 | } |
| 1561 | 1561 | unset($return); |
| 1562 | - if ( ! $html) |
|
| 1562 | + if (!$html) |
|
| 1563 | 1563 | { |
| 1564 | 1564 | return false; |
| 1565 | 1565 | } |
@@ -1593,19 +1593,19 @@ discard block |
||
| 1593 | 1593 | } |
| 1594 | 1594 | else |
| 1595 | 1595 | { |
| 1596 | - if ( ! empty(self::$configs['max_try']) and $http_code == 407) |
|
| 1596 | + if (!empty(self::$configs['max_try']) and $http_code == 407) |
|
| 1597 | 1597 | { |
| 1598 | 1598 | // 扔到队列头部去, 继续采集 |
| 1599 | 1599 | $this->queue_rpush($link); |
| 1600 | 1600 | log::error("Failed to download page {$url}"); |
| 1601 | 1601 | self::$collect_fail++; |
| 1602 | 1602 | } |
| 1603 | - elseif ( ! empty(self::$configs['max_try']) and in_array($http_code, array('0', '502', '503', '429'))) |
|
| 1603 | + elseif (!empty(self::$configs['max_try']) and in_array($http_code, array('0', '502', '503', '429'))) |
|
| 1604 | 1604 | { |
| 1605 | 1605 | // 采集次数加一 |
| 1606 | 1606 | $link['try_num']++; |
| 1607 | 1607 | // 抓取次数 小于 允许抓取失败次数 |
| 1608 | - if ( $link['try_num'] <= $link['max_try'] ) |
|
| 1608 | + if ($link['try_num'] <= $link['max_try']) |
|
| 1609 | 1609 | { |
| 1610 | 1610 | // 扔到队列头部去, 继续采集 |
| 1611 | 1611 | $this->queue_rpush($link); |
@@ -1623,7 +1623,7 @@ discard block |
||
| 1623 | 1623 | } |
| 1624 | 1624 | |
| 1625 | 1625 | // 爬取页面耗时时间 |
| 1626 | - $time_run = round(microtime(true) - $time_start, 3); |
|
| 1626 | + $time_run = round(microtime(true)-$time_start, 3); |
|
| 1627 | 1627 | log::debug("Success download page {$url} in {$time_run} s"); |
| 1628 | 1628 | self::$collect_succ++; |
| 1629 | 1629 | |
@@ -1669,7 +1669,7 @@ discard block |
||
| 1669 | 1669 | foreach ($urls as $key=>$url) |
| 1670 | 1670 | { |
| 1671 | 1671 | //限制最大子域名数量 |
| 1672 | - if ( ! empty(self::$configs['max_sub_num'])) |
|
| 1672 | + if (!empty(self::$configs['max_sub_num'])) |
|
| 1673 | 1673 | { |
| 1674 | 1674 | //抓取子域名超过超过指定值,就丢掉 |
| 1675 | 1675 | $sub_domain_count = $this->sub_domain_count($url); |
@@ -1784,7 +1784,7 @@ discard block |
||
| 1784 | 1784 | return false; |
| 1785 | 1785 | } |
| 1786 | 1786 | // 过滤mailto、tel、sms、wechat、sinaweibo、weixin等协议 |
| 1787 | - if ( ! in_array($parse_url['scheme'], array('http', 'https'))) |
|
| 1787 | + if (!in_array($parse_url['scheme'], array('http', 'https'))) |
|
| 1788 | 1788 | { |
| 1789 | 1789 | return false; |
| 1790 | 1790 | } |
@@ -1794,8 +1794,8 @@ discard block |
||
| 1794 | 1794 | $base_url_path = $domain.$path; |
| 1795 | 1795 | $base_url_path = preg_replace("/\/([^\/]*)\.(.*)$/", '/', $base_url_path); |
| 1796 | 1796 | $base_url_path = preg_replace("/\/$/", '', $base_url_path); |
| 1797 | - $i = $path_step = 0; |
|
| 1798 | - $dstr = $pstr = ''; |
|
| 1797 | + $i = $path_step = 0; |
|
| 1798 | + $dstr = $pstr = ''; |
|
| 1799 | 1799 | $pos = strpos($url, '#'); |
| 1800 | 1800 | if ($pos > 0) |
| 1801 | 1801 | { |
@@ -1809,28 +1809,28 @@ discard block |
||
| 1809 | 1809 | $url = preg_replace('/^\/\//iu', '', $url); |
| 1810 | 1810 | } |
| 1811 | 1811 | // /1234.html |
| 1812 | - elseif($url[0] == '/') |
|
| 1812 | + elseif ($url[0] == '/') |
|
| 1813 | 1813 | { |
| 1814 | 1814 | $url = $domain.$url; |
| 1815 | 1815 | } |
| 1816 | 1816 | // ./1234.html、../1234.html 这种类型的 |
| 1817 | - elseif($url[0] == '.') |
|
| 1817 | + elseif ($url[0] == '.') |
|
| 1818 | 1818 | { |
| 1819 | - if(!isset($url[2])) |
|
| 1819 | + if (!isset($url[2])) |
|
| 1820 | 1820 | { |
| 1821 | 1821 | return false; |
| 1822 | 1822 | } |
| 1823 | 1823 | else |
| 1824 | 1824 | { |
| 1825 | - $urls = explode('/',$url); |
|
| 1826 | - foreach($urls as $u) |
|
| 1825 | + $urls = explode('/', $url); |
|
| 1826 | + foreach ($urls as $u) |
|
| 1827 | 1827 | { |
| 1828 | - if( $u == '..' ) |
|
| 1828 | + if ($u == '..') |
|
| 1829 | 1829 | { |
| 1830 | 1830 | $path_step++; |
| 1831 | 1831 | } |
| 1832 | 1832 | // 遇到 ., 不知道为什么不直接写$u == '.', 貌似一样的 |
| 1833 | - else if( $i < count($urls)-1 ) |
|
| 1833 | + else if ($i < count($urls)-1) |
|
| 1834 | 1834 | { |
| 1835 | 1835 | $dstr .= $urls[$i].'/'; |
| 1836 | 1836 | } |
@@ -1840,29 +1840,29 @@ discard block |
||
| 1840 | 1840 | } |
| 1841 | 1841 | $i++; |
| 1842 | 1842 | } |
| 1843 | - $urls = explode('/',$base_url_path); |
|
| 1844 | - if(count($urls) <= $path_step) |
|
| 1843 | + $urls = explode('/', $base_url_path); |
|
| 1844 | + if (count($urls) <= $path_step) |
|
| 1845 | 1845 | { |
| 1846 | 1846 | return false; |
| 1847 | 1847 | } |
| 1848 | 1848 | else |
| 1849 | 1849 | { |
| 1850 | 1850 | $pstr = ''; |
| 1851 | - for($i=0;$i<count($urls)-$path_step;$i++){ $pstr .= $urls[$i].'/'; } |
|
| 1851 | + for ($i = 0; $i < count($urls)-$path_step; $i++) { $pstr .= $urls[$i].'/'; } |
|
| 1852 | 1852 | $url = $pstr.$dstr; |
| 1853 | 1853 | } |
| 1854 | 1854 | } |
| 1855 | 1855 | } |
| 1856 | 1856 | else |
| 1857 | 1857 | { |
| 1858 | - if( strtolower(substr($url, 0, 7))=='http://' ) |
|
| 1858 | + if (strtolower(substr($url, 0, 7)) == 'http://') |
|
| 1859 | 1859 | { |
| 1860 | 1860 | $url = preg_replace('#^http://#i', '', $url); |
| 1861 | 1861 | $scheme = 'http'; |
| 1862 | 1862 | } |
| 1863 | - else if( strtolower(substr($url, 0, 8))=='https://' ) |
|
| 1863 | + else if (strtolower(substr($url, 0, 8)) == 'https://') |
|
| 1864 | 1864 | { |
| 1865 | - $url = preg_replace('#^https://#i','',$url); |
|
| 1865 | + $url = preg_replace('#^https://#i', '', $url); |
|
| 1866 | 1866 | $scheme = "https"; |
| 1867 | 1867 | } |
| 1868 | 1868 | // 相对路径,像 1111.html 这种 |
@@ -1882,7 +1882,7 @@ discard block |
||
| 1882 | 1882 | $parse_url = @parse_url($url); |
| 1883 | 1883 | $domain = empty($parse_url['host']) ? $domain : $parse_url['host']; |
| 1884 | 1884 | // 如果host不为空, 判断是不是要爬取的域名 |
| 1885 | - if ( ! empty($parse_url['host'])) |
|
| 1885 | + if (!empty($parse_url['host'])) |
|
| 1886 | 1886 | { |
| 1887 | 1887 | //2018-1-3 通配所有域名 |
| 1888 | 1888 | if (empty(self::$configs['domains']) or self::$configs['domains'][0] == '*') |
@@ -1968,16 +1968,16 @@ discard block |
||
| 1968 | 1968 | public function link_uncompress($link) |
| 1969 | 1969 | { |
| 1970 | 1970 | $link = array( |
| 1971 | - 'url' => isset($link['url']) ? $link['url'] : '', |
|
| 1972 | - 'url_type' => isset($link['url_type']) ? $link['url_type'] : '', |
|
| 1973 | - 'method' => isset($link['method']) ? $link['method'] : 'get', |
|
| 1974 | - 'headers' => isset($link['headers']) ? $link['headers'] : array(), |
|
| 1975 | - 'params' => isset($link['params']) ? $link['params'] : array(), |
|
| 1971 | + 'url' => isset($link['url']) ? $link['url'] : '', |
|
| 1972 | + 'url_type' => isset($link['url_type']) ? $link['url_type'] : '', |
|
| 1973 | + 'method' => isset($link['method']) ? $link['method'] : 'get', |
|
| 1974 | + 'headers' => isset($link['headers']) ? $link['headers'] : array(), |
|
| 1975 | + 'params' => isset($link['params']) ? $link['params'] : array(), |
|
| 1976 | 1976 | 'context_data' => isset($link['context_data']) ? $link['context_data'] : '', |
| 1977 | - 'proxy' => isset($link['proxy']) ? $link['proxy'] : self::$configs['proxy'], |
|
| 1978 | - 'try_num' => isset($link['try_num']) ? $link['try_num'] : 0, |
|
| 1979 | - 'max_try' => isset($link['max_try']) ? $link['max_try'] : self::$configs['max_try'], |
|
| 1980 | - 'depth' => isset($link['depth']) ? $link['depth'] : 0, |
|
| 1977 | + 'proxy' => isset($link['proxy']) ? $link['proxy'] : self::$configs['proxy'], |
|
| 1978 | + 'try_num' => isset($link['try_num']) ? $link['try_num'] : 0, |
|
| 1979 | + 'max_try' => isset($link['max_try']) ? $link['max_try'] : self::$configs['max_try'], |
|
| 1980 | + 'depth' => isset($link['depth']) ? $link['depth'] : 0, |
|
| 1981 | 1981 | ); |
| 1982 | 1982 | |
| 1983 | 1983 | return $link; |
@@ -2027,10 +2027,10 @@ discard block |
||
| 2027 | 2027 | exit(0); |
| 2028 | 2028 | } |
| 2029 | 2029 | |
| 2030 | - if (version_compare(PHP_VERSION,'5.4.0','<')) |
|
| 2030 | + if (version_compare(PHP_VERSION, '5.4.0', '<')) |
|
| 2031 | 2031 | { |
| 2032 | 2032 | $fields_str = json_encode($fields); |
| 2033 | - $fields_str = preg_replace_callback("#\\\u([0-9a-f]{4})#i", function ($matchs) |
|
| 2033 | + $fields_str = preg_replace_callback("#\\\u([0-9a-f]{4})#i", function($matchs) |
|
| 2034 | 2034 | { |
| 2035 | 2035 | return @iconv('UCS-2BE', 'UTF-8', pack('H4', $matchs[1])); |
| 2036 | 2036 | }, $fields_str); |
@@ -2099,7 +2099,7 @@ discard block |
||
| 2099 | 2099 | if (!empty($conf['selector'])) |
| 2100 | 2100 | { |
| 2101 | 2101 | // 如果这个field是上一个field的附带连接 |
| 2102 | - if (isset($conf['source_type']) && $conf['source_type']=='attached_url') |
|
| 2102 | + if (isset($conf['source_type']) && $conf['source_type'] == 'attached_url') |
|
| 2103 | 2103 | { |
| 2104 | 2104 | // 取出上个field的内容作为连接, 内容分页是不进队列直接下载网页的 |
| 2105 | 2105 | if (!empty($fields[$conf['attached_url']])) |
@@ -2126,16 +2126,16 @@ discard block |
||
| 2126 | 2126 | } |
| 2127 | 2127 | |
| 2128 | 2128 | // 没有设置抽取规则的类型 或者 设置为 xpath |
| 2129 | - if (!isset($conf['selector_type']) || $conf['selector_type']=='xpath') |
|
| 2129 | + if (!isset($conf['selector_type']) || $conf['selector_type'] == 'xpath') |
|
| 2130 | 2130 | { |
| 2131 | 2131 | // 如果找不到,返回的是false |
| 2132 | 2132 | $values = $this->get_fields_xpath($html, $conf['selector'], $conf['name']); |
| 2133 | 2133 | } |
| 2134 | - elseif ($conf['selector_type']=='css') |
|
| 2134 | + elseif ($conf['selector_type'] == 'css') |
|
| 2135 | 2135 | { |
| 2136 | 2136 | $values = $this->get_fields_css($html, $conf['selector'], $conf['name']); |
| 2137 | 2137 | } |
| 2138 | - elseif ($conf['selector_type']=='regex') |
|
| 2138 | + elseif ($conf['selector_type'] == 'regex') |
|
| 2139 | 2139 | { |
| 2140 | 2140 | $values = $this->get_fields_regex($html, $conf['selector'], $conf['name']); |
| 2141 | 2141 | } |
@@ -2288,7 +2288,7 @@ discard block |
||
| 2288 | 2288 | |
| 2289 | 2289 | $config = self::$db_config; |
| 2290 | 2290 | @mysqli_connect($config['host'], $config['user'], $config['pass'], $config['name'], $config['port']); |
| 2291 | - if(mysqli_connect_errno()) |
|
| 2291 | + if (mysqli_connect_errno()) |
|
| 2292 | 2292 | { |
| 2293 | 2293 | log::error('Export data to a database need Mysql support, '.mysqli_connect_error()); |
| 2294 | 2294 | exit; |
@@ -2308,7 +2308,7 @@ discard block |
||
| 2308 | 2308 | |
| 2309 | 2309 | public function check_cache() |
| 2310 | 2310 | { |
| 2311 | - if ( !self::$use_redis || self::$save_running_state) |
|
| 2311 | + if (!self::$use_redis || self::$save_running_state) |
|
| 2312 | 2312 | { |
| 2313 | 2313 | return false; |
| 2314 | 2314 | } |
@@ -2318,7 +2318,7 @@ discard block |
||
| 2318 | 2318 | //$count = count($keys); |
| 2319 | 2319 | // 直接检查db,清空的时候整个db清空,所以注意db不要跟其他项目混用 |
| 2320 | 2320 | $count = queue::dbsize(); |
| 2321 | - if ( $count > 0 ) |
|
| 2321 | + if ($count > 0) |
|
| 2322 | 2322 | { |
| 2323 | 2323 | // After this operation, 4,318 kB of additional disk space will be used. |
| 2324 | 2324 | // Do you want to continue? [Y/n] |
@@ -2327,7 +2327,7 @@ discard block |
||
| 2327 | 2327 | $msg .= 'Do you want to continue? [Y/n]'; |
| 2328 | 2328 | fwrite(STDOUT, $msg); |
| 2329 | 2329 | $arg = strtolower(trim(fgets(STDIN))); |
| 2330 | - $arg = empty($arg) || !in_array($arg, array('Y', 'N', 'y','n')) ? 'y' : strtolower($arg); |
|
| 2330 | + $arg = empty($arg) || !in_array($arg, array('Y', 'N', 'y', 'n')) ? 'y' : strtolower($arg); |
|
| 2331 | 2331 | if ($arg == 'n') |
| 2332 | 2332 | { |
| 2333 | 2333 | log::warn('Clear redis data...'); |
@@ -2370,9 +2370,9 @@ discard block |
||
| 2370 | 2370 | public function set_task_status() |
| 2371 | 2371 | { |
| 2372 | 2372 | // 每采集成功一个页面, 生成当前进程状态到文件, 供主进程使用 |
| 2373 | - $mem = round(memory_get_usage(true)/(1024*1024),2); |
|
| 2374 | - $use_time = microtime(true) - self::$time_start; |
|
| 2375 | - $speed = round((self::$collect_succ + self::$collect_fail) / $use_time, 2); |
|
| 2373 | + $mem = round(memory_get_usage(true) / (1024 * 1024), 2); |
|
| 2374 | + $use_time = microtime(true)-self::$time_start; |
|
| 2375 | + $speed = round((self::$collect_succ+self::$collect_fail) / $use_time, 2); |
|
| 2376 | 2376 | $status = array( |
| 2377 | 2377 | 'id' => self::$taskid, |
| 2378 | 2378 | 'pid' => self::$taskpid, |
@@ -2472,7 +2472,7 @@ discard block |
||
| 2472 | 2472 | // 更新服务器列表 |
| 2473 | 2473 | $server_list_json = queue::get('server_list'); |
| 2474 | 2474 | $server_list = array(); |
| 2475 | - if ( ! $server_list_json) |
|
| 2475 | + if (!$server_list_json) |
|
| 2476 | 2476 | { |
| 2477 | 2477 | $server_list[$serverid] = array( |
| 2478 | 2478 | 'serverid' => $serverid, |
@@ -2678,7 +2678,7 @@ discard block |
||
| 2678 | 2678 | { |
| 2679 | 2679 | $exists = queue::exists($key); |
| 2680 | 2680 | // 不存在或者当然URL可重复入 |
| 2681 | - if ( ! $exists || $allowed_repeat) |
|
| 2681 | + if (!$exists || $allowed_repeat) |
|
| 2682 | 2682 | { |
| 2683 | 2683 | // 待爬取网页记录数加一 |
| 2684 | 2684 | queue::incr('collect_urls_num'); |
@@ -2906,7 +2906,7 @@ discard block |
||
| 2906 | 2906 | */ |
| 2907 | 2907 | public function incr_pages_num($url = '') |
| 2908 | 2908 | { |
| 2909 | - if ( ! empty($url)) |
|
| 2909 | + if (!empty($url)) |
|
| 2910 | 2910 | { |
| 2911 | 2911 | $domain = $this->getRootDomain($url, 'host'); |
| 2912 | 2912 | } |
@@ -2942,7 +2942,7 @@ discard block |
||
| 2942 | 2942 | */ |
| 2943 | 2943 | public function incr_duration_num($url = '', $time_run = 1) |
| 2944 | 2944 | { |
| 2945 | - if ( ! empty($url)) |
|
| 2945 | + if (!empty($url)) |
|
| 2946 | 2946 | { |
| 2947 | 2947 | $domain = $this->getRootDomain($url); |
| 2948 | 2948 | } |
@@ -2978,7 +2978,7 @@ discard block |
||
| 2978 | 2978 | */ |
| 2979 | 2979 | public function get_duration_num($url = '') |
| 2980 | 2980 | { |
| 2981 | - if ( ! empty($url)) |
|
| 2981 | + if (!empty($url)) |
|
| 2982 | 2982 | { |
| 2983 | 2983 | $domain = $this->getRootDomain($url); |
| 2984 | 2984 | } |
@@ -2992,7 +2992,7 @@ discard block |
||
| 2992 | 2992 | } |
| 2993 | 2993 | else |
| 2994 | 2994 | { |
| 2995 | - $duration[$domain] = ! empty(self::$duration[$domain]) ? self::$duration[$domain] : 0; |
|
| 2995 | + $duration[$domain] = !empty(self::$duration[$domain]) ? self::$duration[$domain] : 0; |
|
| 2996 | 2996 | } |
| 2997 | 2997 | return $duration[$domain] ? $duration[$domain] : 0; |
| 2998 | 2998 | } |
@@ -3159,7 +3159,7 @@ discard block |
||
| 3159 | 3159 | { |
| 3160 | 3160 | static $last_lines = 0; |
| 3161 | 3161 | |
| 3162 | - if(!is_null($force_clear_lines)) |
|
| 3162 | + if (!is_null($force_clear_lines)) |
|
| 3163 | 3163 | { |
| 3164 | 3164 | $last_lines = $force_clear_lines; |
| 3165 | 3165 | } |
@@ -3167,19 +3167,19 @@ discard block |
||
| 3167 | 3167 | // 获取终端宽度 |
| 3168 | 3168 | $toss = $status = null; |
| 3169 | 3169 | $term_width = exec('tput cols', $toss, $status); |
| 3170 | - if($status || empty($term_width)) |
|
| 3170 | + if ($status || empty($term_width)) |
|
| 3171 | 3171 | { |
| 3172 | 3172 | $term_width = 64; // Arbitrary fall-back term width. |
| 3173 | 3173 | } |
| 3174 | 3174 | |
| 3175 | 3175 | $line_count = 0; |
| 3176 | - foreach(explode("\n", $message) as $line) |
|
| 3176 | + foreach (explode("\n", $message) as $line) |
|
| 3177 | 3177 | { |
| 3178 | 3178 | $line_count += count(str_split($line, $term_width)); |
| 3179 | 3179 | } |
| 3180 | 3180 | |
| 3181 | 3181 | // Erasure MAGIC: Clear as many lines as the last output had. |
| 3182 | - for($i = 0; $i < $last_lines; $i++) |
|
| 3182 | + for ($i = 0; $i < $last_lines; $i++) |
|
| 3183 | 3183 | { |
| 3184 | 3184 | // Return to the beginning of the line |
| 3185 | 3185 | echo "\r"; |
@@ -3215,7 +3215,7 @@ discard block |
||
| 3215 | 3215 | } |
| 3216 | 3216 | $display_str = "\033[1A\n\033[K-----------------------------\033[47;30m PHPSPIDER \033[0m-----------------------------\n\033[0m"; |
| 3217 | 3217 | //$display_str = "-----------------------------\033[47;30m PHPSPIDER \033[0m-----------------------------\n\033[0m"; |
| 3218 | - $run_time_str = util::time2second(time() - self::$time_start, false); |
|
| 3218 | + $run_time_str = util::time2second(time()-self::$time_start, false); |
|
| 3219 | 3219 | $display_str .= 'PHPSpider version:'.self::VERSION.' PHP version:'.PHP_VERSION."\n"; |
| 3220 | 3220 | $display_str .= 'start time:'.date('Y-m-d H:i:s', self::$time_start).' run '.$run_time_str." \n"; |
| 3221 | 3221 | |
@@ -3255,12 +3255,12 @@ discard block |
||
| 3255 | 3255 | { |
| 3256 | 3256 | $display_str = "-------------------------------\033[47;30m TASKS \033[0m-------------------------------\n"; |
| 3257 | 3257 | |
| 3258 | - $display_str .= "\033[47;30mtaskid\033[0m". str_pad('', self::$taskid_length+2-strlen('taskid')). |
|
| 3259 | - "\033[47;30mtaskpid\033[0m". str_pad('', self::$pid_length+2-strlen('taskpid')). |
|
| 3260 | - "\033[47;30mmem\033[0m". str_pad('', self::$mem_length+2-strlen('mem')). |
|
| 3261 | - "\033[47;30mcollect succ\033[0m". str_pad('', self::$urls_length-strlen('collect succ')). |
|
| 3262 | - "\033[47;30mcollect fail\033[0m". str_pad('', self::$urls_length-strlen('collect fail')). |
|
| 3263 | - "\033[47;30mspeed\033[0m". str_pad('', self::$speed_length+2-strlen('speed')). |
|
| 3258 | + $display_str .= "\033[47;30mtaskid\033[0m".str_pad('', self::$taskid_length+2-strlen('taskid')). |
|
| 3259 | + "\033[47;30mtaskpid\033[0m".str_pad('', self::$pid_length+2-strlen('taskpid')). |
|
| 3260 | + "\033[47;30mmem\033[0m".str_pad('', self::$mem_length+2-strlen('mem')). |
|
| 3261 | + "\033[47;30mcollect succ\033[0m".str_pad('', self::$urls_length-strlen('collect succ')). |
|
| 3262 | + "\033[47;30mcollect fail\033[0m".str_pad('', self::$urls_length-strlen('collect fail')). |
|
| 3263 | + "\033[47;30mspeed\033[0m".str_pad('', self::$speed_length+2-strlen('speed')). |
|
| 3264 | 3264 | "\n"; |
| 3265 | 3265 | |
| 3266 | 3266 | // "\033[32;40m [OK] \033[0m" |
@@ -3272,12 +3272,12 @@ discard block |
||
| 3272 | 3272 | { |
| 3273 | 3273 | continue; |
| 3274 | 3274 | } |
| 3275 | - $display_str .= str_pad($task['id'], self::$taskid_length + 2). |
|
| 3276 | - str_pad($task['pid'], self::$pid_length + 2). |
|
| 3277 | - str_pad($task['mem'].'MB', self::$mem_length + 2). |
|
| 3275 | + $display_str .= str_pad($task['id'], self::$taskid_length+2). |
|
| 3276 | + str_pad($task['pid'], self::$pid_length+2). |
|
| 3277 | + str_pad($task['mem'].'MB', self::$mem_length+2). |
|
| 3278 | 3278 | str_pad($task['collect_succ'], self::$urls_length). |
| 3279 | 3279 | str_pad($task['collect_fail'], self::$urls_length). |
| 3280 | - str_pad($task['speed'].'/s', self::$speed_length + 2). |
|
| 3280 | + str_pad($task['speed'].'/s', self::$speed_length+2). |
|
| 3281 | 3281 | "\n"; |
| 3282 | 3282 | } |
| 3283 | 3283 | //echo "\033[9;0H"; |
@@ -3288,12 +3288,12 @@ discard block |
||
| 3288 | 3288 | { |
| 3289 | 3289 | $display_str = "-------------------------------\033[47;30m SERVER \033[0m------------------------------\n"; |
| 3290 | 3290 | |
| 3291 | - $display_str .= "\033[47;30mserver\033[0m". str_pad('', self::$server_length+2-strlen('serverid')). |
|
| 3292 | - "\033[47;30mtasknum\033[0m". str_pad('', self::$tasknum_length+2-strlen('tasknum')). |
|
| 3293 | - "\033[47;30mmem\033[0m". str_pad('', self::$mem_length+2-strlen('mem')). |
|
| 3294 | - "\033[47;30mcollect succ\033[0m". str_pad('', self::$urls_length-strlen('collect succ')). |
|
| 3295 | - "\033[47;30mcollect fail\033[0m". str_pad('', self::$urls_length-strlen('collect fail')). |
|
| 3296 | - "\033[47;30mspeed\033[0m". str_pad('', self::$speed_length+2-strlen('speed')). |
|
| 3291 | + $display_str .= "\033[47;30mserver\033[0m".str_pad('', self::$server_length+2-strlen('serverid')). |
|
| 3292 | + "\033[47;30mtasknum\033[0m".str_pad('', self::$tasknum_length+2-strlen('tasknum')). |
|
| 3293 | + "\033[47;30mmem\033[0m".str_pad('', self::$mem_length+2-strlen('mem')). |
|
| 3294 | + "\033[47;30mcollect succ\033[0m".str_pad('', self::$urls_length-strlen('collect succ')). |
|
| 3295 | + "\033[47;30mcollect fail\033[0m".str_pad('', self::$urls_length-strlen('collect fail')). |
|
| 3296 | + "\033[47;30mspeed\033[0m".str_pad('', self::$speed_length+2-strlen('speed')). |
|
| 3297 | 3297 | "\n"; |
| 3298 | 3298 | |
| 3299 | 3299 | $server_list_json = queue::get('server_list'); |
@@ -3320,11 +3320,11 @@ discard block |
||
| 3320 | 3320 | } |
| 3321 | 3321 | |
| 3322 | 3322 | $display_str .= str_pad($serverid, self::$server_length). |
| 3323 | - str_pad($tasknum, self::$tasknum_length + 2). |
|
| 3324 | - str_pad($mem.'MB', self::$mem_length + 2). |
|
| 3323 | + str_pad($tasknum, self::$tasknum_length+2). |
|
| 3324 | + str_pad($mem.'MB', self::$mem_length+2). |
|
| 3325 | 3325 | str_pad($collect_succ, self::$urls_length). |
| 3326 | 3326 | str_pad($collect_fail, self::$urls_length). |
| 3327 | - str_pad($speed.'/s', self::$speed_length + 2). |
|
| 3327 | + str_pad($speed.'/s', self::$speed_length+2). |
|
| 3328 | 3328 | "\n"; |
| 3329 | 3329 | } |
| 3330 | 3330 | return $display_str; |
@@ -3334,11 +3334,11 @@ discard block |
||
| 3334 | 3334 | { |
| 3335 | 3335 | $display_str = "---------------------------\033[47;30m COLLECT STATUS \033[0m--------------------------\n"; |
| 3336 | 3336 | |
| 3337 | - $display_str .= "\033[47;30mfind pages\033[0m". str_pad('', 16-strlen('find pages')). |
|
| 3338 | - "\033[47;30mqueue\033[0m". str_pad('', 14-strlen('queue')). |
|
| 3339 | - "\033[47;30mcollected\033[0m". str_pad('', 15-strlen('collected')). |
|
| 3340 | - "\033[47;30mfields\033[0m". str_pad('', 15-strlen('fields')). |
|
| 3341 | - "\033[47;30mdepth\033[0m". str_pad('', 12-strlen('depth')). |
|
| 3337 | + $display_str .= "\033[47;30mfind pages\033[0m".str_pad('', 16-strlen('find pages')). |
|
| 3338 | + "\033[47;30mqueue\033[0m".str_pad('', 14-strlen('queue')). |
|
| 3339 | + "\033[47;30mcollected\033[0m".str_pad('', 15-strlen('collected')). |
|
| 3340 | + "\033[47;30mfields\033[0m".str_pad('', 15-strlen('fields')). |
|
| 3341 | + "\033[47;30mdepth\033[0m".str_pad('', 12-strlen('depth')). |
|
| 3342 | 3342 | "\n"; |
| 3343 | 3343 | |
| 3344 | 3344 | $collect = $this->get_collect_url_num(); |
@@ -3448,7 +3448,7 @@ discard block |
||
| 3448 | 3448 | if (self::$use_redis) |
| 3449 | 3449 | { |
| 3450 | 3450 | $count = queue::get($domain); |
| 3451 | - if ( ! empty(self::$configs['max_sub_num']) and $count > self::$configs['max_sub_num']) |
|
| 3451 | + if (!empty(self::$configs['max_sub_num']) and $count > self::$configs['max_sub_num']) |
|
| 3452 | 3452 | { |
| 3453 | 3453 | return $count; |
| 3454 | 3454 | } |
@@ -3458,7 +3458,7 @@ discard block |
||
| 3458 | 3458 | } |
| 3459 | 3459 | $hostkey = 'sub_d-'.$host; |
| 3460 | 3460 | $exists = queue::exists($hostkey); |
| 3461 | - if ( ! $exists) |
|
| 3461 | + if (!$exists) |
|
| 3462 | 3462 | { |
| 3463 | 3463 | // 子域名数量加一 |
| 3464 | 3464 | $count = queue::incr($domain); |
@@ -3476,7 +3476,7 @@ discard block |
||
| 3476 | 3476 | return $url; |
| 3477 | 3477 | } |
| 3478 | 3478 | $url = trim($url); |
| 3479 | - if ( ! preg_match('/^http/i', $url)) |
|
| 3479 | + if (!preg_match('/^http/i', $url)) |
|
| 3480 | 3480 | { |
| 3481 | 3481 | $url = 'http://'.$url; |
| 3482 | 3482 | } |
@@ -3511,7 +3511,7 @@ discard block |
||
| 3511 | 3511 | $count = count($urlarr); |
| 3512 | 3512 | $res['scheme'] = $url_parse['scheme']; |
| 3513 | 3513 | $res['host'] = $url_parse['host']; |
| 3514 | - if ( ! empty($url_parse['path'])) |
|
| 3514 | + if (!empty($url_parse['path'])) |
|
| 3515 | 3515 | { |
| 3516 | 3516 | $res['path'] = $url_parse['path']; |
| 3517 | 3517 | } |
@@ -3538,7 +3538,7 @@ discard block |
||
| 3538 | 3538 | $res['name'] = $last_2; |
| 3539 | 3539 | |
| 3540 | 3540 | //排除非标准 ltd 域名 |
| 3541 | - if ( ! in_array($last, $state_domain)) |
|
| 3541 | + if (!in_array($last, $state_domain)) |
|
| 3542 | 3542 | { |
| 3543 | 3543 | return false; |
| 3544 | 3544 | } |
@@ -3550,14 +3550,14 @@ discard block |
||
| 3550 | 3550 | $res['tld'] = $last; |
| 3551 | 3551 | } |
| 3552 | 3552 | //排除顶级根二级后缀 |
| 3553 | - if ($last_1 !== $last and in_array($last_1, $state_domain) and ! in_array($last, array('com', 'net', 'org', 'edu', 'gov'))) |
|
| 3553 | + if ($last_1 !== $last and in_array($last_1, $state_domain) and !in_array($last, array('com', 'net', 'org', 'edu', 'gov'))) |
|
| 3554 | 3554 | { |
| 3555 | 3555 | $res['domain'] = $last_2.'.'.$last_1.'.'.$last; //n.n.com形式 |
| 3556 | 3556 | $res['name'] = $last_2; |
| 3557 | 3557 | $res['tld'] = $last_1.'.'.$last; |
| 3558 | 3558 | } |
| 3559 | 3559 | //限定cn顶级根二级后缀为'com', 'net', 'org', 'edu', 'gov' |
| 3560 | - if (in_array($last, array('cn')) and $last_1 !== $last and strlen($last_1) > 2 and ! in_array($last_1, array('com', 'net', 'org', 'edu', 'gov'))) |
|
| 3560 | + if (in_array($last, array('cn')) and $last_1 !== $last and strlen($last_1) > 2 and !in_array($last_1, array('com', 'net', 'org', 'edu', 'gov'))) |
|
| 3561 | 3561 | { |
| 3562 | 3562 | $res['domain'] = $last_1.'.'.$last; //n.n.cn形式 |
| 3563 | 3563 | $res['name'] = $last_1; |
@@ -3566,7 +3566,7 @@ discard block |
||
| 3566 | 3566 | } |
| 3567 | 3567 | |
| 3568 | 3568 | //检测和验证返回的是不是域名格式 |
| 3569 | - if ( ! empty($res['domain']) and preg_match('/^([\p{Han}a-zA-Z0-9])+([\p{Han}a-zA-Z0-9\-])*\.[a-zA-Z\.\p{Han}]+$/iu', $res['domain'])) |
|
| 3569 | + if (!empty($res['domain']) and preg_match('/^([\p{Han}a-zA-Z0-9])+([\p{Han}a-zA-Z0-9\-])*\.[a-zA-Z\.\p{Han}]+$/iu', $res['domain'])) |
|
| 3570 | 3570 | { |
| 3571 | 3571 | if ($type == 'arr') |
| 3572 | 3572 | { |
@@ -436,8 +436,7 @@ discard block |
||
| 436 | 436 | if ($configs['max_task_per_host'] > 0) |
| 437 | 437 | { |
| 438 | 438 | $configs['queue_order'] = 'rand'; |
| 439 | - } |
|
| 440 | - else |
|
| 439 | + } else |
|
| 441 | 440 | { |
| 442 | 441 | $configs['queue_order'] = isset($configs['queue_order']) ? $configs['queue_order'] : 'list'; |
| 443 | 442 | } |
@@ -512,13 +511,11 @@ discard block |
||
| 512 | 511 | { |
| 513 | 512 | $link['url_type'] = 'content_page'; |
| 514 | 513 | $status = $this->queue_lpush($link, $allowed_repeat); |
| 515 | - } |
|
| 516 | - elseif ($this->is_list_page($url)) |
|
| 514 | + } elseif ($this->is_list_page($url)) |
|
| 517 | 515 | { |
| 518 | 516 | $link['url_type'] = 'list_page'; |
| 519 | 517 | $status = $this->queue_lpush($link, $allowed_repeat); |
| 520 | - } |
|
| 521 | - else |
|
| 518 | + } else |
|
| 522 | 519 | { |
| 523 | 520 | $status = $this->queue_lpush($link, $allowed_repeat); |
| 524 | 521 | } |
@@ -528,12 +525,10 @@ discard block |
||
| 528 | 525 | if ($link['url_type'] == 'scan_page') |
| 529 | 526 | { |
| 530 | 527 | log::debug("Find scan page: {$url}"); |
| 531 | - } |
|
| 532 | - elseif ($link['url_type'] == 'content_page') |
|
| 528 | + } elseif ($link['url_type'] == 'content_page') |
|
| 533 | 529 | { |
| 534 | 530 | log::debug("Find content page: {$url}"); |
| 535 | - } |
|
| 536 | - elseif ($link['url_type'] == 'list_page') |
|
| 531 | + } elseif ($link['url_type'] == 'list_page') |
|
| 537 | 532 | { |
| 538 | 533 | log::debug("Find list page: {$url}"); |
| 539 | 534 | } |
@@ -577,8 +572,7 @@ discard block |
||
| 577 | 572 | { |
| 578 | 573 | $link['url_type'] = 'content_page'; |
| 579 | 574 | $status = $this->queue_lpush($link); |
| 580 | - } |
|
| 581 | - elseif ($this->is_list_page($url)) |
|
| 575 | + } elseif ($this->is_list_page($url)) |
|
| 582 | 576 | { |
| 583 | 577 | $link['url_type'] = 'list_page'; |
| 584 | 578 | $status = $this->queue_lpush($link); |
@@ -589,12 +583,10 @@ discard block |
||
| 589 | 583 | if ($link['url_type'] == 'scan_page') |
| 590 | 584 | { |
| 591 | 585 | log::debug("Find scan page: {$url}"); |
| 592 | - } |
|
| 593 | - elseif ($link['url_type'] == 'content_page') |
|
| 586 | + } elseif ($link['url_type'] == 'content_page') |
|
| 594 | 587 | { |
| 595 | 588 | log::debug("Find content page: {$url}"); |
| 596 | - } |
|
| 597 | - elseif ($link['url_type'] == 'list_page') |
|
| 589 | + } elseif ($link['url_type'] == 'list_page') |
|
| 598 | 590 | { |
| 599 | 591 | log::debug("Find list page: {$url}"); |
| 600 | 592 | } |
@@ -769,8 +761,7 @@ discard block |
||
| 769 | 761 | if (count($info) <= 1) |
| 770 | 762 | { |
| 771 | 763 | echo "PHPSpider[$start_file] not run\n"; |
| 772 | - } |
|
| 773 | - else |
|
| 764 | + } else |
|
| 774 | 765 | { |
| 775 | 766 | //echo "PHPSpider[$start_file] is stoping ...\n"; |
| 776 | 767 | echo "PHPSpider[$start_file] stop success"; |
@@ -851,8 +842,7 @@ discard block |
||
| 851 | 842 | if (-1 === $pid) |
| 852 | 843 | { |
| 853 | 844 | throw new Exception('fork fail'); |
| 854 | - } |
|
| 855 | - elseif ($pid > 0) |
|
| 845 | + } elseif ($pid > 0) |
|
| 856 | 846 | { |
| 857 | 847 | exit(0); |
| 858 | 848 | } |
@@ -865,8 +855,7 @@ discard block |
||
| 865 | 855 | if (-1 === $pid) |
| 866 | 856 | { |
| 867 | 857 | throw new Exception('fork fail'); |
| 868 | - } |
|
| 869 | - elseif (0 !== $pid) |
|
| 858 | + } elseif (0 !== $pid) |
|
| 870 | 859 | { |
| 871 | 860 | exit(0); |
| 872 | 861 | } |
@@ -907,8 +896,7 @@ discard block |
||
| 907 | 896 | if ($all_stop) |
| 908 | 897 | { |
| 909 | 898 | break; |
| 910 | - } |
|
| 911 | - else |
|
| 899 | + } else |
|
| 912 | 900 | { |
| 913 | 901 | log::warn('Task stop waiting...'); |
| 914 | 902 | } |
@@ -1035,8 +1023,7 @@ discard block |
||
| 1035 | 1023 | elseif (self::$daemonize) |
| 1036 | 1024 | { |
| 1037 | 1025 | log::$log_show = true; |
| 1038 | - } |
|
| 1039 | - else |
|
| 1026 | + } else |
|
| 1040 | 1027 | { |
| 1041 | 1028 | log::$log_show = isset(self::$configs['log_show']) ? self::$configs['log_show'] : false; |
| 1042 | 1029 | } |
@@ -1101,8 +1088,7 @@ discard block |
||
| 1101 | 1088 | // 先显示一次面板, 然后下面再每次采集成功显示一次 |
| 1102 | 1089 | $this->display_ui(); |
| 1103 | 1090 | } |
| 1104 | - } |
|
| 1105 | - else |
|
| 1091 | + } else |
|
| 1106 | 1092 | { |
| 1107 | 1093 | $this->daemonize(); |
| 1108 | 1094 | } |
@@ -1163,8 +1149,7 @@ discard block |
||
| 1163 | 1149 | |
| 1164 | 1150 | // 这里用0表示子进程正常退出 |
| 1165 | 1151 | exit(0); |
| 1166 | - } |
|
| 1167 | - else |
|
| 1152 | + } else |
|
| 1168 | 1153 | { |
| 1169 | 1154 | log::error("Fork children task({$taskid}) fail..."); |
| 1170 | 1155 | exit; |
@@ -1229,8 +1214,7 @@ discard block |
||
| 1229 | 1214 | $this->collect_page(); |
| 1230 | 1215 | // 保存任务状态 |
| 1231 | 1216 | $this->set_task_status(); |
| 1232 | - } |
|
| 1233 | - else |
|
| 1217 | + } else |
|
| 1234 | 1218 | { |
| 1235 | 1219 | log::warn('Task('.self::$taskid.') waiting...reason: queue_lsize = '.$queue_lsize.' < tasknum = '.self::$tasknum); |
| 1236 | 1220 | sleep(1); |
@@ -1317,8 +1301,7 @@ discard block |
||
| 1317 | 1301 | if ($task_per_host < self::$configs['max_task_per_host']) |
| 1318 | 1302 | { |
| 1319 | 1303 | $task_per_host = $this->incr_task_per_host($url); |
| 1320 | - } |
|
| 1321 | - else |
|
| 1304 | + } else |
|
| 1322 | 1305 | { |
| 1323 | 1306 | log::warn('Task('.self::$taskid.') task_per_host = '.$task_per_host.' > '.self::$configs['max_task_per_host'].' ; URL: '.$url.' will be retry later...'); |
| 1324 | 1307 | $this->queue_lpush($link); //放回队列 |
@@ -1338,7 +1321,9 @@ discard block |
||
| 1338 | 1321 | if ($this->on_before_download_page) |
| 1339 | 1322 | { |
| 1340 | 1323 | $return = call_user_func($this->on_before_download_page, $url, $link, $this); |
| 1341 | - if (isset($return)) $link = $return; |
|
| 1324 | + if (isset($return)) { |
|
| 1325 | + $link = $return; |
|
| 1326 | + } |
|
| 1342 | 1327 | } |
| 1343 | 1328 | |
| 1344 | 1329 | requests::$input_encoding = null; |
@@ -1420,8 +1405,7 @@ discard block |
||
| 1420 | 1405 | |
| 1421 | 1406 | unset($return); |
| 1422 | 1407 | } |
| 1423 | - } |
|
| 1424 | - elseif ($link['url_type'] == 'content_page') |
|
| 1408 | + } elseif ($link['url_type'] == 'content_page') |
|
| 1425 | 1409 | { |
| 1426 | 1410 | if ($this->on_content_page) |
| 1427 | 1411 | { |
@@ -1432,8 +1416,7 @@ discard block |
||
| 1432 | 1416 | } |
| 1433 | 1417 | unset($return); |
| 1434 | 1418 | } |
| 1435 | - } |
|
| 1436 | - elseif ($link['url_type'] == 'list_page') |
|
| 1419 | + } elseif ($link['url_type'] == 'list_page') |
|
| 1437 | 1420 | { |
| 1438 | 1421 | if ($this->on_list_page) |
| 1439 | 1422 | { |
@@ -1585,13 +1568,11 @@ discard block |
||
| 1585 | 1568 | { |
| 1586 | 1569 | $html .= $link['context_data']; |
| 1587 | 1570 | } |
| 1588 | - } |
|
| 1589 | - else |
|
| 1571 | + } else |
|
| 1590 | 1572 | { |
| 1591 | 1573 | return false; |
| 1592 | 1574 | } |
| 1593 | - } |
|
| 1594 | - else |
|
| 1575 | + } else |
|
| 1595 | 1576 | { |
| 1596 | 1577 | if ( ! empty(self::$configs['max_try']) and $http_code == 407) |
| 1597 | 1578 | { |
@@ -1599,8 +1580,7 @@ discard block |
||
| 1599 | 1580 | $this->queue_rpush($link); |
| 1600 | 1581 | log::error("Failed to download page {$url}"); |
| 1601 | 1582 | self::$collect_fail++; |
| 1602 | - } |
|
| 1603 | - elseif ( ! empty(self::$configs['max_try']) and in_array($http_code, array('0', '502', '503', '429'))) |
|
| 1583 | + } elseif ( ! empty(self::$configs['max_try']) and in_array($http_code, array('0', '502', '503', '429'))) |
|
| 1604 | 1584 | { |
| 1605 | 1585 | // 采集次数加一 |
| 1606 | 1586 | $link['try_num']++; |
@@ -1611,8 +1591,7 @@ discard block |
||
| 1611 | 1591 | $this->queue_rpush($link); |
| 1612 | 1592 | } |
| 1613 | 1593 | log::error("Failed to download page {$url}, retry({$link['try_num']})"); |
| 1614 | - } |
|
| 1615 | - else |
|
| 1594 | + } else |
|
| 1616 | 1595 | { |
| 1617 | 1596 | log::error("Failed to download page {$url}"); |
| 1618 | 1597 | self::$collect_fail++; |
@@ -1711,8 +1690,7 @@ discard block |
||
| 1711 | 1690 | if ($val) |
| 1712 | 1691 | { |
| 1713 | 1692 | $urls[$k] = $val; |
| 1714 | - } |
|
| 1715 | - else |
|
| 1693 | + } else |
|
| 1716 | 1694 | { |
| 1717 | 1695 | unset($urls[$k]); |
| 1718 | 1696 | } |
@@ -1772,11 +1750,12 @@ discard block |
||
| 1772 | 1750 | return false; |
| 1773 | 1751 | } |
| 1774 | 1752 | // 排除没有被解析成功的语言标签 |
| 1775 | - if (substr($url, 0, 3) == '<%=' or substr($url, 0, 1) == '{' or substr($url, 0, 2) == ' {') |
|
| 1776 | - // if(substr($url, 0, 3) == '<%=') |
|
| 1753 | + if (substr($url, 0, 3) == '<%=' or substr($url, 0, 1) == '{' or substr($url, 0, 2) == ' {') { |
|
| 1754 | + // if(substr($url, 0, 3) == '<%=') |
|
| 1777 | 1755 | { |
| 1778 | 1756 | return false; |
| 1779 | 1757 | } |
| 1758 | + } |
|
| 1780 | 1759 | |
| 1781 | 1760 | $parse_url = @parse_url($collect_url); |
| 1782 | 1761 | if (empty($parse_url['scheme']) || empty($parse_url['host'])) |
@@ -1819,8 +1798,7 @@ discard block |
||
| 1819 | 1798 | if(!isset($url[2])) |
| 1820 | 1799 | { |
| 1821 | 1800 | return false; |
| 1822 | - } |
|
| 1823 | - else |
|
| 1801 | + } else |
|
| 1824 | 1802 | { |
| 1825 | 1803 | $urls = explode('/',$url); |
| 1826 | 1804 | foreach($urls as $u) |
@@ -1833,8 +1811,7 @@ discard block |
||
| 1833 | 1811 | else if( $i < count($urls)-1 ) |
| 1834 | 1812 | { |
| 1835 | 1813 | $dstr .= $urls[$i].'/'; |
| 1836 | - } |
|
| 1837 | - else |
|
| 1814 | + } else |
|
| 1838 | 1815 | { |
| 1839 | 1816 | $dstr .= $urls[$i]; |
| 1840 | 1817 | } |
@@ -1844,23 +1821,20 @@ discard block |
||
| 1844 | 1821 | if(count($urls) <= $path_step) |
| 1845 | 1822 | { |
| 1846 | 1823 | return false; |
| 1847 | - } |
|
| 1848 | - else |
|
| 1824 | + } else |
|
| 1849 | 1825 | { |
| 1850 | 1826 | $pstr = ''; |
| 1851 | 1827 | for($i=0;$i<count($urls)-$path_step;$i++){ $pstr .= $urls[$i].'/'; } |
| 1852 | 1828 | $url = $pstr.$dstr; |
| 1853 | 1829 | } |
| 1854 | 1830 | } |
| 1855 | - } |
|
| 1856 | - else |
|
| 1831 | + } else |
|
| 1857 | 1832 | { |
| 1858 | 1833 | if( strtolower(substr($url, 0, 7))=='http://' ) |
| 1859 | 1834 | { |
| 1860 | 1835 | $url = preg_replace('#^http://#i', '', $url); |
| 1861 | 1836 | $scheme = 'http'; |
| 1862 | - } |
|
| 1863 | - else if( strtolower(substr($url, 0, 8))=='https://' ) |
|
| 1837 | + } else if( strtolower(substr($url, 0, 8))=='https://' ) |
|
| 1864 | 1838 | { |
| 1865 | 1839 | $url = preg_replace('#^https://#i','',$url); |
| 1866 | 1840 | $scheme = "https"; |
@@ -2008,12 +1982,10 @@ discard block |
||
| 2008 | 1982 | elseif ($return === false) |
| 2009 | 1983 | { |
| 2010 | 1984 | return false; |
| 2011 | - } |
|
| 2012 | - elseif (!is_array($return)) |
|
| 1985 | + } elseif (!is_array($return)) |
|
| 2013 | 1986 | { |
| 2014 | 1987 | log::warn('on_extract_page return value must be an array'); |
| 2015 | - } |
|
| 2016 | - else |
|
| 1988 | + } else |
|
| 2017 | 1989 | { |
| 2018 | 1990 | $fields = $return; |
| 2019 | 1991 | } |
@@ -2034,8 +2006,7 @@ discard block |
||
| 2034 | 2006 | { |
| 2035 | 2007 | return @iconv('UCS-2BE', 'UTF-8', pack('H4', $matchs[1])); |
| 2036 | 2008 | }, $fields_str); |
| 2037 | - } |
|
| 2038 | - else |
|
| 2009 | + } else |
|
| 2039 | 2010 | { |
| 2040 | 2011 | $fields_str = json_encode($fields, JSON_UNESCAPED_UNICODE); |
| 2041 | 2012 | } |
@@ -2053,13 +2024,11 @@ discard block |
||
| 2053 | 2024 | if (self::$export_type == 'csv') |
| 2054 | 2025 | { |
| 2055 | 2026 | util::put_file(self::$export_file, util::format_csv($fields)."\n", FILE_APPEND); |
| 2056 | - } |
|
| 2057 | - elseif (self::$export_type == 'sql') |
|
| 2027 | + } elseif (self::$export_type == 'sql') |
|
| 2058 | 2028 | { |
| 2059 | 2029 | $sql = db::insert(self::$export_table, $fields, true); |
| 2060 | 2030 | util::put_file(self::$export_file, $sql.";\n", FILE_APPEND); |
| 2061 | - } |
|
| 2062 | - elseif (self::$export_type == 'db') |
|
| 2031 | + } elseif (self::$export_type == 'db') |
|
| 2063 | 2032 | { |
| 2064 | 2033 | db::insert(self::$export_table, $fields); |
| 2065 | 2034 | } |
@@ -2130,12 +2099,10 @@ discard block |
||
| 2130 | 2099 | { |
| 2131 | 2100 | // 如果找不到,返回的是false |
| 2132 | 2101 | $values = $this->get_fields_xpath($html, $conf['selector'], $conf['name']); |
| 2133 | - } |
|
| 2134 | - elseif ($conf['selector_type']=='css') |
|
| 2102 | + } elseif ($conf['selector_type']=='css') |
|
| 2135 | 2103 | { |
| 2136 | 2104 | $values = $this->get_fields_css($html, $conf['selector'], $conf['name']); |
| 2137 | - } |
|
| 2138 | - elseif ($conf['selector_type']=='regex') |
|
| 2105 | + } elseif ($conf['selector_type']=='regex') |
|
| 2139 | 2106 | { |
| 2140 | 2107 | $values = $this->get_fields_regex($html, $conf['selector'], $conf['name']); |
| 2141 | 2108 | } |
@@ -2180,21 +2147,18 @@ discard block |
||
| 2180 | 2147 | // 避免内容分页时attached_url拼接时候string + array了 |
| 2181 | 2148 | $fields[$conf['name']] = ''; |
| 2182 | 2149 | //$fields[$conf['name']] = array(); |
| 2183 | - } |
|
| 2184 | - else |
|
| 2150 | + } else |
|
| 2185 | 2151 | { |
| 2186 | 2152 | if (is_array($values)) |
| 2187 | 2153 | { |
| 2188 | 2154 | if ($repeated) |
| 2189 | 2155 | { |
| 2190 | 2156 | $fields[$conf['name']] = $values; |
| 2191 | - } |
|
| 2192 | - else |
|
| 2157 | + } else |
|
| 2193 | 2158 | { |
| 2194 | 2159 | $fields[$conf['name']] = $values[0]; |
| 2195 | 2160 | } |
| 2196 | - } |
|
| 2197 | - else |
|
| 2161 | + } else |
|
| 2198 | 2162 | { |
| 2199 | 2163 | $fields[$conf['name']] = $values; |
| 2200 | 2164 | } |
@@ -2216,8 +2180,7 @@ discard block |
||
| 2216 | 2180 | if (!isset($return)) |
| 2217 | 2181 | { |
| 2218 | 2182 | log::warn("on_handle_img return value can't be empty\n"); |
| 2219 | - } |
|
| 2220 | - else |
|
| 2183 | + } else |
|
| 2221 | 2184 | { |
| 2222 | 2185 | // 有数据才会执行 on_handle_img 方法, 所以这里不要被替换没了 |
| 2223 | 2186 | $data = $return; |
@@ -2231,8 +2194,7 @@ discard block |
||
| 2231 | 2194 | if (!isset($return)) |
| 2232 | 2195 | { |
| 2233 | 2196 | log::warn("on_extract_field return value can't be empty\n"); |
| 2234 | - } |
|
| 2235 | - else |
|
| 2197 | + } else |
|
| 2236 | 2198 | { |
| 2237 | 2199 | // 有数据才会执行 on_extract_field 方法, 所以这里不要被替换没了 |
| 2238 | 2200 | $fields[$fieldname] = $return; |
@@ -2263,16 +2225,14 @@ discard block |
||
| 2263 | 2225 | log::error('Export data into CSV files need to Set the file path.'); |
| 2264 | 2226 | exit; |
| 2265 | 2227 | } |
| 2266 | - } |
|
| 2267 | - elseif (self::$export_type == 'sql') |
|
| 2228 | + } elseif (self::$export_type == 'sql') |
|
| 2268 | 2229 | { |
| 2269 | 2230 | if (empty(self::$export_file)) |
| 2270 | 2231 | { |
| 2271 | 2232 | log::error('Export data into SQL files need to Set the file path.'); |
| 2272 | 2233 | exit; |
| 2273 | 2234 | } |
| 2274 | - } |
|
| 2275 | - elseif (self::$export_type == 'db') |
|
| 2235 | + } elseif (self::$export_type == 'db') |
|
| 2276 | 2236 | { |
| 2277 | 2237 | if (!function_exists('mysqli_connect')) |
| 2278 | 2238 | { |
@@ -2387,8 +2347,7 @@ discard block |
||
| 2387 | 2347 | { |
| 2388 | 2348 | $key = 'server-'.self::$serverid.'-task_status-'.self::$taskid; |
| 2389 | 2349 | queue::set($key, $task_status); |
| 2390 | - } |
|
| 2391 | - else |
|
| 2350 | + } else |
|
| 2392 | 2351 | { |
| 2393 | 2352 | self::$task_status = array($task_status); |
| 2394 | 2353 | } |
@@ -2447,8 +2406,7 @@ discard block |
||
| 2447 | 2406 | $key = "server-{$serverid}-task_status-".$i; |
| 2448 | 2407 | $task_status[] = queue::get($key); |
| 2449 | 2408 | } |
| 2450 | - } |
|
| 2451 | - else |
|
| 2409 | + } else |
|
| 2452 | 2410 | { |
| 2453 | 2411 | $task_status = self::$task_status; |
| 2454 | 2412 | } |
@@ -2479,8 +2437,7 @@ discard block |
||
| 2479 | 2437 | 'tasknum' => $tasknum, |
| 2480 | 2438 | 'time' => time(), |
| 2481 | 2439 | ); |
| 2482 | - } |
|
| 2483 | - else |
|
| 2440 | + } else |
|
| 2484 | 2441 | { |
| 2485 | 2442 | $server_list = json_decode($server_list_json, true); |
| 2486 | 2443 | $server_list[$serverid] = array( |
@@ -2539,8 +2496,7 @@ discard block |
||
| 2539 | 2496 | if (self::$use_redis) |
| 2540 | 2497 | { |
| 2541 | 2498 | $count = queue::get('collect_urls_num'); |
| 2542 | - } |
|
| 2543 | - else |
|
| 2499 | + } else |
|
| 2544 | 2500 | { |
| 2545 | 2501 | $count = self::$collect_urls_num; |
| 2546 | 2502 | } |
@@ -2560,8 +2516,7 @@ discard block |
||
| 2560 | 2516 | if (self::$use_redis) |
| 2561 | 2517 | { |
| 2562 | 2518 | $count = queue::get('collected_urls_num'); |
| 2563 | - } |
|
| 2564 | - else |
|
| 2519 | + } else |
|
| 2565 | 2520 | { |
| 2566 | 2521 | $count = self::$collected_urls_num; |
| 2567 | 2522 | } |
@@ -2581,8 +2536,7 @@ discard block |
||
| 2581 | 2536 | if (self::$use_redis) |
| 2582 | 2537 | { |
| 2583 | 2538 | queue::incr('collected_urls_num'); |
| 2584 | - } |
|
| 2585 | - else |
|
| 2539 | + } else |
|
| 2586 | 2540 | { |
| 2587 | 2541 | self::$collected_urls_num++; |
| 2588 | 2542 | } |
@@ -2627,8 +2581,7 @@ discard block |
||
| 2627 | 2581 | if (self::$configs['queue_order'] == 'rand') |
| 2628 | 2582 | { |
| 2629 | 2583 | queue::sadd('collect_queue', $link); |
| 2630 | - } |
|
| 2631 | - else |
|
| 2584 | + } else |
|
| 2632 | 2585 | { |
| 2633 | 2586 | queue::lpush('collect_queue', $link); |
| 2634 | 2587 | } |
@@ -2637,8 +2590,7 @@ discard block |
||
| 2637 | 2590 | // 解锁 |
| 2638 | 2591 | queue::unlock($lock); |
| 2639 | 2592 | } |
| 2640 | - } |
|
| 2641 | - else |
|
| 2593 | + } else |
|
| 2642 | 2594 | { |
| 2643 | 2595 | $key = md5($url); |
| 2644 | 2596 | if (!array_key_exists($key, self::$collect_urls)) |
@@ -2690,8 +2642,7 @@ discard block |
||
| 2690 | 2642 | if (self::$configs['queue_order'] == 'rand') |
| 2691 | 2643 | { |
| 2692 | 2644 | queue::sadd('collect_queue', $link); //无序集合 |
| 2693 | - } |
|
| 2694 | - else |
|
| 2645 | + } else |
|
| 2695 | 2646 | { |
| 2696 | 2647 | queue::rpush('collect_queue', $link); //有序列表 |
| 2697 | 2648 | } |
@@ -2700,8 +2651,7 @@ discard block |
||
| 2700 | 2651 | // 解锁 |
| 2701 | 2652 | queue::unlock($lock); |
| 2702 | 2653 | } |
| 2703 | - } |
|
| 2704 | - else |
|
| 2654 | + } else |
|
| 2705 | 2655 | { |
| 2706 | 2656 | $key = md5($url); |
| 2707 | 2657 | if (!array_key_exists($key, self::$collect_urls)) |
@@ -2733,14 +2683,12 @@ discard block |
||
| 2733 | 2683 | if (self::$configs['queue_order'] == 'rand') |
| 2734 | 2684 | { |
| 2735 | 2685 | $link = queue::spop('collect_queue'); |
| 2736 | - } |
|
| 2737 | - else |
|
| 2686 | + } else |
|
| 2738 | 2687 | { |
| 2739 | 2688 | $link = queue::lpop('collect_queue'); |
| 2740 | 2689 | } |
| 2741 | 2690 | $link = json_decode($link, true); |
| 2742 | - } |
|
| 2743 | - else |
|
| 2691 | + } else |
|
| 2744 | 2692 | { |
| 2745 | 2693 | $link = array_pop(self::$collect_queue); |
| 2746 | 2694 | } |
@@ -2762,14 +2710,12 @@ discard block |
||
| 2762 | 2710 | if (self::$configs['queue_order'] == 'rand') |
| 2763 | 2711 | { |
| 2764 | 2712 | $link = queue::spop('collect_queue'); |
| 2765 | - } |
|
| 2766 | - else |
|
| 2713 | + } else |
|
| 2767 | 2714 | { |
| 2768 | 2715 | $link = queue::rpop('collect_queue'); |
| 2769 | 2716 | } |
| 2770 | 2717 | $link = json_decode($link, true); |
| 2771 | - } |
|
| 2772 | - else |
|
| 2718 | + } else |
|
| 2773 | 2719 | { |
| 2774 | 2720 | $link = array_shift(self::$collect_queue); |
| 2775 | 2721 | } |
@@ -2791,13 +2737,11 @@ discard block |
||
| 2791 | 2737 | if (self::$configs['queue_order'] == 'rand') |
| 2792 | 2738 | { |
| 2793 | 2739 | $lsize = queue::scard('collect_queue'); |
| 2794 | - } |
|
| 2795 | - else |
|
| 2740 | + } else |
|
| 2796 | 2741 | { |
| 2797 | 2742 | $lsize = queue::lsize('collect_queue'); |
| 2798 | 2743 | } |
| 2799 | - } |
|
| 2800 | - else |
|
| 2744 | + } else |
|
| 2801 | 2745 | { |
| 2802 | 2746 | $lsize = count(self::$collect_queue); |
| 2803 | 2747 | } |
@@ -2826,8 +2770,7 @@ discard block |
||
| 2826 | 2770 | |
| 2827 | 2771 | queue::unlock($lock); |
| 2828 | 2772 | } |
| 2829 | - } |
|
| 2830 | - else |
|
| 2773 | + } else |
|
| 2831 | 2774 | { |
| 2832 | 2775 | if (self::$depth_num < $depth) |
| 2833 | 2776 | { |
@@ -2849,8 +2792,7 @@ discard block |
||
| 2849 | 2792 | { |
| 2850 | 2793 | $depth_num = queue::get('depth_num'); |
| 2851 | 2794 | return $depth_num ? $depth_num : 0; |
| 2852 | - } |
|
| 2853 | - else |
|
| 2795 | + } else |
|
| 2854 | 2796 | { |
| 2855 | 2797 | return self::$depth_num; |
| 2856 | 2798 | } |
@@ -2868,8 +2810,7 @@ discard block |
||
| 2868 | 2810 | if (self::$use_redis) |
| 2869 | 2811 | { |
| 2870 | 2812 | $fields_num = queue::incr('fields_num'); |
| 2871 | - } |
|
| 2872 | - else |
|
| 2813 | + } else |
|
| 2873 | 2814 | { |
| 2874 | 2815 | self::$fields_num++; |
| 2875 | 2816 | $fields_num = self::$fields_num; |
@@ -2889,8 +2830,7 @@ discard block |
||
| 2889 | 2830 | if (self::$use_redis) |
| 2890 | 2831 | { |
| 2891 | 2832 | $fields_num = queue::get('fields_num'); |
| 2892 | - } |
|
| 2893 | - else |
|
| 2833 | + } else |
|
| 2894 | 2834 | { |
| 2895 | 2835 | $fields_num = self::$fields_num; |
| 2896 | 2836 | } |
@@ -2917,14 +2857,12 @@ discard block |
||
| 2917 | 2857 | if (self::$use_redis) |
| 2918 | 2858 | { |
| 2919 | 2859 | $pages_num[$domain] = queue::incr('pages_num:'.$domain); |
| 2920 | - } |
|
| 2921 | - else |
|
| 2860 | + } else |
|
| 2922 | 2861 | { |
| 2923 | 2862 | if (empty(self::$pages_num[$domain])) |
| 2924 | 2863 | { |
| 2925 | 2864 | self::$pages_num[$domain] = 1; |
| 2926 | - } |
|
| 2927 | - else |
|
| 2865 | + } else |
|
| 2928 | 2866 | { |
| 2929 | 2867 | self::$pages_num[$domain]++; |
| 2930 | 2868 | } |
@@ -2953,14 +2891,12 @@ discard block |
||
| 2953 | 2891 | if (self::$use_redis) |
| 2954 | 2892 | { |
| 2955 | 2893 | $duration[$domain] = queue::incr('duration:'.$domain, $time_run); |
| 2956 | - } |
|
| 2957 | - else |
|
| 2894 | + } else |
|
| 2958 | 2895 | { |
| 2959 | 2896 | if (empty(self::$duration[$domain])) |
| 2960 | 2897 | { |
| 2961 | 2898 | self::$duration[$domain] = $time_run; |
| 2962 | - } |
|
| 2963 | - else |
|
| 2899 | + } else |
|
| 2964 | 2900 | { |
| 2965 | 2901 | self::$duration[$domain] += $time_run; |
| 2966 | 2902 | } |
@@ -2989,8 +2925,7 @@ discard block |
||
| 2989 | 2925 | if (self::$use_redis) |
| 2990 | 2926 | { |
| 2991 | 2927 | $duration[$domain] = queue::get('duration:'.$domain); |
| 2992 | - } |
|
| 2993 | - else |
|
| 2928 | + } else |
|
| 2994 | 2929 | { |
| 2995 | 2930 | $duration[$domain] = ! empty(self::$duration[$domain]) ? self::$duration[$domain] : 0; |
| 2996 | 2931 | } |
@@ -3019,26 +2954,22 @@ discard block |
||
| 3019 | 2954 | if ($type == 'decr') |
| 3020 | 2955 | { |
| 3021 | 2956 | $task_per_host_counter[$domain] = queue::decr('task_per_host:'.$domain); |
| 3022 | - } |
|
| 3023 | - else |
|
| 2957 | + } else |
|
| 3024 | 2958 | { |
| 3025 | 2959 | $task_per_host_counter[$domain] = queue::incr('task_per_host:'.$domain); |
| 3026 | 2960 | } |
| 3027 | - } |
|
| 3028 | - else |
|
| 2961 | + } else |
|
| 3029 | 2962 | { |
| 3030 | 2963 | |
| 3031 | 2964 | if (empty(self::$task_per_host_counter[$domain])) |
| 3032 | 2965 | { |
| 3033 | 2966 | self::$task_per_host_counter[$domain] = 1; |
| 3034 | - } |
|
| 3035 | - else |
|
| 2967 | + } else |
|
| 3036 | 2968 | { |
| 3037 | 2969 | if ($type == 'decr') |
| 3038 | 2970 | { |
| 3039 | 2971 | self::$task_per_host_counter[$domain]--; |
| 3040 | - } |
|
| 3041 | - else |
|
| 2972 | + } else |
|
| 3042 | 2973 | { |
| 3043 | 2974 | self::$task_per_host_counter[$domain]++; |
| 3044 | 2975 | } |
@@ -3063,8 +2994,7 @@ discard block |
||
| 3063 | 2994 | if (self::$use_redis) |
| 3064 | 2995 | { |
| 3065 | 2996 | $count = queue::get('task_per_host:'.$domain); |
| 3066 | - } |
|
| 3067 | - else |
|
| 2997 | + } else |
|
| 3068 | 2998 | { |
| 3069 | 2999 | $count = self::$task_per_host_counter[$domain]; |
| 3070 | 3000 | } |
@@ -3528,8 +3458,7 @@ discard block |
||
| 3528 | 3458 | $res['name'] = $last_1; |
| 3529 | 3459 | $res['tld'] = $last; |
| 3530 | 3460 | } |
| 3531 | - } |
|
| 3532 | - elseif ($count > 2) |
|
| 3461 | + } elseif ($count > 2) |
|
| 3533 | 3462 | { |
| 3534 | 3463 | $last = array_pop($urlarr); |
| 3535 | 3464 | $last_1 = array_pop($urlarr); |
@@ -3571,25 +3500,20 @@ discard block |
||
| 3571 | 3500 | if ($type == 'arr') |
| 3572 | 3501 | { |
| 3573 | 3502 | return $res; |
| 3574 | - } |
|
| 3575 | - elseif ($type == 'host') |
|
| 3503 | + } elseif ($type == 'host') |
|
| 3576 | 3504 | { |
| 3577 | 3505 | return $res['host']; |
| 3578 | - } |
|
| 3579 | - elseif ($type == 'tld') |
|
| 3506 | + } elseif ($type == 'tld') |
|
| 3580 | 3507 | { |
| 3581 | 3508 | return $res['tld']; |
| 3582 | - } |
|
| 3583 | - elseif ($type == 'subdomain') |
|
| 3509 | + } elseif ($type == 'subdomain') |
|
| 3584 | 3510 | { |
| 3585 | 3511 | return $res['name']; |
| 3586 | - } |
|
| 3587 | - else |
|
| 3512 | + } else |
|
| 3588 | 3513 | { |
| 3589 | 3514 | return $res['domain']; |
| 3590 | 3515 | } |
| 3591 | - } |
|
| 3592 | - else |
|
| 3516 | + } else |
|
| 3593 | 3517 | { |
| 3594 | 3518 | return ''; |
| 3595 | 3519 | } |
@@ -385,36 +385,36 @@ |
||
| 385 | 385 | { |
| 386 | 386 | switch($type) |
| 387 | 387 | { |
| 388 | - case E_ERROR: // 1 // |
|
| 389 | - return 'E_ERROR'; |
|
| 390 | - case E_WARNING: // 2 // |
|
| 391 | - return 'E_WARNING'; |
|
| 392 | - case E_PARSE: // 4 // |
|
| 393 | - return 'E_PARSE'; |
|
| 394 | - case E_NOTICE: // 8 // |
|
| 395 | - return 'E_NOTICE'; |
|
| 396 | - case E_CORE_ERROR: // 16 // |
|
| 397 | - return 'E_CORE_ERROR'; |
|
| 398 | - case E_CORE_WARNING: // 32 // |
|
| 399 | - return 'E_CORE_WARNING'; |
|
| 400 | - case E_COMPILE_ERROR: // 64 // |
|
| 401 | - return 'E_COMPILE_ERROR'; |
|
| 402 | - case E_COMPILE_WARNING: // 128 // |
|
| 403 | - return 'E_COMPILE_WARNING'; |
|
| 404 | - case E_USER_ERROR: // 256 // |
|
| 405 | - return 'E_USER_ERROR'; |
|
| 406 | - case E_USER_WARNING: // 512 // |
|
| 407 | - return 'E_USER_WARNING'; |
|
| 408 | - case E_USER_NOTICE: // 1024 // |
|
| 409 | - return 'E_USER_NOTICE'; |
|
| 410 | - case E_STRICT: // 2048 // |
|
| 411 | - return 'E_STRICT'; |
|
| 412 | - case E_RECOVERABLE_ERROR: // 4096 // |
|
| 413 | - return 'E_RECOVERABLE_ERROR'; |
|
| 414 | - case E_DEPRECATED: // 8192 // |
|
| 415 | - return 'E_DEPRECATED'; |
|
| 416 | - case E_USER_DEPRECATED: // 16384 // |
|
| 417 | - return 'E_USER_DEPRECATED'; |
|
| 388 | + case E_ERROR: // 1 // |
|
| 389 | + return 'E_ERROR'; |
|
| 390 | + case E_WARNING: // 2 // |
|
| 391 | + return 'E_WARNING'; |
|
| 392 | + case E_PARSE: // 4 // |
|
| 393 | + return 'E_PARSE'; |
|
| 394 | + case E_NOTICE: // 8 // |
|
| 395 | + return 'E_NOTICE'; |
|
| 396 | + case E_CORE_ERROR: // 16 // |
|
| 397 | + return 'E_CORE_ERROR'; |
|
| 398 | + case E_CORE_WARNING: // 32 // |
|
| 399 | + return 'E_CORE_WARNING'; |
|
| 400 | + case E_COMPILE_ERROR: // 64 // |
|
| 401 | + return 'E_COMPILE_ERROR'; |
|
| 402 | + case E_COMPILE_WARNING: // 128 // |
|
| 403 | + return 'E_COMPILE_WARNING'; |
|
| 404 | + case E_USER_ERROR: // 256 // |
|
| 405 | + return 'E_USER_ERROR'; |
|
| 406 | + case E_USER_WARNING: // 512 // |
|
| 407 | + return 'E_USER_WARNING'; |
|
| 408 | + case E_USER_NOTICE: // 1024 // |
|
| 409 | + return 'E_USER_NOTICE'; |
|
| 410 | + case E_STRICT: // 2048 // |
|
| 411 | + return 'E_STRICT'; |
|
| 412 | + case E_RECOVERABLE_ERROR: // 4096 // |
|
| 413 | + return 'E_RECOVERABLE_ERROR'; |
|
| 414 | + case E_DEPRECATED: // 8192 // |
|
| 415 | + return 'E_DEPRECATED'; |
|
| 416 | + case E_USER_DEPRECATED: // 16384 // |
|
| 417 | + return 'E_USER_DEPRECATED'; |
|
| 418 | 418 | } |
| 419 | 419 | return ""; |
| 420 | 420 | } |
@@ -51,7 +51,7 @@ discard block |
||
| 51 | 51 | { |
| 52 | 52 | self::$_master_pid = posix_getpid(); |
| 53 | 53 | // 产生时钟云,添加后父进程才可以收到信号 |
| 54 | - declare(ticks = 1); |
|
| 54 | + declare(ticks=1); |
|
| 55 | 55 | $this->install_signal(); |
| 56 | 56 | } |
| 57 | 57 | |
@@ -62,7 +62,7 @@ discard block |
||
| 62 | 62 | protected function install_signal() |
| 63 | 63 | { |
| 64 | 64 | // stop |
| 65 | - pcntl_signal(SIGINT, array($this, 'signal_handler'), false); |
|
| 65 | + pcntl_signal(SIGINT, array($this, 'signal_handler'), false); |
|
| 66 | 66 | // reload |
| 67 | 67 | pcntl_signal(SIGUSR1, array($this, 'signal_handler'), false); |
| 68 | 68 | // status |
@@ -80,7 +80,7 @@ discard block |
||
| 80 | 80 | protected function uninstall_signal() |
| 81 | 81 | { |
| 82 | 82 | // uninstall stop signal handler |
| 83 | - pcntl_signal(SIGINT, SIG_IGN, false); |
|
| 83 | + pcntl_signal(SIGINT, SIG_IGN, false); |
|
| 84 | 84 | // uninstall reload signal handler |
| 85 | 85 | pcntl_signal(SIGUSR1, SIG_IGN, false); |
| 86 | 86 | // uninstall status signal handler |
@@ -149,12 +149,12 @@ discard block |
||
| 149 | 149 | $pid = pcntl_fork(); |
| 150 | 150 | |
| 151 | 151 | // 主进程记录子进程pid |
| 152 | - if($pid > 0) |
|
| 152 | + if ($pid > 0) |
|
| 153 | 153 | { |
| 154 | 154 | self::$_worker_pids[$worker_id] = $pid; |
| 155 | 155 | } |
| 156 | 156 | // 子进程运行 |
| 157 | - elseif(0 === $pid) |
|
| 157 | + elseif (0 === $pid) |
|
| 158 | 158 | { |
| 159 | 159 | $this->time_start = microtime(true); |
| 160 | 160 | $this->worker_id = $worker_id; |
@@ -197,14 +197,14 @@ discard block |
||
| 197 | 197 | protected static function set_process_user($user_name) |
| 198 | 198 | { |
| 199 | 199 | // 用户名为空 或者 当前用户不是root用户 |
| 200 | - if(empty($user_name) || posix_getuid() !== 0) |
|
| 200 | + if (empty($user_name) || posix_getuid() !== 0) |
|
| 201 | 201 | { |
| 202 | 202 | return; |
| 203 | 203 | } |
| 204 | 204 | $user_info = posix_getpwnam($user_name); |
| 205 | - if($user_info['uid'] != posix_getuid() || $user_info['gid'] != posix_getgid()) |
|
| 205 | + if ($user_info['uid'] != posix_getuid() || $user_info['gid'] != posix_getgid()) |
|
| 206 | 206 | { |
| 207 | - if(!posix_setgid($user_info['gid']) || !posix_setuid($user_info['uid'])) |
|
| 207 | + if (!posix_setgid($user_info['gid']) || !posix_setuid($user_info['uid'])) |
|
| 208 | 208 | { |
| 209 | 209 | log::add('Can not run woker as '.$user_name." , You shuld be root", "Error"); |
| 210 | 210 | } |
@@ -222,7 +222,7 @@ discard block |
||
| 222 | 222 | if (!empty($title)) |
| 223 | 223 | { |
| 224 | 224 | // 需要扩展 |
| 225 | - if(extension_loaded('proctitle') && function_exists('setproctitle')) |
|
| 225 | + if (extension_loaded('proctitle') && function_exists('setproctitle')) |
|
| 226 | 226 | { |
| 227 | 227 | @setproctitle($title); |
| 228 | 228 | } |
@@ -242,7 +242,7 @@ discard block |
||
| 242 | 242 | { |
| 243 | 243 | // 设置master进程的运行状态为运行中 |
| 244 | 244 | self::$_status = "running"; |
| 245 | - while(1) |
|
| 245 | + while (1) |
|
| 246 | 246 | { |
| 247 | 247 | // pcntl_signal_dispatch 子进程无法接受到信号 |
| 248 | 248 | // 如果有信号到来,尝试触发信号处理函数 |
@@ -254,13 +254,13 @@ discard block |
||
| 254 | 254 | //pcntl_signal_dispatch(); |
| 255 | 255 | |
| 256 | 256 | // 子进程退出信号 |
| 257 | - if($pid > 0) |
|
| 257 | + if ($pid > 0) |
|
| 258 | 258 | { |
| 259 | 259 | //echo "worker[".$pid."] stop\n"; |
| 260 | 260 | //$this->stop(); |
| 261 | 261 | |
| 262 | 262 | // 如果不是正常退出,是被kill等杀掉的 |
| 263 | - if($status !== 0) |
|
| 263 | + if ($status !== 0) |
|
| 264 | 264 | { |
| 265 | 265 | log::add("worker {$pid} exit with status $status", "Warning"); |
| 266 | 266 | } |
@@ -321,7 +321,7 @@ discard block |
||
| 321 | 321 | // 设置master、worker进程的运行状态为关闭状态 |
| 322 | 322 | self::$_status = "shutdown"; |
| 323 | 323 | // master进程 |
| 324 | - if(self::$_master_pid === posix_getpid()) |
|
| 324 | + if (self::$_master_pid === posix_getpid()) |
|
| 325 | 325 | { |
| 326 | 326 | // 循环给worker进程发送关闭信号 |
| 327 | 327 | foreach (self::$_worker_pids as $worker_pid) |
@@ -360,17 +360,17 @@ discard block |
||
| 360 | 360 | public function check_errors() |
| 361 | 361 | { |
| 362 | 362 | // 如果当前worker进程不是正常退出 |
| 363 | - if(self::$_status != "shutdown") |
|
| 363 | + if (self::$_status != "shutdown") |
|
| 364 | 364 | { |
| 365 | 365 | $error_msg = "WORKER EXIT UNEXPECTED "; |
| 366 | 366 | $errors = error_get_last(); |
| 367 | - if($errors && ($errors['type'] === E_ERROR || |
|
| 367 | + if ($errors && ($errors['type'] === E_ERROR || |
|
| 368 | 368 | $errors['type'] === E_PARSE || |
| 369 | 369 | $errors['type'] === E_CORE_ERROR || |
| 370 | 370 | $errors['type'] === E_COMPILE_ERROR || |
| 371 | - $errors['type'] === E_RECOVERABLE_ERROR )) |
|
| 371 | + $errors['type'] === E_RECOVERABLE_ERROR)) |
|
| 372 | 372 | { |
| 373 | - $error_msg .= $this->get_error_type($errors['type']) . " {$errors['message']} in {$errors['file']} on line {$errors['line']}"; |
|
| 373 | + $error_msg .= $this->get_error_type($errors['type'])." {$errors['message']} in {$errors['file']} on line {$errors['line']}"; |
|
| 374 | 374 | } |
| 375 | 375 | log::add($error_msg, 'Error'); |
| 376 | 376 | } |
@@ -383,7 +383,7 @@ discard block |
||
| 383 | 383 | */ |
| 384 | 384 | protected function get_error_type($type) |
| 385 | 385 | { |
| 386 | - switch($type) |
|
| 386 | + switch ($type) |
|
| 387 | 387 | { |
| 388 | 388 | case E_ERROR: // 1 // |
| 389 | 389 | return 'E_ERROR'; |
@@ -181,8 +181,7 @@ |
||
| 181 | 181 | $this->stop(); |
| 182 | 182 | // 这里用0表示正常退出 |
| 183 | 183 | exit(0); |
| 184 | - } |
|
| 185 | - else |
|
| 184 | + } else |
|
| 186 | 185 | { |
| 187 | 186 | log::add("fork one worker fail", "Error"); |
| 188 | 187 | exit; |
@@ -26,7 +26,7 @@ discard block |
||
| 26 | 26 | */ |
| 27 | 27 | public static function init() |
| 28 | 28 | { |
| 29 | - if(extension_loaded('Redis')) |
|
| 29 | + if (extension_loaded('Redis')) |
|
| 30 | 30 | { |
| 31 | 31 | $_instance = new Redis(); |
| 32 | 32 | } |
@@ -42,7 +42,7 @@ discard block |
||
| 42 | 42 | // 验证 |
| 43 | 43 | if ($GLOBALS['config']['redis']['pass']) |
| 44 | 44 | { |
| 45 | - if ( !$_instance->auth($GLOBALS['config']['redis']['pass']) ) |
|
| 45 | + if (!$_instance->auth($GLOBALS['config']['redis']['pass'])) |
|
| 46 | 46 | { |
| 47 | 47 | $errmsg = "Redis Server authentication failed!!"; |
| 48 | 48 | log::add($errmsg, "Error"); |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | //$_instance->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP); // use built-in serialize/unserialize |
| 56 | 56 | //$_instance->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY); // use igBinary serialize/unserialize |
| 57 | 57 | |
| 58 | - $_instance->setOption(Redis::OPT_PREFIX, $GLOBALS['config']['redis']['prefix'] . ":"); |
|
| 58 | + $_instance->setOption(Redis::OPT_PREFIX, $GLOBALS['config']['redis']['prefix'].":"); |
|
| 59 | 59 | |
| 60 | 60 | return $_instance; |
| 61 | 61 | } |
@@ -29,8 +29,7 @@ |
||
| 29 | 29 | if(extension_loaded('Redis')) |
| 30 | 30 | { |
| 31 | 31 | $_instance = new Redis(); |
| 32 | - } |
|
| 33 | - else |
|
| 32 | + } else |
|
| 34 | 33 | { |
| 35 | 34 | $errmsg = "extension redis is not installed"; |
| 36 | 35 | log::add($errmsg, "Error"); |