TableStats.php 5.3 KB

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