static function get_size()
 {
     /** @var $wpdb wpdb */
     global $wpdb;
     $rows = MainWPChildDB::_query('SHOW table STATUS', $wpdb->dbh);
     $size = 0;
     while ($row = MainWPChildDB::fetch_array($rows)) {
         $size += $row['Data_length'];
     }
     return $size;
 }
Exemplo n.º 2
0
 function maintenance_optimize()
 {
     global $wpdb, $table_prefix;
     $sql = 'SHOW TABLE STATUS FROM `' . DB_NAME . '`';
     $result = @MainWPChildDB::_query($sql, $wpdb->dbh);
     if (@MainWPChildDB::num_rows($result) && @MainWPChildDB::is_result($result)) {
         while ($row = MainWPChildDB::fetch_array($result)) {
             if (strpos($row['Name'], $table_prefix) !== false) {
                 $sql = 'OPTIMIZE TABLE ' . $row['Name'];
                 MainWPChildDB::_query($sql, $wpdb->dbh);
             }
         }
     }
 }
 /**
  * The main loop triggered in step 5. Up here to keep it out of the way of the
  * HTML. This walks every table in the db that was selected in step 3 and then
  * walks every row and column replacing all occurences of a string with another.
  * We split large tables into 50,000 row blocks when dealing with them to save
  * on memmory consumption.
  *
  * @param mysql  $connection The db connection object
  * @param string $search     What we want to replace
  * @param string $replace    What we want to replace it with.
  * @param array  $tables     The tables we want to look at.
  *
  * @return array    Collection of information gathered during the run.
  */
 function icit_srdb_replacer($connection, $search = '', $replace = '', $tables = array())
 {
     global $guid, $exclude_cols;
     $report = array('tables' => 0, 'rows' => 0, 'change' => 0, 'updates' => 0, 'start' => microtime(), 'end' => microtime(), 'errors' => array());
     if (is_array($tables) && !empty($tables)) {
         foreach ($tables as $table) {
             $report['tables']++;
             $columns = array();
             // Get a list of columns in this table
             $fields = MainWPChildDB::_query('DESCRIBE ' . $table, $connection);
             while ($column = MainWPChildDB::fetch_array($fields)) {
                 $columns[$column['Field']] = $column['Key'] == 'PRI' ? true : false;
             }
             // Count the number of rows we have in the table if large we'll split into blocks, This is a mod from Simon Wheatley
             $row_count = MainWPChildDB::_query('SELECT COUNT(*) as count FROM ' . $table, $connection);
             // to fix bug
             $rows_result = MainWPChildDB::fetch_array($row_count);
             $row_count = $rows_result['count'];
             if ($row_count == 0) {
                 continue;
             }
             $page_size = 50000;
             $pages = ceil($row_count / $page_size);
             for ($page = 0; $page < $pages; $page++) {
                 $current_row = 0;
                 $start = $page * $page_size;
                 $end = $start + $page_size;
                 // Grab the content of the table
                 $data = MainWPChildDB::_query(sprintf('SELECT * FROM %s LIMIT %d, %d', $table, $start, $end), $connection);
                 if (!$data) {
                     $report['errors'][] = MainWPChildDB::error();
                 }
                 while ($row = MainWPChildDB::fetch_array($data)) {
                     $report['rows']++;
                     // Increment the row counter
                     $current_row++;
                     $update_sql = array();
                     $where_sql = array();
                     $upd = false;
                     foreach ($columns as $column => $primary_key) {
                         if ($guid == 1 && in_array($column, $exclude_cols)) {
                             continue;
                         }
                         $edited_data = $data_to_fix = $row[$column];
                         // Run a search replace on the data that'll respect the serialisation.
                         $edited_data = $this->recursive_unserialize_replace($search, $replace, $data_to_fix);
                         // Something was changed
                         if ($edited_data != $data_to_fix) {
                             $report['change']++;
                             $update_sql[] = $column . ' = "' . MainWPChildDB::real_escape_string($edited_data) . '"';
                             $upd = true;
                         }
                         if ($primary_key) {
                             $where_sql[] = $column . ' = "' . MainWPChildDB::real_escape_string($data_to_fix) . '"';
                         }
                     }
                     if ($upd && !empty($where_sql)) {
                         $sql = 'UPDATE ' . $table . ' SET ' . implode(', ', $update_sql) . ' WHERE ' . implode(' AND ', array_filter($where_sql));
                         $result = MainWPChildDB::_query($sql, $connection);
                         if (!$result) {
                             $report['errors'][] = MainWPChildDB::error();
                         } else {
                             $report['updates']++;
                         }
                     } elseif ($upd) {
                         $report['errors'][] = sprintf('"%s" has no primary key, manual change needed on row %s.', $table, $current_row);
                     }
                 }
             }
         }
     }
     $report['end'] = microtime();
     return $report;
 }
Exemplo n.º 4
0
 public function createBackupDB_legacy($filepath)
 {
     $fh = fopen($filepath, 'w');
     //or error;
     global $wpdb;
     $maxchars = 50000;
     //Get all the tables
     $tables_db = $wpdb->get_results('SHOW TABLES FROM `' . DB_NAME . '`', ARRAY_N);
     foreach ($tables_db as $curr_table) {
         $table = $curr_table[0];
         fwrite($fh, "\n" . 'DROP TABLE IF EXISTS ' . $table . ';');
         $table_create = $wpdb->get_row('SHOW CREATE TABLE ' . $table, ARRAY_N);
         fwrite($fh, "\n" . $table_create[1] . ';');
         //$rows = $wpdb->get_results('SELECT * FROM ' . $table, ARRAY_N);
         $rows = @MainWPChildDB::_query('SELECT * FROM ' . $table, $wpdb->dbh);
         if ($rows) {
             $table_columns = $wpdb->get_results('SHOW COLUMNS FROM ' . $table);
             $table_columns_insert = '';
             foreach ($table_columns as $table_column) {
                 if ($table_columns_insert != '') {
                     $table_columns_insert .= ', ';
                 }
                 $table_columns_insert .= '`' . $table_column->Field . '`';
             }
             $table_insert = 'INSERT INTO `' . $table . '` (';
             $table_insert .= $table_columns_insert;
             $table_insert .= ') VALUES ' . "\n";
             $current_insert = $table_insert;
             $inserted = false;
             $add_insert = '';
             while ($row = @MainWPChildDB::fetch_array($rows)) {
                 //Create new insert!
                 $add_insert = '(';
                 $add_insert_each = '';
                 foreach ($row as $value) {
                     //$add_insert_each .= "'" . str_replace(array("\n", "\r", "'"), array('\n', '\r', "\'"), $value) . "',";
                     $value = addslashes($value);
                     $value = str_replace("\n", "\\n", $value);
                     $value = str_replace("\r", "\\r", $value);
                     $add_insert_each .= '"' . $value . '",';
                 }
                 $add_insert .= trim($add_insert_each, ',') . ')';
                 //If we already inserted something & the total is too long - commit previous!
                 if ($inserted && strlen($add_insert) + strlen($current_insert) >= $maxchars) {
                     fwrite($fh, "\n" . $current_insert . ';');
                     $current_insert = $table_insert;
                     $current_insert .= $add_insert;
                     $inserted = false;
                 } else {
                     if ($inserted) {
                         $current_insert .= ', ' . "\n";
                     }
                     $current_insert .= $add_insert;
                 }
                 $inserted = true;
             }
             if ($inserted) {
                 fwrite($fh, "\n" . $current_insert . ';');
             }
         }
     }
     fclose($fh);
     return true;
 }