示例#1
0
 /**
  * update: alt name modification
  *
  * @return self
  */
 public function updateAltNameModify()
 {
     $em = $this->getEm();
     $altNameRepo = $em->getRepository('Yalesov\\Geoname\\Entity\\AltName');
     $placeRepo = $em->getRepository('Yalesov\\Geoname\\Entity\\Place');
     $languageRepo = $em->getRepository('Yalesov\\Geoname\\Entity\\Language');
     $placeMap = array();
     $languageMap = array();
     foreach (FileSystemManager::fileIterator($this->getTmpDir() . '/update/altName/modification') as $source) {
         if ($this->getLock($source) && ($fh = fopen("{$source}.lock", 'r'))) {
             $this->getCli()->write($source, 'module');
             while ($data = fgetcsv($fh, 0, "\t", "")) {
                 list($id, $placeId, $languageCode, $name, $isPreferred, $isShort, $isColloquial, $isHistoric) = $data;
                 if (!($altName = $altNameRepo->find((int) $id))) {
                     $altName = new Entity\AltName();
                     $altName->setId($id);
                     $em->persist($altName);
                 }
                 $altName->setName($name)->setIsPreferred((bool) trim($isPreferred))->setIsShort((bool) trim($isShort))->setIsColloquial((bool) trim($isColloquial))->setIsHistoric((bool) trim($isHistoric));
                 if (isset($placeMap[$placeId])) {
                     $altName->setPlace($placeMap[$placeId]);
                 } elseif ($place = $placeRepo->find((int) $placeId)) {
                     $altName->setPlace($place);
                     $placeMap[$placeId] = $place;
                 }
                 if (isset($languageMap[$languageCode])) {
                     $altName->setLanguage($languageMap[$languageCode]);
                 } elseif ($language = $languageRepo->findLanguage($languageCode)) {
                     $altName->setLanguage($language);
                     $languageMap[$languageCode] = $language;
                 } else {
                     $altName->setLanguageOther($languageCode);
                 }
             }
             fclose($fh);
             $this->markDone($source);
         }
     }
     $em->flush();
     return $this;
 }
 public function testUpdateAltNameDelete()
 {
     $altNameRepo = $this->em->getRepository('Yalesov\\Geoname\\Entity\\AltName');
     $fooAltName = new Entity\AltName();
     $fooAltName->setId(1)->setName('dummy')->setIsPreferred(false)->setIsShort(false)->setIsColloquial(false)->setIsHistoric(false);
     $this->em->persist($fooAltName);
     $barAltName = new Entity\AltName();
     $barAltName->setId(2)->setName('dummy')->setIsPreferred(false)->setIsShort(false)->setIsColloquial(false)->setIsHistoric(false);
     $this->em->persist($barAltName);
     $this->em->flush();
     mkdir('tmp/geoname/update/altName/delete', 0777, true);
     $fh = fopen('tmp/geoname/update/altName/delete/1', 'a+');
     fwrite($fh, "1\tfoo\tfoo comment\n");
     fclose($fh);
     $this->geoname->updateAltNameDelete();
     $this->assertCount(2, $altNameRepo->findAll());
     $this->assertTrue((bool) $fooAltName->getIsDeprecated());
     $this->assertFalse((bool) $barAltName->getIsDeprecated());
 }