Files
Umbra-Bookshelf/umbra-bookshelf/tests/verify_structure.py
bdm 0bd78e587a Resolve the known issues; switch licence to GPL
Closes the three problems that blocked the consolidation and records the rest
as tasks.

Licence: GPLv2 or later (WordPress.org compatible)

  LICENSE is now the full GPLv2 text and NOTICE is gone (it was Apache
  specific). All 101 SPDX headers read GPL-2.0-or-later and every shipped
  header says "GPLv2 or later".

  This also fixes an inconsistency that predated the consolidation: the theme's
  style.css declared GPLv2 while its PHP files carried Apache headers.

  Old/ and archive/pre-umbra-releases/ keep their original Apache headers on
  purpose -- they are frozen snapshots, not shipped code.

Schema: the catalogue schema is canonical for bd_books and bd_series

  Adds Umbra_Schema_Migration. The catalogue and library modules previously
  both created those two tables with different columns, so whichever plugin
  activated first won and the other read and wrote against the wrong shape.

  The migration detects a library-shaped table, RENAMES it aside (never drops
  it), creates the canonical table and copies rows across:

    - book IDs preserved, keeping bd_book_meta, bd_reviews, bd_ratings and
      bd_review_shortlinks valid
    - title/slug/description resolved from the linked post
    - the library's single isbn/asin land on the ebook columns; amazon_asin is
      also set so existing product links keep working
    - series migrated first so books.series_id still resolves
    - post linkage rewritten as _bd_bookshelf_book_id (preserved meta key)
    - publish_date kept as _bd_bookshelf_publish_date (no catalogue column)
    - bd_ratings goodreads/amazon folded into rating_goodreads/rating_amazon
    - rows whose post is gone are skipped with a log line, not turned into
      unusable books
    - duplicate slugs de-duplicated, since catalogue slug is UNIQUE

  It runs before dbDelta, and on admin_init as well as activation, because
  plugin updates do not re-run activation hooks. Ordering matters: dbDelta adds
  columns but never drops them, so pointed at a library-shaped table it would
  graft on catalogue columns and leave a table satisfying neither schema.

  Umbra_Bookshelf_Helper_Install no longer creates bd_books or bd_series.

Scraping: Goodreads only, one implementation

  There were three Goodreads implementations and two Amazon ones; both library
  "scrapers" were empty stubs attached to cron events that fired on schedule to
  do nothing, and both real scraper classes were never called at all.

  Umbra_Amazon_Scraper is deleted and the duplicate HTML parsing is gone from
  Umbra_Rating_Sync, which now delegates to Umbra_Goodreads_Scraper. The two
  library stubs and their cron registrations are removed. One schedule
  survives: bd_bookshelf_sync_ratings, every 12 hours.

  The rating_amazon column is retained (it holds data and is populated during
  migration) and Amazon retail links are unaffected.

  A new rating fetch failure keeps the last known value instead of writing zero.
  The admin ratings notice no longer warns about Amazon scraping.

Umbra_SEO is now instantiated

  It registers its hooks from a constructor and the bootstrap never constructed
  it, so the class was dead code and no SEO output was produced. Its meta, Open
  Graph, Twitter card and sitemap hooks now attach. Completing SEO for generated
  book detail pages is docs/TASKS.md #2.

Assets that 404'd

  - assets/images/default-cover.png now holds a real 512x815 cover
    (WorkingTitleCover_512x815.png). The helper returned this path but the
    repository contained no images at all.
  - email/assets/js/admin.js is committed as an empty placeholder. It was
    enqueued on every email admin page but had never been committed.

Tests

  - tests/schema_migration_test.php: 42 checks against a real database, driven
    through the sqlite3 CLI on a throwaway file. Covers shape detection, row
    mapping, ID preservation, orphan handling, rating folding, idempotency and
    slug collisions.
  - tests/run_all.sh runs every suite.
  - The declared version is now checked against the plugin header rather than a
    literal, so a version bump cannot silently break the suite.

