Localise brackets in log entry
[lhc/web/wiklou.git] / includes / logging / LogPage.php
1 <?php
2 /**
3 * Contain log classes
4 *
5 * Copyright © 2002, 2004 Brion Vibber <brion@pobox.com>
6 * http://www.mediawiki.org/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 */
25
26 /**
27 * Class to simplify the use of log pages.
28 * The logs are now kept in a table which is easier to manage and trim
29 * than ever-growing wiki pages.
30 *
31 */
32 class LogPage {
33 const DELETED_ACTION = 1;
34 const DELETED_COMMENT = 2;
35 const DELETED_USER = 4;
36 const DELETED_RESTRICTED = 8;
37 // Convenience fields
38 const SUPPRESSED_USER = 12;
39 const SUPPRESSED_ACTION = 9;
40 /* @access private */
41 var $type, $action, $comment, $params;
42
43 /**
44 * @var User
45 */
46 var $doer;
47
48 /**
49 * @var Title
50 */
51 var $target;
52
53 /* @access public */
54 var $updateRecentChanges, $sendToUDP;
55
56 /**
57 * Constructor
58 *
59 * @param string $type one of '', 'block', 'protect', 'rights', 'delete',
60 * 'upload', 'move'
61 * @param $rc Boolean: whether to update recent changes as well as the logging table
62 * @param string $udp pass 'UDP' to send to the UDP feed if NOT sent to RC
63 */
64 public function __construct( $type, $rc = true, $udp = 'skipUDP' ) {
65 $this->type = $type;
66 $this->updateRecentChanges = $rc;
67 $this->sendToUDP = ( $udp == 'UDP' );
68 }
69
70 /**
71 * @return int log_id of the inserted log entry
72 */
73 protected function saveContent() {
74 global $wgLogRestrictions;
75
76 $dbw = wfGetDB( DB_MASTER );
77 $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
78
79 $this->timestamp = $now = wfTimestampNow();
80 $data = array(
81 'log_id' => $log_id,
82 'log_type' => $this->type,
83 'log_action' => $this->action,
84 'log_timestamp' => $dbw->timestamp( $now ),
85 'log_user' => $this->doer->getId(),
86 'log_user_text' => $this->doer->getName(),
87 'log_namespace' => $this->target->getNamespace(),
88 'log_title' => $this->target->getDBkey(),
89 'log_page' => $this->target->getArticleID(),
90 'log_comment' => $this->comment,
91 'log_params' => $this->params
92 );
93 $dbw->insert( 'logging', $data, __METHOD__ );
94 $newId = !is_null( $log_id ) ? $log_id : $dbw->insertId();
95
96 # And update recentchanges
97 if ( $this->updateRecentChanges ) {
98 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
99
100 RecentChange::notifyLog(
101 $now, $titleObj, $this->doer, $this->getRcComment(), '',
102 $this->type, $this->action, $this->target, $this->comment,
103 $this->params, $newId, $this->getRcCommentIRC()
104 );
105 } elseif ( $this->sendToUDP ) {
106 # Don't send private logs to UDP
107 if ( isset( $wgLogRestrictions[$this->type] ) && $wgLogRestrictions[$this->type] != '*' ) {
108 return $newId;
109 }
110
111 # Notify external application via UDP.
112 # We send this to IRC but do not want to add it the RC table.
113 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
114 $rc = RecentChange::newLogEntry(
115 $now, $titleObj, $this->doer, $this->getRcComment(), '',
116 $this->type, $this->action, $this->target, $this->comment,
117 $this->params, $newId, $this->getRcCommentIRC()
118 );
119 $rc->notifyRC2UDP();
120 }
121
122 return $newId;
123 }
124
125 /**
126 * Get the RC comment from the last addEntry() call
127 *
128 * @return string
129 */
130 public function getRcComment() {
131 $rcComment = $this->actionText;
132
133 if ( $this->comment != '' ) {
134 if ( $rcComment == '' ) {
135 $rcComment = $this->comment;
136 } else {
137 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
138 $this->comment;
139 }
140 }
141
142 return $rcComment;
143 }
144
145 /**
146 * Get the RC comment from the last addEntry() call for IRC
147 *
148 * @return string
149 */
150 public function getRcCommentIRC() {
151 $rcComment = $this->ircActionText;
152
153 if ( $this->comment != '' ) {
154 if ( $rcComment == '' ) {
155 $rcComment = $this->comment;
156 } else {
157 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
158 $this->comment;
159 }
160 }
161
162 return $rcComment;
163 }
164
165 /**
166 * Get the comment from the last addEntry() call
167 */
168 public function getComment() {
169 return $this->comment;
170 }
171
172 /**
173 * Get the list of valid log types
174 *
175 * @return Array of strings
176 */
177 public static function validTypes() {
178 global $wgLogTypes;
179
180 return $wgLogTypes;
181 }
182
183 /**
184 * Is $type a valid log type
185 *
186 * @param string $type log type to check
187 * @return Boolean
188 */
189 public static function isLogType( $type ) {
190 return in_array( $type, LogPage::validTypes() );
191 }
192
193 /**
194 * Get the name for the given log type
195 *
196 * @param string $type logtype
197 * @return String: log name
198 * @deprecated in 1.19, warnings in 1.21. Use getName()
199 */
200 public static function logName( $type ) {
201 global $wgLogNames;
202
203 if ( isset( $wgLogNames[$type] ) ) {
204 return str_replace( '_', ' ', wfMessage( $wgLogNames[$type] )->text() );
205 } else {
206 // Bogus log types? Perhaps an extension was removed.
207 return $type;
208 }
209 }
210
211 /**
212 * Get the log header for the given log type
213 *
214 * @todo handle missing log types
215 * @param string $type logtype
216 * @return String: headertext of this logtype
217 * @deprecated in 1.19, warnings in 1.21. Use getDescription()
218 */
219 public static function logHeader( $type ) {
220 global $wgLogHeaders;
221
222 return wfMessage( $wgLogHeaders[$type] )->parse();
223 }
224
225 /**
226 * Generate text for a log entry.
227 * Only LogFormatter should call this function.
228 *
229 * @param string $type log type
230 * @param string $action log action
231 * @param $title Mixed: Title object or null
232 * @param $skin Mixed: Skin object or null. If null, we want to use the wiki
233 * content language, since that will go to the IRC feed.
234 * @param array $params parameters
235 * @param $filterWikilinks Boolean: whether to filter wiki links
236 * @return HTML string
237 */
238 public static function actionText( $type, $action, $title = null, $skin = null,
239 $params = array(), $filterWikilinks = false
240 ) {
241 global $wgLang, $wgContLang, $wgLogActions;
242
243 if ( is_null( $skin ) ) {
244 $langObj = $wgContLang;
245 $langObjOrNull = null;
246 } else {
247 $langObj = $wgLang;
248 $langObjOrNull = $wgLang;
249 }
250
251 $key = "$type/$action";
252
253 if ( isset( $wgLogActions[$key] ) ) {
254 if ( is_null( $title ) ) {
255 $rv = wfMessage( $wgLogActions[$key] )->inLanguage( $langObj )->escaped();
256 } else {
257 $titleLink = self::getTitleLink( $type, $langObjOrNull, $title, $params );
258
259 if ( count( $params ) == 0 ) {
260 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $titleLink )->inLanguage( $langObj )->escaped();
261 } else {
262 $details = '';
263 array_unshift( $params, $titleLink );
264
265 // User suppression
266 if ( preg_match( '/^(block|suppress)\/(block|reblock)$/', $key ) ) {
267 if ( $skin ) {
268 // Localize the duration, and add a tooltip
269 // in English to help visitors from other wikis.
270 // The lrm is needed to make sure that the number
271 // is shown on the correct side of the tooltip text.
272 $durationTooltip = '&lrm;' . htmlspecialchars( $params[1] );
273 $params[1] = "<span class='blockExpiry' title='$durationTooltip'>" .
274 $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
275 } else {
276 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
277 }
278
279 $params[2] = isset( $params[2] ) ?
280 self::formatBlockFlags( $params[2], $langObj ) : '';
281 // Page protections
282 } elseif ( $type == 'protect' && count( $params ) == 3 ) {
283 // Restrictions and expiries
284 if ( $skin ) {
285 $details .= $wgLang->getDirMark() . htmlspecialchars( " {$params[1]}" );
286 } else {
287 $details .= " {$params[1]}";
288 }
289
290 // Cascading flag...
291 if ( $params[2] ) {
292 $text = wfMessage( 'protect-summary-cascade' )
293 ->inLanguage( $langObj )->text();
294 $details .= ' ';
295 $details .= wfMessage( 'brackets', $text )->inLanguage( $langObj )->text();
296
297 }
298 }
299
300 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $params )->inLanguage( $langObj )->escaped() . $details;
301 }
302 }
303 } else {
304 global $wgLogActionsHandlers;
305
306 if ( isset( $wgLogActionsHandlers[$key] ) ) {
307 $args = func_get_args();
308 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
309 } else {
310 wfDebug( "LogPage::actionText - unknown action $key\n" );
311 $rv = "$action";
312 }
313 }
314
315 // For the perplexed, this feature was added in r7855 by Erik.
316 // The feature was added because we liked adding [[$1]] in our log entries
317 // but the log entries are parsed as Wikitext on RecentChanges but as HTML
318 // on Special:Log. The hack is essentially that [[$1]] represented a link
319 // to the title in question. The first parameter to the HTML version (Special:Log)
320 // is that link in HTML form, and so this just gets rid of the ugly [[]].
321 // However, this is a horrible hack and it doesn't work like you expect if, say,
322 // you want to link to something OTHER than the title of the log entry.
323 // The real problem, which Erik was trying to fix (and it sort-of works now) is
324 // that the same messages are being treated as both wikitext *and* HTML.
325 if ( $filterWikilinks ) {
326 $rv = str_replace( '[[', '', $rv );
327 $rv = str_replace( ']]', '', $rv );
328 }
329
330 return $rv;
331 }
332
333 /**
334 * TODO document
335 * @param $type String
336 * @param $lang Language or null
337 * @param $title Title
338 * @param $params Array
339 * @return String
340 */
341 protected static function getTitleLink( $type, $lang, $title, &$params ) {
342 if ( !$lang ) {
343 return $title->getPrefixedText();
344 }
345
346 switch ( $type ) {
347 case 'move':
348 $titleLink = Linker::link(
349 $title,
350 htmlspecialchars( $title->getPrefixedText() ),
351 array(),
352 array( 'redirect' => 'no' )
353 );
354
355 $targetTitle = Title::newFromText( $params[0] );
356
357 if ( !$targetTitle ) {
358 # Workaround for broken database
359 $params[0] = htmlspecialchars( $params[0] );
360 } else {
361 $params[0] = Linker::link(
362 $targetTitle,
363 htmlspecialchars( $params[0] )
364 );
365 }
366 break;
367 case 'block':
368 if ( substr( $title->getText(), 0, 1 ) == '#' ) {
369 $titleLink = $title->getText();
370 } else {
371 // @todo Store the user identifier in the parameters
372 // to make this faster for future log entries
373 $id = User::idFromName( $title->getText() );
374 $titleLink = Linker::userLink( $id, $title->getText() )
375 . Linker::userToolLinks( $id, $title->getText(), false, Linker::TOOL_LINKS_NOBLOCK );
376 }
377 break;
378 case 'merge':
379 $titleLink = Linker::link(
380 $title,
381 $title->getPrefixedText(),
382 array(),
383 array( 'redirect' => 'no' )
384 );
385 $params[0] = Linker::link(
386 Title::newFromText( $params[0] ),
387 htmlspecialchars( $params[0] )
388 );
389 $params[1] = $lang->timeanddate( $params[1] );
390 break;
391 default:
392 if ( $title->isSpecialPage() ) {
393 list( $name, $par ) = SpecialPageFactory::resolveAlias( $title->getDBkey() );
394
395 # Use the language name for log titles, rather than Log/X
396 if ( $name == 'Log' ) {
397 $logPage = new LogPage( $par );
398 $titleLink = Linker::link( $title, $logPage->getName()->escaped() );
399 $titleLink = wfMessage( 'parentheses' )
400 ->inLanguage( $lang )
401 ->rawParams( $titleLink )
402 ->escaped();
403 } else {
404 $titleLink = Linker::link( $title );
405 }
406 } else {
407 $titleLink = Linker::link( $title );
408 }
409 }
410
411 return $titleLink;
412 }
413
414 /**
415 * Add a log entry
416 *
417 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
418 * @param $target Title object
419 * @param string $comment description associated
420 * @param array $params parameters passed later to wfMessage function
421 * @param $doer User object: the user doing the action
422 *
423 * @return int log_id of the inserted log entry
424 */
425 public function addEntry( $action, $target, $comment, $params = array(), $doer = null ) {
426 global $wgContLang;
427
428 if ( !is_array( $params ) ) {
429 $params = array( $params );
430 }
431
432 if ( $comment === null ) {
433 $comment = '';
434 }
435
436 # Trim spaces on user supplied text
437 $comment = trim( $comment );
438
439 # Truncate for whole multibyte characters.
440 $comment = $wgContLang->truncate( $comment, 255 );
441
442 $this->action = $action;
443 $this->target = $target;
444 $this->comment = $comment;
445 $this->params = LogPage::makeParamBlob( $params );
446
447 if ( $doer === null ) {
448 global $wgUser;
449 $doer = $wgUser;
450 } elseif ( !is_object( $doer ) ) {
451 $doer = User::newFromId( $doer );
452 }
453
454 $this->doer = $doer;
455
456 $logEntry = new ManualLogEntry( $this->type, $action );
457 $logEntry->setTarget( $target );
458 $logEntry->setPerformer( $doer );
459 $logEntry->setParameters( $params );
460
461 $formatter = LogFormatter::newFromEntry( $logEntry );
462 $context = RequestContext::newExtraneousContext( $target );
463 $formatter->setContext( $context );
464
465 $this->actionText = $formatter->getPlainActionText();
466 $this->ircActionText = $formatter->getIRCActionText();
467
468 return $this->saveContent();
469 }
470
471 /**
472 * Add relations to log_search table
473 *
474 * @param $field String
475 * @param $values Array
476 * @param $logid Integer
477 * @return Boolean
478 */
479 public function addRelations( $field, $values, $logid ) {
480 if ( !strlen( $field ) || empty( $values ) ) {
481 return false; // nothing
482 }
483
484 $data = array();
485
486 foreach ( $values as $value ) {
487 $data[] = array(
488 'ls_field' => $field,
489 'ls_value' => $value,
490 'ls_log_id' => $logid
491 );
492 }
493
494 $dbw = wfGetDB( DB_MASTER );
495 $dbw->insert( 'log_search', $data, __METHOD__, 'IGNORE' );
496
497 return true;
498 }
499
500 /**
501 * Create a blob from a parameter array
502 *
503 * @param $params Array
504 * @return String
505 */
506 public static function makeParamBlob( $params ) {
507 return implode( "\n", $params );
508 }
509
510 /**
511 * Extract a parameter array from a blob
512 *
513 * @param $blob String
514 * @return Array
515 */
516 public static function extractParams( $blob ) {
517 if ( $blob === '' ) {
518 return array();
519 } else {
520 return explode( "\n", $blob );
521 }
522 }
523
524 /**
525 * Convert a comma-delimited list of block log flags
526 * into a more readable (and translated) form
527 *
528 * @param string $flags Flags to format
529 * @param $lang Language object to use
530 * @return String
531 */
532 public static function formatBlockFlags( $flags, $lang ) {
533 $flags = trim( $flags );
534 if ( $flags === '' ) {
535 return ''; //nothing to do
536 }
537 $flags = explode( ',', $flags );
538
539 for ( $i = 0; $i < count( $flags ); $i++ ) {
540 $flags[$i] = self::formatBlockFlag( $flags[$i], $lang );
541 }
542
543 return wfMessage( 'parentheses' )->inLanguage( $lang )
544 ->rawParams( $lang->commaList( $flags ) )->escaped();
545 }
546
547 /**
548 * Translate a block log flag if possible
549 *
550 * @param int $flag Flag to translate
551 * @param $lang Language object to use
552 * @return String
553 */
554 public static function formatBlockFlag( $flag, $lang ) {
555 static $messages = array();
556
557 if ( !isset( $messages[$flag] ) ) {
558 $messages[$flag] = htmlspecialchars( $flag ); // Fallback
559
560 // For grepping. The following core messages can be used here:
561 // * block-log-flags-angry-autoblock
562 // * block-log-flags-anononly
563 // * block-log-flags-hiddenname
564 // * block-log-flags-noautoblock
565 // * block-log-flags-nocreate
566 // * block-log-flags-noemail
567 // * block-log-flags-nousertalk
568 $msg = wfMessage( 'block-log-flags-' . $flag )->inLanguage( $lang );
569
570 if ( $msg->exists() ) {
571 $messages[$flag] = $msg->escaped();
572 }
573 }
574
575 return $messages[$flag];
576 }
577
578 /**
579 * Name of the log.
580 * @return Message
581 * @since 1.19
582 */
583 public function getName() {
584 global $wgLogNames;
585
586 // BC
587 if ( isset( $wgLogNames[$this->type] ) ) {
588 $key = $wgLogNames[$this->type];
589 } else {
590 $key = 'log-name-' . $this->type;
591 }
592
593 return wfMessage( $key );
594 }
595
596 /**
597 * Description of this log type.
598 * @return Message
599 * @since 1.19
600 */
601 public function getDescription() {
602 global $wgLogHeaders;
603 // BC
604 if ( isset( $wgLogHeaders[$this->type] ) ) {
605 $key = $wgLogHeaders[$this->type];
606 } else {
607 $key = 'log-description-' . $this->type;
608 }
609
610 return wfMessage( $key );
611 }
612
613 /**
614 * Returns the right needed to read this log type.
615 * @return string
616 * @since 1.19
617 */
618 public function getRestriction() {
619 global $wgLogRestrictions;
620 if ( isset( $wgLogRestrictions[$this->type] ) ) {
621 $restriction = $wgLogRestrictions[$this->type];
622 } else {
623 // '' always returns true with $user->isAllowed()
624 $restriction = '';
625 }
626
627 return $restriction;
628 }
629
630 /**
631 * Tells if this log is not viewable by all.
632 * @return bool
633 * @since 1.19
634 */
635 public function isRestricted() {
636 $restriction = $this->getRestriction();
637
638 return $restriction !== '' && $restriction !== '*';
639 }
640 }