Normalise casing of getArticleID used in core
[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 bool|int|null
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 true;
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 foreach ( $params as &$param ) {
259 $groupArray = array_map( 'trim', explode( ',', $param ) );
260 $groupArray = array_map( array( 'User', 'getGroupMember' ), $groupArray );
261 $param = $wgLang->listToText( $groupArray );
262 }
263 }
264
265 if( !isset( $params[0] ) || trim( $params[0] ) == '' ) {
266 $params[0] = $rightsnone;
267 }
268
269 if( !isset( $params[1] ) || trim( $params[1] ) == '' ) {
270 $params[1] = $rightsnone;
271 }
272 }
273
274 if( count( $params ) == 0 ) {
275 $rv = wfMsgExt( $wgLogActions[$key], array( 'parsemag', 'escape', 'replaceafter', 'language' => $langObj ), $titleLink );
276 } else {
277 $details = '';
278 array_unshift( $params, $titleLink );
279
280 // User suppression
281 if ( preg_match( '/^(block|suppress)\/(block|reblock)$/', $key ) ) {
282 if ( $skin ) {
283 $params[1] = '<span class="blockExpiry" dir="ltr" title="' . htmlspecialchars( $params[1] ). '">' .
284 $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
285 } else {
286 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
287 }
288
289 $params[2] = isset( $params[2] ) ?
290 self::formatBlockFlags( $params[2], $langObj ) : '';
291 // Page protections
292 } elseif ( $type == 'protect' && count($params) == 3 ) {
293 // Restrictions and expiries
294 if( $skin ) {
295 $details .= $wgLang->getDirMark() . htmlspecialchars( " {$params[1]}" );
296 } else {
297 $details .= " {$params[1]}";
298 }
299
300 // Cascading flag...
301 if( $params[2] ) {
302 $details .= ' [' . wfMsgExt( 'protect-summary-cascade', array( 'parsemag', 'language' => $langObj ) ) . ']';
303 }
304 }
305
306 $rv = wfMsgExt( $wgLogActions[$key], array( 'parsemag', 'escape', 'replaceafter', 'language' => $langObj ), $params ) . $details;
307 }
308 }
309 } else {
310 global $wgLogActionsHandlers;
311
312 if( isset( $wgLogActionsHandlers[$key] ) ) {
313 $args = func_get_args();
314 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
315 } else {
316 wfDebug( "LogPage::actionText - unknown action $key\n" );
317 $rv = "$action";
318 }
319 }
320
321 // For the perplexed, this feature was added in r7855 by Erik.
322 // The feature was added because we liked adding [[$1]] in our log entries
323 // but the log entries are parsed as Wikitext on RecentChanges but as HTML
324 // on Special:Log. The hack is essentially that [[$1]] represented a link
325 // to the title in question. The first parameter to the HTML version (Special:Log)
326 // is that link in HTML form, and so this just gets rid of the ugly [[]].
327 // However, this is a horrible hack and it doesn't work like you expect if, say,
328 // you want to link to something OTHER than the title of the log entry.
329 // The real problem, which Erik was trying to fix (and it sort-of works now) is
330 // that the same messages are being treated as both wikitext *and* HTML.
331 if( $filterWikilinks ) {
332 $rv = str_replace( '[[', '', $rv );
333 $rv = str_replace( ']]', '', $rv );
334 }
335
336 return $rv;
337 }
338
339 /**
340 * TODO document
341 * @param $type String
342 * @param $lang Language or null
343 * @param $title Title
344 * @param $params Array
345 * @return String
346 */
347 protected static function getTitleLink( $type, $lang, $title, &$params ) {
348 global $wgContLang, $wgUserrightsInterwikiDelimiter;
349
350 if( !$lang ) {
351 return $title->getPrefixedText();
352 }
353
354 switch( $type ) {
355 case 'move':
356 $titleLink = Linker::link(
357 $title,
358 htmlspecialchars( $title->getPrefixedText() ),
359 array(),
360 array( 'redirect' => 'no' )
361 );
362
363 $targetTitle = Title::newFromText( $params[0] );
364
365 if ( !$targetTitle ) {
366 # Workaround for broken database
367 $params[0] = htmlspecialchars( $params[0] );
368 } else {
369 $params[0] = Linker::link(
370 $targetTitle,
371 htmlspecialchars( $params[0] )
372 );
373 }
374 break;
375 case 'block':
376 if( substr( $title->getText(), 0, 1 ) == '#' ) {
377 $titleLink = $title->getText();
378 } else {
379 // @todo Store the user identifier in the parameters
380 // to make this faster for future log entries
381 $id = User::idFromName( $title->getText() );
382 $titleLink = Linker::userLink( $id, $title->getText() )
383 . Linker::userToolLinks( $id, $title->getText(), false, Linker::TOOL_LINKS_NOBLOCK );
384 }
385 break;
386 case 'rights':
387 $text = $wgContLang->ucfirst( $title->getText() );
388 $parts = explode( $wgUserrightsInterwikiDelimiter, $text, 2 );
389
390 if ( count( $parts ) == 2 ) {
391 $titleLink = WikiMap::foreignUserLink( $parts[1], $parts[0],
392 htmlspecialchars( $title->getPrefixedText() ) );
393
394 if ( $titleLink !== false ) {
395 break;
396 }
397 }
398 $titleLink = Linker::link( Title::makeTitle( NS_USER, $text ) );
399 break;
400 case 'merge':
401 $titleLink = Linker::link(
402 $title,
403 $title->getPrefixedText(),
404 array(),
405 array( 'redirect' => 'no' )
406 );
407 $params[0] = Linker::link(
408 Title::newFromText( $params[0] ),
409 htmlspecialchars( $params[0] )
410 );
411 $params[1] = $lang->timeanddate( $params[1] );
412 break;
413 default:
414 if( $title->isSpecialPage() ) {
415 list( $name, $par ) = SpecialPageFactory::resolveAlias( $title->getDBkey() );
416
417 # Use the language name for log titles, rather than Log/X
418 if( $name == 'Log' ) {
419 $titleLink = Linker::link( $title, LogPage::logName( $par ) );
420 $titleLink = wfMessage( 'parentheses' )->rawParams( $titleLink )->escaped();
421 } else {
422 $titleLink = Linker::link( $title );
423 }
424 } else {
425 $titleLink = Linker::link( $title );
426 }
427 }
428
429 return $titleLink;
430 }
431
432 /**
433 * Add a log entry
434 *
435 * @param $action String: one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
436 * @param $target Title object
437 * @param $comment String: description associated
438 * @param $params Array: parameters passed later to wfMsg.* functions
439 * @param $doer User object: the user doing the action
440 *
441 * @return bool|int|null
442 * @TODO: make this use LogEntry::saveContent()
443 */
444 public function addEntry( $action, $target, $comment, $params = array(), $doer = null ) {
445 global $wgContLang;
446
447 if ( !is_array( $params ) ) {
448 $params = array( $params );
449 }
450
451 if ( $comment === null ) {
452 $comment = '';
453 }
454
455 # Truncate for whole multibyte characters.
456 $comment = $wgContLang->truncate( $comment, 255 );
457
458 $this->action = $action;
459 $this->target = $target;
460 $this->comment = $comment;
461 $this->params = LogPage::makeParamBlob( $params );
462
463 if ( $doer === null ) {
464 global $wgUser;
465 $doer = $wgUser;
466 } elseif ( !is_object( $doer ) ) {
467 $doer = User::newFromId( $doer );
468 }
469
470 $this->doer = $doer;
471
472 $logEntry = new ManualLogEntry( $this->type, $action );
473 $logEntry->setTarget( $target );
474 $logEntry->setPerformer( $doer );
475 $logEntry->setParameters( $params );
476
477 $formatter = LogFormatter::newFromEntry( $logEntry );
478 $context = RequestContext::newExtraneousContext( $target );
479 $formatter->setContext( $context );
480
481 $this->actionText = $formatter->getPlainActionText();
482 $this->ircActionText = $formatter->getIRCActionText();
483
484 return $this->saveContent();
485 }
486
487 /**
488 * Add relations to log_search table
489 *
490 * @param $field String
491 * @param $values Array
492 * @param $logid Integer
493 * @return Boolean
494 */
495 public function addRelations( $field, $values, $logid ) {
496 if( !strlen( $field ) || empty( $values ) ) {
497 return false; // nothing
498 }
499
500 $data = array();
501
502 foreach( $values as $value ) {
503 $data[] = array(
504 'ls_field' => $field,
505 'ls_value' => $value,
506 'ls_log_id' => $logid
507 );
508 }
509
510 $dbw = wfGetDB( DB_MASTER );
511 $dbw->insert( 'log_search', $data, __METHOD__, 'IGNORE' );
512
513 return true;
514 }
515
516 /**
517 * Create a blob from a parameter array
518 *
519 * @param $params Array
520 * @return String
521 */
522 public static function makeParamBlob( $params ) {
523 return implode( "\n", $params );
524 }
525
526 /**
527 * Extract a parameter array from a blob
528 *
529 * @param $blob String
530 * @return Array
531 */
532 public static function extractParams( $blob ) {
533 if ( $blob === '' ) {
534 return array();
535 } else {
536 return explode( "\n", $blob );
537 }
538 }
539
540 /**
541 * Convert a comma-delimited list of block log flags
542 * into a more readable (and translated) form
543 *
544 * @param $flags string Flags to format
545 * @param $lang Language object to use
546 * @return String
547 */
548 public static function formatBlockFlags( $flags, $lang ) {
549 $flags = explode( ',', trim( $flags ) );
550
551 if( count( $flags ) > 0 ) {
552 for( $i = 0; $i < count( $flags ); $i++ ) {
553 $flags[$i] = self::formatBlockFlag( $flags[$i], $lang );
554 }
555 return wfMessage( 'parentheses' )->rawParams( $lang->commaList( $flags ) )->escaped();
556 } else {
557 return '';
558 }
559 }
560
561 /**
562 * Translate a block log flag if possible
563 *
564 * @param $flag int Flag to translate
565 * @param $lang Language object to use
566 * @return String
567 */
568 public static function formatBlockFlag( $flag, $lang ) {
569 static $messages = array();
570
571 if( !isset( $messages[$flag] ) ) {
572 $messages[$flag] = htmlspecialchars( $flag ); // Fallback
573
574 // For grepping. The following core messages can be used here:
575 // * block-log-flags-angry-autoblock
576 // * block-log-flags-anononly
577 // * block-log-flags-hiddenname
578 // * block-log-flags-noautoblock
579 // * block-log-flags-nocreate
580 // * block-log-flags-noemail
581 // * block-log-flags-nousertalk
582 $msg = wfMessage( 'block-log-flags-' . $flag )->inLanguage( $lang );
583
584 if ( $msg->exists() ) {
585 $messages[$flag] = $msg->escaped();
586 }
587 }
588
589 return $messages[$flag];
590 }
591
592
593 /**
594 * Name of the log.
595 * @return Message
596 * @since 1.19
597 */
598 public function getName() {
599 global $wgLogNames;
600
601 // BC
602 if ( isset( $wgLogNames[$this->type] ) ) {
603 $key = $wgLogNames[$this->type];
604 } else {
605 $key = 'log-name-' . $this->type;
606 }
607
608 return wfMessage( $key );
609 }
610
611 /**
612 * Description of this log type.
613 * @return Message
614 * @since 1.19
615 */
616 public function getDescription() {
617 global $wgLogHeaders;
618 // BC
619 if ( isset( $wgLogHeaders[$this->type] ) ) {
620 $key = $wgLogHeaders[$this->type];
621 } else {
622 $key = 'log-description-' . $this->type;
623 }
624 return wfMessage( $key );
625 }
626
627 /**
628 * Returns the right needed to read this log type.
629 * @return string
630 * @since 1.19
631 */
632 public function getRestriction() {
633 global $wgLogRestrictions;
634 if ( isset( $wgLogRestrictions[$this->type] ) ) {
635 $restriction = $wgLogRestrictions[$this->type];
636 } else {
637 // '' always returns true with $user->isAllowed()
638 $restriction = '';
639 }
640 return $restriction;
641 }
642
643 /**
644 * Tells if this log is not viewable by all.
645 * @return bool
646 * @since 1.19
647 */
648 public function isRestricted() {
649 $restriction = $this->getRestriction();
650 return $restriction !== '' && $restriction !== '*';
651 }
652
653 }