static function get_by_name($name) { if (empty($name)) { return false; } // Seek by strict city name first $qb = org_routamc_positioning_city_dba::new_query_builder(); $qb->add_constraint('city', 'LIKE', $name); $qb->set_limit(1); $matches = $qb->execute_unchecked(); if (count($matches) > 0) { return $matches[0]; } // Strict name didn't match, seek by alternate names $qb = org_routamc_positioning_city_dba::new_query_builder(); $qb->add_constraint('alternatenames', 'LIKE', "%{$name}%"); // Most likely we're interested in the biggest city that matches $qb->add_order('population', 'DESC'); $qb->set_limit(1); $matches = $qb->execute_unchecked(); if (count($matches) > 0) { return $matches[0]; } return false; }
/** * Empty default implementation, this calls won't do much. * * @param Array $location Parameters to geocode with, conforms to XEP-0080 * @return Array containing geocoded information */ function geocode($location, $options = array()) { $results = array(); $parameters = array('maxRows' => 1); if (!empty($options)) { foreach ($options as $key => $value) { if (isset($parameters[$key])) { $parameters[$key] = $value; } } } if ($parameters['maxRows'] < 1) { $parameters['maxRows'] = 1; } if (!isset($location['city'])) { $this->error = 'POSITIONING_MISSING_ATTRIBUTES'; return null; } $city_entry = null; $qb = org_routamc_positioning_city_dba::new_query_builder(); $qb->add_constraint('city', '=', $location['city']); if (isset($location['country'])) { $qb->add_constraint('country', '=', $location['country']); } $qb->add_order('population', 'DESC'); $qb->set_limit($parameters['maxRows']); $matches = $qb->execute(); if (count($matches) < 1) { // Seek the city entry by alternate names via a LIKE query $qb = org_routamc_positioning_city_dba::new_query_builder(); $qb->add_constraint('alternatenames', 'LIKE', "%|{$location['city']}|%"); if (isset($location['country'])) { $qb->add_constraint('country', '=', $location['country']); } $qb->set_limit($parameters['maxRows']); $matches = $qb->execute(); if (count($matches) < 1) { $this->error = 'POSITIONING_CITY_NOT_FOUND'; return null; } } foreach ($matches as $city_entry) { $city_coordinates = array('latitude' => $city_entry->latitude, 'longitude' => $city_entry->longitude); $position = array(); $position['latitude'] = $city_entry->latitude; $position['longitude'] = $city_entry->longitude; $position['distance'] = array('meters' => 0, 'bearing' => null); $position['city'] = $city_entry->city; $position['region'] = $city_entry->region; $position['country'] = $city_entry->country; $position['postalcode'] = null; $position['alternate_names'] = $city_entry->alternatenames; $position['accuracy'] = ORG_ROUTAMC_POSITIONING_ACCURACY_CITY; $results[] = $position; } return $results; }
function _get_city_by_name($city_name, $results = array()) { if (empty($city_name)) { return 0; } $city_id = 0; $city = org_routamc_positioning_city_dba::get_by_name($city_name); if ($city && $city->id) { $city_id = $city->id; } else { if (!empty($results)) { $city = new org_routamc_positioning_city_dba(); $city->city = $city_name; if (isset($results["{$this->_element_id}_input_place_country"]) && $results["{$this->_element_id}_input_place_country"]) { $city->country = $results["{$this->_element_id}_input_place_country"]; } if (isset($results["{$this->_element_id}_input_place_region"])) { $city->region = $results["{$this->_element_id}_input_place_region"]; } if (isset($results["{$this->_element_id}_input_coordinates_latitude"]) && $results["{$this->_element_id}_input_coordinates_latitude"] != '') { $city->latitude = $results["{$this->_element_id}_input_coordinates_latitude"]; } if (isset($results["{$this->_element_id}_input_coordinates_longitude"]) && $results["{$this->_element_id}_input_coordinates_longitude"] != '') { $city->longitude = $results["{$this->_element_id}_input_coordinates_longitude"]; } if (!$city->create()) { debug_add("Cannot save new city '{$city_name}'"); } $city_id = $city->id; } } return $city_id; }
$cities_created = 0; $row = 0; $handle = fopen($_POST['cities_file_path'], 'r'); while ($data = fgetcsv($handle, 1000, "\t")) { $row++; //if ($row > 1000) { break; } if (!isset($data[$fields_map['featurecode']]) || !in_array($data[$fields_map['featurecode']], $features)) { continue; } if ($data[$fields_map['population']] < $_POST['population_to_import']) { continue; } if (strlen($data[$fields_map['country']]) > 2) { continue; } $new_city = new org_routamc_positioning_city_dba(); $new_city->city = $data[$fields_map['name']]; $new_city->country = $data[$fields_map['country']]; $new_city->latitude = $data[$fields_map['latitude']]; $new_city->longitude = $data[$fields_map['longitude']]; $new_city->population = $data[$fields_map['population']]; $new_city->altitude = $data[$fields_map['elevation']]; // Handle possible alternate names $alternate_names = explode(',', $data[$fields_map['alternatenames']]); if (count($alternate_names) > 0) { foreach ($alternate_names as $name) { $new_city->alternatenames .= "|{$name}"; } if (!empty($data[$fields_map['asciiname']])) { $new_city->alternatenames .= '|' . $data[$fields_map['asciiname']]; }
/** * Imports an item as an event */ private function import_event($item) { // Check that we're trying to import item suitable to be an event if (!isset($item['xcal']) && !isset($item['gd']['when@'])) { // Not an event return false; } // Get start and end times $start = null; $end = null; if (isset($item['xcal']['dtstart'])) { // xCal RSS feed, for example Upcoming or Last.fm $start = strtotime($item['xcal']['dtstart']); } elseif (isset($item['gd']['when@starttime'])) { // gData Atom feed, for example Dopplr $start = strtotime($item['gd']['when@starttime']); } if (isset($item['xcal']['dtend'])) { $end = strtotime($item['xcal']['dtend']); } elseif (isset($item['gd']['when@starttime'])) { $end = strtotime($item['gd']['when@endtime']); } if (!$start || !$end) { return false; } if (!$this->_datamanager) { $schemadb = midcom_helper_datamanager2_schema::load_database($this->_node_config->get('schemadb')); $this->_datamanager = new midcom_helper_datamanager2_datamanager($schemadb); } // TODO: Move to real geocoded stuff $location_parts = array(); if (isset($item['xcal']['x-calconnect-venue_adr_x-calconnect-venue-name'])) { $location_parts[] = $item['xcal']['x-calconnect-venue_adr_x-calconnect-venue-name']; } if (isset($item['xcal']['x-calconnect-venue_adr_x-calconnect-street'])) { $location_parts[] = $item['xcal']['x-calconnect-venue_adr_x-calconnect-street']; } if (isset($item['xcal']['x-calconnect-venue_adr_x-calconnect-city'])) { $location_parts[] = $item['xcal']['x-calconnect-venue_adr_x-calconnect-city']; } if (isset($item['gd']['where@valuestring'])) { $wherevalues = explode(' ', $item['gd']['where@valuestring']); foreach ($wherevalues as $val) { $location_parts[] = $val; } } $qb = net_nemein_calendar_event_dba::new_query_builder(); $qb->add_constraint('node', '=', $this->_feed->node); $qb->add_constraint('extra', '=', md5($item['guid'])); $events = $qb->execute(); if (count($events) > 0) { // This item has been imported already earlier. Update $event = $events[0]; $event->_activitystream_verb = 'http://community-equity.org/schema/1.0/clone'; $event->_rcs_message = sprintf(midcom::get('i18n')->get_string('%s was imported from %s', 'net.nemein.rss'), $event->title, $this->_feed->title); $event->allow_name_catenate = true; if (empty($event->name)) { $resolver = new midcom_helper_reflector_nameresolver($event); // To prevent validation errors in case the auto-catenate is not allowed in the urlname datatype $event->name = $resolver->generate_unique_name(); } } else { $node = new midcom_db_topic($this->_feed->node); $node_lang_code = $node->get_parameter('net.nemein.calendar', 'language'); // This is a new item $event = new net_nemein_calendar_event_dba(); $event->start = $start; $event->end = $end; $event->extra = md5($item['guid']); $event->node = $this->_feed->node; if ($node->get_parameter('net.nemein.calendar', 'symlink_topic') != '') { try { $symlink_topic = new midcom_db_topic($node->get_parameter('net.nemein.calendar', 'symlink_topic')); $event->node = $symlink_topic->id; } catch (midcom_error $e) { $e->log(); } } if ($node_lang_code != '') { $lang_id = midcom::get('i18n')->code_to_id($node_lang_code); $event->lang = $lang_id; } $event->allow_name_catenate = true; $event->title = (string) $item['title']; $event->_activitystream_verb = 'http://community-equity.org/schema/1.0/clone'; $event->_rcs_message = sprintf(midcom::get('i18n')->get_string('%s was imported from %s', 'net.nemein.rss'), $event->title, $this->_feed->title); $resolver = new midcom_helper_reflector_nameresolver($event); $event->name = $resolver->generate_unique_name(); if (!$event->create()) { return false; } } $this->_datamanager->autoset_storage($event); $this->_datamanager->types['start']->value = new DateTime(strftime('%Y-%m-%d %H:%M:%S', $start)); $this->_datamanager->types['end']->value = new DateTime(strftime('%Y-%m-%d %H:%M:%S', $end)); if (is_a($this->_datamanager->types['location'], 'midcom_helper_datamanager2_type_position')) { // Position type, give all values we got, assume order "Street, City, Country" $location_parts = array_reverse($location_parts); if (count($location_parts) > 0) { $country = org_routamc_positioning_country_dba::get_by_name($location_parts[0]); if ($country && $country->code) { $this->_datamanager->types['location']->location->county = $country->code; } } if (count($location_parts) > 1) { $city = org_routamc_positioning_city_dba::get_by_name($location_parts[1]); if ($city && $city->id) { $this->_datamanager->types['location']->location->city = $city->id; } } if (count($location_parts) > 2) { $this->_datamanager->types['location']->location->street = $location_parts[2]; } if (isset($item['gml'])) { $gml_parts = explode(' ', $item['gml']['where_point_pos']); if (count($gml_parts) == 2) { $this->_datamanager->types['location']->location->latitude = (double) $gml_parts[0]; $this->_datamanager->types['location']->location->longitude = (double) $gml_parts[1]; } } } else { // Just give the location string we got $this->_datamanager->types['location']->value = implode(', ', $location_parts); } foreach ($item as $key => $value) { if (isset($this->_datamanager->types[$key])) { $this->_datamanager->types[$key]->value = $value; } } if (!$this->_datamanager->save()) { return false; } // This should be unnecessary but left in place just to be sure if (strlen($this->_datamanager->storage->object->name) == 0) { // Generate something to avoid empty "/" links in case of failures $this->_datamanager->storage->object->name = time(); $this->_datamanager->storage->object->update(); } $this->parse_tags($event, $item, 'description'); $this->parse_parameters($event, $item); return $event->guid; }
<?php midcom::get('auth')->require_admin_user(); $http_request = new org_openpsa_httplib(); $xml = $http_request->get('http://ws.geonames.org/countryInfo?lang=' . midcom::get('i18n')->get_current_language()); $simplexml = simplexml_load_string($xml); foreach ($simplexml->country as $id => $countryinfo) { echo "<br />Importing {$countryinfo->countryName}...\n"; $country = new org_routamc_positioning_country_dba(); $country->code = (string) $countryinfo->countryCode; $country->name = (string) $countryinfo->countryName; $country->codenumeric = (string) $countryinfo->isoNumeric; $country->code3 = (string) $countryinfo->isoAlpha3; $country->fips = (string) $countryinfo->fipsCode; $country->continent = (string) $countryinfo->continent; $country->area = (double) $countryinfo->areaInSqKm; $country->population = (int) $countryinfo->population; $country->currency = (string) $countryinfo->currencyCode; $country->bboxwest = (double) $countryinfo->bBoxWest; $country->bboxnorth = (double) $countryinfo->bBoxNorth; $country->bboxeast = (double) $countryinfo->bBoxEast; $country->bboxsouth = (double) $countryinfo->bBoxSouth; $capital = org_routamc_positioning_city_dba::get_by_name((string) $countryinfo->capital); if ($capital) { $country->capital = $capital->id; } $country->create(); echo midcom_connection::get_error_string(); }