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 $entry = $this->entry
;
168 $params[0] = Message
::rawParam( $this->getPerformerElement() );
169 $params[1] = $entry->getPerformer()->getName();
170 $params[2] = Message
::rawParam( $this->makePageLink( $entry->getTarget() ) );
172 if ( $entry->isLegacy() ) {
173 foreach ( $entry->getParameters() as $index => $value ) {
174 $params[$index +
3] = $value;
178 // Filter out parameters which are not in format #:foo
179 foreach ( $entry->getParameters() as $key => $value ) {
180 if ( strpos( $key, ':' ) === false ) continue;
181 list( $index, $type, $name ) = explode( ':', $key, 3 );
182 $params[$index - 1] = $value;
185 /* Message class doesn't like non consecutive numbering.
186 * Fill in missing indexes with empty strings to avoid
187 * incorrect renumbering.
189 $max = max( array_keys( $params ) );
190 for ( $i = 4; $i < $max; $i++
) {
191 if ( !isset( $params[$i] ) ) {
200 * Helper to make a link to the page, taking the plaintext
201 * value in consideration.
202 * @param $title Title the page
203 * @param $parameters array query parameters
206 protected function makePageLink( Title
$title, $parameters = array() ) {
207 if ( !$this->plaintext
) {
208 $link = Linker
::link( $title, null, array(), $parameters );
210 $link = '[[' . $title->getPrefixedText() . ']]';
216 * Provides the name of the user who performed the log action.
217 * Used as part of log action message or standalone, depending
218 * which parts of the log entry has been hidden.
220 public function getPerformerElement() {
221 $performer = $this->entry
->getPerformer();
223 if ( $this->plaintext
) {
224 $element = $performer->getName();
226 $element = Linker
::userLink(
228 $performer->getName()
231 if ( $this->linkFlood
) {
232 $element .= Linker
::userToolLinks(
234 $performer->getName(),
235 true, // Red if no edits
237 $performer->getEditCount()
242 if ( $this->entry
->isDeleted( LogPage
::DELETED_USER
) ) {
243 $element = self
::getRestrictedElement( 'rev-deleted-user' );
250 * Gets the luser provided comment
251 * @return string HTML
253 public function getComment() {
254 $lang = $this->context
->getLang();
255 $element = $lang->getDirMark() . Linker
::commentBlock( $this->entry
->getComment() );
257 if ( $this->entry
->isDeleted( LogPage
::DELETED_COMMENT
) ) {
258 $element = self
::getRestrictedElement( 'rev-deleted-comment' );
265 * Helper method for displaying restricted element.
266 * @param $message string
267 * @return string HTML
269 protected function getRestrictedElement( $message ) {
270 if ( $this->plaintext
) {
271 return $this->msg( $message )->text();
274 $content = $this->msg( $message )->escaped();
275 $attribs = array( 'class' => 'history-deleted' );
276 return Html
::rawElement( 'span', $attribs, $content );
280 * Shortcut for wfMessage which honors local context.
281 * @todo Would it be better to require replacing the global context instead?
285 protected function msg( $key ) {
286 return wfMessage( $key )
287 ->inLanguage( $this->context
->getLang() )
288 ->title( $this->context
->getTitle() );
294 * This class formats all log entries for log types
295 * which have not been converted to the new system.
296 * This is not about old log entries which store
297 * parameters in a different format - the new
298 * LogFormatter classes have code to support formatting
302 class LegacyLogFormatter
extends LogFormatter
{
303 protected function getActionMessage() {
304 $entry = $this->entry
;
305 $action = LogPage
::actionText(
307 $entry->getSubtype(),
309 $this->context
->getSkin(),
310 (array)$entry->getParameters(),
314 $performer = $this->getPerformerElement();
315 return $performer . $this->msg( 'word-separator' )->text() . $action;
321 * This class formats move log entries.
324 class MoveLogFormatter
extends LogFormatter
{
325 protected function getMessageKey() {
326 $key = parent
::getMessageKey();
327 $params = $this->getMessageParameters();
328 if ( isset( $params[4] ) && $params[4] === '1' ) {
329 $key .= '-noredirect';
334 protected function getMessageParameters() {
335 $params = parent
::getMessageParameters();
336 $oldname = $this->makePageLink( $this->entry
->getTarget(), array( 'redirect' => 'no' ) );
337 $newname = $this->makePageLink( Title
::newFromText( $params[3] ) );
338 $params[2] = Message
::rawParam( $oldname );
339 $params[3] = Message
::rawParam( $newname );