add 404s, fix markdown appearing in og tags

This commit is contained in:
Tomáš Mládek 2019-10-18 14:08:54 +02:00
parent 1d4e214b3e
commit a6454c0d83
6 changed files with 46 additions and 19 deletions

View file

@ -1,7 +1,8 @@
<?php if (isset($tag)): ?> <?php /** @noinspection PhpUndefinedVariableInspection */
if (isset($tag)): ?>
<div class="text tag-text"> <div class="text tag-text">
<h1><?= $tag["Name"] ?></h1> <h1><?= $tag["Name"] ?></h1>
<p class="tag-desc"><?= $tag["Description"] ?></p> <p class="tag-desc"><?= $tag["HTMLDescription"] ?></p>
</div> </div>
<?php endif; ?> <?php endif; ?>

View file

@ -19,10 +19,10 @@
</h3> </h3>
<?php endif; ?> <?php endif; ?>
<?php if (!empty($doc["Description"])): ?> <?php if ($doc["HTMLDescription"]): ?>
<p class="doc-description"><span <p class="doc-description"><span
class="doc-description-intro"><?= empty($doc["URL"]) ? "Content" : "Description" ?> class="doc-description-intro"><?= empty($doc["URL"]) ? "Content" : "Description" ?>
: </span><?= $doc["Description"] ?></p> : </span><?= $doc["HTMLDescription"] ?></p>
<?php endif; ?> <?php endif; ?>
<div class="doc-link"><span class="doc-link-intro">Get print label: </span> <div class="doc-link"><span class="doc-link-intro">Get print label: </span>

View file

@ -107,7 +107,7 @@
<div class="label-otherinfo-date">(Published: <?= $doc["Published"] ?>)</div> <div class="label-otherinfo-date">(Published: <?= $doc["Published"] ?>)</div>
<?php endif; ?> <?php endif; ?>
</h3> </h3>
<p class="label-description"><?= $doc["Description"] ?></p> <p class="label-description"><?= $doc["HTMLDescription"] ?></p>
</div> </div>
</div> </div>
<div class="label-footer"> <div class="label-footer">

View file

@ -2,11 +2,13 @@
class PileDB class PileDB
{ {
private $db; protected $db;
private $pd;
function __construct($dbpath = "pile.db") function __construct($dbpath = "pile.db")
{ {
$this->db = new SQLite3($dbpath); $this->db = new SQLite3($dbpath);
$this->pd = new Parsedown();
} }
function prepare($statement) function prepare($statement)
@ -59,11 +61,20 @@ class PileDB
return $tags; return $tags;
} }
/**
* Fetch pile document as an associative array.
*
* @throws DocumentNotFoundException when document isn't found.
*/
public function fetchDoc($id) public function fetchDoc($id)
{ {
$stmt_doc = $this->db->prepare("SELECT * FROM Documents WHERE ID = :id"); $stmt_doc = $this->db->prepare("SELECT * FROM Documents WHERE ID = :id");
$stmt_doc->bindValue(":id", $id, SQLITE3_INTEGER); $stmt_doc->bindValue(":id", $id, SQLITE3_INTEGER);
$doc = $stmt_doc->execute()->fetchArray(SQLITE3_ASSOC); $doc = $stmt_doc->execute()->fetchArray(SQLITE3_ASSOC);
if ($doc == false) {
throw new DocumentNotFoundException();
}
$doc["HTMLDescription"] = $this->pd->text($doc["Description"]);
$stmt_tags = $this->db->prepare("SELECT t.ID, t.Name FROM Tags t $stmt_tags = $this->db->prepare("SELECT t.ID, t.Name FROM Tags t
JOIN DocumentsToTags dt ON t.ID = dt.Tag JOIN DocumentsToTags dt ON t.ID = dt.Tag
@ -194,7 +205,9 @@ class PileDB
{ {
$stmt = $this->db->prepare("SELECT * FROM Tags WHERE ID == :tag"); $stmt = $this->db->prepare("SELECT * FROM Tags WHERE ID == :tag");
$stmt->bindValue(":tag", $tag, SQLITE3_INTEGER); $stmt->bindValue(":tag", $tag, SQLITE3_INTEGER);
return $stmt->execute()->fetchArray(SQLITE3_ASSOC); $tag = $stmt->execute()->fetchArray(SQLITE3_ASSOC);
$tag["HTMLDescription"] = $this->pd->text($tag["Description"]);
return $tag;
} }
public function updateTag($id, $name, $description) public function updateTag($id, $name, $description)
@ -244,4 +257,9 @@ class PileDB
} }
} }
class DocumentNotFoundException extends Exception
{
// noop
}
?> ?>

