From ba45e2ff3d56bc29a7888ada73daa5d337a868c3 Mon Sep 17 00:00:00 2001 From: sergey Date: Wed, 11 Jan 2017 12:56:22 +0300 Subject: [PATCH 01/11] Split imported JS file - add new elements to the form - split js file into chunks and load them --- app/controllers/db.php | 37 +++++++++++++++++++++++++++- themes/default/views/db/dbImport.php | 6 +++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/app/controllers/db.php b/app/controllers/db.php index 46b90ea..166eb59 100644 --- a/app/controllers/db.php +++ b/app/controllers/db.php @@ -260,6 +260,10 @@ public function doDbImport() { if ($this->isPost()) { $format = x("format"); + $split_js = x("split_js"); + $split_max = x("split_max"); + if (!$split_max) $split_max = 1000; + if (!empty($_FILES["json"]["tmp_name"])) { $tmp = $_FILES["json"]["tmp_name"]; @@ -276,7 +280,38 @@ public function doDbImport() { $ret = array("ok" => 0); if ($format == "js") { - $ret = $this->_mongo->selectDB($this->db)->execute('function (){ ' . $body . ' }'); + + if ($split_js) { + + $body = explode('db.getCollection(', $body); + $chunks = array_chunk($body, $split_max); + + $is_ok = true; + $error_str = ''; + + foreach($chunks as $body_chunk) { + + $chunk_str = 'db.getCollection(' . implode('db.getCollection(', $body_chunk); + + $ret = $this->_mongo->selectDB($this->db)->execute('function (){ ' . $chunk_str . ' }'); + + if (!$ret["ok"]) { + $error_str .= $ret["errmsg"] . PHP_EOL; + $is_ok = false; + } + + } + + if (!$is_ok) { + $ret["ok"] = false + $ret["errmsg"] = $error_str; + } + + } else { + + $ret = $this->_mongo->selectDB($this->db)->execute('function (){ ' . $body . ' }'); + + } if (!$ret["ok"]) { $this->error = $ret["errmsg"]; diff --git a/themes/default/views/db/dbImport.php b/themes/default/views/db/dbImport.php index 1466deb..2d937b3 100644 --- a/themes/default/views/db/dbImport.php +++ b/themes/default/views/db/dbImport.php @@ -12,7 +12,13 @@
+ JS File:
+ + +
+
+ "/>
From b5f4461573bb36a654770e0642a4e91c62d5ee42 Mon Sep 17 00:00:00 2001 From: sergey Date: Wed, 11 Jan 2017 12:59:44 +0300 Subject: [PATCH 02/11] Fix typo --- app/controllers/db.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/db.php b/app/controllers/db.php index 166eb59..ed1a1a5 100644 --- a/app/controllers/db.php +++ b/app/controllers/db.php @@ -303,7 +303,7 @@ public function doDbImport() { } if (!$is_ok) { - $ret["ok"] = false + $ret["ok"] = false; $ret["errmsg"] = $error_str; } From 648757efa07bd395e92f0188d6b27c8722c61efe Mon Sep 17 00:00:00 2001 From: sergey Date: Wed, 11 Jan 2017 13:02:44 +0300 Subject: [PATCH 03/11] Add descriptions. --- themes/default/views/db/dbImport.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/themes/default/views/db/dbImport.php b/themes/default/views/db/dbImport.php index 2d937b3..c67488f 100644 --- a/themes/default/views/db/dbImport.php +++ b/themes/default/views/db/dbImport.php @@ -14,10 +14,12 @@ JS File:
+Support gzipped (.js.gz) files.



+Useful if file more than 4mb (2.x mongo) or 16mb (3.x mongo).
"/> From dd68ac095a01f9624c57cd58fa549f76a52b6f97 Mon Sep 17 00:00:00 2001 From: sergey Date: Wed, 11 Jan 2017 13:05:51 +0300 Subject: [PATCH 04/11] Fix label for checkbox --- themes/default/views/db/dbImport.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/themes/default/views/db/dbImport.php b/themes/default/views/db/dbImport.php index c67488f..eb3ad0e 100644 --- a/themes/default/views/db/dbImport.php +++ b/themes/default/views/db/dbImport.php @@ -17,7 +17,7 @@ Support gzipped (.js.gz) files.

-
+

Useful if file more than 4mb (2.x mongo) or 16mb (3.x mongo).
From 73dd4f96a40ba2587c44afca2a8b19a1e889d93d Mon Sep 17 00:00:00 2001 From: sergey Date: Wed, 11 Jan 2017 13:25:26 +0300 Subject: [PATCH 05/11] Update - gzip, form - Use gzencode / gzdecode to be compatible with gzip program. - Use range form input for split number --- app/controllers/db.php | 21 ++++++++++++++------- themes/default/views/db/dbImport.php | 7 ++++++- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/app/controllers/db.php b/app/controllers/db.php index ed1a1a5..02c43f5 100644 --- a/app/controllers/db.php +++ b/app/controllers/db.php @@ -238,7 +238,7 @@ public function doDbExport() { ob_end_clean(); header("Content-type: application/x-gzip"); header("Content-Disposition: attachment; filename=\"{$prefix}.gz\""); - echo gzcompress($this->contents, 9); + echo gzencode($this->contents, 9); exit(); } else { @@ -268,12 +268,19 @@ public function doDbImport() { $tmp = $_FILES["json"]["tmp_name"]; //read file by it's format - $body = ""; + $body = file_get_contents($tmp); if (preg_match("/\\.gz$/", $_FILES["json"]["name"])) { - $body = gzuncompress(file_get_contents($tmp)); - } - else { - $body = file_get_contents($tmp); + $_body = @gzdecode($body); + if (!$_body) { + $_body = @gzuncompress($body); + } + if (!$_body) { + $this->error = "Can't decompress file!"; + $body = ''; + } else { + $body = $_body; + unset $_body; + } } //check format @@ -303,7 +310,7 @@ public function doDbImport() { } if (!$is_ok) { - $ret["ok"] = false; + $ret["ok"] = 0; $ret["errmsg"] = $error_str; } diff --git a/themes/default/views/db/dbImport.php b/themes/default/views/db/dbImport.php index eb3ad0e..76cdea6 100644 --- a/themes/default/views/db/dbImport.php +++ b/themes/default/views/db/dbImport.php @@ -18,7 +18,12 @@
-
+ 1000
+ Useful if file more than 4mb (2.x mongo) or 16mb (3.x mongo).
"/> From befda57cb74dddc9c8ec868fccfc7709af735b73 Mon Sep 17 00:00:00 2001 From: sergey Date: Wed, 11 Jan 2017 13:29:47 +0300 Subject: [PATCH 06/11] Fix unset --- app/controllers/db.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/db.php b/app/controllers/db.php index 02c43f5..ae84709 100644 --- a/app/controllers/db.php +++ b/app/controllers/db.php @@ -279,7 +279,7 @@ public function doDbImport() { $body = ''; } else { $body = $_body; - unset $_body; + unset($_body); } } From 1fab013362baa05e11fe4a840e552b26f563086d Mon Sep 17 00:00:00 2001 From: sergey Date: Wed, 11 Jan 2017 13:37:27 +0300 Subject: [PATCH 07/11] Add more error output for chunks --- app/controllers/db.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/db.php b/app/controllers/db.php index ae84709..31cfc36 100644 --- a/app/controllers/db.php +++ b/app/controllers/db.php @@ -303,7 +303,7 @@ public function doDbImport() { $ret = $this->_mongo->selectDB($this->db)->execute('function (){ ' . $chunk_str . ' }'); if (!$ret["ok"]) { - $error_str .= $ret["errmsg"] . PHP_EOL; + $error_str .= $ret["errmsg"] . '
Chunk with error:
' . PHP_EOL . $chunk_str . PHP_EOL; $is_ok = false; } From 4813dc5113be456c09289bec03e201597172ba58 Mon Sep 17 00:00:00 2001 From: sergey Date: Wed, 11 Jan 2017 13:50:02 +0300 Subject: [PATCH 08/11] Filter js chunks from garbage --- app/controllers/db.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/app/controllers/db.php b/app/controllers/db.php index 31cfc36..9115905 100644 --- a/app/controllers/db.php +++ b/app/controllers/db.php @@ -291,6 +291,17 @@ public function doDbImport() { if ($split_js) { $body = explode('db.getCollection(', $body); + + function filter_js_chunk($item) { + if (strpos($item, ').insert({') !== false) { + return true; + } + return false; + } + + $body = array_filter($body, "filter_js_chunk"); + + $chunks = array_chunk($body, $split_max); $is_ok = true; From 0104edb73b552c8f0f4bd57431f953e01bb1e46f Mon Sep 17 00:00:00 2001 From: Sergey Dryabzhinsky Date: Fri, 26 May 2017 21:13:36 +0300 Subject: [PATCH 09/11] Revert "Split import js file" --- app/controllers/db.php | 67 +++------------------------- themes/default/views/db/dbImport.php | 13 ------ 2 files changed, 7 insertions(+), 73 deletions(-) diff --git a/app/controllers/db.php b/app/controllers/db.php index 9115905..46b90ea 100644 --- a/app/controllers/db.php +++ b/app/controllers/db.php @@ -238,7 +238,7 @@ public function doDbExport() { ob_end_clean(); header("Content-type: application/x-gzip"); header("Content-Disposition: attachment; filename=\"{$prefix}.gz\""); - echo gzencode($this->contents, 9); + echo gzcompress($this->contents, 9); exit(); } else { @@ -260,76 +260,23 @@ public function doDbImport() { if ($this->isPost()) { $format = x("format"); - $split_js = x("split_js"); - $split_max = x("split_max"); - if (!$split_max) $split_max = 1000; - if (!empty($_FILES["json"]["tmp_name"])) { $tmp = $_FILES["json"]["tmp_name"]; //read file by it's format - $body = file_get_contents($tmp); + $body = ""; if (preg_match("/\\.gz$/", $_FILES["json"]["name"])) { - $_body = @gzdecode($body); - if (!$_body) { - $_body = @gzuncompress($body); - } - if (!$_body) { - $this->error = "Can't decompress file!"; - $body = ''; - } else { - $body = $_body; - unset($_body); - } + $body = gzuncompress(file_get_contents($tmp)); + } + else { + $body = file_get_contents($tmp); } //check format $ret = array("ok" => 0); if ($format == "js") { - - if ($split_js) { - - $body = explode('db.getCollection(', $body); - - function filter_js_chunk($item) { - if (strpos($item, ').insert({') !== false) { - return true; - } - return false; - } - - $body = array_filter($body, "filter_js_chunk"); - - - $chunks = array_chunk($body, $split_max); - - $is_ok = true; - $error_str = ''; - - foreach($chunks as $body_chunk) { - - $chunk_str = 'db.getCollection(' . implode('db.getCollection(', $body_chunk); - - $ret = $this->_mongo->selectDB($this->db)->execute('function (){ ' . $chunk_str . ' }'); - - if (!$ret["ok"]) { - $error_str .= $ret["errmsg"] . '
Chunk with error:
' . PHP_EOL . $chunk_str . PHP_EOL; - $is_ok = false; - } - - } - - if (!$is_ok) { - $ret["ok"] = 0; - $ret["errmsg"] = $error_str; - } - - } else { - - $ret = $this->_mongo->selectDB($this->db)->execute('function (){ ' . $body . ' }'); - - } + $ret = $this->_mongo->selectDB($this->db)->execute('function (){ ' . $body . ' }'); if (!$ret["ok"]) { $this->error = $ret["errmsg"]; diff --git a/themes/default/views/db/dbImport.php b/themes/default/views/db/dbImport.php index 76cdea6..1466deb 100644 --- a/themes/default/views/db/dbImport.php +++ b/themes/default/views/db/dbImport.php @@ -12,20 +12,7 @@
- JS File:
-Support gzipped (.js.gz) files.

- - -
- 1000
- -Useful if file more than 4mb (2.x mongo) or 16mb (3.x mongo).
- "/>
From 6f85f95e0645482f6fabaacb1900bc83d0bd14b5 Mon Sep 17 00:00:00 2001 From: Sergey Dryabzhinsky Date: Fri, 26 May 2017 21:30:43 +0300 Subject: [PATCH 10/11] Issue #4 - use gzencode / gzdecode to be compatible with console gzip - add gzdecode to be compatible with php versions < 5.4 --- app/controllers/db.php | 13 ++++------ app/funcs/functions.php | 8 ++++++ config.php | 55 ----------------------------------------- 3 files changed, 13 insertions(+), 63 deletions(-) delete mode 100644 config.php diff --git a/app/controllers/db.php b/app/controllers/db.php index 46b90ea..366d56b 100644 --- a/app/controllers/db.php +++ b/app/controllers/db.php @@ -237,8 +237,8 @@ public function doDbExport() { if (x("gzip")) { ob_end_clean(); header("Content-type: application/x-gzip"); - header("Content-Disposition: attachment; filename=\"{$prefix}.gz\""); - echo gzcompress($this->contents, 9); + header("Content-Disposition: attachment; filename=\"{$prefix}.js.gz\""); + echo gzencode($this->contents, 9); exit(); } else { @@ -264,12 +264,9 @@ public function doDbImport() { $tmp = $_FILES["json"]["tmp_name"]; //read file by it's format - $body = ""; - if (preg_match("/\\.gz$/", $_FILES["json"]["name"])) { - $body = gzuncompress(file_get_contents($tmp)); - } - else { - $body = file_get_contents($tmp); + $body = file_get_contents($tmp); + if (preg_match("/\\.js\\.gz$/", $_FILES["json"]["name"])) { + $body = gzdecode($body); } //check format diff --git a/app/funcs/functions.php b/app/funcs/functions.php index 7a6c269..2c896f5 100644 --- a/app/funcs/functions.php +++ b/app/funcs/functions.php @@ -280,4 +280,12 @@ function r_get_collection_icon($collectionName) { return "table"; } +// PHP < 5.4 +if (!function_exists('gzdecode')) { + // http://php.net/manual/ru/function.gzdecode.php#106397 + function gzdecode($data) { + return gzinflate(substr($data,10,-8)); + } +} + ?> \ No newline at end of file diff --git a/config.php b/config.php deleted file mode 100644 index 5dd3a9f..0000000 --- a/config.php +++ /dev/null @@ -1,55 +0,0 @@ - \ No newline at end of file From c31a320d4c2328f196b026db9949d07d2df90636 Mon Sep 17 00:00:00 2001 From: Sergey Dryabzhinsky Date: Fri, 26 May 2017 21:31:39 +0300 Subject: [PATCH 11/11] Missing gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1a118b8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +/logs/* +/config.php