Moving a bit deeper into the realm of "how to break redirects..."
Steps to replicate the issue
- Ensure the page at [[Example]] exists (and is not a redirect).
- Create a redirect that points to [[example#Foobar1234567890]]
What happens?:
- [[Example]] exists (and is not a redirect). So, the criteria in T326057: No warning on creating broken redirect to red link are met.
- The redirect half-works; it takes the user to page [[Example]], but since there is no section heading #Foobar1234567890, it doesn't take the user to that section, but leaves the user at the top of the page.
What should happen instead?:
MediaWiki should check the heading exists on the target page. If it doesn't exist, an error should be thrown.
The error message should probably say something like:
The redirect's target page {{{1}}} exists, but does not have the heading {{{2}}}. Please check you have the right target page and that the heading exists on that page. If you resubmit the same target, the page will be created anyway.
Ideally, this would also work for special pages, so [[Special:Preferences#mw-prefsection-rendering]] would be OK, but [[Special:Preferences#I-like-bananas]] wouldn't be.
Benefits:
Would prevent the creation of new "Broken section redirects," though it would still be possible if the name of a section heading is changed.
Notes:
A new function might have to be written to check whether section headings exist.
HTML <a name="foo"/> and <a id="bar"/> is not permitted by wikitext. Refer to https://www.mediawiki.org/wiki/Help:HTML_in_wikitext
However 1, <span> tags with id= attributes are permitted, and can be added by any editor, anywhere.
However 2, custom content models are permitted, and these could be literally anything https://www.mediawiki.org/wiki/Manual:Page_content_models
Therefore, it's probably a wrong approach to try to inspect the target page's Wikitext, as it might not be Wikitext, but something else entirely such as Markdown. So, the correct approach should be to pull the target page's HTML, run through that and find any relevant tags like <a id="foo"/> or <a name="bar"/> or <span id="qzzy"/>, etc, tags, store them as a list in an array. Including the first entry of Top, case-insensitive, which is always allowed.
Then compare the section heading to the array, and throw an error if the section header doesn't match anything in the array.
It's important that both normal text and percent encoding forms are recognised, so ? and %3F would be equivalent.
This may be best as part of includes/editpage/Constraint/BrokenRedirectConstraint.php rather than as a separate edit constraint.