/** * Get a news post. * * @param string $id The news 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 news post 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, 'news'); $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]['comments'] = $comments->getCount($entry['_id']); $valid[$key]['rating'] = $this->getScore($entry['_id']); } if ($justOne) { return reset($valid); } return array($valid, $total); }