siddharthvp / mwn

A JavaScript & TypeScript MediaWiki bot framework for Node.js
https://mwn.toolforge.org
GNU Lesser General Public License v3.0
47 stars 9 forks source link

Handling of categories with prefix doesn't work #80

Open JN-Jones opened 2 months ago

JN-Jones commented 2 months ago

In our wiki we have some categories specifically to group templates. In certain scenarios however this seems to cause issues with the bot. Ie new bot.Category("Template:Infobox") throws Error: not a category page instead of referencing Category:Template:Infobox. This can also be observed with MwnTitle: new bot.Title('Template:Infobox', Namespace.CATEGORY); results in { namespace: 10, title: 'Infobox', fragment: null }. Using an invalid prefix however works as intended: new bot.Title('Invalid:Infobox', Namespace.CATEGORY); results in { namespace: 14, title: 'Infobox', fragment: null }. Prefixing the title correctly with Category: also works: new bot.Title('Category:Template:Infobox', Namespace.CATEGORY); results in { namespace: 14, title: 'Template:Infobox', fragment: null }.

One use case where this issue becomes apparent is when trying to map included categories:

    const article = new bot.Page(PAGE_IN_TEMPLATE_CATEGORY, Namespace.TEMPLATE);
    const categories = (await article.categories()).map(({ category }) => new bot.Category(category));
siddharthvp commented 1 month ago

This is mostly intentional. Title's interface matches that of mw.Title in the on-site JS interface - which works similarly and only uses the second constructor arg if the page name passed doesn't already have a namespace.

You can use makeTitle to create a title without namespace auto-inference, and then create a Category object using the Title object.

let title = bot.Title.makeTitle(Namespace.CATEGORY, "Template:Infobox");
let category = new bot.Category(title);