Instead of throwing fatals, accept null titles in old log entries.
[lhc/web/wiklou.git] / includes / logging / LogFormatter.php
1 <?php
2 /**
3 * Contains classes for formatting log entries
4 *
5 * @file
6 * @author Niklas Laxström
7 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
8 * @since 1.19
9 */
10
11 /**
12 * Implements the default log formatting.
13 * Can be overridden by subclassing and setting
14 * $wgLogActionsHandlers['type/subtype'] = 'class'; or
15 * $wgLogActionsHandlers['type/*'] = 'class';
16 * @since 1.19
17 */
18 class LogFormatter {
19
20 // Static->
21
22 /**
23 * Constructs a new formatter suitable for given entry.
24 * @param $entry LogEntry
25 * @return LogFormatter
26 */
27 public static function newFromEntry( LogEntry $entry ) {
28 global $wgLogActionsHandlers;
29 $fulltype = $entry->getFullType();
30 $wildcard = $entry->getType() . '/*';
31 $handler = '';
32
33 if ( isset( $wgLogActionsHandlers[$fulltype] ) ) {
34 $handler = $wgLogActionsHandlers[$fulltype];
35 } elseif ( isset( $wgLogActionsHandlers[$wildcard] ) ) {
36 $handler = $wgLogActionsHandlers[$wildcard];
37 }
38
39 if ( $handler !== '' && class_exists( $handler ) ) {
40 return new $handler( $entry );
41 }
42
43 return new LegacyLogFormatter( $entry );
44 }
45
46 /**
47 * Handy shortcut for constructing a formatter directly from
48 * database row.
49 * @param $row
50 * @see DatabaseLogEntry::getSelectQueryData
51 * @return LogFormatter
52 */
53 public static function newFromRow( $row ) {
54 return self::newFromEntry( DatabaseLogEntry::newFromRow( $row ) );
55 }
56
57 // Nonstatic->
58
59 /// @var LogEntry
60 protected $entry;
61
62 /// Whether to output user tool links
63 protected $linkFlood = false;
64
65 /**
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.
70 * @var boolean
71 */
72 protected $plaintext = false;
73
74 protected function __construct( LogEntry $entry ) {
75 $this->entry = $entry;
76 $this->context = RequestContext::getMain();
77 }
78
79 /**
80 * Replace the default context
81 * @param $context RequestContext
82 */
83 public function setContext( RequestContext $context ) {
84 $this->context = $context;
85 }
86
87 /**
88 * If set to true, will produce user tool links after
89 * the user name. This should be replaced with generic
90 * CSS/JS solution.
91 * @param $value boolean
92 */
93 public function setShowUserToolLinks( $value ) {
94 $this->linkFlood = $value;
95 }
96
97 /**
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
103 */
104 public function getPlainActionText() {
105 $this->plaintext = true;
106 $text = $this->getActionText();
107 $this->plaintext = false;
108 return $text;
109 }
110
111 /**
112 * Gets the log action, including username.
113 * @return string HTML
114 */
115 public function getActionText() {
116 $element = $this->getActionMessage();
117 if ( $element instanceof Message ) {
118 $element = $this->plaintext ? $element->text() : $element->escaped();
119 }
120
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' );
124 }
125
126 return $element;
127 }
128
129 /**
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
134 */
135 protected function getActionMessage() {
136 $message = $this->msg( $this->getMessageKey() );
137 $message->params( $this->getMessageParameters() );
138 return $message;
139 }
140
141 /**
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
147 */
148 protected function getMessageKey() {
149 $type = $this->entry->getType();
150 $subtype = $this->entry->getSubtype();
151 $key = "logentry-$type-$subtype";
152 return $key;
153 }
154
155 /**
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
162 * @return array
163 */
164 protected function getMessageParameters() {
165 $entry = $this->entry;
166
167 $params = array();
168 $params[0] = Message::rawParam( $this->getPerformerElement() );
169 $params[1] = $entry->getPerformer()->getName();
170 $params[2] = Message::rawParam( $this->makePageLink( $entry->getTarget() ) );
171
172 if ( $entry->isLegacy() ) {
173 foreach ( $entry->getParameters() as $index => $value ) {
174 $params[$index + 3] = $value;
175 }
176 }
177
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;
183 }
184
185 /* Message class doesn't like non consecutive numbering.
186 * Fill in missing indexes with empty strings to avoid
187 * incorrect renumbering.
188 */
189 $max = max( array_keys( $params ) );
190 for ( $i = 4; $i < $max; $i++ ) {
191 if ( !isset( $params[$i] ) ) {
192 $params[$i] = '';
193 }
194 }
195
196 return $params;
197 }
198
199 /**
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
204 * @return String
205 */
206 protected function makePageLink( Title $title = null, $parameters = array() ) {
207 if ( !$this->plaintext ) {
208 $link = Linker::link( $title, null, array(), $parameters );
209 } else {
210 $link = '[[' . $title->getPrefixedText() . ']]';
211 }
212 return $link;
213 }
214
215 /**
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.
219 */
220 public function getPerformerElement() {
221 $performer = $this->entry->getPerformer();
222
223 if ( $this->plaintext ) {
224 $element = $performer->getName();
225 } else {
226 $element = Linker::userLink(
227 $performer->getId(),
228 $performer->getName()
229 );
230
231 if ( $this->linkFlood ) {
232 $element .= Linker::userToolLinks(
233 $performer->getId(),
234 $performer->getName(),
235 true, // Red if no edits
236 0, // Flags
237 $performer->getEditCount()
238 );
239 }
240 }
241
242 if ( $this->entry->isDeleted( LogPage::DELETED_USER ) ) {
243 $element = self::getRestrictedElement( 'rev-deleted-user' );
244 }
245
246 return $element;
247 }
248
249 /**
250 * Gets the luser provided comment
251 * @return string HTML
252 */
253 public function getComment() {
254 $lang = $this->context->getLang();
255 $element = $lang->getDirMark() . Linker::commentBlock( $this->entry->getComment() );
256
257 if ( $this->entry->isDeleted( LogPage::DELETED_COMMENT ) ) {
258 $element = self::getRestrictedElement( 'rev-deleted-comment' );
259 }
260
261 return $element;
262 }
263
264 /**
265 * Helper method for displaying restricted element.
266 * @param $message string
267 * @return string HTML
268 */
269 protected function getRestrictedElement( $message ) {
270 if ( $this->plaintext ) {
271 return $this->msg( $message )->text();
272 }
273
274 $content = $this->msg( $message )->escaped();
275 $attribs = array( 'class' => 'history-deleted' );
276 return Html::rawElement( 'span', $attribs, $content );
277 }
278
279 /**
280 * Shortcut for wfMessage which honors local context.
281 * @todo Would it be better to require replacing the global context instead?
282 * @param $key string
283 * @return Message
284 */
285 protected function msg( $key ) {
286 return wfMessage( $key )
287 ->inLanguage( $this->context->getLang() )
288 ->title( $this->context->getTitle() );
289 }
290
291 }
292
293 /**
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
299 * those too.
300 * @since 1.19
301 */
302 class LegacyLogFormatter extends LogFormatter {
303 protected function getActionMessage() {
304 $entry = $this->entry;
305 $action = LogPage::actionText(
306 $entry->getType(),
307 $entry->getSubtype(),
308 $entry->getTarget(),
309 $this->context->getSkin(),
310 (array)$entry->getParameters(),
311 true
312 );
313
314 $performer = $this->getPerformerElement();
315 return $performer . $this->msg( 'word-separator' )->text() . $action;
316 }
317
318 }
319
320 /**
321 * This class formats move log entries.
322 * @since 1.19
323 */
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';
330 }
331 return $key;
332 }
333
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 );
340 return $params;
341 }
342 }