| @@ -1,11 +1,11 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if (!defined('ABSPATH')) exit; |
| 4 | -if (!class_exists('BVDBCallback')) : | |
| 4 | +if (!class_exists('WPRDBCallback')) : | |
| 5 | 5 | require_once dirname( __FILE__ ) . '/../streams.php'; |
| 6 | 6 | |
| 7 | -class BVDBCallback extends BVCallbackBase { | |
| 7 | +class WPRDBCallback extends WPRCallbackBase { | |
| 8 | 8 | public $db; |
| 9 | 9 | public $stream; |
| 10 | 10 | public $account; |
| 11 | 11 | public $siteinfo; |
| @@ -11,9 +11,9 @@ | ||
| 11 | 11 | public $siteinfo; |
| 12 | 12 | |
| 13 | 13 | public static $bvTables = array("fw_requests", "lp_requests", "ip_store"); |
| 14 | 14 | |
| 15 | - const DB_WING_VERSION = 1.3; | |
| 15 | + const DB_WING_VERSION = 1.5; | |
| 16 | 16 | |
| 17 | 17 | public function __construct($callback_handler) { |
| 18 | 18 | $this->db = $callback_handler->db; |
| 19 | 19 | $this->account = $callback_handler->account; |
| @@ -29,9 +29,9 @@ | ||
| 29 | 29 | } |
| 30 | 30 | |
| 31 | 31 | public function getTableData($table, $tname, $offset, $limit, $bsize, $filter, $pkeys, $include_rows = false) { |
| 32 | 32 | $tinfo = array(); |
| 33 | - | |
| 33 | + | |
| 34 | 34 | $rows_count = $this->db->rowsCount($table); |
| 35 | 35 | $result = array('count' => $rows_count); |
| 36 | 36 | if ($limit == 0) { |
| 37 | 37 | $limit = $rows_count; |
| @@ -68,8 +68,88 @@ | ||
| 68 | 68 | $result['tinfo'] = $tinfo; |
| 69 | 69 | return $result; |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | + public function buildPrimaryKeyCursorFilter($pkeys, $last_ids) { | |
| 73 | + if (empty($pkeys)) return null; | |
| 74 | + foreach($pkeys as $pk) { | |
| 75 | + if (!isset($last_ids[$pk]) || $last_ids[$pk] === '') return null; | |
| 76 | + } | |
| 77 | + | |
| 78 | + $conditions = array(); | |
| 79 | + $order_by = array(); | |
| 80 | + foreach($pkeys as $index => $pk) { | |
| 81 | + $key = "`".$pk."`"; | |
| 82 | + $order_by[] = $key; | |
| 83 | + $parts = array(); | |
| 84 | + for ($eq_index = 0; $eq_index < $index; $eq_index++) { | |
| 85 | + $eq_pk = $pkeys[$eq_index]; | |
| 86 | + $eq_key = "`".$eq_pk."`"; | |
| 87 | + $eq_last_id = esc_sql((string) $last_ids[$eq_pk]); | |
| 88 | + $parts[] = $eq_key." = '".$eq_last_id."'"; | |
| 89 | + } | |
| 90 | + $last_id = esc_sql((string) $last_ids[$pkeys[$index]]); | |
| 91 | + $parts[] = $key." > '".$last_id."'"; | |
| 92 | + $condition = implode(" AND ", $parts); | |
| 93 | + $conditions[] = ($index == 0) ? $condition : "(".$condition.")"; | |
| 94 | + } | |
| 95 | + return " WHERE ".implode(" OR ", $conditions)." ORDER BY ".implode(",", $order_by); | |
| 96 | + } | |
| 97 | + | |
| 98 | + public function getTableDataByCursor($table, $tname, $offset, $limit, $bsize, $filter, $pkeys, $include_rows = false, $cursor_mode = false) { | |
| 99 | + $tinfo = array(); | |
| 100 | + | |
| 101 | + $rows_count = $this->db->rowsCount($table); | |
| 102 | + $result = array('count' => $rows_count); | |
| 103 | + if ($limit == 0) { | |
| 104 | + $limit = $rows_count; | |
| 105 | + } | |
| 106 | + $srows = 1; | |
| 107 | + $current_filter = $filter; | |
| 108 | + $cursor_active = ($cursor_mode === true && !empty($pkeys)); | |
| 109 | + $query_offset = $offset; | |
| 110 | + while (($limit > 0) && ($srows > 0)) { | |
| 111 | + if ($bsize > $limit) | |
| 112 | + $bsize = $limit; | |
| 113 | + $rows = $this->db->getTableContent($table, '*', $current_filter, $bsize, $query_offset); | |
| 114 | + $srows = sizeof($rows); | |
| 115 | + $data = array(); | |
| 116 | + $data["table_name"] = $tname; | |
| 117 | + $data["offset"] = $offset; | |
| 118 | + $data["size"] = $srows; | |
| 119 | + $serialized_rows = serialize($rows); | |
| 120 | + $data['md5'] = md5($serialized_rows); | |
| 121 | + $data['length'] = strlen($serialized_rows); | |
| 122 | + array_push($tinfo, $data); | |
| 123 | + if (!empty($pkeys) && $srows > 0) { | |
| 124 | + $end_row = end($rows); | |
| 125 | + $last_ids = $this->getLastID($pkeys, $end_row); | |
| 126 | + $data['last_ids'] = $last_ids; | |
| 127 | + $result['last_ids'] = $last_ids; | |
| 128 | + if ($cursor_active) { | |
| 129 | + $cursor_filter = $this->buildPrimaryKeyCursorFilter($pkeys, $last_ids); | |
| 130 | + if ($cursor_filter !== null) { | |
| 131 | + $current_filter = $cursor_filter; | |
| 132 | + } else { | |
| 133 | + $current_filter = $filter; | |
| 134 | + $cursor_active = false; | |
| 135 | + } | |
| 136 | + } | |
| 137 | + } | |
| 138 | + if ($include_rows) { | |
| 139 | + $data["rows"] = $rows; | |
| 140 | + $str = serialize($data); | |
| 141 | + $this->stream->writeStream($str); | |
| 142 | + } | |
| 143 | + $offset += $srows; | |
| 144 | + $limit -= $srows; | |
| 145 | + $query_offset = $cursor_active ? 0 : $offset; | |
| 146 | + } | |
| 147 | + $result['size'] = $offset; | |
| 148 | + $result['tinfo'] = $tinfo; | |
| 149 | + return $result; | |
| 150 | + } | |
| 151 | + | |
| 72 | 152 | public function streamQueryResult($identifier, $query, $pkeys) { |
| 73 | 153 | $data = array(); |
| 74 | 154 | $data["identifier"] = $identifier; |
| 75 | 155 | $data["query"] = $query; |
| @@ -106,9 +186,9 @@ | ||
| 106 | 186 | } |
| 107 | 187 | |
| 108 | 188 | $randomString = ''; |
| 109 | 189 | for ($i = 0; $i < $bsize; $i++) { |
| 110 | - $randomString .= $characters[rand(0, $charactersLength - 1)]; | |
| 190 | + $randomString .= $characters[rand(0, $charactersLength - 1)]; // phpcs:ignore WordPress.WP.AlternativeFunctions.rand_rand | |
| 111 | 191 | } |
| 112 | 192 | |
| 113 | 193 | $this->stream->writeStream($randomString); |
| 114 | 194 | $totalSize -= $bsize; |
| @@ -181,9 +261,9 @@ | ||
| 181 | 261 | |
| 182 | 262 | public function process($request) { |
| 183 | 263 | $db = $this->db; |
| 184 | 264 | $params = $request->params; |
| 185 | - $stream_init_info = BVStream::startStream($this->account, $request); | |
| 265 | + $stream_init_info = WPRStream::startStream($this->account, $request); | |
| 186 | 266 | |
| 187 | 267 | if (array_key_exists('stream', $stream_init_info)) { |
| 188 | 268 | $this->stream = $stream_init_info['stream']; |
| 189 | 269 | switch ($request->method) { |
| @@ -296,9 +376,14 @@ | ||
| 296 | 376 | $bsize = intval($params['bsize']); |
| 297 | 377 | $filter = (array_key_exists('filter', $params)) ? $params['filter'] : ""; |
| 298 | 378 | $tname = $params['tname']; |
| 299 | 379 | $pkeys = (array_key_exists('pkeys', $params)) ? $params['pkeys'] : array(); |
| 300 | - $resp = $this->getTableData($table, $tname, $offset, $limit, $bsize, $filter, $pkeys, false); | |
| 380 | + $cursor_mode = (array_key_exists('cursor_mode', $params)) ? $params['cursor_mode'] : false; | |
| 381 | + if ($cursor_mode === true) { | |
| 382 | + $resp = $this->getTableDataByCursor($table, $tname, $offset, $limit, $bsize, $filter, $pkeys, false, $cursor_mode); | |
| 383 | + } else { | |
| 384 | + $resp = $this->getTableData($table, $tname, $offset, $limit, $bsize, $filter, $pkeys, false); | |
| 385 | + } | |
| 301 | 386 | break; |
| 302 | 387 | case "getmulttables": |
| 303 | 388 | $result = array(); |
| 304 | 389 | $tableParams = $params['table_params']; |
| @@ -321,9 +406,14 @@ | ||
| 321 | 406 | $bsize = intval($params['bsize']); |
| 322 | 407 | $filter = (array_key_exists('filter', $params)) ? $params['filter'] : ""; |
| 323 | 408 | $tname = $params['tname']; |
| 324 | 409 | $pkeys = (array_key_exists('pkeys', $params)) ? $params['pkeys'] : array(); |
| 325 | - $resp = $this->getTableData($table, $tname, $offset, $limit, $bsize, $filter, $pkeys, true); | |
| 410 | + $cursor_mode = (array_key_exists('cursor_mode', $params)) ? $params['cursor_mode'] : false; | |
| 411 | + if ($cursor_mode === true) { | |
| 412 | + $resp = $this->getTableDataByCursor($table, $tname, $offset, $limit, $bsize, $filter, $pkeys, true, $cursor_mode); | |
| 413 | + } else { | |
| 414 | + $resp = $this->getTableData($table, $tname, $offset, $limit, $bsize, $filter, $pkeys, true); | |
| 415 | + } | |
| 326 | 416 | break; |
| 327 | 417 | case "tblexists": |
| 328 | 418 | $resp = array("tblexists" => $db->isTablePresent($params['table'])); |
| 329 | 419 | break; |
| @@ -385,5 +475,5 @@ | ||
| 385 | 475 | } |
| 386 | 476 | return $resp; |
| 387 | 477 | } |
| 388 | 478 | } |
| 389 | -endif; | |
| 479 | +endif; | |