public static function export_stats($system) { includePackage('db'); PageViews::createDatabaseTables(); if ($system == 'web') { $table = Kurogo::getSiteVar('PAGE_VIEWS_TABLE'); $logfile = Kurogo::getSiteVar('WEB_CURRENT_LOG_FILE'); $target = Kurogo::getSiteVar('WEB_LOG_FILE'); } else {// assume 'api' $table = Kurogo::getSiteVar('API_STATS_TABLE'); $logfile = Kurogo::getSiteVar('API_CURRENT_LOG_FILE'); $target = Kurogo::getSiteVar('API_LOG_FILE'); } // Create directories if needed if (!file_exists($logfile) || !file_exists($target)) { $dirs = array_unique(array(dirname($logfile), dirname($target))); foreach ($dirs as $dir) { if (!file_exists($dir) && !mkdir($dir, 0755, true)) { error_log("could not create $dir"); } } } $today = date('Ymd', time()); if (file_exists($target) && date('Ymd', filemtime($target)) == $today) return; // we have already exported today $logFolder = Kurogo::getSiteVar('TMP_DIR'); $logfilecopy = $logFolder . "/mobi_log_copy.$today"; if (!is_writable($logFolder)) { throw new Exception("Unable to write to TMP_DIR $logFolder"); } if (!$outfile = fopen($target, 'a')) { error_log("could not open $target for writing"); return; } if (!rename($logfile, $logfilecopy)) { error_log("failed to rename $logfile to $logfilecopy"); return; } if (!touch($logfile)) { error_log("failed to create empty $logfile"); return; } $conn = SiteDB::connection(); $result = $conn->query( "SELECT day, platform, module, viewcount FROM $table WHERE day=(SELECT MAX(day) FROM $table)"); $stats = Array(); while ($row = $result->fetch()) { self::increment_array($stats, $row['day'], $row['platform'], $row['module']); } $infile = fopen($logfilecopy, 'r'); $date_length = strlen(date(Kurogo::getSiteVar('LOG_DATE_FORMAT'))); while (!feof($infile)) { $line = fgets($infile, 1024); fwrite($outfile, $line); if (preg_match(Kurogo::getSiteVar('LOG_DATE_PATTERN'), $line, $matches) == 0) continue; // the following match positions should also be defined where // the date regex is defined $day = sprintf("%s-%s-%s", $matches[3], $matches[1], $matches[2]); if (preg_match('/^.{' . $date_length . '} (\w+) (\w+):/', $line, $matches)) { $platform = $matches[1]; $module = $matches[2]; if ($module) { self::increment_array($stats, $day, $platform, $module); } } } fclose($outfile); fclose($infile); if ($stats) { $conn = SiteDB::connection(); $conn->beginTransaction(); $conn->lockTable("$table"); foreach ($stats as $day => $platforms) { foreach ($platforms as $platform => $modules) { foreach ($modules as $module => $count) { $sql = "INSERT INTO $table ( day, platform, module, viewcount ) VALUES (?,?,?,?)"; $conn->query($sql, array($day, $platform, $module,$count)); } } } $conn->unlockTable(); $conn->commit(); } unlink($logfilecopy); }