function doStatChilds($table, $entity, $header, &$total, $level)
{
    global $DB;
    // Search child entities
    $sql = "SELECT `id`\n           FROM `glpi_entities`\n           WHERE `entities_id` = '{$entity}'\n           ORDER BY `name`";
    $result = $DB->query($sql);
    while ($data = $DB->fetch_array($result)) {
        $fille = doStat($table, $data["id"], $header, $level);
        foreach ($header as $id => $name) {
            $total[$id] += $fille[$id];
        }
        $total["tot"] += $fille["tot"];
    }
}
 */
function dropToCsv($filename, $data)
{
    $ret = "";
    $first = true;
    $header = array();
    foreach ($data as $name => $d1) {
        if ($first) {
            $ret .= "name;" . implode(";", array_keys($d1)) . "\n";
            $header = $d1;
            $first = false;
        }
        $ret .= $name;
        foreach ($header as $key => $noneed) {
            if (isset($d1[$key])) {
                $ret .= ";" . $d1[$key];
            } else {
                $ret .= ";0";
            }
        }
        $ret .= "\n";
    }
    file_put_contents($filename, $ret);
}
dropToCsv("search_P_5.csv", doStat(true, "P_5"));
dropToCsv("search_P_10.csv", doStat(true, "P_10"));
dropToCsv("search_P_20.csv", doStat(true, "P_20"));
dropToCsv("linking_P_5.csv", doStat(false, "P_5"));
dropToCsv("linking_P_10.csv", doStat(false, "P_10"));
dropToCsv("linking_P_20.csv", doStat(false, "P_20"));