/** * Get a user by id. * * @param string $id The user id. * @param bool $idlib If the id library should be used. * @param bool $justOne If only one entry should be returned. * * @return array The user found as an array. */ protected function get($id, $idlib = true, $justOne = true) { if ($idlib) { $keys = Id::dissectKeys($id, 'user'); $query = array('username' => $this->clean($keys['username'])); } else { $query = array('_id' => $this->_toMongoId($id)); } if ($justOne) { return $this->resolveDeps($this->db->findOne($query)); } $users = iterator_to_array($this->db->find($query)); foreach ($users as $key => $user) { $users[$key] = $this->resolveCerts($user); } return $users; }
/** * Get an article(s). * * @param string $id Article id. * @param bool $idlib True if the Id library should be used (False for MongoIds) * @param bool $justOne True if only one entry should be returned. * @param bool $fixUTF8 True if UTF8 should be decoded. * * @return mixed The article/articles as an array, or an error string. */ protected function get($id, $idlib = true, $justOne = false, $fixUTF8 = true, $page = 1, $limit = self::PER_PAGE) { $query = array('published' => true, 'ghosted' => false); if ($idlib) { $keys = Id::dissectKeys($id, 'article'); $query['date'] = array('$gte' => $keys['date'], '$lte' => $keys['date'] + $keys['ambiguity']); } else { $query['_id'] = $this->_toMongoId($id); } $results = $this->db->find($query)->skip(($page - 1) * self::PER_PAGE)->sort(array('date' => -1)); $total = $results->count(); $valid = array(); if ($limit != null) { $results->limit($limit); } if ($idlib) { foreach ($results as $result) { if (!Id::validateHash($id, array('ambiguity' => $keys['ambiguity'], 'reportedDate' => $keys['date'], 'date' => $result['date'], 'title' => $result['title']), 'news')) { continue; } array_push($valid, $result); } } else { $valid = iterator_to_array($results); } if ($justOne) { $valid = array(reset($valid)); } if (empty($valid)) { return array('Invalid id.', 0); } $comments = new comments(ConnectionFactory::get('mongo')); foreach ($valid as $key => $entry) { $this->resolveUser($valid[$key]['user']); if ($fixUTF8) { $this->resolveUTF8($valid[$key]); } $valid[$key]['rating'] = $this->getScore($entry['_id']); $valid[$key]['comments'] = $comments->getCount($entry['_id']); } if ($justOne) { return reset($valid); } return array($valid, $total); }
/** * Gets a bug. * * @param string $id Bug id. * @param bool $idlib True if the Id library should be used (False for MongoIds) * @param bool $justOne True if only one entry should be returned. * @param bool $fixUTF8 True if UTF8 should be decoded. * * @return mixed The bug/bugs as an array, or an error string. */ protected function get($id, $idlib = true, $justOne = false, $fixUTF8 = true, $page = 1, $limit = self::PER_PAGE) { $query = array('ghosted' => false); if ($idlib) { $keys = Id::dissectKeys($id, 'bugs'); $query['created'] = (int) $keys['time']; } else { $query['_id'] = $this->_toMongoId($id); } $results = $this->db->find($query)->skip(($page - 1) * self::PER_PAGE); $total = $results->count(); $valid = array(); if ($limit != null) { $results->limit($limit); } if ($idlib) { foreach ($results as $result) { if (!Id::validateHash($id, array('_id' => $result['_id'], 'created' => $result['created']), 'bugs')) { continue; } array_push($valid, $result); } } else { $valid = iterator_to_array($results); } if ($justOne) { $valid = array(reset($valid)); } if (empty($valid) || $total == 0) { return array('Invalid id.', 0); } foreach ($valid as $key => $entry) { $this->resolveUser($valid[$key]['reporter']); if ($fixUTF8) { $this->resolveUTF8($valid[$key]); } } if ($justOne) { return reset($valid); } return array($valid, $total); }