Reputation: 787
I'm trying to display a page's categories in my MediaWiki skin without resorting to the following default code:
<?php if ( $this->data['catlinks'] ) { $this->html( 'catlinks' ); } ?>
Which adds a bunch of unnecessary HTML that I do not want.
<div id='catlinks' class='catlinks'>
<div id="mw-normal-catlinks" class="mw-normal-catlinks">
<a href="/wiki/Special:Categories" title="Special:Categories">Category</a>:
<ul><li><a href="/w/index.php?title=Category:Player_Character&action=edit&redlink=1" class="new" title="Category:Player Character (page does not exist)">Player Character</a></li></ul>
</div>
</div>
Ideally I would just get the category names as plain text or an array, but I don't want to extrapolate them from the HTML that is returned by the code above.
How can I echo a MediaWiki page's categories as plain text?
Update:
I'm using this code to generate category breadcrumbs for my Wiki. Starting with the most top-level category and working it's way down. (Not tested excessively). Please note this is probably very hacky and would be better as an Extension.
// Get the current page's Title
$wiki_title = $this->data['skin']->getTitle();
// Get the categories
$parenttree = $wiki_title->getParentCategoryTree();
// Skin object passed by reference cause it can not be
// accessed under the method subfunction drawCategoryBrowser
$tempout = explode( "\n", $this->data['skin']->drawCategoryBrowser( $parenttree ) );
// Convert data to usable array
$wiki_category_breadcrumbs = explode( ">", $tempout[1]);
// Returns every category as URL with <li> tags wrapped around it
foreach( $wiki_category_breadcrumbs as $value ) {
echo "<li>". $value ."</li>";
}
Upvotes: 0
Views: 651
Reputation: 8520
The fact that categories are only available as html, is one of the weirder parts of MediaWiki skinning. It should still be possible to fetch them as an array, though. Depending on where in the skin code you are (you don't tell us what $this
refers to), these (untested) snippets should probably do the job:
From the execute()
-function:
$title = $this->data['skin']->getTitle();
$categories = $title->getParentCategories();
From the initPage()
-function:
$title = $this->getTitle();
$categories = $title->getParentCategories();
Upvotes: 1