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