function startElement($parser, $name, $attrs)
{
    global $id, $file, $xrai_db, $db_topicelements;
    if ($name == "pool") {
        $id = $attrs["topic"];
        if (!$id) {
            die("No topic id defined!\n");
        }
        $res = $xrai_db->query("DELETE FROM {$db_topicelements} WHERE idtopic=?", array($id));
        if (DB::isError($res)) {
            die($res->getUserInfo() . "\n");
        }
        return;
    }
    if (!$id) {
        die("No topic id defined!\n");
    }
    switch ($name) {
        case "file":
            $file = getFileId($attrs["file"]);
            break;
        case "path":
            $pathid = getPathId($attrs["path"]);
            if ($file > 0 && $pathid > 0) {
                print "Adding {$file}, {$path}\n";
                $res = $xrai_db->autoExecute($db_topicelements, array("idfile" => $file, "idtopic" => $id, "idpath" => $pathid));
                if (DB::isError($res)) {
                    die($res->getUserInfo() . "\n");
                }
            } else {
                print "Skipping {$file}, {$pathid}\n";
            }
            break;
        default:
            die("Unexpected tag: {$name}");
    }
}
function startElement($parser, $name, $attrs)
{
    global $filepath, $id, $file, $xrai_db, $db_topicelements;
    if ($name == "pool") {
        $id = $attrs["topic"];
        if (!$id) {
            die("No topic id defined!\n");
        }
        $res = $xrai_db->query("DELETE FROM {$db_topicelements} WHERE idtopic=?", array($id));
        if (DB::isError($res)) {
            die($res->getUserInfo() . "\n");
        }
        return;
    }
    if (!$id) {
        die("No topic id defined!\n");
    }
    switch ($name) {
        case "file":
            $filepath = $attrs["file"];
            $file = getFileId($attrs["file"]);
            break;
        case "passage":
            if ($attrs["start"] && $attrs["end"]) {
                $startid = getPathId(normalisePath($attrs["start"]));
                $endid = getPathId(normalisePath($attrs["end"]));
                if ($file > 0 && $startid > 0 && $endid > 0) {
                    print "Adding passage {$file}, {$startid}, {$endid} ({$filepath}, {$attrs['start']}, {$attrs['end']})\n";
                    $res = $xrai_db->autoExecute($db_topicelements, array("idfile" => $file, "idtopic" => $id, "idstartpath" => $startid, "idendpath" => $endid));
                    if (DB::isError($res)) {
                        print "[ERROR: " . $res->getUserInfo() . "] Skipping {$file}, {$pathid}\n";
                    }
                } else {
                    print "[ERROR] Skipping {$file}, {$pathid} (file and/or start/end path are not valid)\n";
                }
            } else {
                print "[ERROR] Skipping {$file}, {$pathid} (no start/end attributes for a passage)\n";
            }
            break;
        case "path":
            if ($attrs["path"]) {
                $pathid = getPathId($attrs["path"]);
                if ($file > 0 && $pathid > 0) {
                    print "Adding element {$file}, {$pathid} ({$filepath}, {$attrs['path']})\n";
                    $res = $xrai_db->autoExecute($db_topicelements, array("idfile" => $file, "idtopic" => $id, "idstartpath" => $pathid, "idendpath" => $pathid));
                    if (DB::isError($res)) {
                        print "[ERROR: " . $res->getUserInfo() . "] Skipping {$file}, {$pathid}\n";
                    }
                    // die($res->getUserInfo() . "\n");
                } else {
                    print "[ERROR] Skipping {$file}, {$pathid} (file and/or path are not valid)\n";
                }
            } else {
                print "[ERROR] Skipping {$file}, {$pathid}\n";
            }
            break;
        default:
            die("Unexpected tag: {$name}");
    }
}