Exemplo n.º 1
0
function inject_stored_items($nopid = false)
{
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    if (system_is_overloaded(basename(__FILE__))) {
        $php = $unix->LOCATE_PHP5_BIN();
        ufdbguard_admin_events("Overloaded system... ask to run this task later...", __FUNCTION__, __FILE__, __LINE__, "proxy");
        $unix->THREAD_COMMAND_SET("{$php} " . __FILE__ . " --inject");
    }
    if (!$nopid) {
        $pid = @file_get_contents($pidfile);
        $myfile = basename(__FILE__);
        if ($unix->process_exists($pid, $myfile)) {
            ufdbguard_admin_events("Task already running PID: {$pid}, aborting current task", __FUNCTION__, __FILE__, __LINE__, "proxy");
            return;
        }
    }
    $mypid = getmypid();
    @file_put_contents($pidfile, $mypid);
    $t1 = time();
    $file = "/var/cache/purge.calculated.db";
    if (!is_file($file)) {
        echo "{$file} no such file\n";
        return;
    }
    $q = new mysql_blackbox();
    if (!$q->TABLE_EXISTS("cacheitems_localhost")) {
        $q->build_cached_items_table("localhost");
    }
    $BIGARRAY = inject_stored_array();
    $prefix = "INSERT IGNORE INTO cacheitems_localhost(sitename,familysite,size,items) VALUES ";
    $q->QUERY_SQL("TRUNCATE TABLE cacheitems_localhost");
    $f = array();
    $c = 0;
    while (list($sitename, $array) = each($BIGARRAY)) {
        $c++;
        $f[] = "('{$sitename}','{$array["FAMILY"]}','{$array["SIZE"]}','{$array["ITEMS"]}')";
        if (count($f) > 500) {
            $q->QUERY_SQL($prefix . @implode(",", $f));
            if (!$q->ok) {
                squid_admin_mysql(0, "MySQL error!", $q->mysql_error, __FILE__, __LINE__);
                return;
            }
        }
    }
    if (count($f) > 0) {
        $q->QUERY_SQL($prefix . @implode(",", $f));
        if (!$q->ok) {
            squid_admin_mysql(0, "MySQL error!", $q->mysql_error, __FILE__, __LINE__);
            return;
        }
    }
    $took = $unix->distanceOfTimeInWords($t1, time());
    if ($c > 0) {
        squid_admin_mysql(2, "Sucess adding {$c} cached websites took:{$took}", null, __FILE__, __LINE__);
    }
    @unlink($file);
}