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