Logging to Special:Log in MediaWiki

While working on page language selector for core, I learnt how to log to Special:Log in MediaWiki. It is very easy to use it, but I was stuck for a couple of hours not knowing answers to some questions 🙂

So, let’s begin.

Defining a log category

If you need a separate log category, you need to declare the log entries usually done in DefaultSettings.php. There is an array containing all Log entries:

  $wgLogTypes = array(

Also, specify a log formatter using (again in DefaultSettings.php)

  $wgLogActionsHandlers['mylog/action'] = 'LogFormatter';	

Here we are using the default log formatter but you can make your own.

Adding a log entry

Now comes the task of writing a log entry. Here is how it is done.
New manual log entry, having the log category and the action

  $logEntry = new ManualLogEntry( category, action );

Set the performer of the entry

  global $wgUser;
  // Set the user as the current user
  $user = $wgUser;
  $logEntry->setPerformer( $user );

Note that on Special pages you can simply use
$entry->setPerformer( $this->getUser() );

Set the target of the log entry as the title.

  $entry->setTarget( $title );

You can add a comment as well.

  $entry->setComment( $comment );

Now you need to declare Log parameters. Here we are using the default log formatter, so here $1 is the user, $2 is the gender, $3 is the page we are referring to. We can add other parameters starting from 4.

  $logParams = array(
      '4::param1' => $var1,
      '5::param2' => $var2
  $entry->setParameters( $logParams );

Finally, insert the entry into the log table and publish it.

  $logid = $entry->insert();
  $entry->publish( $logid );

Adding system messages

An interesting thing is all you don’t need to declare all system messages that it uses anywhere( You can read more about it here http://www.mediawiki.org/wiki/Help:System_message ). If you use the conventional messages, you are good to go. For instance, if your log entry is 'mylog' in DefaultSettings.php, the following System messages can directly be assigned their values( for instance in en.json ) :

  "log-name-mylog": "Name of the log shown on Special:Log",
  "log-description-mylog": "Description of log shown on Special:Log.",
  "logentry-mylog-action": "The log entry on Special:Log.",

The "logentry-mylog-action" is meant to set the log entry to be displayed for the action.
This is how it should be used with the default log formatter. Add this to say en.json

  "logentry-mylog-action": "$1 {{GENDER:$2|action}} to page $3 with parameters $4 and $5"

And done! Whenever the logging entry is triggered, you will get an entry in Special:Log. Was easy enough right? 🙂

Making your log category optional

It is easy to make your log category optional, to be enabled only if a certain variable is set by the user.
So, instead of adding the entries to DefaultSettings.php, you can just add them to Setup.php as it is executed after LocalSettings.php, so you can check the configuration setting’s value there.

  if ( $wgVariable ) {
      $wgLogTypes[] = 'mylog';
      $wgLogActionsHandlers['mylog/action'] = 'LogFormatter';


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s