Mediawiki从另一个文件调用钩子函数

时间:2021-09-20 06:57:17

Hello I need to save some content to database in MediaWiki when a new page is created. So I added hook in my LocalSettings.php:

您好我需要在创建新页面时将一些内容保存到MediaWiki中的数据库。所以我在LocalSettings.php中添加了钩子:

$wgHooks['PageContentSaveComplete'][] ='assign_responsibility';

But I need to call the function assing_responsibility() from a extension php file Responsibility.php not LocalSettings. I am new at Mediawiki system and I cant find out How to tell MediaWiki where it can find required hook function? Thank you

但我需要从扩展php文件Responsibility.php调用函数assing_responsibility()而不是LocalSettings。我是Mediawiki系统的新手,我无法找到如何告诉MediaWiki哪里可以找到所需的钩子功能?谢谢

1 个解决方案

#1


Hook values are PHP callables; they can be defined in any file as long as the file gets loaded before the hook gets called (or, if you use a class method instead of a global function, the class is registered via $wgAutoloadClasses).

钩子值是PHP可调用的;只要在调用钩子之前加载文件,就可以在任何文件中定义它们(或者,如果使用类方法而不是全局函数,则通过$ wgAutoloadClasses注册该类)。

The convention is that your extension (which I assume is called Responsibility) creates a hook file:

惯例是您的扩展(我假设称为责任)创建一个钩子文件:

// ResponsibilityHooks.php
class ResponsibilityHooks {
    public static function onPageContentSaveComplete(/*...*/) { /*...*/ }
    // ...
}

and makes sure it can be autoloaded:

并确保它可以自动加载:

// Responsibility.php
$wgHooks['PageContentSaveComplete'][] = 'ResponsibilityHooks::onPageContentSaveComplete';   
$wgAutoloadClasses['ResponsibilityHooks'] = __DIR__ . '/ResponsibilityHooks.php';

#1


Hook values are PHP callables; they can be defined in any file as long as the file gets loaded before the hook gets called (or, if you use a class method instead of a global function, the class is registered via $wgAutoloadClasses).

钩子值是PHP可调用的;只要在调用钩子之前加载文件,就可以在任何文件中定义它们(或者,如果使用类方法而不是全局函数,则通过$ wgAutoloadClasses注册该类)。

The convention is that your extension (which I assume is called Responsibility) creates a hook file:

惯例是您的扩展(我假设称为责任)创建一个钩子文件:

// ResponsibilityHooks.php
class ResponsibilityHooks {
    public static function onPageContentSaveComplete(/*...*/) { /*...*/ }
    // ...
}

and makes sure it can be autoloaded:

并确保它可以自动加载:

// Responsibility.php
$wgHooks['PageContentSaveComplete'][] = 'ResponsibilityHooks::onPageContentSaveComplete';   
$wgAutoloadClasses['ResponsibilityHooks'] = __DIR__ . '/ResponsibilityHooks.php';