Page MenuHomePhabricator

Race condition when adding a portlet link on English Wiktionary
Closed, ResolvedPublicBUG REPORT

Authored By
Jack_who_built_the_house
Sep 2 2023, 2:45 PM
Referenced Files
F37650601: image.png
Sep 4 2023, 2:45 AM
F37650598: image.png
Sep 4 2023, 2:45 AM
F37650594: image.png
Sep 4 2023, 2:45 AM
F37648579: image.png
Sep 2 2023, 2:45 PM
F37648577: image.png
Sep 2 2023, 2:45 PM

Description

Steps to replicate the issue (include links if applicable):

What happens?:
Sometimes the link "Citations" is added to the namespaces portlet (and looking wrong because of T345490):

image.png (101×245 px, 3 KB)

Sometimes to the Tools portlet:

image.png (131×122 px, 3 KB)

What should have happened instead?:
The link "Citations" should be added to the namespace tabs.

What is the cause of the issue:
A part of the reason is this piece of code in the Wiktionary gadget:

var portlet = document.getElementById('p-namespaces') ? 'p-namespaces' : 'p-cactions';

This code is there since 2014. It chooses the portlet (which then supplies to mw.util.addPortletLink) based on the presense of #p-namespaces on the page.

The second part of the reason is this piece of code added to resources/skins.vector.js/skin.js by @Jdlrobson as a temporary fix ("This code maps items added by gadgets to the new menu").

So, from the looks of it, this temporary fix doesn't work reliably for the case above and instead causes a race condition. Don't know what is the best solution here. (Is the check for document.getElementById('p-namespaces') existence even needed? Are we targeting some old skins with it?)

Event Timeline

It turned out there's not only a race condition; on pages like https://en.wiktionary.org/wiki/Citations:house the execution of the script stops with error

image.png (128×1 px, 19 KB)
and links to the entry and its discussion aren't added. Instead of
image.png (95×250 px, 4 KB)

users see this:
image.png (102×247 px, 4 KB)

This is because on this line the script expects that the #p-namespace element is real and not a dummy and traverses its subtree. If there were no #p-namespace at all, the code would have fallen back to using #p-cactions. But because there is #p-namespace, the script terminates with an error. So, as a matter of fact, this dummy-adding code broke how tabs work in the "Citations" namespace of English Wiktionary.

Jack_who_built_the_house claimed this task.

The local solution is simple – choose the first findable portlet like document.getElementById('p-associated-pages') || document.getElementById('p-namespaces') || document.getElementById('p-cactions'), see https://en.wiktionary.org/wiki/Project:Grease_pit/2023/September#Fix_MediaWiki:Gadget-DocTabs.js. So I close this. But the very need to use such a tricky way hints a poor architecture IMO, see T345490#9138919.