135 lines
3.7 KiB
PHP
135 lines
3.7 KiB
PHP
<?php
|
|
require_once 'db.php';
|
|
|
|
function article_ids_by_newest()
|
|
{
|
|
$conn = get_connection();
|
|
$results = $conn->query('SELECT article_id FROM article ORDER BY published_at DESC');
|
|
$ids = [];
|
|
|
|
while ($row = $results->fetch_assoc())
|
|
$ids[] = $row['article_id'];
|
|
|
|
return $ids;
|
|
}
|
|
|
|
function article_ids_by_oldest()
|
|
{
|
|
$conn = get_connection();
|
|
$results = $conn->query('SELECT article_id FROM article ORDER BY published_at ASC');
|
|
$ids = [];
|
|
|
|
while ($row = $results->fetch_assoc())
|
|
$ids[] = $row['article_id'];
|
|
|
|
return $ids;
|
|
}
|
|
|
|
function articles_ids_by_author($author_id)
|
|
{
|
|
$results = exec_stmt('SELECT article_id FROM article WHERE author_id = ? ORDER BY published_at DESC', 'i', $author_id);
|
|
$ids = [];
|
|
|
|
while ($row = $results->fetch_assoc())
|
|
$ids[] = $row['article_id'];
|
|
|
|
return $ids;
|
|
}
|
|
|
|
function article_tags()
|
|
{
|
|
$conn = get_connection();
|
|
$results = $conn->query('SELECT * FROM tags');
|
|
$tags = [];
|
|
|
|
while ($row = $results->fetch_assoc())
|
|
$tags[] = $row;
|
|
|
|
return $tags;
|
|
}
|
|
|
|
function article_ids_by_tag($tag)
|
|
{
|
|
$results = exec_stmt('SELECT article_id FROM article WHERE article.article_id IN ( SELECT article_tags.article_id FROM article_tags INNER JOIN tags ON article_tags.tag_id = tags.tag_id WHERE tag = ?) ORDER BY published_at DESC', 's', $tag);
|
|
$ids = [];
|
|
|
|
while ($row = $results->fetch_assoc())
|
|
$ids[] = $row['article_id'];
|
|
|
|
return $ids;
|
|
}
|
|
|
|
function create_article($author_id, $title, $excerpt, $tags, $markdown_file_contents)
|
|
{
|
|
$id = exec_stmt('INSERT INTO article (author_id, title, excerpt, published_at) VALUES (?, ?, ?, CURRENT_TIMESTAMP)', 'iss', $author_id, $title, $excerpt);
|
|
|
|
$path = $_ENV['ARTICLES_FQ_PATH'] . $id . '/';
|
|
mkdir($path);
|
|
|
|
$fs = fopen($path . 'article.md', 'a');
|
|
fwrite($fs, $markdown_file_contents);
|
|
fclose($fs);
|
|
|
|
if (count($tags) > 0) {
|
|
$tag_insert_stmt = 'INSERT INTO article_tags (article_id, tag_id) VALUES ';
|
|
$types = '';
|
|
for ($i = 0; $i < count($tags); $i++) {
|
|
$tag_insert_stmt .= '(' . $id . ', ?)';
|
|
|
|
if ($i < count($tags) - 1)
|
|
$tag_insert_stmt .= ', ';
|
|
|
|
$types .= 'i';
|
|
}
|
|
|
|
exec_stmt($tag_insert_stmt, $types, ...$tags);
|
|
}
|
|
|
|
return $id;
|
|
}
|
|
|
|
function update_article($article_id, $title, $excerpt, $tags, $markdown_file_contents)
|
|
{
|
|
exec_stmt('UPDATE article SET title = ?, excerpt = ?, updated_at = CURRENT_TIMESTAMP WHERE article_id = ?', 'ssi', $title, $excerpt, $article_id);
|
|
|
|
$path = $_ENV['ARTICLES_FQ_PATH'] . $article_id . '/';
|
|
$fs = fopen($path . 'article.md', 'w');
|
|
fwrite($fs, $markdown_file_contents);
|
|
fclose($fs);
|
|
|
|
exec_stmt('DELETE from article_tags WHERE article_id = ?', 'i', $article_id);
|
|
|
|
if (count($tags) > 0) {
|
|
$tag_insert_stmt = 'INSERT INTO article_tags (article_id, tag_id) VALUES ';
|
|
$types = '';
|
|
for ($i = 0; $i < count($tags); $i++) {
|
|
$tag_insert_stmt .= '(' . $article_id . ', ?)';
|
|
|
|
if ($i < count($tags) - 1)
|
|
$tag_insert_stmt .= ', ';
|
|
|
|
$types .= 'i';
|
|
}
|
|
|
|
exec_stmt($tag_insert_stmt, $types, ...$tags);
|
|
}
|
|
}
|
|
|
|
function delete_article($article_id)
|
|
{
|
|
exec_stmt('DELETE FROM article_tags WHERE article_id = ?', 'i', $article_id);
|
|
exec_stmt('DELETE FROM article WHERE article_id = ?', 'i', $article_id);
|
|
|
|
delete_dir($_ENV['ARTICLES_FQ_PATH'] . $article_id . '/');
|
|
}
|
|
|
|
function delete_articles_by_author($author_id)
|
|
{
|
|
exec_stmt('DELETE FROM article WHERE author_id = ?', 'i', $author_id);
|
|
}
|
|
|
|
function increment_read_counter($article_id)
|
|
{
|
|
exec_stmt('UPDATE article SET read_count = read_count + 1 WHERE article_id = ?', 'i', $article_id);
|
|
}
|