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 !== '' && 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 RequestContext
83 public function setContext( RequestContext
$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. The 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();
230 if ( $this->plaintext
) {
231 $element = $performer->getName();
233 $element = Linker
::userLink(
235 $performer->getName()
238 if ( $this->linkFlood
) {
239 $element .= Linker
::userToolLinks(
241 $performer->getName(),
242 true, // Red if no edits
244 $performer->getEditCount()
249 if ( $this->entry
->isDeleted( LogPage
::DELETED_USER
) ) {
250 $element = self
::getRestrictedElement( 'rev-deleted-user' );
257 * Gets the luser provided comment
258 * @return string HTML
260 public function getComment() {
261 $comment = Linker
::commentBlock( $this->entry
->getComment() );
262 // No hard coded spaces thanx
263 $element = ltrim( $comment );
265 if ( $this->entry
->isDeleted( LogPage
::DELETED_COMMENT
) ) {
266 $element = self
::getRestrictedElement( 'rev-deleted-comment' );
273 * Helper method for displaying restricted element.
274 * @param $message string
275 * @return string HTML
277 protected function getRestrictedElement( $message ) {
278 if ( $this->plaintext
) {
279 return $this->msg( $message )->text();
282 $content = $this->msg( $message )->escaped();
283 $attribs = array( 'class' => 'history-deleted' );
284 return Html
::rawElement( 'span', $attribs, $content );
288 * Shortcut for wfMessage which honors local context.
289 * @todo Would it be better to require replacing the global context instead?
293 protected function msg( $key ) {
294 return wfMessage( $key )
295 ->inLanguage( $this->context
->getLang() )
296 ->title( $this->context
->getTitle() );
302 * This class formats all log entries for log types
303 * which have not been converted to the new system.
304 * This is not about old log entries which store
305 * parameters in a different format - the new
306 * LogFormatter classes have code to support formatting
310 class LegacyLogFormatter
extends LogFormatter
{
311 protected function getActionMessage() {
312 $entry = $this->entry
;
313 $action = LogPage
::actionText(
315 $entry->getSubtype(),
317 $this->context
->getSkin(),
318 (array)$entry->getParameters(),
322 $performer = $this->getPerformerElement();
323 return $performer . $this->msg( 'word-separator' )->text() . $action;
329 * This class formats move log entries.
332 class MoveLogFormatter
extends LogFormatter
{
333 protected function getMessageKey() {
334 $key = parent
::getMessageKey();
335 $params = $this->getMessageParameters();
336 if ( isset( $params[4] ) && $params[4] === '1' ) {
337 $key .= '-noredirect';
342 protected function getMessageParameters() {
343 $params = parent
::getMessageParameters();
344 $oldname = $this->makePageLink( $this->entry
->getTarget(), array( 'redirect' => 'no' ) );
345 $newname = $this->makePageLink( Title
::newFromText( $params[3] ) );
346 $params[2] = Message
::rawParam( $oldname );
347 $params[3] = Message
::rawParam( $newname );
353 * This class formats delete log entries.
356 class DeleteLogFormatter
extends LogFormatter
{
357 protected function getMessageKey() {
358 $key = parent
::getMessageKey();
359 if ( in_array( $this->entry
->getSubtype(), array( 'event', 'revision' ) ) ) {
360 if ( count( $this->getMessageParameters() ) < 5 ) {
361 return "$key-legacy";
367 protected function getMessageParameters() {
368 if ( isset( $this->parsedParametersDeleteLog
) ) {
369 return $this->parsedParametersDeleteLog
;
372 $params = parent
::getMessageParameters();
373 $subtype = $this->entry
->getSubtype();
374 if ( in_array( $subtype, array( 'event', 'revision' ) ) ) {
376 ($subtype === 'event' && count( $params ) === 6 ) ||
377 ($subtype === 'revision' && $params[3] === 'revision' )
379 $paramStart = $subtype === 'revision' ?
4 : 3;
381 $old = $this->parseBitField( $params[$paramStart+
1] );
382 $new = $this->parseBitField( $params[$paramStart+
2] );
383 list( $hid, $unhid, $extra ) = RevisionDeleter
::getChanges( $new, $old );
385 foreach ( $hid as $v ) {
386 $changes[] = $this->msg( "$v-hid" )->plain();
388 foreach ( $unhid as $v ) {
389 $changes[] = $this->msg( "$v-unhid" )->plain();
391 foreach ( $extra as $v ) {
392 $changes[] = $this->msg( $v )->plain();
394 $changeText = $this->context
->getLang()->listToText( $changes );
397 $newParams = array_slice( $params, 0, 3 );
398 $newParams[3] = $changeText;
399 $count = count( explode( ',', $params[$paramStart] ) );
400 $newParams[4] = $this->context
->getLang()->formatNum( $count );
401 return $this->parsedParametersDeleteLog
= $newParams;
403 return $this->parsedParametersDeleteLog
= array_slice( $params, 0, 3 );
407 return $this->parsedParametersDeleteLog
= $params;
410 protected function parseBitField( $string ) {
411 // Input is like ofield=2134 or just the number
412 if ( strpos( $string, 'field=' ) === 1 ) {
413 list( , $field ) = explode( '=', $string );
416 return (int) $string;