Skip to content

Commit

Permalink
Use league/climate in ColorCLI class
Browse files Browse the repository at this point in the history
  • Loading branch information
DariusIII committed Oct 19, 2018
1 parent 78148e5 commit bafe7c6
Show file tree
Hide file tree
Showing 66 changed files with 706 additions and 806 deletions.
36 changes: 15 additions & 21 deletions Blacklight/Backfill.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function backfillAllGroups($groupName = '', $articles = '', $type = ''):
);

if ($this->_echoCLI) {
ColorCLI::doEcho(ColorCLI::header($dMessage), true);
ColorCLI::header($dMessage);
}

if ($articles !== '' && ! is_numeric($articles)) {
Expand All @@ -136,7 +136,7 @@ public function backfillAllGroups($groupName = '', $articles = '', $type = ''):
$dMessage = 'Starting group '.$counter.' of '.$groupCount;

if ($this->_echoCLI) {
ColorCLI::doEcho(ColorCLI::header($dMessage), true);
ColorCLI::header($dMessage);
}
}
$this->backfillGroup($groupArr, $groupCount - $counter, $articles);
Expand All @@ -146,13 +146,13 @@ public function backfillAllGroups($groupName = '', $articles = '', $type = ''):
$dMessage = 'Backfilling completed in '.number_format(microtime(true) - $allTime, 2).' seconds.';

if ($this->_echoCLI) {
ColorCLI::doEcho(ColorCLI::primary($dMessage), true);
ColorCLI::primary($dMessage);
}
} else {
$dMessage = 'No groups specified. Ensure groups are added to database for updating.';

if ($this->_echoCLI) {
ColorCLI::doEcho(ColorCLI::warning($dMessage), true);
ColorCLI::warning($dMessage);
}
}
}
Expand All @@ -165,7 +165,7 @@ public function backfillAllGroups($groupName = '', $articles = '', $type = ''):
* @param int|string $articles
*
* @return void
* @throws \Exception
* @throws \Throwable
*/
public function backfillGroup($groupArr, $left, $articles = ''): void
{
Expand All @@ -184,7 +184,7 @@ public function backfillGroup($groupArr, $left, $articles = ''): void
'. Otherwise the group is dead, you must disable it.';

if ($this->_echoCLI) {
ColorCLI::doEcho(ColorCLI::error($dMessage), true);
ColorCLI::error($dMessage);
}

return;
Expand All @@ -200,7 +200,7 @@ public function backfillGroup($groupArr, $left, $articles = ''): void
}

if ($this->_echoCLI) {
ColorCLI::doEcho(ColorCLI::primary('Processing '.$groupName), true);
ColorCLI::primary('Processing '.$groupName);
}

// Check if this is days or post backfill.
Expand Down Expand Up @@ -233,28 +233,26 @@ public function backfillGroup($groupArr, $left, $articles = ''): void
}

if ($this->_echoCLI) {
ColorCLI::doEcho(ColorCLI::notice($dMessage), true);
ColorCLI::notice($dMessage);
}

return;
}

if ($this->_echoCLI) {
ColorCLI::doEcho(
ColorCLI::primary(
ColorCLI::primary(
'Group '.
$groupName.
"'s oldest article is ".
number_format($data['first']).
', newest is '.
number_format($data['last']).
".\nOur target article is ".
'.Our target article is '.
number_format($targetpost).
'. Our oldest article is article '.
number_format($groupArr['first_record']).
'.'
), true
);
);
}

// Set first and last, moving the window by max messages.
Expand All @@ -270,16 +268,15 @@ public function backfillGroup($groupArr, $left, $articles = ''): void
$done = false;
while ($done === false) {
if ($this->_echoCLI) {
ColorCLI::doEcho(
color('Getting '.
ColorCLI::header('Getting '.
number_format($last - $first + 1).
' articles from '.
$groupName.
', '.
$left.
' group(s) left. ('.
number_format($first - $targetpost).
' articles in queue')->fg('yellow'), true);
' articles in queue');
}

flush();
Expand Down Expand Up @@ -318,17 +315,14 @@ public function backfillGroup($groupArr, $left, $articles = ''): void
}

if ($this->_echoCLI) {
ColorCLI::doEcho(
ColorCLI::primary(
ColorCLI::primary(
PHP_EOL.
'Group '.
$groupName.
' processed in '.
number_format(microtime(true) - $startGroup, 2).
' seconds.'
),
true
);
);
}
}

