[ Index ]

PHP Cross Reference of phpBB-3.2.8-deutsch

title

Body

[close]

/phpbb/db/extractor/ -> mysql_extractor.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\db\extractor;
  15  
  16  use phpbb\db\extractor\exception\extractor_not_initialized_exception;
  17  
  18  class mysql_extractor extends base_extractor
  19  {
  20      /**
  21      * {@inheritdoc}
  22      */
  23  	public function write_start($table_prefix)
  24      {
  25          if (!$this->is_initialized)
  26          {
  27              throw new extractor_not_initialized_exception();
  28          }
  29  
  30          $sql_data = "#\n";
  31          $sql_data .= "# phpBB Backup Script\n";
  32          $sql_data .= "# Dump of tables for $table_prefix\n";
  33          $sql_data .= "# DATE : " . gmdate("d-m-Y H:i:s", $this->time) . " GMT\n";
  34          $sql_data .= "#\n";
  35          $this->flush($sql_data);
  36      }
  37  
  38      /**
  39      * {@inheritdoc}
  40      */
  41  	public function write_table($table_name)
  42      {
  43          static $new_extract;
  44  
  45          if (!$this->is_initialized)
  46          {
  47              throw new extractor_not_initialized_exception();
  48          }
  49  
  50          if ($new_extract === null)
  51          {
  52              if ($this->db->get_sql_layer() === 'mysqli' || version_compare($this->db->sql_server_info(true), '3.23.20', '>='))
  53              {
  54                  $new_extract = true;
  55              }
  56              else
  57              {
  58                  $new_extract = false;
  59              }
  60          }
  61  
  62          if ($new_extract)
  63          {
  64              $this->new_write_table($table_name);
  65          }
  66          else
  67          {
  68              $this->old_write_table($table_name);
  69          }
  70      }
  71  
  72      /**
  73      * {@inheritdoc}
  74      */
  75  	public function write_data($table_name)
  76      {
  77          if (!$this->is_initialized)
  78          {
  79              throw new extractor_not_initialized_exception();
  80          }
  81  
  82          if ($this->db->get_sql_layer() === 'mysqli')
  83          {
  84              $this->write_data_mysqli($table_name);
  85          }
  86          else
  87          {
  88              $this->write_data_mysql($table_name);
  89          }
  90      }
  91  
  92      /**
  93      * Extracts data from database table (for MySQLi driver)
  94      *
  95      * @param    string    $table_name    name of the database table
  96      * @return null
  97      * @throws \phpbb\db\extractor\exception\extractor_not_initialized_exception when calling this function before init_extractor()
  98      */
  99  	protected function write_data_mysqli($table_name)
 100      {
 101          if (!$this->is_initialized)
 102          {
 103              throw new extractor_not_initialized_exception();
 104          }
 105  
 106          $sql = "SELECT *
 107              FROM $table_name";
 108          $result = mysqli_query($this->db->get_db_connect_id(), $sql, MYSQLI_USE_RESULT);
 109          if ($result != false)
 110          {
 111              $fields_cnt = mysqli_num_fields($result);
 112  
 113              // Get field information
 114              $field = mysqli_fetch_fields($result);
 115              $field_set = array();
 116  
 117              for ($j = 0; $j < $fields_cnt; $j++)
 118              {
 119                  $field_set[] = $field[$j]->name;
 120              }
 121  
 122              $search            = array("\\", "'", "\x00", "\x0a", "\x0d", "\x1a", '"');
 123              $replace        = array("\\\\", "\\'", '\0', '\n', '\r', '\Z', '\\"');
 124              $fields            = implode(', ', $field_set);
 125              $sql_data        = 'INSERT INTO ' . $table_name . ' (' . $fields . ') VALUES ';
 126              $first_set        = true;
 127              $query_len        = 0;
 128              $max_len        = get_usable_memory();
 129  
 130              while ($row = mysqli_fetch_row($result))
 131              {
 132                  $values    = array();
 133                  if ($first_set)
 134                  {
 135                      $query = $sql_data . '(';
 136                  }
 137                  else
 138                  {
 139                      $query  .= ',(';
 140                  }
 141  
 142                  for ($j = 0; $j < $fields_cnt; $j++)
 143                  {
 144                      if (!isset($row[$j]) || is_null($row[$j]))
 145                      {
 146                          $values[$j] = 'NULL';
 147                      }
 148                      else if (($field[$j]->flags & 32768) && !($field[$j]->flags & 1024))
 149                      {
 150                          $values[$j] = $row[$j];
 151                      }
 152                      else
 153                      {
 154                          $values[$j] = "'" . str_replace($search, $replace, $row[$j]) . "'";
 155                      }
 156                  }
 157                  $query .= implode(', ', $values) . ')';
 158  
 159                  $query_len += strlen($query);
 160                  if ($query_len > $max_len)
 161                  {
 162                      $this->flush($query . ";\n\n");
 163                      $query = '';
 164                      $query_len = 0;
 165                      $first_set = true;
 166                  }
 167                  else
 168                  {
 169                      $first_set = false;
 170                  }
 171              }
 172              mysqli_free_result($result);
 173  
 174              // check to make sure we have nothing left to flush
 175              if (!$first_set && $query)
 176              {
 177                  $this->flush($query . ";\n\n");
 178              }
 179          }
 180      }
 181  
 182      /**
 183      * Extracts data from database table (for MySQL driver)
 184      *
 185      * @param    string    $table_name    name of the database table
 186      * @return null
 187      * @throws \phpbb\db\extractor\exception\extractor_not_initialized_exception when calling this function before init_extractor()
 188      */
 189  	protected function write_data_mysql($table_name)
 190      {
 191          if (!$this->is_initialized)
 192          {
 193              throw new extractor_not_initialized_exception();
 194          }
 195  
 196          $sql = "SELECT *
 197              FROM $table_name";
 198          $result = mysql_unbuffered_query($sql, $this->db->get_db_connect_id());
 199  
 200          if ($result != false)
 201          {
 202              $fields_cnt = mysql_num_fields($result);
 203  
 204              // Get field information
 205              $field = array();
 206              for ($i = 0; $i < $fields_cnt; $i++)
 207              {
 208                  $field[] = mysql_fetch_field($result, $i);
 209              }
 210              $field_set = array();
 211  
 212              for ($j = 0; $j < $fields_cnt; $j++)
 213              {
 214                  $field_set[] = $field[$j]->name;
 215              }
 216  
 217              $search            = array("\\", "'", "\x00", "\x0a", "\x0d", "\x1a", '"');
 218              $replace        = array("\\\\", "\\'", '\0', '\n', '\r', '\Z', '\\"');
 219              $fields            = implode(', ', $field_set);
 220              $sql_data        = 'INSERT INTO ' . $table_name . ' (' . $fields . ') VALUES ';
 221              $first_set        = true;
 222              $query_len        = 0;
 223              $max_len        = get_usable_memory();
 224  
 225              while ($row = mysql_fetch_row($result))
 226              {
 227                  $values = array();
 228                  if ($first_set)
 229                  {
 230                      $query = $sql_data . '(';
 231                  }
 232                  else
 233                  {
 234                      $query  .= ',(';
 235                  }
 236  
 237                  for ($j = 0; $j < $fields_cnt; $j++)
 238                  {
 239                      if (!isset($row[$j]) || is_null($row[$j]))
 240                      {
 241                          $values[$j] = 'NULL';
 242                      }
 243                      else if ($field[$j]->numeric && ($field[$j]->type !== 'timestamp'))
 244                      {
 245                          $values[$j] = $row[$j];
 246                      }
 247                      else
 248                      {
 249                          $values[$j] = "'" . str_replace($search, $replace, $row[$j]) . "'";
 250                      }
 251                  }
 252                  $query .= implode(', ', $values) . ')';
 253  
 254                  $query_len += strlen($query);
 255                  if ($query_len > $max_len)
 256                  {
 257                      $this->flush($query . ";\n\n");
 258                      $query = '';
 259                      $query_len = 0;
 260                      $first_set = true;
 261                  }
 262                  else
 263                  {
 264                      $first_set = false;
 265                  }
 266              }
 267              mysql_free_result($result);
 268  
 269              // check to make sure we have nothing left to flush
 270              if (!$first_set && $query)
 271              {
 272                  $this->flush($query . ";\n\n");
 273              }
 274          }
 275      }
 276  
 277      /**
 278      * Extracts database table structure (for MySQLi or MySQL 3.23.20+)
 279      *
 280      * @param    string    $table_name    name of the database table
 281      * @return null
 282      * @throws \phpbb\db\extractor\exception\extractor_not_initialized_exception when calling this function before init_extractor()
 283      */
 284  	protected function new_write_table($table_name)
 285      {
 286          if (!$this->is_initialized)
 287          {
 288              throw new extractor_not_initialized_exception();
 289          }
 290  
 291          $sql = 'SHOW CREATE TABLE ' . $table_name;
 292          $result = $this->db->sql_query($sql);
 293          $row = $this->db->sql_fetchrow($result);
 294  
 295          $sql_data = '# Table: ' . $table_name . "\n";
 296          $sql_data .= "DROP TABLE IF EXISTS $table_name;\n";
 297          $this->flush($sql_data . $row['Create Table'] . ";\n\n");
 298  
 299          $this->db->sql_freeresult($result);
 300      }
 301  
 302      /**
 303      * Extracts database table structure (for MySQL verisons older than 3.23.20)
 304      *
 305      * @param    string    $table_name    name of the database table
 306      * @return null
 307      * @throws \phpbb\db\extractor\exception\extractor_not_initialized_exception when calling this function before init_extractor()
 308      */
 309  	protected function old_write_table($table_name)
 310      {
 311          if (!$this->is_initialized)
 312          {
 313              throw new extractor_not_initialized_exception();
 314          }
 315  
 316          $sql_data = '# Table: ' . $table_name . "\n";
 317          $sql_data .= "DROP TABLE IF EXISTS $table_name;\n";
 318          $sql_data .= "CREATE TABLE $table_name(\n";
 319          $rows = array();
 320  
 321          $sql = "SHOW FIELDS
 322              FROM $table_name";
 323          $result = $this->db->sql_query($sql);
 324  
 325          while ($row = $this->db->sql_fetchrow($result))
 326          {
 327              $line = '   ' . $row['Field'] . ' ' . $row['Type'];
 328  
 329              if (!is_null($row['Default']))
 330              {
 331                  $line .= " DEFAULT '{$row['Default']}'";
 332              }
 333  
 334              if ($row['Null'] != 'YES')
 335              {
 336                  $line .= ' NOT NULL';
 337              }
 338  
 339              if ($row['Extra'] != '')
 340              {
 341                  $line .= ' ' . $row['Extra'];
 342              }
 343  
 344              $rows[] = $line;
 345          }
 346          $this->db->sql_freeresult($result);
 347  
 348          $sql = "SHOW KEYS
 349              FROM $table_name";
 350  
 351          $result = $this->db->sql_query($sql);
 352  
 353          $index = array();
 354          while ($row = $this->db->sql_fetchrow($result))
 355          {
 356              $kname = $row['Key_name'];
 357  
 358              if ($kname != 'PRIMARY')
 359              {
 360                  if ($row['Non_unique'] == 0)
 361                  {
 362                      $kname = "UNIQUE|$kname";
 363                  }
 364              }
 365  
 366              if ($row['Sub_part'])
 367              {
 368                  $row['Column_name'] .= '(' . $row['Sub_part'] . ')';
 369              }
 370              $index[$kname][] = $row['Column_name'];
 371          }
 372          $this->db->sql_freeresult($result);
 373  
 374          foreach ($index as $key => $columns)
 375          {
 376              $line = '   ';
 377  
 378              if ($key == 'PRIMARY')
 379              {
 380                  $line .= 'PRIMARY KEY (' . implode(', ', $columns) . ')';
 381              }
 382              else if (strpos($key, 'UNIQUE') === 0)
 383              {
 384                  $line .= 'UNIQUE ' . substr($key, 7) . ' (' . implode(', ', $columns) . ')';
 385              }
 386              else if (strpos($key, 'FULLTEXT') === 0)
 387              {
 388                  $line .= 'FULLTEXT ' . substr($key, 9) . ' (' . implode(', ', $columns) . ')';
 389              }
 390              else
 391              {
 392                  $line .= "KEY $key (" . implode(', ', $columns) . ')';
 393              }
 394  
 395              $rows[] = $line;
 396          }
 397  
 398          $sql_data .= implode(",\n", $rows);
 399          $sql_data .= "\n);\n\n";
 400  
 401          $this->flush($sql_data);
 402      }
 403  }


Generated: Tue Apr 7 19:42:26 2020 Cross-referenced by PHPXref 0.7.1