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