Added notifyRC2UDP() convenience function
[mediawiki.git] / includes / LogPage.php
blob63f081ecc0d72c45b53e1beb930fedd790740204
1 <?php
3 # Copyright (C) 2002, 2004 Brion Vibber <brion@pobox.com>
4 # http://www.mediawiki.org/
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.
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.
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
21 /**
22 * Contain log classes
23 * @file
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.
32 class LogPage {
33 const DELETED_ACTION = 1;
34 const DELETED_COMMENT = 2;
35 const DELETED_USER = 4;
36 const DELETED_RESTRICTED = 8;
37 /* @access private */
38 var $type, $action, $comment, $params, $target, $doer;
39 /* @acess public */
40 var $updateRecentChanges, $sendToUDP;
42 /**
43 * Constructor
45 * @param string $type One of '', 'block', 'protect', 'rights', 'delete',
46 * 'upload', 'move'
47 * @param bool $rc Whether to update recent changes as well as the logging table
48 * @param bool $udp Whether to send to the UDP feed
50 public function __construct( $type, $rc = true, $udp = true ) {
51 $this->type = $type;
52 $this->updateRecentChanges = $rc;
53 $this->sendToUDP = $udp;
56 protected function saveContent() {
57 global $wgLogRestrictions;
59 $dbw = wfGetDB( DB_MASTER );
60 $log_id = $dbw->nextSequenceValue( 'log_log_id_seq' );
62 $this->timestamp = $now = wfTimestampNow();
63 $data = array(
64 'log_id' => $log_id,
65 'log_type' => $this->type,
66 'log_action' => $this->action,
67 'log_timestamp' => $dbw->timestamp( $now ),
68 'log_user' => $this->doer->getId(),
69 'log_namespace' => $this->target->getNamespace(),
70 'log_title' => $this->target->getDBkey(),
71 'log_comment' => $this->comment,
72 'log_params' => $this->params
74 $dbw->insert( 'logging', $data, __METHOD__ );
75 $newId = !is_null($log_id) ? $log_id : $dbw->insertId();
77 # And update recentchanges
78 if( $this->updateRecentChanges ) {
79 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
80 RecentChange::notifyLog( $now, $titleObj, $this->doer, $this->getRcComment(), '', $this->type,
81 $this->action, $this->target, $this->comment, $this->params, $newId );
82 } else if( $this->sendToUDP ) {
83 # Don't send private logs to UDP
84 if( isset($wgLogRestrictions[$this->type]) && $wgLogRestrictions[$this->type] !='*' ) {
85 return true;
87 # Notify external application via UDP.
88 # We send this to IRC but do not want to add it the RC table.
89 global $wgRC2UDPAddress, $wgRC2UDPOmitBots;
90 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
91 $rc = RecentChange::newLogEntry( $now, $titleObj, $this->doer, $this->getRcComment(), '',
92 $this->type, $this->action, $this->target, $this->comment, $this->params, $newId );
93 if( $wgRC2UDPAddress && ( !$rc->getAttribute('rc_bot') || !$wgRC2UDPOmitBots ) ) {
94 RecentChange::sendToUDP( $rc->getIRCLine() );
97 return true;
101 * Get the RC comment from the last addEntry() call
103 public function getRcComment() {
104 $rcComment = $this->actionText;
105 if( '' != $this->comment ) {
106 if ($rcComment == '')
107 $rcComment = $this->comment;
108 else
109 $rcComment .= wfMsgForContent( 'colon-separator' ) . $this->comment;
111 return $rcComment;
115 * Get the comment from the last addEntry() call
117 public function getComment() {
118 return $this->comment;
122 * @static
124 public static function validTypes() {
125 global $wgLogTypes;
126 return $wgLogTypes;
130 * @static
132 public static function isLogType( $type ) {
133 return in_array( $type, LogPage::validTypes() );
137 * @static
139 public static function logName( $type ) {
140 global $wgLogNames, $wgMessageCache;
142 if( isset( $wgLogNames[$type] ) ) {
143 $wgMessageCache->loadAllMessages();
144 return str_replace( '_', ' ', wfMsg( $wgLogNames[$type] ) );
145 } else {
146 // Bogus log types? Perhaps an extension was removed.
147 return $type;
152 * @todo handle missing log types
153 * @param string $type logtype
154 * @return string Headertext of this logtype
156 public static function logHeader( $type ) {
157 global $wgLogHeaders, $wgMessageCache;
158 $wgMessageCache->loadAllMessages();
159 return wfMsgExt($wgLogHeaders[$type],array('parseinline'));
163 * @static
164 * @return HTML string
166 public static function actionText( $type, $action, $title = NULL, $skin = NULL,
167 $params = array(), $filterWikilinks = false )
169 global $wgLang, $wgContLang, $wgLogActions, $wgMessageCache;
171 $wgMessageCache->loadAllMessages();
172 $key = "$type/$action";
173 # Defer patrol log to PatrolLog class
174 if( $key == 'patrol/patrol' ) {
175 return PatrolLog::makeActionText( $title, $params, $skin );
177 if( isset( $wgLogActions[$key] ) ) {
178 if( is_null( $title ) ) {
179 $rv = wfMsg( $wgLogActions[$key] );
180 } else {
181 $titleLink = self::getTitleLink( $type, $skin, $title, $params );
182 if( $key == 'rights/rights' ) {
183 if( $skin ) {
184 $rightsnone = wfMsg( 'rightsnone' );
185 foreach ( $params as &$param ) {
186 $groupArray = array_map( 'trim', explode( ',', $param ) );
187 $groupArray = array_map( array( 'User', 'getGroupName' ), $groupArray );
188 $param = $wgLang->listToText( $groupArray );
190 } else {
191 $rightsnone = wfMsgForContent( 'rightsnone' );
193 if( !isset( $params[0] ) || trim( $params[0] ) == '' )
194 $params[0] = $rightsnone;
195 if( !isset( $params[1] ) || trim( $params[1] ) == '' )
196 $params[1] = $rightsnone;
198 if( count( $params ) == 0 ) {
199 if ( $skin ) {
200 $rv = wfMsg( $wgLogActions[$key], $titleLink );
201 } else {
202 $rv = wfMsgForContent( $wgLogActions[$key], $titleLink );
204 } else {
205 $details = '';
206 array_unshift( $params, $titleLink );
207 if ( $key == 'block/block' || $key == 'suppress/block' || $key == 'block/reblock' ) {
208 if ( $skin ) {
209 $params[1] = '<span title="' . htmlspecialchars( $params[1] ). '">' .
210 $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
211 } else {
212 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
214 $params[2] = isset( $params[2] ) ?
215 self::formatBlockFlags( $params[2], is_null( $skin ) ) : '';
216 } else if ( $type == 'protect' && count($params) == 3 ) {
217 $details .= " {$params[1]}"; // restrictions and expiries
218 if( $params[2] ) {
219 $details .= ' ['.wfMsg('protect-summary-cascade').']';
221 } else if ( $type == 'move' && count( $params ) == 3 ) {
222 if( $params[2] ) {
223 $details .= ' [' . wfMsg( 'move-redirect-suppressed' ) . ']';
226 $rv = wfMsgReal( $wgLogActions[$key], $params, true, !$skin ) . $details;
229 } else {
230 global $wgLogActionsHandlers;
231 if( isset( $wgLogActionsHandlers[$key] ) ) {
232 $args = func_get_args();
233 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
234 } else {
235 wfDebug( "LogPage::actionText - unknown action $key\n" );
236 $rv = "$action";
239 if( $filterWikilinks ) {
240 $rv = str_replace( "[[", "", $rv );
241 $rv = str_replace( "]]", "", $rv );
243 return $rv;
246 protected static function getTitleLink( $type, $skin, $title, &$params ) {
247 global $wgLang, $wgContLang;
248 if( !$skin ) {
249 return $title->getPrefixedText();
251 switch( $type ) {
252 case 'move':
253 $titleLink = $skin->makeLinkObj( $title,
254 htmlspecialchars( $title->getPrefixedText() ), 'redirect=no' );
255 $targetTitle = Title::newFromText( $params[0] );
256 if ( !$targetTitle ) {
257 # Workaround for broken database
258 $params[0] = htmlspecialchars( $params[0] );
259 } else {
260 $params[0] = $skin->makeLinkObj( $targetTitle, htmlspecialchars( $params[0] ) );
262 break;
263 case 'block':
264 if( substr( $title->getText(), 0, 1 ) == '#' ) {
265 $titleLink = $title->getText();
266 } else {
267 // TODO: Store the user identifier in the parameters
268 // to make this faster for future log entries
269 $id = User::idFromName( $title->getText() );
270 $titleLink = $skin->userLink( $id, $title->getText() )
271 . $skin->userToolLinks( $id, $title->getText(), false, Linker::TOOL_LINKS_NOBLOCK );
273 break;
274 case 'rights':
275 $text = $wgContLang->ucfirst( $title->getText() );
276 $titleLink = $skin->makeLinkObj( Title::makeTitle( NS_USER, $text ) );
277 break;
278 case 'merge':
279 $titleLink = $skin->makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
280 $params[0] = $skin->makeLinkObj( Title::newFromText( $params[0] ), htmlspecialchars( $params[0] ) );
281 $params[1] = $wgLang->timeanddate( $params[1] );
282 break;
283 default:
284 if( $title->getNamespace() == NS_SPECIAL ) {
285 list( $name, $par ) = SpecialPage::resolveAliasWithSubpage( $title->getDBKey() );
286 # Use the language name for log titles, rather than Log/X
287 if( $name == 'Log' ) {
288 $titleLink = '('.$skin->makeLinkObj( $title, LogPage::logName( $par ) ).')';
289 } else {
290 $titleLink = $skin->makeLinkObj( $title );
292 } else {
293 $titleLink = $skin->makeLinkObj( $title );
296 return $titleLink;
300 * Add a log entry
301 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
302 * @param object &$target A title object.
303 * @param string $comment Description associated
304 * @param array $params Parameters passed later to wfMsg.* functions
305 * @param User $doer The user doing the action
307 public function addEntry( $action, $target, $comment, $params = array(), $doer = null ) {
308 if ( !is_array( $params ) ) {
309 $params = array( $params );
312 $this->action = $action;
313 $this->target = $target;
314 $this->comment = $comment;
315 $this->params = LogPage::makeParamBlob( $params );
317 if ($doer === null) {
318 global $wgUser;
319 $doer = $wgUser;
320 } elseif (!is_object( $doer ) ) {
321 $doer = User::newFromId( $doer );
324 $this->doer = $doer;
326 $this->actionText = LogPage::actionText( $this->type, $action, $target, NULL, $params );
328 return $this->saveContent();
332 * Create a blob from a parameter array
333 * @static
335 public static function makeParamBlob( $params ) {
336 return implode( "\n", $params );
340 * Extract a parameter array from a blob
341 * @static
343 public static function extractParams( $blob ) {
344 if ( $blob === '' ) {
345 return array();
346 } else {
347 return explode( "\n", $blob );
352 * Convert a comma-delimited list of block log flags
353 * into a more readable (and translated) form
355 * @param $flags Flags to format
356 * @param $forContent Whether to localize the message depending of the user
357 * language
358 * @return string
360 public static function formatBlockFlags( $flags, $forContent = false ) {
361 $flags = explode( ',', trim( $flags ) );
362 if( count( $flags ) > 0 ) {
363 for( $i = 0; $i < count( $flags ); $i++ )
364 $flags[$i] = self::formatBlockFlag( $flags[$i], $forContent );
365 return '(' . implode( ', ', $flags ) . ')';
366 } else {
367 return '';
372 * Translate a block log flag if possible
374 * @param $flag Flag to translate
375 * @param $forContent Whether to localize the message depending of the user
376 * language
377 * @return string
379 public static function formatBlockFlag( $flag, $forContent = false ) {
380 static $messages = array();
381 if( !isset( $messages[$flag] ) ) {
382 $k = 'block-log-flags-' . $flag;
383 if( $forContent )
384 $msg = wfMsgForContent( $k );
385 else
386 $msg = wfMsg( $k );
387 $messages[$flag] = htmlspecialchars( wfEmptyMsg( $k, $msg ) ? $flag : $msg );
389 return $messages[$flag];
394 * Aliases for backwards compatibility with 1.6
396 define( 'MW_LOG_DELETED_ACTION', LogPage::DELETED_ACTION );
397 define( 'MW_LOG_DELETED_USER', LogPage::DELETED_USER );
398 define( 'MW_LOG_DELETED_COMMENT', LogPage::DELETED_COMMENT );
399 define( 'MW_LOG_DELETED_RESTRICTED', LogPage::DELETED_RESTRICTED );