Docs

  - KNOWN-ISSUES.md rewritten: three resolved, three remaining.
  - TASKS.md added: email admin behaviour, book detail page SEO, batching the
    rating sync, reclaiming legacy tables, reconciling the two book models,
    retiring the Old/ archive.
  - SPEC and ARCHITECTURE updated for schema ownership, the migration ordering
    constraint, and the single scraper.

Version 1.1.0.
2026-10-22 09:47:00 -07:00

185 lines
7.4 KiB
Python

#!/usr/bin/env python3
"""Static verification for the merged umbra-bookshelf plugin.
Checks:
1. PHP syntax of every file
2. No class declared twice (fatal error in PHP)
3. Every class referenced by new X / X:: actually exists
4. Every require_once path resolves on disk
5. Every wp_enqueue_* asset path resolves on disk (no 404s)
6. No leftover pre-rename identifiers
7. Preserved storage/API contracts still present
"""
import io
import os
import re
import subprocess
import sys
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
failures = []
notes = []
def rel(p):
return os.path.relpath(p, ROOT)
def walk(exts):
for dirpath, _, files in os.walk(ROOT):
for f in files:
if f.endswith(exts):
yield os.path.join(dirpath, f)
# ---------------------------------------------------------------- 1. php -l
php = [f for f in walk('.php')]
for f in php:
r = subprocess.run(['php', '-l', f], capture_output=True, text=True)
if 'No syntax errors' not in r.stdout:
failures.append('PHP SYNTAX: %s\n%s' % (rel(f), r.stdout + r.stderr))
print('1. php -l: %d files checked' % len(php))
# ------------------------------------------------- 2. duplicate class decls
# Shipped source only: tests/ defines intentional stubs of real classes.
source_php = [f for f in php if os.sep + 'tests' + os.sep not in f + os.sep]
decls = {}
for f in source_php:
src = io.open(f, encoding='utf-8', errors='replace').read()
for m in re.finditer(r'^\s*(?:final\s+|abstract\s+)?class\s+([A-Za-z_][A-Za-z0-9_]*)',
src, re.M):
decls.setdefault(m.group(1), []).append(rel(f))
dupes = {c: fs for c, fs in decls.items() if len(fs) > 1}
for c, fs in sorted(dupes.items()):
failures.append('DUPLICATE CLASS: %s declared in %s' % (c, ', '.join(fs)))
print('2. class declarations: %d unique, %d duplicated'
% (len(decls), len(dupes)))
# --------------------------------------------- 3. referenced classes exist
refs = set()
for f in source_php:
src = io.open(f, encoding='utf-8', errors='replace').read()
for m in re.finditer(r'\bnew\s+([A-Z][A-Za-z0-9_]*)\s*\(', src):
refs.add(m.group(1))
for m in re.finditer(r"\b([A-Z][A-Za-z0-9_]*)::", src):
refs.add(m.group(1))
# names defined by this plugin, or provided by WordPress
wp_provided = {'BD_BookShelfHelper', # created via class_alias()
'WP_Error', 'WP_Query', 'WP_Post', 'WP_REST_Response',
'WP_REST_Request', 'WP_REST_Server', 'WP_Term', 'wpdb',
'ReflectionClass', 'DateTime', 'Exception', 'ArrayObject'}
known_external = {'GeminiClient',
# browser JS globals, matched by the `new X(` heuristic
# when scanning inline <script> blocks inside PHP files
'FormData', 'URL', 'Image', 'Audio',
# SPL / core exceptions
'RuntimeException', 'InvalidArgumentException', 'LogicException'}
missing = sorted(r for r in refs
if r not in decls
and r not in wp_provided
and r not in known_external)
for r in missing:
failures.append('UNDEFINED CLASS REFERENCED: %s' % r)
print('3. class references: %d referenced, %d undefined' % (len(refs), len(missing)))
# --------------------------------------------------- 4. require_once paths
for f in php:
src = io.open(f, encoding='utf-8', errors='replace').read()
for m in re.finditer(r"require(?:_once)?\s+([^;]+);", src):
expr = m.group(1)
pm = re.search(r"UMBRA_BOOKSHELF_DIR\s*\.\s*'([^']+)'", expr)
if pm:
target = os.path.join(ROOT, pm.group(1).lstrip('/'))
if not os.path.isfile(target):
failures.append('MISSING REQUIRE: %s -> %s' % (rel(f), pm.group(1)))
elif "__DIR__" in expr:
pm = re.search(r"__DIR__\s*\.\s*'([^']+)'", expr)
if pm:
target = os.path.join(os.path.dirname(f), pm.group(1).lstrip('/'))
if not os.path.isfile(target):
failures.append('MISSING REQUIRE: %s -> %s'
% (rel(f), pm.group(1)))
print('4. require_once targets verified')
# ------------------------------------------------------- 5. asset 404 check
assets = 0
for f in list(walk('.php')):
src = io.open(f, encoding='utf-8', errors='replace').read()
for m in re.finditer(
r"UMBRA_BOOKSHELF_URL\s*\.\s*'([^']+\.(?:css|js|png|jpg|svg))'",
src):
target = os.path.join(ROOT, m.group(1))
assets += 1
if not os.path.isfile(target):
failures.append('ASSET 404: %s -> %s' % (rel(f), m.group(1)))
print('5. enqueued assets: %d references, all resolved' % assets)
# -------------------------------------------- 6. leftover pre-rename tokens
stale_pat = re.compile(r'\b(?:BD_[A-Z][A-Za-z0-9_]*|BDM_[A-Za-z0-9_]*)\b')
stale = []
# Scan shipped plugin source only. Excluded on purpose:
# spec/archive/ -- historical pre-merge specs, must keep the old names
# tests/ -- asserts that the old names are GONE, so it mentions them
SCAN_DIRS = ('includes', 'admin', 'public', 'library', 'email')
scan_files = []
for d in SCAN_DIRS:
for dirpath, _, files in os.walk(os.path.join(ROOT, d)):
for f in files:
if f.endswith(('.php', '.js', '.css')):
scan_files.append(os.path.join(dirpath, f))
for f in ['umbra-bookshelf.php', 'uninstall.php']:
scan_files.append(os.path.join(ROOT, f))
scan_files = [f for f in scan_files if os.path.isfile(f)]
for f in scan_files:
src = io.open(f, encoding='utf-8', errors='replace').read()
for i, line in enumerate(src.splitlines(), 1):
for m in stale_pat.finditer(line):
# BD_BookShelfHelper is intentionally kept as the legacy theme alias
if m.group(0) == 'BD_BookShelfHelper':
notes.append('%s:%d keeps legacy alias %s'
% (rel(f), i, m.group(0)))
continue
stale.append('%s:%d %s' % (rel(f), i, m.group(0)))
for s in sorted(set(stale)):
failures.append('STALE IDENTIFIER: %s' % s)
print('6. stale identifiers: %d (excluding documented legacy alias)'
% len(set(stale)))
# ------------------------------------- 7. preserved contracts still present
contracts = [
('bd_books', 'table name'),
('bd_series', 'table name'),
('bd_bookshelf_db_version', 'option key'),
('bd_bookshelf_activation_time', 'option key'),
('_bd_bookshelf_book_id', 'post meta key'),
('bd_bookshelf_import_posts', 'nonce action'),
('bd_bookshelf_import_csv', 'nonce/ajax action'),
('bd_bookshelf_sync_ratings', 'cron hook'),
('bd_bookshelf_goodreads_refresh', 'cron hook'),
('bd_bookshelf_amazon_scrape', 'cron hook'),
('bdm_update_links', 'ajax action'),
('bdm_autosave', 'ajax action'),
('bdm_ajax_signup', 'ajax action'),
('bd_bookshelf_consent', 'cookie name'),
]
blob = ''
for f in walk(('.php', '.js')):
blob += io.open(f, encoding='utf-8', errors='replace').read()
for tok, kind in contracts:
if tok not in blob:
failures.append('LOST CONTRACT (%s): %s' % (kind, tok))
print('7. preserved contracts: %d checked' % len(contracts))
# ------------------------------------------------------------------- report
print()
for n in sorted(set(notes)):
print('note: %s' % n)
print()
if failures:
print('FAIL (%d)' % len(failures))
for f in failures:
print(' - %s' % f)
sys.exit(1)
print('PASS - all static checks clean')