Commit 0c41f2d6 authored by Simon Welsh's avatar Simon Welsh

Refactor and search

git-svn-id: svn+ssh://lsgn/var/logbot-www@10 5f99df0c-6b0a-442a-b40a-040688e84a9d
parent 33ba5c45
<?php
final class DB {
private $con;
public static function inst() {
static $inst = null;
if(!$inst) {
$inst = new self;
}
return $inst;
}
private function __construct() {
$this->con = pg_connect('user=simon password=pass dbname=logbot');
}
public function query($sql) {
return pg_query($this->con, $sql);
}
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';
$res = $this->query($sql);
$results = array();
while($row = pg_fetch_assoc($res)) {
$results[] = $row;
}
return $results;
}
}
function search_form() {
$form = '<form method="get" action="search.php"><input type="text" name="q" /><input type="submit" value="Search logs" /></form>';
return $form;
}
function handle_log($id, $log, $format = 'H:i:s') {
$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> ';
switch($log['Command']) {
case 'PRIVMSG':
$html .= '<span class="irc-black">&lt;' . htmlentities($log['Nick']) . '&gt; ';
$html .= htmlentities($log['Text']);
$html .= '</span>';
break;
case 'ACTION':
$html .= '<span class="irc-brick">* ' . htmlentities($log['Nick']) . ' ';
$html .= htmlentities($log['Text']);
$html .= '</span>';
break;
case 'PART':
case 'JOIN':
$html .= '<span class="irc-green">* ' . htmlentities($log['Nick']) . ' has ';
$html .= $log['Command'] == 'PART' ? 'left' : 'joined';
$html .= ' #silverstripe</span>';
break;
case 'QUIT':
$html .= '<span class="irc-navy">* ' . htmlentities($log['Nick']) . ' quit (';
$html .= htmlentities($log['Text']);
$html .= ')</span>';
break;
case 'NICK':
$html .= '<span class="irc-green">* ' . htmlentities($log['Nick']) . ' is now known as ';
$html .= htmlentities($log['Target']);
$html .= '</span>';
break;
case 'TOPIC':
$html .= '<span class="irc-green">* ' . htmlentities($log['Nick']) . ' changed the topic to \'';
$html .= htmlentities($log['Text']);
$html .= '\'</span>';
break;
case 'NOTICE':
case 'MODE':
return null;
default:
$html = '<span id="' . $index . '" class="log">' . var_export($log, true);
break;
}
$html .= '</span>';
return $html;
}
function get_head() {
global $channel, $server, $nick, $robots, $title;
ob_start();
include 'head.inc';
$head = ob_get_contents();
ob_end_clean();
return $head;
}
function get_foot() {
global $channel, $server, $nick, $robots, $title;
ob_start();
include 'foot.inc';
$foot = ob_get_contents();
ob_end_clean();
return $foot;
}
function load_defaults() {
global $channel, $server, $nick, $robots, $title;
$defaults = array(
'channel' => '#silverstripe',
'server' => 'irc.freenode.net',
'nick' => 'ss-log',
'robots' => 'index,follow'
);
$defaults['title'] = sprintf('IRC Log for %s on %s, collected by %s', $defaults['channel'], $defaults['server'], $defaults['nick']);
foreach($defaults as $name => $val) {
if(!$$name) $$name = $val;
}
}
function build_db_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');
while($row = pg_fetch_array($res)) {
$time = strtotime($row[0]);
$year = date('Y', $time);
$month = date('F', $time);
$day = date('j', $time);
$dates[$year][$month][$day] = array($day, $row[0]);
}
}
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
$defaults = array(
'channel' => '#silverstripe',
'server' => 'irc.freenode.net',
'nick' => 'ss-log',
'robots' => 'index,follow'
);
$defaults['title'] = sprintf('IRC Log for %s on %s, collected by %s', $defaults['channel'], $defaults['server'], $defaults['nick']);
extract($defaults, EXTR_SKIP);
?>
<!doctype html>
<html>
<head>
......
<?php
$con = pg_connect('user=simon password=pass dbname=logbot');
$res = pg_query($con, 'SELECT DISTINCT DATE("Time") FROM "Log" WHERE DATE("Time") > \'2000-01-01\' ORDER BY DATE("Time") DESC');
error_reporting(E_ALL);
$dates = array();
while($row = pg_fetch_array($res)) {
$time = strtotime($row[0]);
$year = date('Y', $time);
$month = date('F', $time);
$day = date('j', $time);
$dates[$year][$month][$day] = array($day, $row[0]);
}
require_once 'funcs.php';
$html = '';
$dates = build_db_dates();
if(isset($_GET['date']) && preg_match('#^20[0-9]{2}-[01][0-9]-[0-3][0-9]$#', $_GET['date'])) {
$time = strtotime($_GET['date']);
$year = date('Y', $time);
$month = date('F', $time);
$day = date('j', $time);
$body = '<p><a href="index.php">Index</a></p><h2>IRC Log for ';
$body .= date('j F Y', $time);
$body .= '</h2>';
$body .= search_form();
$body .= '<p>Timestamps are in NZT (<a href="http://timeanddate.com/worldclock/city.html?n=264" title="Click here to see what time it is right now in Wellington, New Zealand">New Zealand Time</a>).</p>';
if(isset($dates[$year][$month][$day])) {
$robots = 'index,nofollow';
$html .= '<p><a href="index.php">Index</a></p><h2>IRC Log for ';
$html .= date('j F Y', $time);
$html .= '</h2><p>Timestamps are in NZT (<a href="http://timeanddate.com/worldclock/city.html?n=264" title="Click here to see what time it is right now in Wellington, New Zealand">New Zealand Time</a>).</p>';
$html .= '<div class="logs">';
$body .= '<div class="logs">';
$res = pg_query($con, '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" FROM "Log" WHERE DATE("Time") = \'' . date('Y-m-d', $time) . '\' ORDER BY "Time" ASC');
$id = 0;
while($row = pg_fetch_assoc($res)) {
++$id;
// <span class="irc-brick">* wmk waves back</span><br />
$logTime = strtotime($row['Time']);
$index = 'log_' . $row['ID'];
$log = '<span id="' . $index . '" class="log">';
$log .= '<span class="irc-linenum"><a name="' . ($id) . '"></a><a href="#' . $index . '">' . $id . '</a>:</span><span class="irc-date">[';
$log .= date('H:i:s', $logTime) . ']</span> ';
switch($row['Command']) {
case 'PRIVMSG':
$log .= '<span class="irc-black">&lt;' . htmlentities($row['Nick']) . '&gt; ';
$log .= htmlentities($row['Text']);
$log .= '</span>';
break;
case 'ACTION':
$log .= '<span class="irc-brick">* ' . htmlentities($row['Nick']) . ' ';
$log .= htmlentities($row['Text']);
$log .= '</span>';
break;
case 'PART':
case 'JOIN':
$log .= '<span class="irc-green">* ' . htmlentities($row['Nick']) . ' has ';
$log .= $row['Command'] == 'PART' ? 'left' : 'joined';
$log .= ' #silverstripe</span>';
break;
case 'QUIT':
$log .= '<span class="irc-navy">* ' . htmlentities($row['Nick']) . ' quit (';
$log .= htmlentities($row['Text']);
$log .= ')</span>';
break;
case 'NICK':
$log .= '<span class="irc-green">* ' . htmlentities($row['Nick']) . ' is now known as ';
$log .= htmlentities($row['Target']);
$log .= '</span>';
break;
case 'TOPIC':
$log .= '<span class="irc-green">* ' . htmlentities($row['Nick']) . ' changed the topic to \'';
$log .= htmlentities($row['Text']);
$log .= '\'</span>';
break;
case 'NOTICE':
case 'MODE':
--$id;
continue 2;
default:
$log = '<span id="' . $index . '" class="log">' . var_export($row, true);
break;
$log = handle_log(++$id, $row);
if($log) {
$body .= $log . '<br />';
} else {
--$id;
}
$log .= '</span><br />';
$html .= $log;
}
$html .= '</div>';
$body .= '</div>';
} elseif(file_exists('old/' . date('Y-m-d', $time) . '.log')) {
$robots = 'index,nofollow';
$html .= '<p><a href="index.php">Index</a></p><h2>IRC Log for ';
$html .= date('j F Y', $time);
$html .= '</h2><p>Timestamps are in NZT (<a href="http://timeanddate.com/worldclock/city.html?n=264" title="Click here to see what time it is right now in Wellington, New Zealand">New Zealand Time</a>).</p>';
$html .= file_get_contents('old/' . date('Y-m-d', $time) . '.log');
$html .= '</div>';
$body .= file_get_contents('old/' . date('Y-m-d', $time) . '.log');
} else {
goto NoLog;
}
} else {
NoLog:
$body = search_form();
$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);
}
$dates = build_dates();
if(count($dates) > 1) {
$html .= '<p>Year:';
$body .= '<p>Year:';
foreach(array_keys($dates) as $year) {
$html .= " <a href='#$year'>$year</a>";
$body .= " <a href='#$year'>$year</a>";
}
$html .= '</p>';
$body .= '</p>';
}
foreach($dates as $year => $months) {
$html .= "<h3><a name='$year'>$year</a></h3><div class='year'>";
$body .= "<h3><a name='$year'>$year</a></h3><div class='year'>";
if(count($months) > 1) {
$html .= '<p>Month:';
$body .= '<p>Month:';
foreach(array_keys($months) as $month) {
$html .= " <a href='#{$year}_$month'>$month</a>";
$body .= " <a href='#{$year}_$month'>$month</a>";
}
$html .= '</p>';
$body .= '</p>';
}
foreach($months as $month => $days) {
$html .= "<h4><a name='{$year}_$month'>$month</a></h4><div class='month'><ul>";
$body .= "<h4><a name='{$year}_$month'>$month</a></h4><div class='month'><ul>";
foreach($days as $day) {
$html .= "<li><a href='index.php?date=$day[1]'>$day[0] $month $year</a></li>";
$body .= "<li><a href='index.php?date=$day[1]'>$day[0] $month $year</a></li>";
}
$html .= '</ul></div>';
$body .= '</ul></div>';
}
$html .= '</div>';
$body .= '</div>';
}
}
ob_start();
include 'head.inc';
$head = ob_get_contents();
ob_end_clean();
load_defaults();
ob_start();
include 'foot.inc';
$foot .= ob_get_contents();
ob_end_clean();
$head = get_head();
$foot = get_foot();
echo $head, $html, $foot;
echo $head, $body, $foot;
<?php
error_reporting(E_ALL);
require_once 'funcs.php';
if(empty($_GET['q']) || !trim($_GET['q'])) {
header('Location: index.php');
exit;
}
$robots = 'noindex,nofollow';
$term = $_GET['q'];
$results = DB::inst()->search($term);
$term = htmlentities($term);
$id = 0;
$body = '<p><a href="index.php">Index</a></p>';
$body .= search_form();
$body .= '<h2>Search results for "';
$body .= $term;
$body .= '"</h2><p>Timestamps are in NZT (<a href="http://timeanddate.com/worldclock/city.html?n=264" title="Click here to see what time it is right now in Wellington, New Zealand">New Zealand Time</a>).</p>';
if(empty($results)) {
$body .= '<strong>No results found</strong>';
} else {
$body .= '<div class="logs">';
foreach($results as $row) {
$log = handle_log(++$id, $row, 'd/m/Y H:i:s');
if($log) {
$body .= $log . '<br />';
} else {
--$id;
}
}
$body .= '</div>';
}
load_defaults();
$head = get_head();
$foot = get_foot();
echo $head, $body, $foot;
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