aboutsummaryrefslogtreecommitdiff
path: root/lib/posts.php
blob: 8ffb92b4a76c08cc426e1d0d6a17ffe91cefebb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php

class Post extends model {

    const DB_TABLE = 'posts';

    public int $id;
    public string $title;
    public string $md;
    public string $html;
    public string $tocHtml;
    public string $text;
    public int $ts;
    public int $updateTs;
    public bool $visible;
    public bool $toc;
    public string $shortName;

    function edit(array $fields) {
        $cur_ts = time();
        if (!$this->visible && $fields['visible'])
            $fields['ts'] = $cur_ts;

        $fields['update_ts'] = $cur_ts;

        if ($fields['md'] != $this->md) {
            $fields['html'] = markup::markdownToHtml($fields['md']);
            $fields['text'] = markup::htmlToText($fields['html']);
        }

        if ((isset($fields['toc']) && $fields['toc']) || $this->toc) {
            $fields['toc_html'] = markup::toc($fields['md']);
        }

        parent::edit($fields);
        $this->updateImagePreviews();
    }

    function updateHtml() {
        $html = markup::markdownToHtml($this->md);
        $this->html = $html;

        DB()->query("UPDATE posts SET html=? WHERE id=?", $html, $this->id);
    }

    function updateText() {
        $html = markup::markdownToHtml($this->md);
        $text = markup::htmlToText($html);
        $this->text = $text;

        DB()->query("UPDATE posts SET text=? WHERE id=?", $text, $this->id);
    }

    function getDescriptionPreview(int $len): string {
        if (mb_strlen($this->text) >= $len)
            return mb_substr($this->text, 0, $len-3).'...';
        return $this->text;
    }

    function getFirstImage(): ?Upload {
        if (!preg_match('/\{image:([\w]{8})/', $this->md, $match))
            return null;
        return uploads::getUploadByRandomId($match[1]);
    }

    function getUrl(): string {
        return $this->shortName != '' ? "/{$this->shortName}/" : "/{$this->id}/";
    }

    function getDate(): string {
        return date('j M', $this->ts);
    }

    function getYear(): int {
        return (int)date('Y', $this->ts);
    }

    function getFullDate(): string {
        return date('j F Y', $this->ts);
    }

    function getUpdateDate(): string {
        return date('j M', $this->updateTs);
    }

    function getFullUpdateDate(): string {
        return date('j F Y', $this->updateTs);
    }

    function getHtml(bool $is_retina, string $theme): string {
        $html = $this->html;
        $html = markup::htmlImagesFix($html, $is_retina, $theme);
        return $html;
    }

    function getToc(): ?string {
        return $this->toc ? $this->tocHtml : null;
    }

    function isUpdated(): bool {
        return $this->updateTs && $this->updateTs != $this->ts;
    }

    /**
     * @return Tag[]
     */
    function getTags(): array {
        $db = DB();
        $q = $db->query("SELECT tags.* FROM posts_tags
            LEFT JOIN tags ON tags.id=posts_tags.tag_id
            WHERE posts_tags.post_id=?
            ORDER BY posts_tags.tag_id", $this->id);
        return array_map('Tag::create_instance', $db->fetchAll($q));
    }

    /**
     * @return int[]
     */
    function getTagIds(): array {
        $ids = [];
        $db = DB();
        $q = $db->query("SELECT tag_id FROM posts_tags WHERE post_id=? ORDER BY tag_id", $this->id);
        while ($row = $db->fetch($q)) {
            $ids[] = (int)$row['tag_id'];
        }
        return $ids;
    }

    function setTagIds(array $new_tag_ids) {
        $cur_tag_ids = $this->getTagIds();
        $add_tag_ids = array_diff($new_tag_ids, $cur_tag_ids);
        $rm_tag_ids = array_diff($cur_tag_ids, $new_tag_ids);

        $db = DB();
        if (!empty($add_tag_ids)) {
            $rows = [];
            foreach ($add_tag_ids as $id)
                $rows[] = ['post_id' => $this->id, 'tag_id' => $id];
            $db->multipleInsert('posts_tags', $rows);
        }

        if (!empty($rm_tag_ids))
            $db->query("DELETE FROM posts_tags WHERE post_id=? AND tag_id IN(".implode(',', $rm_tag_ids).")", $this->id);

        $upd_tag_ids = array_merge($new_tag_ids, $rm_tag_ids);
        $upd_tag_ids = array_unique($upd_tag_ids);
        foreach ($upd_tag_ids as $id)
            tags::recountTagPosts($id);
    }

    /**
     * @param bool $update Whether to overwrite preview if already exists
     * @return int
     * @throws Exception
     */
    function updateImagePreviews(bool $update = false): int {
        $images = [];
        if (!preg_match_all('/\{image:([\w]{8}),(.*?)}/', $this->md, $matches))
            return 0;

        for ($i = 0; $i < count($matches[0]); $i++) {
            $id = $matches[1][$i];
            $w = $h = null;
            $opts = explode(',', $matches[2][$i]);
            foreach ($opts as $opt) {
                if (str_contains($opt, '=')) {
                    list($k, $v) = explode('=', $opt);
                    if ($k == 'w')
                        $w = (int)$v;
                    else if ($k == 'h')
                        $h = (int)$v;
                }
            }
            $images[$id][] = [$w, $h];
        }

        if (empty($images))
            return 0;

        $images_affected = 0;
        $uploads = uploads::getUploadsByRandomId(array_keys($images), true);
        foreach ($uploads as $u) {
            foreach ($images[$u->randomId] as $s) {
                list($w, $h) = $s;
                list($w, $h) = $u->getImagePreviewSize($w, $h);
                if ($u->createImagePreview($w, $h, $update, $u->imageMayHaveAlphaChannel()))
                    $images_affected++;
            }
        }

        return $images_affected;
    }

}

class posts {

