master
ブランチ master
LittleBeast / src / Std / Lib / Db / Convert / JsonTarget.php
9.9 KB、235行 生 Blame 履歴
1<?php
2/*************************************************************
3# 076 License
4
5Copyright (c) テクニカル諏訪子
6
7Permission is hereby granted to any person obtaining a copy of the software
8Little Beast (the "Software") to use, modify, merge, copy, publish, distribute,
9sublicense, and/or sell copies of the Software, subject to the following conditions:
10
11 1. **Origin Attribution**:
12 - You must not misrepresent the origin of the Software; you must not claim
13 you created the original Software.
14 - If the Software is used in a product, you must either:
15 a. Provide clear attribution in the product's documentation, user interface,
16 or other visible areas, **OR**
17 b. Pay the original developers a fee they specify in writing.
18 2. **Usage Restriction**:
19 - The Software, or any derivative works, dependencies, or libraries
20 incorporating it, must not be used for censorship or to suppress freedom of
21 speech, expression, or creativity. Prohibited uses include, but are not
22 limited to:
23 - Censorship of so-called "hate speech", visuals, non-mainstream opinions,
24 ideas, or objective reality.
25 - Tools or systems designed to restrict access to information or
26 artistic works.
27 3. **Notice Preservation**:
28 - This license and the above copyright notice must remain intact in all copies
29 of the source code.
30
31THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
32INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
33PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
34HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
35CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
36OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 */
38namespace Std\Lib\Db\Convert;
39
40use Std\Lib\Db\DbException;
41use Std\Lib\Db\Sqlite\Blob;
42
43/**
44 * LittleBeastのJSONのデータ(data/)に書く(Storesを参照)
45 *
46 * lb_のテーブルは元のファイルにし、他のテーブルはdata/db/{テーブル}.jsonにする。
47 * 書く先のディレクトリが空でない場合は、$overwriteが必要(同じ名前のファイルは置き換え、他のファイルはそのまま)。
48 */
49final class JsonTarget implements Target {
50 private array $warnings = [];
51 private array $buf = [];
52 private array $open = [];
53 private array $first = [];
54
55 /**
56 * @param string $dir データのディレクトリ
57 * @param bool $overwrite 空でないディレクトリに書く
58 */
59 public function __construct(private string $dir, private bool $overwrite = false) {
60 $this->dir = rtrim($dir, '/\\');
61 }
62
63 public function warnings(): array {
64 return array_values(array_unique($this->warnings));
65 }
66
67 public function begin(bool $drop): void {
68 if (is_dir($this->dir)) {
69 $empty = count(array_diff(scandir($this->dir) ?: [], ['.', '..'])) === 0;
70 if (!$empty && !$this->overwrite && !$drop) throw new DbException("ディレクトリが空ではありません:{$this->dir}", 0, 'HY000');
71 } else if (!@mkdir($this->dir, 0755, true) && !is_dir($this->dir)) {
72 throw new DbException("ディレクトリを作れません:{$this->dir}", 0, 'HY000');
73 }
74 }
75
76 private function path(string $rel): string {
77 if (!Stores::safePath($rel)) throw new DbException("不正なパス:{$rel}", 0, 'HY000');
78 $p = $this->dir.'/'.$rel;
79 $d = dirname($p);
80 if (!is_dir($d) && !@mkdir($d, 0755, true) && !is_dir($d)) throw new DbException("ディレクトリを作れません:{$d}", 0, 'HY000');
81 return $p;
82 }
83
84 private function put(string $rel, string $data): void {
85 $p = $this->path($rel);
86 $tmp = $p.'.'.bin2hex(random_bytes(4)).'.tmp';
87 if (file_put_contents($tmp, $data) === false || !rename($tmp, $p)) {
88 @unlink($tmp);
89 throw new DbException("ファイルに書けません:{$p}", 0, 'HY000');
90 }
91 }
92
93 public function createTable(array $t): void {
94 $l = strtolower($t['name']);
95 if (isset(Stores::tables()[$l])) {
96 $this->buf[$l] = [];
97 return;
98 }
99 // 普通のテーブル:定義を書いて、行を追加していく
100 $p = $this->path(Stores::tableFile($t['name']));
101 $fh = @fopen($p.'.tmp', 'wb');
102 if ($fh === false) throw new DbException("ファイルに書けません:{$p}", 0, 'HY000');
103 $head = ['table' => $t['name'], 'columns' => array_map(fn($c) => array_filter($c, fn($v) => $v !== null && $v !== false) + ['name' => $c['name'], 'type' => $c['type']], $t['columns']),
104 'pk' => $t['pk'], 'indexes' => $t['indexes']];
105 foreach ($head['columns'] as $i => $c) {
106 if (isset($c['default']['value'])) $head['columns'][$i]['default']['value'] = self::jsonValue($c['default']['value']);
107 }
108 $json = json_encode($head, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
109 fwrite($fh, substr($json, 0, -1).",\"rows\":[\n");
110 $this->open[$l] = [$fh, $p];
111 $this->first[$l] = true;
112 }
113
114 /** JSONに入れる値(BLOBとNaN・無限) */
115 public static function jsonValue(mixed $v): mixed {
116 if ($v instanceof Blob) return ['$blob' => base64_encode($v->data)];
117 if (is_float($v) && (is_nan($v) || is_infinite($v))) return ['$float' => is_nan($v) ? 'NaN' : ($v > 0 ? 'Infinity' : '-Infinity')];
118 return $v;
119 }
120
121 public function insertRows(array $t, array $rows): void {
122 $l = strtolower($t['name']);
123 if (isset($this->buf[$l])) {
124 // 列の名前で(書く先で列の順番が違っても良い様に)
125 foreach ($rows as $r) {
126 $row = [];
127 foreach ($t['columns'] as $i => $c) $row[strtolower($c['name'])] = $r[$i];
128 $this->buf[$l][] = $row;
129 }
130 return;
131 }
132 [$fh] = $this->open[$l];
133 $out = '';
134 foreach ($rows as $r) {
135 $line = json_encode(array_map([self::class, 'jsonValue'], $r), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRESERVE_ZERO_FRACTION | JSON_INVALID_UTF8_SUBSTITUTE);
136 $out .= ($this->first[$l] ? '' : ",\n").$line;
137 $this->first[$l] = false;
138 }
139 fwrite($fh, $out);
140 }
141
142 public function finishTable(array $t): void {
143 $l = strtolower($t['name']);
144 if (!isset($this->open[$l])) return;
145 [$fh, $p] = $this->open[$l];
146 fwrite($fh, "\n]}\n");
147 fclose($fh);
148 rename($p.'.tmp', $p);
149 unset($this->open[$l]);
150 }
151
152 public function end(): void {
153 $b = $this->buf;
154 $col = fn(array $r, string $k) => $r[$k] ?? null;
155 $decode = function(mixed $v, string $what) {
156 if (is_array($v) || $v === null) return $v;
157 $d = json_decode((string)$v, true);
158 if (json_last_error() !== JSON_ERROR_NONE) throw new DbException("不正なJSON:{$what}", 0, 'HY000');
159 return $d;
160 };
161 $seqSort = fn(array &$rows) => usort($rows, fn($a, $b) => (int)$a['seq'] <=> (int)$b['seq']);
162
163 foreach ($b['lb_users'] ?? [] as $r) {
164 $name = (string)$col($r, 'username');
165 if (!preg_match('/^[A-Za-z0-9]+$/', $name) || !is_numeric($col($r, 'id'))) throw new DbException("不正なユーザー:{$name}", 0, 'HY000');
166 $decode($r['data'], "lb_users.{$name}");
167 $this->put('user/'.(int)$r['id'].'.'.$name.'.json', (string)$r['data']);
168 }
169
170 $threads = [];
171 foreach ($b['lb_comment_threads'] ?? [] as $r) $threads[(string)$r['thread']] = $decode($r['data'], 'lb_comment_threads') ?: [];
172 $comments = [];
173 foreach ($b['lb_comments'] ?? [] as $r) $comments[(string)$r['thread']][] = $r;
174 foreach ($comments as $thread => $list) $threads[$thread] ??= [];
175 foreach ($threads as $thread => $doc) {
176 if (!Stores::safeThread($thread)) throw new DbException("不正なスレッド:{$thread}", 0, 'HY000');
177 $list = $comments[$thread] ?? [];
178 $seqSort($list);
179 $doc = is_array($doc) ? $doc : [];
180 $doc['comments'] = [];
181 foreach ($list as $r) $doc['comments'][(string)$r['id']] = $decode($r['data'], "lb_comments.{$thread}");
182 $this->put("comments/{$thread}.json", (string)json_encode($doc, Stores::JSON_FLAGS));
183 }
184
185 if (!empty($b['lb_comment_ap'])) {
186 $map = [];
187 foreach ($b['lb_comment_ap'] as $r) $map[(string)$r['ap_id']] = $decode($r['data'], 'lb_comment_ap');
188 $this->put('comments/ap.json', (string)json_encode($map, Stores::JSON_FLAGS));
189 }
190
191 $notes = [];
192 foreach ($b['lb_notifications'] ?? [] as $r) $notes[(string)$r['username']][] = $r;
193 foreach ($notes as $user => $list) {
194 if (!Stores::safeName($user)) throw new DbException("不正なユーザー:{$user}", 0, 'HY000');
195 $seqSort($list);
196 $this->put("notifications/{$user}.json", (string)json_encode(array_map(fn($r) => $decode($r['data'], 'lb_notifications'), $list), Stores::JSON_FLAGS));
197 }
198
199 if (isset($b['lb_cron'])) {
200 $list = $b['lb_cron'];
201 $seqSort($list);
202 $this->put('cron/cron.json', (string)json_encode(array_map(fn($r) => $decode($r['data'], 'lb_cron'), $list), Stores::JSON_FLAGS));
203 }
204
205 foreach (['lb_followers' => 'followers.txt', 'lb_following' => 'following.txt'] as $t => $file) {
206 if (!isset($b[$t])) continue;
207 $list = $b[$t];
208 $seqSort($list);
209 $lines = array_values(array_filter(array_map(fn($r) => trim((string)$r['actor']), $list), fn($l) => $l !== ''));
210 $this->put($file, empty($lines) ? '' : implode("\n", $lines)."\n");
211 }
212
213 foreach ($b['lb_ap_notes'] ?? [] as $r) {
214 $id = (string)$r['id'];
215 if (!Stores::safeName($id)) throw new DbException("不正なID:{$id}", 0, 'HY000');
216 $decode($r['data'], "lb_ap_notes.{$id}");
217 $this->put("ap/notes/{$id}.json", (string)$r['data']);
218 }
219
220 foreach ($b['lb_files'] ?? [] as $r) {
221 $c = $r['content'];
222 $this->put((string)$r['path'], $c instanceof Blob ? $c->data : (string)$c);
223 }
224 $this->buf = [];
225 }
226
227 public function abort(): void {
228 foreach ($this->open as [$fh, $p]) {
229 @fclose($fh);
230 @unlink($p.'.tmp');
231 }
232 $this->open = [];
233 $this->buf = [];
234 }
235}