MediaWiki $ wgUser到$ this-> getUser()和$ context-> getUser()不工作

时间:2017-01-22 00:47:34

标签: php mediawiki mediawiki-extensions

我正在学习Mediawiki并查看一些扩展程序。

mediawiki.org上的

Manual:$wgUser声明全局变量$wgUser不应用于新代码。

Manual:RequestContext.php表示应使用$this->getUser()$context->getUser()代替上下文对象。

但是,当我尝试在Who's Online的扩展名中使用$this->getUser()->getName()时,我收到以下错误:

Fatal error: Using $this when not in object context in /home/ghsfhaco/public_html/wiki/extensions/WhosOnline/WhosOnlineHooks.php on line 19

当我将其更改为$context->getUser()->getName()时,我收到此错误:

Fatal error: Call to a member function getUser() on null in /home/ghsfhaco/public_html/wiki/extensions/WhosOnline/WhosOnlineHooks.php on line 19

可以在Mediawiki找到完整的Extension:WhosOnline,但这是特定页面:

    class WhosOnlineHooks {

    // update online data
    public static function onBeforePageDisplay() {
        global $wgUser;

        // write to DB (use master)
        $dbw = wfGetDB( DB_MASTER );
        $now = gmdate( 'YmdHis', time() );

        // row to insert to table
        $row = array(
            'userid' => $wgUser->getId(),
            'username' => $wgUser->getName(),
            'timestamp' => $now
        );

        $method = __METHOD__;
        $dbw->onTransactionIdle( function() use ( $dbw, $method, $row ) {
            $dbw->upsert(
                'online',
                $row,
                array( array( 'userid', 'username' ) ),
                array( 'timestamp' => $row['timestamp'] ),
                $method
            );
        } );

        return true;
    }

    public static function onLoadExtensionSchemaUpdates( $updater ) {
        $updater->addExtensionUpdate( array( 'addTable', 'online',
            __DIR__ . '/whosonline.sql', true ) );
        return true;
    }
}

应该怎么做?

BTW,我正在使用Mediawiki 1.28.0。

1 个答案:

答案 0 :(得分:1)

从您链接的页面(使用请求上下文> 使用挂钩时):如果您的挂钩提供OutputPage作为参数,请使用由它提供的上下文。 BeforePageDisplay确实提供了一个OutputPage,所以只需使用它的getUser()方法。

相关问题