function start($xtime = 0) { $unix = new unix(); if ($GLOBALS["VERBOSE"]) { "echo Loading done...\n"; } $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid"; $timefile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time"; if ($GLOBALS["VERBOSE"]) { echo "Timefile = {$timefile}\n"; } $pid = @file_get_contents($pidfile); if (!$GLOBALS["FORCE"]) { if ($pid < 100) { $pid = null; } $unix = new unix(); if ($unix->process_exists($pid, basename(__FILE__))) { if ($GLOBALS["VERBOSE"]) { echo "Already executed pid {$pid}\n"; } return; } $timeexec = $unix->file_time_min($timefile); if (!$GLOBALS["SCHEDULED"]) { if ($timeexec < 2880) { return; } } $mypid = getmypid(); @file_put_contents($pidfile, $mypid); } @unlink($timefile); @file_put_contents($timefile, time()); build_progress("Starting refreshing...", 10); $sql = "SELECT tablename, familyday,DATE_FORMAT(zDate,'%Y%m%d') AS `suffix` FROM tables_day \n\t\t\tWHERE familyday=0 AND DAY(zDate)<DAY(NOW()) AND YEAR(zDate) = YEAR(NOW()) AND MONTH(zDate) = MONTH(NOW())"; $q = new mysql_squid_builder(); $q->CheckTables(); $results = $q->QUERY_SQL($sql); if (!$q->ok) { echo $q->mysql_error; return; } if ($GLOBALS["VERBOSE"]) { echo mysql_num_rows($results) . " rows\n"; } $c = 0; $Max = mysql_num_rows($results); while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) { $worktable = "{$ligne["suffix"]}_visited"; $SourceTable = "{$ligne["suffix"]}_hour"; $desttable = "{$ligne["suffix"]}_family"; $c++; $prc = $c / $Max * 100; $prc = round($prc); if ($prc > 80) { $prc = 80; } build_progress("Checking ...{$worktable} {$desttable}", $prc); if (!$q->TABLE_EXISTS($worktable)) { if (!$q->TABLE_EXISTS($SourceTable)) { $q->QUERY_SQL("UPDATE tables_day SET familyday=1 WHERE tablename='{$ligne["tablename"]}'"); continue; } if (!repair_visited_from_sources_table($SourceTable, $worktable)) { continue; } } if (!perform($worktable, $desttable)) { continue; } $q->QUERY_SQL("UPDATE tables_day SET familyday=1 WHERE tablename='{$ligne["tablename"]}'"); } build_progress("Calculating master table", 80); CONDENSED(); build_progress("Done", 100); }
function REBUILD_FULL_MEMBERS() { $now = date("Ymd"); $currentable = "{$now}_hour"; $q = new mysql_squid_builder(); $TABLES = $q->LIST_TABLES_HOURS(); $username = trim($username); $c = 0; $MAX = count($TABLES); while (list($tablesource, $b) = each($TABLES)) { if ($tablesource == $currentable) { continue; } if (!preg_match("#^[0-9]+_hour#", $tablesource)) { echo "Skipping {$tablesource}\n"; continue; } $time = $q->TIME_FROM_HOUR_TABLE($tablesource); $DAY = date("d", $time); $YEAR = date("Y", $time); $MONTH = date("m", $time); $c++; $month_table = date("Ym", $time) . "_members"; echo "{$tablesource} -> Day: {$DAY}, Year: {$YEAR}, Month, {$MONTH}, table:{$month_table}\n"; $MONTH_TABLES[$month_table]["YEAR"] = $YEAR; $MONTH_TABLES[$month_table]["MONTH"] = $MONTH; build_progress("Parsing {$tablesource} {$c}/{$MAX}", 20); $NEWTABLES[$tablesource] = $month_table; } while (list($tablesource, $b) = each($MONTH_TABLES)) { echo "Cleaning/Creating {$tablesource}\n"; if ($q->TABLE_EXISTS($month_table)) { $q->QUERY_SQL("TRUNCATE TABLE {$month_table}"); } else { echo "Creating {$month_table}\n"; $q->CreateMembersMonthTable($month_table); } } $c = 0; while (list($tablesource, $month_table) = each($NEWTABLES)) { if (!preg_match("#^[0-9]+_hour#", $tablesource)) { continue; } $c++; build_progress("Parsing {$tablesource} {$c}/{$MAX}", 20); $results = $q->QUERY_SQL("SELECT SUM(hits) as hits,SUM(size) as size, MAC,client,hostname,uid FROM {$tablesource} GROUP BY MAC,client,hostname,uid"); if (!$q->ok) { echo $q->mysql_error . "\n"; build_progress("Parsing {$tablesource} {$c}/{$MAX} failed", 110); return; } $sum = mysql_num_rows($results); if ($sum == 0) { echo "{$tablesource} Nothing!\n"; continue; } $time = $q->TIME_FROM_HOUR_TABLE($tablesource); $DAY = date("d", $time); echo "Month table:{$month_table} Day {$DAY} {$sum} rows\n"; $prefix = "INSERT IGNORE INTO `{$month_table}` (zMD5,client,`day`,size,hits,uid,MAC,hostname) VALUES "; $row = array(); $d = 0; $IP = new IP(); while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) { $zMD5 = md5(serialize($ligne)); $client = mysql_escape_string2($ligne["client"]); $uid = mysql_escape_string2($ligne["uid"]); $MAC = mysql_escape_string2($ligne["MAC"]); $hostname = mysql_escape_string2($ligne["hostname"]); $size = $ligne["size"]; $hits = $ligne["hits"]; if ($IP->isValid($uid)) { $uid = null; } $row[] = "('{$zMD5}','{$client}','{$DAY}','{$size}','{$hits}','{$uid}','{$MAC}','{$hostname}')"; $d++; if (count($row) > 500) { $q->QUERY_SQL($prefix . @implode(",", $row)); build_progress("Insterting " . count($row) . " elements", 20); echo "Added {$d} rows\n"; $row = array(); } } if (count($row) > 0) { $q->QUERY_SQL($prefix . @implode(",", $row)); build_progress("Insterting " . count($row) . " elements", 20); echo "Added {$d} rows\n"; $row = array(); } } MEMBERS_YEAR(true); CONDENSED(true); build_progress("{success}", 20); }