Example #1
0
 /** db profiling **/
 public function doProfile()
 {
     $this->db = xn("db");
     import("lib.mongo.RQuery");
     import("lib.page.RPageStyle1");
     $query = new RQuery($this->_mongo, $this->db, "system.profile");
     $page = new RPageStyle1();
     $page->setTotal($query->count());
     $page->setSize(10);
     $page->setAutoQuery();
     $this->page = $page;
     $this->rows = $query->offset($page->offset())->limit($page->size())->desc("ts")->findAll();
     foreach ($this->rows as $index => $row) {
         $this->rows[$index]["text"] = $this->_highlight($row, "json");
     }
     $this->display();
 }
    /** show one collection **/
    public function doIndex()
    {
        $this->db = xn("db");
        $this->collection = xn("collection");
        //selected format last time
        $this->last_format = rock_cookie("rock_format", "json");
        //write query to log
        $params = xn();
        if ($this->_logQuery && count($params) > 3) {
            //not only "action", "db" and "collection"
            $logDir = dirname(__ROOT__) . DS . "logs";
            if (!empty($params["criteria"]) && strlen(trim($params["criteria"], "{} \t\n\r")) > 0) {
                if (is_writable($logDir)) {
                    $logFile = $this->_logFile($this->db, $this->collection);
                    $fp = null;
                    if (!is_file($logFile)) {
                        $fp = fopen($logFile, "a+");
                        fwrite($fp, '<?php exit("Permission Denied"); ?>' . "\n");
                    } else {
                        $fp = fopen($logFile, "a+");
                    }
                    fwrite($fp, date("Y-m-d H:i:s") . "\n" . var_export($params, true) . "\n================\n");
                    fclose($fp);
                }
            }
        }
        //information
        $db = $this->_mongo->selectDB($this->db);
        $info = MCollection::info($db, $this->collection);
        $this->canAddField = !$info["capped"];
        //field and sort
        $fields = xn("field");
        //order fields
        $orders = xn("order");
        //order type:asc|desc
        if (empty($fields)) {
            if (!$this->_server->docsNatureOrder()) {
                $fields = array($info["capped"] ? "\$natural" : "_id", "", "", "");
            } else {
                $fields = array();
            }
            $orders = array("desc", "asc", "asc", "asc");
            x("field", $fields);
            x("order", $orders);
        }
        //format
        $format = x("format");
        if (!$format) {
            $format = $this->last_format;
            x("format", $format);
        }
        //remember last format choice
        $this->last_format = $format;
        $this->_rememberFormat($format);
        //read fields from collection
        import("models.MCollection");
        $this->nativeFields = MCollection::fields($db, $this->collection);
        $this->queryFields = x("query_fields");
        if (!is_array($this->queryFields)) {
            $this->queryFields = array();
        }
        $this->indexFields = $db->selectCollection($this->collection)->getIndexInfo();
        $this->recordsCount = $db->selectCollection($this->collection)->count();
        foreach ($this->indexFields as $index => $indexField) {
            $this->indexFields[$index]["keystring"] = $this->_encodeJson($indexField["key"]);
        }
        $this->queryHints = x("query_hints");
        if (!is_array($this->queryHints)) {
            $this->queryHints = array();
        }
        //new obj in modification
        $newobj = trim(xn("newobj"));
        if (!$newobj) {
            if ($format == "array") {
                x("newobj", 'array(
	\'$set\' => array (
		//your attributes
	)
)');
            } else {
                x("newobj", '{
	\'$set\': {
		//your attributes
	}
}');
            }
        }
        //conditions
        $native = xn("criteria");
        $criteria = $native;
        if (empty($criteria)) {
            $criteria = array();
            if ($format == "array") {
                $native = "array(\n\t\n)";
            } else {
                if ($format == "json") {
                    $native = '{

}';
                }
            }
            x("pagesize", 10);
        } else {
            $row = null;
            $eval = new VarEval($criteria, $format, $db);
            $row = $eval->execute();
            if (is_object($row)) {
                $row = get_object_vars($row);
            }
            if (!is_array($row)) {
                $this->message = "Criteria must be a valid " . ($format == "json" ? "JSON object" : "array");
                $this->jsonLink = "#";
                $this->arrayLink = "#";
                $this->display();
                return;
            }
            $criteria = $row;
        }
        //remember criteria in cookie (may be replaced by query history some day)
        //setcookie("criteria_" . $this->db . "__" . $this->collection . "__" . $format, $native, time() + );
        x("criteria", $native);
        import("lib.mongo.RQuery");
        $query = new RQuery($this->_mongo, $this->db, $this->collection);
        $query->cond($criteria);
        //sort
        $realOrderedFields = array();
        $realOrderedOrders = array();
        foreach ($fields as $index => $field) {
            if (!empty($field)) {
                $realOrderedFields[] = $field;
                if ($orders[$index] == "asc") {
                    $realOrderedOrders[] = "asc";
                    $query->asc($field);
                } else {
                    $realOrderedOrders[] = "desc";
                    $query->desc($field);
                }
            }
        }
        x("field", $realOrderedFields);
        x("order", $realOrderedOrders);
        //command
        $command = x("command");
        if (!$command) {
            $command = "findAll";
            x("command", $command);
        }
        $limit = xi("limit");
        if ($limit > 0) {
            $query->limit($limit);
        }
        $count = $limit > 0 && $command == "findAll" ? $query->count(true) : $query->count();
        switch ($command) {
            case "findAll":
                if (!empty($this->queryFields)) {
                    $query->result($this->queryFields);
                }
                if (!empty($this->queryHints)) {
                    foreach ($this->indexFields as $index) {
                        if (in_array($index["name"], $this->queryHints)) {
                            $query->hint($index["key"]);
                        }
                    }
                }
                break;
            case "remove":
                $microtime = microtime(true);
                $query->delete();
                $this->cost = microtime(true) - $microtime;
                break;
            case "modify":
                $microtime = microtime(true);
                $row = null;
                $newobj = xn("newobj");
                $eval = new VarEval($newobj, $format, $db);
                $row = $eval->execute();
                if (is_array($row)) {
                    $query->upsert($row);
                }
                $this->cost = microtime(true) - $microtime;
                break;
        }
        //construct links
        if ($format == "json") {
            $params = xn();
            unset($params["newobj"]);
            $exportor = new VarExportor($db, $criteria);
            $params["format"] = "array";
            $params["criteria"] = $exportor->export();
            $this->arrayLink = $this->path($this->action(), $params);
            $params = xn();
            unset($params["newobj"]);
            $params["format"] = "json";
            $this->jsonLink = $this->path($this->action(), $params);
        } else {
            if ($format == "array") {
                $params = xn();
                unset($params["newobj"]);
                $params["format"] = "array";
                $this->arrayLink = $this->path($this->action(), $params);
                $params = xn();
                unset($params["newobj"]);
                $params["format"] = "json";
                if (empty($criteria)) {
                    $params["criteria"] = "{\n\t\n}";
                } else {
                    $exportor = new VarExportor($db, $criteria);
                    $params["criteria"] = $exportor->export(MONGO_EXPORT_JSON);
                }
                $this->jsonLink = $this->path($this->action(), $params);
            }
        }
        if ($command != "findAll") {
            $this->count = $count;
            $this->display();
            return;
        }
        //pagination
        $pagesize = xi("pagesize");
        if ($pagesize < 1) {
            $pagesize = 10;
        }
        import("lib.page.RPageStyle1");
        $page = new RPageStyle1();
        $page->setTotal($count);
        $page->setSize($pagesize);
        $page->setAutoQuery();
        $this->page = $page;
        $query->offset($page->offset());
        if ($limit > 0) {
            $query->limit(min($limit, $page->size()));
        } else {
            $query->limit($page->size());
        }
        $microtime = microtime(true);
        $this->rows = $query->findAll(true);
        $this->cost = microtime(true) - $microtime;
        foreach ($this->rows as $index => $row) {
            $native = $row;
            $exportor = new VarExportor($query->db(), $native);
            $row["text"] = $exportor->export($format);
            $row["data"] = $this->_highlight($native, $format, true);
            $row["can_delete"] = isset($row["_id"]) && !$info["capped"];
            $row["can_modify"] = isset($row["_id"]);
            $row["can_duplicate"] = isset($row["_id"]);
            $row["can_add_field"] = isset($row["_id"]) && !$info["capped"];
            $row["can_refresh"] = isset($row["_id"]);
            $this->rows[$index] = $row;
        }
        $this->display();
    }