fix: Task 8 review round 1 - quote-aware tokenizer, context check, skipped global nodes (S45)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01At3V6SjwbMe6eMzVGW3Hs7
This commit is contained in:
2026-09-26 16:32:00 +02:00
co-authored by Claude Opus 5.5
parent bb85868192
commit 93d2bde286
6 changed files with 893 additions and 114 deletions
@@ -0,0 +1,201 @@
<?php
declare(strict_types=1);
namespace LIT\MCP\Integrations\Divi\Compose;
/**
* An independent second scan of an HTML fragment (ruling S45): a small state machine after the
* HTML tokenizer rules, written separately from TextReplacer's regex tokenizer so a mistake in
* one is caught by the other. It lists the byte spans that are NOT text: tags (with quoted
* attribute values, where ">" does not end the tag), comments, bogus comments and the bodies of
* raw-text elements (script, style, …). Where it is unsure it errs towards "not text": an
* unterminated construct runs to the end, a script body containing "<!--" (the escaped/double-
* escaped script states) makes the rest of the string markup. Linear time, pure PHP.
*/
final class HtmlContext
{
/** Elements whose body the browser does not parse as text/markup. */
public const RAW = ['script', 'style', 'xmp', 'iframe', 'noembed', 'noframes', 'noscript', 'plaintext'];
/** @return list<array{int, int}> [start, end) byte spans that are markup or raw bodies. */
public static function markupSpans(string $html): array
{
$spans = [];
$length = strlen($html);
$i = 0;
while ($i < $length) {
$lt = strpos($html, '<', $i);
if ($lt === false) {
break;
}
$end = self::markupEnd($html, $lt);
if ($end === $lt) {
$i = $lt + 1;
continue;
}
$end ??= $length;
$name = self::rawName($html, $lt, $end);
if ($name !== null && $end < $length) {
$end = self::rawEnd($html, $name, $end);
}
$spans[] = [$lt, $end];
$i = $end;
}
return $spans;
}
/** Whether [start, end) overlaps no markup span. */
public static function isText(array $spans, int $start, int $end): bool
{
foreach ($spans as [$from, $to]) {
if ($start < $to && $from < $end) {
return false;
}
}
return true;
}
/** Whether offset $position lies between spans (not strictly inside one). */
public static function isBoundary(array $spans, int $position): bool
{
foreach ($spans as [$from, $to]) {
if ($from < $position && $position < $to) {
return false;
}
}
return true;
}
/**
* End (exclusive) of the markup starting with the "<" at $lt; $lt itself when that "<" is
* plain text, null when the markup is not terminated.
*/
private static function markupEnd(string $html, int $lt): ?int
{
$next = $html[$lt + 1] ?? '';
if (substr($html, $lt, 4) === '<!--') {
return self::commentEnd($html, $lt + 4);
}
if ($next === '!' || $next === '?') {
return self::after($html, '>', $lt + 2);
}
if ($next === '/') {
$third = $html[$lt + 2] ?? '';
if (ctype_alpha($third)) {
return self::tagEnd($html, $lt + 3);
}
return $third === '>' ? $lt + 3 : self::after($html, '>', $lt + 2);
}
return ctype_alpha($next) ? self::tagEnd($html, $lt + 2) : $lt;
}
/** After "<!--": "-->" or "--!>", and the abrupt "<!-->" / "<!--->". */
private static function commentEnd(string $html, int $from): ?int
{
if (($html[$from] ?? '') === '>') {
return $from + 1;
}
if (substr($html, $from, 2) === '->') {
return $from + 2;
}
$close = strpos($html, '-->', $from);
$bang = strpos($html, '--!>', $from);
if ($bang !== false && ($close === false || $bang < $close)) {
return $bang + 4;
}
return $close === false ? null : $close + 3;
}
/**
* Inside a tag: ">" ends it unless it lies in an attribute value quoted right after "="
* (whitespace allowed around "=").
*/
private static function tagEnd(string $html, int $from): ?int
{
$length = strlen($html);
for ($p = $from; $p < $length; $p++) {
$char = $html[$p];
if ($char === '>') {
return $p + 1;
}
if ($char !== '=') {
continue;
}
$p++;
while ($p < $length && strpos(" \t\n\r\f", $html[$p]) !== false) {
$p++;
}
$quote = $html[$p] ?? '';
if ($quote === '"' || $quote === "'") {
$close = strpos($html, $quote, $p + 1);
if ($close === false) {
return null;
}
$p = $close;
} else {
$p--;
}
}
return null;
}
private static function after(string $html, string $needle, int $from): ?int
{
$found = strpos($html, $needle, $from);
return $found === false ? null : $found + strlen($needle);
}
/** Name of a raw-text element opened by the start tag [$lt, $end), else null. */
private static function rawName(string $html, int $lt, int $end): ?string
{
if (preg_match('/^<([A-Za-z][A-Za-z0-9]*)/', substr($html, $lt, min(20, $end - $lt)), $match) !== 1) {
return null;
}
$name = strtolower($match[1]);
return in_array($name, self::RAW, true) ? $name : null;
}
/**
* Where the raw body after $from ends: at its "</name" followed by whitespace, "/" or ">".
* plaintext never ends; a script body with "<!--" is treated as running to the end.
*/
private static function rawEnd(string $html, string $name, int $from): int
{
$length = strlen($html);
if ($name === 'plaintext') {
return $length;
}
$p = $from;
while (($found = stripos($html, '</' . $name, $p)) !== false) {
$follow = $html[$found + 2 + strlen($name)] ?? '';
if ($follow !== '' && strpos(" \t\n\r\f/>", $follow) !== false) {
if ($name === 'script' && str_contains(substr($html, $from, $found - $from), '<!--')) {
return $length;
}
return $found;
}
$p = $found + 1;
}
return $length;
}
}
+297 -88
View File
@@ -7,6 +7,7 @@ namespace LIT\MCP\Integrations\Divi\Compose;
use InvalidArgumentException;
use LIT\MCP\Integrations\Divi\BlockTree\AttrMap;
use LIT\MCP\Integrations\Divi\BlockTree\NodeSummary;
use LIT\MCP\Integrations\Divi\BlockTree\Serializer;
/**
* Replaces visible text in the attributes of one block (lit/divi-replace-text, spec §5.7, D10):
@@ -14,25 +15,32 @@ use LIT\MCP\Integrations\Divi\BlockTree\NodeSummary;
* title, label, alt, titleText) – never in URLs, IDs, styles or $variable(…)$ references.
* Pure PHP.
*
* Ruling S8: each string is split into tags, entities and references (kept byte for byte) and
* plain text; only plain text outside <script>/<style> bodies is searched. The inserted text is
* the replacement as the $clean callback returns it for that place (AttrWriter::cleanValue() in
* the ability: sanitized by the field kind, markers rejected – rulings S2/S28). A string with no
* hit is returned as it is and never cleaned (Task 1 forward risk: stored references outside
* the allow list must survive). A changed string must have exactly the tags, entities and
* references of the original plus those of the inserted text – otherwise the replacement glued
* something new together across a boundary (a lone "<" closed into a tag, "&amp" + ";", a
* partial reference closed by ")$") and the whole call is refused (InvalidArgumentException).
* Rulings S8/S45: each string is tokenized (quote-aware tags, comments, entities, references,
* raw-text bodies such as <script>); only text tokens are searched. A string with an
* unterminated tag, quote, comment or reference and a hit is refused. The inserted text is the
* replacement as the $clean callback returns it for that place (AttrWriter::cleanValue() in the
* ability: sanitized by the field kind, markers rejected – rulings S2/S28). A string with no
* effective hit is returned as it is and never cleaned (Task 1 forward risk). A changed string
* must keep every non-text token of the original byte for byte, gain only the inserted text's
* own, add no reference marker, and an independent scan (HtmlContext) must see each replaced
* hit and both ends of each insertion in text context – otherwise the whole call is refused
* (InvalidArgumentException, the page is not changed).
*/
final class TextReplacer
{
/** Ruling S8, verbatim: tags, entities and references are delimiters, the rest is plain text. */
public const SPLIT = '/(<[^>]*>|&[#A-Za-z0-9]+;|\$variable\(.+?\)\$)/';
/**
* Rulings S8/S45: comments, tags (quote-aware: a ">" inside a quoted attribute value does not
* end the tag; an unquoted "<" does, so an unterminated tag cannot make PCRE rescan the
* rest from every "<"), entities and references are tokens, the rest is text. Possessive
* quantifiers keep it linear.
*/
public const SPLIT = '/((?s:<!--.*?-->)|<(?:[^<>"\']++|"[^"]*+"|\'[^\']*+\')*+>|&[#A-Za-z0-9]++;'
. '|\$variable\(.+?\)\$)/';
private const TEXT_KEYS = ['text', 'title', 'label', 'alt', 'titleText'];
/** Elements whose body is code, not text. */
private const RAW_OPEN = '/^<(script|style)\b/i';
/** Raw-text elements (HtmlContext::RAW): their body is code, never text. */
private const RAW_OPEN = '/^<(script|style|xmp|iframe|noembed|noframes|noscript|plaintext)\b/i';
private const CONTEXT = 30;
@@ -64,7 +72,9 @@ final class TextReplacer
$problems = [];
if (! mb_check_encoding($search, 'UTF-8') || mb_strlen($search) < 2) {
$problems['search'] = 'Suchtext: mindestens 2 Zeichen gültiges UTF-8.';
} elseif (VariableRef::contains(VariableRef::withoutControls($search))) {
} elseif (VariableRef::withoutControls($search) !== $search) {
$problems['search'] = 'Suchtext darf keine Steuerzeichen enthalten.';
} elseif (VariableRef::contains($search)) {
$problems['search'] = 'Suchtext darf keinen $variable(…)$-Verweis enthalten; dynamische Inhalte werden '
. 'nie ersetzt.';
}
@@ -79,6 +89,49 @@ final class TextReplacer
return $problems;
}
/**
* The search text as it can be stored in block markup: raw, as serialize_block_attributes()
* escapes it (<, >, &, " and --; the JS builder keeps non-ASCII raw) and with \uXXXX for
* non-ASCII (WordPress' PHP serializer). Case-insensitive adds the lower and upper case
* forms, since the hex digits of \u escapes do not fold.
*
* @return list<string>
*/
public static function forms(string $search, bool $caseSensitive): array
{
$variants = $caseSensitive ? [$search] : [$search, mb_strtolower($search), mb_strtoupper($search)];
$forms = [];
foreach ($variants as $variant) {
$escaped = substr(Serializer::attributes(['s' => $variant]), 6, -2);
array_push($forms, $variant, $escaped, self::unicodeEscaped($escaped));
}
return array_values(array_unique($forms));
}
/** Whether block markup (e.g. of a global module this tool does not edit) contains the search text. */
public function mentions(string $markup): bool
{
foreach (self::forms($this->search, $this->caseSensitive) as $form) {
$found = $this->caseSensitive ? str_contains($markup, $form) : mb_stripos($markup, $form) !== false;
if ($found) {
return true;
}
}
return false;
}
/** Non-ASCII characters as JSON \uXXXX escapes (UTF-16, lower-case hex like json_encode()). */
private static function unicodeEscaped(string $value): string
{
return (string) preg_replace_callback('/[^\x00-\x7F]/u', static function (array $match): string {
$units = unpack('n*', (string) mb_convert_encoding($match[0], 'UTF-16BE', 'UTF-8'));
return implode('', array_map(static fn (int $unit): string => sprintf('\\u%04x', $unit), (array) $units));
}, $value);
}
/**
* @param array<string, mixed> $attrs
* @param callable(string $path, ?string $key, string $replacement): ?string $clean
@@ -145,48 +198,45 @@ final class TextReplacer
int &$count,
array &$samples
): string {
$parts = preg_split(self::SPLIT, $value, -1, PREG_SPLIT_DELIM_CAPTURE);
if (! is_array($parts)) {
return $value;
}
$hits = $this->hits($parts);
if ($hits === null || array_sum($hits) === 0) {
return $value;
}
$place = $path . ($key === null ? '' : '.' . $key);
$tokens = self::tokenize($value);
if ($tokens === null) {
// Unterminated markup, quotes, comments or references: where does the text end? Never guess.
if (preg_match($this->pattern, $value) === 1) {
throw self::refused($place);
}
return $value;
}
$hits = $this->hits($tokens);
if ($hits === null || $hits === []) {
return $value;
}
$replacement = $this->replacement($path, $key, $clean);
if ($replacement === null) {
return $value;
}
$expected = [];
$insert = static fn (): string => $replacement;
$replacementDelimiters = self::delimiters($replacement);
foreach ($parts as $i => $part) {
if ($i % 2 === 1) {
$expected[] = $part;
} elseif (($hits[$i] ?? 0) > 0) {
$parts[$i] = (string) preg_replace_callback($this->pattern, $insert, $part);
for ($n = 0; $n < $hits[$i]; $n++) {
array_push($expected, ...$replacementDelimiters);
}
}
// Only hits that change something (search == replacement, or a cleaned replacement equal
// to the match, change nothing and are no hits).
$hits = array_values(array_filter($hits, static fn (array $hit): bool => $hit[2] !== $replacement));
if ($hits === []) {
return $value;
}
$replaced = implode('', $parts);
// Every marker of the original must be a whole reference (else the string holds a partial
// or nested one Divi might read differently), none may be added, and the structure must
// be the original's plus the inserted text's own.
$markers = substr_count($value, VariableRef::MARKER);
$wholeReferences = $markers === self::referenceCount($parts);
$sameMarkers = substr_count($replaced, VariableRef::MARKER) === $markers;
if (! $wholeReferences || ! $sameMarkers || self::delimiters($replaced) !== $expected) {
$inserted = self::tokenize($replacement);
if ($inserted === null) {
throw self::refused($place);
}
$count += array_sum($hits);
[$replaced, $insertedRanges, $expected] = self::splice($value, $tokens, $hits, $replacement, $inserted);
if (! self::safe($value, $tokens, $hits, $replaced, $insertedRanges, $expected)) {
throw self::refused($place);
}
$count += count($hits);
if (count($samples) < 5) {
$samples[] = [
'path' => $place,
@@ -199,40 +249,222 @@ final class TextReplacer
}
/**
* Hits per plain part (index => count); plain text inside <script>/<style> has none. Null
* when a part cannot be searched (invalid UTF-8): the string stays as it is.
* Hits in text tokens: [byte offset in the value, byte length, matched text]. Null when a
* token cannot be searched (invalid UTF-8): the string stays as it is.
*
* @param list<string> $parts
* @return array<int, int>|null
* @param list<array{string, string, int}> $tokens
* @return list<array{int, int, string}>|null
*/
private function hits(array $parts): ?array
private function hits(array $tokens): ?array
{
$hits = [];
$raw = null;
foreach ($parts as $i => $part) {
if ($i % 2 === 1) {
if ($raw === null && preg_match(self::RAW_OPEN, $part, $match) === 1) {
$raw = strtolower($match[1]);
} elseif ($raw !== null && preg_match('#^</' . $raw . '\b#i', $part) === 1) {
$raw = null;
}
foreach ($tokens as [$type, $text, $offset]) {
if ($type !== 'text') {
continue;
}
if ($raw !== null || $part === '') {
continue;
}
$found = preg_match_all($this->pattern, $part);
if ($found === false) {
if (preg_match_all($this->pattern, $text, $matches, PREG_OFFSET_CAPTURE) === false) {
return null;
}
$hits[$i] = $found;
foreach ($matches[0] as [$match, $at]) {
$hits[] = [$offset + (int) $at, strlen((string) $match), (string) $match];
}
}
return $hits;
}
/**
* The value with every hit replaced, the byte ranges of the inserted text in it, and the
* non-text tokens it must have: the original ones with the inserted text's own at each hit.
*
* @param list<array{string, string, int}> $tokens
* @param list<array{int, int, string}> $hits
* @param list<array{string, string, int}> $inserted
* @return array{string, list<array{int, int}>, list<array{string, string}>}
*/
private static function splice(
string $value,
array $tokens,
array $hits,
string $replacement,
array $inserted
): array {
$own = self::structure($inserted);
$replaced = '';
$ranges = [];
$expected = [];
$position = 0;
$next = 0;
foreach ($tokens as [$type, $text, $offset]) {
if ($type !== 'text') {
$expected[] = [$type, $text];
continue;
}
$end = $offset + strlen($text);
while (isset($hits[$next]) && $hits[$next][0] < $end) {
[$at, $length] = $hits[$next];
$replaced .= substr($value, $position, $at - $position);
$ranges[] = [strlen($replaced), strlen($replaced) + strlen($replacement)];
$replaced .= $replacement;
array_push($expected, ...$own);
$position = $at + $length;
$next++;
}
}
$replaced .= substr($value, $position);
return [$replaced, $ranges, $expected];
}
/**
* Rulings S8/S45: the changed value is safe when (1) every reference marker of the original
* is a whole reference and none is added, (2) its full non-text tokens (tags byte for byte,
* comments, entities, references, raw bodies) are exactly the expected ones, and (3) an
* independent scan (HtmlContext) puts every replaced hit of the original and both ends of
* every inserted text in text context.
*
* @param list<array{string, string, int}> $tokens
* @param list<array{int, int, string}> $hits
* @param list<array{int, int}> $insertedRanges
* @param list<array{string, string}> $expected
*/
private static function safe(
string $value,
array $tokens,
array $hits,
string $replaced,
array $insertedRanges,
array $expected
): bool {
$markers = substr_count($value, VariableRef::MARKER);
$references = count(array_filter(
$tokens,
static fn (array $token): bool => $token[0] === 'token' && str_starts_with($token[1], VariableRef::MARKER)
));
$after = self::tokenize($replaced);
$sameMarkers = $markers === $references && substr_count($replaced, VariableRef::MARKER) === $markers;
if (! $sameMarkers || $after === null || self::structure($after) !== $expected) {
return false;
}
$before = HtmlContext::markupSpans($value);
foreach ($hits as [$at, $length]) {
if (! HtmlContext::isText($before, $at, $at + $length)) {
return false;
}
}
$spans = HtmlContext::markupSpans($replaced);
foreach ($insertedRanges as [$start, $end]) {
if (! HtmlContext::isBoundary($spans, $start) || ! HtmlContext::isBoundary($spans, $end)) {
return false;
}
}
return true;
}
/**
* @param list<array{string, string, int}> $tokens
* @return list<array{string, string}> The non-text tokens (type and bytes), in order.
*/
private static function structure(array $tokens): array
{
$structure = [];
foreach ($tokens as [$type, $text]) {
if ($type !== 'text') {
$structure[] = [$type, $text];
}
}
return $structure;
}
/**
* Ruling S45 tokenizer: [type, bytes, offset] with type "token" (tag – quote-aware, ">" in a
* quoted value does not end it –, comment, entity, reference), "raw" (body of script, style
* and the other raw-text elements) or "text". Null when the value has an unterminated tag,
* quote, comment or reference – then nobody can say where its text is.
*
* @return list<array{string, string, int}>|null
*/
private static function tokenize(string $value): ?array
{
if (self::unterminated($value)) {
return null;
}
$parts = preg_split(self::SPLIT, $value, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE);
if (! is_array($parts)) {
return null;
}
$tokens = [];
$raw = null;
foreach ($parts as $i => [$text, $offset]) {
$text = (string) $text;
if ($i % 2 === 1) {
if ($raw === null && preg_match(self::RAW_OPEN, $text, $match) === 1) {
$raw = strtolower($match[1]);
} elseif ($raw !== null && self::closes($raw, $text)) {
$raw = null;
}
$tokens[] = ['token', $text, (int) $offset];
continue;
}
if ($text === '') {
continue;
}
// A "<" that starts markup but matched no token: an unterminated tag or quote.
if ($raw === null && preg_match('#<[A-Za-z/!?]#', $text) === 1) {
return null;
}
$tokens[] = [$raw === null ? 'text' : 'raw', $text, (int) $offset];
}
return $tokens;
}
/** Whether token $text closes raw-text element $raw ("</script-x>" does not; plaintext never ends). */
private static function closes(string $raw, string $text): bool
{
return $raw !== 'plaintext' && preg_match('#^</' . $raw . '[\s/>]#i', $text) === 1;
}
/**
* Cheap checks that keep the tokenizer linear: a comment opened after the last "-->", or a
* reference marker after the last ")$" of its line, can never be closed – PCRE would retry
* it from every later opener.
*/
private static function unterminated(string $value): bool
{
$open = strrpos($value, '<!--');
if ($open !== false) {
$close = strrpos($value, '-->');
if ($close === false || $close < $open + 2) {
return true;
}
}
if (! str_contains($value, VariableRef::MARKER)) {
return false;
}
foreach (explode("\n", $value) as $line) {
$marker = strrpos($line, VariableRef::MARKER);
$end = $marker === false ? false : strrpos($line, ')$');
if ($marker !== false && ($end === false || $end < $marker)) {
return true;
}
}
return false;
}
/** @param callable(string, ?string, string): ?string $clean */
private function replacement(string $path, ?string $key, callable $clean): ?string
{
@@ -248,29 +480,6 @@ final class TextReplacer
return $this->cleaned[$place];
}
/** @return list<string> Tags, entities and references of $value, in order. */
private static function delimiters(string $value): array
{
$parts = preg_split(self::SPLIT, $value, -1, PREG_SPLIT_DELIM_CAPTURE);
return is_array($parts)
? array_values(array_filter($parts, static fn (int $i): bool => $i % 2 === 1, ARRAY_FILTER_USE_KEY))
: [$value];
}
/** @param list<string> $parts */
private static function referenceCount(array $parts): int
{
$count = 0;
foreach ($parts as $i => $part) {
if ($i % 2 === 1 && str_starts_with($part, VariableRef::MARKER)) {
$count++;
}
}
return $count;
}
/**
* $value as a sanitizer could leave it: controls gone, then tags (nested like strip_tags
* counts them, an unclosed one up to the end) and %XX octets removed until nothing changes.
@@ -39,6 +39,8 @@ final class DiviReplaceTextAbility implements Ability
private const SAMPLES = 5;
public const GLOBAL_REASON = 'globales Modul – in der Bibliothek bearbeiten';
public function __construct(
private ContentSchema $schema,
private DiviPageLoader $loader,
@@ -63,7 +65,9 @@ final class DiviReplaceTextAbility implements Ability
. 'dynamischen Inhalte) auf den Divi-Seiten eines Inhaltstyps. Standard ist dry_run: true – liefert die '
. 'Treffer je Seite mit tree_rev. Zum Ersetzen dry_run: false, targets = [{post_id, tree_rev}] aus dem '
. 'Probelauf und confirm: true nach Rückfrage beim Nutzer. Jede Seite wird einzeln gespeichert und erhält '
. 'eine eigene Revision; results meldet jede Seite.';
. 'eine eigene Revision; results meldet jede Seite. In reinen Textfeldern (Titel, Button-Texte) werden '
. 'Leerzeichen am Anfang und Ende der Ersetzung entfernt. Globale Module werden nicht geändert, sondern '
. 'in skipped_nodes genannt (in der Divi-Bibliothek bearbeiten).';
}
public function inputSchema(): array
@@ -104,6 +108,7 @@ final class DiviReplaceTextAbility implements Ability
'dry_run' => ['type' => 'boolean'],
'matches' => ['type' => 'array', 'items' => ['type' => 'object']],
'skipped' => ['type' => 'array', 'items' => ['type' => 'object']],
'skipped_nodes' => ['type' => 'array', 'items' => ['type' => 'object']],
'next_cursor' => ['type' => ['string', 'null']],
'results' => ['type' => 'array', 'items' => ['type' => 'object']],
], ['dry_run']);
@@ -169,6 +174,7 @@ final class DiviReplaceTextAbility implements Ability
$page = Cursor::page($candidates, $limit, $offset);
$matches = [];
$skipped = [];
$skippedNodes = [];
$total = 0;
foreach ($page['items'] as $postId) {
$postId = (int) $postId;
@@ -190,7 +196,10 @@ final class DiviReplaceTextAbility implements Ability
continue;
}
if ($found['count'] > 0) {
foreach ($found['skipped_nodes'] as $node) {
$skippedNodes[] = ['post_id' => $postId] + $node;
}
if ($found['count'] > 0 || $found['skipped_nodes'] !== []) {
$total += $found['count'];
$matches[] = [
'post_id' => $postId,
@@ -199,19 +208,25 @@ final class DiviReplaceTextAbility implements Ability
'tree_rev' => $divi->treeRev(),
'count' => $found['count'],
'samples' => $found['samples'],
'skipped_nodes' => count($found['skipped_nodes']),
];
}
}
return ToolResult::ok(sprintf(
'Probelauf: %d Treffer auf %d Seiten – nichts geändert.%s',
'Probelauf: %d Treffer auf %d Seiten – nichts geändert.%s%s',
$total,
count($matches),
$skippedNodes === [] ? '' : sprintf(
' %d Knoten (globale Module) werden nicht geändert, siehe skipped_nodes.',
count($skippedNodes)
),
$page['next_cursor'] === null ? '' : ' Weitere Seiten mit next_cursor prüfen.'
))
->with('dry_run', true)
->with('matches', $matches)
->with('skipped', $skipped)
->with('skipped_nodes', $skippedNodes)
->with('next_cursor', $page['next_cursor'])
->toArray();
}
@@ -263,7 +278,12 @@ final class DiviReplaceTextAbility implements Ability
count($results),
array_sum(array_column($replaced, 'count'))
));
$skippedNodes = [];
foreach ($results as $entry) {
foreach ($entry['skipped_nodes'] ?? [] as $node) {
$skippedNodes[] = ['post_id' => $entry['post_id']] + $node;
$tool->warning(sprintf('Eintrag %d, Knoten %s: %s', $entry['post_id'], $node['id'], $node['reason']));
}
if (! $entry['ok']) {
$tool->warning(sprintf('Eintrag %d: %s', $entry['post_id'], $entry['error']));
continue;
@@ -277,7 +297,10 @@ final class DiviReplaceTextAbility implements Ability
}
}
return $tool->with('dry_run', false)->with('results', $results)->toArray();
return $tool->with('dry_run', false)
->with('results', $results)
->with('skipped_nodes', $skippedNodes)
->toArray();
}
/**
@@ -330,6 +353,7 @@ final class DiviReplaceTextAbility implements Ability
return $base + ['ok' => false, 'error' => $e->getMessage() . ' Diese Seite wurde nicht geändert.'];
}
$base['skipped_nodes'] = $found['skipped_nodes'];
if ($found['count'] === 0) {
return $base + ['ok' => true, 'count' => 0, 'tree_rev' => $page->treeRev()];
}
@@ -357,7 +381,7 @@ final class DiviReplaceTextAbility implements Ability
* the entry's AttrWriter (field kind; markers rejected); URL and code fields are skipped.
*
* @return array{count: int, samples: list<array<string, string>>,
* blocks: list<array{Block, array<string, mixed>}>}
* blocks: list<array{Block, array<string, mixed>}>, skipped_nodes: list<array{id: string, reason: string}>}
* @throws InvalidArgumentException When a place would be changed unsafely – the page fails.
*/
private function replace(DiviPage $page, TextReplacer $replacer): array
@@ -366,9 +390,16 @@ final class DiviReplaceTextAbility implements Ability
$count = 0;
$samples = [];
$blocks = [];
$skipped = [];
foreach ($page->index->entries() as $entry) {
$block = $entry['block'];
if (! $block->isDivi() || $block->isOpaque()) {
if ($block->isDivi() && $block->isOpaque()) {
if ($replacer->mentions(Serializer::block($block))) {
$skipped[] = ['id' => (string) $entry['id'], 'reason' => self::opaqueReason($block)];
}
continue;
}
if (! $block->isDivi()) {
continue;
}
@@ -397,30 +428,22 @@ final class DiviReplaceTextAbility implements Ability
}
}
return ['count' => $count, 'samples' => $samples, 'blocks' => $blocks];
return ['count' => $count, 'samples' => $samples, 'blocks' => $blocks, 'skipped_nodes' => $skipped];
}
/**
* IDs of Divi entries whose content contains the search text as stored: raw, as
* serialize_block_attributes() escapes it (<, >, &, " and --), and additionally with \uXXXX
* for non-ASCII (WordPress' PHP serializer). Case-insensitive adds the lower and upper case
* forms (LIKE is case-insensitive for letters, not for the hex digits of \u escapes). The
* exact match happens in the tree; this is only a prefilter.
* IDs of Divi entries whose content contains the search text in one of its stored forms
* (TextReplacer::forms()). The exact match happens in the tree; this is only a prefilter.
*
* @return list<int>
*/
private function candidates(string $postType, string $search, bool $caseSensitive, int $limit, int $offset): array
{
$db = $this->db();
$variants = $caseSensitive ? [$search] : [$search, mb_strtolower($search), mb_strtoupper($search)];
$forms = [];
foreach ($variants as $variant) {
$escaped = substr(Serializer::attributes(['s' => $variant]), 6, -2);
array_push($forms, $variant, $escaped, self::unicodeEscaped($escaped));
}
$forms = TextReplacer::forms($search, $caseSensitive);
$likes = array_map(
static fn (string $form): string => '%' . $db->esc_like($form) . '%',
array_values(array_unique($forms))
$forms
);
$sql = "SELECT ID FROM {$db->posts} WHERE post_type = %s"
@@ -443,14 +466,12 @@ final class DiviReplaceTextAbility implements Ability
return array_map('intval', is_array($ids) ? $ids : []);
}
/** Non-ASCII characters as JSON \uXXXX escapes (UTF-16, lower-case hex like json_encode()). */
private static function unicodeEscaped(string $value): string
/** Why an opaque node is not changed (global modules are edited in the Divi library, Etappe 3). */
private static function opaqueReason(Block $block): string
{
return (string) preg_replace_callback('/[^\x00-\x7F]/u', static function (array $match): string {
$units = unpack('n*', (string) mb_convert_encoding($match[0], 'UTF-16BE', 'UTF-8'));
return implode('', array_map(static fn (int $unit): string => sprintf('\\u%04x', $unit), (array) $units));
}, $value);
return $block->isGlobal()
? self::GLOBAL_REASON
: 'Knoten nicht lesbar (Attribute kein JSON-Objekt) – im Divi-Builder bearbeiten';
}
/** The client message of an error (its JSON "message"), without the JSON around it. */