Expand Down
95 changes: 30 additions & 65 deletions Blacklight/Binaries.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,14 +346,14 @@ public function updateGroup($groupMySQL, $maxHeaders = 0): void
}

if ($this->_echoCLI) {
ColorCLI::doEcho(ColorCLI::primary('Processing '.$groupMySQL['name']), true);
ColorCLI::primary('Processing '.$groupMySQL['name']);
}

// Attempt to repair any missing parts before grabbing new ones.
if ((int) $groupMySQL['last_record'] !== 0) {
if ($this->_partRepair) {
if ($this->_echoCLI) {
ColorCLI::doEcho(ColorCLI::primary('Part repair enabled. Checking for missing parts.'), true);
ColorCLI::primary('Part repair enabled. Checking for missing parts.');
}
$this->partRepair($groupMySQL);

Expand All @@ -363,7 +363,7 @@ public function updateGroup($groupMySQL, $maxHeaders = 0): void
$this->partRepair($groupMySQL, $tableNames);
}
} elseif ($this->_echoCLI) {
ColorCLI::doEcho(ColorCLI::primary('Part repair disabled by user.'), true);
ColorCLI::primary('Part repair disabled by user.');
}
}

Expand Down Expand Up @@ -431,8 +431,7 @@ public function updateGroup($groupMySQL, $maxHeaders = 0): void
// If total is bigger than 0 it means we have new parts in the newsgroup.
if ($total > 0) {
if ($this->_echoCLI) {
ColorCLI::doEcho(
ColorCLI::primary(
ColorCLI::primary(
(
(int) $groupMySQL['last_record'] === 0
? 'New group '.$groupNNTP['group'].' starting with '.
Expand All @@ -447,9 +446,7 @@ public function updateGroup($groupMySQL, $maxHeaders = 0): void
" for next pass.\nServer oldest: ".number_format($groupNNTP['first']).
' Server newest: '.number_format($groupNNTP['last']).
' Local newest: '.number_format($groupMySQL['last_record'])
),
true
);
);
}

$done = false;
Expand All @@ -468,13 +465,11 @@ public function updateGroup($groupMySQL, $maxHeaders = 0): void
$first++;

if ($this->_echoCLI) {
ColorCLI::doEcho(
ColorCLI::header(
ColorCLI::header(
PHP_EOL.'Getting '.number_format($last - $first + 1).' articles ('.number_format($first).
' to '.number_format($last).') from '.$groupMySQL['name'].' - ('.
number_format($groupLast - $last).' articles in queue).'
)
);
);
}

// Get article headers from newsgroup.
Expand Down Expand Up @@ -539,24 +534,18 @@ public function updateGroup($groupMySQL, $maxHeaders = 0): void
}

if ($this->_echoCLI) {
ColorCLI::doEcho(
ColorCLI::primary(
ColorCLI::primary(
PHP_EOL.'Group '.$groupMySQL['name'].' processed in '.
number_format(microtime(true) - $startGroup, 2).' seconds.'
),
true
);
);
}
} elseif ($this->_echoCLI) {
ColorCLI::doEcho(
ColorCLI::primary(
ColorCLI::primary(
'No new articles for '.$groupMySQL['name'].' (first '.number_format($first).
', last '.number_format($last).', grouplast '.number_format($groupMySQL['last_record']).
', total '.number_format($total).")\n".'Server oldest: '.number_format($groupNNTP['first']).
' Server newest: '.number_format($groupNNTP['last']).' Local newest: '.number_format($groupMySQL['last_record'])
),
true
);
);
}
}

Expand Down Expand Up @@ -782,13 +771,10 @@ public function scan($groupMySQL, $first, $last, $type = 'update', $missingParts
$this->addMissingParts($rangeNotReceived, $this->tableNames['prname'], $this->groupMySQL['id']);

if ($this->_echoCLI) {
ColorCLI::doEcho(
ColorCLI::alternate(
ColorCLI::alternate(
'Server did not return '.$notReceivedCount.
' articles from '.$this->groupMySQL['name'].'.'
),
true
);
);
}
}
unset($rangeNotReceived);
Expand Down Expand Up @@ -1022,14 +1008,11 @@ protected function updateBlacklistUsage(): void
*/
protected function outputHeaderInitial(): void
{
ColorCLI::doEcho(
ColorCLI::primary(
ColorCLI::primary(
'Received '.\count($this->headersReceived).
' articles of '.number_format($this->last - $this->first + 1).' requested, '.
$this->headersBlackListed.' blacklisted, '.$this->notYEnc.' not yEnc.'
),
true
);
);
}

