diff --git a/core/config.php b/core/config.php index d7ce928..81b4caf 100644 --- a/core/config.php +++ b/core/config.php @@ -76,5 +76,5 @@ /** * Флаг использования логирования */ - const USE_LOG = false; + const USE_LOG = true; diff --git a/core/cron.php b/core/cron.php index 1829821..32a1d98 100644 --- a/core/cron.php +++ b/core/cron.php @@ -9,6 +9,7 @@ try { (new MQ())->run(); + Core\MQTasks::sendBash();// test bash loading } catch (Throwable $t) { SystemFunctions::sendTelegram('CRON: Произошла ошибка' . PHP_EOL . $t->getMessage() . PHP_EOL . 'File: ' . $t->getFile() . PHP_EOL . 'Line: ' . $t->getLine()); echo 'Error: ' . $t->getMessage() . ' / line: ' . $t->getLine(); diff --git a/core/lib/Core/MQTasks.class.php b/core/lib/Core/MQTasks.class.php index e4a1ee3..b251a57 100644 --- a/core/lib/Core/MQTasks.class.php +++ b/core/lib/Core/MQTasks.class.php @@ -41,7 +41,9 @@ }, $frazes); $i = 0; + foreach ($frazes as $post) { + //sendTelegram($post); $text = str_replace('
', PHP_EOL, $post); $text = htmlspecialchars_decode($text); $text = str_replace('\'', '\\\'', $text); @@ -55,23 +57,89 @@ sleep(2); return 'Добавлено ' . $i . ' записей из ' . count($frazes); } -/* - public static function sendBash2(...$args): void + + public static function bashPage($page) { - - + /** @var DB $DB */ $DB = DB::getInstance(); - $html = file_get_contents('http://bashorg.org/random'); + $file = file_get_contents('http://bashorg.org/page/' . $page . '/'); + $file = preg_match_all('|
(.+)
|U', $file, $frazes); - //$file = preg_match_all('|
(.+)
|U',$html,$frazes); - $file = preg_match_all('
(.+?)
',$html,$top); - -print_r($top); $frazes = $frazes[1]; - $frazes = array_map(function($e){ + $frazes = array_map(function ($e) { return \iconv('windows-1251//IGNORE', 'UTF-8//IGNORE', $e); }, $frazes); - }*/ + $i =0; + foreach ($frazes as $post) { + //sendTelegram($post); + $text = str_replace('
', PHP_EOL, $post); + $text = htmlspecialchars_decode($text); + $text = str_replace('\'', '\\\'', $text); + $hash = md5($text); + $res = $DB->query('SELECT * FROM bashorg WHERE hash="' . $hash . '"'); + if (!$res) { + $i++; + $itemId = $DB->addItem('bashorg', ['hash' => $hash, 'text' => $text]); + sendTelegram('Добавлена цитата ID ' . $itemId . ' (' . $page . ' страница)' . PHP_EOL . $text); + } + } + sleep(2); + return 'bashPage: Добавлено ' . $i . ' записей из ' . count($frazes); + } + + public static function bashPageMeta($page) + { + /** @var DB $DB */ + $DB = DB::getInstance(); + + $file = file_get_contents('http://bashorg.org/page/' . $page . '/'); + $file = \iconv('windows-1251//IGNORE', 'UTF-8//IGNORE', $file); + preg_match_all('|(?<=)[\w\W]*?(?=(.+)|U', $frazes, $posts); + $posts = $posts[1]; + + preg_match_all('|Цитата #(.+)|U', $frazes, $ids); + $ids = $ids[1]; + + preg_match_all('| \| (.+) \| добавлено|U', $frazes, $dates); + $dates = $dates[1]; + + preg_match_all('|(.+)|U', $frazes, $ratings); + $ratings = $ratings[2]; + + $result = []; + foreach ($posts as $key => $post) { + $result[] = [ + 'post' => $post, + 'date' => $dates[$key], + 'rating' => $ratings[$key], + 'id' => $ids[$key], + ]; + } + + $i =0; + $j = 0; + foreach ($result as $element) { + $text = str_replace('
', PHP_EOL, $element['post']); + $text = htmlspecialchars_decode($text); + $text = str_replace('\'', '\\\'', $text); + $hash = md5($text); + $res = $DB->query('SELECT * FROM bashorg WHERE hash="' . $hash . '"'); + if (!$res) { + $i++; + $itemId = $DB->addItem('bashorg', ['hash' => $hash, 'text' => $text, 'etx_id' => (int)$element['id'], 'date' => $element['date'], 'rating' => $element['rating']]); + sendTelegram('Добавлена цитата ID ' . $itemId . ' (' . $page . ' страница)' . PHP_EOL . $text); + } else { + $j++; + $DB->update('bashorg', ['hash' => $hash], ['ext_id' => (int)$element['id'], 'date' => $element['date'], 'rating' => $element['rating']]); + } + } + sleep(2); + return 'bashMeta: Добавлено ' . $i . ', обновлено ' . $j . ' записей из ' . count($result); + } } \ No newline at end of file diff --git a/core/lib/Core/Models/MQ.class.php b/core/lib/Core/Models/MQ.class.php index 5c1094a..66a9572 100644 --- a/core/lib/Core/Models/MQ.class.php +++ b/core/lib/Core/Models/MQ.class.php @@ -93,6 +93,11 @@ */ private $runNow = false; + /** + * @var bool $useCheckDuplicates Флаг проверки задания на дубликат + */ + private $useCheckDuplicates = true; + /** * @var null $attempts Количество попыток выполнения задания */ @@ -119,6 +124,19 @@ return $this; } + /** + * Установка режима проверки задания на дубликат при создании + * + * @param bool useCheckDuplicates Флаг проверки задания на дубликат + * + * @return $this + */ + public function setCheckDuplicates(bool $useCheckDuplicates = true): self + { + $this->useCheckDuplicates = $useCheckDuplicates; + return $this; + } + /** * Установка количества попыток выполнения задания * @@ -390,7 +408,8 @@ } $params = $this->convertToJson($params); - if ($this->checkDuplicates($class, $method, $params)) { + if ($this->useCheckDuplicates === true + && $this->checkDuplicates($class, $method, $params)) { throw new CoreException('Попытка создания дубликата задания', CoreException::ERROR_DIPLICATE_TASK); } @@ -549,7 +568,7 @@ 'response' => $this->convertToJson($result), ] ); - $response->setStatus(self::STATUS_OK)->setResponse($this->convertToJson($result)); + $response->setStatus(self::STATUS_OK)->setExecutionTime($endTime)->setResponse($this->convertToJson($result)); $this->saveExecutedTask($taskId); if (USE_LOG) { @@ -582,7 +601,7 @@ $this->saveExecutedTask($taskId); } - $response->setStatus(self::STATUS_ERROR)->setResponse($t->getMessage()); + $response->setStatus(self::STATUS_ERROR)->setExecutionTime($endTime)->setResponse($t->getMessage()); if (USE_LOG) { Log::logToFile( @@ -594,8 +613,6 @@ false ); } - // TODO: Следующая строка нужна больше для отладки - в дальнейшем нужно удалить ее - //echo 'Exception: ' . $t->getFile() . ' в строке ' . $t->getLine() . PHP_EOL . $t->getMessage() . PHP_EOL; } return $response; diff --git a/core/lib/Core/Models/MQResponse.class.php b/core/lib/Core/Models/MQResponse.class.php index fc89ff0..e7fffe8 100644 --- a/core/lib/Core/Models/MQResponse.class.php +++ b/core/lib/Core/Models/MQResponse.class.php @@ -36,6 +36,11 @@ */ private $paramsJson = null; + /** + * @var string|null $executionTime Время выполнения задания + */ + private $executionTime = null; + /** * @var string|null $response Результат выполнения задания */ @@ -71,6 +76,29 @@ return $this; } + /** + * Получение времени выполнения задания + * + * @return ?string + */ + public function getExecutionTime(): ?string + { + return $this->executionTime; + } + + /** + * Установка времени выполнения задания + * + * @param string|null $executionTime Время выполнения + * + * @return $this + */ + public function setExecutionTime(?string $executionTime = null): self + { + $this->executionTime = $executionTime; + return $this; + } + /** * Получение статуса задания *