| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDataAccess\API { |
| 4 |
|
| 5 |
use WPDataAccess\Connection\WPDADB; |
| 6 |
|
| 7 |
class WPDA_Tree extends WPDA_API_Core { |
| 8 |
|
| 9 |
public function register_rest_routes() { |
| 10 |
|
| 11 |
register_rest_route( |
| 12 |
WPDA_API::WPDA_NAMESPACE, |
| 13 |
'tree/dbs', |
| 14 |
array( |
| 15 |
'methods' => array( 'POST' ), |
| 16 |
'callback' => array( $this, 'tree_dbs' ), |
| 17 |
'permission_callback' => '__return_true', |
| 18 |
) |
| 19 |
); |
| 20 |
|
| 21 |
register_rest_route( |
| 22 |
WPDA_API::WPDA_NAMESPACE, |
| 23 |
'tree/tbl', |
| 24 |
array( |
| 25 |
'methods' => array( 'POST' ), |
| 26 |
'callback' => array( $this, 'tree_tbl' ), |
| 27 |
'permission_callback' => '__return_true', |
| 28 |
'args' => array( |
| 29 |
'dbs' => $this->get_param( 'dbs' ), |
| 30 |
) |
| 31 |
) |
| 32 |
); |
| 33 |
|
| 34 |
register_rest_route( |
| 35 |
WPDA_API::WPDA_NAMESPACE, |
| 36 |
'tree/vws', |
| 37 |
array( |
| 38 |
'methods' => array( 'POST' ), |
| 39 |
'callback' => array( $this, 'tree_vws' ), |
| 40 |
'permission_callback' => '__return_true', |
| 41 |
'args' => array( |
| 42 |
'dbs' => $this->get_param( 'dbs' ), |
| 43 |
) |
| 44 |
) |
| 45 |
); |
| 46 |
|
| 47 |
register_rest_route( |
| 48 |
WPDA_API::WPDA_NAMESPACE, |
| 49 |
'tree/cls', |
| 50 |
array( |
| 51 |
'methods' => array( 'POST' ), |
| 52 |
'callback' => array( $this, 'tree_cls' ), |
| 53 |
'permission_callback' => '__return_true', |
| 54 |
'args' => array( |
| 55 |
'dbs' => $this->get_param( 'dbs' ), |
| 56 |
'tbl' => $this->get_param( 'tbl' ), |
| 57 |
) |
| 58 |
) |
| 59 |
); |
| 60 |
|
| 61 |
register_rest_route( |
| 62 |
WPDA_API::WPDA_NAMESPACE, |
| 63 |
'tree/idx', |
| 64 |
array( |
| 65 |
'methods' => array( 'POST' ), |
| 66 |
'callback' => array( $this, 'tree_idx' ), |
| 67 |
'permission_callback' => '__return_true', |
| 68 |
'args' => array( |
| 69 |
'dbs' => $this->get_param( 'dbs' ), |
| 70 |
'tbl' => $this->get_param( 'tbl' ), |
| 71 |
) |
| 72 |
) |
| 73 |
); |
| 74 |
|
| 75 |
register_rest_route( |
| 76 |
WPDA_API::WPDA_NAMESPACE, |
| 77 |
'tree/trg', |
| 78 |
array( |
| 79 |
'methods' => array( 'POST' ), |
| 80 |
'callback' => array( $this, 'tree_trg' ), |
| 81 |
'permission_callback' => '__return_true', |
| 82 |
'args' => array( |
| 83 |
'dbs' => $this->get_param( 'dbs' ), |
| 84 |
'tbl' => $this->get_param( 'tbl' ), |
| 85 |
) |
| 86 |
) |
| 87 |
); |
| 88 |
|
| 89 |
register_rest_route( |
| 90 |
WPDA_API::WPDA_NAMESPACE, |
| 91 |
'tree/frk', |
| 92 |
array( |
| 93 |
'methods' => array( 'POST' ), |
| 94 |
'callback' => array( $this, 'tree_frk' ), |
| 95 |
'permission_callback' => '__return_true', |
| 96 |
'args' => array( |
| 97 |
'dbs' => $this->get_param( 'dbs' ), |
| 98 |
'tbl' => $this->get_param( 'tbl' ), |
| 99 |
) |
| 100 |
) |
| 101 |
); |
| 102 |
|
| 103 |
register_rest_route( |
| 104 |
WPDA_API::WPDA_NAMESPACE, |
| 105 |
'tree/fnc', |
| 106 |
array( |
| 107 |
'methods' => array( 'POST' ), |
| 108 |
'callback' => array( $this, 'tree_fnc' ), |
| 109 |
'permission_callback' => '__return_true', |
| 110 |
'args' => array( |
| 111 |
'dbs' => $this->get_param( 'dbs' ), |
| 112 |
) |
| 113 |
) |
| 114 |
); |
| 115 |
|
| 116 |
register_rest_route( |
| 117 |
WPDA_API::WPDA_NAMESPACE, |
| 118 |
'tree/prc', |
| 119 |
array( |
| 120 |
'methods' => array( 'POST' ), |
| 121 |
'callback' => array( $this, 'tree_prc' ), |
| 122 |
'permission_callback' => '__return_true', |
| 123 |
'args' => array( |
| 124 |
'dbs' => $this->get_param( 'dbs' ), |
| 125 |
) |
| 126 |
) |
| 127 |
); |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
public function tree_dbs( $request ) { |
| 132 |
if ( ! $this->current_user_can_access() ) { |
| 133 |
return $this->unauthorized(); |
| 134 |
} |
| 135 |
|
| 136 |
if ( ! $this->current_user_token_valid( $request ) ) { |
| 137 |
return $this->invalid_nonce(); |
| 138 |
} |
| 139 |
|
| 140 |
return $this->get_dbs(); |
| 141 |
} |
| 142 |
|
| 143 |
public function tree_tbl( $request ) { |
| 144 |
if ( ! $this->current_user_can_access() ) { |
| 145 |
return $this->unauthorized(); |
| 146 |
} |
| 147 |
|
| 148 |
if ( ! $this->current_user_token_valid( $request ) ) { |
| 149 |
return $this->invalid_nonce(); |
| 150 |
} |
| 151 |
|
| 152 |
$dbs = $request->get_param( 'dbs' ); |
| 153 |
|
| 154 |
return $this->get_tbl( $dbs ); |
| 155 |
} |
| 156 |
|
| 157 |
public function tree_vws( $request ) { |
| 158 |
if ( ! $this->current_user_can_access() ) { |
| 159 |
return $this->unauthorized(); |
| 160 |
} |
| 161 |
|
| 162 |
if ( ! $this->current_user_token_valid( $request ) ) { |
| 163 |
return $this->invalid_nonce(); |
| 164 |
} |
| 165 |
|
| 166 |
$dbs = $request->get_param( 'dbs' ); |
| 167 |
|
| 168 |
return $this->get_vws( $dbs ); |
| 169 |
} |
| 170 |
|
| 171 |
public function tree_cls( $request ) { |
| 172 |
if ( ! $this->current_user_can_access() ) { |
| 173 |
return $this->unauthorized(); |
| 174 |
} |
| 175 |
|
| 176 |
if ( ! $this->current_user_token_valid( $request ) ) { |
| 177 |
return $this->invalid_nonce(); |
| 178 |
} |
| 179 |
|
| 180 |
$dbs = $request->get_param( 'dbs' ); |
| 181 |
$tbl = $request->get_param( 'tbl' ); |
| 182 |
|
| 183 |
return $this->get_cls( $dbs, $tbl ); |
| 184 |
} |
| 185 |
|
| 186 |
public function tree_idx( $request ) { |
| 187 |
if ( ! $this->current_user_can_access() ) { |
| 188 |
return $this->unauthorized(); |
| 189 |
} |
| 190 |
|
| 191 |
if ( ! $this->current_user_token_valid( $request ) ) { |
| 192 |
return $this->invalid_nonce(); |
| 193 |
} |
| 194 |
|
| 195 |
$dbs = $request->get_param( 'dbs' ); |
| 196 |
$tbl = $request->get_param( 'tbl' ); |
| 197 |
|
| 198 |
return $this->get_idx( $dbs, $tbl ); |
| 199 |
} |
| 200 |
|
| 201 |
public function tree_trg( $request ) { |
| 202 |
if ( ! $this->current_user_can_access() ) { |
| 203 |
return $this->unauthorized(); |
| 204 |
} |
| 205 |
|
| 206 |
if ( ! $this->current_user_token_valid( $request ) ) { |
| 207 |
return $this->invalid_nonce(); |
| 208 |
} |
| 209 |
|
| 210 |
$dbs = $request->get_param( 'dbs' ); |
| 211 |
$tbl = $request->get_param( 'tbl' ); |
| 212 |
|
| 213 |
return $this->get_trg( $dbs, $tbl ); |
| 214 |
} |
| 215 |
|
| 216 |
public function tree_frk( $request ) { |
| 217 |
if ( ! $this->current_user_can_access() ) { |
| 218 |
return $this->unauthorized(); |
| 219 |
} |
| 220 |
|
| 221 |
if ( ! $this->current_user_token_valid( $request ) ) { |
| 222 |
return $this->invalid_nonce(); |
| 223 |
} |
| 224 |
|
| 225 |
$dbs = $request->get_param( 'dbs' ); |
| 226 |
$tbl = $request->get_param( 'tbl' ); |
| 227 |
|
| 228 |
return $this->get_frk( $dbs, $tbl ); |
| 229 |
} |
| 230 |
|
| 231 |
public function tree_fnc( $request ) { |
| 232 |
if ( ! $this->current_user_can_access() ) { |
| 233 |
return $this->unauthorized(); |
| 234 |
} |
| 235 |
|
| 236 |
if ( ! $this->current_user_token_valid( $request ) ) { |
| 237 |
return $this->invalid_nonce(); |
| 238 |
} |
| 239 |
|
| 240 |
$dbs = $request->get_param( 'dbs' ); |
| 241 |
|
| 242 |
return $this->get_fnc( $dbs ); |
| 243 |
} |
| 244 |
|
| 245 |
public function tree_prc( $request ) { |
| 246 |
if ( ! $this->current_user_can_access() ) { |
| 247 |
return $this->unauthorized(); |
| 248 |
} |
| 249 |
|
| 250 |
if ( ! $this->current_user_token_valid( $request ) ) { |
| 251 |
return $this->invalid_nonce(); |
| 252 |
} |
| 253 |
|
| 254 |
$dbs = $request->get_param( 'dbs' ); |
| 255 |
|
| 256 |
return $this->get_prc( $dbs ); |
| 257 |
} |
| 258 |
|
| 259 |
public function get_dbs() { |
| 260 |
// Local databases |
| 261 |
global $wpdb; |
| 262 |
$local = $wpdb->get_results(" |
| 263 |
SELECT schema_name AS dbs, |
| 264 |
'local' AS dbs_type |
| 265 |
FROM information_schema.schemata |
| 266 |
ORDER BY schema_name |
| 267 |
", |
| 268 |
'ARRAY_A' |
| 269 |
); |
| 270 |
|
| 271 |
for ( $i=0; $i < count( $local ); $i++ ) { |
| 272 |
if ( $wpdb->dbname === $local[ $i ]['dbs'] ) { |
| 273 |
// WordPress database |
| 274 |
$local[ $i ]['dbs_type'] = 'wp'; |
| 275 |
} |
| 276 |
// System databases |
| 277 |
if ( |
| 278 |
'information_schema' === $local[ $i ]['dbs'] || |
| 279 |
'mysql' === $local[ $i ]['dbs'] || |
| 280 |
'performance_schema' === $local[ $i ]['dbs'] || |
| 281 |
'sys' === $local[ $i ]['dbs'] |
| 282 |
) { |
| 283 |
// WordPress database |
| 284 |
$local[ $i ]['dbs_type'] = 'system'; |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
// Remote databases |
| 289 |
$remote = array(); |
| 290 |
$rdb = WPDADB::get_remote_databases(); |
| 291 |
foreach ( $rdb as $key => $val ) { |
| 292 |
$remote[] = array( |
| 293 |
'dbs' => $key, |
| 294 |
'dbs_type' => 'remote', |
| 295 |
); |
| 296 |
} |
| 297 |
|
| 298 |
return $this->WPDA_Rest_Response( |
| 299 |
'', |
| 300 |
array_merge( $local, $remote ) //phpcs:ignore - 8.1 proof |
| 301 |
); |
| 302 |
} |
| 303 |
|
| 304 |
public function get_tbl_vws( $dbs ) { |
| 305 |
|
| 306 |
$wpdadb = WPDADB::get_db_connection( $dbs ); |
| 307 |
if ( $wpdadb === null ) { |
| 308 |
return $this->WPDA_Rest_Response( |
| 309 |
'', |
| 310 |
array() |
| 311 |
); |
| 312 |
} |
| 313 |
|
| 314 |
$query = $wpdadb->prepare( |
| 315 |
" |
| 316 |
select table_name as table_name |
| 317 |
from information_schema.tables |
| 318 |
where table_schema = %s |
| 319 |
order by table_name |
| 320 |
", |
| 321 |
array( |
| 322 |
$wpdadb->dbname, |
| 323 |
) |
| 324 |
); |
| 325 |
|
| 326 |
return $this->WPDA_Rest_Response( |
| 327 |
'', |
| 328 |
array_column( |
| 329 |
$wpdadb->get_results( $query, 'ARRAY_A' ), // phpcs:ignore Standard.Category.SniffName.ErrorCode |
| 330 |
'table_name' |
| 331 |
) |
| 332 |
); |
| 333 |
|
| 334 |
} |
| 335 |
|
| 336 |
private function get_tbl( $dbs ) { |
| 337 |
|
| 338 |
$wpdadb = WPDADB::get_db_connection( $dbs ); |
| 339 |
if ( $wpdadb === null ) { |
| 340 |
return $this->WPDA_Rest_Response( |
| 341 |
'', |
| 342 |
array() |
| 343 |
); |
| 344 |
} |
| 345 |
|
| 346 |
$query = $wpdadb->prepare( |
| 347 |
" |
| 348 |
select table_name as table_name |
| 349 |
from information_schema.tables |
| 350 |
where table_schema = %s |
| 351 |
and table_type not in ('VIEW', 'SYSTEM VIEW') |
| 352 |
order by table_name |
| 353 |
", |
| 354 |
array( |
| 355 |
$wpdadb->dbname, |
| 356 |
) |
| 357 |
); |
| 358 |
|
| 359 |
return $this->WPDA_Rest_Response( |
| 360 |
'', |
| 361 |
array_column( |
| 362 |
$wpdadb->get_results( $query, 'ARRAY_A' ), // phpcs:ignore Standard.Category.SniffName.ErrorCode |
| 363 |
'table_name' |
| 364 |
) |
| 365 |
); |
| 366 |
|
| 367 |
} |
| 368 |
|
| 369 |
private function get_vws( $dbs ) { |
| 370 |
|
| 371 |
$wpdadb = WPDADB::get_db_connection( $dbs ); |
| 372 |
if ( $wpdadb === null ) { |
| 373 |
return $this->WPDA_Rest_Response( |
| 374 |
'', |
| 375 |
array() |
| 376 |
); |
| 377 |
} |
| 378 |
|
| 379 |
$query = $wpdadb->prepare(" |
| 380 |
select table_name as view_name |
| 381 |
from information_schema.tables |
| 382 |
where table_schema = %s |
| 383 |
and table_type in ('VIEW', 'SYSTEM VIEW') |
| 384 |
order by table_name |
| 385 |
", |
| 386 |
array( |
| 387 |
$wpdadb->dbname, |
| 388 |
) |
| 389 |
); |
| 390 |
|
| 391 |
return $this->WPDA_Rest_Response( |
| 392 |
'', |
| 393 |
array_column( |
| 394 |
$wpdadb->get_results( $query, 'ARRAY_A' ), // phpcs:ignore Standard.Category.SniffName.ErrorCode |
| 395 |
'view_name' |
| 396 |
) |
| 397 |
); |
| 398 |
|
| 399 |
} |
| 400 |
|
| 401 |
public function get_cls( $dbs, $tbl ) { |
| 402 |
|
| 403 |
$wpdadb = WPDADB::get_db_connection( $dbs ); |
| 404 |
if ( $wpdadb === null ) { |
| 405 |
return $this->WPDA_Rest_Response( |
| 406 |
'', |
| 407 |
array() |
| 408 |
); |
| 409 |
} |
| 410 |
|
| 411 |
$query = $wpdadb->prepare(" |
| 412 |
SELECT column_name AS column_name, |
| 413 |
extra AS extra, |
| 414 |
column_type AS column_type, |
| 415 |
is_nullable AS is_nullable, |
| 416 |
IF(LEFT(column_default,1)='\'' AND RIGHT(column_default,1)='\'', SUBSTR(column_default,2,LENGTH(column_default)-2), column_default) AS column_default, |
| 417 |
character_maximum_length AS character_maximum_length, |
| 418 |
numeric_scale AS numeric_scale, |
| 419 |
numeric_precision AS numeric_precision, |
| 420 |
ordinal_position AS ordinal_position |
| 421 |
FROM information_schema.columns |
| 422 |
WHERE table_schema = %s |
| 423 |
AND table_name = %s |
| 424 |
ORDER BY ordinal_position |
| 425 |
", |
| 426 |
array( |
| 427 |
$wpdadb->dbname, |
| 428 |
$tbl, |
| 429 |
) |
| 430 |
); |
| 431 |
|
| 432 |
return $this->WPDA_Rest_Response( |
| 433 |
'', |
| 434 |
$wpdadb->get_results( $query, 'ARRAY_A' ) // phpcs:ignore Standard.Category.SniffName.ErrorCode |
| 435 |
); |
| 436 |
|
| 437 |
} |
| 438 |
|
| 439 |
private function get_idx( $dbs, $tbl ) { |
| 440 |
|
| 441 |
$wpdadb = WPDADB::get_db_connection( $dbs ); |
| 442 |
if ( $wpdadb === null ) { |
| 443 |
return $this->WPDA_Rest_Response( |
| 444 |
'', |
| 445 |
array() |
| 446 |
); |
| 447 |
} |
| 448 |
|
| 449 |
$query = $wpdadb->prepare(" |
| 450 |
SELECT index_name AS index_name, |
| 451 |
IF(non_unique=0, 'YES', 'NO') AS non_unique, |
| 452 |
seq_in_index AS seq_in_index, |
| 453 |
column_name AS column_name, |
| 454 |
collation AS collation, |
| 455 |
nullable AS nullable, |
| 456 |
index_type AS index_type, |
| 457 |
cardinality AS cardinality |
| 458 |
FROM information_schema.statistics |
| 459 |
WHERE table_schema = %s |
| 460 |
AND table_name = %s |
| 461 |
ORDER BY index_name, seq_in_index |
| 462 |
", |
| 463 |
array( |
| 464 |
$wpdadb->dbname, |
| 465 |
$tbl, |
| 466 |
) |
| 467 |
); |
| 468 |
|
| 469 |
return $this->WPDA_Rest_Response( |
| 470 |
'', |
| 471 |
$wpdadb->get_results( $query, 'ARRAY_A' ) // phpcs:ignore Standard.Category.SniffName.ErrorCode |
| 472 |
); |
| 473 |
|
| 474 |
} |
| 475 |
|
| 476 |
private function get_trg( $dbs, $tbl ) { |
| 477 |
|
| 478 |
$wpdadb = WPDADB::get_db_connection( $dbs ); |
| 479 |
if ( $wpdadb === null ) { |
| 480 |
return $this->WPDA_Rest_Response( |
| 481 |
'', |
| 482 |
array() |
| 483 |
); |
| 484 |
} |
| 485 |
|
| 486 |
$query = $wpdadb->prepare(' |
| 487 |
SELECT trigger_name AS trigger_name, |
| 488 |
event_manipulation AS event_manipulation, |
| 489 |
action_timing AS action_timing, |
| 490 |
action_statement AS action_statement |
| 491 |
FROM information_schema.triggers |
| 492 |
WHERE event_object_schema = %s |
| 493 |
AND event_object_table = %s |
| 494 |
ORDER BY trigger_name |
| 495 |
', |
| 496 |
array( |
| 497 |
$wpdadb->dbname, |
| 498 |
$tbl, |
| 499 |
) |
| 500 |
); |
| 501 |
|
| 502 |
return $this->WPDA_Rest_Response( |
| 503 |
'', |
| 504 |
$wpdadb->get_results( $query, 'ARRAY_A' ) // phpcs:ignore Standard.Category.SniffName.ErrorCode |
| 505 |
); |
| 506 |
|
| 507 |
} |
| 508 |
|
| 509 |
private function get_frk( $dbs, $tbl ) { |
| 510 |
|
| 511 |
$wpdadb = WPDADB::get_db_connection( $dbs ); |
| 512 |
if ( $wpdadb === null ) { |
| 513 |
return $this->WPDA_Rest_Response( |
| 514 |
'', |
| 515 |
array() |
| 516 |
); |
| 517 |
} |
| 518 |
|
| 519 |
$query = $wpdadb->prepare(' |
| 520 |
select constraint_name AS constraint_name, |
| 521 |
column_name AS column_name, |
| 522 |
referenced_table_name AS referenced_table_name, |
| 523 |
referenced_column_name AS referenced_column_name |
| 524 |
from information_schema.key_column_usage |
| 525 |
where table_schema = %s |
| 526 |
and table_name = %s |
| 527 |
and referenced_table_name is not null |
| 528 |
order by ordinal_position |
| 529 |
', |
| 530 |
array( |
| 531 |
$wpdadb->dbname, |
| 532 |
$tbl, |
| 533 |
) |
| 534 |
); |
| 535 |
|
| 536 |
return $this->WPDA_Rest_Response( |
| 537 |
'', |
| 538 |
$wpdadb->get_results( $query, 'ARRAY_A' ) // phpcs:ignore Standard.Category.SniffName.ErrorCode |
| 539 |
); |
| 540 |
|
| 541 |
} |
| 542 |
|
| 543 |
private function get_prc( $dbs ) { |
| 544 |
|
| 545 |
return $this->get_rtn( $dbs, 'PROCEDURE' ); |
| 546 |
} |
| 547 |
|
| 548 |
private function get_fnc( $dbs ) { |
| 549 |
|
| 550 |
return $this->get_rtn( $dbs, 'FUNCTION' ); |
| 551 |
} |
| 552 |
|
| 553 |
private function get_rtn( $dbs, $typ ) { |
| 554 |
|
| 555 |
$wpdadb = WPDADB::get_db_connection( $dbs ); |
| 556 |
if ( $wpdadb === null ) { |
| 557 |
return $this->WPDA_Rest_Response( |
| 558 |
'', |
| 559 |
array() |
| 560 |
); |
| 561 |
} |
| 562 |
|
| 563 |
$query = $wpdadb->prepare(' |
| 564 |
SELECT routine_name AS routine_name, |
| 565 |
routine_definition AS routine_definition |
| 566 |
FROM information_schema.routines |
| 567 |
WHERE routine_schema = %s |
| 568 |
AND routine_type = %s |
| 569 |
', |
| 570 |
array( |
| 571 |
$wpdadb->dbname, |
| 572 |
$typ, |
| 573 |
) |
| 574 |
); |
| 575 |
|
| 576 |
return $this->WPDA_Rest_Response( |
| 577 |
'', |
| 578 |
$wpdadb->get_results( $query, 'ARRAY_A' ) // phpcs:ignore Standard.Category.SniffName.ErrorCode |
| 579 |
); |
| 580 |
|
| 581 |
} |
| 582 |
|
| 583 |
} |
| 584 |
|
| 585 |
} |