Context
For the refresh of the math formulae datasets (T439313), MathSearch indexes the math tags of every page when its revision is saved (I015dd8). It finds them by searching the wikitext of the page with Parser::extractTagsAndParams() and a fixed list of tags whose content is shown as text (nowiki, pre, source, syntaxhighlight). Before Math renders a tag, MathSearch gives it its anchor as id, so links from the search reach the formula.
This only approximates the parser. The parser leaves the content of every registered tag to its extension: Cite parses the content of <ref>, other extensions do not, and <source> is only verbatim with SyntaxHighlight. Math tags that a page gets from a template are not in its wikitext at all, so they are neither indexed nor have an anchor.
Other extensions record their data during the parse instead, for example TemplateData in the ParserOutput, and write it to their tables after saving. For MathSearch, a hook on Math's tag hook could record anchor, hash, input and attributes, so the index would contain exactly the formulae the page renders, including those from templates and {{#tag:math|…}}, and the list of verbatim tags could go.
Then only what renders would be indexed. Formulae in modules, in missing templates or on Page: pages without ProofreadPage, as on the math server, would no longer be counted. The imported dumps would need the templates and modules the pages use, and all steps would have to run again.
The datasets are made by a series of commands on the math server (https://github.com/MaRDI4NFDI/wikiFilter/tree/master/wmcloud): a Python script filters the dumps for pages with math tags, importDump.php imports them into one wiki per dump, UpdateMath.php indexes them, and SQL statements export the datasets.
Goal
A single maintenance script in MathSearch should do all of it in PHP: read a dump, keep the pages with math tags together with the templates and modules, import and parse them, and export the inputs, the statistics and the MathML. That replaces the Python filter, which is harder to maintain than PHP next to the code it feeds, and gives the pipeline the single entry point it lacks so far.
The downside is that the script also has to write the filtered dumps. It reads all revisions once and keeps the pages with math tags, and all templates and modules in a temporary store, since a dump is sorted by page id and a template can come after the pages that use it. Which templates a page needs is only known during the parse, through {{ {{{1}}} }}, #switch or #invoke, so keeping all of them is the exact way. It writes <wikiid>-math.xml.bz2 and <wikiid>-templates.xml.bz2.