BacklinksCache is created per-title right now. Instead, it needs to become a service class, maybe \MediaWiki\Page\LinksLookup?
Additionally, the following methods in from the Title object can be moved there:
- Title::getLinksTo
- Title::getTemplateLinksTo
- Title::getLinksFrom
- Title::getTemplateLinksFrom
- Title::getBrokenLinksFrom
During the refactoring, it seems like some methods can be merged together, e.g. Title::getLinksTo and BacklinksCache::getLinks seem to be doing exactly the same thing - getting an array of titles, linking to the provided title in various ways, with the Title-based one doing no caching.
We can also improve the API - passing in the table name seems wrong, it at the very least should be a class constant, or perhaps we should split it into separate methods, e.g. getRedirectsTo, getTemplateLinksTo, etc.
All methods should be converted to receiving/returning PageIdentity/LinkTarget/Iterator<PageIdentity> as appropriate (probably PageIdentity for the vast majority of methods).
New service and all its methods has to be covered by tests.
Perhaps the LinksLookup has to be an interface, with 2 implementations, CachedLinksLookup and DBLinksLookup, with the caching one being an adapter to the DB one. Not sure about that.