public function testParseTopic() { $topicContent = file_get_contents(__DIR__ . '/../InputSamples/forum-topic-516059.html'); $topic = ForumParser::parseTopic($topicContent); //Some sanity checking on the pages and main content $this->assertInternalType('array', $topic); $this->assertGreaterThan(0, $topic['pages']); $this->assertInternalType('array', $topic['list']); $comment = $topic['list'][0]; //Some sanity checking on the topic details $this->assertInstanceOf('Atarashii\\APIBundle\\Model\\Forum', $comment); $this->assertInternalType('int', $comment->getId()); $this->assertInternalType('string', $comment->getUsername()); $this->assertInternalType('string', $comment->getComment()); $this->assertInstanceOf('\\DateTime', new \DateTime($comment->getTime())); //Some sanity checking on profile class in the topic details $this->assertInstanceOf('Atarashii\\APIBundle\\Model\\Profile', $comment->profile); $this->assertInternalType('string', $comment->profile->getAvatarUrl()); $this->assertInternalType('string', $comment->profile->details->getStatus()); $this->assertInstanceOf('\\DateTime', new \DateTime($comment->profile->details->getJoinDate())); $this->assertInternalType('string', $comment->profile->details->getAccessRank()); $this->assertInternalType('string', $comment->profile->details->getForumPosts()); }
/** * Get the topic of MAL. * * @param Request $request HTTP Request object * @param int $id The ID of the forum topic as assigned by MyAnimeList * * @return View */ public function getForumTopicAction(Request $request, $id) { // http://myanimelist.net/forum/?topicid=#{id} $page = (int) $request->query->get('page'); if ($page <= 0) { $page = 1; } if ((int) $id == '') { return $this->view(array('error' => 'Invalid topic ID'), 200); } $downloader = $this->get('atarashii_api.communicator'); try { $forumcontent = $downloader->fetch('/forum/?topicid=' . $id . '&show=' . ($page * 50 - 50)); } catch (Exception\CurlException $e) { return $this->view(array('error' => 'network-error'), 500); } $forumtopic = ForumParser::parseTopic($forumcontent); $response = new Response(); $response->setPublic(); $response->setMaxAge(300); //5 minutes $response->headers->addCacheControlDirective('must-revalidate', true); $response->setEtag('forum/topic/' . $id); //Also, set "expires" header for caches that don't understand Cache-Control $date = new \DateTime(); $date->modify('+300 seconds'); //5 minutes $response->setExpires($date); $view = $this->view($forumtopic); $view->setResponse($response); $view->setStatusCode(200); return $view; }
/** * Search for a topic in the forum. * * @param Request $request The HTTP Request object. * * @return View */ public function getForumAction(Request $request) { // http://myanimelist.net/forum/?action=search&q=#{keyword}&u=#{user}&uloc=#{userCategory}&loc=#{category} $user = $request->query->get('user'); $query = $request->query->get('query'); $userCategory = (int) $request->query->get('userCategory'); $category = (int) $request->query->get('category'); if ($userCategory <= 0) { $userCategory = 1; } if ($category < 0) { $category = -1; } $downloader = $this->get('atarashii_api.communicator'); try { $content = $downloader->fetch('/forum/?action=search&q=' . $query . '&u=' . $user . '&uloc=' . $userCategory . '&loc=' . $category); } catch (Exception\CurlException $e) { return $this->view(array('error' => 'network-error'), 500); } $response = new Response(); $response->setPublic(); $response->setMaxAge(3600); //One hour $response->headers->addCacheControlDirective('must-revalidate', true); $response->setEtag('forum/search?q=' . urlencode($query . $user)); //Also, set "expires" header for caches that don't understand Cache-Control $date = new \DateTime(); $date->modify('+3600 seconds'); //One hour $response->setExpires($date); if (strpos($content, 'User not found') !== false || !strpos($content, 'Topic') !== false) { $view = $this->view(array('error' => 'not-found')); $view->setResponse($response); $view->setStatusCode(404); return $view; } else { $result = ForumParser::parseTopics($content); $view = $this->view($result); $view->setResponse($response); $view->setStatusCode(200); return $view; } }