Created
August 8, 2014 12:11
-
-
Save neves/2d4d1d5718f7e53699c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$file = $argv[1]; | |
$pdo = new PDO('mysql:host=localhost;dbname=log', 'root', ''); | |
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
$stmt = $pdo->prepare("INSERT INTO logs SET | |
data = ?, | |
elapse = ?, | |
ip = ?, | |
method = ?, | |
url = ?, | |
path = ?, | |
query = ?, | |
ano_mes = ?, | |
dia = ?, | |
semana = ?, | |
hora = ?, | |
seconds = ? | |
"); | |
$linha = 1; | |
$fp = fopen($file, 'r'); | |
$pdo->exec("BEGIN"); | |
while ( $row = fgetcsv($fp, 0, "\t") ) { | |
echo "\r".number_format($linha, 0); | |
$row[1] = intval(floatval($row[1]) * 1000); | |
if ( ! strpos($row[0], '.') ) { | |
$row[0] = substr($row[0], 0, 19) . '.' . substr($row[0], -4); | |
} | |
$row[5] = null; | |
$row[6] = null; | |
$row[7] = null; | |
$row[8] = null; | |
$row[9] = null; | |
$row[10] = null; | |
$row[11] = null; | |
if ($row[4]) { | |
$row[5] = substr(parse_url($row[4], PHP_URL_PATH), 0, 255); | |
$row[6] = parse_url($row[4], PHP_URL_QUERY); | |
$dt = new DateTime($row[0]); | |
$row[7] = $dt->format('Ym'); | |
$row[8] = $dt->format('d'); | |
$row[9] = $dt->format('w'); | |
$row[10] = $dt->format('H'); | |
$row[11] = $dt->format('U'); | |
} | |
try { | |
$stmt->execute($row); | |
} catch(Exception $e) { | |
echo "\n$e"; | |
print_r($row); | |
} | |
$linha++; | |
} | |
$pdo->exec("COMMIT"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment