]>
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);
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;
34 $segments = explode('---', $this->data
, 2);
35 if (count($segments) > 1) {
36 $this->html_excerpt
= mk_markdown($segments[0]);
41 function cx_post_make_slug($title) {
42 $alnum_title = preg_replace('/[^A-Za-z0-9 ]?/', '', $title);
44 $slug_components = explode(' ', $alnum_title, 10);
45 $slug_components = array_filter($slug_components);
46 $slug_components = array_values($slug_components); // re-index
48 $slug = join('-', $slug_components);
49 $slug = strtolower($slug);
54 function cx_posts_add_post($site_id, $title, $slug, $date, $draft, $data) {
55 $creation_time = $update_time = time();
58 $slug = cx_post_make_slug($title);
65 $sql = 'INSERT INTO posts (
76 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);';
77 cx_db_exec($sql, $site_id, $creation_time, $update_time, $slug, $date, false, $draft, $title, $data, 1);
80 function cx_posts_update_post($post_id, $title, $slug, $date, $draft, $data) {
81 $update_time = time();
84 $slug = cx_post_make_slug($title);
92 SET post_update_time = ?,
100 cx_db_exec($sql, $update_time, $slug, $date, $draft, $title, $data, $post_id);
103 function cx_posts_delete_post($post_id) {
104 $sql = 'DELETE FROM posts
105 WHERE post_id == ?;';
107 cx_db_exec($sql, $post_id);
110 function cx_posts_get(int $limit = 0, bool $include_drafts = false) {
119 WHERE post_is_page == FALSE';
121 if ($include_drafts == false) {
122 $sql .= ' AND post_is_draft == FALSE';
125 $sql .= ' ORDER BY post_date DESC';
128 $sql .= ' LIMIT ' . $limit;
133 foreach (cx_db_query($sql) as $post) {
134 $p = new Post($post);
139 function cx_posts_find_post($post_id) {
148 WHERE post_is_page == FALSE
152 foreach (cx_db_query($sql, $post_id) as $post) {
153 $p = new Post($post);
160 function cx_posts_find_post_id($post_slug) {
167 foreach (cx_db_query($sql, $post_slug) as $post) {
168 return $post['post_id'];
174 function cx_pages_get() {
183 WHERE post_is_page == TRUE
184 AND post_is_draft == FALSE
185 ORDER BY post_creation_time DESC;';
187 foreach (cx_db_query($sql) as $post) {
188 $p = new Post($post);
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,
201 post_is_page BOOLEAN,
202 post_is_draft BOOLEAN,
205 post_data_version INTEGER,
207 FOREIGN KEY(post_site_id) REFERENCES sites(site_id)