[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/phpbb/install/module/install_database/task/ -> create_schema.php (source)

   1  <?php
   2  /**
   3   *
   4   * This file is part of the phpBB Forum Software package.
   5   *
   6   * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7   * @license GNU General Public License, version 2 (GPL-2.0)
   8   *
   9   * For full copyright and license information, please see
  10   * the docs/CREDITS.txt file.
  11   *
  12   */
  13  
  14  namespace phpbb\install\module\install_database\task;
  15  
  16  use phpbb\install\exception\resource_limit_reached_exception;
  17  
  18  /**
  19   * Create database schema
  20   */
  21  class create_schema extends \phpbb\install\task_base
  22  {
  23      /**
  24       * @var \phpbb\install\helper\config
  25       */
  26      protected $config;
  27  
  28      /**
  29       * @var \phpbb\db\driver\driver_interface
  30       */
  31      protected $db;
  32  
  33      /**
  34       * @var \phpbb\db\tools\tools_interface
  35       */
  36      protected $db_tools;
  37  
  38      /**
  39       * @var \phpbb\install\helper\database
  40       */
  41      protected $database_helper;
  42  
  43      /**
  44       * @var \phpbb\filesystem\filesystem_interface
  45       */
  46      protected $filesystem;
  47  
  48      /**
  49       * @var \phpbb\install\helper\iohandler\iohandler_interface
  50       */
  51      protected $iohandler;
  52  
  53      /**
  54       * @var string
  55       */
  56      protected $phpbb_root_path;
  57  
  58      /**
  59       * @var string
  60       */
  61      protected $php_ext;
  62  
  63      /**
  64       * Constructor
  65       *
  66       * @param \phpbb\install\helper\config                            $config                Installer's config provider
  67       * @param \phpbb\install\helper\database                        $db_helper            Installer's database helper
  68       * @param \phpbb\filesystem\filesystem_interface                $filesystem            Filesystem service
  69       * @param \phpbb\install\helper\iohandler\iohandler_interface    $iohandler            Installer's input-output handler
  70       * @param string                                                $phpbb_root_path    Path phpBB's root
  71       * @param string                                                $php_ext            Extension of PHP files
  72       */
  73  	public function __construct(\phpbb\install\helper\config $config,
  74                                  \phpbb\install\helper\database $db_helper,
  75                                  \phpbb\filesystem\filesystem_interface $filesystem,
  76                                  \phpbb\install\helper\iohandler\iohandler_interface $iohandler,
  77                                  $phpbb_root_path,
  78                                  $php_ext)
  79      {
  80          $dbms = $db_helper->get_available_dbms($config->get('dbms'));
  81          $dbms = $dbms[$config->get('dbms')]['DRIVER'];
  82          $factory = new \phpbb\db\tools\factory();
  83  
  84          $this->db                = new $dbms();
  85          $this->db->sql_connect(
  86              $config->get('dbhost'),
  87              $config->get('dbuser'),
  88              $config->get('dbpasswd'),
  89              $config->get('dbname'),
  90              $config->get('dbport'),
  91              false,
  92              false
  93          );
  94  
  95          $this->config            = $config;
  96          $this->db_tools            = $factory->get($this->db);
  97          $this->database_helper    = $db_helper;
  98          $this->filesystem        = $filesystem;
  99          $this->iohandler        = $iohandler;
 100          $this->phpbb_root_path    = $phpbb_root_path;
 101          $this->php_ext            = $php_ext;
 102  
 103          parent::__construct(true);
 104      }
 105  
 106      /**
 107       * {@inheritdoc}
 108       */
 109  	public function run()
 110      {
 111          // As this task may take a large amount of time to complete refreshing the page might be necessary for some
 112          // server configurations with limited resources
 113          if (!$this->config->get('pre_schema_forced_refresh'))
 114          {
 115              if ($this->config->get_time_remaining() < 5)
 116              {
 117                  $this->config->set('pre_schema_forced_refresh', true);
 118                  throw new resource_limit_reached_exception();
 119              }
 120          }
 121  
 122          $this->db->sql_return_on_error(true);
 123  
 124          $dbms = $this->config->get('dbms');
 125          $dbms_info = $this->database_helper->get_available_dbms($dbms);
 126          $schema_name = $dbms_info[$dbms]['SCHEMA'];
 127          $delimiter = $dbms_info[$dbms]['DELIM'];
 128          $table_prefix = $this->config->get('table_prefix');
 129  
 130          if ($dbms === 'mysql')
 131          {
 132              if (version_compare($this->db->sql_server_info(true), '4.1.3', '>='))
 133              {
 134                  $schema_name .= '_41';
 135              }
 136              else
 137              {
 138                  $schema_name .= '_40';
 139              }
 140          }
 141  
 142          $db_schema_path = $this->phpbb_root_path . 'install/schemas/' . $schema_name . '_schema.sql';
 143  
 144          // Load database vendor specific code if there is any
 145          if ($this->filesystem->exists($db_schema_path))
 146          {
 147              $sql_query = @file_get_contents($db_schema_path);
 148              $sql_query = preg_replace('#phpbb_#i', $table_prefix, $sql_query);
 149              $sql_query = $this->database_helper->remove_comments($sql_query);
 150              $sql_query = $this->database_helper->split_sql_file($sql_query, $delimiter);
 151  
 152              foreach ($sql_query as $sql)
 153              {
 154                  if (!$this->db->sql_query($sql))
 155                  {
 156                      $error = $this->db->sql_error($this->db->get_sql_error_sql());
 157                      $this->iohandler->add_error_message('INST_ERR_DB', $error['message']);
 158                  }
 159              }
 160  
 161              unset($sql_query);
 162          }
 163  
 164          $change_prefix = false;
 165  
 166          // Generate database schema
 167          if ($this->filesystem->exists($this->phpbb_root_path . 'install/schemas/schema.json'))
 168          {
 169              $db_table_schema = @file_get_contents($this->phpbb_root_path . 'install/schemas/schema.json');
 170              $db_table_schema = json_decode($db_table_schema, true);
 171              $change_prefix = true;
 172          }
 173          else
 174          {
 175              global $table_prefix;
 176  
 177              $table_prefix = $this->config->get('table_prefix');
 178  
 179              if (!defined('CONFIG_TABLE'))
 180              {
 181                  // We need to include the constants file for the table constants
 182                  // when we generate the schema from the migration files.
 183                  include ($this->phpbb_root_path . 'includes/constants.' . $this->php_ext);
 184              }
 185  
 186              $finder = new \phpbb\finder($this->filesystem, $this->phpbb_root_path, null, $this->php_ext);
 187              $migrator_classes = $finder->core_path('phpbb/db/migration/data/')->get_classes();
 188              $factory = new \phpbb\db\tools\factory();
 189              $db_tools = $factory->get($this->db, true);
 190              $schema_generator = new \phpbb\db\migration\schema_generator(
 191                  $migrator_classes,
 192                  new \phpbb\config\config(array()),
 193                  $this->db,
 194                  $db_tools,
 195                  $this->phpbb_root_path,
 196                  $this->php_ext,
 197                  $table_prefix
 198              );
 199              $db_table_schema = $schema_generator->get_schema();
 200          }
 201  
 202          if (!defined('CONFIG_TABLE'))
 203          {
 204              // CONFIG_TABLE is required by sql_create_index() to check the
 205              // length of index names. However table_prefix is not defined
 206              // here yet, so we need to create the constant ourselves.
 207              define('CONFIG_TABLE', $table_prefix . 'config');
 208          }
 209  
 210          foreach ($db_table_schema as $table_name => $table_data)
 211          {
 212              $this->db_tools->sql_create_table(
 213                  ( ($change_prefix) ? ($table_prefix . substr($table_name, 6)) : $table_name ),
 214                  $table_data
 215              );
 216          }
 217      }
 218  
 219      /**
 220       * {@inheritdoc}
 221       */
 222  	static public function get_step_count()
 223      {
 224          return 1;
 225      }
 226  
 227      /**
 228       * {@inheritdoc}
 229       */
 230  	public function get_task_lang_name()
 231      {
 232          return 'TASK_CREATE_DATABASE_SCHEMA';
 233      }
 234  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1