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