Page MenuHomePhabricator

"Invalid JSON" when trying to publish an Implementation by code (JS)
Closed, ResolvedPublicBUG REPORT

Description

Steps to replicate the issue (include links if applicable):

What happens?:

Red error box appears, simply saying "Invalid JSON".

What should have happened instead?:

The Implementation object is created.
Or if there's some restriction that my code is violating, a clearer error message.

Software version (on Special:Version page; skip for WMF-hosted wikis like Wikipedia):
live wiki

Other information (browser name/version, screenshots, etc.):
FF 146, Plasma 6, NixOS

I'd ran into this once before, also with a JS snippet full of RegExes.

Event Timeline

The LOC causing this:

try {
			$content = ZObjectContentHandler::makeContent( $data, $title );
		} catch ( ZErrorException $e ) {
			return ZObjectPage::newFatal( $e->getZError() );
		} catch ( MWContentSerializationException $mwe ) {
// here!

The code gets embedded in json and becomes:

{"Z1K1":"Z2","Z2K1":{"Z1K1":"Z6","Z6K1":"Z30994"},"Z2K2":{"Z1K1":"Z14","Z14K1":"Z30837","Z14K3":{"Z1K1":"Z16","Z16K1":"Z600","Z16K2":"function Z30837( Z30837K1 ) {\n\tlet s = Z30837K1.normalize(\"NFC\");\n//\ts = s.replace(/(?<![aeiouyæøœɐɑɒɔɘəɛɜɞɤɨɪɯɵɶʉʊʌʏ])\.(?![aeiouyæøœɐɑɒɔɘəɛɜɞɤɨɪɯɵɶʉʊʌʏ])/g, \"\"); // remove redundant syllable breaks (leaving only those adjacent to vowels)\n\ts = s.replace(/[aeiouyæøœɐɑɒɔɘəɛɜɞɤɨɪɯɵɶʉʊʌʏ][\u0311\u032F]/g, \"C\"); // replace non-syllabic vowels with non-syllable marker //TODO does this always work in NFC?\n\ts = s.replace(/.[\u030D\u0329]/g, \".V.\"); // replace syllabic consonants with syllable marker //TODO same question\n\ts = s.normalize(\"NFKD\").replace(/\p{Mn}+/gu, \"\"); // strip all diacritics\n\ts = s.replace(/[Vaeiouyæøœɐɑɒɔɘəɛɜɞɤɨɪɯɵɶʉʊʌʏ]+/g, \"V\"); // coalesce consecutive vowels\n//\ts = s.replace(/[^V]+/g, \".\"); // coalesce consecutive non-vowels\n//\ts = `.${s}.`;\n\treturn s.split('V').length - 1;\n}"}},"Z2K3":{"Z1K1":"Z12","Z12K1":["Z11"]},"Z2K4":{"Z1K1":"Z32","Z32K1":["Z31"]},"Z2K5":{"Z1K1":"Z12","Z12K1":["Z11"]}}

where this:

  • .replace(/\p{Mn}+/gu, \"\"); should be .replace(/\\p{Mn}+/gu, \"\");
  • s.replace(/(?<![aeiouyæøœɐɑɒɔɘəɛɜɞɤɨɪɯɵɶʉʊʌʏ])\.(?! should be s.replace(/(?<![aeiouyæøœɐɑɒɔɘəɛɜɞɤɨɪɯɵɶʉʊʌʏ])\\.(?!

The strange thing is that the json send in the payload to the API = actually valid. So it must be that the replaceNullReferencePlaceholder that does regex stuff with backslashes replaces things.

I checked and concluded:

The Problem
The function replaceNullReferencePlaceholder was using preg_replace() with a replacement string that contained backslashes. PHP's preg_replace() interprets escape sequences in the replacement string, so backslashes like \\p{Mn} were being processed as \p{Mn}, breaking the JSON.

The Solution
Changed the code to use preg_replace_callback() instead of preg_replace() for the final replacement. This way:

  1. The callback receives the matched content
  2. We only modify the code content part (captured in $matches[2])
  3. We reconstruct the JSON structure manually, avoiding PHP's escape sequence interpretation
  4. Backslashes in the code content are preserved as-is

The fix:

  • Uses preg_replace_callback() to avoid escape sequence interpretation
  • Captures whitespace separately to preserve JSON formatting
  • Only replaces Z0 references within the code content, not in the JSON structure
  • Reconstructs the JSON manually without going through preg_replace() replacement string processing

This should preserve backslashes in regex patterns like \\p{Mn} and \\. in the JavaScript code.

Change #1224935 had a related patch set uploaded (by Daphne Smit; author: Daphne Smit):

[mediawiki/extensions/WikiLambda@master] [T413466] Fix backslash escaping in replaceNullReferencePlaceholder

https://gerrit.wikimedia.org/r/1224935

DSmit-WMF changed the task status from Open to In Progress.Jan 9 2026, 12:41 PM
DSmit-WMF claimed this task.

Change #1224935 merged by jenkins-bot:

[mediawiki/extensions/WikiLambda@master] Fix backslash escaping in replaceNullReferencePlaceholder

https://gerrit.wikimedia.org/r/1224935

Jdforrester-WMF subscribed.

@DSmit-WMF: This has now been deployed to production; is it good to sign off?

Thank you! Sorry for the bug.