    static function getCount(bool $include_hidden = false): int {
        $db = DB();
        $sql = "SELECT COUNT(*) FROM posts";
        if (!$include_hidden) {
            $sql .= " WHERE visible=1";
        }
        return (int)$db->result($db->query($sql));
    }

    static function getCountByTagId(int $tag_id, bool $include_hidden = false): int {
        $db = DB();
        if ($include_hidden) {
            $sql = "SELECT COUNT(*) FROM posts_tags WHERE tag_id=?";
        } else {
            $sql = "SELECT COUNT(*) FROM posts_tags
            LEFT JOIN posts ON posts.id=posts_tags.post_id
            WHERE posts_tags.tag_id=? AND posts.visible=1";
        }
        return (int)$db->result($db->query($sql, $tag_id));
    }

    /**
     * @return Post[]
     */
    static function getList(int $offset = 0, int $count = -1, bool $include_hidden = false): array {
        $db = DB();
        $sql = "SELECT * FROM posts";
        if (!$include_hidden)
            $sql .= " WHERE visible=1";
        $sql .= " ORDER BY ts DESC";
        if ($offset != 0 && $count != -1)
            $sql .=  "LIMIT $offset, $count";
        $q = $db->query($sql);
        return array_map('Post::create_instance', $db->fetchAll($q));
    }

    /**
     * @return Post[]
     */
    static function getPostsByTagId(int $tag_id, bool $include_hidden = false): array {
        $db = DB();
        $sql = "SELECT posts.* FROM posts_tags
        LEFT JOIN posts ON posts.id=posts_tags.post_id
        WHERE posts_tags.tag_id=?";
        if (!$include_hidden)
            $sql .= " AND posts.visible=1";
        $sql .= " ORDER BY posts.ts DESC";
        $q = $db->query($sql, $tag_id);
        return array_map('Post::create_instance', $db->fetchAll($q));
    }

    static function add(array $data = []): int|bool {
        $db = DB();

        $html = \markup::markdownToHtml($data['md']);
        $text = \markup::htmlToText($html);

        $data += [
            'ts' => time(),
            'html' => $html,
            'text' => $text,
        ];

        if (!$db->insert('posts', $data))
            return false;

        $id = $db->insertId();

        $post = self::get($id);
        $post->updateImagePreviews();

        return $id;
    }

    static function delete(Post $post): void {
        $tags = $post->getTags();

        $db = DB();
        $db->query("DELETE FROM posts WHERE id=?", $post->id);
        $db->query("DELETE FROM posts_tags WHERE post_id=?", $post->id);

        foreach ($tags as $tag)
            tags::recountTagPosts($tag->id);
    }

    static function get(int $id): ?Post {
        $db = DB();
        $q = $db->query("SELECT * FROM posts WHERE id=?", $id);
        return $db->numRows($q) ? new Post($db->fetch($q)) : null;
    }

    static function getByName(string $short_name): ?Post {
        $db = DB();
        $q = $db->query("SELECT * FROM posts WHERE short_name=?", $short_name);
        return $db->numRows($q) ? new Post($db->fetch($q)) : null;
    }

    static function getPostsById(array $ids, bool $flat = false): array {
        if (empty($ids)) {
            return [];
        }

        $db = DB();
        $posts = array_fill_keys($ids, null);

        $q = $db->query("SELECT * FROM posts WHERE id IN(".implode(',', $ids).")");

        while ($row = $db->fetch($q)) {
            $posts[(int)$row['id']] = new Post($row);
        }

        if ($flat) {
            $list = [];
            foreach ($ids as $id) {
                $list[] = $posts[$id];
            }
            unset($posts);
            return $list;
        }

        return $posts;
    }

}