Background
In wikitext, you can place a page into one or more categories, e.g. [[Category:Fruits]]. When parsing an edit (Parser), these are extracted and stored along with the rest of the article content in ParserCache/ParserOutput where during a pageview, it can be retrieved at no overhead (i.e. already included the ParserOutput object) via ParserOutput::getCategoryNames.
The skin retrieves these and renders them at the bottom of the page, below the article content:
Problem / Opportunity
This depends on two bits of information that are not cached, and are instead retrieved from the database on every pageview:
- Page existence metadata for the category pages (i.e. red or blue).
- Page property "hiddencat" for the category pages (i.e. normal or hidden cat).
I'm guessing that this was implemented this way, rather than behind the ParserCache, because otherwise when a category page is created or deleted, the information would be stale for upto 30 days in the ParserCache. That could be fixed by setting up LinksUpdate propagation (as we do for pagelinksand templatelinks), by either recording them as pagelinks, or by treating outgoing categorylinks the same (i.e. only recurse on creation or deletion to turn blue links red and vice versa).
Except that would not suffice, because the hiddencat status can technically change on every edit, so you'd either have to introduce a new way to detect changes from and to that state with dedicated change propagation, or, propagate on every category page edit which would be prohibitively expensive and profoundly wasteful, and so we don't it that way.
Instead, we fetch this live from the database on each pageview.
For logged-out users, pageviews are regularly CDN-cached for 14 days and we do nothing to invalidate that on category creations/edits/deletions. So the reality is that we do cache this information, and there's seemingly been no complaint or effort to change that in twenty years.
For logged-in users, this information is magically always up-to-date. This most visible when it comes to seeing the effect of your own actions. We have affordances throughout MediaWiki (e.g. ChronologyProtector, and UseDC=master cookies) to make sure users see the effect for their own actions, even if more broadly, for performance reasons, we don't want to instantly purge everything everywhere.
As part of T433637, we're trying to leverage the CDN cache for pageviews with ESI. If that succeeds, then this will implicitly become cached and stale the same way for logged-in users as well,
If that is okay with ESI, it should be oaky without ESI as well, so let's evaluate what we can to to avoid these DB queries on pageviews.
Status quo
It happens in three phases today.
- Parsing: Parser.php calls ParserOutput::addCategory() and the name is later stored via ParserCache.
- OutputPage (early):
- retrieve ParserOutput from ParserCache
- call OutputPage::addParserOutputMetadata > OutputPage::addCategoryLinks
- This uses OutputPage::addCategoryLinksToLBAndGetResult to fetche page existence metadata (fed to LinkCache), and hiddencat page prop. This bypasses the PageProps service/cache, but it does combine the page and pageprop queries, so makes 1 query today, not 2.
- It pre-generates an array with small chunks of HTML using LinkRenderer::makeLink <a href=.. title=.. class=..>..</a>, which makes use of the LinkCache page existence metadata.
- It groups the HTML chunks by "normal" and "hidden".
- Skin (late): The skin eventually calls Skin::getCategories to get the HTML blob for the bottom of the page
- This calls Skin::getCategoryLinks > OutputPage::getCategoryLinks to get the pre-generated and pre-grouped HTML chunks.
- It wraps it in an unordered list. This currently does a second call to OutputPage::getCategoryLinks to know whether the list is empty. This is an ugly workaround the fact that its own business logic in Skin::getCategoryLinks failing to account for this or conveying it, even tough it clearly needs it.

