]> git.bts.cx Git - cx.git/blob - cx/lib/posts.php
Fix? CSS for images in posts
[cx.git] / cx / lib / posts.php
1 <?php
2
3 cx_require('lib', 'db.php');
4 cx_require('lib', 'setup.php');
5 cx_require('third_party', 'parsedown', 'Parsedown.php');
6
7 function mk_markdown($markdown) {
8         static $Parsedown = new Parsedown();
9
10         return $Parsedown->text($markdown);
11 }
12
13 class Post {
14         public $id;
15         public $title;
16         public $slug;
17         public $date;
18         public $is_draft;
19         public $data;
20         public $html_content;
21         public $html_excerpt;
22
23         public function __construct($dict) {
24                 $this->id = $dict['post_id']; // FIXME, hide when not used?
25                 $this->title = $dict['post_title'];
26                 $this->slug = $dict['post_slug'];
27                 $this->date = $dict['post_date'];
28                 $this->is_draft = $dict['post_is_draft'];
29                 $this->data = $dict['post_data'];
30                 $this->html_content = mk_markdown($this->data);
31                 $this->html_excerpt = null;
32
33                 // Read more...
34                 $segments = explode('---', $this->data, 2);
35                 if (count($segments) > 1) {
36                         $this->html_excerpt = mk_markdown($segments[0]);
37                 }
38         }
39 }
40
41 function cx_post_make_slug($title) {
42         $alnum_title = preg_replace('/[^A-Za-z0-9 ]?/', '', $title);
43         
44         $slug_components = explode(' ', $alnum_title, 10);
45         $slug_components = array_filter($slug_components);
46         $slug_components = array_values($slug_components); // re-index
47
48         $slug = join('-', $slug_components);
49         $slug = strtolower($slug);
50
51         return $slug;
52 }
53
54 function cx_posts_add_post($site_id, $title, $slug, $date, $draft, $data) {
55         $creation_time = $update_time = time();
56         
57         if ($slug == null) {
58                 $slug = cx_post_make_slug($title);
59         }
60
61         if ($date == null) {
62                 $date = $update_time;
63         }
64
65         $sql = 'INSERT INTO posts (
66                         post_site_id,
67                         post_creation_time,
68                         post_update_time,
69                         post_slug,
70                         post_date,
71                         post_is_page,
72                         post_is_draft,
73                         post_title,
74                         post_data,
75                         post_data_version)
76                 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);';
77         cx_db_exec($sql, $site_id, $creation_time, $update_time, $slug, $date, false, $draft, $title, $data, 1);
78 }
79
80 function cx_posts_update_post($post_id, $title, $slug, $date, $draft, $data) {
81         $update_time = time();
82         
83         if ($slug == null) {
84                 $slug = cx_post_make_slug($title);
85         }
86
87         if ($date == null) {
88                 $date = $update_time;
89         }
90
91         $sql = 'UPDATE posts
92                 SET post_update_time = ?,
93                 post_slug = ?,
94                 post_date = ?,
95                 post_is_draft = ?,
96                 post_title = ?,
97                 post_data = ?
98                 WHERE post_id == ?;';
99                 //LIMIT 1;';
100         cx_db_exec($sql, $update_time, $slug, $date, $draft, $title, $data, $post_id);
101 }
102
103 function cx_posts_delete_post($post_id) {
104         $sql = 'DELETE FROM posts
105                 WHERE post_id == ?;';
106                 //LIMIT 1;';
107         cx_db_exec($sql, $post_id);
108 }
109
110 function cx_posts_get(int $limit = 0, bool $include_drafts = false) {
111         $sql = 'SELECT
112                 post_id,
113                 post_slug,
114                 post_date,
115                 post_is_draft,
116                 post_title,
117                 post_data
118                 FROM posts
119                 WHERE post_is_page == FALSE';
120         
121         if ($include_drafts == false) {
122                 $sql .= ' AND post_is_draft == FALSE';
123         }
124
125         $sql .= ' ORDER BY post_date DESC';
126         
127         if ($limit > 0) {
128                 $sql .= ' LIMIT ' . $limit;
129         }
130
131         $sql .= ';';
132
133         foreach (cx_db_query($sql) as $post) {
134                 $p = new Post($post);
135                 yield $p;
136         }
137 }
138
139 function cx_posts_find_post($post_id) {
140         $sql = 'SELECT
141                 post_id,
142                 post_slug,
143                 post_date,
144                 post_is_draft,
145                 post_title,
146                 post_data
147                 FROM posts
148                 WHERE post_is_page == FALSE
149                 AND post_id == ?
150                 LIMIT 1;';
151
152         foreach (cx_db_query($sql, $post_id) as $post) {
153                 $p = new Post($post);
154                 return $p;
155         }
156
157         return null;
158 }
159
160 function cx_posts_find_post_id($post_slug) {
161         $sql = 'SELECT
162                 post_id
163                 FROM posts
164                 WHERE post_slug == ?
165                 LIMIT 1;';
166
167         foreach (cx_db_query($sql, $post_slug) as $post) {
168                 return $post['post_id'];
169         }
170
171         return null;
172 }
173
174 function cx_pages_get() {
175         $sql = 'SELECT
176                 post_id,
177                 post_slug,
178                 post_date,
179                 post_is_draft,
180                 post_title,
181                 post_data
182                 FROM posts
183                 WHERE post_is_page == TRUE
184                 AND post_is_draft == FALSE
185                 ORDER BY post_creation_time DESC;';
186
187         foreach (cx_db_query($sql) as $post) {
188                 $p = new Post($post);
189                 yield $p;
190         }
191 }
192
193 cx_setup_register(1, function() {
194         cx_db_exec('CREATE TABLE posts (
195                         post_id INTEGER PRIMARY KEY,
196                         post_site_id INTEGER,
197                         post_creation_time INTEGER,
198                         post_update_time INTEGER,
199                         post_slug STRING,
200                         post_date INTEGER,
201                         post_is_page BOOLEAN,
202                         post_is_draft BOOLEAN,
203                         post_title STRING,
204                         post_data BLOB,
205                         post_data_version INTEGER,
206
207                         FOREIGN KEY(post_site_id) REFERENCES sites(site_id)
208                 );');
209 });