手册:将操作记录到Special:Log
Appearance
此页面描述了如何将操作记录到Special:Log。
(将操作记录到应用程序日志的方法由Manual:Structured logging收录。)
示例
这描绘了你如何为扩展程序将Special:Log项目编码。
日志消息出现在Special:Log页面,并能被过滤(例如日志名称、用户、标题或日期范围)。
在你的起始扩展程序文件中
以下应在extension.json生效:
{
"LogTypes": [ "foo" ],
"LogNames": {
"foo": "foo-name"
},
"LogHeaders": {
"foo": "foo-header"
},
"LogActionsHandlers": {
"foo/*": "LogFormatter"
}
}
在i18n/en.json文件中
{
"log-name-foo": "Foo log",
"log-description-foo": "These events track when Foo events happen in the system.",
"logentry-foo-bar": "$1 {{GENDER:$2|did bar}} to page $3"
}
消息文档(qqq.json):
{
"log-name-foo": "The Special:Log log name that appears in the drop-down on the Special:Log page",
"log-description-foo": "The Special:Log description that appears on the Special:Log page when you filter logs on this specific log name",
"logentry-foo-bar": "The template of the log entry message"
}
在扩展程序代码中
// Anywhere in your code where you want to generate a log entry
$logEntry = new ManualLogEntry( 'foo', 'bar' ); // Log action 'bar' in the Special:Log for 'foo'
$logEntry->setPerformer( $user ); // User object, the user who performed this action
$logEntry->setTarget( $this ); // The page that this log entry affects, a Title object
$logEntry->setComment( $reason ); // Optional, user provided comment
// 可选地为日志项的i18n消息的使用添加附加参数。
// 编号应从4开始,并可以被用于作为$4、$5等的消息。
//
// 索引$1、$2和$3被保留并提供消息的用户名和目标页面参数。
// $1 is a reference to the user page and user talk page in the wiki
// $2 is used to determine the gender of the user for any gender specific messages
// $3 is a reference to the page on which the action took place
//
// 如果你想存储不应在消息中可用的参数,不要将数字作为消息键的前缀且不要使用冒号。
//
// The format is index:formatspecifier:name.
// Format specifier is currently unused, but in future you could say for example
// that this param is a number, format it according to the user language.
// Name is just for giving identifier for the value, and helps to debug issues
// versus unindexed list of parameters.
$logEntry->setParameters( [
'4::paramname' => 'customparam',
'hiddenparam' => 'ugly stuff',
] );
// We're not done yet, we need to insert the log entry into the database.
// Insert adds it to the logging table and returns the id of that log entry.
$logid = $logEntry->insert();
// Optionally, publish the log entry in recent changes and the UDP feed of recent changes
// if we want. UDP feed is mainly used for echoing the recent change items into IRC.
// publish() takes second param with values 'rcandudp' (default), 'rc' and 'udp'.
$logEntry->publish( $logid );
添加链接
若想将链接加入到日志项中,你应该将页面名等转移到日志参数中,并使用makePageLink()在你的日志格式化工具中格式化它们。
若使用其他方法,非Html结果(例如到IRC的UDP反馈)将会损坏。
参见LogFormatter以查看带链接的日志格式化工具的例子。