Example #1
0
/**
 * extract all places from the given record and insert them into the places table
 *
 * @param        $gid
 * @param        $ged_id
 * @param string $gedrec
 */
function update_places($gid, $ged_id, $gedrec)
{
    global $placecache;
    static $sql_insert_placelinks = null;
    static $sql_insert_places = null;
    static $sql_select_places = null;
    if (!$sql_insert_placelinks) {
        // Use INSERT IGNORE as a (temporary) fix for https://bugs.launchpad.net/webtrees/+bug/582226
        // It ignores places that utf8_unicode_ci consider to be the same (i.e. accents).
        // For example Québec and Quebec
        // We need a better solution that attaches multiple names to single places
        $sql_insert_placelinks = WT_DB::prepare("INSERT IGNORE INTO `##placelinks` (pl_p_id, pl_gid, pl_file) VALUES (?,?,?)");
        $sql_insert_places = WT_DB::prepare("INSERT INTO `##places` (p_place, p_parent_id, p_file, p_std_soundex, p_dm_soundex) VALUES (?,?,?,?,?)");
        $sql_select_places = WT_DB::prepare("SELECT p_id FROM `##places` WHERE p_file=? AND p_parent_id=? AND p_place=?");
    }
    if (!isset($placecache)) {
        $placecache = array();
    }
    $personplace = array();
    // import all place locations, but not control info such as
    // 0 HEAD/1 PLAC or 0 _EVDEF/1 PLAC
    $pt = preg_match_all("/^[2-9] PLAC (.+)/m", $gedrec, $match, PREG_SET_ORDER);
    for ($i = 0; $i < $pt; $i++) {
        $place = trim($match[$i][1]);
        $lowplace = WT_I18N::strtolower($place);
        //-- if we have already visited this place for this person then we don't need to again
        if (isset($personplace[$lowplace])) {
            continue;
        }
        $personplace[$lowplace] = 1;
        $places = explode(',', $place);
        //-- reverse the array to start at the highest level
        $secalp = array_reverse($places);
        $parent_id = 0;
        $search = true;
        foreach ($secalp as $place) {
            $place = trim($place);
            $key = strtolower($place . "_" . $parent_id);
            //-- if this place has already been added then we don't need to add it again
            if (isset($placecache[$key])) {
                $parent_id = $placecache[$key];
                if (!isset($personplace[$key])) {
                    $personplace[$key] = 1;
                    $sql_insert_placelinks->execute(array($parent_id, $gid, $ged_id));
                }
                continue;
            }
            //-- only search the database while we are finding places in it
            if ($search) {
                //-- check if this place and level has already been added
                $tmp = $sql_select_places->execute(array($ged_id, $parent_id, $place))->fetchOne();
                if ($tmp) {
                    $p_id = $tmp;
                } else {
                    $search = false;
                }
            }
            //-- if we are not searching then we have to insert the place into the db
            if (!$search) {
                $std_soundex = WT_Soundex::soundex_std($place);
                $dm_soundex = WT_Soundex::soundex_dm($place);
                $sql_insert_places->execute(array($place, $parent_id, $ged_id, $std_soundex, $dm_soundex));
                $p_id = WT_DB::getInstance()->lastInsertId();
            }
            $sql_insert_placelinks->execute(array($p_id, $gid, $ged_id));
            //-- increment the level and assign the parent id for the next place level
            $parent_id = $p_id;
            $placecache[$key] = $p_id;
            $personplace[$key] = 1;
        }
    }
}