/** * Update a manga on a user's list. * * Uses the contents of the HTTP Request to get the needed data for updating the * requested title. The user must have passed the basic authentication needs and the * PHP_AUTH_USER and PHP_AUTH_PW variables must be set. If so, the get variables of * "status", "chapters", "volumes", and "score" are checked and used in the creation * of a manga object. The object is used to make an XML document that is then posted * to MyAnimeList. * * @param Request $request Contains all the needed information to update the title. * @param int $id ID of the manga. * * @return View */ public function updateAction(Request $request, $id, $apiVersion) { // http://mymangalist.net/api/mangalist/update/#{id}.xml //get the credentials we received $username = $this->getRequest()->server->get('PHP_AUTH_USER'); $password = $this->getRequest()->server->get('PHP_AUTH_PW'); //Don't bother making a request if the user didn't send any authentication if ($username === null) { $view = $this->view(array('error' => 'unauthorized'), 401); $view->setHeader('WWW-Authenticate', 'Basic realm="myanimelist.net"'); return $view; } $manga = new Manga(); $manga->setId($id); //Only use values we were sent for the Update XML $update_items = array(); try { if ($request->request->get('status') !== null) { $manga->setReadStatus($request->request->get('status')); $update_items[] = 'status'; } if ($request->request->get('chapters') !== null) { $manga->setChaptersRead($request->request->get('chapters')); $update_items[] = 'chapters'; } if ($request->request->get('volumes') !== null) { $manga->setVolumesRead($request->request->get('volumes')); $update_items[] = 'volumes'; } if ($request->request->get('score') !== null) { $manga->setScore($request->request->get('score')); $update_items[] = 'score'; } //API 2 Items if ($apiVersion >= 2.0) { if ($request->request->get('downloaded_chap') !== null) { $manga->setChapDownloaded($request->request->get('downloaded_chap')); //Int $update_items[] = 'downloaded'; } if ($request->request->get('reread_count') !== null) { $manga->setRereadCount($request->request->get('reread_count')); //Int $update_items[] = 'rereadCount'; } if ($request->request->get('reread_value') !== null) { $manga->setRereadValue($request->request->get('reread_value')); //Int $update_items[] = 'rereadValue'; } if ($request->request->get('start') !== null) { $manga->setReadingStart(DateTime::createFromFormat('Y-m-d', $request->request->get('start'))); //Needs to be DT! $update_items[] = 'start'; } if ($request->request->get('end') !== null) { $manga->setReadingEnd(DateTime::createFromFormat('Y-m-d', $request->request->get('end'))); //Needs to be DT! $update_items[] = 'end'; } if ($request->request->get('priority') !== null) { $manga->setPriority($request->request->get('priority')); $update_items[] = 'priority'; } if ($request->request->get('is_rereading') !== null) { $manga->setRereading($request->request->get('is_rereading')); //Bool - 0 = no, 1 = yes $update_items[] = 'isRereading'; } if ($request->request->get('comments') !== null) { $manga->setPersonalComments($request->request->get('comments')); //Plain text string. No HTML. $update_items[] = 'comments'; } if ($request->request->get('tags') !== null) { $manga->setPersonalTags($request->request->get('tags')); //Comma-separated string $update_items[] = 'tags'; } } } catch (\Exception $e) { return $this->view(array('error' => $e->getMessage()), 500); } $xmlcontent = $manga->MALApiXml($update_items); $connection = $this->get('atarashii_api.communicator'); try { $connection->sendXML('/api/mangalist/update/' . $manga->getId() . '.xml', $xmlcontent, $username, $password); return $this->view('ok', 200); } catch (Exception\ClientErrorResponseException $e) { $view = $this->view(array('error' => 'unauthorized'), 401); $view->setHeader('WWW-Authenticate', 'Basic realm="myanimelist.net"'); return $view; } catch (Exception\ServerErrorResponseException $e) { //MAL broke API responses, so we have to check the content on the response to make sure //it actually was an error. $response = $e->getResponse()->getBody(true); if (stripos($response, 'Updated') === 0) { return $this->view('ok', 200); } return $this->view(array('error' => 'not-found'), 404); } catch (Exception\CurlException $e) { return $this->view(array('error' => 'network-error'), 500); } }
public function testMalXml() { $manga = new Manga(); $items = array(); $output = "<?xml version=\"1.0\"?>\n<entry><chapter>7</chapter><volume>7</volume><status>1</status><score>9</score><downloaded_chapters>8</downloaded_chapters><times_reread>1</times_reread><reread_value>4</reread_value><date_start>01012015</date_start><date_finish>01022015</date_finish><priority>0</priority><comments>This is a comment.</comments><tags>one,two,three</tags></entry>\n"; $manga->setId(1); $manga->setChaptersRead(7); $items[] = 'chapters'; $manga->setVolumesRead(7); $items[] = 'volumes'; $manga->setReadStatus(1); $items[] = 'status'; $manga->setScore(9); $items[] = 'score'; $manga->setChapDownloaded(8); $items[] = 'downloaded'; $manga->setRereadCount(1); $items[] = 'rereadCount'; $manga->setRereadValue(4); $items[] = 'rereadValue'; $manga->setReadingStart(new \DateTime('20150101')); $items[] = 'start'; $manga->setReadingEnd(new \DateTime('20150102')); $items[] = 'end'; $manga->setPriority(6); $items[] = 'priority'; $manga->setPersonalComments('This is a comment.'); $items[] = 'comments'; $manga->setPersonalTags('one,two,three'); $items[] = 'tags'; $this->assertEquals($output, $manga->MALApiXml($items)); }