3 * Contains classes for formatting log entries
6 * @author Niklas Laxström
7 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
12 * Implements the default log formatting.
13 * Can be overridden by subclassing and setting
14 * $wgLogActionsHandlers['type/subtype'] = 'class'; or
15 * $wgLogActionsHandlers['type/*'] = 'class';
23 * Constructs a new formatter suitable for given entry.
24 * @param $entry LogEntry
25 * @return LogFormatter
27 public static function newFromEntry( LogEntry
$entry ) {
28 global $wgLogActionsHandlers;
29 $fulltype = $entry->getFullType();
30 $wildcard = $entry->getType() . '/*';
33 if ( isset( $wgLogActionsHandlers[$fulltype] ) ) {
34 $handler = $wgLogActionsHandlers[$fulltype];
35 } elseif ( isset( $wgLogActionsHandlers[$wildcard] ) ) {
36 $handler = $wgLogActionsHandlers[$wildcard];
39 if ( $handler !== '' && is_string( $handler ) && class_exists( $handler ) ) {
40 return new $handler( $entry );
43 return new LegacyLogFormatter( $entry );
47 * Handy shortcut for constructing a formatter directly from
50 * @see DatabaseLogEntry::getSelectQueryData
51 * @return LogFormatter
53 public static function newFromRow( $row ) {
54 return self
::newFromEntry( DatabaseLogEntry
::newFromRow( $row ) );
62 /// Whether to output user tool links
63 protected $linkFlood = false;
66 * Set to true if we are constructing a message text that is going to
67 * be included in page history or send to IRC feed. Links are replaced
68 * with plaintext or with [[pagename]] kind of syntax, that is parsed
69 * by page histories and IRC feeds.
72 protected $plaintext = false;
74 protected function __construct( LogEntry
$entry ) {
75 $this->entry
= $entry;
76 $this->context
= RequestContext
::getMain();
80 * Replace the default context
81 * @param $context IContextSource
83 public function setContext( IContextSource
$context ) {
84 $this->context
= $context;
88 * If set to true, will produce user tool links after
89 * the user name. This should be replaced with generic
91 * @param $value boolean
93 public function setShowUserToolLinks( $value ) {
94 $this->linkFlood
= $value;
98 * Ugly hack to produce plaintext version of the message.
99 * Usually you also want to set extraneous request context
100 * to avoid formatting for any particular user.
101 * @see getActionText()
102 * @return string text
104 public function getPlainActionText() {
105 $this->plaintext
= true;
106 $text = $this->getActionText();
107 $this->plaintext
= false;
112 * Gets the log action, including username.
113 * @return string HTML
115 public function getActionText() {
116 $element = $this->getActionMessage();
117 if ( $element instanceof Message
) {
118 $element = $this->plaintext ?
$element->text() : $element->escaped();
121 if ( $this->entry
->isDeleted( LogPage
::DELETED_ACTION
) ) {
122 $performer = $this->getPerformerElement() . $this->msg( 'word-separator' )->text();
123 $element = $performer . self
::getRestrictedElement( 'rev-deleted-event' );
130 * Returns a sentence describing the log action. Usually
131 * a Message object is returned, but old style log types
132 * and entries might return pre-escaped html string.
133 * @return Message|pre-escaped html
135 protected function getActionMessage() {
136 $message = $this->msg( $this->getMessageKey() );
137 $message->params( $this->getMessageParameters() );
142 * Returns a key to be used for formatting the action sentence.
143 * Default is logentry-TYPE-SUBTYPE for modern logs. Legacy log
144 * types will use custom keys, and subclasses can also alter the
145 * key depending on the entry itself.
146 * @return string message key
148 protected function getMessageKey() {
149 $type = $this->entry
->getType();
150 $subtype = $this->entry
->getSubtype();
151 $key = "logentry-$type-$subtype";
156 * Extract parameters intented for action message from
157 * array of all parameters. There are three hardcoded
158 * parameters (array zero-indexed, this list not):
159 * - 1: user name with premade link
160 * - 2: usable for gender magic function
161 * - 3: target page with premade link
164 protected function getMessageParameters() {
165 if ( isset( $this->parsedParameters
) ) {
166 return $this->parsedParameters
;
169 $entry = $this->entry
;
172 $params[0] = Message
::rawParam( $this->getPerformerElement() );
173 $params[1] = $entry->getPerformer()->getName();
174 $params[2] = Message
::rawParam( $this->makePageLink( $entry->getTarget() ) );
176 if ( $entry->isLegacy() ) {
177 foreach ( $entry->getParameters() as $index => $value ) {
178 $params[$index +
3] = $value;
182 // Filter out parameters which are not in format #:foo
183 foreach ( $entry->getParameters() as $key => $value ) {
184 if ( strpos( $key, ':' ) === false ) continue;
185 list( $index, $type, $name ) = explode( ':', $key, 3 );
186 $params[$index - 1] = $value;
189 /* Message class doesn't like non consecutive numbering.
190 * Fill in missing indexes with empty strings to avoid
191 * incorrect renumbering.
193 $max = max( array_keys( $params ) );
194 for ( $i = 4; $i < $max; $i++
) {
195 if ( !isset( $params[$i] ) ) {
200 return $this->parsedParameters
= $params;
204 * Helper to make a link to the page, taking the plaintext
205 * value in consideration.
206 * @param $title Title the page
207 * @param $parameters array query parameters
210 protected function makePageLink( Title
$title = null, $parameters = array() ) {
211 if ( !$this->plaintext
) {
212 $link = Linker
::link( $title, null, array(), $parameters );
214 if ( !$title instanceof Title
) {
215 throw new MWException( "Expected title, got null" );
217 $link = '[[' . $title->getPrefixedText() . ']]';
223 * Provides the name of the user who performed the log action.
224 * Used as part of log action message or standalone, depending
225 * which parts of the log entry has been hidden.
227 public function getPerformerElement() {
228 $performer = $this->entry
->getPerformer();
229 $element = $this->makeUserLink( $performer );
231 if ( $this->entry
->isDeleted( LogPage
::DELETED_USER
) ) {
232 $element = self
::getRestrictedElement( 'rev-deleted-user' );
239 * Gets the luser provided comment
240 * @return string HTML
242 public function getComment() {
243 $comment = Linker
::commentBlock( $this->entry
->getComment() );
244 // No hard coded spaces thanx
245 $element = ltrim( $comment );
247 if ( $this->entry
->isDeleted( LogPage
::DELETED_COMMENT
) ) {
248 $element = self
::getRestrictedElement( 'rev-deleted-comment' );
255 * Helper method for displaying restricted element.
256 * @param $message string
257 * @return string HTML
259 protected function getRestrictedElement( $message ) {
260 if ( $this->plaintext
) {
261 return $this->msg( $message )->text();
264 $content = $this->msg( $message )->escaped();
265 $attribs = array( 'class' => 'history-deleted' );
266 return Html
::rawElement( 'span', $attribs, $content );
270 * Shortcut for wfMessage which honors local context.
271 * @todo Would it be better to require replacing the global context instead?
275 protected function msg( $key ) {
276 return wfMessage( $key )
277 ->inLanguage( $this->context
->getLang() )
278 ->title( $this->context
->getTitle() );
281 protected function makeUserLink( User
$user ) {
282 if ( $this->plaintext
) {
283 $element = $user->getName();
285 $element = Linker
::userLink(
290 if ( $this->linkFlood
) {
291 $element .= Linker
::userToolLinks(
294 true, // Red if no edits
296 $user->getEditCount()
306 * This class formats all log entries for log types
307 * which have not been converted to the new system.
308 * This is not about old log entries which store
309 * parameters in a different format - the new
310 * LogFormatter classes have code to support formatting
314 class LegacyLogFormatter
extends LogFormatter
{
315 protected function getActionMessage() {
316 $entry = $this->entry
;
317 $action = LogPage
::actionText(
319 $entry->getSubtype(),
321 $this->context
->getSkin(),
322 (array)$entry->getParameters(),
326 $performer = $this->getPerformerElement();
327 return $performer . $this->msg( 'word-separator' )->text() . $action;
333 * This class formats move log entries.
336 class MoveLogFormatter
extends LogFormatter
{
337 protected function getMessageKey() {
338 $key = parent
::getMessageKey();
339 $params = $this->getMessageParameters();
340 if ( isset( $params[4] ) && $params[4] === '1' ) {
341 $key .= '-noredirect';
346 protected function getMessageParameters() {
347 $params = parent
::getMessageParameters();
348 $oldname = $this->makePageLink( $this->entry
->getTarget(), array( 'redirect' => 'no' ) );
349 $newname = $this->makePageLink( Title
::newFromText( $params[3] ) );
350 $params[2] = Message
::rawParam( $oldname );
351 $params[3] = Message
::rawParam( $newname );
357 * This class formats delete log entries.
360 class DeleteLogFormatter
extends LogFormatter
{
361 protected function getMessageKey() {
362 $key = parent
::getMessageKey();
363 if ( in_array( $this->entry
->getSubtype(), array( 'event', 'revision' ) ) ) {
364 if ( count( $this->getMessageParameters() ) < 5 ) {
365 return "$key-legacy";
371 protected function getMessageParameters() {
372 if ( isset( $this->parsedParametersDeleteLog
) ) {
373 return $this->parsedParametersDeleteLog
;
376 $params = parent
::getMessageParameters();
377 $subtype = $this->entry
->getSubtype();
378 if ( in_array( $subtype, array( 'event', 'revision' ) ) ) {
380 ($subtype === 'event' && count( $params ) === 6 ) ||
381 ($subtype === 'revision' && isset( $params[3] ) && $params[3] === 'revision' )
383 $paramStart = $subtype === 'revision' ?
4 : 3;
385 $old = $this->parseBitField( $params[$paramStart+
1] );
386 $new = $this->parseBitField( $params[$paramStart+
2] );
387 list( $hid, $unhid, $extra ) = RevisionDeleter
::getChanges( $new, $old );
389 foreach ( $hid as $v ) {
390 $changes[] = $this->msg( "$v-hid" )->plain();
392 foreach ( $unhid as $v ) {
393 $changes[] = $this->msg( "$v-unhid" )->plain();
395 foreach ( $extra as $v ) {
396 $changes[] = $this->msg( $v )->plain();
398 $changeText = $this->context
->getLang()->listToText( $changes );
401 $newParams = array_slice( $params, 0, 3 );
402 $newParams[3] = $changeText;
403 $count = count( explode( ',', $params[$paramStart] ) );
404 $newParams[4] = $this->context
->getLang()->formatNum( $count );
405 return $this->parsedParametersDeleteLog
= $newParams;
407 return $this->parsedParametersDeleteLog
= array_slice( $params, 0, 3 );
411 return $this->parsedParametersDeleteLog
= $params;
414 protected function parseBitField( $string ) {
415 // Input is like ofield=2134 or just the number
416 if ( strpos( $string, 'field=' ) === 1 ) {
417 list( , $field ) = explode( '=', $string );
420 return (int) $string;
426 * This class formats patrol log entries.
429 class PatrolLogFormatter
extends LogFormatter
{
430 protected function getMessageKey() {
431 $key = parent
::getMessageKey();
432 $params = $this->getMessageParameters();
433 if ( isset( $params[5] ) && $params[5] ) {
439 protected function getMessageParameters() {
440 $params = parent
::getMessageParameters();
441 $newParams = array_slice( $params, 0, 3 );
443 $target = $this->entry
->getTarget();
445 $revision = $this->context
->getLang()->formatNum( $oldid, true );
447 if ( $this->plaintext
) {
448 $revlink = $revision;
449 } elseif ( $target->exists() ) {
454 $revlink = Linker
::link( $target, htmlspecialchars( $revision ), array(), $query );
456 $revlink = htmlspecialchars( $revision );
459 $newParams[3] = Message
::rawParam( $revlink );
465 * This class formats new user log entries.
468 class NewUsersLogFormatter
extends LogFormatter
{
469 protected function getMessageParameters() {
470 $params = parent
::getMessageParameters();
471 if ( $this->entry
->getSubtype() === 'create2' ) {
472 if ( isset( $params[3] ) ) {
473 $target = User
::newFromId( $params[3] );
475 $target = User
::newFromName( $this->entry
->getTarget()->getText(), false );
477 $params[2] = Message
::rawParam( $this->makeUserLink( $target ) );
478 $params[3] = $target->getName();
483 public function getComment() {
484 $timestamp = wfTimestamp( TS_MW
, $this->entry
->getTimestamp() );
485 if ( $timestamp < '20080129000000' ) {
486 # Suppress $comment from old entries (before 2008-01-29),
487 # not needed and can contain incorrect links
490 return parent
::getComment();