| @@ -1,19 +1,18 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if (!defined('ABSPATH')) exit; |
| 4 | -if (!class_exists('WPRDBCallback')) : | |
| 4 | +if (!class_exists('BVDBCallback')) : | |
| 5 | 5 | require_once dirname( __FILE__ ) . '/../streams.php'; |
| 6 | 6 | |
| 7 | -class WPRDBCallback extends WPRCallbackBase { | |
| 7 | +class BVDBCallback extends BVCallbackBase { | |
| 8 | 8 | public $db; |
| 9 | 9 | public $stream; |
| 10 | 10 | public $account; |
| 11 | - public $siteinfo; | |
| 12 | 11 | |
| 13 | 12 | public static $bvTables = array("fw_requests", "lp_requests", "ip_store"); |
| 14 | 13 | |
| 15 | - const DB_WING_VERSION = 1.5; | |
| 14 | + const DB_WING_VERSION = 1.2; | |
| 16 | 15 | |
| 17 | 16 | public function __construct($callback_handler) { |
| 18 | 17 | $this->db = $callback_handler->db; |
| 19 | 18 | $this->account = $callback_handler->account; |
| @@ -29,9 +28,9 @@ | ||
| 29 | 28 | } |
| 30 | 29 | |
| 31 | 30 | public function getTableData($table, $tname, $offset, $limit, $bsize, $filter, $pkeys, $include_rows = false) { |
| 32 | 31 | $tinfo = array(); |
| 33 | - | |
| 32 | + | |
| 34 | 33 | $rows_count = $this->db->rowsCount($table); |
| 35 | 34 | $result = array('count' => $rows_count); |
| 36 | 35 | if ($limit == 0) { |
| 37 | 36 | $limit = $rows_count; |
| @@ -68,88 +67,8 @@ | ||
| 68 | 67 | $result['tinfo'] = $tinfo; |
| 69 | 68 | return $result; |
| 70 | 69 | } |
| 71 | 70 | |
| 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 | - | |
| 152 | 71 | public function streamQueryResult($identifier, $query, $pkeys) { |
| 153 | 72 | $data = array(); |
| 154 | 73 | $data["identifier"] = $identifier; |
| 155 | 74 | $data["query"] = $query; |
| @@ -186,9 +105,9 @@ | ||
| 186 | 105 | } |
| 187 | 106 | |
| 188 | 107 | $randomString = ''; |
| 189 | 108 | for ($i = 0; $i < $bsize; $i++) { |
| 190 | - $randomString .= $characters[rand(0, $charactersLength - 1)]; // phpcs:ignore WordPress.WP.AlternativeFunctions.rand_rand | |
| 109 | + $randomString .= $characters[rand(0, $charactersLength - 1)]; | |
| 191 | 110 | } |
| 192 | 111 | |
| 193 | 112 | $this->stream->writeStream($randomString); |
| 194 | 113 | $totalSize -= $bsize; |
| @@ -218,9 +137,8 @@ | ||
| 218 | 137 | $resp = array(); |
| 219 | 138 | foreach($tables as $table) { |
| 220 | 139 | $tname = $table; |
| 221 | 140 | $resp[$tname] = array("description" => $this->db->describeTable($table)); |
| 222 | - $resp[$tname]["primary_keys_index"] = $this->db->showTableIndex($table); | |
| 223 | 141 | } |
| 224 | 142 | return $resp; |
| 225 | 143 | } |
| 226 | 144 | |
| @@ -261,9 +179,9 @@ | ||
| 261 | 179 | |
| 262 | 180 | public function process($request) { |
| 263 | 181 | $db = $this->db; |
| 264 | 182 | $params = $request->params; |
| 265 | - $stream_init_info = WPRStream::startStream($this->account, $request); | |
| 183 | + $stream_init_info = BVStream::startStream($this->account, $request); | |
| 266 | 184 | |
| 267 | 185 | if (array_key_exists('stream', $stream_init_info)) { |
| 268 | 186 | $this->stream = $stream_init_info['stream']; |
| 269 | 187 | switch ($request->method) { |
| @@ -279,9 +197,8 @@ | ||
| 279 | 197 | break; |
| 280 | 198 | case "describetable": |
| 281 | 199 | $table = $params['table']; |
| 282 | 200 | $resp = array("table_description" => $db->describeTable($table)); |
| 283 | - $resp["primary_keys_index"] = $db->showTableIndex($table); | |
| 284 | 201 | break; |
| 285 | 202 | case "checktable": |
| 286 | 203 | $table = $params['table']; |
| 287 | 204 | $type = $params['type']; |
| @@ -376,14 +293,9 @@ | ||
| 376 | 293 | $bsize = intval($params['bsize']); |
| 377 | 294 | $filter = (array_key_exists('filter', $params)) ? $params['filter'] : ""; |
| 378 | 295 | $tname = $params['tname']; |
| 379 | 296 | $pkeys = (array_key_exists('pkeys', $params)) ? $params['pkeys'] : array(); |
| 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 | - } | |
| 297 | + $resp = $this->getTableData($table, $tname, $offset, $limit, $bsize, $filter, $pkeys, false); | |
| 386 | 298 | break; |
| 387 | 299 | case "getmulttables": |
| 388 | 300 | $result = array(); |
| 389 | 301 | $tableParams = $params['table_params']; |
| @@ -406,14 +318,9 @@ | ||
| 406 | 318 | $bsize = intval($params['bsize']); |
| 407 | 319 | $filter = (array_key_exists('filter', $params)) ? $params['filter'] : ""; |
| 408 | 320 | $tname = $params['tname']; |
| 409 | 321 | $pkeys = (array_key_exists('pkeys', $params)) ? $params['pkeys'] : array(); |
| 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 | - } | |
| 322 | + $resp = $this->getTableData($table, $tname, $offset, $limit, $bsize, $filter, $pkeys, true); | |
| 416 | 323 | break; |
| 417 | 324 | case "tblexists": |
| 418 | 325 | $resp = array("tblexists" => $db->isTablePresent($params['table'])); |
| 419 | 326 | break; |
| @@ -475,5 +382,5 @@ | ||
| 475 | 382 | } |
| 476 | 383 | return $resp; |
| 477 | 384 | } |
| 478 | 385 | } |
| 479 | -endif; | |
| 386 | +endif; | |