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