feat: write results warn about sections built like a library section (spec §5.5)

SimilarSections compares inserted sections (not global instances) with
at most 200 editable library section entries by structure fingerprint,
read lazily once per request, only while library IDs may be named (P13).
lit/library-insert is not asked.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-09-27 21:16:16 +02:00
co-authored by Claude Opus 5.5
parent f82964f617
commit 4f70886006
6 changed files with 260 additions and 6 deletions
@@ -0,0 +1,95 @@
<?php
declare(strict_types=1);
namespace LIT\MCP\Integrations\Divi\Library;
use LIT\MCP\Integrations\Divi\BlockTree\Block;
use LIT\MCP\Integrations\Divi\BlockTree\Fingerprint;
use LIT\MCP\Integrations\Divi\BlockTree\LibraryContent;
use LIT\MCP\Integrations\Divi\BlockTree\MalformedMarkup;
use LIT\MCP\Integrations\Divi\BlockTree\Parser;
use LIT\MCP\Integrations\Divi\Pages\DiviChange;
defined('ABSPATH') || exit;
/**
* The quality warning "similar section" (spec §5.5, D6, E11): an inserted section built like a section entry of
* the Divi Library (Fingerprint::structure) – compared with library entries only (small; at most MAX_ITEMS, the
* ones the user may edit), read only when a write inserts a section and then once per request (kept in memory; no
* index or meta, F31). Global instances are already linked to the library and never warn. Only while library IDs
* may be named (ruling P13). The warning names the entry's title and ID, never the page's texts. Not final so the
* writer can be tested with a mock.
*/
class SimilarSections
{
public const MAX_ITEMS = 200;
/** @var array<string, array{id: int, title: string}>|null Structure fingerprint => entry. */
private ?array $index = null;
public function __construct(private LibraryAccess $access, private Parser $parser = new Parser())
{
}
/** @return list<string> */
public function warnings(DiviChange $change): array
{
if (! $this->access->revealsIds()) {
return [];
}
$warnings = [];
foreach ($change->entries() as $entry) {
$block = $entry['block'];
if (
$entry['action'] !== 'inserted'
|| ! $block instanceof Block
|| $block->name() !== 'divi/section'
|| $block->isOpaque()
|| $block->isGlobal()
) {
continue;
}
$match = $this->index()[Fingerprint::structure($block)] ?? null;
if ($match !== null) {
$warnings[] = sprintf(
'Ein gleich aufgebauter Abschnitt liegt in der Divi-Bibliothek: „%s“ (%d) – lit-library-insert '
. 'erwägen; weitere Vorkommen mit lit/library-find-similar.',
$match['title'],
$match['id']
);
}
}
return array_values(array_unique($warnings));
}
/** @return array<string, array{id: int, title: string}> */
private function index(): array
{
if ($this->index !== null) {
return $this->index;
}
$this->index = [];
foreach ($this->access->store()->find(['layout_type' => 'section'], self::MAX_ITEMS - 1, 0) as $item) {
if (! current_user_can('edit_post', (int) $item->ID)) {
continue;
}
try {
$roots = LibraryContent::roots(LibraryContent::load($this->parser, (string) $item->post_content));
} catch (MalformedMarkup) {
continue;
}
if (count($roots) === 1 && $roots[0]->name() === 'divi/section') {
$this->index[Fingerprint::structure($roots[0])] ??= [
'id' => (int) $item->ID,
'title' => (string) $item->post_title,
];
}
}
return $this->index;
}
}