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)
{
$conn = get_connection();
$stmt = $conn->prepare('SELECT article_id FROM article WHERE author_id = ? ORDER BY published_at DESC');
$stmt->bind_param('i', $author_id);
$stmt->execute();
$results = $stmt->get_result();
$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)
{
$conn = get_connection();
$stmt = $conn->prepare('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');
$stmt->bind_param('s', $tag);
$stmt->execute();
$results = $stmt->get_result();
$ids = [];
while ($row = $results->fetch_assoc())
$ids[] = $row['article_id'];
return $ids;
}
/*
* Reads the markdown file for a given article and generates the HTML for it.
*/
function article_page_from_markdown($article_id)
{
$conn = get_connection();
$stmt = $conn->prepare('SELECT * FROM article WHERE article_id = ?');
$stmt->bind_param('i', $article_id);
$stmt->execute();
$article = $stmt->get_result()->fetch_assoc();
$stmt = $conn->prepare('SELECT user_id, username, profile_picture FROM user WHERE user_id = ?');
$stmt->bind_param('i', $article['author_id']);
$stmt->execute();
$author = $stmt->get_result()->fetch_assoc();
$markdown = read_file_one_string($_ENV['ARTICLES_FQ_PATH'] . $article_id . '/article.md');
$parsedown = new Parsedown();
$article_content = $parsedown->text($markdown);
$article_toc = '';
$lines = preg_split("/\r\n|\n|\r/", $article_content);
foreach ($lines as $line)
if (str_contains($line, '
'))
$article_toc .= '
' . extract_content_from_tag($line, '
', '
') . '';
$html = '
' . $article['title'] . '
Published on ' . $article['published_at'] . '
' . $article_toc . '
' . $article_content . '
';
return $html;
}
function article_filters()
{
$filters = '
';
return $filters;
}
/*
* Generates cards for a given article.
*/
function article_card($article_id)
{
$conn = get_connection();
$stmt = $conn->prepare('SELECT * FROM article WHERE article_id = ?');
$stmt->bind_param('i', $article_id);
$stmt->execute();
$article = $stmt->get_result()->fetch_assoc();
$stmt = $conn->prepare('SELECT tag FROM article_tags INNER JOIN tags ON article_tags.tag_id = tags.tag_id WHERE article_id = ?');
$stmt->bind_param('i', $article_id);
$stmt->execute();
$tags_results = $stmt->get_result();
$tags_html = '';
if ($tags_results != null) {
$tags_html .= '';
while ($row = $tags_results->fetch_assoc())
$tags_html .= '
#' . $row['tag'] . '';
$tags_html .= '
';
}
$stmt = $conn->prepare('SELECT user_id, username, profile_picture FROM user WHERE user_id = ?');
$stmt->bind_param('i', $article['author_id']);
$stmt->execute();
$author = $stmt->get_result()->fetch_assoc();
$card = '
' . $article['title'] . '
' . $article['excerpt'] . '
';
return $card;
}
function create_article($author_id, $title, $excerpt, $tags, $markdown_file_contents)
{
$conn = get_connection();
$stmt = $conn->prepare('INSERT INTO article (author_id, title, excerpt, published_at) VALUES (?, ?, ?, CURRENT_TIMESTAMP)');
$stmt->bind_param('iss', $author_id, $title, $excerpt);
$stmt->execute();
$id = mysqli_insert_id($conn);
$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 ';
for ($i = 0; $i < count($tags); $i++) {
$tag_insert_stmt .= '(' . $id . ', ?)';
if ($i < count($tags) - 1)
$tag_insert_stmt .= ', ';
}
$stmt = $conn->prepare($tag_insert_stmt);
$stmt->bind_param('i', ...$tags);
$stmt->execute();
}
return $id;
}
function delete_article($article_id)
{
$conn = get_connection();
$stmt = $conn->prepare('DELETE FROM article_tags WHERE article_id = ?');
$stmt->bind_param('i', $article_id);
$stmt->execute();
$stmt = $conn->prepare('DELETE FROM article WHERE article_id = ?');
$stmt->bind_param('i', $article_id);
$stmt->execute();
delete_dir($_ENV['ARTICLES_FQ_PATH'] . $article_id . '/');
}
function delete_articles_by_author($author_id)
{
$conn = get_connection();
$stmt = $conn->prepare('DELETE FROM article WHERE author_id = ?');
$stmt->bind_param('i', $author_id);
$stmt->execute();
}
function increment_read_counter($article_id)
{
$conn = get_connection();
$stmt = $conn->prepare('UPDATE article SET read_count = read_count + 1 WHERE article_id = ?');
$stmt->bind_param('i', $article_id);
$stmt->execute();
}