Revert r45605 and generalize the ability to have logs go to UDP.
[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 /* @access private */
38 var $type, $action, $comment, $params, $target, $doer;
39 /* @acess public */
40 var $updateRecentChanges, $sendToUDP;
41
42 /**
43 * Constructor
44 *
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
49 */
50 function __construct( $type, $rc = true, $udp = true ) {
51 $this->type = $type;
52 $this->updateRecentChanges = $rc;
53 $this->sendToUDP = $udp;
54 }
55
56 protected function saveContent() {
57 global $wgUser, $wgLogRestrictions;
58 $fname = 'LogPage::saveContent';
59
60 $dbw = wfGetDB( DB_MASTER );
61 $log_id = $dbw->nextSequenceValue( 'log_log_id_seq' );
62
63 $this->timestamp = $now = wfTimestampNow();
64 $data = array(
65 'log_id' => $log_id,
66 'log_type' => $this->type,
67 'log_action' => $this->action,
68 'log_timestamp' => $dbw->timestamp( $now ),
69 'log_user' => $this->doer->getId(),
70 'log_namespace' => $this->target->getNamespace(),
71 'log_title' => $this->target->getDBkey(),
72 'log_comment' => $this->comment,
73 'log_params' => $this->params
74 );
75 $dbw->insert( 'logging', $data, $fname );
76 $newId = !is_null($log_id) ? $log_id : $dbw->insertId();
77
78 if( !($dbw->affectedRows() > 0) ) {
79 wfDebugLog( "logging", "LogPage::saveContent failed to insert row - Error {$dbw->lastErrno()}: {$dbw->lastError()}" );
80 }
81 # And update recentchanges
82 if( $this->updateRecentChanges ) {
83 # Don't add private logs to RC!
84 if( !isset($wgLogRestrictions[$this->type]) || $wgLogRestrictions[$this->type]=='*' ) {
85 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
86 $rcComment = $this->getRcComment();
87 RecentChange::notifyLog( $now, $titleObj, $this->doer, $rcComment, '',
88 $this->type, $this->action, $this->target, $this->comment, $this->params, $newId );
89 }
90 } else if( $this->sendToUDP ) {
91 # Notify external application via UDP.
92 # We send this to IRC but do not want to add it the RC table.
93 global $wgRC2UDPAddress, $wgRC2UDPOmitBots;
94 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
95 $rcComment = $this->getRcComment();
96 $rc = RecentChange::newLogEntry( $now, $titleObj, $this->doer, $rcComment, '',
97 $this->type, $this->action, $this->target, $this->comment, $this->params, $newId );
98 if( $wgRC2UDPAddress && ( !$rc->getAttribute('rc_bot') || !$wgRC2UDPOmitBots ) ) {
99 RecentChange::sendToUDP( $rc->getIRCLine() );
100 }
101 }
102 return true;
103 }
104
105 /**
106 * Get the RC comment from the last addEntry() call
107 */
108 public function getRcComment() {
109 $rcComment = $this->actionText;
110 if( '' != $this->comment ) {
111 if ($rcComment == '')
112 $rcComment = $this->comment;
113 else
114 $rcComment .= wfMsgForContent( 'colon-separator' ) . $this->comment;
115 }
116 return $rcComment;
117 }
118
119 /**
120 * Get the comment from the last addEntry() call
121 */
122 public function getComment() {
123 return $this->comment;
124 }
125
126 /**
127 * @static
128 */
129 public static function validTypes() {
130 global $wgLogTypes;
131 return $wgLogTypes;
132 }
133
134 /**
135 * @static
136 */
137 public static function isLogType( $type ) {
138 return in_array( $type, LogPage::validTypes() );
139 }
140
141 /**
142 * @static
143 */
144 public static function logName( $type ) {
145 global $wgLogNames, $wgMessageCache;
146
147 if( isset( $wgLogNames[$type] ) ) {
148 $wgMessageCache->loadAllMessages();
149 return str_replace( '_', ' ', wfMsg( $wgLogNames[$type] ) );
150 } else {
151 // Bogus log types? Perhaps an extension was removed.
152 return $type;
153 }
154 }
155
156 /**
157 * @todo handle missing log types
158 * @param string $type logtype
159 * @return string Headertext of this logtype
160 */
161 static function logHeader( $type ) {
162 global $wgLogHeaders, $wgMessageCache;
163 $wgMessageCache->loadAllMessages();
164 return wfMsgExt($wgLogHeaders[$type],array('parseinline'));
165 }
166
167 /**
168 * @static
169 * @return HTML string
170 */
171 static function actionText( $type, $action, $title = NULL, $skin = NULL,
172 $params = array(), $filterWikilinks = false )
173 {
174 global $wgLang, $wgContLang, $wgLogActions, $wgMessageCache;
175
176 $wgMessageCache->loadAllMessages();
177 $key = "$type/$action";
178 # Defer patrol log to PatrolLog class
179 if( $key == 'patrol/patrol' ) {
180 return PatrolLog::makeActionText( $title, $params, $skin );
181 }
182 if( isset( $wgLogActions[$key] ) ) {
183 if( is_null( $title ) ) {
184 $rv = wfMsg( $wgLogActions[$key] );
185 } else {
186 $titleLink = self::getTitleLink( $type, $skin, $title, $params );
187 if( $key == 'rights/rights' ) {
188 if( $skin ) {
189 $rightsnone = wfMsg( 'rightsnone' );
190 foreach ( $params as &$param ) {
191 $groupArray = array_map( 'trim', explode( ',', $param ) );
192 $groupArray = array_map( array( 'User', 'getGroupName' ), $groupArray );
193 $param = $wgLang->listToText( $groupArray );
194 }
195 } else {
196 $rightsnone = wfMsgForContent( 'rightsnone' );
197 }
198 if( !isset( $params[0] ) || trim( $params[0] ) == '' )
199 $params[0] = $rightsnone;
200 if( !isset( $params[1] ) || trim( $params[1] ) == '' )
201 $params[1] = $rightsnone;
202 }
203 if( count( $params ) == 0 ) {
204 if ( $skin ) {
205 $rv = wfMsg( $wgLogActions[$key], $titleLink );
206 } else {
207 $rv = wfMsgForContent( $wgLogActions[$key], $titleLink );
208 }
209 } else {
210 $details = '';
211 array_unshift( $params, $titleLink );
212 if ( $key == 'block/block' || $key == 'suppress/block' || $key == 'block/reblock' ) {
213 if ( $skin ) {
214 $params[1] = '<span title="' . htmlspecialchars( $params[1] ). '">' .
215 $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
216 } else {
217 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
218 }
219 $params[2] = isset( $params[2] ) ?
220 self::formatBlockFlags( $params[2], is_null( $skin ) ) : '';
221 } else if ( $type == 'protect' && count($params) == 3 ) {
222 $details .= " {$params[1]}"; // restrictions and expiries
223 if( $params[2] ) {
224 $details .= ' ['.wfMsg('protect-summary-cascade').']';
225 }
226 } else if ( $type == 'move' && count( $params ) == 3 ) {
227 if( $params[2] ) {
228 $details .= ' [' . wfMsg( 'move-redirect-suppressed' ) . ']';
229 }
230 }
231 $rv = wfMsgReal( $wgLogActions[$key], $params, true, !$skin ) . $details;
232 }
233 }
234 } else {
235 global $wgLogActionsHandlers;
236 if( isset( $wgLogActionsHandlers[$key] ) ) {
237 $args = func_get_args();
238 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
239 } else {
240 wfDebug( "LogPage::actionText - unknown action $key\n" );
241 $rv = "$action";
242 }
243 }
244 if( $filterWikilinks ) {
245 $rv = str_replace( "[[", "", $rv );
246 $rv = str_replace( "]]", "", $rv );
247 }
248 return $rv;
249 }
250
251 protected static function getTitleLink( $type, $skin, $title, &$params ) {
252 global $wgLang, $wgContLang;
253 if( !$skin ) {
254 return $title->getPrefixedText();
255 }
256 switch( $type ) {
257 case 'move':
258 $titleLink = $skin->makeLinkObj( $title,
259 htmlspecialchars( $title->getPrefixedText() ), 'redirect=no' );
260 $targetTitle = Title::newFromText( $params[0] );
261 if ( !$targetTitle ) {
262 # Workaround for broken database
263 $params[0] = htmlspecialchars( $params[0] );
264 } else {
265 $params[0] = $skin->makeLinkObj( $targetTitle, htmlspecialchars( $params[0] ) );
266 }
267 break;
268 case 'block':
269 if( substr( $title->getText(), 0, 1 ) == '#' ) {
270 $titleLink = $title->getText();
271 } else {
272 // TODO: Store the user identifier in the parameters
273 // to make this faster for future log entries
274 $id = User::idFromName( $title->getText() );
275 $titleLink = $skin->userLink( $id, $title->getText() )
276 . $skin->userToolLinks( $id, $title->getText(), false, Linker::TOOL_LINKS_NOBLOCK );
277 }
278 break;
279 case 'rights':
280 $text = $wgContLang->ucfirst( $title->getText() );
281 $titleLink = $skin->makeLinkObj( Title::makeTitle( NS_USER, $text ) );
282 break;
283 case 'merge':
284 $titleLink = $skin->makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
285 $params[0] = $skin->makeLinkObj( Title::newFromText( $params[0] ), htmlspecialchars( $params[0] ) );
286 $params[1] = $wgLang->timeanddate( $params[1] );
287 break;
288 default:
289 if( $title->getNamespace() == NS_SPECIAL ) {
290 list( $name, $par ) = SpecialPage::resolveAliasWithSubpage( $title->getDBKey() );
291 # Use the language name for log titles, rather than Log/X
292 if( $name == 'Log' ) {
293 $titleLink = '('.$skin->makeLinkObj( $title, LogPage::logName( $par ) ).')';
294 } else {
295 $titleLink = $skin->makeLinkObj( $title );
296 }
297 } else {
298 $titleLink = $skin->makeLinkObj( $title );
299 }
300 }
301 return $titleLink;
302 }
303
304 /**
305 * Add a log entry
306 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
307 * @param object &$target A title object.
308 * @param string $comment Description associated
309 * @param array $params Parameters passed later to wfMsg.* functions
310 * @param User $doer The user doing the action
311 */
312 function addEntry( $action, $target, $comment, $params = array(), $doer = null ) {
313 if ( !is_array( $params ) ) {
314 $params = array( $params );
315 }
316
317 $this->action = $action;
318 $this->target = $target;
319 $this->comment = $comment;
320 $this->params = LogPage::makeParamBlob( $params );
321
322 if ($doer === null) {
323 global $wgUser;
324 $doer = $wgUser;
325 } elseif (!is_object( $doer ) ) {
326 $doer = User::newFromId( $doer );
327 }
328
329 $this->doer = $doer;
330
331 $this->actionText = LogPage::actionText( $this->type, $action, $target, NULL, $params );
332
333 return $this->saveContent();
334 }
335
336 /**
337 * Create a blob from a parameter array
338 * @static
339 */
340 static function makeParamBlob( $params ) {
341 return implode( "\n", $params );
342 }
343
344 /**
345 * Extract a parameter array from a blob
346 * @static
347 */
348 static function extractParams( $blob ) {
349 if ( $blob === '' ) {
350 return array();
351 } else {
352 return explode( "\n", $blob );
353 }
354 }
355
356 /**
357 * Convert a comma-delimited list of block log flags
358 * into a more readable (and translated) form
359 *
360 * @param $flags Flags to format
361 * @param $forContent Whether to localize the message depending of the user
362 * language
363 * @return string
364 */
365 public static function formatBlockFlags( $flags, $forContent = false ) {
366 $flags = explode( ',', trim( $flags ) );
367 if( count( $flags ) > 0 ) {
368 for( $i = 0; $i < count( $flags ); $i++ )
369 $flags[$i] = self::formatBlockFlag( $flags[$i], $forContent );
370 return '(' . implode( ', ', $flags ) . ')';
371 } else {
372 return '';
373 }
374 }
375
376 /**
377 * Translate a block log flag if possible
378 *
379 * @param $flag Flag to translate
380 * @param $forContent Whether to localize the message depending of the user
381 * language
382 * @return string
383 */
384 public static function formatBlockFlag( $flag, $forContent = false ) {
385 static $messages = array();
386 if( !isset( $messages[$flag] ) ) {
387 $k = 'block-log-flags-' . $flag;
388 if( $forContent )
389 $msg = wfMsgForContent( $k );
390 else
391 $msg = wfMsg( $k );
392 $messages[$flag] = htmlspecialchars( wfEmptyMsg( $k, $msg ) ? $flag : $msg );
393 }
394 return $messages[$flag];
395 }
396 }
397
398 /**
399 * Aliases for backwards compatibility with 1.6
400 */
401 define( 'MW_LOG_DELETED_ACTION', LogPage::DELETED_ACTION );
402 define( 'MW_LOG_DELETED_USER', LogPage::DELETED_USER );
403 define( 'MW_LOG_DELETED_COMMENT', LogPage::DELETED_COMMENT );
404 define( 'MW_LOG_DELETED_RESTRICTED', LogPage::DELETED_RESTRICTED );