Skip to content

Relocate schema into includes/database.php and standardize includes - #37

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

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

Conversation

@TheWitness

@TheWitness TheWitness commented Sep 30, 2026 •

Copy link
Copy Markdown
Member

Summary

Aligns plugin_wmi with the fleet-wide schema/includes convention and adds the fleet manifest.json + upgrade-time file-pruning mechanism.

  • Relocated schema provisioning — moved plugin_wmi_setup_tables() verbatim into includes/database.php, now loaded via require_once from plugin_wmi_install(). The function is unchanged (core host.wmi_account column add, seven table creates, and the two data_input seed rows), so behavior is identical.
  • Renamed functions.php → includes/functions.php and updated every caller (setup.php, poller_wmi.php, wmi_queries.php, wmi_accounts.php), including the previously-missed api_device_new hook (wmi_api_device_new()).
  • include/include_once → require/require_once across production entry points.
  • File manifest + upgrade pruning — new root manifest.json with three arrays: tombstones (paths older versions shipped that have since moved/been removed — here functions.php), expected (top-level files/directories shipping today, directories with a trailing /), and whitelist (user-data paths never to touch — empty for wmi). wmi_prune_files() (in setup.php, called from plugin_wmi_upgrade()) deletes the tombstoned paths and the dev-only tests/ tree, refuses any path that resolves outside the plugin directory (a tampered manifest.json), warns on any file/directory it cannot remove, and logs — without removing — any top-level entry the manifest does not account for. tests/bin/validate-manifest.php (wired into plugin-ci-workflow.yml) fails on drift between expected and the real tree.
  • Tests/tooling — pointed WmiLifecycleTest, PreparedStatementConsistencyTest, and the phpunit <source> at the relocated files; allowlisted the install-only includes/database.php and the web/CLI entry points in the patch-coverage gate; added PruneFilesTest (tombstone/file/dir removal, path-escape refusal, permission-warning, whitelist/.git protection, missing/malformed no-ops), sandboxed base_path in the upgrade test, added a wmi_api_device_new unit test, and regenerated locales/po/cacti.pot.

Scope note

This is the lighter relocation variant: the schema is moved as-is (raw CREATE TABLE IF NOT EXISTS) rather than transcribed to api_plugin_db_table_create() + db_update_table(), given wmi's seven tables (including the composite-PK host_wmi_cache, a MEMORY-engine table, and intermixed data_input seeding).

Validation

Full Pest suite green (44 passed) and the patch-coverage gate passes at 100% of changed measured lines against develop; 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: wmi_prune_files() / wmi_rmtree() (the plugin_wmi_ 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.

- Move plugin_wmi_setup_tables() verbatim into includes/database.php,
  loaded via require_once from plugin_wmi_install().
- Rename functions.php -> includes/functions.php and update its callers.
- Convert include/include_once to require/require_once across production
  entry points.
- Update tests, phpunit source, patch-coverage allowlist, and the .pot
  to track the relocated files.

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

A registered hook still loads the removed functions path, and the relocated schema is omitted from two coverage checks.

Review effort: Balanced
Findings: 2 Medium severity

Open (2)
What changed in this PR

This PR reorganizes the WMI plugin’s schema and shared functions to match the fleet-wide includes/ layout.

Changes:

  • Move schema provisioning and shared functions into includes/, updating the affected loaders.
  • Standardize production includes on require and require_once.
  • Update test paths, coverage configuration, and translation source references.
File Description
wmi_tools.php Requires dependencies.
wmi_script.php Requires dependencies.
wmi_queries.php Loads relocated functions.
wmi_accounts.php Loads relocated functions.
tests/​Unit/​WmiLifecycleTest.php Loads relocated schema.
tests/​Security/​PreparedStatementConsistencyTest.php Updates the functions path.
tests/​bin/​patch-coverage.php Allowlists the schema file.
setup.php Loads relocated schema during installation.
script/​wmi-script.php Requires dependencies.
poller_wmi.php Loads relocated functions.
phpunit.xml Updates the coverage source path.
locales/​po/​cacti.pot Updates translation source references.
includes/​functions.php Relocates shared functions and requires dependencies.
includes/​database.php Houses relocated schema provisioning.

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

Comment thread tests/Security/PreparedStatementConsistencyTest.php
Comment thread wmi_queries.php
browniebraun
browniebraun previously approved these changes Sep 30, 2026
The include->require standardization touched only top-level chdir+require lines in the web pages (auth.php) and CLI/script-server entry points (cli_check.php/global.php), which cannot be loaded into the isolated unit process and therefore cannot be covered. Move those six files out of phpunit.xml <source> and into patch-coverage.php's \, matching the established plugin_thold pattern. setup.php/includes/functions.php/linux_wmi.php remain measured.
Adds the fleet manifest.json (tombstones/expected/whitelist), a
plugin_wmi_prune_files() run from plugin_wmi_upgrade() that removes the
tombstoned functions.php and the dev-only tests/ tree, refuses any path
resolving outside the plugin directory, and warns on files it cannot
remove. Also repoints the api_device_new hook's include to the relocated
includes/functions.php, adds manifest drift validation to CI, and unit
tests for the prune helper and the device_new hook.
TheWitness and others added 3 commits September 30, 2026 17:04
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 Linux_WMI client library moves into includes/; includes/functions.php
loads it at the top (so run_store_wmi_query() keeps working), and the console
pages, poller, script and tests reference the new path. The old path is
tombstoned so existing installs drop the stale top-level copy on upgrade.

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

The pruning guard can delete the plugin root, and one standardized CLI require resolves to a nonexistent path.

Review effort: Balanced
Findings: 2 High severity · 2 Low severity

Open (4)
Resolved since last review (2)

Comment thread setup.php
Comment thread wmi_script.php Outdated
Comment thread includes/database.php
Comment thread setup.php Outdated
Copilot and others added 6 commits September 30, 2026 22:06
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.
Point the structure tree at the relocated files (libraries now under
includes/, data files under docs/, stylesheets under css/) and align the
trailing '# ...' comments to a single column so they no longer drift right.
- 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.
The CLI/script-server bootstrap resolved to the non-existent
plugins/global/cli_check.php. Point it at the parent Cacti include
directory (../../include/cli_check.php) which ships cli_check.php and
loads the global bootstrap. lib/snmp.php remains require_once.
@cigamit
cigamit merged commit 44af83b into develop Oct 1, 2026
3 checks passed
@cigamit
cigamit deleted the refactor/schema-includes-database branch October 1, 2026 06:39
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