Files
newznab-tmux/misc/update/tmux/bin/update_groups.php
T
2018-01-05 12:57:07 +01:00

60 lines
1.8 KiB
PHP

<?php
require_once dirname(__DIR__, 4).DIRECTORY_SEPARATOR.'bootstrap/autoload.php';
use nntmux\NNTP;
use nntmux\db\DB;
use nntmux\ColorCLI;
use nntmux\ConsoleTools;
$start = time();
$pdo = new DB();
$consoleTools = new ConsoleTools();
// Create the connection here and pass
$nntp = new NNTP(['Settings' => $pdo]);
if ($nntp->doConnect() !== true) {
exit(ColorCLI::error('Unable to connect to usenet.'));
}
echo ColorCLI::header('Getting first/last for all your active groups.');
$data = $nntp->getGroups();
if ($nntp->isError($data)) {
exit(ColorCLI::error('Failed to getGroups() from nntp server.'));
}
echo ColorCLI::header('Inserting new values into short_groups table.');
$pdo->queryExec('TRUNCATE TABLE short_groups');
// Put into an array all active groups
$res = $pdo->query('SELECT name FROM groups WHERE active = 1 OR backfill = 1');
foreach ($data as $newgroup) {
if (myInArray($res, $newgroup['group'], 'name')) {
$pdo->queryInsert(sprintf('INSERT INTO short_groups (name, first_record, last_record, updated) VALUES (%s, %s, %s, NOW())', $pdo->escapeString($newgroup['group']), $pdo->escapeString($newgroup['first']), $pdo->escapeString($newgroup['last'])));
echo ColorCLI::primary('Updated '.$newgroup['group']);
}
}
echo ColorCLI::header('Running time: '.$consoleTools->convertTimer(time() - $start));
function myInArray($array, $value, $key)
{
//loop through the array
foreach ($array as $val) {
//if $val is an array cal myInArray again with $val as array input
if (is_array($val)) {
if (myInArray($val, $value, $key)) {
return true;
}
} else {
//else check if the given key has $value as value
if ($array[$key] == $value) {
return true;
}
}
}
return false;
}