TableStats.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. foreach ($_REQUEST['t_h'] as $key => $value) {
  122. if ($this->db . '.' . $this->tableName == $key) {
  123. $this->x = (double) $_REQUEST['t_x'][$key];
  124. $this->y = (double) $_REQUEST['t_y'][$key];
  125. break;
  126. }
  127. }
  128. }
  129. /**
  130. * Loads the table's display field
  131. *
  132. * @return void
  133. */
  134. protected function loadDisplayField()
  135. {
  136. $this->displayfield = $this->relation->getDisplayField($this->db, $this->tableName);
  137. }
  138. /**
  139. * Loads the PRIMARY key.
  140. *
  141. * @return void
  142. */
  143. protected function loadPrimaryKey()
  144. {
  145. $result = $GLOBALS['dbi']->query(
  146. 'SHOW INDEX FROM ' . Util::backquote($this->tableName) . ';',
  147. DatabaseInterface::CONNECT_USER,
  148. DatabaseInterface::QUERY_STORE
  149. );
  150. if ($GLOBALS['dbi']->numRows($result) > 0) {
  151. while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
  152. if ($row['Key_name'] == 'PRIMARY') {
  153. $this->primary[] = $row['Column_name'];
  154. }
  155. }
  156. }
  157. }
  158. /**
  159. * Returns title of the current table,
  160. * title can have the dimensions/co-ordinates of the table
  161. *
  162. * @return string title of the current table
  163. */
  164. protected function getTitle()
  165. {
  166. return ($this->tableDimension
  167. ? sprintf('%.0fx%0.f', $this->width, $this->heightCell)
  168. : ''
  169. )
  170. . ' ' . $this->tableName;
  171. }
  172. }