]>
git.bts.cx Git - cx.git/blob - cx/lib/posts.php
3 cx_require('lib', 'db.php');
4 cx_require('lib', 'setup.php');
5 cx_require('third_party', 'parsedown', 'Parsedown.php');
7 function mk_markdown($markdown) {
8 static $Parsedown = new Parsedown();
10 return $Parsedown->text($markdown);
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;
32 $segments = explode('---', $this->data
, 2);
33 if (count($segments) > 1) {
34 $this->html_excerpt
= mk_markdown($segments[0]);
39 function cx_post_make_slug($title) {
40 $alnum_title = preg_replace('/[^A-Za-z0-9 ]?/', '', $title);
42 $slug_components = explode(' ', $alnum_title, 10);
43 $slug_components = array_filter($slug_components);
44 $slug_components = array_values($slug_components); // re-index
46 $slug = join('-', $slug_components);
47 $slug = strtolower($slug);
52 function cx_posts_add_post($site_id, $title, $slug, $date, $data) {
53 $creation_time = $update_time = time();
56 $slug = cx_post_make_slug($title);
63 $sql = 'INSERT INTO posts (
73 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);';
74 cx_db_exec($sql, $site_id, $creation_time, $update_time, $slug, $date, false, $title, $data, 1);
77 function cx_posts_update_post($post_id, $title, $slug, $date, $data) {
78 $update_time = time();
81 $slug = cx_post_make_slug($title);
89 SET post_update_time = ?,
96 cx_db_exec($sql, $update_time, $slug, $date , $title, $data, $post_id);
99 function cx_posts_delete_post($post_id) {
100 $sql = 'DELETE FROM posts
101 WHERE post_id == ?;';
103 cx_db_exec($sql, $post_id);
106 function cx_posts_get(int $limit = 0) {
114 WHERE post_is_page==FALSE
115 ORDER BY post_date DESC';
118 $sql .= ' LIMIT ' . $limit;
123 foreach (cx_db_query($sql) as $post) {
124 $p = new Post($post);
129 function cx_posts_find_post($post_id) {
137 WHERE post_is_page == FALSE
141 foreach (cx_db_query($sql, $post_id) as $post) {
142 $p = new Post($post);
149 function cx_posts_find_post_id($post_slug) {
156 foreach (cx_db_query($sql, $post_slug) as $post) {
157 return $post['post_id'];
163 function cx_pages_get() {
171 WHERE post_is_page == TRUE
172 ORDER BY post_creation_time DESC;';
174 foreach (cx_db_query($sql) as $post) {
175 $p = new Post($post);
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,
188 post_is_page BOOLEAN,
191 post_data_version INTEGER,
193 FOREIGN KEY(post_site_id) REFERENCES sites(site_id)