Call a hook inside another hook function in MediaWiki

51 Views Asked by At

Hello guys I'm new to MediaWiki and trying to build my own extension. Using this extension I'm trying to show some content blow page heading but only to a page specific to a category.

For that, I'm using two hooks:

  1. onArticleViewHeader ( To add my HTML content below the page heading)
  2. onOutputPageMakeCategoryLinks (To get all the category of page being loaded)

From the first hook, I'm able to show my content using the following code:

    public static function onArticleViewHeader( &$article, &$outputDone, &$pcache ) {
        $article->getContext()->getOutput()->addHTML("Printed from a hook");            
    }

The above code prints the HTML below every page heading but I want to load HTML only to a specific page category. So for that, I'm trying to load the category and I'm just trying to call my first hook only if the category gets caught.

    public static function onOutputPageMakeCategoryLinks( &$out, $categories, &$links ) {
        foreach($categories as $category){
            if($category=="my_page_category"){
                MyExtentionClass::onArticleViewHeader();
            }
        }
    }

I know I'm calling the hook in a bad manner which is not correct. But I just wanted to call my 1st hook 'onArticleViewHeader' from inside of my 2nd hook so that I can print my HTML only to a page with a specific category.

2

There are 2 best solutions below

2
Tgr On

Just use $article->getPage()->getCategories() in the header hook.

0
Santosh Kp On

Haven't really got the exact solution of the question I asked but has got the way out to solve the problem I have been facing.

I just tried getting the current categories in the "onArticleViewHeader" itself by using some of MediaWiki's global variables.


global $wgOut;

$title = Title::newFromText( $wgOut->getPageTitle() );
$categories = $title->getParentCategories();

if(isset($categories['Category:my_cat_name']){

    //formed my logic here

}

This might help some other people facing this kind of issue.