View file

@ -4,12 +4,19 @@ require '_util/PileDB.php';
require '_vendor/erusev/parsedown/Parsedown.php'; require '_vendor/erusev/parsedown/Parsedown.php';
$db = new PileDB(); $db = new PileDB();
$pd = new Parsedown();
session_start(); session_start();
$page = new Template();
if (isset($_GET["item"])) { if (isset($_GET["item"])) {
$doc = $db->fetchDoc($_GET["item"]); try {
$doc["Description"] = $pd->text($doc["Description"]); $doc = $db->fetchDoc($_GET["item"]);
} catch (DocumentNotFoundException $e) {
http_response_code(404);
$page->text = "Document not found.";
$page->redirect = "/";
echo $page->render("full_text.php");
die(0);
}
$doc_template = new Template(); $doc_template = new Template();
$doc_template->doc = $doc; $doc_template->doc = $doc;
@ -27,7 +34,6 @@ if (isset($_GET["item"])) {
$tag = $db->findTag($_GET["tag"]); $tag = $db->findTag($_GET["tag"]);
} }
$docs = $db->listDocs($tag["ID"]); $docs = $db->listDocs($tag["ID"]);
$tag["Description"] = $pd->text($tag["Description"]);
$selected_tag = $tag; $selected_tag = $tag;
$doc_list_template->tag = $tag; $doc_list_template->tag = $tag;
} }
@ -39,7 +45,6 @@ if (isset($_GET["item"])) {
$content = $intro_template->render('front_intro.php'); $content = $intro_template->render('front_intro.php');
} }
$page = new Template();
$page->doc_count = $db->getDocCount(); $page->doc_count = $db->getDocCount();
$page->none_count = $db->getUntaggedDocCount(); $page->none_count = $db->getUntaggedDocCount();
$page->tags = $db->getTags(); $page->tags = $db->getTags();

View file

@ -6,13 +6,16 @@ require '_templates/Template.php';
require '_util/PileDB.php'; require '_util/PileDB.php';
use Mpdf\Mpdf;
$db = new PileDB(); $db = new PileDB();
$doc = $db->fetchDoc($_GET["id"]); try {
$doc = $db->fetchDoc($_GET["id"]);
$pd = new Parsedown(); } catch (DocumentNotFoundException $e) {
$doc["Description"] = $pd->text($doc["Description"]); http_response_code(404);
$page->text = "Document not found.";
$page->redirect = "/";
echo $page->render("full_text.php");
die(0);
}
$front = new Template(); $front = new Template();
$front->doc = $doc; $front->doc = $doc;
@ -38,7 +41,7 @@ try {
$mpdf->showImageErrors = true; $mpdf->showImageErrors = true;
$mpdf->WriteHTML($front->render("_templates/label_template.php")); $mpdf->WriteHTML($front->render("_templates/label_template.php"));
$mpdf->Output(); $mpdf->Output();
} catch (\Mpdf\MpdfException $exception) { } catch (Exception $exception) {
http_response_code(500); ?> http_response_code(500); ?>
<h1>Something went wrong generating the label.</h1> <h1>Something went wrong generating the label.</h1>
<pre><?= $exception->getMessage() ?></pre> <pre><?= $exception->getMessage() ?></pre>