Mercurial > hg > pnewss
diff fetchnews.php @ 3:0dcdb73cbcbf
Increased article body length to 16MB in database scheme, cleaned up the code, added a script to forcefully reload an individual article
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Tue, 12 Apr 2011 01:47:09 +0200 |
parents | 40e545510a57 |
children | 01dc7eeaf5df |
line wrap: on
line diff
--- a/fetchnews.php Tue Apr 12 00:29:41 2011 +0200 +++ b/fetchnews.php Tue Apr 12 01:47:09 2011 +0200 @@ -1,7 +1,7 @@ +#!/usr/bin/php <?php chdir(__DIR__); -require_once './pdo.php'; -require_once './config.php'; +require_once './common.php'; foreach ($db->evalAllAssoc('SELECT * FROM `peers`') as $peer) { $socket = stream_socket_client($peer['address']); @@ -44,26 +44,17 @@ $db->insert('INSERT INTO `groupmessages` (`group`, `message`) VALUES (?, ?)', array($group['id'], $message['id'])); } } else { - nntp_writeline($socket, 'HEAD '.$i); + nntp_writeline($socket, 'ARTICLE '.$i); $line = nntp_readline($socket); $code = strtok($line, " \t"); - if ($code != 221) die("Error code $code from $peer[address]\n"); + if ($code != 220) die("Error code $code from $peer[address]\n"); strtok(" \t"); //article number $lines = nntp_readlines($socket); - nntp_removeheader(&$lines, 'Xref'); - nntp_updatepath(&$lines, 'pNewss.Core.UCIS.nl'); - $header = implode("\r\n", $lines); - - nntp_writeline($socket, 'BODY '.$i); - $line = nntp_readline($socket); - $code = strtok($line, " \t"); - if ($code != 222) die("Error code $code from $peer[address]\n"); - strtok(" \t"); //article number - $lines = nntp_readlines($socket); - $body = implode("\r\n", $lines); - - $id = $db->insert('INSERT INTO `messages` (`messageid`, `header`, `body`) VALUES (?, ?, ?)', array($messageid, $header, $body)); - $db->insert('INSERT INTO `groupmessages` (`group`, `message`) VALUES (?, ?)', array($group['id'], $id)); + try { + nntp_article_store($lines); + } catch (Exception $ex) { + writelog($ex->getMessage()); + } } } $db->update('UPDATE `peergroups` SET `low` = ?, `high` = ? WHERE `peer` = ? AND `group` = ?', array($low, $high, $peergroup['peer'], $peergroup['group'])); @@ -113,41 +104,6 @@ fclose($socket); } -function nntp_removeheader(&$lines, $header) { - $header = strtoupper($header).':'; - $hlen = strlen($header); - foreach ($lines as $key => $line) if (strtoupper(substr($line, 0, $hlen)) == $header) unset($lines[$key]); -} -function nntp_updatepath(&$lines, $value) { - $header = strtoupper('Path').':'; - $hlen = strlen($header); - $found = FALSE; - foreach ($lines as &$line) if (strtoupper(substr($line, 0, $hlen)) == $header) { - $parts = explode(': ', $line, 2); - $parts[1] = $value.'!'.$parts[1]; - $line = implode(': ', $parts); - $found = TRUE; - break; - } - if (!$found) $lines[] = 'Path: '.$value; +function writelog($line) { + print($line."\n"); } - -function nntp_readline($socket) { - $line = rtrim(fgets($socket, 512), "\r\n"); - print('R: '.$line."\n"); - return $line; -} -function nntp_writeline($socket, $line) { - print('W: '.$line."\n"); - fwrite($socket, $line."\r\n"); -} -function nntp_readlines($socket) { - $line = nntp_readline($socket); - $lines = array(); - while ($line != '.' && $line !== FALSE && $line !== FALSE) { - $lines[] = $line; - $line = nntp_readline($socket); - } - if ($line != '.') die("Unexpected end of message header\n"); - return $lines; -}