TableStats.class.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Contains abstract class to hold table preferences/statistics
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. * Table preferences/statistics
  13. *
  14. * This class preserves the table co-ordinates,fields
  15. * and helps in drawing/generating the tables.
  16. *
  17. * @package PhpMyAdmin
  18. * @abstract
  19. */
  20. abstract class TableStats
  21. {
  22. protected $diagram;
  23. protected $db;
  24. protected $pageNumber;
  25. protected $tableName;
  26. protected $showKeys;
  27. protected $tableDimension;
  28. public $displayfield;
  29. public $fields = array();
  30. public $primary = array();
  31. public $x, $y;
  32. public $width = 0;
  33. public $heightCell = 0;
  34. protected $offline;
  35. /**
  36. * Constructor
  37. *
  38. * @param object $diagram schema diagram
  39. * @param string $db current db name
  40. * @param integer $pageNumber current page number (from the
  41. * $cfg['Servers'][$i]['table_coords'] table)
  42. * @param string $tableName table name
  43. * @param boolean $showKeys whether to display keys or not
  44. * @param boolean $tableDimension whether to display table position or not
  45. * @param boolean $offline whether the coordinates are sent
  46. * from the browser
  47. */
  48. public function __construct(
  49. $diagram, $db, $pageNumber, $tableName, $showKeys, $tableDimension, $offline
  50. ) {
  51. $this->diagram = $diagram;
  52. $this->db = $db;
  53. $this->pageNumber = $pageNumber;
  54. $this->tableName = $tableName;
  55. $this->showKeys = $showKeys;
  56. $this->tableDimension = $tableDimension;
  57. $this->offline = $offline;
  58. // checks whether the table exists
  59. // and loads fields
  60. $this->validateTableAndLoadFields();
  61. // load table coordinates
  62. $this->loadCoordinates();
  63. // loads display field
  64. $this->loadDisplayField();
  65. // loads primary keys
  66. $this->loadPrimaryKey();
  67. }
  68. /**
  69. * Validate whether the table exists.
  70. *
  71. * @return void
  72. */
  73. protected function validateTableAndLoadFields()
  74. {
  75. $sql = 'DESCRIBE ' . PMA_Util::backquote($this->tableName);
  76. $result = $GLOBALS['dbi']->tryQuery(
  77. $sql, null, PMA_DatabaseInterface::QUERY_STORE
  78. );
  79. if (! $result || ! $GLOBALS['dbi']->numRows($result)) {
  80. $this->showMissingTableError();
  81. }
  82. if ($this->showKeys) {
  83. $indexes = PMA_Index::getFromTable($this->tableName, $this->db);
  84. $all_columns = array();
  85. foreach ($indexes as $index) {
  86. $all_columns = array_merge(
  87. $all_columns,
  88. array_flip(array_keys($index->getColumns()))
  89. );
  90. }
  91. $this->fields = array_keys($all_columns);
  92. } else {
  93. while ($row = $GLOBALS['dbi']->fetchRow($result)) {
  94. $this->fields[] = $row[0];
  95. }
  96. }
  97. }
  98. /**
  99. * Displays an error when the table cannot be found.
  100. *
  101. * @return void
  102. * @abstract
  103. */
  104. protected abstract function showMissingTableError();
  105. /**
  106. * Loads coordinates of a table
  107. *
  108. * @return void
  109. */
  110. protected function loadCoordinates()
  111. {
  112. foreach ($_REQUEST['t_h'] as $key => $value) {
  113. if ($this->db . '.' . $this->tableName == $key) {
  114. $this->x = (double) $_REQUEST['t_x'][$key];
  115. $this->y = (double) $_REQUEST['t_y'][$key];
  116. break;
  117. }
  118. }
  119. }
  120. /**
  121. * Loads the table's display field
  122. *
  123. * @return void
  124. */
  125. protected function loadDisplayField()
  126. {
  127. $this->displayfield = PMA_getDisplayField($this->db, $this->tableName);
  128. }
  129. /**
  130. * Loads the PRIMARY key.
  131. *
  132. * @return void
  133. */
  134. protected function loadPrimaryKey()
  135. {
  136. $result = $GLOBALS['dbi']->query(
  137. 'SHOW INDEX FROM ' . PMA_Util::backquote($this->tableName) . ';',
  138. null, PMA_DatabaseInterface::QUERY_STORE
  139. );
  140. if ($GLOBALS['dbi']->numRows($result) > 0) {
  141. while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
  142. if ($row['Key_name'] == 'PRIMARY') {
  143. $this->primary[] = $row['Column_name'];
  144. }
  145. }
  146. }
  147. }
  148. /**
  149. * Returns title of the current table,
  150. * title can have the dimensions/co-ordinates of the table
  151. *
  152. * @return string title of the current table
  153. */
  154. protected function getTitle()
  155. {
  156. return ($this->tableDimension
  157. ? sprintf('%.0fx%0.f', $this->width, $this->heightCell)
  158. : ''
  159. )
  160. . ' ' . $this->tableName;
  161. }
  162. }