Topic on Project:Support desk

Is it possible to have loop in category hierarchy?

4
Franklin Yu (talkcontribs)

For example, category A < category B < category C < category A (where "<" means "is sub-category of"). If possible, how do we prevent/detect that?

Ciencia Al Poder (talkcontribs)

It is possible, and there's no way to prevent that AFAIK. The operation to find such a loop may be expensive, imagine traversing the chain of thousands of categories on Wikipedia... Doing this may require a Maintenance script, or a bot that would download all categories and constructing the tree client-side. I've done a quick search to see if Pywikibot has a script to do this but I haven't found it.

Franklin Yu (talkcontribs)

Thank you. I believe detecting the cycles regularly (annually or monthly) is expensive (roughly time and memory each time, since in most Wikipedia it is a sparse graph). However, I am wondering whether it would be less expensive to detect once, and monitor for change? Categories does not get changed frequently.

Ciencia Al Poder (talkcontribs)

Well, it depends on how long a chain of categories can be, because the code would need to perform a query for each step of the chain to find the parent one, because there's no way to retrieve the entire chain on a single query, unless a new table is created tracking all subcategories of a given category recursively.

This check can also be triggered more often than you think, for example, adding/changing a category on a template used on a lot of pages.

Anyway, feel free to request such a feature on phabricator

Reply to "Is it possible to have loop in category hierarchy?"