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