krz/php-blog

Archived

A PHP blogging system with built-in static comments. blog comments php static

Commit 40ff718d02

40ff718d02c38bdbaf9a690faf46e609c4f050df

parent: 703f8be3fd

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-22 20:55 UTC

Add Qlty config; pass Template its fields as an array

Template no longer creates dynamic properties, which PHP 8.2+ printed
as deprecation notices into every page, and two parameters are now
explicitly nullable for PHP 8.4. Page output is otherwise unchanged.
Parsedown.php and prism.js are vendored and excluded from Qlty.

Layout: unified · split

.qlty/.gitignore added +7
@@ -0,0 +1,7 @@
1*
2!configs
3!configs/**
4!hooks
5!hooks/**
6!qlty.toml
7!.gitignore
.qlty/qlty.toml added +47
@@ -0,0 +1,47 @@
1config_version = "0"
2
3exclude_patterns = [
4 "**/vendor/**",
5 "**/node_modules/**",
6 "**/testdata/**",
7 "**/dist/**",
8 "**/.build/**",
9 "**/build/**",
10 "**/*.min.*",
11 "**/generated/**",
12 ".claude/**",
13 "**/Pods/**",
14 # Vendored: erusev/parsedown and a PrismJS 1.23.0 build.
15 "_classes/Parsedown.php",
16 "static/prism.js",
17]
18
19test_patterns = [
20 "**/test/**",
21 "**/tests/**",
22 "**/Tests/**",
23 "**/*_test.*",
24 "**/test_*.*",
25 "**/*.test.*",
26 "**/*Tests.swift",
27 "e2e/**",
28]
29
30[[source]]
31name = "default"
32default = true
33
34[[plugin]]
35name = "osv-scanner"
36
37[[plugin]]
38name = "trufflehog"
39
40# Early returns are guard clauses, which keep the happy path unindented.
41[smells.return_statements]
42enabled = false
43
44# Total file complexity is the sum of its functions, which function_complexity
45# already checks.
46[smells.file_complexity]
47enabled = false
_classes/Template.php +19 −15
@@ -3,15 +3,23 @@
3 3
4class Template 4class Template
5{ 5{
6 public function __construct(string $canonicalURL, string $pageDescription, string $pageTitle, string $pageExtras, string $contentCol, string $commentSection) 6 // Template placeholders, in the order they are filled in.
7 private const PLACEHOLDERS = [
8 'Page_Title',
9 'Page_Description',
10 'Canonical_URL',
11 'Page_Extras',
12 'Content_Column',
13 'User_Comments',
14 'Current_Year',
15 ];
16
17 private array $fields;
18
19 // $fields maps each {Placeholder} in _templates/template.html to its value.
20 public function __construct(array $fields)
7 { 21 {
8 $this->canonicalURL = $canonicalURL; 22 $this->fields = $fields + ['Current_Year' => date("Y")];
9 $this->description = $pageDescription;
10 $this->title = $pageTitle;
11 $this->extras = $pageExtras;
12 $this->content = $contentCol;
13 $this->comments = $commentSection;
14 $this->currentYear = date("Y");
15 } 23 }
16 24
17 public function echoTemplate() 25 public function echoTemplate()
@@ -21,13 +29,9 @@ class Template
21 $page = file_get_contents($templateFile); 29 $page = file_get_contents($templateFile);
22 30
23 // Replace the template variables 31 // Replace the template variables
24 $page = str_replace('{Page_Title}', $this->title, $page); 32 foreach (self::PLACEHOLDERS as $name) {
25 $page = str_replace('{Page_Description}', $this->description, $page); 33 $page = str_replace('{' . $name . '}', $this->fields[$name], $page);
26 $page = str_replace('{Canonical_URL}', $this->canonicalURL, $page); 34 }
27 $page = str_replace('{Page_Extras}', $this->extras, $page);
28 $page = str_replace('{Content_Column}', $this->content, $page);
29 $page = str_replace('{User_Comments}', $this->comments, $page);
30 $page = str_replace('{Current_Year}', $this->currentYear, $page);
31 35
32 // Echo the filled-out template 36 // Echo the filled-out template
33 echo $page; 37 echo $page;
_functions/loadJSON.php +1 −1
@@ -81,7 +81,7 @@ function loadPostJSON(string $fileName, string $query)
81 } 81 }
82} 82}
83 83
84function loadCommentJSON(string $fileName, string $query = null): string 84function loadCommentJSON(string $fileName, ?string $query = null): string
85{ 85{
86 // Set up an empty comment section 86 // Set up an empty comment section
87 $commentSection = ''; 87 $commentSection = '';
_functions/parseMarkdown.php +1 −1
@@ -1,6 +1,6 @@
1<?php 1<?php
2 2
3function parseMarkdown(string $fileName = null, string $fileContents = null) 3function parseMarkdown(?string $fileName = null, ?string $fileContents = null)
4{ 4{
5 if ($fileName != null) { 5 if ($fileName != null) {
6 // Get file contents 6 // Get file contents
index.php +32 −32
@@ -55,14 +55,14 @@ function showHomepage()
55 55
56 // Create a template 56 // Create a template
57 include_once('_classes/Template.php'); 57 include_once('_classes/Template.php');
58 $template = new Template( 58 $template = new Template([
59 $GLOBALS['fullDomain'], 59 'Canonical_URL' => $GLOBALS['fullDomain'],
60 'Explore the thoughts of...', 60 'Page_Description' => 'Explore the thoughts of...',
61 'Blog | YourName', 61 'Page_Title' => 'Blog | YourName',
62 '', 62 'Page_Extras' => '',
63 $contentCol, 63 'Content_Column' => $contentCol,
64 '' 64 'User_Comments' => '',
65 ); 65 ]);
66 66
67 // Echo the HTML to the user 67 // Echo the HTML to the user
68 $template->echoTemplate(); 68 $template->echoTemplate();
@@ -102,14 +102,14 @@ function showPost($params)
102 102
103 // Create a template 103 // Create a template
104 include_once('_classes/Template.php'); 104 include_once('_classes/Template.php');
105 $template = new Template( 105 $template = new Template([
106 $GLOBALS['fullDomain'] . '/post/' . $query, 106 'Canonical_URL' => $GLOBALS['fullDomain'] . '/post/' . $query,
107 $headerData->description . ' Read more at ' . $GLOBALS['fullDomain'] . '!', 107 'Page_Description' => $headerData->description . ' Read more at ' . $GLOBALS['fullDomain'] . '!',
108 $headerData->title . ' | Blog', 108 'Page_Title' => $headerData->title . ' | Blog',
109 '<link rel="stylesheet" href="/static/prism.css"><script src="/static/prism.js"></script>', 109 'Page_Extras' => '<link rel="stylesheet" href="/static/prism.css"><script src="/static/prism.js"></script>',
110 $contentCol, 110 'Content_Column' => $contentCol,
111 $commentSection 111 'User_Comments' => $commentSection,
112 ); 112 ]);
113 113
114 // Echo the HTML to the user 114 // Echo the HTML to the user
115 $template->echoTemplate(); 115 $template->echoTemplate();
@@ -132,14 +132,14 @@ function showComments()
132 132
133 // Create a template 133 // Create a template
134 include_once('_classes/Template.php'); 134 include_once('_classes/Template.php');
135 $template = new Template( 135 $template = new Template([
136 $GLOBALS['fullDomain'] . '/comments/', 136 'Canonical_URL' => $GLOBALS['fullDomain'] . '/comments/',
137 'Read through some recent comments for blog posts at ' . $GLOBALS['fullDomain'] . '.', 137 'Page_Description' => 'Read through some recent comments for blog posts at ' . $GLOBALS['fullDomain'] . '.',
138 'Recent Comments | Blog', 138 'Page_Title' => 'Recent Comments | Blog',
139 '<link rel="stylesheet" href="/static/prism.css"><script src="/static/prism.js"></script><meta name="robots" content="noindex,nofollow">', 139 'Page_Extras' => '<link rel="stylesheet" href="/static/prism.css"><script src="/static/prism.js"></script><meta name="robots" content="noindex,nofollow">',
140 '', 140 'Content_Column' => '',
141 $commentSection 141 'User_Comments' => $commentSection,
142 ); 142 ]);
143 143
144 // Echo the HTML to the user 144 // Echo the HTML to the user
145 $template->echoTemplate(); 145 $template->echoTemplate();
@@ -156,14 +156,14 @@ function showCategory()
156 156
157 // Create a template 157 // Create a template
158 include_once('_classes/Template.php'); 158 include_once('_classes/Template.php');
159 $template = new Template( 159 $template = new Template([
160 $GLOBALS['fullDomain'] . '/categories/', 160 'Canonical_URL' => $GLOBALS['fullDomain'] . '/categories/',
161 'Browse the categories for blog posts at ' . $GLOBALS['fullDomain'] . '.', 161 'Page_Description' => 'Browse the categories for blog posts at ' . $GLOBALS['fullDomain'] . '.',
162 'Categories | Blog', 162 'Page_Title' => 'Categories | Blog',
163 '<meta name="robots" content="noindex,nofollow">', 163 'Page_Extras' => '<meta name="robots" content="noindex,nofollow">',
164 $contentCol, 164 'Content_Column' => $contentCol,
165 '' 165 'User_Comments' => '',
166 ); 166 ]);
167 167
168 // Echo the HTML to the user 168 // Echo the HTML to the user
169 $template->echoTemplate(); 169 $template->echoTemplate();