T104523: Prevent infinite recursion
- The TokenTransformManager already has loop and depth detection mechanisms, they just weren't being used when calling out to the php preprocessor. This was under the faulty assumption that we'd never get into nesting scenarios, since all our expansion is done for us.
- However, template expansion returns unrendered extension tags, which have higher precedence than templates. For extensions that support wikitext, like ref and gallery, we can then get recursive calls for template expansion and ugliness ensues.
The seeds of this are alluded to here, https://github.com/wikimedia/parsoid/blob/master/lib/utils/DOMTraverser.js#L91-L93