PageRenderTime 29ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/manage/phpmyadminlite/libraries/StorageEngine.class.php

https://gitlab.com/albert925/lading-ach
PHP | 424 lines | 208 code | 34 blank | 182 comment | 29 complexity | 6af465282428499edae1e35ea9d84919 MD5 | raw file
  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. * @version $Id$
  7. * @package phpMyAdmin
  8. */
  9. /**
  10. * defines
  11. */
  12. define('PMA_ENGINE_SUPPORT_NO', 0);
  13. define('PMA_ENGINE_SUPPORT_DISABLED', 1);
  14. define('PMA_ENGINE_SUPPORT_YES', 2);
  15. define('PMA_ENGINE_SUPPORT_DEFAULT', 3);
  16. define('PMA_ENGINE_DETAILS_TYPE_PLAINTEXT', 0);
  17. define('PMA_ENGINE_DETAILS_TYPE_SIZE', 1);
  18. define('PMA_ENGINE_DETAILS_TYPE_NUMERIC', 2); //Has no effect yet...
  19. define('PMA_ENGINE_DETAILS_TYPE_BOOLEAN', 3); // 'ON' or 'OFF'
  20. /**
  21. * base Storage Engine Class
  22. * @package phpMyAdmin
  23. */
  24. class PMA_StorageEngine
  25. {
  26. /**
  27. * @var string engine name
  28. */
  29. var $engine = 'dummy';
  30. /**
  31. * @var string engine title/description
  32. */
  33. var $title = 'PMA Dummy Engine Class';
  34. /**
  35. * @var string engine lang description
  36. */
  37. var $comment = 'If you read this text inside phpMyAdmin, something went wrong...';
  38. /**
  39. * @var integer engine supported by current server
  40. */
  41. var $support = PMA_ENGINE_SUPPORT_NO;
  42. /**
  43. * returns array of storage engines
  44. *
  45. * @static
  46. * @staticvar array $storage_engines storage engines
  47. * @access public
  48. * @uses PMA_DBI_fetch_result()
  49. * @return array of storage engines
  50. */
  51. static public function getStorageEngines()
  52. {
  53. static $storage_engines = null;
  54. if (null !== $storage_engines) {
  55. return $storage_engines;
  56. }
  57. return PMA_DBI_fetch_result('SHOW STORAGE ENGINES', 'Engine');
  58. }
  59. /**
  60. * returns HTML code for storage engine select box
  61. *
  62. * @author rabus
  63. * @static
  64. * @uses PMA_StorageEngine::getStorageEngines()
  65. * @uses strtolower()
  66. * @uses htmlspecialchars()
  67. * @param string $name The name of the select form element
  68. * @param string $id The ID of the form field
  69. * @param string $selected The selected engine
  70. * @param boolean $offerUnavailableEngines
  71. * Should unavailable storage engines be offered?
  72. * @return string html selectbox
  73. */
  74. static public function getHtmlSelect($name = 'engine', $id = null,
  75. $selected = null, $offerUnavailableEngines = false)
  76. {
  77. $selected = strtolower($selected);
  78. $output = '<select name="' . $name . '"'
  79. . (empty($id) ? '' : ' id="' . $id . '"') . '>' . "\n";
  80. foreach (PMA_StorageEngine::getStorageEngines() as $key => $details) {
  81. if (!$offerUnavailableEngines
  82. && ($details['Support'] == 'NO' || $details['Support'] == 'DISABLED')) {
  83. continue;
  84. }
  85. // currently (MySQL 5.1.26) there is no way we can be informed
  86. // that PBMS does not support normal table creation so
  87. // we use an exception here
  88. if ('PBMS' == $details['Engine']) {
  89. continue;
  90. }
  91. $output .= ' <option value="' . htmlspecialchars($key). '"'
  92. . (empty($details['Comment'])
  93. ? '' : ' title="' . htmlspecialchars($details['Comment']) . '"')
  94. . (strtolower($key) == $selected || (empty($selected) && $details['Support'] == 'DEFAULT')
  95. ? ' selected="selected"' : '') . '>' . "\n"
  96. . ' ' . htmlspecialchars($details['Engine']) . "\n"
  97. . ' </option>' . "\n";
  98. }
  99. $output .= '</select>' . "\n";
  100. return $output;
  101. }
  102. /**
  103. * public static final PMA_StorageEngine getEngine()
  104. *
  105. * Loads the corresponding engine plugin, if available.
  106. *
  107. * @uses str_replace()
  108. * @uses file_exists()
  109. * @uses PMA_StorageEngine
  110. * @param string $engine The engine ID
  111. * @return object The engine plugin
  112. */
  113. static public function getEngine($engine)
  114. {
  115. $engine = str_replace('/', '', str_replace('.', '', $engine));
  116. $engine_lowercase_filename = strtolower($engine);
  117. if (file_exists('./libraries/engines/' . $engine_lowercase_filename . '.lib.php')
  118. && include_once './libraries/engines/' . $engine_lowercase_filename . '.lib.php') {
  119. $class_name = 'PMA_StorageEngine_' . $engine;
  120. $engine_object = new $class_name($engine);
  121. } else {
  122. $engine_object = new PMA_StorageEngine($engine);
  123. }
  124. return $engine_object;
  125. }
  126. /**
  127. * return true if given engine name is supported/valid, otherwise false
  128. *
  129. * @static
  130. * @uses PMA_StorageEngine::getStorageEngines()
  131. * @param string $engine name of engine
  132. * @return boolean whether $engine is valid or not
  133. */
  134. static public function isValid($engine)
  135. {
  136. $storage_engines = PMA_StorageEngine::getStorageEngines();
  137. return isset($storage_engines[$engine]);
  138. }
  139. /**
  140. * returns as HTML table of the engine's server variables
  141. *
  142. * @uses PMA_ENGINE_DETAILS_TYPE_SIZE
  143. * @uses PMA_ENGINE_DETAILS_TYPE_NUMERIC
  144. * @uses PMA_StorageEngine::getVariablesStatus()
  145. * @uses $GLOBALS['strNoDetailsForEngine']
  146. * @uses PMA_showHint()
  147. * @uses PMA_formatByteDown()
  148. * @uses PMA_formatNumber()
  149. * @uses htmlspecialchars()
  150. * @return string The table that was generated based on the retrieved information
  151. */
  152. function getHtmlVariables()
  153. {
  154. $odd_row = false;
  155. $ret = '';
  156. foreach ($this->getVariablesStatus() as $details) {
  157. $ret .= '<tr class="' . ($odd_row ? 'odd' : 'even') . '">' . "\n"
  158. . ' <td>' . "\n";
  159. if (!empty($details['desc'])) {
  160. $ret .= ' ' . PMA_showHint($details['desc']) . "\n";
  161. }
  162. $ret .= ' </td>' . "\n"
  163. . ' <th>' . htmlspecialchars($details['title']) . '</th>' . "\n"
  164. . ' <td class="value">';
  165. switch ($details['type']) {
  166. case PMA_ENGINE_DETAILS_TYPE_SIZE:
  167. $parsed_size = PMA_formatByteDown($details['value']);
  168. $ret .= $parsed_size[0] . '&nbsp;' . $parsed_size[1];
  169. unset($parsed_size);
  170. break;
  171. case PMA_ENGINE_DETAILS_TYPE_NUMERIC:
  172. $ret .= PMA_formatNumber($details['value']) . ' ';
  173. break;
  174. default:
  175. $ret .= htmlspecialchars($details['value']) . ' ';
  176. }
  177. $ret .= '</td>' . "\n"
  178. . '</tr>' . "\n";
  179. $odd_row = !$odd_row;
  180. }
  181. if (! $ret) {
  182. $ret = '<p>' . "\n"
  183. . ' ' . $GLOBALS['strNoDetailsForEngine'] . "\n"
  184. . '</p>' . "\n";
  185. } else {
  186. $ret = '<table class="data">' . "\n" . $ret . '</table>' . "\n";
  187. }
  188. return $ret;
  189. }
  190. /**
  191. * returns array with detailed info about engine specific server variables
  192. *
  193. * @uses PMA_ENGINE_DETAILS_TYPE_PLAINTEXT
  194. * @uses PMA_StorageEngine::getVariables()
  195. * @uses PMA_StorageEngine::getVariablesLikePattern()
  196. * @uses PMA_DBI_query()
  197. * @uses PMA_DBI_fetch_assoc()
  198. * @uses PMA_DBI_free_result()
  199. * @return array with detailed info about specific engine server variables
  200. */
  201. function getVariablesStatus()
  202. {
  203. $variables = $this->getVariables();
  204. $like = $this->getVariablesLikePattern();
  205. if ($like) {
  206. $like = " LIKE '" . $like . "' ";
  207. } else {
  208. $like = '';
  209. }
  210. $mysql_vars = array();
  211. $sql_query = 'SHOW GLOBAL VARIABLES ' . $like . ';';
  212. $res = PMA_DBI_query($sql_query);
  213. while ($row = PMA_DBI_fetch_assoc($res)) {
  214. if (isset($variables[$row['Variable_name']])) {
  215. $mysql_vars[$row['Variable_name']] = $variables[$row['Variable_name']];
  216. } elseif (! $like
  217. && strpos(strtolower($row['Variable_name']), strtolower($this->engine)) !== 0) {
  218. continue;
  219. }
  220. $mysql_vars[$row['Variable_name']]['value'] = $row['Value'];
  221. if (empty($mysql_vars[$row['Variable_name']]['title'])) {
  222. $mysql_vars[$row['Variable_name']]['title'] = $row['Variable_name'];
  223. }
  224. if (! isset($mysql_vars[$row['Variable_name']]['type'])) {
  225. $mysql_vars[$row['Variable_name']]['type'] = PMA_ENGINE_DETAILS_TYPE_PLAINTEXT;
  226. }
  227. }
  228. PMA_DBI_free_result($res);
  229. return $mysql_vars;
  230. }
  231. /**
  232. * Constructor
  233. *
  234. * @uses PMA_StorageEngine::getStorageEngines()
  235. * @uses PMA_ENGINE_SUPPORT_DEFAULT
  236. * @uses PMA_ENGINE_SUPPORT_YES
  237. * @uses PMA_ENGINE_SUPPORT_DISABLED
  238. * @uses PMA_ENGINE_SUPPORT_NO
  239. * @uses $this->engine
  240. * @uses $this->title
  241. * @uses $this->comment
  242. * @uses $this->support
  243. * @param string $engine The engine ID
  244. */
  245. function __construct($engine)
  246. {
  247. $storage_engines = PMA_StorageEngine::getStorageEngines();
  248. if (!empty($storage_engines[$engine])) {
  249. $this->engine = $engine;
  250. $this->title = $storage_engines[$engine]['Engine'];
  251. $this->comment =
  252. (isset($storage_engines[$engine]['Comment'])
  253. ? $storage_engines[$engine]['Comment']
  254. : '');
  255. switch ($storage_engines[$engine]['Support']) {
  256. case 'DEFAULT':
  257. $this->support = PMA_ENGINE_SUPPORT_DEFAULT;
  258. break;
  259. case 'YES':
  260. $this->support = PMA_ENGINE_SUPPORT_YES;
  261. break;
  262. case 'DISABLED':
  263. $this->support = PMA_ENGINE_SUPPORT_DISABLED;
  264. break;
  265. case 'NO':
  266. default:
  267. $this->support = PMA_ENGINE_SUPPORT_NO;
  268. }
  269. }
  270. }
  271. /**
  272. * public String getTitle()
  273. *
  274. * Reveals the engine's title
  275. * @uses $this->title
  276. * @return string The title
  277. */
  278. function getTitle()
  279. {
  280. return $this->title;
  281. }
  282. /**
  283. * public String getComment()
  284. *
  285. * Fetches the server's comment about this engine
  286. * @uses $this->comment
  287. * @return string The comment
  288. */
  289. function getComment()
  290. {
  291. return $this->comment;
  292. }
  293. /**
  294. * public String getSupportInformationMessage()
  295. *
  296. * @uses $GLOBALS['strDefaultEngine']
  297. * @uses $GLOBALS['strEngineAvailable']
  298. * @uses $GLOBALS['strEngineDisabled']
  299. * @uses $GLOBALS['strEngineUnsupported']
  300. * @uses $GLOBALS['strEngineUnsupported']
  301. * @uses PMA_ENGINE_SUPPORT_DEFAULT
  302. * @uses PMA_ENGINE_SUPPORT_YES
  303. * @uses PMA_ENGINE_SUPPORT_DISABLED
  304. * @uses PMA_ENGINE_SUPPORT_NO
  305. * @uses $this->support
  306. * @uses $this->title
  307. * @uses sprintf
  308. * @return string The localized message.
  309. */
  310. function getSupportInformationMessage()
  311. {
  312. switch ($this->support) {
  313. case PMA_ENGINE_SUPPORT_DEFAULT:
  314. $message = $GLOBALS['strDefaultEngine'];
  315. break;
  316. case PMA_ENGINE_SUPPORT_YES:
  317. $message = $GLOBALS['strEngineAvailable'];
  318. break;
  319. case PMA_ENGINE_SUPPORT_DISABLED:
  320. $message = $GLOBALS['strEngineDisabled'];
  321. break;
  322. case PMA_ENGINE_SUPPORT_NO:
  323. default:
  324. $message = $GLOBALS['strEngineUnsupported'];
  325. }
  326. return sprintf($message, htmlspecialchars($this->title));
  327. }
  328. /**
  329. * public string[][] getVariables()
  330. *
  331. * Generates a list of MySQL variables that provide information about this
  332. * engine. This function should be overridden when extending this class
  333. * for a particular engine.
  334. *
  335. * @abstract
  336. * @return Array The list of variables.
  337. */
  338. function getVariables()
  339. {
  340. return array();
  341. }
  342. /**
  343. * returns string with filename for the MySQL helppage
  344. * about this storage engne
  345. *
  346. * @return string mysql helppage filename
  347. */
  348. function getMysqlHelpPage()
  349. {
  350. return $this->engine . '-storage-engine';
  351. }
  352. /**
  353. * public string getVariablesLikePattern()
  354. *
  355. * @abstract
  356. * @return string SQL query LIKE pattern
  357. */
  358. function getVariablesLikePattern()
  359. {
  360. return false;
  361. }
  362. /**
  363. * public String[] getInfoPages()
  364. *
  365. * Returns a list of available information pages with labels
  366. *
  367. * @abstract
  368. * @return array The list
  369. */
  370. function getInfoPages()
  371. {
  372. return array();
  373. }
  374. /**
  375. * public String getPage()
  376. *
  377. * Generates the requested information page
  378. *
  379. * @abstract
  380. * @param string $id The page ID
  381. *
  382. * @return string The page
  383. * boolean or false on error.
  384. */
  385. function getPage($id)
  386. {
  387. return false;
  388. }
  389. }
  390. ?>