Exemplo n.º 1
0
 public function createBackupDB($filepath_prefix, $archiveExt = false, &$archiver = null)
 {
     $timeout = 20 * 60 * 60;
     //20minutes
     // @codingStandardsIgnoreStart
     @set_time_limit($timeout);
     @ini_set('max_execution_time', $timeout);
     $mem = '512M';
     @ini_set('memory_limit', $mem);
     // @codingStandardsIgnoreEnd
     /** @var $wpdb wpdb */
     global $wpdb;
     $db_files = array();
     //Get all the tables
     $tables_db = $wpdb->get_results('SHOW TABLES FROM `' . DB_NAME . '`', ARRAY_N);
     foreach ($tables_db as $curr_table) {
         if (null !== $archiver) {
             $archiver->updatePidFile();
         }
         $table = $curr_table[0];
         $currentfile = $filepath_prefix . '-' . MainWP_Helper::sanitize_filename($table) . '.sql';
         $db_files[] = $currentfile;
         if (file_exists($currentfile)) {
             continue;
         }
         $fh = fopen($currentfile . '.tmp', 'w');
         //or error;
         fwrite($fh, "\n\n" . 'DROP TABLE IF EXISTS ' . $table . ';');
         //todo fix this
         //$table_create = $wpdb->get_row( $wpdb->prepare( 'SHOW CREATE TABLE %s', $table ), ARRAY_N );
         $table_create = $wpdb->get_row('SHOW CREATE TABLE ' . $table, ARRAY_N);
         fwrite($fh, "\n" . $table_create[1] . ";\n\n");
         // @codingStandardsIgnoreStart
         $rows = @MainWP_Child_DB::_query('SELECT * FROM ' . $table, $wpdb->dbh);
         // @codingStandardsIgnoreEnd
         if ($rows) {
             $i = 0;
             $table_insert = 'INSERT INTO `' . $table . '` VALUES (';
             // @codingStandardsIgnoreStart
             while ($row = @MainWP_Child_DB::fetch_array($rows)) {
                 // @codingStandardsIgnoreEnd
                 $query = $table_insert;
                 foreach ($row as $value) {
                     $query .= '"' . MainWP_Child_DB::real_escape_string($value) . '", ';
                 }
                 $query = trim($query, ', ') . ');';
                 fwrite($fh, "\n" . $query);
                 $i++;
                 if ($i >= 50) {
                     fflush($fh);
                     $i = 0;
                 }
                 $query = null;
                 $row = null;
             }
         }
         $rows = null;
         fflush($fh);
         fclose($fh);
         rename($currentfile . '.tmp', $currentfile);
     }
     fclose(fopen($filepath_prefix . '.sql', 'w'));
     $db_files[] = $filepath_prefix . '.sql';
     $archivefilePath = null;
     if (false !== $archiveExt) {
         $archivefilePath = $filepath_prefix . '.sql.' . $archiveExt;
         if ('zip' === $archiveExt) {
             $this->archiver = null;
         } else {
             $this->archiver = new Tar_Archiver($this, $archiveExt);
         }
         if ($this->zipFile($db_files, $archivefilePath) && file_exists($archivefilePath)) {
             foreach ($db_files as $db_file) {
                 @unlink($db_file);
             }
         } else {
             //todo: throw exception!
         }
     }
     return false !== $archiveExt ? array('filepath' => $archivefilePath) : $db_files;
 }