CSVライブラリも削除
諏訪子 <suwako@076.moe>、2025-12-30 19:09
コミット cb0334367743a5b513b4e21cef14396faa31b5ac
・親 580ab9a
・ファイルを見る
5個のファイル、+0 -228
| @@ -21,21 +21,11 @@ | ||
| 21 | 21 | 'pubkey' => ROOT.'/public/static/pub.pem', |
| 22 | 22 | 'privkey' => ROOT.'/data/priv.pem', |
| 23 | 23 | ]); |
| 24 | -define('DBINFO', [ | |
| 25 | - 'host' => 'localhost', | |
| 26 | - 'username' => 'littlebeast', | |
| 27 | - 'password' => '', | |
| 28 | - 'dbname' => 'littlebeast', | |
| 29 | - 'port' => 3306, | |
| 30 | - 'debug' => false, | |
| 31 | -]); | |
| 32 | 24 | |
| 33 | 25 | define('MAILER_ENABLED', false); |
| 34 | 26 | define('LOGGING_ENABLED', true); |
| 35 | 27 | define('ATOM_ENABLED', true); |
| 36 | -define('RSS_ENABLED', false); | |
| 37 | 28 | define('ACTIVITYPUB_ENABLED', false); |
| 38 | -define('CSV_ENABLED', false); | |
| 39 | 29 | define('CURL_ENABLED', true); |
| 40 | 30 | define('AUTH_ENABLED', false); |
| 41 | 31 | define('AUTH_REGISTER_ENABLED', false); |
変更
route.php
| @@ -50,8 +50,6 @@ | ||
| 50 | 50 | $routes[] = Route::add('GET', 'staffonly', Page::class.'@staffonly'); |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | -/* if (RSS_ENABLED) {} */ | |
| 54 | - | |
| 55 | 53 | if (ATOM_ENABLED) { |
| 56 | 54 | $routes[] = Route::add('GET', 'blog.atom', Atom::class.'@feed'); |
| 57 | 55 | } |
削除
src/Site/Lib/Csv.php
| @@ -1,85 +0,0 @@ | ||
| 1 | -<?php | |
| 2 | -namespace Site\Lib; | |
| 3 | - | |
| 4 | -enum Delimiter: int { | |
| 5 | - case COMMA = 1; | |
| 6 | - case SEMICOLON = 2; | |
| 7 | - case TAB = 3; | |
| 8 | - case PIPE = 4; | |
| 9 | - | |
| 10 | - public static function default(): self { | |
| 11 | - return self::COMMA; | |
| 12 | - } | |
| 13 | - | |
| 14 | - public function getChar(): string { | |
| 15 | - return match ($this) { | |
| 16 | - self::COMMA => ",", | |
| 17 | - self::SEMICOLON => ";", | |
| 18 | - self::TAB => "\t", | |
| 19 | - self::PIPE => "|", | |
| 20 | - }; | |
| 21 | - } | |
| 22 | -} | |
| 23 | - | |
| 24 | -/** | |
| 25 | - * CSVパーシングクラス | |
| 26 | - */ | |
| 27 | -class Csv { | |
| 28 | - // リクエスト関連のプロパティ | |
| 29 | - private bool $isHeader = false; | |
| 30 | - private int $length = 8192; | |
| 31 | - private Delimiter $delimiter; | |
| 32 | - private string $filename; | |
| 33 | - private $fp; | |
| 34 | - | |
| 35 | - public function __construct(string $filename, int $length = 8192) { | |
| 36 | - $this->length = $length; | |
| 37 | - $this->filename = $filename; | |
| 38 | - $this->delimiter = Delimiter::default(); | |
| 39 | - | |
| 40 | - $this->fp = fopen($this->filename, 'r'); | |
| 41 | - if ($this->fp === false) { | |
| 42 | - $msg = "ファイルを開けられません。"; | |
| 43 | - logger(\LogType::Csv, $msg); | |
| 44 | - throw new \Exception($msg); | |
| 45 | - } | |
| 46 | - } | |
| 47 | - | |
| 48 | - public function __destruct() { | |
| 49 | - if ($this->fp !== false) { | |
| 50 | - fclose($this->fp); | |
| 51 | - } | |
| 52 | - } | |
| 53 | - | |
| 54 | - public function parse(?Delimiter $delimiter = null, bool $isHeader = false): array { | |
| 55 | - $res = []; | |
| 56 | - $this->isHeader = $isHeader; | |
| 57 | - $this->delimiter = $delimiter ?? $this->delimiter; | |
| 58 | - | |
| 59 | - $delimChar = $this->delimiter->getChar(); | |
| 60 | - | |
| 61 | - rewind($this->fp); | |
| 62 | - | |
| 63 | - if ($this->isHeader) { | |
| 64 | - $res = ['header' => [], 'body' => []]; | |
| 65 | - $head = fgets($this->fp, $this->length); | |
| 66 | - if ($head !== false) { | |
| 67 | - $res['header'] = str_getcsv($head, $delimChar); | |
| 68 | - } | |
| 69 | - } | |
| 70 | - | |
| 71 | - while (($buffer = fgets($this->fp, $this->length)) !== false) { | |
| 72 | - $row = str_getcsv($buffer, $delimChar); | |
| 73 | - if ($this->isHeader) $res['body'][] = $row; | |
| 74 | - else $res[] = $row; | |
| 75 | - } | |
| 76 | - | |
| 77 | - if (!feof($this->fp)) { | |
| 78 | - $msg = "エラー:fgets()の失敗"; | |
| 79 | - logger(\LogType::Csv, $msg); | |
| 80 | - throw new \Exception($msg); | |
| 81 | - } | |
| 82 | - | |
| 83 | - return $res; | |
| 84 | - } | |
| 85 | -} |
削除
src/Site/Test/LibCsv.php
| @@ -1,126 +0,0 @@ | ||
| 1 | -<?php | |
| 2 | -namespace Site\Test; | |
| 3 | - | |
| 4 | -require_once __DIR__.'/../../../autoload.php'; | |
| 5 | - | |
| 6 | -use Site\Lib\Tester; | |
| 7 | -use Site\Lib\Csv; | |
| 8 | -use Site\Lib\Delimiter; | |
| 9 | - | |
| 10 | -if (CSV_ENABLED) { | |
| 11 | - $test = new Tester([ | |
| 12 | - 'colorOutput' => true, | |
| 13 | - 'verboseOutput' => true | |
| 14 | - ]); | |
| 15 | - | |
| 16 | - $test->describe('Csvの基本的なパーシング', function($test): void { | |
| 17 | - $test->it('簡単なCSVファイルをパーシングするはず', function($test): void { | |
| 18 | - $str = "岩田聡,プロジューサー\n宮本茂,デザイナー\nJeffrey Epstein,幼児性愛者"; | |
| 19 | - | |
| 20 | - $tmpFile = tempnam(sys_get_temp_dir(), 'csv_test'); | |
| 21 | - file_put_contents($tmpFile, $str); | |
| 22 | - | |
| 23 | - $csv = new Csv($tmpFile); | |
| 24 | - $res = $csv->parse(); | |
| 25 | - | |
| 26 | - unlink($tmpFile); | |
| 27 | - | |
| 28 | - $expect = [ | |
| 29 | - ["岩田聡", "プロジューサー"], | |
| 30 | - ["宮本茂", "デザイナー"], | |
| 31 | - ["Jeffrey Epstein", "幼児性愛者"] | |
| 32 | - ]; | |
| 33 | - | |
| 34 | - $test->assertNotNull($res); | |
| 35 | - $test->assertEquals($res, $expect); | |
| 36 | - }); | |
| 37 | - | |
| 38 | - $test->it('異なるデリミタでCSVをパーシングするはず', function ($test): void { | |
| 39 | - // セミコロン | |
| 40 | - $semiStr = "岩田聡;プロジューサー\n宮本茂;デザイナー"; | |
| 41 | - $tmpFile = tempnam(sys_get_temp_dir(), 'csv_semi_test'); | |
| 42 | - file_put_contents($tmpFile, $semiStr); | |
| 43 | - | |
| 44 | - $csv = new Csv($tmpFile); | |
| 45 | - $res = $csv->parse(Delimiter::SEMICOLON); | |
| 46 | - | |
| 47 | - unlink($tmpFile); | |
| 48 | - | |
| 49 | - $expect = [ | |
| 50 | - ["岩田聡", "プロジューサー"], | |
| 51 | - ["宮本茂", "デザイナー"] | |
| 52 | - ]; | |
| 53 | - | |
| 54 | - $test->assertEquals($res, $expect, "セミコロンデリミタでパーシングに失敗"); | |
| 55 | - | |
| 56 | - // タブ | |
| 57 | - $tabStr = "岩田聡\tプロジューサー\n宮本茂\tデザイナー"; | |
| 58 | - $tmpFile = tempnam(sys_get_temp_dir(), 'csv_tab_test'); | |
| 59 | - file_put_contents($tmpFile, $tabStr); | |
| 60 | - | |
| 61 | - $csv = new Csv($tmpFile); | |
| 62 | - $res = $csv->parse(Delimiter::TAB); | |
| 63 | - | |
| 64 | - unlink($tmpFile); | |
| 65 | - | |
| 66 | - $test->assertEquals($res, $expect, "タブデリミタでパーシングに失敗"); | |
| 67 | - | |
| 68 | - // パイプ | |
| 69 | - $pipeStr = "岩田聡|プロジューサー\n宮本茂|デザイナー"; | |
| 70 | - $tmpFile = tempnam(sys_get_temp_dir(), 'csv_pipe_test'); | |
| 71 | - file_put_contents($tmpFile, $pipeStr); | |
| 72 | - | |
| 73 | - $csv = new Csv($tmpFile); | |
| 74 | - $res = $csv->parse(Delimiter::PIPE); | |
| 75 | - | |
| 76 | - unlink($tmpFile); | |
| 77 | - | |
| 78 | - $test->assertEquals($res, $expect, "パイプデリミタでパーシングに失敗"); | |
| 79 | - }); | |
| 80 | - | |
| 81 | - $test->it('ヘッダー付きCSVをパーシングするはず', function ($test) { | |
| 82 | - $str = "name,job title\n岩田聡,プロジューサー\n宮本茂,デザイナー"; | |
| 83 | - | |
| 84 | - $tmpFile = tempnam(sys_get_temp_dir(), 'csv_header_test'); | |
| 85 | - file_put_contents($tmpFile, $str); | |
| 86 | - | |
| 87 | - $csv = new Csv($tmpFile); | |
| 88 | - $res = $csv->parse(Delimiter::COMMA, true); // isHeader = true | |
| 89 | - | |
| 90 | - unlink($tmpFile); | |
| 91 | - | |
| 92 | - $expect = [ | |
| 93 | - 'header' => ["name", "job title"], | |
| 94 | - 'body' => [ | |
| 95 | - ["岩田聡", "プロジューサー"], | |
| 96 | - ["宮本茂", "デザイナー"], | |
| 97 | - ], | |
| 98 | - ]; | |
| 99 | - | |
| 100 | - $test->assertNotNull($res); | |
| 101 | - $test->assertEquals($res, $expect, "ヘッダーパーシングに失敗"); | |
| 102 | - }); | |
| 103 | - | |
| 104 | - $test->it('セル内にカンマを含むCSVをパーシングするはず', function ($test): void { | |
| 105 | - $str = "\"守矢, 諏訪子\",エンジニア\n\"青, 猫ちゃん\",サーバー管理者"; | |
| 106 | - | |
| 107 | - $tmpFile = tempnam(sys_get_temp_dir(), 'csv_quoted_comma_test'); | |
| 108 | - file_put_contents($tmpFile, $str); | |
| 109 | - | |
| 110 | - $csv = new Csv($tmpFile); | |
| 111 | - $res = $csv->parse(\Site\Lib\delimiter::COMMA); | |
| 112 | - | |
| 113 | - unlink($tmpFile); | |
| 114 | - | |
| 115 | - $expect = [ | |
| 116 | - ["守矢, 諏訪子", "エンジニア"], | |
| 117 | - ["青, 猫ちゃん", "サーバー管理者"] | |
| 118 | - ]; | |
| 119 | - | |
| 120 | - $test->assertNotNull($res); | |
| 121 | - $test->assertEquals($res, $expect, "セル内にカンマを含むパーシングに失敗"); | |
| 122 | - }); | |
| 123 | - }); | |
| 124 | - | |
| 125 | - $test->printSummary(); | |
| 126 | -} |
変更
util.php
| @@ -4,7 +4,6 @@ | ||
| 4 | 4 | enum LogType { |
| 5 | 5 | case ActivityPub; |
| 6 | 6 | case Auth; |
| 7 | - case Csv; | |
| 8 | 7 | case Mailer; |
| 9 | 8 | } |
| 10 | 9 | |
| @@ -218,10 +217,6 @@ | ||
| 218 | 217 | break; |
| 219 | 218 | case LogType::Auth: |
| 220 | 219 | $logfile .= 'auth_log.text'; |
| 221 | - $success = true; | |
| 222 | - break; | |
| 223 | - case LogType::Csv: | |
| 224 | - $logfile .= 'csv_log.text'; | |
| 225 | 220 | $success = true; |
| 226 | 221 | break; |
| 227 | 222 | case LogType::Mailer: |