[ Index ]

PHP Cross Reference of phpBB-3.3.3-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              $schema_name .= '_41';
 133          }
 134  
 135          $db_schema_path = $this->phpbb_root_path . 'install/schemas/' . $schema_name . '_schema.sql';
 136  
 137          // Load database vendor specific code if there is any
 138          if ($this->filesystem->exists($db_schema_path))
 139          {
 140              $sql_query = @file_get_contents($db_schema_path);
 141              $sql_query = preg_replace('#phpbb_#i', $table_prefix, $sql_query);
 142              $sql_query = $this->database_helper->remove_comments($sql_query);
 143              $sql_query = $this->database_helper->split_sql_file($sql_query, $delimiter);
 144  
 145              foreach ($sql_query as $sql)
 146              {
 147                  if (!$this->db->sql_query($sql))
 148                  {
 149                      $error = $this->db->sql_error($this->db->get_sql_error_sql());
 150                      $this->iohandler->add_error_message('INST_ERR_DB', $error['message']);
 151                  }
 152              }
 153  
 154              unset($sql_query);
 155          }
 156  
 157          $change_prefix = false;
 158  
 159          // Generate database schema
 160          if ($this->filesystem->exists($this->phpbb_root_path . 'install/schemas/schema.json'))
 161          {
 162              $db_table_schema = @file_get_contents($this->phpbb_root_path . 'install/schemas/schema.json');
 163              $db_table_schema = json_decode($db_table_schema, true);
 164              $change_prefix = true;
 165          }
 166          else
 167          {
 168              global $table_prefix;
 169  
 170              $table_prefix = $this->config->get('table_prefix');
 171  
 172              if (!defined('CONFIG_TABLE'))
 173              {
 174                  // We need to include the constants file for the table constants
 175                  // when we generate the schema from the migration files.
 176                  include ($this->phpbb_root_path . 'includes/constants.' . $this->php_ext);
 177              }
 178  
 179              $finder = new \phpbb\finder($this->filesystem, $this->phpbb_root_path, null, $this->php_ext);
 180              $migrator_classes = $finder->core_path('phpbb/db/migration/data/')->get_classes();
 181              $factory = new \phpbb\db\tools\factory();
 182              $db_tools = $factory->get($this->db, true);
 183              $schema_generator = new \phpbb\db\migration\schema_generator(
 184                  $migrator_classes,
 185                  new \phpbb\config\config(array()),
 186                  $this->db,
 187                  $db_tools,
 188                  $this->phpbb_root_path,
 189                  $this->php_ext,
 190                  $table_prefix
 191              );
 192              $db_table_schema = $schema_generator->get_schema();
 193          }
 194  
 195          if (!defined('CONFIG_TABLE'))
 196          {
 197              // CONFIG_TABLE is required by sql_create_index() to check the
 198              // length of index names. However table_prefix is not defined
 199              // here yet, so we need to create the constant ourselves.
 200              define('CONFIG_TABLE', $table_prefix . 'config');
 201          }
 202  
 203          foreach ($db_table_schema as $table_name => $table_data)
 204          {
 205              $this->db_tools->sql_create_table(
 206                  ( ($change_prefix) ? ($table_prefix . substr($table_name, 6)) : $table_name ),
 207                  $table_data
 208              );
 209          }
 210      }
 211  
 212      /**
 213       * {@inheritdoc}
 214       */
 215  	static public function get_step_count()
 216      {
 217          return 1;
 218      }
 219  
 220      /**
 221       * {@inheritdoc}
 222       */
 223  	public function get_task_lang_name()
 224      {
 225          return 'TASK_CREATE_DATABASE_SCHEMA';
 226      }
 227  }


Generated: Sun Feb 14 20:08:31 2021 Cross-referenced by PHPXref 0.7.1