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