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