コード例 #1
0
 /**
  * update: place modification
  *
  * @return self
  */
 public function updatePlaceModify()
 {
     $em = $this->getEm();
     $placeRepo = $em->getRepository('Yalesov\\Geoname\\Entity\\Place');
     $featureRepo = $em->getRepository('Yalesov\\Geoname\\Entity\\Feature');
     $timezoneRepo = $em->getRepository('Yalesov\\Geoname\\Entity\\Timezone');
     $countryRepo = $em->getRepository('Yalesov\\Geoname\\Entity\\Country');
     $countryMap = array();
     $featureMap = array();
     $placeMap = array();
     foreach (FileSystemManager::fileIterator($this->getTmpDir() . '/update/place/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, $name, , , $latitude, $longitude, $featureClass, $featureCode, $countryCode, , $admin1Code, $admin2Code, $admin3Code, $admin4Code, $population, $elevation, $digiEleModel, $timezoneCode, ) = $data;
                 if (!($place = $placeRepo->find((int) $id))) {
                     $place = new Entity\Place();
                     $place->setId($id);
                     $placeMap[$id] = $place;
                     $em->persist($place);
                 }
                 $place->setName($name)->setLatitude($latitude)->setLongitude($longitude)->setElevation($elevation)->setDigiEleModel($digiEleModel)->setCountryCode($countryCode)->setAdmin1Code($admin1Code)->setAdmin2Code($admin2Code)->setAdmin3Code($admin3Code)->setAdmin4Code($admin4Code)->setPopulation($population);
                 $fullFeatureCode = "{$featureClass}.{$featureCode}";
                 if (isset($featureMap[$fullFeatureCode])) {
                     $feature = $featureMap[$fullFeatureCode];
                 } elseif (!($feature = $featureRepo->findByGeonameCode($fullFeatureCode))) {
                     $feature = new Entity\Feature();
                     $em->persist($feature);
                     $feature->setCode($featureCode);
                     if (isset($featureMap[$featureClass])) {
                         $feature->setParent($featureMap[$featureClass]);
                     } elseif ($parent = $featureRepo->findOneBy(array('code' => $featureClass))) {
                         $featureMap[$featureClass] = $parent;
                         $feature->setParent($parent);
                     }
                     $featureMap[$fullFeatureCode] = $feature;
                 }
                 $place->setFeature($feature);
                 if (!empty($timezoneCode)) {
                     if (!($timezone = $timezoneRepo->findOneBy(array('code' => $timezoneCode)))) {
                         $timezone = new Entity\Timezone();
                         $em->persist($timezone);
                         $timezone->setCode($timezoneCode);
                         if (isset($countryMap[$countryCode])) {
                             $timezone->setCountry($countryMap[$countryCode]);
                         } elseif ($country = $countryRepo->findOneBy(array('iso2' => $countryCode))) {
                             $timezone->setCountry($country);
                         }
                     }
                     $place->setTimezone($timezone);
                 }
                 // hierarchy
                 $em->flush();
                 // in case this entity will become
                 // dependency for the next entity
                 if ($featureClass !== 'A' || !in_array($featureCode, array('ADM1', 'ADM2', 'ADM3', 'ADM4'))) {
                     continue;
                 }
                 // determine parent feature
                 $parentFeatureCode = '';
                 $adminLevel = (int) substr($featureCode, -1);
                 while ($adminLevel > 1) {
                     $parentLevel = $adminLevel - 1;
                     $parentVar = "admin{$parentLevel}Code";
                     $parentCode = ${$parentVar};
                     if ($parentCode != '00') {
                         $parentFeatureCode = "ADM{$parentLevel}";
                         break;
                     }
                     $adminLevel--;
                 }
                 // assemble criteria
                 $criteria = array('admin3Code' => '', 'admin2Code' => '', 'admin1Code' => '', 'countryCode' => '');
                 switch ($parentFeatureCode) {
                     // intentional fall throughs
                     case 'ADM3':
                         $criteria['admin3Code'] = $admin3Code;
                     case 'ADM2':
                         $criteria['admin2Code'] = $admin2Code;
                     case 'ADM1':
                         $criteria['admin1Code'] = $admin1Code;
                     default:
                         $criteria['countryCode'] = $countryCode;
                 }
                 if ($parentFeatureCode) {
                     $criteria['featureCode'] = $parentFeatureCode;
                     $criteria['featureClass'] = 'A';
                 }
                 if ($places = $placeRepo->findPlace($criteria, 1)) {
                     $place->setParent(current($places));
                 }
             }
             fclose($fh);
             $this->markDone($source);
         }
     }
     $em->flush();
     return $this;
 }
コード例 #2
0
 public function testUpdateAltNameModify()
 {
     $altNameRepo = $this->em->getRepository('Yalesov\\Geoname\\Entity\\AltName');
     $fooPlace = new Entity\Place();
     $fooPlace->setId(1);
     $this->em->persist($fooPlace);
     $fooLang = new Entity\Language();
     $this->em->persist($fooLang);
     $fooLang->setIso3('foo');
     $fooAltName = new Entity\AltName();
     $fooAltName->setId(1)->setName('dummy')->setIsPreferred(false)->setIsShort(false)->setIsColloquial(false)->setIsHistoric(false);
     $this->em->persist($fooAltName);
     $this->em->flush();
     mkdir('tmp/geoname/update/altName/modification', 0777, true);
     $fh = fopen('tmp/geoname/update/altName/modification/1', 'a+');
     fwrite($fh, "1\t1\tfoo\tfoo alt name\t1\t1\t1\t1\n");
     fwrite($fh, "2\t2\tcustom lang\tfoo alt name\t\t\t\t\n");
     fclose($fh);
     $this->geoname->updateAltNameModify();
     $this->assertCount(2, $altNameRepo->findAll());
     $this->assertSame('foo alt name', $fooAltName->getName());
     $this->assertSame(true, $fooAltName->getIsPreferred());
     $this->assertSame(true, $fooAltName->getIsShort());
     $this->assertSame(true, $fooAltName->getIsColloquial());
     $this->assertSame(true, $fooAltName->getIsHistoric());
     $this->assertSame($fooPlace, $fooAltName->getPlace());
     $this->assertSame($fooLang, $fooAltName->getLanguage());
     $altName = $altNameRepo->find(2);
     $this->assertNotEmpty($altName);
     $this->assertSame('foo alt name', $altName->getName());
     $this->assertSame(false, $altName->getIsPreferred());
     $this->assertSame(false, $altName->getIsShort());
     $this->assertSame(false, $altName->getIsColloquial());
     $this->assertSame(false, $altName->getIsHistoric());
     $this->assertEmpty($altName->getPlace());
     $this->assertEmpty($altName->getLanguage());
 }