user3734
user3734

Reputation: 13

how can i get parent category in mediawiki?

I have three levels of categories. For example:

I'd like to get parent 1 and 2 via PHP and javascript.

Upvotes: 0

Views: 719

Answers (1)

leo
leo

Reputation: 8520

You don't write what context you want to do this in, but given that you have access to the a CategoryPage object (you probably do), you should use the method CategoryPage::getCategories to get an array of CategoryPage objects representing the parent categories. Then you can query them in the same way.

Note though, that hidden categories will also show up when doing this. Also note that MediaWiki does not have any limits on the category structure, so you should be prepared to handle situations like Category:A > Category:B > Category:A

Once you get the “grandparent” categories you can return them as the result of a parser function if you want to print it out (no need for javascript then). The arrays extension might come in handy, as there could be more than one category. If you want to use it in a javascript, you could either return it as a variable (though mw.config or otherwise), or though the API.

If this is something that is only needed for a javascript, you could also do mw.config.get("wgCategories"), and then query the API for categories of those categories: api.php?action=query&titles=Category:A|Category:B&prop=categories

An alternative approach, if you do not want to do any programming, could be to install Semantic MediaWiki, that allows you to query for categories, among other things. That might also allow you to achieve whatever it is you are trying to do in a cleaner way than relying on a category hack and a custom extension.

Upvotes: 1

Related Questions