Module talk:Hatnote
Redlink detection in _formatLink
On a few hatnote templates, MB has requested that I add redlink detection, because it's a reasonably common error that could be tracked and fixed. I've sandboxed a basic implementation of that functionality, adding Category:Articles with hatnote templates targeting a nonexistent page. I'd like feedback that this looks okay; the obvious misgiving that people might have is that checking for a page's existence is an "expensive operation", so this would introduce many thousands of "expensive" operations across the site. On the other hand, it's not that expensive on balance, and it wouldn't take up much of the limit for most pages. The other issue is that it might produce "false positives" via uses of {{format link}}, which reuses the hatnote functionality; this could probably be mitigated in several ways. {{Nihiltres |talk |edits}} 21:17, 11 December 2021 (UTC)
- I don't know if your solution will apply to all hatnotes and you don't need a list of specific templates to update or not. If the latter, I recently found a case of
{{main}}
with a redlink target. this version of Rivière de la Tortue (Delson) has a redlink target in{{other uses}}
. That template has the "poorly implemented" check, but it didn't seem to work. Just mentioning this in case it is of interest or use in developing/testing your implementation. MB 02:49, 15 December 2021 (UTC)- This solution would apply to essentially all hatnotes that use automatic lists, by adding the check to the automatic formatting that those use. It wouldn't apply to wikitext-based hatnotes, which would have to have the functionality added manually per template, which should be easy. In the Rivière de la Tortue (Delson) example, the "poorly implemented" check failed because it only checks for a redlink when defaulting is used, while that page specifies the disambiguation page manually. My design checks for redlinks in all "target" links (it won't catch links in text inputs, but that's okay). {{Nihiltres |talk |edits}} 18:15, 16 December 2021 (UTC)
- I think that adding redlink detection for hatnote templates is a good idea, but I'm not sure that coupling it to the formatLink function is the way to go. {{Format link}} is used in several templates that don't relate to hatnotes, and having pages using these templates outputting a hatnote tracking category would be confusing. How about creating a separate function in Module:Hatnote that does the check for existence and then calls formatLink? Existing hatnote modules would need to be updated to use the new function, but it should only be a one-line fix for each module, and it would avoid any potential confusion. — Mr. Stradivarius ♪ talk ♪ 05:30, 15 December 2021 (UTC)
- I've updated the sandbox implementation to mitigate the problems somewhat. The prototype requires passing an option, allowing the category to be changed by supplying it as a category name string (an empty string disables categorization), or using the default with any non-string truthy value. I've specified "true" for most of the native methods (AFAICT only formatLink is used outside hatnotes), and added a parameter passthrough for the formatLink method that {{format link}} uses. That means the current prototype shouldn't affect {{format link}} and similar unless the user explicitly asks for it by specifying a category name to use in case of a redlink. That's probably a good "quick" solution. That said, having done that work, the cleanest solution would probably be to extract the link-formatting functionality into its own module (including the optional redlink categorization I've prototyped)—since it's being used outside hatnotes in the first place—and then to call that module inside Module:Hatnote's link-formatting functions, which could then (more) safely assume that they were hatnote-specific. I'm not eager to do the work for that, but it'd probably be a better long-term solution. {{Nihiltres |talk |edits}} 18:15, 16 December 2021 (UTC)
Extracting formatLink
I've done a decent chunk of the work to extract the functionality of formatLink
and _formatLink
to Module:Format link, as that seems to be the most sensible restructuring given that {{format link}} doesn't necessarily concern hatnotes.
The code at that module integrates improvements I've made that allow an option, specified as a parameter to formatLink
or a string option to _formatLink
, to enable redlink categorization if and only if specified, with the specified category name. Similarly, Module:Hatnote/sandbox has been updated to remove the base formatLink functionality, to call the new module, and to specify a default hatnote redlink-target category name.
A few other modules will need updating to match, but … does this look reasonable as designed? It still couples formatting a link to error detection (meh), but it's done in a much more generalized way that's not hatnote-specific where implemented. I'll need to do some more work to update testcases (migrating some from the hatnote module to the new module, adding some to test the new functionality) and (re)write documentation, but it's largely ready to go, so I'd appreciate further feedback (Mr. Stradivarius, especially). Thanks, {{Nihiltres |talk |edits}} 18:50, 20 December 2021 (UTC)