/** show dbs in left frame **/ public function doDbs() { $dbs = $this->_server->listDbs(); $this->dbs = array_values(rock_array_sort($dbs["databases"], "name")); $this->baseUrl = $this->path("admin.dbs"); $this->tableUrl = $this->path("collection.index"); $this->showDbSelector = false; //add collection count foreach ($this->dbs as $index => $db) { $collectionCount = count(MDb::listCollections($this->_mongo->selectDB($db["name"]))); $db["collectionCount"] = $collectionCount; if (isset($db["sizeOnDisk"])) { $db["size"] = round($db["sizeOnDisk"] / 1024 / 1024, 2); //M } $this->dbs[$index] = $db; } //current db $db = x("db"); $this->tables = array(); if ($db) { $mongodb = $this->_mongo->selectDB($db); $tables = MDb::listCollections($mongodb); foreach ($tables as $table) { $this->tables[$table->getName()] = $table->count(); } } $this->display(); }
/** show databases **/ public function doDatabases() { $ret = $this->_server->listDbs(); $this->dbs = $ret["databases"]; foreach ($this->dbs as $index => $db) { $mongodb = $this->_mongo->selectDB($db["name"]); $ret = $mongodb->command(array("dbstats" => 1)); $ret["collections"] = count(MDb::listCollections($mongodb)); if (isset($db["sizeOnDisk"])) { $ret["diskSize"] = $this->_formatBytes($db["sizeOnDisk"]); $ret["dataSize"] = $this->_formatBytes($ret["dataSize"]); } else { $ret["diskSize"] = "-"; $ret["dataSize"] = "-"; } $ret["storageSize"] = $this->_formatBytes($ret["storageSize"]); $ret["indexSize"] = $this->_formatBytes($ret["indexSize"]); $this->dbs[$index] = array_merge($this->dbs[$index], $ret); } $this->dbs = rock_array_sort($this->dbs, "name"); $this->display(); }
/** export db **/ public function doDbExport() { $this->db = xn("db"); $db = $this->_mongo->selectDB($this->db); $this->collections = MDb::listCollections($db); $this->selectedCollections = array(); if (!$this->isPost()) { $this->selectedCollections[] = xn("collection"); } else { $checkeds = xn("checked"); $canDownload = xn("can_download"); if (is_array($checkeds)) { $this->selectedCollections = array_keys($checkeds); } sort($this->selectedCollections); import("classes.VarExportor"); $this->contents = ""; $this->countRows = 0; //indexes foreach ($this->selectedCollections as $collection) { $collObj = $db->selectCollection($collection); $infos = $collObj->getIndexInfo(); foreach ($infos as $info) { $options = array(); if (isset($info["unique"])) { $options["unique"] = $info["unique"]; } $exportor = new VarExportor($db, $info["key"]); $exportor2 = new VarExportor($db, $options); $this->contents .= "\n/** {$collection} indexes **/\ndb.getCollection(\"" . addslashes($collection) . "\").ensureIndex(" . $exportor->export(MONGO_EXPORT_JSON) . "," . $exportor2->export(MONGO_EXPORT_JSON) . ");\n"; } } //data foreach ($this->selectedCollections as $collection) { $cursor = $db->selectCollection($collection)->find(); $this->contents .= "\n/** " . $collection . " records **/\n"; foreach ($cursor as $one) { $this->countRows++; $exportor = new VarExportor($db, $one); $this->contents .= "db.getCollection(\"" . addslashes($collection) . "\").insert(" . $exportor->export(MONGO_EXPORT_JSON) . ");\n"; unset($exportor); } unset($cursor); } if (x("can_download")) { $prefix = "mongo-" . urlencode($this->db) . "-" . date("Ymd-His"); //gzip if (x("gzip")) { ob_end_clean(); header("Content-type: application/x-gzip"); header("Content-Disposition: attachment; filename=\"{$prefix}.gz\""); echo gzcompress($this->contents, 9); exit; } else { ob_end_clean(); header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=\"{$prefix}.js\""); echo $this->contents; exit; } } } $this->display(); }
/** rename collection **/ public function doCollectionRename() { $this->db = xn("db"); $this->collection = xn("collection"); $this->realName = $this->collection; if ($this->isPost()) { $oldname = trim(xn("oldname")); $newname = trim(xn("newname")); $removeExists = trim(xn("remove_exists")); if ($newname === "") { $this->error = "Please enter a new name."; $this->display(); return; } if (!$removeExists) { //Is there a same name? $collections = MDb::listCollections($this->_mongo->selectDB($this->db)); foreach ($collections as $collection) { if ($collection->getName() == $newname) { $this->error = "There is already a '{$newname}' collection, you should drop it before renaming."; $this->display(); return; } } } $this->ret = $this->_mongo->selectDB($this->db)->execute('function (coll, newname, dropExists) { db.getCollection(coll).renameCollection(newname, dropExists);}', array($oldname, $newname, (bool) $removeExists)); if ($this->ret["ok"]) { $this->realName = $newname; $this->message = "Operation success. <a href=\"?action=collection.index&db={$this->db}&collection={$newname}\">[GO »]</a>"; } else { $this->error = "Operation failure"; } $this->ret_json = $this->_highlight($this->ret, "json"); } $this->display(); }
/** export db **/ public function doDbExport() { if (x("format") == "delete") { $file = BACKUP_DIR . '/' . $_POST["name"]; //你要删除的文件目录 if (file_exists($file)) { $result = @unlink($file); //unlink函数删除文件用的 if ($result == true) { //echo '文件已经成功删除'; } else { //echo '文件无法删除'; } } else { //echo("文件不存在"); } $this->display(); return; } global $MONGO; $MONGO['data']['MongoBinData'] = 'info'; $this->db = xn("db"); $db = $this->_mongo->selectDB($this->db); $this->collections = MDb::listCollections($db); $this->selectedCollections = array(); if (!$this->isPost()) { $this->selectedCollections[] = xn("collection"); } else { $checkeds = xn("checked"); $canDownload = xn("can_download"); if (is_array($checkeds)) { $this->selectedCollections = array_keys($checkeds); } sort($this->selectedCollections); import("classes.VarExportor"); $prefix = "mongo-" . urlencode($this->db) . "-" . date("Ymd-His"); //将文件放到服务器上 $file_path = BACKUP_DIR . '/' . $prefix . '.js'; //indexes foreach ($this->selectedCollections as $collection) { $collObj = $db->selectCollection($collection); $infos = $collObj->getIndexInfo(); foreach ($infos as $info) { $options = array(); if (isset($info["unique"])) { $options["unique"] = $info["unique"]; } $exportor = new VarExportor($db, $info["key"]); $exportor2 = new VarExportor($db, $options); $contents = "\n/** {$collection} indexes **/\ndb.getCollection(\"" . addslashes($collection) . "\").ensureIndex(" . $exportor->export(MONGO_EXPORT_JSON) . "," . $exportor2->export(MONGO_EXPORT_JSON) . ");\n"; file_put_contents($file_path, $contents, FILE_APPEND); unset($exportor); unset($exportor2); } } $countRows = 0; //data foreach ($this->selectedCollections as $collection) { $contents = "\n/** " . $collection . " records **/\n"; file_put_contents($file_path, $contents, FILE_APPEND); $cursor = $db->selectCollection($collection)->find(); foreach ($cursor as $one) { $countRows++; $exportor = new VarExportor($db, $one); $contents = "db.getCollection(\"" . addslashes($collection) . "\").insert(" . $exportor->export(MONGO_EXPORT_JSON) . ");\n\n"; file_put_contents($file_path, $contents, FILE_APPEND); unset($exportor); } unset($cursor); } if (x("can_download")) { //gzip if (x("gzip")) { ob_end_clean(); header("Content-type: application/x-gzip"); header("Content-Disposition: attachment; filename=\"{$prefix}.gz\""); $str = file_get_contents($file_path); if ($str) { echo gzcompress($str, 9); } @unlink($file_path); //echo gzcompress($this->contents, 9); exit; } else { ob_end_clean(); header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=\"{$prefix}.js\""); readfile($file_path); @unlink($file_path); //echo $this->contents; exit; } } elseif (x("butts")) { //error_log(json_encode(array('GET'=>$_GET,'POST'=>$_POST))); $myfile = fopen(BACKUP_DIR . '/' . $prefix . '.txt', "w") or die("Unable to open file!"); fwrite($myfile, json_encode($_POST['checked'])); fclose($myfile); } } $this->display(); }