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