2.11.11.3 release
[phpmyadmin/arisferyanto.git] / libraries / StorageEngine.class.php
blob012b7fd14596d997c17abf0c97ec9afc6eaaca38
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Library for extracting information about the available storage engines
6 * @version $Id$
7 */
9 /**
10 * defines
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);
17 define('PMA_ENGINE_DETAILS_TYPE_PLAINTEXT', 0);
18 define('PMA_ENGINE_DETAILS_TYPE_SIZE', 1);
19 define('PMA_ENGINE_DETAILS_TYPE_NUMERIC', 2); //Has no effect yet...
20 define('PMA_ENGINE_DETAILS_TYPE_BOOLEAN', 3); // 'ON' or 'OFF'
22 /**
23 * base Storage Engine Class
25 class PMA_StorageEngine
27 /**
28 * @var string engine name
30 var $engine = 'dummy';
32 /**
33 * @var string engine title/description
35 var $title = 'PMA Dummy Engine Class';
37 /**
38 * @var string engine lang description
40 var $comment = 'If you read this text inside phpMyAdmin, something went wrong...';
42 /**
43 * @var integer engine supported by current server
45 var $support = PMA_ENGINE_SUPPORT_NO;
47 /**
48 * returns array of storage engines
50 * @static
51 * @staticvar array $storage_engines storage engines
52 * @access public
53 * @uses PMA_MYSQL_INT_VERSION
54 * @uses PMA_StorageEngine::getStorageEnginesBefore40102()
55 * @uses PMA_DBI_fetch_result()
56 * @return array of storage engines
58 function getStorageEngines()
60 static $storage_engines = null;
62 if (null !== $storage_engines) {
63 return $storage_engines;
66 $storage_engines = array();
68 // SHOW STORAGE ENGINES comes with MySQL 4.1.2
69 if (PMA_MYSQL_INT_VERSION < 40102) {
70 $storage_engines = PMA_StorageEngine::getStorageEnginesBefore40102();
71 } else {
72 $storage_engines = PMA_DBI_fetch_result('SHOW STORAGE ENGINES', 'Engine');
75 return $storage_engines;
78 /**
79 * returns HTML code for storage engine select box
81 * @author rabus
82 * @static
83 * @uses PMA_StorageEngine::getStorageEngines()
84 * @uses strtolower()
85 * @uses htmlspecialchars()
86 * @param string $name The name of the select form element
87 * @param string $id The ID of the form field
88 * @param string $selected The selected engine
89 * @param boolean $offerUnavailableEngines
90 * Should unavailable storage engines be offered?
91 * @return string html selectbox
93 function getHtmlSelect($name = 'engine', $id = null,
94 $selected = null, $offerUnavailableEngines = false)
96 $selected = strtolower($selected);
97 $output = '<select name="' . $name . '"'
98 . (empty($id) ? '' : ' id="' . $id . '"') . '>' . "\n";
100 foreach (PMA_StorageEngine::getStorageEngines() as $key => $details) {
101 if (!$offerUnavailableEngines
102 && ($details['Support'] == 'NO' || $details['Support'] == 'DISABLED')) {
103 continue;
105 $output .= ' <option value="' . htmlspecialchars($key). '"'
106 . (empty($details['Comment'])
107 ? '' : ' title="' . htmlspecialchars($details['Comment']) . '"')
108 . (strtolower($key) == $selected || (empty($selected) && $details['Support'] == 'DEFAULT')
109 ? ' selected="selected"' : '') . '>' . "\n"
110 . ' ' . htmlspecialchars($details['Engine']) . "\n"
111 . ' </option>' . "\n";
113 $output .= '</select>' . "\n";
114 return $output;
118 * returns array of storage engines for MySQL < 4.1, hard coded
119 * Emulating SHOW STORAGE ENGINES...
121 * @static
122 * @access public
123 * @uses PMA_DBI_query()
124 * @uses PMA_DBI_fetch_row()
125 * @uses PMA_DBI_free_result()
126 * @uses substr()
127 * @return array of storage engines
129 function getStorageEnginesBefore40102()
131 $storage_engines = array(
132 'myisam' => array(
133 'Engine' => 'MyISAM',
134 'Support' => 'DEFAULT'
136 'merge' => array(
137 'Engine' => 'MERGE',
138 'Support' => 'YES'
140 'heap' => array(
141 'Engine' => 'HEAP',
142 'Support' => 'YES'
144 'memory' => array(
145 'Engine' => 'MEMORY',
146 'Support' => 'YES'
149 $known_engines = array(
150 'archive' => 'ARCHIVE',
151 'bdb' => 'BDB',
152 'csv' => 'CSV',
153 'innodb' => 'InnoDB',
154 'isam' => 'ISAM',
155 'gemini' => 'Gemini'
157 $res = PMA_DBI_query('SHOW VARIABLES LIKE \'have\\_%\';');
158 while ($row = PMA_DBI_fetch_row($res)) {
159 $current = substr($row[0], 5);
160 if (! empty($known_engines[$current])) {
161 $storage_engines[$current] = array(
162 'Engine' => $known_engines[$current],
163 'Support' => $row[1]
167 PMA_DBI_free_result($res);
169 return $storage_engines;
174 * public static final PMA_StorageEngine getEngine()
176 * Loads the corresponding engine plugin, if available.
178 * @uses str_replace()
179 * @uses file_exists()
180 * @uses PMA_StorageEngine
181 * @param string $engine The engine ID
182 * @return object The engine plugin
184 function getEngine($engine)
186 $engine = str_replace('/', '', str_replace('.', '', $engine));
187 $engine_lowercase_filename = strtolower($engine);
188 if (file_exists('./libraries/engines/' . $engine_lowercase_filename . '.lib.php')
189 && include_once './libraries/engines/' . $engine_lowercase_filename . '.lib.php') {
190 $class_name = 'PMA_StorageEngine_' . $engine;
191 $engine_object = new $class_name($engine);
192 } else {
193 $engine_object = new PMA_StorageEngine($engine);
195 return $engine_object;
199 * return true if given engine name is supported/valid, otherwise false
201 * @static
202 * @uses PMA_StorageEngine::getStorageEngines()
203 * @param string $engine name of engine
204 * @reutrn boolean whether $engine is valid or not
206 function isValid($engine)
208 $storage_engines = PMA_StorageEngine::getStorageEngines();
209 return isset($storage_engines[$engine]);
213 * returns as HTML table of the engine's server variables
215 * @uses PMA_ENGINE_DETAILS_TYPE_SIZE
216 * @uses PMA_ENGINE_DETAILS_TYPE_NUMERIC
217 * @uses PMA_StorageEngine::getVariablesStatus()
218 * @uses $GLOBALS['strNoDetailsForEngine']
219 * @uses PMA_showHint()
220 * @uses PMA_formatByteDown()
221 * @uses PMA_formatNumber()
222 * @uses htmlspecialchars()
223 * @return string The table that was generated based on the retrieved information
225 function getHtmlVariables()
227 $odd_row = false;
228 $ret = '';
230 foreach ($this->getVariablesStatus() as $details) {
231 $ret .= '<tr class="' . ($odd_row ? 'odd' : 'even') . '">' . "\n"
232 . ' <td>' . "\n";
233 if (!empty($details['desc'])) {
234 $ret .= ' ' . PMA_showHint($details['desc']) . "\n";
236 $ret .= ' </td>' . "\n"
237 . ' <th>' . htmlspecialchars($details['title']) . '</th>' . "\n"
238 . ' <td class="value">';
239 switch ($details['type']) {
240 case PMA_ENGINE_DETAILS_TYPE_SIZE:
241 $parsed_size = PMA_formatByteDown($details['value']);
242 $ret .= $parsed_size[0] . '&nbsp;' . $parsed_size[1];
243 unset($parsed_size);
244 break;
245 case PMA_ENGINE_DETAILS_TYPE_NUMERIC:
246 $ret .= PMA_formatNumber($details['value']) . ' ';
247 break;
248 default:
249 $ret .= htmlspecialchars($details['value']) . ' ';
251 $ret .= '</td>' . "\n"
252 . '</tr>' . "\n";
253 $odd_row = !$odd_row;
256 if (! $ret) {
257 $ret = '<p>' . "\n"
258 . ' ' . $GLOBALS['strNoDetailsForEngine'] . "\n"
259 . '</p>' . "\n";
260 } else {
261 $ret = '<table class="data">' . "\n" . $ret . '</table>' . "\n";
264 return $ret;
268 * returns array with detailed info about engine specific server variables
270 * @uses PMA_ENGINE_DETAILS_TYPE_PLAINTEXT
271 * @uses PMA_StorageEngine::getVariables()
272 * @uses PMA_StorageEngine::getVariablesLikePattern()
273 * @uses PMA_MYSQL_INT_VERSION
274 * @uses PMA_DBI_query()
275 * @uses PMA_DBI_fetch_assoc()
276 * @uses PMA_DBI_free_result()
277 * @return array with detailed info about specific engine server variables
279 function getVariablesStatus()
281 $variables = $this->getVariables();
282 $like = $this->getVariablesLikePattern();
284 if ($like) {
285 $like = " LIKE '" . $like . "' ";
286 } else {
287 $like = '';
290 if (PMA_MYSQL_INT_VERSION >= 40102) {
291 $global = ' GLOBAL ';
292 } else {
293 $global = '';
296 $mysql_vars = array();
298 $sql_query = 'SHOW ' . $global . ' VARIABLES ' . $like . ';';
299 $res = PMA_DBI_query($sql_query);
300 while ($row = PMA_DBI_fetch_assoc($res)) {
301 if (isset($variables[$row['Variable_name']])) {
302 $mysql_vars[$row['Variable_name']] = $variables[$row['Variable_name']];
303 } elseif (! $like
304 && strpos(strtolower($row['Variable_name']), strtolower($this->engine)) !== 0) {
305 continue;
307 $mysql_vars[$row['Variable_name']]['value'] = $row['Value'];
309 if (empty($mysql_vars[$row['Variable_name']]['title'])) {
310 $mysql_vars[$row['Variable_name']]['title'] = $row['Variable_name'];
313 if (! isset($mysql_vars[$row['Variable_name']]['type'])) {
314 $mysql_vars[$row['Variable_name']]['type'] = PMA_ENGINE_DETAILS_TYPE_PLAINTEXT;
317 PMA_DBI_free_result($res);
319 return $mysql_vars;
323 * Constructor
325 * @uses PMA_StorageEngine::getStorageEngines()
326 * @uses PMA_ENGINE_SUPPORT_DEFAULT
327 * @uses PMA_ENGINE_SUPPORT_YES
328 * @uses PMA_ENGINE_SUPPORT_DISABLED
329 * @uses PMA_ENGINE_SUPPORT_NO
330 * @uses $this->engine
331 * @uses $this->title
332 * @uses $this->comment
333 * @uses $this->support
334 * @param string $engine The engine ID
336 function __construct($engine)
338 $storage_engines = PMA_StorageEngine::getStorageEngines();
339 if (!empty($storage_engines[$engine])) {
340 $this->engine = $engine;
341 $this->title = $storage_engines[$engine]['Engine'];
342 $this->comment =
343 (isset($storage_engines[$engine]['Comment'])
344 ? $storage_engines[$engine]['Comment']
345 : '');
346 switch ($storage_engines[$engine]['Support']) {
347 case 'DEFAULT':
348 $this->support = PMA_ENGINE_SUPPORT_DEFAULT;
349 break;
350 case 'YES':
351 $this->support = PMA_ENGINE_SUPPORT_YES;
352 break;
353 case 'DISABLED':
354 $this->support = PMA_ENGINE_SUPPORT_DISABLED;
355 break;
356 case 'NO':
357 default:
358 $this->support = PMA_ENGINE_SUPPORT_NO;
364 * old PHP 4 style constructor
365 * @deprecated
366 * @see PMA_StorageEngine::__construct()
367 * @uses PMA_StorageEngine::__construct()
368 * @param string $engine engine name
370 function PMA_StorageEngine($engine)
372 $this->__construct($engine);
376 * public String getTitle()
378 * Reveals the engine's title
379 * @uses $this->title
380 * @return string The title
382 function getTitle()
384 return $this->title;
388 * public String getComment()
390 * Fetches the server's comment about this engine
391 * @uses $this->comment
392 * @return string The comment
394 function getComment()
396 return $this->comment;
400 * public String getSupportInformationMessage()
402 * @uses $GLOBALS['strDefaultEngine']
403 * @uses $GLOBALS['strEngineAvailable']
404 * @uses $GLOBALS['strEngineDisabled']
405 * @uses $GLOBALS['strEngineUnsupported']
406 * @uses $GLOBALS['strEngineUnsupported']
407 * @uses PMA_ENGINE_SUPPORT_DEFAULT
408 * @uses PMA_ENGINE_SUPPORT_YES
409 * @uses PMA_ENGINE_SUPPORT_DISABLED
410 * @uses PMA_ENGINE_SUPPORT_NO
411 * @uses $this->support
412 * @uses $this->title
413 * @uses sprintf
414 * @return string The localized message.
416 function getSupportInformationMessage()
418 switch ($this->support) {
419 case PMA_ENGINE_SUPPORT_DEFAULT:
420 $message = $GLOBALS['strDefaultEngine'];
421 break;
422 case PMA_ENGINE_SUPPORT_YES:
423 $message = $GLOBALS['strEngineAvailable'];
424 break;
425 case PMA_ENGINE_SUPPORT_DISABLED:
426 $message = $GLOBALS['strEngineDisabled'];
427 break;
428 case PMA_ENGINE_SUPPORT_NO:
429 default:
430 $message = $GLOBALS['strEngineUnsupported'];
432 return sprintf($message, htmlspecialchars($this->title));
436 * public string[][] getVariables()
438 * Generates a list of MySQL variables that provide information about this
439 * engine. This function should be overridden when extending this class
440 * for a particular engine.
442 * @abstract
443 * @return Array The list of variables.
445 function getVariables()
447 return array();
451 * returns string with filename for the MySQL helppage
452 * about this storage engne
454 * @return string mysql helppage filename
456 function getMysqlHelpPage()
458 return $this->engine . '-storage-engine';
462 * public string getVariablesLikePattern()
464 * @abstract
465 * @return string SQL query LIKE pattern
467 function getVariablesLikePattern()
469 return false;
473 * public String[] getInfoPages()
475 * Returns a list of available information pages with labels
477 * @abstract
478 * @return array The list
480 function getInfoPages()
482 return array();
486 * public String getPage()
488 * Generates the requested information page
490 * @abstract
491 * @param string $id The page ID
493 * @return string The page
494 * boolean or false on error.
496 function getPage($id)
498 return false;