私はMediawikiを学び、いくつかの拡張機能を見ています。
Manual:$wgUser(mediawiki.org)には、グローバル変数$wgUser
を新しいコードに使用しないでください。
Manual:RequestContext.phpは、$this->getUser()
または$context->getUser()
のいずれかを使用してコンテキストオブジェクトを代わりに使用する必要があると言います。
しかし、私はオンラインだ誰のための拡張で$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
Extension:WhosOnlineはMediawikiにありますが、具体的なページは次のとおりです。
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;
}
}
どのように正確に行う必要がありますか?
私はMediawiki 1.28.0を使用しています。 (リクエストでの作業フックを使用する場合>をコンテキスト)ページから
'$ this' /' $ context'は、メソッドを呼び出す前に存在しなければなりません。あなたの本当の疑問は、 "PHPでオブジェクトがどのように機能するのですか?"であり、[PHPマニュアル](https://secure.php.net/manual/en/language.oop5.basic.php)を読むことが最良の方法ですそれを学ぶ。 – Tgr