Translated using Weblate (Chinese (Traditional))
[phpmyadmin.git] / libraries / StorageEngine.php
blob07c7dcc11cd9216e7df9926d437894b745ffec0a
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Library for extracting information about the available storage engines
6 * @package PhpMyAdmin
7 */
8 namespace PMA\libraries;
10 /**
11 * defines
13 use PMA\libraries\engines\Bdb;
14 use PMA\libraries\engines\Berkeleydb;
15 use PMA\libraries\engines\Binlog;
16 use PMA\libraries\engines\Innobase;
17 use PMA\libraries\engines\Innodb;
18 use PMA\libraries\engines\Memory;
19 use PMA\libraries\engines\Merge;
20 use PMA\libraries\engines\Mrg_Myisam;
21 use PMA\libraries\engines\Myisam;
22 use PMA\libraries\engines\Ndbcluster;
23 use PMA\libraries\engines\Pbxt;
24 use PMA\libraries\engines\Performance_Schema;
26 define('PMA_ENGINE_SUPPORT_NO', 0);
27 define('PMA_ENGINE_SUPPORT_DISABLED', 1);
28 define('PMA_ENGINE_SUPPORT_YES', 2);
29 define('PMA_ENGINE_SUPPORT_DEFAULT', 3);
31 define('PMA_ENGINE_DETAILS_TYPE_PLAINTEXT', 0);
32 define('PMA_ENGINE_DETAILS_TYPE_SIZE', 1);
33 define('PMA_ENGINE_DETAILS_TYPE_NUMERIC', 2); //Has no effect yet...
34 define('PMA_ENGINE_DETAILS_TYPE_BOOLEAN', 3); // 'ON' or 'OFF'
36 /**
37 * Base Storage Engine Class
39 * @package PhpMyAdmin
41 class StorageEngine
43 /**
44 * @var string engine name
46 var $engine = 'dummy';
48 /**
49 * @var string engine title/description
51 var $title = 'PMA Dummy Engine Class';
53 /**
54 * @var string engine lang description
56 var $comment
57 = 'If you read this text inside phpMyAdmin, something went wrong...';
59 /**
60 * @var integer engine supported by current server
62 var $support = PMA_ENGINE_SUPPORT_NO;
64 /**
65 * Constructor
67 * @param string $engine The engine ID
69 public function __construct($engine)
71 $storage_engines = StorageEngine::getStorageEngines();
72 if (! empty($storage_engines[$engine])) {
73 $this->engine = $engine;
74 $this->title = $storage_engines[$engine]['Engine'];
75 $this->comment = (isset($storage_engines[$engine]['Comment'])
76 ? $storage_engines[$engine]['Comment']
77 : '');
78 switch ($storage_engines[$engine]['Support']) {
79 case 'DEFAULT':
80 $this->support = PMA_ENGINE_SUPPORT_DEFAULT;
81 break;
82 case 'YES':
83 $this->support = PMA_ENGINE_SUPPORT_YES;
84 break;
85 case 'DISABLED':
86 $this->support = PMA_ENGINE_SUPPORT_DISABLED;
87 break;
88 case 'NO':
89 default:
90 $this->support = PMA_ENGINE_SUPPORT_NO;
95 /**
96 * Returns array of storage engines
98 * @static
99 * @staticvar array $storage_engines storage engines
100 * @access public
101 * @return string[] array of storage engines
103 static public function getStorageEngines()
105 static $storage_engines = null;
107 if (null == $storage_engines) {
108 $storage_engines
109 = $GLOBALS['dbi']->fetchResult('SHOW STORAGE ENGINES', 'Engine');
110 if (PMA_MYSQL_INT_VERSION >= 50708) {
111 $disabled = Util::cacheGet(
112 'disabled_storage_engines',
113 function () {
114 return $GLOBALS['dbi']->fetchValue(
115 'SELECT @@disabled_storage_engines'
119 foreach (explode(",", $disabled) as $engine) {
120 if (isset($storage_engines[$engine])) {
121 $storage_engines[$engine]['Support'] = 'DISABLED';
127 return $storage_engines;
131 * Returns HTML code for storage engine select box
133 * @param string $name The name of the select form element
134 * @param string $id The ID of the form field
135 * @param string $selected The selected engine
136 * @param boolean $offerUnavailableEngines Should unavailable storage
137 * engines be offered?
138 * @param boolean $addEmpty Whether to provide empty option
140 * @static
141 * @return string html selectbox
143 static public function getHtmlSelect(
144 $name = 'engine', $id = null,
145 $selected = null, $offerUnavailableEngines = false,
146 $addEmpty = false
148 $selected = mb_strtolower($selected);
149 $output = '<select name="' . $name . '"'
150 . (empty($id) ? '' : ' id="' . $id . '"') . '>' . "\n";
152 if ($addEmpty) {
153 $output .= '<option value=""></option>';
156 foreach (StorageEngine::getStorageEngines() as $key => $details) {
157 // Don't show PERFORMANCE_SCHEMA engine (MySQL 5.5)
158 if (! $offerUnavailableEngines
159 && ($details['Support'] == 'NO'
160 || $details['Support'] == 'DISABLED'
161 || $details['Engine'] == 'PERFORMANCE_SCHEMA')
163 continue;
166 $output .= ' <option value="' . htmlspecialchars($key) . '"'
167 . (empty($details['Comment'])
168 ? '' : ' title="' . htmlspecialchars($details['Comment']) . '"')
169 . (mb_strtolower($key) == $selected
170 || (empty($selected) && $details['Support'] == 'DEFAULT' && ! $addEmpty)
171 ? ' selected="selected"' : '')
172 . '>' . "\n"
173 . ' ' . htmlspecialchars($details['Engine']) . "\n"
174 . ' </option>' . "\n";
176 $output .= '</select>' . "\n";
177 return $output;
181 * Loads the corresponding engine plugin, if available.
183 * @param string $engine The engine ID
185 * @return StorageEngine The engine plugin
186 * @static
188 static public function getEngine($engine)
190 switch(strtolower($engine)) {
191 case 'bdb':
192 return new Bdb($engine);
193 case 'berkeleydb':
194 return new Berkeleydb($engine);
195 case 'binlog':
196 return new Binlog($engine);
197 case 'innobase':
198 return new Innobase($engine);
199 case 'innodb':
200 return new Innodb($engine);
201 case 'memory':
202 return new Memory($engine);
203 case 'merge':
204 return new Merge($engine);
205 case 'mrg_myisam':
206 return new Mrg_Myisam($engine);
207 case 'myisam':
208 return new Myisam($engine);
209 case 'ndbcluster':
210 return new Ndbcluster($engine);
211 case 'pbxt':
212 return new Pbxt($engine);
213 case 'performance_schema':
214 return new Performance_Schema($engine);
215 default:
216 return new StorageEngine($engine);
221 * Returns true if given engine name is supported/valid, otherwise false
223 * @param string $engine name of engine
225 * @static
226 * @return boolean whether $engine is valid or not
228 static public function isValid($engine)
230 if ($engine == "PBMS") {
231 return true;
233 $storage_engines = StorageEngine::getStorageEngines();
234 return isset($storage_engines[$engine]);
238 * Returns as HTML table of the engine's server variables
240 * @return string The table that was generated based on the retrieved
241 * information
243 public function getHtmlVariables()
245 $ret = '';
247 foreach ($this->getVariablesStatus() as $details) {
248 $ret .= '<tr>' . "\n"
249 . ' <td>' . "\n";
250 if (! empty($details['desc'])) {
251 $ret .= ' '
252 . Util::showHint($details['desc'])
253 . "\n";
255 $ret .= ' </td>' . "\n"
256 . ' <th>' . htmlspecialchars($details['title']) . '</th>'
257 . "\n"
258 . ' <td class="value">';
259 switch ($details['type']) {
260 case PMA_ENGINE_DETAILS_TYPE_SIZE:
261 $parsed_size = $this->resolveTypeSize($details['value']);
262 $ret .= $parsed_size[0] . '&nbsp;' . $parsed_size[1];
263 unset($parsed_size);
264 break;
265 case PMA_ENGINE_DETAILS_TYPE_NUMERIC:
266 $ret .= Util::formatNumber($details['value']) . ' ';
267 break;
268 default:
269 $ret .= htmlspecialchars($details['value']) . ' ';
271 $ret .= '</td>' . "\n"
272 . '</tr>' . "\n";
275 if (! $ret) {
276 $ret = '<p>' . "\n"
277 . ' '
278 . __(
279 'There is no detailed status information available for this '
280 . 'storage engine.'
282 . "\n"
283 . '</p>' . "\n";
284 } else {
285 $ret = '<table class="data">' . "\n" . $ret . '</table>' . "\n";
288 return $ret;
292 * Returns the engine specific handling for
293 * PMA_ENGINE_DETAILS_TYPE_SIZE type variables.
295 * This function should be overridden when
296 * PMA_ENGINE_DETAILS_TYPE_SIZE type needs to be
297 * handled differently for a particular engine.
299 * @param integer $value Value to format
301 * @return string the formatted value and its unit
303 public function resolveTypeSize($value)
305 return Util::formatByteDown($value);
309 * Returns array with detailed info about engine specific server variables
311 * @return array array with detailed info about specific engine server variables
313 public function getVariablesStatus()
315 $variables = $this->getVariables();
316 $like = $this->getVariablesLikePattern();
318 if ($like) {
319 $like = " LIKE '" . $like . "' ";
320 } else {
321 $like = '';
324 $mysql_vars = array();
326 $sql_query = 'SHOW GLOBAL VARIABLES ' . $like . ';';
327 $res = $GLOBALS['dbi']->query($sql_query);
328 while ($row = $GLOBALS['dbi']->fetchAssoc($res)) {
329 if (isset($variables[$row['Variable_name']])) {
330 $mysql_vars[$row['Variable_name']]
331 = $variables[$row['Variable_name']];
332 } elseif (! $like
333 && mb_strpos(mb_strtolower($row['Variable_name']), mb_strtolower($this->engine)) !== 0
335 continue;
337 $mysql_vars[$row['Variable_name']]['value'] = $row['Value'];
339 if (empty($mysql_vars[$row['Variable_name']]['title'])) {
340 $mysql_vars[$row['Variable_name']]['title'] = $row['Variable_name'];
343 if (! isset($mysql_vars[$row['Variable_name']]['type'])) {
344 $mysql_vars[$row['Variable_name']]['type']
345 = PMA_ENGINE_DETAILS_TYPE_PLAINTEXT;
348 $GLOBALS['dbi']->freeResult($res);
350 return $mysql_vars;
354 * Reveals the engine's title
356 * @return string The title
358 public function getTitle()
360 return $this->title;
364 * Fetches the server's comment about this engine
366 * @return string The comment
368 public function getComment()
370 return $this->comment;
374 * Information message on whether this storage engine is supported
376 * @return string The localized message.
378 public function getSupportInformationMessage()
380 switch ($this->support) {
381 case PMA_ENGINE_SUPPORT_DEFAULT:
382 $message = __('%s is the default storage engine on this MySQL server.');
383 break;
384 case PMA_ENGINE_SUPPORT_YES:
385 $message = __('%s is available on this MySQL server.');
386 break;
387 case PMA_ENGINE_SUPPORT_DISABLED:
388 $message = __('%s has been disabled for this MySQL server.');
389 break;
390 case PMA_ENGINE_SUPPORT_NO:
391 default:
392 $message = __(
393 'This MySQL server does not support the %s storage engine.'
396 return sprintf($message, htmlspecialchars($this->title));
400 * Generates a list of MySQL variables that provide information about this
401 * engine. This function should be overridden when extending this class
402 * for a particular engine.
404 * @return array The list of variables.
406 public function getVariables()
408 return array();
412 * Returns string with filename for the MySQL helppage
413 * about this storage engine
415 * @return string MySQL help page filename
417 public function getMysqlHelpPage()
419 return $this->engine . '-storage-engine';
423 * Returns the pattern to be used in the query for SQL variables
424 * related to the storage engine
426 * @return string SQL query LIKE pattern
428 public function getVariablesLikePattern()
430 return '';
434 * Returns a list of available information pages with labels
436 * @return string[] The list
438 public function getInfoPages()
440 return array();
444 * Generates the requested information page
446 * @param string $id page id
448 * @return string html output
450 public function getPage($id)
452 if (! array_key_exists($id, $this->getInfoPages())) {
453 return '';
456 $id = 'getPage' . $id;
458 return $this->$id();