/**
Expand All @@ -1039,8 +1022,7 @@ protected function outputHeaderDuration(): void
{
$currentMicroTime = microtime(true);
if ($this->_echoCLI) {
ColorCLI::doEcho(
ColorCLI::alternateOver($this->timeHeaders.'s').
ColorCLI::alternateOver($this->timeHeaders.'s').
ColorCLI::primaryOver(' to download articles, ').
ColorCLI::alternateOver($this->timeCleaning.'s').
ColorCLI::primaryOver(' to process collections, ').
Expand All @@ -1049,9 +1031,7 @@ protected function outputHeaderDuration(): void
ColorCLI::alternateOver(number_format($currentMicroTime - $this->startPR, 2).'s').
ColorCLI::primaryOver(' for part repair, ').
ColorCLI::alternateOver(number_format($currentMicroTime - $this->startLoop, 2).'s').
ColorCLI::primary(' total.'),
true
);
ColorCLI::primary(' total.');
}
}

Expand Down Expand Up @@ -1106,22 +1086,19 @@ public function partRepair($groupArr, $tables = ''): void
DB::commit();
}
if ($e->getMessage() === 'SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction') {
ColorCLI::doEcho(ColorCLI::notice('Deadlock occurred'));
ColorCLI::notice('Deadlock occurred');
DB::rollBack();
}
}

$missingCount = \count($missingParts);
if ($missingCount > 0) {
if ($this->_echoCLI) {
ColorCLI::doEcho(
ColorCLI::primary(
ColorCLI::primary(
'Attempting to repair '.
number_format($missingCount).
' parts.'
),
true
);
);
}

// Loop through each part to group into continuous ranges with a maximum range of messagebuffer/4.
Expand Down Expand Up @@ -1199,14 +1176,11 @@ public function partRepair($groupArr, $tables = ''): void
}

if ($this->_echoCLI) {
ColorCLI::doEcho(
ColorCLI::primary(
ColorCLI::primary(
PHP_EOL.
number_format($partsRepaired).
' parts repaired.'
),
true
);
);
}
}

Expand Down Expand Up @@ -1334,12 +1308,9 @@ public function daytopost($days, $data): string
}

if ($this->_echoCLI) {
ColorCLI::doEcho(
ColorCLI::primary(
ColorCLI::primary(
'Searching for an approximate article number for group '.$data['group'].' '.$days.' days back.'
),
true
);
);
}

// Pick the middle to start with
Expand Down Expand Up @@ -1395,13 +1366,10 @@ public function daytopost($days, $data): string

$wantedArticle = (int) $wantedArticle;
if ($this->_echoCLI) {
ColorCLI::doEcho(
ColorCLI::primary(
ColorCLI::primary(
PHP_EOL.'Found article #'.$wantedArticle.' which has a date of '.date('r', $articleTime).
', vs wanted date of '.date('r', $goalTime).'. Difference from goal is '.Carbon::createFromTimestamp($goalTime)->diffInDays(Carbon::createFromTimestamp($articleTime)).'days.'
),
true
);
);
}

return $wantedArticle;
Expand Down Expand Up @@ -1655,10 +1623,7 @@ public function delete($collectionID): void
private function log($message, $method, $color): void
{
if ($this->_echoCLI) {
ColorCLI::doEcho(
ColorCLI::$color($message.' ['.__CLASS__."::$method]"),
true
);
ColorCLI::$color($message.' ['.__CLASS__."::$method]");
}
}

Expand Down Expand Up @@ -1714,11 +1679,11 @@ protected function runQuery($query)
try {
return DB::insert($query);
} catch (QueryException $e) {
ColorCLI::doEcho(ColorCLI::debug('Query error occurred.'), true);
ColorCLI::debug('Query error occurred.');
} catch (\PDOException $e) {
ColorCLI::doEcho(ColorCLI::debug('Query error occurred.'), true);
ColorCLI::debug('Query error occurred.');
} catch (\Throwable $e) {
ColorCLI::doEcho(ColorCLI::debug('Query error occurred.'), true);
ColorCLI::debug('Query error occurred.');
}

return false;
Expand Down
Loading

0 comments on commit bafe7c6

Please sign in to comment.