Exemplo n.º 1
0
 public function testParseTopics()
 {
     $boardContent = file_get_contents(__DIR__ . '/../InputSamples/forum-board-14.html');
     $board = ForumParser::parseTopics($boardContent);
     //Some sanity checking on the pages and main content
     $this->assertInternalType('array', $board);
     $this->assertGreaterThan(0, $board['pages']);
     $this->assertInternalType('array', $board['list']);
     $topic = $board['list'][0];
     //Some sanity checking on the topic
     $this->assertInstanceOf('Atarashii\\APIBundle\\Model\\Forum', $topic);
     $this->assertInternalType('int', $topic->getId());
     $this->assertInternalType('string', $topic->getUsername());
     $this->assertInternalType('string', $topic->getName());
     $this->assertInternalType('string', $topic->getUsername());
     $this->assertInternalType('int', $topic->getReplies());
     $this->assertInstanceOf('\\DateTime', new \DateTime($topic->getTime()));
     //Some sanity checking on the reply class in the topic details
     $topicReply = $topic->getReply();
     $this->assertInternalType('array', $topicReply);
     $this->assertInternalType('string', $topicReply['username']);
     $this->assertInstanceOf('\\DateTime', new \DateTime($topicReply['time']));
 }
 /**
  * Get the manga discussion forum topics of MAL.
  *
  * @param Request $request HTTP Request object
  * @param int     $id      The ID of the topic as assigned by MyAnimeList
  *
  * @return View
  */
 public function getForumMangaAction(Request $request, $id)
 {
     // http://myanimelist.net/forum/?mangaid=#{id}
     $page = (int) $request->query->get('page');
     if ($page <= 0) {
         $page = 1;
     }
     if ((int) $id == '') {
         return $this->view(array('error' => 'Invalid board ID'), 200);
     }
     $downloader = $this->get('atarashii_api.communicator');
     try {
         $forumcontent = $downloader->fetch('/forum/?mangaid=' . $id . '&show=' . ($page * 50 - 50));
     } catch (Exception\CurlException $e) {
         return $this->view(array('error' => 'network-error'), 500);
     }
     $forumtopics = ForumParser::parseTopics($forumcontent);
     $response = new Response();
     $response->setPublic();
     $response->setMaxAge(900);
     //15 minutes
     $response->headers->addCacheControlDirective('must-revalidate', true);
     $response->setEtag('forum/manga/' . $id);
     //Also, set "expires" header for caches that don't understand Cache-Control
     $date = new \DateTime();
     $date->modify('+900 seconds');
     //15 minutes
     $response->setExpires($date);
     $view = $this->view($forumtopics);
     $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;
     }
 }