Don't look for pipes in the root node.
[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, $target, $doer;
42 /* @acess public */
43 var $updateRecentChanges, $sendToUDP;
44
45 /**
46 * Constructor
47 *
48 * @param $type String: one of '', 'block', 'protect', 'rights', 'delete',
49 * 'upload', 'move'
50 * @param $rc Boolean: whether to update recent changes as well as the logging table
51 * @param $udp String: pass 'UDP' to send to the UDP feed if NOT sent to RC
52 */
53 public function __construct( $type, $rc = true, $udp = 'skipUDP' ) {
54 $this->type = $type;
55 $this->updateRecentChanges = $rc;
56 $this->sendToUDP = ( $udp == 'UDP' );
57 }
58
59 protected function saveContent() {
60 global $wgLogRestrictions;
61
62 $dbw = wfGetDB( DB_MASTER );
63 $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
64
65 $this->timestamp = $now = wfTimestampNow();
66 $data = array(
67 'log_id' => $log_id,
68 'log_type' => $this->type,
69 'log_action' => $this->action,
70 'log_timestamp' => $dbw->timestamp( $now ),
71 'log_user' => $this->doer->getId(),
72 'log_user_text' => $this->doer->getName(),
73 'log_namespace' => $this->target->getNamespace(),
74 'log_title' => $this->target->getDBkey(),
75 'log_page' => $this->target->getArticleId(),
76 'log_comment' => $this->comment,
77 'log_params' => $this->params
78 );
79 $dbw->insert( 'logging', $data, __METHOD__ );
80 $newId = !is_null( $log_id ) ? $log_id : $dbw->insertId();
81
82 # And update recentchanges
83 if( $this->updateRecentChanges ) {
84 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
85 RecentChange::notifyLog(
86 $now, $titleObj, $this->doer, $this->getRcComment(), '',
87 $this->type, $this->action, $this->target, $this->comment,
88 $this->params, $newId
89 );
90 } elseif( $this->sendToUDP ) {
91 # Don't send private logs to UDP
92 if( isset( $wgLogRestrictions[$this->type] ) && $wgLogRestrictions[$this->type] != '*' ) {
93 return true;
94 }
95 # Notify external application via UDP.
96 # We send this to IRC but do not want to add it the RC table.
97 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
98 $rc = RecentChange::newLogEntry(
99 $now, $titleObj, $this->doer, $this->getRcComment(), '',
100 $this->type, $this->action, $this->target, $this->comment,
101 $this->params, $newId
102 );
103 $rc->notifyRC2UDP();
104 }
105 return $newId;
106 }
107
108 /**
109 * Get the RC comment from the last addEntry() call
110 */
111 public function getRcComment() {
112 $rcComment = $this->actionText;
113 if( $this->comment != '' ) {
114 if ( $rcComment == '' ) {
115 $rcComment = $this->comment;
116 } else {
117 $rcComment .= wfMsgForContent( 'colon-separator' ) . $this->comment;
118 }
119 }
120 return $rcComment;
121 }
122
123 /**
124 * Get the comment from the last addEntry() call
125 */
126 public function getComment() {
127 return $this->comment;
128 }
129
130 /**
131 * Get the list of valid log types
132 *
133 * @return Array of strings
134 */
135 public static function validTypes() {
136 global $wgLogTypes;
137 return $wgLogTypes;
138 }
139
140 /**
141 * Is $type a valid log type
142 *
143 * @param $type String: log type to check
144 * @return Boolean
145 */
146 public static function isLogType( $type ) {
147 return in_array( $type, LogPage::validTypes() );
148 }
149
150 /**
151 * Get the name for the given log type
152 *
153 * @param $type String: logtype
154 * @return String: log name
155 */
156 public static function logName( $type ) {
157 global $wgLogNames;
158
159 if( isset( $wgLogNames[$type] ) ) {
160 return str_replace( '_', ' ', wfMsg( $wgLogNames[$type] ) );
161 } else {
162 // Bogus log types? Perhaps an extension was removed.
163 return $type;
164 }
165 }
166
167 /**
168 * Get the log header for the given log type
169 *
170 * @todo handle missing log types
171 * @param $type String: logtype
172 * @return String: headertext of this logtype
173 */
174 public static function logHeader( $type ) {
175 global $wgLogHeaders;
176 return wfMsgExt( $wgLogHeaders[$type], array( 'parseinline' ) );
177 }
178
179 /**
180 * Generate text for a log entry
181 *
182 * @param $type String: log type
183 * @param $action String: log action
184 * @param $title Mixed: Title object or null
185 * @param $skin Mixed: Skin object or null. If null, we want to use the wiki
186 * content language, since that will go to the IRC feed.
187 * @param $params Array: parameters
188 * @param $filterWikilinks Boolean: whether to filter wiki links
189 * @return HTML string
190 */
191 public static function actionText( $type, $action, $title = null, $skin = null,
192 $params = array(), $filterWikilinks = false )
193 {
194 global $wgLang, $wgContLang, $wgLogActions;
195
196 $key = "$type/$action";
197 # Defer patrol log to PatrolLog class
198 if( $key == 'patrol/patrol' ) {
199 return PatrolLog::makeActionText( $title, $params, $skin );
200 }
201 if( isset( $wgLogActions[$key] ) ) {
202 if( is_null( $title ) ) {
203 $rv = wfMsgHtml( $wgLogActions[$key] );
204 } else {
205 $titleLink = self::getTitleLink( $type, $skin, $title, $params );
206 if( $key == 'rights/rights' ) {
207 if( $skin ) {
208 $rightsnone = wfMsg( 'rightsnone' );
209 foreach ( $params as &$param ) {
210 $groupArray = array_map( 'trim', explode( ',', $param ) );
211 $groupArray = array_map( array( 'User', 'getGroupMember' ), $groupArray );
212 $param = $wgLang->listToText( $groupArray );
213 }
214 } else {
215 $rightsnone = wfMsgForContent( 'rightsnone' );
216 }
217 if( !isset( $params[0] ) || trim( $params[0] ) == '' ) {
218 $params[0] = $rightsnone;
219 }
220 if( !isset( $params[1] ) || trim( $params[1] ) == '' ) {
221 $params[1] = $rightsnone;
222 }
223 }
224 if( count( $params ) == 0 ) {
225 if ( $skin ) {
226 $rv = wfMsgHtml( $wgLogActions[$key], $titleLink );
227 } else {
228 $rv = wfMsgExt( $wgLogActions[$key], array( 'parsemag', 'escape', 'replaceafter', 'content' ), $titleLink );
229 }
230 } else {
231 $details = '';
232 array_unshift( $params, $titleLink );
233 // User suppression
234 if ( preg_match( '/^(block|suppress)\/(block|reblock)$/', $key ) ) {
235 if ( $skin ) {
236 $params[1] = '<span class="blockExpiry" dir="ltr" title="' . htmlspecialchars( $params[1] ). '">' .
237 $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
238 } else {
239 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
240 }
241 $params[2] = isset( $params[2] ) ?
242 self::formatBlockFlags( $params[2], is_null( $skin ) ) : '';
243
244 // Page protections
245 } elseif ( $type == 'protect' && count($params) == 3 ) {
246 // Restrictions and expiries
247 if( $skin ) {
248 $details .= htmlspecialchars( " {$params[1]}" );
249 } else {
250 $details .= " {$params[1]}";
251 }
252 // Cascading flag...
253 if( $params[2] ) {
254 if ( $skin ) {
255 $details .= ' [' . wfMsg( 'protect-summary-cascade' ) . ']';
256 } else {
257 $details .= ' [' . wfMsgForContent( 'protect-summary-cascade' ) . ']';
258 }
259 }
260
261 // Page moves
262 } elseif ( $type == 'move' && count( $params ) == 3 && $action != 'move_rev' ) {
263 if( $params[2] ) {
264 if ( $skin ) {
265 $details .= ' [' . wfMsg( 'move-redirect-suppressed' ) . ']';
266 } else {
267 $details .= ' [' . wfMsgForContent( 'move-redirect-suppressed' ) . ']';
268 }
269 }
270
271 // Revision deletion
272 } elseif ( preg_match( '/^(delete|suppress)\/revision$/', $key ) && count( $params ) == 5 ) {
273 $count = substr_count( $params[2], ',' ) + 1; // revisions
274 $ofield = intval( substr( $params[3], 7 ) ); // <ofield=x>
275 $nfield = intval( substr( $params[4], 7 ) ); // <nfield=x>
276 $details .= ': ' . RevisionDeleter::getLogMessage( $count, $nfield, $ofield, false, is_null( $skin ) );
277
278 // Log deletion
279 } elseif ( preg_match( '/^(delete|suppress)\/event$/', $key ) && count( $params ) == 4 ) {
280 $count = substr_count( $params[1], ',' ) + 1; // log items
281 $ofield = intval( substr( $params[2], 7 ) ); // <ofield=x>
282 $nfield = intval( substr( $params[3], 7 ) ); // <nfield=x>
283 $details .= ': ' . RevisionDeleter::getLogMessage( $count, $nfield, $ofield, true, is_null( $skin ) );
284 }
285
286 if ( $skin ) {
287 $rv = wfMsgHtml( $wgLogActions[$key], $params ) . $details;
288 } else {
289 $rv = wfMsgExt( $wgLogActions[$key], array( 'parsemag', 'escape', 'replaceafter', 'content' ), $params ) . $details;
290 }
291 }
292 }
293 } else {
294 global $wgLogActionsHandlers;
295 if( isset( $wgLogActionsHandlers[$key] ) ) {
296 $args = func_get_args();
297 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
298 } else {
299 wfDebug( "LogPage::actionText - unknown action $key\n" );
300 $rv = "$action";
301 }
302 }
303
304 // For the perplexed, this feature was added in r7855 by Erik.
305 // The feature was added because we liked adding [[$1]] in our log entries
306 // but the log entries are parsed as Wikitext on RecentChanges but as HTML
307 // on Special:Log. The hack is essentially that [[$1]] represented a link
308 // to the title in question. The first parameter to the HTML version (Special:Log)
309 // is that link in HTML form, and so this just gets rid of the ugly [[]].
310 // However, this is a horrible hack and it doesn't work like you expect if, say,
311 // you want to link to something OTHER than the title of the log entry.
312 // The real problem, which Erik was trying to fix (and it sort-of works now) is
313 // that the same messages are being treated as both wikitext *and* HTML.
314 if( $filterWikilinks ) {
315 $rv = str_replace( '[[', '', $rv );
316 $rv = str_replace( ']]', '', $rv );
317 }
318 return $rv;
319 }
320
321 protected static function getTitleLink( $type, $skin, $title, &$params ) {
322 global $wgLang, $wgContLang, $wgUserrightsInterwikiDelimiter;
323 if( !$skin ) {
324 return $title->getPrefixedText();
325 }
326 switch( $type ) {
327 case 'move':
328 $titleLink = $skin->link(
329 $title,
330 htmlspecialchars( $title->getPrefixedText() ),
331 array(),
332 array( 'redirect' => 'no' )
333 );
334 $targetTitle = Title::newFromText( $params[0] );
335 if ( !$targetTitle ) {
336 # Workaround for broken database
337 $params[0] = htmlspecialchars( $params[0] );
338 } else {
339 $params[0] = $skin->link(
340 $targetTitle,
341 htmlspecialchars( $params[0] )
342 );
343 }
344 break;
345 case 'block':
346 if( substr( $title->getText(), 0, 1 ) == '#' ) {
347 $titleLink = $title->getText();
348 } else {
349 // TODO: Store the user identifier in the parameters
350 // to make this faster for future log entries
351 $id = User::idFromName( $title->getText() );
352 $titleLink = $skin->userLink( $id, $title->getText() )
353 . $skin->userToolLinks( $id, $title->getText(), false, Linker::TOOL_LINKS_NOBLOCK );
354 }
355 break;
356 case 'rights':
357 $text = $wgContLang->ucfirst( $title->getText() );
358 $parts = explode( $wgUserrightsInterwikiDelimiter, $text, 2 );
359 if ( count( $parts ) == 2 ) {
360 $titleLink = WikiMap::foreignUserLink( $parts[1], $parts[0],
361 htmlspecialchars( $title->getPrefixedText() ) );
362 if ( $titleLink !== false ) {
363 break;
364 }
365 }
366 $titleLink = $skin->link( Title::makeTitle( NS_USER, $text ) );
367 break;
368 case 'merge':
369 $titleLink = $skin->link(
370 $title,
371 $title->getPrefixedText(),
372 array(),
373 array( 'redirect' => 'no' )
374 );
375 $params[0] = $skin->link(
376 Title::newFromText( $params[0] ),
377 htmlspecialchars( $params[0] )
378 );
379 $params[1] = $wgLang->timeanddate( $params[1] );
380 break;
381 default:
382 if( $title->getNamespace() == NS_SPECIAL ) {
383 list( $name, $par ) = SpecialPage::resolveAliasWithSubpage( $title->getDBkey() );
384 # Use the language name for log titles, rather than Log/X
385 if( $name == 'Log' ) {
386 $titleLink = '(' . $skin->link( $title, LogPage::logName( $par ) ) . ')';
387 } else {
388 $titleLink = $skin->link( $title );
389 }
390 } else {
391 $titleLink = $skin->link( $title );
392 }
393 }
394 return $titleLink;
395 }
396
397 /**
398 * Add a log entry
399 *
400 * @param $action String: one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
401 * @param $target Title object
402 * @param $comment String: description associated
403 * @param $params Array: parameters passed later to wfMsg.* functions
404 * @param $doer User object: the user doing the action
405 */
406 public function addEntry( $action, $target, $comment, $params = array(), $doer = null ) {
407 if ( !is_array( $params ) ) {
408 $params = array( $params );
409 }
410
411 if ( $comment === null ) {
412 $comment = '';
413 }
414
415 $this->action = $action;
416 $this->target = $target;
417 $this->comment = $comment;
418 $this->params = LogPage::makeParamBlob( $params );
419
420 if ( $doer === null ) {
421 global $wgUser;
422 $doer = $wgUser;
423 } elseif ( !is_object( $doer ) ) {
424 $doer = User::newFromId( $doer );
425 }
426
427 $this->doer = $doer;
428
429 $this->actionText = LogPage::actionText( $this->type, $action, $target, null, $params );
430
431 return $this->saveContent();
432 }
433
434 /**
435 * Add relations to log_search table
436 *
437 * @param $field String
438 * @param $values Array
439 * @param $logid Integer
440 * @return Boolean
441 */
442 public function addRelations( $field, $values, $logid ) {
443 if( !strlen( $field ) || empty( $values ) ) {
444 return false; // nothing
445 }
446 $data = array();
447 foreach( $values as $value ) {
448 $data[] = array(
449 'ls_field' => $field,
450 'ls_value' => $value,
451 'ls_log_id' => $logid
452 );
453 }
454 $dbw = wfGetDB( DB_MASTER );
455 $dbw->insert( 'log_search', $data, __METHOD__, 'IGNORE' );
456 return true;
457 }
458
459 /**
460 * Create a blob from a parameter array
461 *
462 * @param $params Array
463 * @return String
464 */
465 public static function makeParamBlob( $params ) {
466 return implode( "\n", $params );
467 }
468
469 /**
470 * Extract a parameter array from a blob
471 *
472 * @param $blob String
473 * @return Array
474 */
475 public static function extractParams( $blob ) {
476 if ( $blob === '' ) {
477 return array();
478 } else {
479 return explode( "\n", $blob );
480 }
481 }
482
483 /**
484 * Convert a comma-delimited list of block log flags
485 * into a more readable (and translated) form
486 *
487 * @param $flags Flags to format
488 * @param $forContent Whether to localize the message depending of the user
489 * language
490 * @return String
491 */
492 public static function formatBlockFlags( $flags, $forContent = false ) {
493 global $wgLang;
494
495 $flags = explode( ',', trim( $flags ) );
496 if( count( $flags ) > 0 ) {
497 for( $i = 0; $i < count( $flags ); $i++ ) {
498 $flags[$i] = self::formatBlockFlag( $flags[$i], $forContent );
499 }
500 return '(' . $wgLang->commaList( $flags ) . ')';
501 } else {
502 return '';
503 }
504 }
505
506 /**
507 * Translate a block log flag if possible
508 *
509 * @param $flag Flag to translate
510 * @param $forContent Whether to localize the message depending of the user
511 * language
512 * @return String
513 */
514 public static function formatBlockFlag( $flag, $forContent = false ) {
515 static $messages = array();
516 if( !isset( $messages[$flag] ) ) {
517 $messages[$flag] = htmlspecialchars( $flag ); // Fallback
518
519 $msg = wfMessage( 'block-log-flags-' . $flag );
520 if ( $forContent ) {
521 $msg->inContentLanguage();
522 }
523 if ( $msg->exists() ) {
524 $messages[$flag] = $msg->escaped();
525 }
526 }
527 return $messages[$flag];
528 }
529 }
530
531 /**
532 * Aliases for backwards compatibility with 1.6
533 */
534 define( 'MW_LOG_DELETED_ACTION', LogPage::DELETED_ACTION );
535 define( 'MW_LOG_DELETED_USER', LogPage::DELETED_USER );
536 define( 'MW_LOG_DELETED_COMMENT', LogPage::DELETED_COMMENT );
537 define( 'MW_LOG_DELETED_RESTRICTED', LogPage::DELETED_RESTRICTED );