Ejemplo n.º 1
0
 public function doUnKnowUpdate(WordsEntity $word)
 {
     // invalid setting
     if ($word->getStatus() != $this->marker->getStatus()) {
         throw new EngRuntimeException("status doesn't match!");
     }
     $newFailureCount = $word->getFailure() + 1;
     if ($newFailureCount >= $this->marker->getFailure()) {
         $newStatus = $this->getNextStatusWhenFail($word->getStatus());
         if ($newStatus != $word->getStatus()) {
             $word->setStatus($newStatus);
             $word->setSuccess(0);
             $word->setFailure(0);
         } else {
             $word->setFailure($newFailureCount);
         }
     } else {
         $word->setFailure($newFailureCount);
     }
 }
 private function downloadWordsVoice($name, $force)
 {
     $params = array();
     if ($force) {
         $sql = "SELECT * FROM words WHERE 1 = 1";
     } else {
         $sql = "SELECT * FROM words WHERE (voice IS NULL or voice = '')";
     }
     if ($name != null) {
         $sql .= ' AND name = :name';
         $params['name'] = $name;
     }
     $sql .= " AND voice not like '%/%'";
     $handle = $this->db->executeQuery($sql, $params);
     while ($row = $handle->fetch()) {
         $entity = new WordsEntity();
         $entity->fromArray($row);
         try {
             $this->updateWord($entity, $force);
         } catch (\Exception $e) {
             $this->output->writeln(sprintf("word [%s] update failed, error info: %s", $entity->getName(), $e->getMessage()));
         }
     }
 }
 public function addNewAction()
 {
     $json = array('status' => 0, 'errorWords' => '');
     $key = trim($this->request->request->get('key'));
     $words = preg_split("|[\r\n ]+|i", $key);
     if (empty($words)) {
         return new JsonResponse($json);
     }
     $errorWords = array();
     $successWords = array();
     $updateWords = array();
     foreach ($words as $word) {
         $entity = $this->em->getRepository('Eng:WordsEntity')->findOneBy(array('name' => $word));
         if ($entity === null) {
             $entity = new WordsEntity();
             $entity->setCreateTime(new \Datetime('now'));
             $entity->setName($word);
         }
         try {
             $mean = $this->container['wordMeanDownloader']->download($entity->getName());
             $entity->setMeans(implode("\n", $mean->getMeaning()));
             $entity->setPronunciation($mean->getPhonetic());
             $voiceName = $this->container['wordVoiceDownloader']->download($entity->getName());
             $entity->setVoice($voiceName);
             $entity->setStatus(WordsEntity::NEWONE);
         } catch (\Exception $e) {
             $this->log->addWarning(sprintf("Can not find this word:%s, info:%s", $entity->getName(), $e->getMessage()));
             $errorWords[] = $entity->getName();
             continue;
         }
         if ($entity->getId() !== null) {
             $updateWords[] = $entity->getName();
         } else {
             $successWords[] = $entity->getName();
         }
         $this->em->persist($entity);
         $this->em->flush();
     }
     $message = array();
     if (!empty($successWords)) {
         $message[] = 'new words: ' . count($successWords);
     }
     if (!empty($updateWords)) {
         $message[] = 'update words: ' . count($updateWords);
     }
     if (!empty($errorWords)) {
         if (empty($successWords) && empty($updateWords)) {
             $json['status'] = 1;
         }
         $message[] = 'error words: ' . count($errorWords);
         $json['errorWords'] = implode("\n", $errorWords);
     }
     $json['message'] = implode(', ', $message);
     return new JsonResponse($json);
 }