/** server infomation **/ public function doIndex() { $db = $this->_mongo->selectDB("admin"); //command line $query = $db->command(array("getCmdLineOpts" => 1)); if (isset($query["argv"])) { $this->commandLine = implode(" ", $query["argv"]); } else { $this->commandLine = ""; } //web server $this->webServers = array(); if (isset($_SERVER["SERVER_SOFTWARE"])) { list($webServer) = explode(" ", $_SERVER["SERVER_SOFTWARE"]); $this->webServers["Web server"] = $webServer; } $this->webServers["<a href=\"http://www.php.net\" target=\"_blank\">PHP version</a>"] = "PHP " . PHP_VERSION; $this->webServers["<a href=\"http://www.php.net/mongo\" target=\"_blank\">PHP extension</a>"] = "<a href=\"http://pecl.php.net/package/mongo\" target=\"_blank\">mongo</a>/" . RMongo::getVersion(); $this->directives = ini_get_all("mongo"); //build info $ret = $db->command(array("buildinfo" => 1)); $this->buildInfos = array(); if ($ret["ok"]) { unset($ret["ok"]); $this->buildInfos = $ret; } //connection $this->connections = array("Host" => $this->_server->mongoHost(), "Port" => $this->_server->mongoPort(), "Username" => "******", "Password" => "******"); $this->display(); }
/** * List databases on the server * * @return array */ public function listDbs() { $dbs = array(); try { $dbs = $this->_mongo->listDBs(); } catch (Exception $e) { $dbs["ok"] = false; } if (empty($dbs["ok"])) { $user = MUser::userInSession(); $dbs = array("databases" => array(), "totalSize" => 0, "ok" => 1); foreach ($user->dbs() as $db) { $dbs["databases"][] = array("name" => $db, "empty" => false, "sizeOnDisk" => 0); } } //@todo: should we show user input databases only? $onlyDbs = $this->uiOnlyDbs(); $hideDbs = $this->uiHideDbs(); foreach ($dbs["databases"] as $index => $database) { $name = $database["name"]; if (!empty($hideDbs) && in_array($name, $hideDbs)) { unset($dbs["databases"][$index]); } if (!empty($onlyDbs) && !in_array($name, $onlyDbs)) { unset($dbs["databases"][$index]); } } return $dbs; }
/** * Export var as string then highlight it. * * @param mixed $var variable to be exported * @param string $format data format, array|json * @param boolean $label if add label to field * @return string */ protected function _highlight($var, $format = "array", $label = false) { import("classes.VarExportor"); $exportor = new VarExportor($this->_mongo->selectDB("admin"), $var); $varString = null; $highlight = true; $mixed = false; switch ($this->_server->docsRender()) { case "default": $varString = $exportor->export($format, $label); break; case "plain": $varString = $exportor->export($format, false); $label = false; $highlight = false; break; case "mixed": $varString = $exportor->export($format, false); if (strlen($varString) > $this->_server->docsRenderLimit()) { $highlight = false; } else { $highlight = true; } $label = false; $mixed = true; break; default: $varString = $exportor->export($format, $label); break; } $string = null; if ($highlight) { if ($format == "array") { $string = highlight_string("<?php " . $varString, true); $string = preg_replace("/" . preg_quote('<span style="color: #0000BB"><?php </span>', "/") . "/", '', $string, 1); } else { $string = json_format_html($varString); } } else { $string = "<div><xmp style='width:600px;overflow:auto'>" . $varString . "</xmp></div>"; } if ($label) { $id = addslashes(isset($var["_id"]) ? rock_id_string($var["_id"]) : ""); $string = preg_replace_callback("/(['\"])rockfield\\.(.+)\\.rockfield(['\"])/U", create_function('$match', ' $fields = explode(".rockfield.", $match[2]); return "<span class=\\"field\\" field=\\"" . implode(".", $fields) . "\\">" . $match[1] . array_pop($fields) . $match[3] . "</span>";'), $string); $string = preg_replace_callback("/__rockmore\\.(.+)\\.rockmore__/U", create_function('$match', ' $field = str_replace("rockfield.", "", $match[1]); return "<a href=\\"#\\" onclick=\\"fieldOpMore(\'" . $field . "\',\'' . $id . '\');return false;\\" title=\\"More text\\">[...]</a>";'), $string); } return $string; }
/** * Export var as string then highlight it. * * @param mixed $var variable to be exported * @param string $format data format, array|json * @param boolean $label if add label to field * @return string */ protected function _highlight($var, $format = "array", $label = false) { import("classes.VarExportor"); $exportor = new VarExportor($this->_mongo->selectDB("admin"), $var); $varString = $exportor->export($format, $label); $string = null; if ($format == "array") { $string = highlight_string("<?php " . $varString, true); $string = preg_replace("/" . preg_quote('<span style="color: #0000BB"><?php </span>', "/") . "/", '', $string, 1); } else { $string = json_format_html($varString); } if ($label) { $id = addslashes(isset($var["_id"]) ? rock_id_string($var["_id"]) : ""); $string = preg_replace_callback("/(['\"])rockfield\\.(.+)\\.rockfield(['\"])/U", create_function('$match', ' $fields = explode(".rockfield.", $match[2]); return "<span class=\\"field\\" field=\\"" . implode(".", $fields) . "\\">" . $match[1] . array_pop($fields) . $match[3] . "</span>";'), $string); $string = preg_replace_callback("/__rockmore\\.(.+)\\.rockmore__/U", create_function('$match', ' $field = str_replace("rockfield.", "", $match[1]); return "<a href=\\"#\\" onclick=\\"fieldOpMore(\'" . $field . "\',\'' . $id . '\');return false;\\" title=\\"More text\\">[...]</a>";'), $string); } return $string; }
/** * Save current object to MongoDB * * @param boolean $refresh Should refresh the object fields values? * @return boolean */ function save($refresh = false) { if (!$this->_collection) { import("@.RMongoException"); throw new RMongoException("Object is not in any collection, please use setCollection() to method to set a collection."); } $bool = true; if ($this->_id) { //if exists if (!empty($this->_operations)) { $bool = $this->_collection->update(array("_id" => $this->_id), $this->_operations, array("upsert" => false, "multiple" => false)); if ($refresh) { $bool = $this->refresh(); } } } else { $bool = $this->_collection->insert($this->_attrs, true); if ($bool) { $this->_id = $this->_attrs["_id"]; import("@.RMongo"); RMongo::setLastInsertId($this->_id->__toString()); } } $this->_operations = array(); return $bool; }
/** transfer db collections from one server to another **/ public function doDbTransfer() { $this->db = xn("db"); $db = $this->_mongo->selectDB($this->db); $this->collections = $db->listCollections(); $this->servers = $this->_admin->servers(); $this->selectedCollections = array(); if (!$this->isPost()) { $this->selectedCollections[] = xn("collection"); x("copy_indexes", 1); $this->target_host = ""; $this->target_sock = ""; $this->target_port = 27017; $this->target_auth = 0; $this->target_username = ""; $this->target_password = ""; } else { $this->target_host = trim(xn("target_host")); $this->target_sock = trim(xn("target_sock")); $this->target_port = xi("target_port"); $this->target_auth = xi("target_auth"); $this->target_username = trim(xn("target_username")); $this->target_password = trim(xn("target_password")); $checkeds = xn("checked"); if (is_array($checkeds)) { $this->selectedCollections = array_keys($checkeds); } if (empty($checkeds)) { $this->error = "Please select collections which you want to transfer."; $this->display(); return; } if (empty($this->target_host) && empty($this->target_sock)) { $this->error = "Target host must not be empty."; $this->display(); return; } $copyIndexes = xi("copy_indexes"); /**if ($target === "") { $this->error = "Please enter a valid database name."; $this->display(); return; }**/ //start to transfer $targetOptions = array(); if ($this->target_auth) { $targetOptions["username"] = $this->target_username; $targetOptions["password"] = $this->target_password; } $uri = null; if ($this->target_sock) { $uri = "mongodb://" . $this->target_sock; } else { $uri = "mongodb://" . $this->target_host . ":" . $this->target_port; } $targetConnection = new RMongo($uri, $targetOptions); $targetDb = $targetConnection->selectDB($this->db); if ($this->target_auth) { // "authenticate" can only be used between 1.0.1 - 1.2.11 if (RMongo::compareVersion("1.0.1") >= 0 && RMongo::compareVersion("1.2.11") < 0) { $targetDb->authenticate($this->target_username, $this->target_password); } } $errors = array(); foreach ($this->selectedCollections as $collectionName) { $ret = $targetDb->command(array("cloneCollection" => $this->db . "." . $collectionName, "from" => $this->_server->uri(), "copyIndexes" => (bool) $copyIndexes)); if (!$ret["ok"]) { $errors[] = MMongo::readException($ret); break; } } if (!empty($errors)) { $this->error = implode("<br/>", $errors); $this->display(); return; } $this->message = "All data were transfered to '{$this->target_host}' successfully."; } $this->display(); }
/** * Create collection * * @param MongoDB $db MongoDB * @param string $name Collection name * @param array $options Options, capped, size, max */ public static function createCollection(MongoDB $db, $name, array $options) { if (RMongo::compareVersion("1.4.0") >= 0) { $db->createCollection($name, $options); } else { $db->createCollection($name, isset($options["capped"]) ? $options["capped"] : false, isset($options["size"]) ? $options["size"] : 0, isset($options["max"]) ? $options["max"] : 0); } }
/** * 插入新的行,_id是上一行的ID加1 * * @param array $attrs 新行的属性集 * @return boolean */ function insertNext(array $attrs) { $response = $this->_db->execute('function insertObject(o, myCollection) { var x = db.getCollection(myCollection); while( 1 ) { // determine next _id value to try var c = x.find({},{_id:1}).sort({_id:-1}).limit(1); var i = c.hasNext() ? c.next()._id + 1 : 1; o._id = i; x.insert(o); var err = db.getLastErrorObj(); if( err && err.code ) { if( err.code == 11000 /* dup key */ ) continue; else print("unexpected error inserting data: " + tojson(err)); } break; } return o._id; }', array($attrs, $this->_collectionName)); if ($response["ok"]) { import("@.RMongo"); RMongo::setLastInsertId($response["retval"]); } return $response["ok"]; }
static function setLastInsertId($lastId) { self::$_lastId = $lastId; }