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