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