summaryrefslogtreecommitdiff
path: root/lib/posts.php
blob: bf8d1493761b6a918a258936ade9c4e43b6c3b7d (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
<?php

class posts {

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

    public static function getPostsCountByTagId(int $tag_id, bool $include_hidden = false): int {
        $db = getDb();
        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));
    }

    public static function getPosts(int $offset = 0, int $count = -1, bool $include_hidden = false): array {
        $db = getDb();
        $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[]
     */
    public static function getPostsByTagId(int $tag_id, bool $include_hidden = false): array {
        $db = getDb();
        $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));
    }

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

        $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 = posts::get($id);
        $post->updateImagePreviews();

        return $id;
    }

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

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

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

    public static function getTagIds(array $tags): array {
        $found_tags = [];
        $map = [];

        $db = getDb();
        $q = $db->query("SELECT id, tag FROM tags
            WHERE tag IN ('".implode("','", array_map(function($tag) use ($db) { return $db->escape($tag); }, $tags))."')");
        while ($row = $db->fetch($q)) {
            $found_tags[] = $row['tag'];
            $map[$row['tag']] = (int)$row['id'];
        }

        $notfound_tags = array_diff($tags, $found_tags);
        if (!empty($notfound_tags)) {
            foreach ($notfound_tags as $tag) {
                $db->insert('tags', ['tag' => $tag]);
                $map[$tag] = $db->insertId();
            }
        }

        return $map;
    }

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

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

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

        $db = getDb();
        $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;
    }

    public static function getAllTags(bool $include_hidden = false): array {
        $db = getDb();
        $field = $include_hidden ? 'posts_count' : 'visible_posts_count';
        $q = $db->query("SELECT * FROM tags WHERE $field > 0 ORDER BY $field DESC, tag");
        return array_map('Tag::create_instance', $db->fetchAll($q));
    }

    public static function getTag(string $tag): ?Tag {
        $db = getDb();
        $q = $db->query("SELECT * FROM tags WHERE tag=?", $tag);
        return $db->numRows($q) ? new Tag($db->fetch($q)) : null;
    }

    /**
     * @param int $tag_id
     */
    public static function recountPostsWithTag($tag_id) {
        $db = getDb();
        $count = $db->result($db->query("SELECT COUNT(*) FROM posts_tags WHERE tag_id=?", $tag_id));
        $vis_count = $db->result($db->query("SELECT COUNT(*) FROM posts_tags
            LEFT JOIN posts ON posts.id=posts_tags.post_id
            WHERE posts_tags.tag_id=? AND posts.visible=1", $tag_id));
        $db->query("UPDATE tags SET posts_count=?, visible_posts_count=? WHERE id=?",
            $count, $vis_count, $tag_id);
    }

    public static function splitStringToTags(string $tags): array {
        $tags = trim($tags);
        if ($tags == '') {
            return [];
        }

        $tags = preg_split('/,\s+/', $tags);
        $tags = array_filter($tags, function($tag) { return trim($tag) != ''; });
        $tags = array_map('trim', $tags);
        $tags = array_map('mb_strtolower', $tags);

        return $tags;
    }

}