Files
novaconium/novaconium/bin/create-static-page.php
T
code 37b6764431 Add content index: keywords, tags/categories, search, and XML sitemap
Ships Blog tags/categories, Internal search, and XML sitemap together as
one shared mechanism, plus a new meta keywords request, rather than three
separate ones - the "worth deciding together" call ISSUES.md made when
XML sitemap was first written.

Content stays in files. Per-page metadata is four Twig blocks in the
root layout, the same override mechanism already used for
title/description/og_* - keywords (rendered), tags/changefreq/priority
(not rendered, harvested only). App\ContentIndexer crawls every routable
page (Overlay::listPageDirs(), new) and pulls each block via
Renderer::renderForIndex() (new) calling Twig's own renderBlock() API,
not regex-parsing .twig source, so overrides and layout inheritance
resolve exactly like a real render. Rendered HTML is stripped and
indexed into a SQLite FTS5 table for search.

Off by default (content_index_enabled) - same posture as Matomo/admin
auth, since this is a real SQLite dependency plenty of sites won't want.
Verified true zero footprint when disabled: no data/novaconium.sqlite
gets created, all three consumer routes 404 like they don't exist.
content_index_auto (default true) reindexes lazily on first stale touch
of a consumer route, never on a normal page view; both that path and the
explicit `php novaconium/bin/index-content.php` share one reindex().

Two real bugs caught by testing, not review: a reentrancy bug where
/search's own sidecar calling ensureFresh() during the crawl triggered a
nested reindex() mid-transaction (fixed with a static re-entrancy guard),
and a wrong PDO constant in the search sidecar. Also fixed two unrelated
pre-existing bugs found while building this: an unescaped {{ }} in
admin/docs/sidecars that fataled Twig on any real render of that page,
and a stale "no database layer yet" claim there and in Lib\Input's
docblock, left over from before Lib\Db shipped.

Lib\Db's migrations_dir now accepts an ordered list of roots, not just
one path, so the content index's schema could ship as a framework
migration (novaconium/migrations/) without colliding with project
migrations in App/migrations/ - the two-root extension point flagged in
AGENTS.md when SQLite groundwork shipped. Migrations are tracked by path
relative to the repo root rather than bare filename so two roots with a
same-named file can't shadow each other.

New consumer routes: novaconium/pages/sitemap.xml/ and
novaconium/pages/search/ (framework defaults), App/pages/blog/tag/[tag]/
(project-owned, since blog/ is project content - the existing hand
-written post array in App/pages/blog/index.php is untouched). Added
tags to the 4 existing blog posts as a real demonstration.

Closes the Blog tags/categories, Internal search, and XML sitemap
backlog items in novaconium/ISSUES.md.
2026-07-14 17:35:30 +00:00

103 lines
3.5 KiB
PHP

<?php
/**
* Scaffolds a new static page from the same copy-paste starter template
* documented at /admin/docs/seo, so adding a page doesn't require manually
* copying it by hand.
*
* Usage:
* php novaconium/bin/create-static-page.php <path>
*
* <path> is relative to App/pages/ and becomes the route — with or
* without a trailing index.twig/.twig, so any of these are equivalent:
* php novaconium/bin/create-static-page.php pricing
* php novaconium/bin/create-static-page.php blog/my-new-post
* php novaconium/bin/create-static-page.php blog/my-new-post.twig
* php novaconium/bin/create-static-page.php blog/my-new-post/index.twig
*
* Creates App/pages/<path>/index.twig and nothing else — no sidecar, since
* the point of this template is a sidecar-less, statically-cacheable page
* (see /admin/docs/caching). Add an index.php next to it yourself if the
* page ends up needing one (see /admin/docs/sidecars).
*/
$path = $argv[1] ?? null;
if ($path === null || trim($path) === '') {
fwrite(STDERR, "Usage: php novaconium/bin/create-static-page.php <path>\n");
fwrite(STDERR, "Example: php novaconium/bin/create-static-page.php blog/my-new-post\n");
exit(1);
}
// Normalize away a trailing /index.twig or .twig, so the path can be
// passed either as a bare route or as a file-shaped argument.
$path = trim($path, '/');
$path = preg_replace('#/index\.twig$#', '', $path);
$path = preg_replace('#\.twig$#', '', $path);
if ($path === '') {
fwrite(STDERR, "Error: path cannot be empty.\n");
exit(1);
}
// Reserved segments (see Router::resolve()) can never be routed to
// directly — refuse to scaffold a page nothing could ever visit.
foreach (explode('/', $path) as $segment) {
if ($segment === '' || str_starts_with($segment, '_') || $segment === '404') {
fwrite(STDERR, "Error: \"$segment\" is a reserved path segment (starts with _, or is literally \"404\") and can never be routed to directly.\n");
exit(1);
}
}
$pageDir = __DIR__ . '/../../App/pages/' . $path;
$templateFile = $pageDir . '/index.twig';
if (is_file($templateFile)) {
fwrite(STDERR, "Error: $templateFile already exists — not overwriting.\n");
exit(1);
}
if (!is_dir($pageDir) && !mkdir($pageDir, 0775, true) && !is_dir($pageDir)) {
fwrite(STDERR, "Error: could not create directory $pageDir\n");
exit(1);
}
// "my-new-post" -> "My New Post", used to pre-fill the title/heading.
$title = ucwords(str_replace(['-', '_'], ' ', basename($path)));
$template = <<<TWIG
{% extends layout %}
{% block title %}$title{% endblock %}
{% block description %}One or two sentences describing this page.{% endblock %}
{% block robots %}index, follow{% endblock %}
{% block keywords %}{% endblock %}
{% block tags %}{% endblock %}
{% block changefreq %}monthly{% endblock %}
{% block priority %}0.5{% endblock %}
{% block canonical %}{{ request_path|default('/') }}{% endblock %}
{% block og_type %}website{% endblock %}
{% block og_title %}{{ block('title') }}{% endblock %}
{% block og_description %}{{ block('description') }}{% endblock %}
{% block og_url %}{{ block('canonical') }}{% endblock %}
{% block twitter_card %}summary{% endblock %}
{% block twitter_title %}{{ block('title') }}{% endblock %}
{% block twitter_description %}{{ block('description') }}{% endblock %}
{% block content %}
<article>
<h1>$title</h1>
<p>...</p>
</article>
{% endblock %}
TWIG;
file_put_contents($templateFile, $template);
echo "Created $templateFile\n";
echo "Visit it at /$path once you fill in the content.\n";