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