StorageEngine.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Library for extracting information about the available storage engines
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. * defines
  13. */
  14. define('PMA_ENGINE_SUPPORT_NO', 0);
  15. define('PMA_ENGINE_SUPPORT_DISABLED', 1);
  16. define('PMA_ENGINE_SUPPORT_YES', 2);
  17. define('PMA_ENGINE_SUPPORT_DEFAULT', 3);
  18. define('PMA_ENGINE_DETAILS_TYPE_PLAINTEXT', 0);
  19. define('PMA_ENGINE_DETAILS_TYPE_SIZE', 1);
  20. define('PMA_ENGINE_DETAILS_TYPE_NUMERIC', 2); //Has no effect yet...
  21. define('PMA_ENGINE_DETAILS_TYPE_BOOLEAN', 3); // 'ON' or 'OFF'
  22. /**
  23. * Base Storage Engine Class
  24. *
  25. * @package PhpMyAdmin
  26. */
  27. class PMA_StorageEngine
  28. {
  29. /**
  30. * @var string engine name
  31. */
  32. var $engine = 'dummy';
  33. /**
  34. * @var string engine title/description
  35. */
  36. var $title = 'PMA Dummy Engine Class';
  37. /**
  38. * @var string engine lang description
  39. */
  40. var $comment
  41. = 'If you read this text inside phpMyAdmin, something went wrong...';
  42. /**
  43. * @var integer engine supported by current server
  44. */
  45. var $support = PMA_ENGINE_SUPPORT_NO;
  46. /**
  47. * Constructor
  48. *
  49. * @param string $engine The engine ID
  50. */
  51. public function __construct($engine)
  52. {
  53. $storage_engines = PMA_StorageEngine::getStorageEngines();
  54. if (! empty($storage_engines[$engine])) {
  55. $this->engine = $engine;
  56. $this->title = $storage_engines[$engine]['Engine'];
  57. $this->comment = (isset($storage_engines[$engine]['Comment'])
  58. ? $storage_engines[$engine]['Comment']
  59. : '');
  60. switch ($storage_engines[$engine]['Support']) {
  61. case 'DEFAULT':
  62. $this->support = PMA_ENGINE_SUPPORT_DEFAULT;
  63. break;
  64. case 'YES':
  65. $this->support = PMA_ENGINE_SUPPORT_YES;
  66. break;
  67. case 'DISABLED':
  68. $this->support = PMA_ENGINE_SUPPORT_DISABLED;
  69. break;
  70. case 'NO':
  71. default:
  72. $this->support = PMA_ENGINE_SUPPORT_NO;
  73. }
  74. }
  75. }
  76. /**
  77. * Returns array of storage engines
  78. *
  79. * @static
  80. * @staticvar array $storage_engines storage engines
  81. * @access public
  82. * @return string[] array of storage engines
  83. */
  84. static public function getStorageEngines()
  85. {
  86. static $storage_engines = null;
  87. if (null == $storage_engines) {
  88. if (PMA_DRIZZLE) {
  89. $sql = "SELECT
  90. p.plugin_name AS Engine,
  91. (CASE
  92. WHEN p.plugin_name = @@storage_engine THEN 'DEFAULT'
  93. WHEN p.is_active THEN 'YES'
  94. ELSE 'DISABLED' END) AS Support,
  95. m.module_description AS Comment
  96. FROM data_dictionary.plugins p
  97. JOIN data_dictionary.modules m USING (module_name)
  98. WHERE p.plugin_type = 'StorageEngine'
  99. AND p.plugin_name NOT IN ('FunctionEngine', 'schema')";
  100. $storage_engines = $GLOBALS['dbi']->fetchResult($sql, 'Engine');
  101. } else {
  102. $storage_engines
  103. = $GLOBALS['dbi']->fetchResult('SHOW STORAGE ENGINES', 'Engine');
  104. }
  105. }
  106. return $storage_engines;
  107. }
  108. /**
  109. * Returns HTML code for storage engine select box
  110. *
  111. * @param string $name The name of the select form element
  112. * @param string $id The ID of the form field
  113. * @param string $selected The selected engine
  114. * @param boolean $offerUnavailableEngines Should unavailable storage
  115. * engines be offered?
  116. *
  117. * @static
  118. * @return string html selectbox
  119. */
  120. static public function getHtmlSelect(
  121. $name = 'engine', $id = null,
  122. $selected = null, $offerUnavailableEngines = false
  123. ) {
  124. $selected = strtolower($selected);
  125. $output = '<select name="' . $name . '"'
  126. . (empty($id) ? '' : ' id="' . $id . '"') . '>' . "\n";
  127. foreach (PMA_StorageEngine::getStorageEngines() as $key => $details) {
  128. // Don't show PERFORMANCE_SCHEMA engine (MySQL 5.5)
  129. // Don't show MyISAM for Drizzle (allowed only for temporary tables)
  130. if (! $offerUnavailableEngines
  131. && ($details['Support'] == 'NO'
  132. || $details['Support'] == 'DISABLED'
  133. || $details['Engine'] == 'PERFORMANCE_SCHEMA')
  134. || (PMA_DRIZZLE && $details['Engine'] == 'MyISAM')
  135. ) {
  136. continue;
  137. }
  138. $output .= ' <option value="' . htmlspecialchars($key) . '"'
  139. . (empty($details['Comment'])
  140. ? '' : ' title="' . htmlspecialchars($details['Comment']) . '"')
  141. . (strtolower($key) == $selected
  142. || (empty($selected) && $details['Support'] == 'DEFAULT')
  143. ? ' selected="selected"' : '')
  144. . '>' . "\n"
  145. . ' ' . htmlspecialchars($details['Engine']) . "\n"
  146. . ' </option>' . "\n";
  147. }
  148. $output .= '</select>' . "\n";
  149. return $output;
  150. }
  151. /**
  152. * Loads the corresponding engine plugin, if available.
  153. *
  154. * @param string $engine The engine ID
  155. *
  156. * @static
  157. * @return PMA_StorageEngine The engine plugin
  158. */
  159. static public function getEngine($engine)
  160. {
  161. $engine = str_replace('/', '', str_replace('.', '', $engine));
  162. $filename = './libraries/engines/' . strtolower($engine) . '.lib.php';
  163. if (file_exists($filename) && include_once $filename) {
  164. switch(strtolower($engine)) {
  165. case 'bdb':
  166. return new PMA_StorageEngine_Bdb($engine);
  167. case 'berkeleydb':
  168. return new PMA_StorageEngine_Berkeleydb($engine);
  169. case 'binlog':
  170. return new PMA_StorageEngine_Binlog($engine);
  171. case 'innobase':
  172. return new PMA_StorageEngine_Innobase($engine);
  173. case 'innodb':
  174. return new PMA_StorageEngine_Innodb($engine);
  175. case 'memory':
  176. return new PMA_StorageEngine_Memory($engine);
  177. case 'merge':
  178. return new PMA_StorageEngine_Merge($engine);
  179. case 'mrg_myisam':
  180. return new PMA_StorageEngine_MrgMyisam($engine);
  181. case 'myisam':
  182. return new PMA_StorageEngine_Myisam($engine);
  183. case 'ndbcluster':
  184. return new PMA_StorageEngine_Ndbcluster($engine);
  185. case 'pbxt':
  186. return new PMA_StorageEngine_Pbxt($engine);
  187. case 'performance_schema':
  188. return new PMA_StorageEngine_PerformanceSchema($engine);
  189. }
  190. } else {
  191. return new PMA_StorageEngine($engine);
  192. }
  193. }
  194. /**
  195. * Returns true if given engine name is supported/valid, otherwise false
  196. *
  197. * @param string $engine name of engine
  198. *
  199. * @static
  200. * @return boolean whether $engine is valid or not
  201. */
  202. static public function isValid($engine)
  203. {
  204. if ($engine == "PBMS") {
  205. return true;
  206. }
  207. $storage_engines = PMA_StorageEngine::getStorageEngines();
  208. return isset($storage_engines[$engine]);
  209. }
  210. /**
  211. * Returns as HTML table of the engine's server variables
  212. *
  213. * @return string The table that was generated based on the retrieved
  214. * information
  215. */
  216. public function getHtmlVariables()
  217. {
  218. $odd_row = false;
  219. $ret = '';
  220. foreach ($this->getVariablesStatus() as $details) {
  221. $ret .= '<tr class="' . ($odd_row ? 'odd' : 'even') . '">' . "\n"
  222. . ' <td>' . "\n";
  223. if (! empty($details['desc'])) {
  224. $ret .= ' '
  225. . PMA_Util::showHint($details['desc'])
  226. . "\n";
  227. }
  228. $ret .= ' </td>' . "\n"
  229. . ' <th>' . htmlspecialchars($details['title']) . '</th>'
  230. . "\n"
  231. . ' <td class="value">';
  232. switch ($details['type']) {
  233. case PMA_ENGINE_DETAILS_TYPE_SIZE:
  234. $parsed_size = $this->resolveTypeSize($details['value']);
  235. $ret .= $parsed_size[0] . '&nbsp;' . $parsed_size[1];
  236. unset($parsed_size);
  237. break;
  238. case PMA_ENGINE_DETAILS_TYPE_NUMERIC:
  239. $ret .= PMA_Util::formatNumber($details['value']) . ' ';
  240. break;
  241. default:
  242. $ret .= htmlspecialchars($details['value']) . ' ';
  243. }
  244. $ret .= '</td>' . "\n"
  245. . '</tr>' . "\n";
  246. $odd_row = ! $odd_row;
  247. }
  248. if (! $ret) {
  249. $ret = '<p>' . "\n"
  250. . ' '
  251. . __('There is no detailed status information available for this storage engine.')
  252. . "\n"
  253. . '</p>' . "\n";
  254. } else {
  255. $ret = '<table class="data">' . "\n" . $ret . '</table>' . "\n";
  256. }
  257. return $ret;
  258. }
  259. /**
  260. * Returns the engine specific handling for
  261. * PMA_ENGINE_DETAILS_TYPE_SIZE type variables.
  262. *
  263. * This function should be overridden when
  264. * PMA_ENGINE_DETAILS_TYPE_SIZE type needs to be
  265. * handled differently for a particular engine.
  266. *
  267. * @param integer $value Value to format
  268. *
  269. * @return string the formatted value and its unit
  270. */
  271. public function resolveTypeSize($value)
  272. {
  273. return PMA_Util::formatByteDown($value);
  274. }
  275. /**
  276. * Returns array with detailed info about engine specific server variables
  277. *
  278. * @return array array with detailed info about specific engine server variables
  279. */
  280. public function getVariablesStatus()
  281. {
  282. $variables = $this->getVariables();
  283. $like = $this->getVariablesLikePattern();
  284. if ($like) {
  285. $like = " LIKE '" . $like . "' ";
  286. } else {
  287. $like = '';
  288. }
  289. $mysql_vars = array();
  290. $sql_query = 'SHOW GLOBAL VARIABLES ' . $like . ';';
  291. $res = $GLOBALS['dbi']->query($sql_query);
  292. while ($row = $GLOBALS['dbi']->fetchAssoc($res)) {
  293. if (isset($variables[$row['Variable_name']])) {
  294. $mysql_vars[$row['Variable_name']]
  295. = $variables[$row['Variable_name']];
  296. } elseif (! $like
  297. && strpos(strtolower($row['Variable_name']), strtolower($this->engine)) !== 0
  298. ) {
  299. continue;
  300. }
  301. $mysql_vars[$row['Variable_name']]['value'] = $row['Value'];
  302. if (empty($mysql_vars[$row['Variable_name']]['title'])) {
  303. $mysql_vars[$row['Variable_name']]['title'] = $row['Variable_name'];
  304. }
  305. if (! isset($mysql_vars[$row['Variable_name']]['type'])) {
  306. $mysql_vars[$row['Variable_name']]['type']
  307. = PMA_ENGINE_DETAILS_TYPE_PLAINTEXT;
  308. }
  309. }
  310. $GLOBALS['dbi']->freeResult($res);
  311. return $mysql_vars;
  312. }
  313. /**
  314. * Reveals the engine's title
  315. *
  316. * @return string The title
  317. */
  318. public function getTitle()
  319. {
  320. return $this->title;
  321. }
  322. /**
  323. * Fetches the server's comment about this engine
  324. *
  325. * @return string The comment
  326. */
  327. public function getComment()
  328. {
  329. return $this->comment;
  330. }
  331. /**
  332. * Information message on whether this storge engine is supported
  333. *
  334. * @return string The localized message.
  335. */
  336. public function getSupportInformationMessage()
  337. {
  338. switch ($this->support) {
  339. case PMA_ENGINE_SUPPORT_DEFAULT:
  340. $message = __('%s is the default storage engine on this MySQL server.');
  341. break;
  342. case PMA_ENGINE_SUPPORT_YES:
  343. $message = __('%s is available on this MySQL server.');
  344. break;
  345. case PMA_ENGINE_SUPPORT_DISABLED:
  346. $message = __('%s has been disabled for this MySQL server.');
  347. break;
  348. case PMA_ENGINE_SUPPORT_NO:
  349. default:
  350. $message = __('This MySQL server does not support the %s storage engine.');
  351. }
  352. return sprintf($message, htmlspecialchars($this->title));
  353. }
  354. /**
  355. * Generates a list of MySQL variables that provide information about this
  356. * engine. This function should be overridden when extending this class
  357. * for a particular engine.
  358. *
  359. * @return array The list of variables.
  360. */
  361. public function getVariables()
  362. {
  363. return array();
  364. }
  365. /**
  366. * Returns string with filename for the MySQL helppage
  367. * about this storage engine
  368. *
  369. * @return string MySQL help page filename
  370. */
  371. public function getMysqlHelpPage()
  372. {
  373. return $this->engine . '-storage-engine';
  374. }
  375. /**
  376. * Returns the pattern to be used in the query for SQL variables
  377. * related to the storage engine
  378. *
  379. * @return string SQL query LIKE pattern
  380. */
  381. public function getVariablesLikePattern()
  382. {
  383. return false;
  384. }
  385. /**
  386. * Returns a list of available information pages with labels
  387. *
  388. * @return string[] The list
  389. */
  390. public function getInfoPages()
  391. {
  392. return array();
  393. }
  394. /**
  395. * Generates the requested information page
  396. *
  397. * @param string $id The page ID
  398. *
  399. * @return string|boolean The page or false on error.
  400. */
  401. public function getPage($id)
  402. {
  403. return false;
  404. }
  405. }
  406. ?>