club-1 / flarum-ext-cross-references

Add cross reference links when a discussion is mentioned from another one.
GNU Affero General Public License v3.0
6 stars 1 forks source link

Fetch all available discussions in a single DB query for this post in the Renderer #33

Open n-peugnet opened 1 year ago

n-peugnet commented 1 year ago

Currently the Renderer makes 1 DB query for each CROSSREFERENCE* tag in the post. https://github.com/club-1/flarum-ext-cross-references/blob/152437db44471e500930d9f570f9cbed58ea7ae0/src/Formatter/CrossReferencesRenderer.php#L69-L97

It is not very optimised for posts that contains multiple tags of this kind. Even less so when multiple tags all refer to the same discussion. It would be good first aggregate all the discussion ids to be able to fetch all available discussion in a single DB query. Then, at replacement time, if the discussion is not in the fetched collection, when can mark it as unknown.

This is also a good opportunity to refactor the code to parse the XML only once and make the replacements in the resulting DOM instead of parsing it 3 times with Utils::replaceAttributes().