问题
I'm setting up a bunch of different language mediawiki's on one codebase. So far most of it is working, but I want to use a main page that looks different than the rest of the pages. To do this I originally just added some css rules that only applied to the main_page class:
body .page-Main_Page { <rules> }
The problem is that in other languages the main page is called differently (and the class changes accordingly), so I either have to add css rules for all possible main page titles (not gonna happen) or do a check inside the skin that adds a class to the body if the current page is the main page.
Unfortunately, there's no way to check that. Most solutions I've googled are based on either 'Main Page' or setting your own title. I want the skin to detect the title automatically.
The only solution I´ve thought of so far is take mediawiki´s variable for the main page url and compare it to the current page title:
$mainurl = $this->data['nav_urls']['mainpage']['href'];
$ismain = $this->data['thispage'] == substr($mainurl,'6');
(the ['href'] starts with /wiki/, hence the offset of 6)
and later
<body class="<?php echo ($ismain ? 'mainpage' : ''); ?>">
(and some more classes of course, but you get the idea)
But this gives me another problem. In spanish, the page is called Página Principal. The data['thispage'] has no problems with that, but the ['href'] is saved as /wiki/P%C3%A1gina_Principal
So... can anyone either tell me how to 1. lookup the translation for 'Main Page' in the language that is currently used OR 2. convert P%C3%A1gina_Principal into Página Principal (and the like for other languages) OR 3. show me another way to check if I'm on the main page?
回答1:
There is actually an even simpler way:
if ($this->getSkin()->getTitle()->isMainPage())
Returns a boolean
回答2:
Haha omg, it was so simple.
$this->data['skin']->mTitle->mUrlform == $mainurl;
Well that proves once again that typing out your problem to someone else can sometimes give you just the insight you needed.. :P
回答3:
Unfortunately it doesn't work for me with MediaWiki 1.16.4. I looked into it a little bit and it works (for me) when checking on
$this->data['skin']->mTitle->mUrlform == "Main_Page"
The problem is that
$this->data['nav_urls']['mainpage']['href'] == "/en/Main_Page"
so it requires some more thinking to get to a more generic solution.
(Could be that upgrading to a later MW will make the above code work though.)
来源:https://stackoverflow.com/questions/3592299/mediawiki-custom-skin-check-if-current-page-is-main-page