Skip to content

Use require/require_once for all file inclusions - #136

Merged
cigamit merged 13 commits into
developfrom
refactor/schema-includes-database
Oct 1, 2026
Merged

cigamit merged 13 commits into
developfrom
refactor/schema-includes-database

Conversation

@TheWitness

@TheWitness TheWitness commented Sep 30, 2026 •

Copy link
Copy Markdown
Member

Summary

Part of a fleet-wide consistency pass. This plugin already follows the target layout — its library code is under includes/ and its schema lives in includes/setup/database.php using api_plugin_db_table_create() with a gpsmap_upgrade_database() upgrade path — so this PR combines the include→require sweep with the fleet manifest.json + upgrade-time file-pruning mechanism.

require over include

Every include/include_once across gpsmap.php, gpstemplates.php, setup.php, includes/setup/database.php, and the includes/polling/* files is now require/require_once for fail-fast consistency. Paths are unchanged (no relocations).

gpsmap_security.php intentionally stays in the plugin root — it's referenced as a root-level guard by ~8 files (including via __DIR__ . '/../...' from includes/ subfiles) and is symlinked as a root file by the test harness.

File manifest + upgrade pruning

  • New root manifest.json with tombstones (empty — this plugin has no relocations), expected (the top-level files/directories shipping today, directories with a trailing /), and whitelist (empty — images/ ships stock marker icons, there is no user-writable runtime tree).
  • gpsmap_prune_files() (in setup.php, called from the version-change block of gpsmap_check_upgrade() right after gpsmap_upgrade_database()) removes the dev-only tests/ tree on upgrade, refuses any path that resolves outside the plugin directory (a tampered manifest.json), warns in the Cacti log on any file/directory it cannot remove, leaves whitelist/.git* alone, and logs — without removing — any unaccounted top-level entry.
  • tests/bin/validate-manifest.php (wired into plugin-ci-workflow.yml) fails on drift between expected and the real top-level tree.
  • Added tests/Unit/PruneFilesTest.php. The isolated-process tests/Integration/UpgradeTest.php probe already runs under a temp base_path (gpsmap_test_use_tmp_root()), so its check_upgrade() calls find no manifest and the prune is a no-op there.

Docs

  • .github/copilot-instructions.md: doc example updated to require_once, plus a require-over-include convention bullet and a manifest/prune note. CHANGELOG entry added.

Validation

php -l clean on all changed PHP files; full Pest suite green (147 passed, the 9 risky cases are pre-existing assertion-free Polling/Integration probes) and patch-coverage passes (the setup.php changes are in the established unmeasured allowlist); manifest drift-check passes and the translation template is up to date.

Revision: hardening & fleet cleanup

Since the initial description, this PR also:

  • Prunes phpunit.xml on upgrade (alongside the dev-only tests/ tree) and leaves .md* lint configs in place — the drift check now ignores tests/, phpunit.xml, .git*, .md*, and whitelisted paths.
  • Hardens the prune against tampered manifests: refuses any tombstone whose normalized path contains a ./.. traversal segment or resolves outside the plugin directory (including via a symlinked directory), and protects a directory when a whitelisted entry lives beneath it — each with added unit coverage.
  • Renames the prune helpers to the documented naming convention: gpsmap_prune_files() / gpsmap_rmtree() (the plugin_gpsmap_ prefix is reserved for lifecycle/hook-registration functions).
  • Gives tests/Unit/PruneFilesTest.php the full standard GPL v2 header and aligns the Project Structure block in .github/copilot-instructions.md.

Switches every include/include_once to require/require_once for fail-fast consistency. Schema already lives in includes/setup/database.php via api_plugin_db_table_create()/gpsmap_upgrade_database(), and the library code is already under includes/, so no schema or file-move changes were needed.
Copilot AI balanced review requested due to automatic review settings September 30, 2026 01:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Three remaining test inclusions contradict the stated repository-wide require-only convention.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Replaces production PHP inclusions with fail-fast require variants and documents the convention.

Changes:

  • Converts plugin entry-point, setup, and polling inclusions.
  • Updates repository guidance and changelog.
  • Leaves three test-file include calls inconsistent with the new absolute rule.
File Description
setup.php Requires database setup dependencies.
includes/​setup/​database.php Requires database and plugin setup files.
includes/​polling/​processregion.php Requires the host class.
includes/​polling/​kmlcreation.php Requires KML icon helpers.
includes/​polling/​functions.php Requires polling dependencies.
includes/​polling.php Requires poller implementation files.
gpstemplates.php Requires authentication and security guards.
gpsmap.php Requires authentication, setup, and icon files.
CHANGELOG.md Records the inclusion-policy change.
.github/​copilot-instructions.md Establishes the require-only convention.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread .github/copilot-instructions.md
Copilot added 5 commits September 29, 2026 21:51
The patch-coverage gate requires every changed production PHP file to appear in Clover (or be allowlisted) and 100% of changed statement lines in measured files to be covered. Adds the web/poller entry points that cannot be loaded in the isolated unit process to tests/bin/patch-coverage.php's allowlist, and reverts the one include->require change in includes/polling/functions.php (callRegion() is a live-poller-only helper with no unit coverage).
…er include->require

The patch-coverage gate requires every changed production PHP file to be in Clover or allowlisted, and 100% of changed statement lines in measured files to be covered. Keeps the require/require_once conversion on the web entry points (gpsmap.php, gpstemplates.php), the hook-registration bootstrap (setup.php) and the install/upgrade bootstrap (includes/setup/database.php) - all unmeasurable in the isolated unit process and therefore allowlisted - and reverts the include->require swaps in the live-poller-only internals (includes/polling.php, includes/polling/kmlcreation.php, includes/polling/processregion.php) whose changed lines only execute inside a real poller run.
…cludes-database

# Conflicts:
#	CHANGELOG.md
@TheWitness

Copy link
Copy Markdown
Member Author

This branch now also sets the plugin's INFO compat (minimum supported Cacti version) to 1.2.32, added in the latest commit as part of the fleet-wide compat standardization.

browniebraun
browniebraun previously approved these changes Sep 30, 2026
Copilot added 5 commits September 30, 2026 19:55
phpunit.xml ships in the repo for CI but is a dev-only test artifact, so it
is removed from manifest.json 'expected', pruned from installs on upgrade
(like tests/), and excluded from the manifest drift check.

The retired markdown-lint configs (.mdlrc, .md_style.rb) are deleted, and
.md* is now ignored like .git*: protected from pruning and excluded from
drift if it reappears.
The upgrade-time prune now rejects any tombstone containing '.'/'..' segments
(which could escape the plugin directory or resolve to its root) and treats
ancestors of whitelist entries as protected, so a tombstone on a parent
directory can no longer delete a whitelisted file beneath it.
The trailing '# ...' comments in the Project Structure block drifted further
right down the tree; align them all to a single column.
- Use the full license header (from the plugin's own setup.php) in
  tests/Unit/PruneFilesTest.php instead of the abbreviated copyright banner.
- Document that the manifest drift check and the upgrade-time prune also
  handle phpunit.xml and .md* files, matching the implemented behavior.
The repository naming contract reserves the plugin_<name>_ prefix for plugin
lifecycle / hook-registration functions; all other functions use the plain
<name>_ prefix. Rename the internal upgrade helpers accordingly:

  plugin_<name>_prune_files() -> <name>_prune_files()
  plugin_<name>_rmtree()      -> <name>_rmtree()

The call site, unit tests, and the copilot-instructions.md references are
updated to match. No behavioral change.
@cigamit
cigamit merged commit 07cd018 into develop Oct 1, 2026
5 checks passed
@cigamit
cigamit deleted the refactor/schema-includes-database branch October 1, 2026 06:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants