From 4b5df4f8292cbf54d97ca95708fb63f0acdd468f Mon Sep 17 00:00:00 2001 From: Karel Wintersky Date: Wed, 3 Aug 2022 18:24:52 +0300 Subject: [PATCH] 1.0.2 * [R] Release * [+] added helper * [*] cut-off footer fix * [-] removed "karelwintersky/arris.helpers" DEPS * [*] Curl downloader will be used only if curl/curl installed --- composer.json | 3 +- interfaces/LongreadsHelperInterface.php | 12 +++ interfaces/LongreadsInterface.php | 4 +- sources/Longreads.php | 38 ++++--- sources/LongreadsHelper.php | 131 ++++++++++++++++++++++++ 5 files changed, 170 insertions(+), 18 deletions(-) create mode 100644 interfaces/LongreadsHelperInterface.php create mode 100644 sources/LongreadsHelper.php diff --git a/composer.json b/composer.json index 384e213..6d7fd7f 100644 --- a/composer.json +++ b/composer.json @@ -18,8 +18,7 @@ "ext-pdo": "*", "ext-json": "*", "ext-curl": "*", - "psr/log": "^1.1", - "karelwintersky/arris.helpers": ">=0.4.0" + "psr/log": "^1.1" }, "suggest": { "curl/curl": "^2.3" diff --git a/interfaces/LongreadsHelperInterface.php b/interfaces/LongreadsHelperInterface.php new file mode 100644 index 0000000..eb72757 --- /dev/null +++ b/interfaces/LongreadsHelperInterface.php @@ -0,0 +1,12 @@ +api_request_types = [ @@ -118,10 +120,12 @@ public function __construct(PDO $pdo, array $options = [], $logger = null) $this->path_to_favicon = $options['path.favicon'] ?? ''; $this->path_to_footer_template = $options['path.footer_template'] ?? ''; - $this->option_cutoff_footer = $options['options.option_cutoff_footer'] ?? true; - $this->option_localize_media = $options['options.option_localize_media'] ?? true; + $this->option_cutoff_footer = (bool)($options['options.option_cutoff_footer'] ?? true); + $this->option_localize_media = (bool)($options['options.option_localize_media'] ?? true); $this->option_download_client = $options['options.download_client'] ?? 'native'; + $this->debug_write_raw_html = (bool)($options['debug.write_raw_html'] ?? false); + $this->sql_table = $options['sql.table'] ?? 'longreads'; if (is_array($options['projects'])) { @@ -257,7 +261,9 @@ public function import($id, $folder = null, string $import_mode = 'update') $html = $page->html; - file_put_contents("{$path_store}/index_raw.html", $html); + if ($this->debug_write_raw_html) { + file_put_contents("{$path_store}/index_raw.html", $html); + } // локализация favicon if ('' !== $this->path_to_favicon) { @@ -282,8 +288,8 @@ public function import($id, $folder = null, string $import_mode = 'update') $html = str_replace('src="/www.youtube.com', 'src="//www.youtube.com', $html); if ($this->option_cutoff_footer) { - $html = str_replace(["\n", "\r"], "", $html); - $html = preg_replace('#<\!--/allrecords-->.*?$#ium', "\n", $html); + $html = substr($html, 0, strpos($html, '')); + $html .= ''; } if ('' !== $this->path_to_footer_template) { @@ -339,7 +345,7 @@ public function import($id, $folder = null, string $import_mode = 'update') 'folder' => $folder ]; - $sql = DB::makeUpdateQuery($this->sql_table, $dataset, "`id` = {$id}"); + $sql = LongreadsHelper::makeUpdateQuery($this->sql_table, $dataset, "`id` = {$id}"); } else { @@ -362,7 +368,7 @@ public function import($id, $folder = null, string $import_mode = 'update') 'filename' => $page->filename ]; - $sql = DB::makeInsertQuery($this->sql_table, $dataset); + $sql = LongreadsHelper::makeInsertQuery($this->sql_table, $dataset); } $this->logger->debug('PDO SQL Query: ', [ str_replace("\r\n", "", $sql) ]); @@ -380,7 +386,7 @@ public function import($id, $folder = null, string $import_mode = 'update') // очищаем папку от файлов // удаляем папку if ($is_directory_created) { - FS::rmdir($path_store); + LongreadsHelper::rmdir($path_store); } $this->logger->debug("Возникла ошибка при импорте лонгрида: ", [ $e->getMessage() ]); @@ -430,11 +436,11 @@ public function add($page = null) if ($count > 0) { $state = 'update'; $this->logger->debug('Обновляем информацию о лонгриде в БД', [ $dataset['id'] ]); - $sql = DB::makeReplaceQuery($this->sql_table, $dataset); + $sql = LongreadsHelper::makeReplaceQuery($this->sql_table, $dataset); } else { $state = 'ok'; $this->logger->debug('Добавляем информацию о лонгриде в БД', [ $dataset['id'] ]); - $sql = DB::makeInsertQuery($this->sql_table, $dataset); + $sql = LongreadsHelper::makeInsertQuery($this->sql_table, $dataset); } $this->logger->debug('PDO SQL Query: ', [ str_replace("\r\n", "", $sql) ]); @@ -658,7 +664,7 @@ private function getPageFullExport($id) private function downloadFile($from, $to) { - if ($this->option_download_client === 'curl') { + if ($this->option_download_client === 'curl' && class_exists('\Curl\Curl')) { return $this->downloadFileCurl($from, $to); } else { return $this->downloadFileNative($from, $to); @@ -679,7 +685,7 @@ private function downloadFileCurl($from, $to): bool throw new RuntimeException("Ошибка создания файла `{$to}`"); } - $curl = new Curl(); + $curl = new \Curl\Curl(); $curl->setOpt(CURLOPT_FILE, $file_handle); $curl->get($from); @@ -729,6 +735,8 @@ private function makeApiURI(string $command, array $http_request_query) : "http://api.tildacdn.info/{$this->api_options['version']}/{$command}/?" . http_build_query( $http_request_query ); } + + } # -eof- diff --git a/sources/LongreadsHelper.php b/sources/LongreadsHelper.php new file mode 100644 index 0000000..44e6b34 --- /dev/null +++ b/sources/LongreadsHelper.php @@ -0,0 +1,131 @@ + $value) { + if (strtoupper(trim($value)) === 'NOW()') { + $fields[] = "`{$index}` = NOW()"; + unset($dataset[ $index ]); + continue; + } + + $fields[] = "`{$index}` = :{$index}"; + } + + $query .= implode(', ', $fields); + + $query .= "{$where}; "; + + return $query; + } + + /** + * Строит INSERT-запрос на основе массива данных для указанной таблицы. + * В массиве допустима конструкция 'key' => 'NOW()' + * В этом случае она будет добавлена в запрос и удалена из набора данных (он пере). + * + * @param $table -- таблица + * @param $dataset -- передается по ссылке, мутабелен + * @return string -- результирующая строка запроса + */ + public static function makeInsertQuery(string $table, &$dataset):string + { + if (empty($dataset)) { + return "INSERT INTO {$table} () VALUES (); "; + } + + $set = []; + + $query = "INSERT INTO `{$table}` SET "; + + foreach ($dataset as $index => $value) { + if (strtoupper(trim($value)) === 'NOW()') { + $set[] = "`{$index}` = NOW()"; + unset($dataset[ $index ]); + continue; + } + + $set[] = "`{$index}` = :{$index}"; + } + + $query .= implode(', ', $set) . ' ;'; + + return $query; + } + + /** + * Build UPDATE query by dataset for given table + * + * @param $tablename + * @param $dataset + * @param $where_condition + * @return bool|string + */ + public static function makeUpdateQuery(string $table, &$dataset, $where_condition):string + { + $set = []; + + if (empty($dataset)) { + return false; + } + + $query = "UPDATE `{$table}` SET"; + + foreach ($dataset as $index => $value) { + if (strtoupper(trim($value)) === 'NOW()') { + $set[] = "`{$index}` = NOW()"; + unset($dataset[ $index ]); + continue; + } + + $set[] = "`{$index}` = :{$index}"; + } + + $query .= implode(', ', $set); + + if (is_array($where_condition)) { + $where_condition = key($where_condition) . ' = ' . current($where_condition); + } + + if ( is_string($where_condition ) && !strpos($where_condition, 'WHERE')) { + $where_condition = " WHERE {$where_condition}"; + } + + if (is_null($where_condition)) { + $where_condition = ''; + } + + $query .= " $where_condition ;"; + + return $query; + } + +} \ No newline at end of file