Commit ae1dfeb7 authored by Simon Welsh's avatar Simon Welsh

Importing of old logs, ditches any sort of FS-backed logs :)

git-svn-id: svn+ssh://lsgn/var/logbot-www@12 5f99df0c-6b0a-442a-b40a-040688e84a9d
parent 4aabb3ee
......@@ -2,6 +2,7 @@
final class DB {
private $con;
private $inst;
public static function inst() {
static $inst = null;
......@@ -21,7 +22,7 @@ final class DB {
public function search($value) {
$value = pg_escape_literal($this->con, $value);
$sql = 'SELECT "Time", "Text", "Command", "Target", "Nick", "ID", ts_rank_cd("Search", query, 16 | 32) AS "Rank" FROM "Log", plainto_tsquery(\'english\', ' . $value . ') query WHERE "Search" @@ query ORDER BY "Rank" DESC';
$sql = 'SELECT "Time", "Text", "Command", "Target", "Nick", "ID", "Old", ts_rank_cd("Search", query, 16 | 32) AS "Rank" FROM "Log", plainto_tsquery(\'english\', ' . $value . ') query WHERE "Search" @@ query ORDER BY "Rank" DESC';
$res = $this->query($sql);
$results = array();
while($row = pg_fetch_assoc($res)) {
......@@ -29,6 +30,13 @@ final class DB {
}
return $results;
}
public function add_row($time, $text, $cmd, $target, $nick) {
if(!$this->inst) {
$this->inst = pg_prepare($this->con, 'inst_query', 'INSERT INTO "Log" ("Time", "Text", "Command", "Target", "Nick", "Old") VALUES($1, $2, $3, $4, $5, true)');
}
pg_execute($this->con, 'inst_query', func_get_args());
}
}
function search_form() {
......@@ -37,14 +45,19 @@ function search_form() {
return $form;
}
function handle_log($id, $log, $format = 'H:i:s') {
function handle_log($id, $log, $format = ['H:i:s', 'H:i']) {
$logTime = strtotime($log['Time']);
$index = 'log_' . $log['ID'];
$day = date('Y-m-d', $logTime);
$html = '<span id="' . $index . '" class="log">';
$html .= '<span class="irc-linenum"><a name="' . ($id) . '"></a><a href="index.php?date=' . $day .
'#' . $index . '">' . $id . '</a>:</span><span class="irc-date">[';
$html .= date($format, $logTime) . ']</span> ';
if($log['Old']) {
$html .= date($format[1], $logTime);
} else {
$html .= date($format[0], $logTime);
}
$html .= ']</span> ';
switch($log['Command']) {
case 'PRIVMSG':
$html .= '<span class="irc-black">&lt;' . htmlentities($log['Nick']) . '&gt; ';
......@@ -79,6 +92,7 @@ function handle_log($id, $log, $format = 'H:i:s') {
break;
case 'NOTICE':
case 'MODE':
case 'KICK':
return null;
default:
$html = '<span id="' . $index . '" class="log">' . var_export($log, true);
......@@ -122,7 +136,7 @@ function load_defaults() {
}
}
function build_db_dates() {
function build_dates() {
static $dates = array();
if(!$dates) {
$res = DB::inst()->query('SELECT DISTINCT DATE("Time") FROM "Log" WHERE DATE("Time") > \'2000-01-01\' ORDER BY DATE("Time") DESC');
......@@ -136,30 +150,3 @@ function build_db_dates() {
}
return $dates;
}
function build_fs_dates() {
static $dates = array();
if(!$dates) {
$dir = opendir('old');
$toParse = array();
while(false !== ($file = readdir($dir))) {
if(strpos($file, ".log") == 10) {
$toParse[] = substr($file, 0, 10);
}
}
rsort($toParse);
foreach($toParse as $date) {
$time = strtotime($date);
$year = date('Y', $time);
$month = date('F', $time);
$day = date('j', $time);
$dates[$year][$month][$day] = array($day, $date);
}
}
return $dates;
}
function build_dates() {
return array_replace_recursive(build_db_dates(), build_fs_dates());
}
<?php
if(php_sapi_name() != "cli") {
die('Can only import from the command line, sucker!');
}
error_reporting(E_ALL);
require_once 'funcs.php';
$limit = '2013-05-17';
$args = $_SERVER['argv'];
array_shift($args);
$c = 0;
foreach($args as $file) {
if(!preg_match('#(\d{4}-\d{2}-\d{2})\.log$#', $file, $matches)) {
continue;
}
$date = $matches[1];
if($date >= $limit) {
echo "Skipping $date\n";
continue;
}
$contents = file($file);
if(!$contents) continue;
foreach($contents as $line) {
// Split line into parts
if(!preg_match('#\[(?:<a name="[0-9_]+">)?([0-9]{1,2}:[0-9]{2})(?:</a>)?]</span> <span class="([^"]+)">(.*)</span><br />#', $line, $matches)) {
echo "Skipping line $line";
continue;
}
$time = $matches[1];
$colour = $matches[2];
$msg = $matches[3];
// Strip links
$msg = preg_replace('#<a href="[^>]+">([^>]+)</a>#', '$1', $msg);
$msg = str_replace(array('&lt;', '&gt;', '&amp;'), array('<', '>', '&'), $msg);
// stmt = con.Prepare("INSERT INTO \"Log\" (\"Time\", \"Text\", \"Command\", \"Target\", \"Nick\") VALUES($1, $2, $3, $4, $5)")
// stmt.Exec(line.Time, line.Text(), line.Cmd, line.Target(), line.Nick)
if(strlen($time) == 3) $time = "0$time";
$time = "$date $time";
// expect back $text, $cmd, $target, $nick
switch($colour) {
case 'irc-green':
// There's a lot in here
preg_match('#^\* (\S+) #', $msg, $matches);
$nick = $matches[1];
$target = '#silverstripe';
$msg = substr($msg, strlen($matches[0]));
switch($msg[0]) {
case '(':
// join or part
$text = '';
preg_match('#^\([^)]+\) has (joined|left)#', $msg, $matches);
$cmd = $matches[1] == 'joined' ? 'JOIN' : 'PART';
break;
case 'w':
// kicked
case 's':
// Mode, nop
continue 3;
case 'i':
// nick or "topic is'"
if($msg[3] == "'") continue 3;
preg_match('#is now known as (.*)$#', $msg, $matches);
$target = $matches[1];
$text = '';
$cmd = 'NICK';
break;
case 'c':
// topic
$cmd = 'TOPIC';
preg_match('#changes topic to \'(.*)\'$#', $msg, $matches);
$text = $matches[1];
break;
case 'b':
// by-line
continue 3;
default:
// No idea
var_dump($msg);
continue 3;
}
break;
case 'irc-black':
// Two options: <- *sender* message -or- <sender> message
if($msg[1] == '-') {
preg_match('#^<- \*([^\*]+)\* (.*)$#', $msg, $matches);
} else {
preg_match('#^<([^>]+)> (.*)$#', $msg, $matches);
}
$text = $matches[2];
$cmd = 'PRIVMSG';
$target = '#silverstripe';
$nick = $matches[1];
break;
case 'irc-brown':
// Notice
// -sender?- message
preg_match('#^-(\S*)- (.*)$#', $msg, $matches);
$text = $matches[2];
$cmd = 'NOTICE';
$target = $nick = $matches[1];
break;
case 'irc-navy':
if($msg == '* Disconnected.') {
continue 2;
}
preg_match('#^\* (\S+) \(.+?\) Quit \((.*)\)$#', $msg, $matches);
$text = $matches[2];
$cmd = 'QUIT';
$target = '#silverstripe';
$nick = $matches[1];
break;
case 'irc-brick':
// Always * sender message
preg_match('#^\* (\S+) (.*)$#', $msg, $matches);
$text = $matches[2];
$cmd = 'ACTION';
$target = '#silverstripe';
$nick = $matches[1];
break;
case 'irc-red':
// ping, time and version: nop.
continue 2;
default:
var_dump($matches);
continue 2;
}
DB::inst()->add_row($time, $text, $cmd, $target, $nick);
}
}
......@@ -4,7 +4,7 @@ error_reporting(E_ALL);
require_once 'funcs.php';
$dates = build_db_dates();
$dates = build_dates();
if(isset($_GET['date']) && preg_match('#^20[0-9]{2}-[01][0-9]-[0-3][0-9]$#', $_GET['date'])) {
$time = strtotime($_GET['date']);
......@@ -22,7 +22,7 @@ if(isset($_GET['date']) && preg_match('#^20[0-9]{2}-[01][0-9]-[0-3][0-9]$#', $_G
$robots = 'index,nofollow';
$body .= '<div class="logs">';
$res = DB::inst()->query('SELECT "Time", "Text", "Command", "Target", "Nick", "ID" FROM "Log" WHERE DATE("Time") = \'' . date('Y-m-d', $time) . '\' ORDER BY "Time" ASC');
$res = DB::inst()->query('SELECT "Time", "Text", "Command", "Target", "Nick", "ID", "Old" FROM "Log" WHERE DATE("Time") = \'' . date('Y-m-d', $time) . '\' ORDER BY "Time" ASC');
$id = 0;
while($row = pg_fetch_assoc($res)) {
......@@ -34,9 +34,6 @@ if(isset($_GET['date']) && preg_match('#^20[0-9]{2}-[01][0-9]-[0-3][0-9]$#', $_G
}
}
$body .= '</div>';
} elseif(file_exists('old/' . date('Y-m-d', $time) . '.log')) {
$robots = 'index,nofollow';
$body .= file_get_contents('old/' . date('Y-m-d', $time) . '.log');
} else {
goto NoLog;
}
......@@ -45,8 +42,6 @@ if(isset($_GET['date']) && preg_match('#^20[0-9]{2}-[01][0-9]-[0-3][0-9]$#', $_G
NoLog:
$body = search_form();
$dates = build_dates();
if(count($dates) > 1) {
$body .= '<p>Year:';
foreach(array_keys($dates) as $year) {
......
......@@ -29,7 +29,7 @@ if(empty($results)) {
} else {
$body .= '<div class="logs">';
foreach($results as $row) {
$log = handle_log(++$id, $row, 'd/m/Y H:i:s');
$log = handle_log(++$id, $row, ['d/m/Y H:i:s', 'd/m/Y H:i']);
if($log) {
$body .= $log . '<br />';
} else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment