RealRowCountController.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Controllers\Database\Structure;
  4. use PhpMyAdmin\Controllers\Database\AbstractController;
  5. use PhpMyAdmin\DatabaseInterface;
  6. use PhpMyAdmin\ResponseRenderer;
  7. use PhpMyAdmin\Template;
  8. use PhpMyAdmin\Url;
  9. use PhpMyAdmin\Util;
  10. /**
  11. * Handles request for real row count on database level view page.
  12. */
  13. final class RealRowCountController extends AbstractController
  14. {
  15. /** @var DatabaseInterface */
  16. private $dbi;
  17. public function __construct(ResponseRenderer $response, Template $template, string $db, DatabaseInterface $dbi)
  18. {
  19. parent::__construct($response, $template, $db);
  20. $this->dbi = $dbi;
  21. }
  22. public function __invoke(): void
  23. {
  24. global $cfg, $db, $errorUrl;
  25. $parameters = [
  26. 'real_row_count_all' => $_REQUEST['real_row_count_all'] ?? null,
  27. 'table' => $_REQUEST['table'] ?? null,
  28. ];
  29. Util::checkParameters(['db']);
  30. $errorUrl = Util::getScriptNameForOption($cfg['DefaultTabDatabase'], 'database');
  31. $errorUrl .= Url::getCommon(['db' => $db], '&');
  32. if (! $this->hasDatabase() || ! $this->response->isAjax()) {
  33. return;
  34. }
  35. [$tables] = Util::getDbInfo($this->db, '_structure');
  36. // If there is a request to update all table's row count.
  37. if (! isset($parameters['real_row_count_all'])) {
  38. // Get the real row count for the table.
  39. $realRowCount = (int) $this->dbi
  40. ->getTable($this->db, (string) $parameters['table'])
  41. ->getRealRowCountTable();
  42. // Format the number.
  43. $realRowCount = Util::formatNumber($realRowCount, 0);
  44. $this->response->addJSON(['real_row_count' => $realRowCount]);
  45. return;
  46. }
  47. // Array to store the results.
  48. $realRowCountAll = [];
  49. // Iterate over each table and fetch real row count.
  50. foreach ($tables as $table) {
  51. $rowCount = $this->dbi
  52. ->getTable($this->db, $table['TABLE_NAME'])
  53. ->getRealRowCountTable();
  54. $realRowCountAll[] = [
  55. 'table' => $table['TABLE_NAME'],
  56. 'row_count' => Util::formatNumber($rowCount, 0),
  57. ];
  58. }
  59. $this->response->addJSON(['real_row_count_all' => $realRowCountAll]);
  60. }
  61. }