Page MenuHomePhabricator

Clean up the ParserFetchTemplateData hook
Open, Needs TriagePublic

Description

The ParserFetchTemplateData hook could use some cleanup.

  1. It currently takes an array of titles and processes them in a batch, but the actual implementation in TemplateData iterates through the titles one by one, and the Parsoid code which invokes ParserFetchTemplateData only ever passes in a single title. We should simplify the API and just take a single title and return data for that single title.
  2. It is somewhat unusual that templates have a Title corresponding to them, eg {{Foo}} corresponds to {{Template:Foo}}. This isn't the case for extensions (T204283) or parser functions, though, and we'd like to eventually support TemplateData for them as well. Further, scribunto modules do have a title -- ie, {{#invoke:Foo}} corresponds to Module:Foo -- but the correspondence isn't exact. I think instead of taking a title, the signature should be something like string $type, string $name with the Template:Foo case corresponding to 'template', 'Foo'. This would be extensible to the other types of invocations we'd like to eventually support. (If this takes long enough, the string $type could become an enumeration, but we can't do that until our minimum PHP version is 8.1.) (#invoke:Foo is perhaps a special case, we maybe need to pass 'Foo' as well as 'invoke' for the "parserfunction" $type.)
  3. Returning an object without a type is quite odd, and we compound it by returning several not-quite-an-object values for missing, no templatedata, or bad data. These should probably be "normal" null returns, with maybe a &$error or something if we need to distiguish the type (it doesn't seem like we use the error type at all). Ideally we'd return a properly typed object, maybe one defined in Parsoid. If we wanted to maintain loose coupling, however, we could simply return an associative array (and document the property names), since the current Parsoid code promptly turns the object into an associative array anyway.

I propose to introduce a new hook named ParserGetTemplateData or maybe ParserGetInvocationData (to accommodate extensions and parser functions?), and deprecate the existing hook and move to using the new hook.

Event Timeline

See also:

@Krinkle wrote at T55413 in 2013:

A few random points:

  • 'titles' parameter in the API module
  • The PHP parser prefers native magic word over templates (creating Template:PAGENAME and using {{PAGENAME}}, will not use that template).
  • Should there be an implied property 'type':
    • type:template -> {{Foo}}, {{:Foo}}, {{Template:Foo}}, {{Project:Foo}}
    • type:parserfunction -> {{PAGENAME}}, {{urlencode:123}}, {{#special:Watchlist}}