/** * Get the records in a collection * * @param string $collection * @return array */ public function listRows($collection) { foreach ($this->sort as $key => $val) { //cast vals to int $sort[$key] = (int) $val; } $col = $this->mongo->selectCollection($collection); $find = array(); if (isset($_GET['find']) && $_GET['find']) { $_GET['find'] = trim($_GET['find']); if (strpos($_GET['find'], 'array') === 0) { eval('$find = ' . $_GET['find'] . ';'); } else { if (is_string($_GET['find'])) { if ($findArr = json_decode($_GET['find'], true)) { $find = $findArr; } } } } if (isset($_GET['search']) && $_GET['search']) { switch (substr(trim($_GET['search']), 0, 1)) { //first character case '/': //regex $find[$_GET['searchField']] = new mongoRegex($_GET['search']); break; case '{': //JSON if ($search = json_decode($_GET['search'], true)) { $find[$_GET['searchField']] = $search; } break; case '(': $types = array('bool', 'boolean', 'int', 'integer', 'float', 'double', 'string', 'array', 'object', 'null', 'mongoid'); $closeParentheses = strpos($_GET['search'], ')'); if ($closeParentheses) { $cast = strtolower(substr($_GET['search'], 1, $closeParentheses - 1)); if (in_array($cast, $types)) { $search = trim(substr($_GET['search'], $closeParentheses + 1)); if ($cast == 'mongoid') { $search = new MongoID($search); } else { settype($search, $cast); } $find[$_GET['searchField']] = $search; break; } } //else no-break //else no-break default: //text-search if (strpos($_GET['search'], '*') === false) { if (!is_numeric($_GET['search'])) { $find[$_GET['searchField']] = $_GET['search']; } else { //$_GET is always a string-type $in = array((string) $_GET['search'], (int) $_GET['search'], (double) $_GET['search']); $find[$_GET['searchField']] = array('$in' => $in); } } else { //text with wildcards $regex = '/' . str_replace('\\*', '.*', preg_quote($_GET['search'])) . '/i'; $find[$_GET['searchField']] = new mongoRegex($regex); } break; } } $cols = !isset($_GET['cols']) ? array() : array_fill_keys($_GET['cols'], true); $cur = $col->find($find, $cols)->sort($sort); $this->count = $cur->count(); //get keys of first object if ($_SESSION['limit'] && $this->count > $_SESSION['limit'] && (!isset($_GET['export']) || $_GET['export'] != 'nolimit')) { if ($this->count > 1) { $this->colKeys = phpMoAdmin::getArrayKeys($col->findOne()); } $cur->limit($_SESSION['limit']); if (isset($_GET['skip'])) { if ($this->count <= $_GET['skip']) { $_GET['skip'] = $this->count - $_SESSION['limit']; } $cur->skip($_GET['skip']); } } else { if ($this->count) { // results exist but are fewer than per-page limit $this->colKeys = phpMoAdmin::getArrayKeys($cur->getNext()); } else { if ($find && $col->count()) { //query is not returning anything, get cols from first obj in collection $this->colKeys = phpMoAdmin::getArrayKeys($col->findOne()); } } } //get keys of last or much-later object if ($this->count > 1) { $curLast = $col->find()->sort($sort); if ($this->count > 2) { $curLast->skip(min($this->count, 100) - 1); } $this->colKeys = array_merge($this->colKeys, phpMoAdmin::getArrayKeys($curLast->getNext())); ksort($this->colKeys); } return $cur; }
/** * Get the records in a collection * * @param string $collection * @return array */ public function listRows($collection) { foreach ($this->sort as $key => $val) { //cast vals to int $sort[$key] = (int) $val; } $col = $this->mongo->selectCollection($collection); $this->count = $col->count(); $cur = $col->find()->sort($sort); if ($_SESSION['limit'] && $this->count > $_SESSION['limit']) { if ($this->count > 1) { $this->colKeys = phpMoAdmin::getArrayKeys($col->findOne()); } $cur->limit($_SESSION['limit']); if (isset($_GET['skip'])) { if ($this->count <= $_GET['skip']) { $_GET['skip'] = $this->count - $_SESSION['limit']; } $cur->skip($_GET['skip']); } } else { if ($this->count > 1) { $this->colKeys = phpMoAdmin::getArrayKeys($cur->getNext()); } } if ($this->count > 1) { $curLast = $col->find()->sort($sort); if ($this->count > 2) { $curLast->skip(min($this->count, 100) - 1); } $this->colKeys = array_merge($this->colKeys, phpMoAdmin::getArrayKeys($curLast->getNext())); ksort($this->colKeys); } return $cur; }