[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/phpbb/install/module/install_database/task/ -> add_default_data.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 add_default_data extends \phpbb\install\task_base
  22  {
  23      /**
  24       * @var \phpbb\db\driver\driver_interface
  25       */
  26      protected $db;
  27  
  28      /**
  29       * @var \phpbb\install\helper\database
  30       */
  31      protected $database_helper;
  32  
  33      /**
  34       * @var \phpbb\install\helper\config
  35       */
  36      protected $config;
  37  
  38      /**
  39       * @var \phpbb\install\helper\iohandler\iohandler_interface
  40       */
  41      protected $iohandler;
  42  
  43      /**
  44       * @var \phpbb\language\language
  45       */
  46      protected $language;
  47  
  48      /**
  49       * @var string
  50       */
  51      protected $phpbb_root_path;
  52  
  53      /**
  54       * Constructor
  55       *
  56       * @param \phpbb\install\helper\database                        $db_helper    Installer's database helper
  57       * @param \phpbb\install\helper\config                            $config        Installer config
  58       * @param \phpbb\install\helper\iohandler\iohandler_interface    $iohandler    Installer's input-output handler
  59       * @param \phpbb\install\helper\container_factory                $container    Installer's DI container
  60       * @param \phpbb\language\language                                $language    Language service
  61       * @param string                                                $root_path    Root path of phpBB
  62       */
  63  	public function __construct(\phpbb\install\helper\database $db_helper,
  64                                  \phpbb\install\helper\config $config,
  65                                  \phpbb\install\helper\iohandler\iohandler_interface $iohandler,
  66                                  \phpbb\install\helper\container_factory $container,
  67                                  \phpbb\language\language $language,
  68                                  $root_path)
  69      {
  70          $this->db                = $container->get('dbal.conn.driver');
  71          $this->database_helper    = $db_helper;
  72          $this->config            = $config;
  73          $this->iohandler        = $iohandler;
  74          $this->language            = $language;
  75          $this->phpbb_root_path    = $root_path;
  76  
  77          parent::__construct(true);
  78      }
  79  
  80      /**
  81       * {@inheritdoc}
  82       */
  83  	public function run()
  84      {
  85          $this->db->sql_return_on_error(true);
  86  
  87          $table_prefix = $this->config->get('table_prefix');
  88          $dbms = $this->config->get('dbms');
  89          $dbms_info = $this->database_helper->get_available_dbms($dbms);
  90  
  91          // Get schema data from file
  92          $sql_query = @file_get_contents($this->phpbb_root_path . 'install/schemas/schema_data.sql');
  93  
  94          // Clean up SQL
  95          $sql_query = $this->replace_dbms_specific_sql($sql_query);
  96          $sql_query = preg_replace('# phpbb_([^\s]*) #i', ' ' . $table_prefix . '\1 ', $sql_query);
  97          $sql_query = preg_replace_callback('#\{L_([A-Z0-9\-_]*)\}#s', array($this, 'lang_replace_callback'), $sql_query);
  98          $sql_query = $this->database_helper->remove_comments($sql_query);
  99          $sql_query = $this->database_helper->split_sql_file($sql_query, $dbms_info[$dbms]['DELIM']);
 100  
 101          $i = $this->config->get('add_default_data_index', 0);
 102          $total = count($sql_query);
 103          $sql_query = array_slice($sql_query, $i);
 104  
 105          foreach ($sql_query as $sql)
 106          {
 107              if (!$this->db->sql_query($sql))
 108              {
 109                  $error = $this->db->sql_error($this->db->get_sql_error_sql());
 110                  $this->iohandler->add_error_message('INST_ERR_DB', $error['message']);
 111              }
 112  
 113              $i++;
 114  
 115              // Stop execution if resource limit is reached
 116              if ($this->config->get_time_remaining() <= 0 || $this->config->get_memory_remaining() <= 0)
 117              {
 118                  break;
 119              }
 120          }
 121  
 122          $this->config->set('add_default_data_index', $i);
 123  
 124          if ($i < $total)
 125          {
 126              throw new resource_limit_reached_exception();
 127          }
 128      }
 129  
 130      /**
 131       * Process DB specific SQL
 132       *
 133       * @return string
 134       */
 135  	protected function replace_dbms_specific_sql($query)
 136      {
 137          if ($this->db instanceof \phpbb\db\driver\mssql_base)
 138          {
 139              $query = preg_replace('#\# MSSQL IDENTITY (phpbb_[a-z_]+) (ON|OFF) \##s', 'SET IDENTITY_INSERT \1 \2;', $query);
 140          }
 141          else if ($this->db instanceof \phpbb\db\driver\postgres)
 142          {
 143              $query = preg_replace('#\# POSTGRES (BEGIN|COMMIT) \##s', '\1; ', $query);
 144          }
 145          else if ($this->db instanceof \phpbb\db\driver\mysql_base)
 146          {
 147              $query = str_replace('\\', '\\\\', $query);
 148          }
 149  
 150          return $query;
 151      }
 152  
 153      /**
 154       * Callback function for language replacing
 155       *
 156       * @param array    $matches
 157       * @return string
 158       */
 159  	public function lang_replace_callback($matches)
 160      {
 161          if (!empty($matches[1]))
 162          {
 163              return $this->db->sql_escape($this->language->lang($matches[1]));
 164          }
 165  
 166          return '';
 167      }
 168  
 169      /**
 170       * {@inheritdoc}
 171       */
 172  	static public function get_step_count()
 173      {
 174          return 1;
 175      }
 176  
 177      /**
 178       * {@inheritdoc}
 179       */
 180  	public function get_task_lang_name()
 181      {
 182          return 'TASK_ADD_DEFAULT_DATA';
 183      }
 184  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1