]>
git.bts.cx Git - cx.git/blob - cx/lib/posts.php
0a2ac4d997ff9c5133c0c0a27cffc6e4ee376f9e
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);
15 public $hero_image_alt;
17 public function __construct($dict) {
18 $this->hero_image
= isset($dict['post_hero_image']) ?
$dict['post_hero_image'] : null;
19 $this->hero_image_alt
= isset($dict['post_hero_image_alt']) ?
$dict['post_hero_image_alt'] : null;
33 public function __construct($dict) {
34 $this->id
= $dict['post_id']; // FIXME, hide when not used?
35 $this->title
= $dict['post_title'];
36 $this->slug
= $dict['post_slug'];
37 $this->date
= $dict['post_date'];
38 $this->is_draft
= $dict['post_is_draft'];
39 $this->data
= $dict['post_data'];
40 $this->html_content
= mk_markdown($this->data
);
41 $this->html_excerpt
= null;
44 $segments = explode('---', $this->data
, 2);
45 if (count($segments) > 1) {
46 $this->html_excerpt
= mk_markdown($segments[0]);
50 public function get_metadata() {
53 $doc = new DOMDocument();
54 $doc->loadHTML($this->html_content
);
56 $image_tag = $doc->getElementsByTagName('img')[0];
58 if ($image_tag != null) {
59 $data['post_hero_image'] = $image_tag->getAttribute('src');
60 $data['post_hero_image_alt'] = htmlspecialchars($image_tag->getAttribute('alt'));
63 return new PostMetadata($data);
67 function cx_post_make_slug($title) {
68 $alnum_title = preg_replace('/[^A-Za-z0-9 ]?/', '', $title);
70 $slug_components = explode(' ', $alnum_title, 10);
71 $slug_components = array_filter($slug_components);
72 $slug_components = array_values($slug_components); // re-index
74 $slug = join('-', $slug_components);
75 $slug = strtolower($slug);
80 function cx_posts_add_post($site_id, $title, $slug, $date, $draft, $data) {
81 $creation_time = $update_time = time();
84 $slug = cx_post_make_slug($title);
91 $sql = 'INSERT INTO posts (
102 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);';
103 cx_db_exec($sql, $site_id, $creation_time, $update_time, $slug, $date, false, $draft, $title, $data, 1);
106 function cx_posts_update_post($post_id, $title, $slug, $date, $draft, $data) {
107 $update_time = time();
110 $slug = cx_post_make_slug($title);
114 $date = $update_time;
118 SET post_update_time = ?,
124 WHERE post_id == ?;';
126 cx_db_exec($sql, $update_time, $slug, $date, $draft, $title, $data, $post_id);
129 function cx_posts_delete_post($post_id) {
130 $sql = 'DELETE FROM posts
131 WHERE post_id == ?;';
133 cx_db_exec($sql, $post_id);
136 function cx_posts_get(int $limit = 0, bool $include_drafts = false) {
145 WHERE post_is_page == FALSE';
147 if ($include_drafts == false) {
148 $sql .= ' AND post_is_draft == FALSE';
151 $sql .= ' ORDER BY post_date DESC';
154 $sql .= ' LIMIT ' . $limit;
159 foreach (cx_db_query($sql) as $post) {
160 $p = new Post($post);
165 function cx_posts_find_post($post_id) {
174 WHERE post_is_page == FALSE
178 foreach (cx_db_query($sql, $post_id) as $post) {
179 $p = new Post($post);
186 function cx_posts_find_post_id($post_slug) {
193 foreach (cx_db_query($sql, $post_slug) as $post) {
194 return $post['post_id'];
200 function cx_pages_get() {
209 WHERE post_is_page == TRUE
210 AND post_is_draft == FALSE
211 ORDER BY post_creation_time DESC;';
213 foreach (cx_db_query($sql) as $post) {
214 $p = new Post($post);
219 cx_setup_register(1, function() {
220 cx_db_exec('CREATE TABLE posts (
221 post_id INTEGER PRIMARY KEY,
222 post_site_id INTEGER,
223 post_creation_time INTEGER,
224 post_update_time INTEGER,
227 post_is_page BOOLEAN,
228 post_is_draft BOOLEAN,
231 post_data_version INTEGER,
233 FOREIGN KEY(post_site_id) REFERENCES sites(site_id)