Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
fetus-hina committed Nov 21, 2024
2 parents 1266fa7 + de4638f commit e861640
Show file tree
Hide file tree
Showing 29 changed files with 1,004 additions and 1,047 deletions.
48 changes: 38 additions & 10 deletions commands/BlogFeedController.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* @copyright Copyright (C) 2015-2021 AIZAWA Hina
* @copyright Copyright (C) 2015-2024 AIZAWA Hina
* @license https://github.com/fetus-hina/stat.ink/blob/master/LICENSE MIT
* @author AIZAWA Hina <hina@fetus.jp>
*/
Expand All @@ -11,18 +11,24 @@
namespace app\commands;

use Exception;
use GuzzleHttp\Client as GuzzleHttpClient;
use Laminas\Feed\Reader\Http\ClientInterface as FeedReaderHttpClientInterface;
use Laminas\Feed\Reader\Http\Psr7ResponseDecorator;
use Laminas\Feed\Reader\Reader as FeedReader;
use Laminas\Validator\Uri as UriValidator;
use TypeError;
use Yii;
use app\components\helpers\TypeHelper;
use app\models\BlogEntry;
use jp3cki\uuid\NS as UuidNS;
use jp3cki\uuid\Uuid;
use yii\console\Controller;

use function count;
use function preg_match;
use function printf;
use function usort;

class BlogFeedController extends Controller
final class BlogFeedController extends Controller
{
public function actionCrawl()
{
Expand All @@ -37,6 +43,16 @@ public function actionCrawl()
private function fetchFeed()
{
echo "Fetching feed...\n";
FeedReader::setHttpClient(
new class () implements FeedReaderHttpClientInterface {
public function get($uri)
{
return new Psr7ResponseDecorator(
(new GuzzleHttpClient())->request('GET', $uri),
);
}
},
);
$feed = FeedReader::import('https://blog.fetus.jp/category/website/stat-ink/feed');
echo "done.\n";
$ret = [];
Expand All @@ -47,6 +63,8 @@ private function fetchFeed()
$ret[] = $entry;
}
usort($ret, fn ($a, $b) => $a->getDateCreated()->getTimestamp() <=> $b->getDateCreated()->getTimestamp());
printf("%d entries\n", count($ret));

return $ret;
}

Expand All @@ -56,14 +74,14 @@ private function processEntry($entry)
if (!$id) {
return;
}
$uuid = Uuid::v5(
(new UriValidator())->isValid($id)
? UuidNs::url()
: 'd0ec81fc-c8e6-11e5-a890-9ca3ba01e1f8',
$id,
)->__toString();
$uuid = TypeHelper::string(
Uuid::v5(
self::isValidUrl($id) ? UuidNs::url() : 'd0ec81fc-c8e6-11e5-a890-9ca3ba01e1f8',
$id,
),
);
$link = $entry->getLink();
if (!(new UriValidator())->isValid($link)) {
if (!self::isValidUrl($link)) {
return;
}

Expand All @@ -85,4 +103,14 @@ private function processEntry($entry)
echo "Registered new blog entry\n";
printf(" #%d, %s %s\n", $model->id, $model->url, $model->title);
}

private static function isValidUrl(mixed $url): bool
{
try {
$url = TypeHelper::url($url);
return (bool)preg_match('#\Ahttps?://#i', $url);
} catch (TypeError) {
return false;
}
}
}
17 changes: 16 additions & 1 deletion components/helpers/TypeHelper.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* @copyright Copyright (C) 2015-2023 AIZAWA Hina
* @copyright Copyright (C) 2015-2024 AIZAWA Hina
* @license https://github.com/fetus-hina/stat.ink/blob/master/LICENSE MIT
* @author AIZAWA Hina <hina@fetus.jp>
*/
Expand All @@ -21,8 +21,10 @@
use function is_scalar;
use function is_string;

use const FILTER_FLAG_PATH_REQUIRED;
use const FILTER_VALIDATE_FLOAT;
use const FILTER_VALIDATE_INT;
use const FILTER_VALIDATE_URL;

final class TypeHelper
{
Expand Down Expand Up @@ -82,6 +84,19 @@ public static function floatOrNull(mixed $value): ?float
return is_float($value) ? $value : null;
}

public static function url(mixed $value): string
{
$value = filter_var(
self::string($value),
FILTER_VALIDATE_URL,
FILTER_FLAG_PATH_REQUIRED,
);
return match (true) {
is_string($value) => $value,
default => throw new TypeError('The value is not a valid URL'),
};
}

public static function array(mixed $value): array
{
return match (true) {
Expand Down
10 changes: 4 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"ext-session": "*",
"ext-zlib": "*",
"cweagans/composer-patches": "^1.7.3",
"geoip2/geoip2": "^3.0.0",
"geoip2/geoip2": "^3.1.0",
"guzzlehttp/guzzle": "^7.9.2",
"jaybizzle/crawler-detect": "^1.2.121",
"jp3cki/mb_str_replace": "^4.0.2",
"jp3cki/uuid": "^4.0.1",
Expand All @@ -30,12 +31,9 @@
"jp3cki/yii2-twitter-widget": "^1.0",
"jp3cki/yii2-zxcvbn": "^1.0",
"laminas/laminas-feed": "^2.23.0",
"laminas/laminas-http": "^2.20",
"laminas/laminas-validator": "^2.64.1",
"lipis/flag-icons": "^7.2.3",
"lusitanian/oauth": "^0.8.11",
"markrogoyski/math-php": "^2.10.0",
"omnilight/yii2-scheduling": "^1.1.6",
"paragonie/constant_time_encoding": "^3.0.0",
"php-curl-class/php-curl-class": "^11.0.0",
"statink/yii2-anonymizer": "^1.0.4",
Expand All @@ -48,7 +46,7 @@
"statink/yii2-spl1-stages": "^1.0.1",
"statink/yii2-spl2-stages": "^1.0.2",
"statink/yii2-twitter-webintents": "^1.0.2",
"symfony/yaml": "^6.4.13 || ^7.0.0",
"symfony/yaml": "^7.1.6",
"twbs/bootstrap-icons": "^1.11.3",
"yiisoft/yii2": "^2.0.51",
"yiisoft/yii2-bootstrap": "^2.0.11",
Expand All @@ -66,7 +64,7 @@
"ergebnis/composer-normalize": "^2.44.0",
"jp3cki/coding-standard": "^1.0.0",
"proget-hq/phpstan-yii2": "^0.8.0",
"squizlabs/php_codesniffer": "^3.11.0",
"squizlabs/php_codesniffer": "^3.11.1",
"yiisoft/yii2-debug": "^2.1.25",
"yiisoft/yii2-gii": "^2.2.6"
},
Expand Down
Loading

0 comments on commit e861640

Please sign in to comment.