StorageEngine.class.php 13 KB

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