From bcf20adf436fd0eb1ee5b5dc32d4520bac197c92 Mon Sep 17 00:00:00 2001 From: Peter Ivanov Date: Fri, 12 Dec 2014 13:54:45 +0200 Subject: [PATCH] fix on #34 template export to zip with sql --- src/Microweber/Application.php | 6 +- .../Utils/Adapters/Cache/LaravelCache.php | 4 +- src/Microweber/functions/common.php | 2 +- .../template_exporter/Worker.php | 146 +++++++++--------- .../developer_tools/template_exporter/log.php | 14 +- 5 files changed, 92 insertions(+), 80 deletions(-) diff --git a/src/Microweber/Application.php b/src/Microweber/Application.php index f0dc8eb..67f9948 100644 --- a/src/Microweber/Application.php +++ b/src/Microweber/Application.php @@ -51,13 +51,15 @@ public static function getInstance($params = null) return self::$instance; } - public function make($class) + public function make($property) { - return app()->make($class); + + return app()->make($property); } public function __get($property) { + return $this->make($property); } } \ No newline at end of file diff --git a/src/Microweber/Utils/Adapters/Cache/LaravelCache.php b/src/Microweber/Utils/Adapters/Cache/LaravelCache.php index 97652b7..c88609d 100644 --- a/src/Microweber/Utils/Adapters/Cache/LaravelCache.php +++ b/src/Microweber/Utils/Adapters/Cache/LaravelCache.php @@ -19,7 +19,7 @@ function __construct($app) $driver = $this->app['config']['cache.driver']; - if ($driver == 'file' or $driver == 'database') { + if ($driver == 'database') { $this->support_tags = false; } else { $this->support_tags = true; @@ -92,7 +92,9 @@ public function cache_group($cache_group) public function save($data_to_cache, $cache_id, $cache_group = 'global') { + if (!$this->support_tags) { + return; return Cache::put($cache_group . $cache_id, $data_to_cache, $this->ttl); } diff --git a/src/Microweber/functions/common.php b/src/Microweber/functions/common.php index c5fb824..a58c51e 100644 --- a/src/Microweber/functions/common.php +++ b/src/Microweber/functions/common.php @@ -4,7 +4,7 @@ function mw($class = null) { $app = \Microweber\Application::getInstance(); - + $class = str_replace('/','\\',$class); if ($class == null or $class == false or strtolower($class) == 'application') { return $app; } else { diff --git a/userfiles/modules/admin/developer_tools/template_exporter/Worker.php b/userfiles/modules/admin/developer_tools/template_exporter/Worker.php index f08b754..fc01b98 100644 --- a/userfiles/modules/admin/developer_tools/template_exporter/Worker.php +++ b/userfiles/modules/admin/developer_tools/template_exporter/Worker.php @@ -10,7 +10,7 @@ */ -namespace Microweber\admin\developer_tools\template_exporter; +namespace admin\developer_tools\template_exporter; use ZipArchive; @@ -90,20 +90,20 @@ function __construct($app = null) } - + static function log_bg_action($back_log_action) { if ($back_log_action == false) { - mw()->log->delete("is_system=y&rel=export&user_ip=" . USER_IP); + mw()->log_manager->delete("is_system=y&rel_type=export&user_ip=" . USER_IP); } else { - $check = mw()->log->get("order_by=created_at desc&one=true&is_system=y&created_at=[mt]30 min ago&field=action&rel=export&user_ip=" . USER_IP); + $check = mw()->log_manager->get("order_by=created_at desc&one=true&is_system=y&created_at=[mt]30 min ago&field=action&rel_type=export&user_ip=" . USER_IP); if (is_array($check) and isset($check['id'])) { - mw()->log->save("is_system=y&field=action&rel=export&value=" . $back_log_action . "&user_ip=" . USER_IP . "&id=" . $check['id']); + mw()->log_manager->save("is_system=y&field=action&rel_type=export&value=" . $back_log_action . "&user_ip=" . USER_IP . "&id=" . $check['id']); } else { - mw()->log->save("is_system=y&field=action&rel=export&value=" . $back_log_action . "&user_ip=" . USER_IP); + mw()->log_manager->save("is_system=y&field=action&rel_type=export&value=" . $back_log_action . "&user_ip=" . USER_IP); } } @@ -140,10 +140,10 @@ function exec_create_full() $here = $this->get_bakup_location(); $filename = $here . 'full_export_' . date("Y-M-d-His") . '_' . uniqid() . '' . '.zip'; - $userfiles_folder = MW_USERFILES; + $userfiles_folder = userfiles_path(); $locations = array(); - $locations[] = MW_USERFILES; + $locations[] = userfiles_path(); //$locations[] = $filename2; $fileTime = date("D, d M Y H:i:s T"); @@ -171,7 +171,7 @@ function exec_create_full() $this->log_action($back_log_action); - $zip->addDirectoryContent(MW_USERFILES, '', true); + $zip->addDirectoryContent(userfiles_path(), '', true); $back_log_action = "Adding userfiles to zip"; $this->log_action($back_log_action); @@ -184,7 +184,7 @@ function exec_create_full() $this->log_action($back_log_action); sleep(5); - $back_log_action = "reload"; + $back_log_action = "rel_typeoad"; $this->log_action($back_log_action); sleep(5); @@ -286,9 +286,9 @@ function cronjob($params = false) $cache_id_loc = 'export_progress'; $cache_state_id = 'export_zip_state'; - $cache_content = $this->app->cache->get($cache_id, 'export'); - $cache_lock = $this->app->cache->get($cache_id_loc, 'export'); - $cache_state = $this->app->cache->get($cache_state_id, 'export', 30); + $cache_content = $this->app->cache_manager->get($cache_id, 'export'); + $cache_lock = $this->app->cache_manager->get($cache_id_loc, 'export'); + $cache_state = $this->app->cache_manager->get($cache_state_id, 'export', 30); @@ -305,8 +305,8 @@ function cronjob($params = false) if ($cache_content == false or empty($cache_content)) { - $this->app->cache->save(false, $cache_id_loc, 'export'); - $this->app->cache->save(false, $cache_id, 'export'); + $this->app->cache_manager->save(false, $cache_id_loc, 'export'); + $this->app->cache_manager->save(false, $cache_id, 'export'); return true; @@ -325,7 +325,7 @@ function cronjob($params = false) $cache_lock['files_count'] = count($cache_content); $cache_lock['time'] = $time; $cache_lock['filename'] = $filename; - $this->app->cache->save($cache_lock, $cache_id_loc, 'export'); + $this->app->cache_manager->save($cache_lock, $cache_id_loc, 'export'); // return false; } else { if (isset($cache_lock['filename'])) { @@ -356,14 +356,14 @@ function cronjob($params = false) if (is_array($export_actions)) { $i = 0; - $this->app->cache->save($filename, $cache_id_loc, 'export'); + $this->app->cache_manager->save($filename, $cache_id_loc, 'export'); if (!$mw_export_zip_obj->open($filename, ZIPARCHIVE::CREATE)) { $zip_opened = 1; return false; } - $this->app->cache->save('opened', $cache_state_id, 'export'); + $this->app->cache_manager->save('opened', $cache_state_id, 'export'); $limit_per_turn = 20; @@ -381,7 +381,7 @@ function cronjob($params = false) $mw_export_zip_obj->close(); } } - $this->app->cache->save('closed', $cache_state_id, 'export'); + $this->app->cache_manager->save('closed', $cache_state_id, 'export'); } else { $cache_lock['processed']++; @@ -397,14 +397,14 @@ function cronjob($params = false) $back_log_action = "Progress {$precent}% ({$cache_lock['processed']}/{$cache_lock['files_count']})
" . basename($item) . ""; $this->log_action($back_log_action); - $this->app->cache->save($cache_lock, $cache_id_loc, 'export'); + $this->app->cache_manager->save($cache_lock, $cache_id_loc, 'export'); if ($item == 'make_db_export') { $limit_per_turn = 1; $mw_export_zip_obj->close(); - $this->app->cache->save('closed', $cache_state_id, 'export'); + $this->app->cache_manager->save('closed', $cache_state_id, 'export'); $db_file = $this->create($bak_fn . '.sql'); @@ -414,7 +414,7 @@ function cronjob($params = false) $zip_opened = 1; return false; } - $this->app->cache->save('opened', $cache_state_id, 'export'); + $this->app->cache_manager->save('opened', $cache_state_id, 'export'); if (isset($db_file['filename'])) { @@ -428,21 +428,21 @@ function cronjob($params = false) } } } else { - $relative_loc = str_replace(MW_USERFILES, '', $item); + $rel_typeative_loc = str_replace(userfiles_path(), '', $item); $new_export_actions = array(); if (is_dir($item)) { - $mw_export_zip_obj->addEmptyDir($relative_loc); + $mw_export_zip_obj->addEmptyDir($rel_typeative_loc); } elseif (is_file($item)) { // d($item); - //$relative_loc_dn = dirname($relative_loc); + //$rel_typeative_loc_dn = dirname($rel_typeative_loc); - //$mw_export_zip_obj->addFromString($relative_loc, file_get_contents($item)); + //$mw_export_zip_obj->addFromString($rel_typeative_loc, file_get_contents($item)); - $mw_export_zip_obj->addFile($item, $relative_loc); + $mw_export_zip_obj->addFile($item, $rel_typeative_loc); } @@ -456,16 +456,16 @@ function cronjob($params = false) if (isset($new_export_actions) and !empty($new_export_actions)) { $export_actions = array_merge($export_actions, $new_export_actions); array_unique($export_actions); - $this->app->cache->save($export_actions, $cache_id, 'export'); + $this->app->cache_manager->save($export_actions, $cache_id, 'export'); } else { - $this->app->cache->save($export_actions, $cache_id, 'export'); + $this->app->cache_manager->save($export_actions, $cache_id, 'export'); } // d($export_actions[$key]); if (empty($export_actions)) { - $this->app->cache->save(false, $cache_id, 'export'); + $this->app->cache_manager->save(false, $cache_id, 'export'); } @@ -474,13 +474,16 @@ function cronjob($params = false) } $mw_export_zip_obj->close(); - $this->app->cache->save('closed', $cache_state_id, 'export'); + $this->app->cache_manager->save('closed', $cache_state_id, 'export'); } } - // $this->app->cache->save(false, $cache_id_loc, 'export'); + // $this->app->cache_manager->save(false, $cache_id_loc, 'export'); if (empty($export_actions)) { - $this->app->cache->save(false, $cache_id, 'export'); + + $this->log_action('done'); + + $this->app->cache_manager->save(false, $cache_id, 'export'); } return $cache_content; @@ -503,14 +506,11 @@ function create($filename = false) only_admin_access(); } - $temp_db = $db = $this->app->config('db'); + $temp_db = false;; // Settings $table = '*'; - $host = $DBhost = $db['host']; - $user = $DBuser = $db['user']; - $pass = $DBpass = $db['pass']; - $name = $DBName = $db['dbname']; + // Set the suffix of the export filename if ($table == '*') { @@ -563,8 +563,8 @@ function create($filename = false) file_put_contents($hta, 'Deny from all'); } - $head = "/* Microweber database export exported on: " . date('l jS \of F Y h:i:s A') . " */ \n"; - $head .= "/* MW_TABLE_PREFIX: " . MW_TABLE_PREFIX . " */ \n\n\n"; + $head = "/* Microweber database exported on: " . date('l jS \of F Y h:i:s A') . " */ \n"; + $head .= "/* get_table_prefix(): " . get_table_prefix() . " */ \n\n\n"; file_put_contents($sql_bak_file, $head); $return = ""; $tables = '*'; @@ -597,29 +597,29 @@ function create($filename = false) foreach ($tables as $table) { $is_cms_table = false; - if (MW_TABLE_PREFIX == '') { + if (get_table_prefix() == '') { $is_cms_table = 1; - } elseif (stristr($table, MW_TABLE_PREFIX)) { + } elseif (stristr($table, get_table_prefix())) { $is_cms_table = 1; } - if ($table == MW_TABLE_PREFIX . 'users') { + if ($table == get_table_prefix() . 'users') { $is_cms_table = false; - } else if ($table == MW_TABLE_PREFIX . 'content_fields_drafts') { + } else if ($table == get_table_prefix() . 'content_fields_drafts') { $is_cms_table = false; - } else if ($table == MW_TABLE_PREFIX . 'modules') { + } else if ($table == get_table_prefix() . 'modules') { $is_cms_table = false; - } else if ($table == MW_TABLE_PREFIX . 'elements') { + } else if ($table == get_table_prefix() . 'elements') { $is_cms_table = false; - } else if ($table == MW_TABLE_PREFIX . 'system_licenses') { + } else if ($table == get_table_prefix() . 'system_licenses') { $is_cms_table = false; - } else if ($table == MW_TABLE_PREFIX . 'stats_users_online') { + } else if ($table == get_table_prefix() . 'stats_users_online') { $is_cms_table = false; - } else if ($table == MW_TABLE_PREFIX . 'rating') { + } else if ($table == get_table_prefix() . 'rating') { $is_cms_table = false; - } else if ($table == MW_TABLE_PREFIX . 'log') { + } else if ($table == get_table_prefix() . 'log') { $is_cms_table = false; - } else if ($table == MW_TABLE_PREFIX . 'countries') { + } else if ($table == get_table_prefix() . 'countries') { $is_cms_table = false; } @@ -636,8 +636,8 @@ function create($filename = false) //$result = mysql_query('SELECT * FROM ' . $table); $qs = 'SELECT * FROM ' . $table; - if ($table == MW_TABLE_PREFIX . 'categories_items' or $table == MW_TABLE_PREFIX . 'categories') { - $qs = 'SELECT * FROM ' . $table . " where rel='content' "; + if ($table == get_table_prefix() . 'categories_items' or $table == get_table_prefix() . 'categories') { + $qs = 'SELECT * FROM ' . $table . " where rel_type='content' "; } else { $qs = 'SELECT * FROM ' . $table; } @@ -646,11 +646,11 @@ function create($filename = false) $result = mw()->database_manager->query($qs, $cache_id = false, $cache_group = false, $only_query = false, $temp_db); $num_fields = count($result[0]); //$num_fields = mysql_num_fields($result); - $table_without_prefix = $this->prefix_placeholder . str_ireplace(MW_TABLE_PREFIX, "", $table); + $table_without_prefix = $this->prefix_placeholder . str_ireplace(get_table_prefix(), "", $table); // First part of the output - remove the table //$return .= 'DROP TABLE IF EXISTS ' . $table_without_prefix . $this -> file_q_sep . "\n\n\n"; - // $return = 'DROP TABLE IF EXISTS ' . $table_without_prefix . $this->file_q_sep . "\n\n\n"; - // $this->append_string_to_file($sql_bak_file, $return); + // $return = 'DROP TABLE IF EXISTS ' . $table_without_prefix . $this->file_q_sep . "\n\n\n"; + // $this->append_string_to_file($sql_bak_file, $return); // Second part of the output - create table // $res_ch = mysql_query('SHOW CREATE TABLE ' . $table); @@ -669,14 +669,14 @@ function create($filename = false) $row2 = array_values($res_ch[0]); - $create_table_without_prefix = str_ireplace(MW_TABLE_PREFIX, $this->prefix_placeholder, $row2[1]); + $create_table_without_prefix = str_ireplace(get_table_prefix(), $this->prefix_placeholder, $row2[1]); //$return .= "\n\n" . $create_table_without_prefix . $this -> file_q_sep . "\n\n\n"; $create_table_without_prefix = str_ireplace('CREATE TABLE', 'CREATE TABLE IF NOT EXISTS', $create_table_without_prefix); $return = "\n\n" . $create_table_without_prefix . $this->file_q_sep . "\n\n\n"; - // $return = "\n\n" . $create_table_without_prefix . $this->file_q_sep . "\n\n\n"; + // $return = "\n\n" . $create_table_without_prefix . $this->file_q_sep . "\n\n\n"; $this->append_string_to_file($sql_bak_file, $return); // Third part of the output - insert values into new table //for ($i = 0; $i < $num_fields; $i++) { @@ -714,7 +714,7 @@ function create($filename = false) $this->log_action(false); $back_log_action = "Saving to file " . basename($filess); $this->log_action($back_log_action); - + $end = microtime_float(); $end = round($end - $start, 3); $this->log_action(false); @@ -748,7 +748,7 @@ function get_bakup_location() if ($loc != false) { return $loc; } - $here = MW_USERFILES . "export" . DS; + $here = userfiles_path() . "export" . DS; if (!is_dir($here)) { mkdir_recursive($here); @@ -759,9 +759,9 @@ function get_bakup_location() } } - $here = MW_USERFILES . "export" . DS . MW_TABLE_PREFIX . DS; + $here = userfiles_path() . "export" . DS . get_table_prefix() . DS; - if (!is_dir($here)) { + if (!is_dir($here)) { mkdir_recursive($here); } @@ -794,7 +794,7 @@ function create_full() $export_actions = array(); $export_actions[] = 'make_db_export'; - $userfiles_folder = MW_USERFILES; + $userfiles_folder = userfiles_path(); $media_folder = media_base_path(); @@ -811,7 +811,7 @@ function create_full() } } - + $host = (parse_url(site_url())); @@ -879,9 +879,9 @@ function create_full() $cache_state_id = 'export_zip_state'; - $this->app->cache->save($export_actions, $cache_id, 'export'); - $this->app->cache->save(false, $cache_id_loc, 'export'); - $this->app->cache->save(false, $cache_state_id, 'export'); + $this->app->cache_manager->save($export_actions, $cache_id, 'export'); + $this->app->cache_manager->save(false, $cache_id_loc, 'export'); + $this->app->cache_manager->save(false, $cache_state_id, 'export'); if (!defined('MW_NO_SESSION')) { define('MW_NO_SESSION', 1); @@ -897,18 +897,18 @@ function create_full() function log_action($back_log_action) { - if (mw_is_installed() and mw_is_installed() == true) { + if (mw_is_installed() == true) { if ($back_log_action == false) { - $this->app->log->delete("is_system=y&rel=export&user_ip=" . USER_IP); + $this->app->log_manager->delete("is_system=y&rel_type=export&user_ip=" . USER_IP); } else { - $check = $this->app->log->get("order_by=created_at desc&one=true&is_system=y&created_at=[mt]30 min ago&field=action&rel=export&user_ip=" . USER_IP); + $check = $this->app->log_manager->get("order_by=created_at desc&one=true&is_system=y&created_at=[mt]30 min ago&field=action&rel_type=export&user_ip=" . USER_IP); if (is_array($check) and isset($check['id'])) { - $this->app->log->save("is_system=y&field=action&rel=export&value=" . $back_log_action . "&user_ip=" . USER_IP . "&id=" . $check['id']); + $this->app->log_manager->save("is_system=y&field=action&rel_type=export&value=" . $back_log_action . "&user_ip=" . USER_IP . "&id=" . $check['id']); } else { - $this->app->log->save("is_system=y&field=action&rel=export&value=" . $back_log_action . "&user_ip=" . USER_IP); + $this->app->log_manager->save("is_system=y&field=action&rel_type=export&value=" . $back_log_action . "&user_ip=" . USER_IP); } } } @@ -1119,4 +1119,4 @@ function readfile_chunked($filename, $retbytes = TRUE) } -$mw_export_zip_obj = false; \ No newline at end of file +$mw_export_zip_obj = false; diff --git a/userfiles/modules/admin/developer_tools/template_exporter/log.php b/userfiles/modules/admin/developer_tools/template_exporter/log.php index a55de08..305e77e 100644 --- a/userfiles/modules/admin/developer_tools/template_exporter/log.php +++ b/userfiles/modules/admin/developer_tools/template_exporter/log.php @@ -7,11 +7,19 @@ } } -$check = mw()->log_manager->get("order_by=created_at desc&one=true&no_cache=true&is_system=y&created_at=[mt]30 min ago&field=upload_size&rel=uploader&user_ip=" . USER_IP); +$check = mw()->log_manager->get("order_by=created_at desc&one=true&no_cache=true&is_system=y&created_at=[mt]30 min ago&field=upload_size&rel_type=uploader&user_ip=" . USER_IP); $job = mw('admin/developer_tools/template_exporter/Worker')->cronjob(); -$check = mw()->log_manager->get("order_by=created_at desc&one=true&no_cache=true&is_system=y&created_at=[mt]30 min ago&field=action&rel=export&user_ip=" . USER_IP); +$check = mw()->log_manager->get("order_by=created_at desc&one=true&no_cache=true&is_system=y&created_at=[mt]30 min ago&field=action&rel_type=export&user_ip=" . USER_IP); + + + if (isset($check['value'])) { - if ($check['value'] == 'reload') { + if ($check['value'] == 'reload' or $check['value'] == 'done') { + +mw()->log_manager->delete("is_system=y&rel_type=export&user_ip=" . USER_IP); + + + ?>