Skip to content

Commit

Permalink
1.0.2
Browse files Browse the repository at this point in the history
* [R] Release
* [+] added helper
* [*] cut-off footer fix
* [-] removed "karelwintersky/arris.helpers" DEPS
* [*] Curl downloader will be used only if curl/curl installed
  • Loading branch information
KarelWintersky committed Aug 3, 2022
1 parent 6fdf743 commit 4b5df4f
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 18 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 12 additions & 0 deletions interfaces/LongreadsHelperInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace AJUR\FSNews;

interface LongreadsHelperInterface
{
public static function rmdir($directory): bool;
public static function makeReplaceQuery(string $table, array &$dataset, string $where = '');
public static function makeInsertQuery(string $table, &$dataset):string;
public static function makeUpdateQuery(string $table, &$dataset, $where_condition):string;
}

4 changes: 3 additions & 1 deletion interfaces/LongreadsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use PDO;
use Psr\Log\LoggerInterface;
use RuntimeException as RuntimeExceptionAlias;

interface LongreadsInterface
{
Expand Down Expand Up @@ -75,8 +76,9 @@ public function getStoredByID($id = null);
* @param $id
* @param null $folder
* @param string $import_mode
*
* @return mixed
* @throws RuntimeExceptionAlias
*
*/
public function import($id, $folder = null, string $import_mode = 'update');

Expand Down
38 changes: 23 additions & 15 deletions sources/Longreads.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@

namespace AJUR\FSNews;

use Curl\Curl;
use PDOException;
use Psr\Log\NullLogger;
use RuntimeException;
use PDO;
use Psr\Log\LoggerInterface;
use Arris\Helpers\DB;
use Arris\Helpers\FS;
use stdClass;

class Longreads implements LongreadsInterface
Expand Down Expand Up @@ -94,6 +91,11 @@ class Longreads implements LongreadsInterface
*/
private $option_download_client;

/**
* @var bool
*/
private $debug_write_raw_html;

public function __construct(PDO $pdo, array $options = [], $logger = null)
{
$this->api_request_types = [
Expand All @@ -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'])) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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', "<!--/allrecords-->\n", $html);
$html = substr($html, 0, strpos($html, '<!--/allrecords-->'));
$html .= '<!--/allrecords-->';
}

if ('' !== $this->path_to_footer_template) {
Expand Down Expand Up @@ -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 {

Expand All @@ -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) ]);
Expand All @@ -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() ]);

Expand Down Expand Up @@ -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) ]);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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-
131 changes: 131 additions & 0 deletions sources/LongreadsHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

namespace AJUR\FSNews;

class LongreadsHelper implements LongreadsHelperInterface
{
public static function rmdir($directory): bool
{
if (!is_dir( $directory )) {
return false;
}

$files = array_diff( scandir( $directory ), [ '.', '..' ] );

foreach ($files as $file) {
$target = "{$directory}/{$file}";
(is_dir( $target ))
? self::rmdir( $target )
: unlink( $target );
}
return rmdir( $directory );
}

public static function makeReplaceQuery(string $table, array &$dataset, string $where = '')
{
$fields = [];

if (empty($dataset))
return false;

$query = "REPLACE `{$table}` SET ";

foreach ($dataset as $index => $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;
}

}

0 comments on commit 4b5df4f

Please sign in to comment.