home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / phpMyAdmin / libraries / StorageEngine.class.php < prev    next >
Encoding:
PHP Script  |  2008-06-23  |  15.2 KB  |  503 lines

  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: StorageEngine.class.php 10654 2007-09-20 16:25:32Z lem9 $
  7.  */
  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.  
  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'
  21.  
  22. /**
  23.  * base Storage Engine Class
  24.  */
  25. class PMA_StorageEngine
  26. {
  27.     /**
  28.      * @var string engine name
  29.      */
  30.     var $engine  = 'dummy';
  31.  
  32.     /**
  33.      * @var string engine title/description
  34.      */
  35.     var $title   = 'PMA Dummy Engine Class';
  36.  
  37.     /**
  38.      * @var string engine lang description
  39.      */
  40.     var $comment = 'If you read this text inside phpMyAdmin, something went wrong...';
  41.  
  42.     /**
  43.      * @var integer engine supported by current server
  44.      */
  45.     var $support = PMA_ENGINE_SUPPORT_NO;
  46.  
  47.     /**
  48.      * returns array of storage engines
  49.      *
  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
  57.      */
  58.     function getStorageEngines()
  59.     {
  60.         static $storage_engines = null;
  61.  
  62.         if (null !== $storage_engines) {
  63.             return $storage_engines;
  64.         }
  65.  
  66.         $storage_engines = array();
  67.  
  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');
  73.         }
  74.  
  75.         return $storage_engines;
  76.     }
  77.  
  78.     /**
  79.      * returns HTML code for storage engine select box
  80.      *
  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
  92.      */
  93.     function getHtmlSelect($name = 'engine', $id = null,
  94.       $selected = null, $offerUnavailableEngines = false)
  95.     {
  96.         $selected   = strtolower($selected);
  97.         $output     = '<select name="' . $name . '"'
  98.             . (empty($id) ? '' : ' id="' . $id . '"') . '>' . "\n";
  99.  
  100.         foreach (PMA_StorageEngine::getStorageEngines() as $key => $details) {
  101.             if (!$offerUnavailableEngines
  102.               && ($details['Support'] == 'NO' || $details['Support'] == 'DISABLED')) {
  103.                 continue;
  104.             }
  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";
  112.         }
  113.         $output .= '</select>' . "\n";
  114.         return $output;
  115.     }
  116.  
  117.     /**
  118.      * returns array of storage engines for MySQL < 4.1, hard coded
  119.      * Emulating SHOW STORAGE ENGINES...
  120.      *
  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
  128.      */
  129.     function getStorageEnginesBefore40102()
  130.     {
  131.         $storage_engines = array(
  132.             'myisam' => array(
  133.                 'Engine'  => 'MyISAM',
  134.                 'Support' => 'DEFAULT'
  135.             ),
  136.             'merge' => array(
  137.                 'Engine'  => 'MERGE',
  138.                 'Support' => 'YES'
  139.             ),
  140.             'heap' => array(
  141.                 'Engine'  => 'HEAP',
  142.                 'Support' => 'YES'
  143.             ),
  144.             'memory' => array(
  145.                 'Engine'  => 'MEMORY',
  146.                 'Support' => 'YES'
  147.             )
  148.         );
  149.         $known_engines = array(
  150.             'archive' => 'ARCHIVE',
  151.             'bdb'     => 'BDB',
  152.             'csv'     => 'CSV',
  153.             'innodb'  => 'InnoDB',
  154.             'isam'    => 'ISAM',
  155.             'gemini'  => 'Gemini'
  156.         );
  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]
  164.                 );
  165.             }
  166.         }
  167.         PMA_DBI_free_result($res);
  168.  
  169.         return $storage_engines;
  170.     }
  171.  
  172.  
  173.     /**
  174.      * public static final PMA_StorageEngine getEngine()
  175.      *
  176.      * Loads the corresponding engine plugin, if available.
  177.      *
  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
  183.      */
  184.     function getEngine($engine)
  185.     {
  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);
  194.         }
  195.         return $engine_object;
  196.     }
  197.  
  198.     /**
  199.      * return true if given engine name is supported/valid, otherwise false
  200.      *
  201.      * @static
  202.      * @uses    PMA_StorageEngine::getStorageEngines()
  203.      * @param   string  $engine name of engine
  204.      * @reutrn  boolean whether $engine is valid or not
  205.      */
  206.     function isValid($engine)
  207.     {
  208.         $storage_engines = PMA_StorageEngine::getStorageEngines();
  209.         return isset($storage_engines[$engine]);
  210.     }
  211.  
  212.     /**
  213.      * returns as HTML table of the engine's server variables
  214.      *
  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
  224.      */
  225.     function getHtmlVariables()
  226.     {
  227.         $odd_row    = false;
  228.         $ret        = '';
  229.  
  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";
  235.             }
  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] . ' ' . $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']) . '   ';
  250.             }
  251.             $ret .= '</td>' . "\n"
  252.                   . '</tr>' . "\n";
  253.             $odd_row = !$odd_row;
  254.         }
  255.  
  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";
  262.         }
  263.  
  264.         return $ret;
  265.     }
  266.  
  267.     /**
  268.      * returns array with detailed info about engine specific server variables
  269.      *
  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
  278.      */
  279.     function getVariablesStatus()
  280.     {
  281.         $variables = $this->getVariables();
  282.         $like = $this->getVariablesLikePattern();
  283.  
  284.         if ($like) {
  285.             $like = " LIKE '" . $like . "' ";
  286.         } else {
  287.             $like = '';
  288.         }
  289.  
  290.         if (PMA_MYSQL_INT_VERSION >= 40102) {
  291.             $global = ' GLOBAL ';
  292.         } else {
  293.             $global = '';
  294.         }
  295.  
  296.         $mysql_vars = array();
  297.  
  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;
  306.             }
  307.             $mysql_vars[$row['Variable_name']]['value'] = $row['Value'];
  308.  
  309.             if (empty($mysql_vars[$row['Variable_name']]['title'])) {
  310.                 $mysql_vars[$row['Variable_name']]['title'] = $row['Variable_name'];
  311.             }
  312.  
  313.             if (! isset($mysql_vars[$row['Variable_name']]['type'])) {
  314.                 $mysql_vars[$row['Variable_name']]['type'] = PMA_ENGINE_DETAILS_TYPE_PLAINTEXT;
  315.             }
  316.         }
  317.         PMA_DBI_free_result($res);
  318.  
  319.         return $mysql_vars;
  320.     }
  321.  
  322.     /**
  323.      * Constructor
  324.      *
  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
  335.      */
  336.     function __construct($engine)
  337.     {
  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;
  359.             }
  360.         }
  361.     }
  362.  
  363.     /**
  364.      * old PHP 4 style constructor
  365.      * @deprecated
  366.      * @see     PMA_StorageEngine::__construct()
  367.      * @uses    PMA_StorageEngine::__construct()
  368.      * @param   string  $engine engine name
  369.      */
  370.     function PMA_StorageEngine($engine)
  371.     {
  372.         $this->__construct($engine);
  373.     }
  374.  
  375.     /**
  376.      * public String getTitle()
  377.      *
  378.      * Reveals the engine's title
  379.      * @uses    $this->title
  380.      * @return  string   The title
  381.      */
  382.     function getTitle()
  383.     {
  384.         return $this->title;
  385.     }
  386.  
  387.     /**
  388.      * public String getComment()
  389.      *
  390.      * Fetches the server's comment about this engine
  391.      * @uses    $this->comment
  392.      * @return  string   The comment
  393.      */
  394.     function getComment()
  395.     {
  396.         return $this->comment;
  397.     }
  398.  
  399.     /**
  400.      * public String getSupportInformationMessage()
  401.      *
  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.
  415.      */
  416.     function getSupportInformationMessage()
  417.     {
  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'];
  431.         }
  432.         return sprintf($message, htmlspecialchars($this->title));
  433.     }
  434.  
  435.     /**
  436.      * public string[][] getVariables()
  437.      *
  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.
  441.      *
  442.      * @abstract
  443.      * @return   Array   The list of variables.
  444.      */
  445.     function getVariables()
  446.     {
  447.         return array();
  448.     }
  449.  
  450.     /**
  451.      * returns string with filename for the MySQL helppage
  452.      * about this storage engne
  453.      *
  454.      * @return  string  mysql helppage filename
  455.      */
  456.     function getMysqlHelpPage()
  457.     {
  458.         return $this->engine . '-storage-engine';
  459.     }
  460.  
  461.     /**
  462.      * public string getVariablesLikePattern()
  463.      *
  464.      * @abstract
  465.      * @return  string  SQL query LIKE pattern
  466.      */
  467.     function getVariablesLikePattern()
  468.     {
  469.         return false;
  470.     }
  471.  
  472.     /**
  473.      * public String[] getInfoPages()
  474.      *
  475.      * Returns a list of available information pages with labels
  476.      *
  477.      * @abstract
  478.      * @return  array    The list
  479.      */
  480.     function getInfoPages()
  481.     {
  482.         return array();
  483.     }
  484.  
  485.     /**
  486.      * public String getPage()
  487.      *
  488.      * Generates the requested information page
  489.      *
  490.      * @abstract
  491.      * @param   string  $id The page ID
  492.      *
  493.      * @return  string      The page
  494.      *          boolean     or false on error.
  495.      */
  496.     function getPage($id)
  497.     {
  498.         return false;
  499.     }
  500. }
  501.  
  502. ?>
  503.