]> git.bts.cx Git - cx.git/blob - cx/lib/posts.php
f056c8834797ada3b96a028e07c61a5f6c283b80
[cx.git] / cx / lib / posts.php
1 <?php
2
3 cx_require('lib', 'db.php');
4 cx_require('lib', 'setup.php');
5 cx_require('lib', 'markdown.php');
6
7 class PostMetadata {
8         public $hero_image;
9         public $hero_image_alt;
10
11         public function __construct($dict) {
12                 $this->hero_image = isset($dict['post_hero_image']) ? $dict['post_hero_image'] : null;
13                 $this->hero_image_alt = isset($dict['post_hero_image_alt']) ? $dict['post_hero_image_alt'] : null;
14         }
15 }
16
17 class Post {
18         public $id;
19         public $title;
20         public $slug;
21         public $date;
22         public $is_page;
23         public $is_draft;
24         public $data;
25         public $html_content;
26         public $html_excerpt;
27
28         public function __construct($dict) {
29                 $this->id = $dict['post_id']; // FIXME, hide when not used?
30                 $this->title = $dict['post_title'];
31                 $this->slug = $dict['post_slug'];
32                 $this->date = $dict['post_date'];
33                 $this->is_page = $dict['post_is_page'];
34                 $this->is_draft = $dict['post_is_draft'];
35                 $this->data = $dict['post_data'];
36                 $this->html_content = cx_markdown_generate_html($this->data);
37                 $this->html_excerpt = null;
38
39                 // Read more...
40                 $segments = explode('---', $this->data, 2);
41                 if (count($segments) > 1) {
42                         $this->html_excerpt = cx_markdown_generate_html($segments[0]);
43                 }
44         }
45
46         public function get_metadata() {
47                 $data = [];
48
49                 $doc = new DOMDocument();
50                 $doc->loadHTML($this->html_content);
51
52                 $image_tag = $doc->getElementsByTagName('img')[0];
53
54                 if ($image_tag != null) {
55                         $data['post_hero_image'] = $image_tag->getAttribute('src');
56                         $data['post_hero_image_alt'] = htmlspecialchars($image_tag->getAttribute('alt'));
57                 }
58
59                 return new PostMetadata($data);
60         }
61 }
62
63 function cx_post_make_slug($title) {
64         $alnum_title = preg_replace('/[^A-Za-z0-9 ]?/', '', $title);
65         
66         $slug_components = explode(' ', $alnum_title, 10);
67         $slug_components = array_filter($slug_components);
68         $slug_components = array_values($slug_components); // re-index
69
70         $slug = join('-', $slug_components);
71         $slug = strtolower($slug);
72
73         return $slug;
74 }
75
76 function cx_posts_add_post($site_id, $title, $slug, $date, $page, $draft, $data) {
77         $creation_time = $update_time = time();
78         
79         if ($slug == null) {
80                 $slug = cx_post_make_slug($title);
81         }
82
83         if ($date == null) {
84                 $date = $update_time;
85         }
86
87         $sql = 'INSERT INTO posts (
88                         post_site_id,
89                         post_creation_time,
90                         post_update_time,
91                         post_slug,
92                         post_date,
93                         post_is_page,
94                         post_is_draft,
95                         post_title,
96                         post_data,
97                         post_data_version)
98                 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);';
99         cx_db_exec($sql, $site_id, $creation_time, $update_time, $slug, $date, $page, $draft, $title, $data, 1);
100 }
101
102 function cx_posts_update_post($post_id, $title, $slug, $date, $page, $draft, $data) {
103         $update_time = time();
104         
105         if ($slug == null) {
106                 $slug = cx_post_make_slug($title);
107         }
108
109         if ($date == null) {
110                 $date = $update_time;
111         }
112
113         $sql = 'UPDATE posts
114                 SET post_update_time = ?,
115                 post_slug = ?,
116                 post_date = ?,
117                 post_is_page = ?,
118                 post_is_draft = ?,
119                 post_title = ?,
120                 post_data = ?
121                 WHERE post_id == ?;';
122                 //LIMIT 1;';
123         cx_db_exec($sql, $update_time, $slug, $date, $page, $draft, $title, $data, $post_id);
124 }
125
126 function cx_posts_delete_post($post_id) {
127         $sql = 'DELETE FROM posts
128                 WHERE post_id == ?;';
129                 //LIMIT 1;';
130         cx_db_exec($sql, $post_id);
131 }
132
133 function cx_posts_get(int $limit = 0, int $offset = 0, bool $include_drafts = false) {
134         $sql = 'SELECT
135                 post_id,
136                 post_slug,
137                 post_date,
138                 post_is_draft,
139                 post_title,
140                 post_data
141                 FROM posts
142                 WHERE post_is_page == FALSE';
143         
144         if ($include_drafts == false) {
145                 $sql .= ' AND post_is_draft == FALSE';
146         }
147
148         $sql .= ' ORDER BY post_date DESC';
149         
150         if ($limit > 0) {
151                 $sql .= ' LIMIT ' . $limit;
152         }
153         
154         if ($offset > 0) {
155                 $sql .= ' OFFSET ' . $offset;
156         }
157
158         $sql .= ';';
159
160         foreach (cx_db_query($sql) as $post) {
161                 $p = new Post($post);
162                 yield $p;
163         }
164 }
165
166 function cx_posts_count(bool $include_drafts = false) {
167         $sql = 'SELECT
168                 COUNT(post_id) AS _count
169                 FROM posts
170                 WHERE post_is_page == FALSE';
171         
172         if ($include_drafts == false) {
173                 $sql .= ' AND post_is_draft == FALSE';
174         }
175
176         $sql .= ';';
177
178
179         foreach (cx_db_query($sql) as $count_details) {
180                 return $count_details['_count'];
181         }
182
183         return 0;
184 }
185
186 function cx_posts_find_post($post_id) {
187         $sql = 'SELECT
188                 post_id,
189                 post_slug,
190                 post_date,
191                 post_is_draft,
192                 post_title,
193                 post_data
194                 FROM posts
195                 WHERE post_id == ?
196                 LIMIT 1;';
197
198         foreach (cx_db_query($sql, $post_id) as $post) {
199                 $p = new Post($post);
200                 return $p;
201         }
202
203         return null;
204 }
205
206 function cx_posts_find_article_id($post_slug) {
207         $sql = 'SELECT
208                 post_id
209                 FROM posts
210                 WHERE post_slug == ?
211                 AND post_is_page == FALSE
212                 AND post_is_draft == FALSE
213                 LIMIT 1;';
214
215         foreach (cx_db_query($sql, $post_slug) as $post) {
216                 return $post['post_id'];
217         }
218
219         return null;
220 }
221
222 function cx_posts_find_page_id($post_slug) {
223         $sql = 'SELECT
224                 post_id
225                 FROM posts
226                 WHERE post_slug == ?
227                 AND post_is_page == TRUE
228                 AND post_is_draft == FALSE
229                 LIMIT 1;';
230
231         foreach (cx_db_query($sql, $post_slug) as $post) {
232                 return $post['post_id'];
233         }
234
235         return null;
236 }
237
238 function cx_pages_get() {
239         $sql = 'SELECT
240                 post_id,
241                 post_slug,
242                 post_date,
243                 post_is_draft,
244                 post_title,
245                 post_data
246                 FROM posts
247                 WHERE post_is_page == TRUE
248                 AND post_is_draft == FALSE
249                 ORDER BY post_creation_time DESC;';
250
251         foreach (cx_db_query($sql) as $post) {
252                 $p = new Post($post);
253                 yield $p;
254         }
255 }
256
257 cx_setup_register(1, function() {
258         cx_db_exec('CREATE TABLE posts (
259                         post_id INTEGER PRIMARY KEY,
260                         post_site_id INTEGER,
261                         post_creation_time INTEGER,
262                         post_update_time INTEGER,
263                         post_slug STRING,
264                         post_date INTEGER,
265                         post_is_page BOOLEAN,
266                         post_is_draft BOOLEAN,
267                         post_title STRING,
268                         post_data BLOB,
269                         post_data_version INTEGER,
270
271                         FOREIGN KEY(post_site_id) REFERENCES sites(site_id)
272                 );');
273 });