Tweak $udp param
[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 if not to RC
49 */
50 public function __construct( $type, $rc = true, $udp = 'UDP' ) {
51 $this->type = $type;
52 $this->updateRecentChanges = $rc;
53 $this->sendToUDP = ($udp == 'UDP');
54 }
55
56 protected function saveContent() {
57 global $wgLogRestrictions;
58
59 $dbw = wfGetDB( DB_MASTER );
60 $log_id = $dbw->nextSequenceValue( 'log_log_id_seq' );
61
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
73 );
74 $dbw->insert( 'logging', $data, __METHOD__ );
75 $newId = !is_null($log_id) ? $log_id : $dbw->insertId();
76
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;
86 }
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() );
95 }
96 }
97 return true;
98 }
99
100 /**
101 * Get the RC comment from the last addEntry() call
102 */
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;
110 }
111 return $rcComment;
112 }
113
114 /**
115 * Get the comment from the last addEntry() call
116 */
117 public function getComment() {
118 return $this->comment;
119 }
120
121 /**
122 * @static
123 */
124 public static function validTypes() {
125 global $wgLogTypes;
126 return $wgLogTypes;
127 }
128
129 /**
130 * @static
131 */
132 public static function isLogType( $type ) {
133 return in_array( $type, LogPage::validTypes() );
134 }
135
136 /**
137 * @static
138 */
139 public static function logName( $type ) {
140 global $wgLogNames, $wgMessageCache;
141
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;
148 }
149 }
150
151 /**
152 * @todo handle missing log types
153 * @param string $type logtype
154 * @return string Headertext of this logtype
155 */
156 public static function logHeader( $type ) {
157 global $wgLogHeaders, $wgMessageCache;
158 $wgMessageCache->loadAllMessages();
159 return wfMsgExt($wgLogHeaders[$type],array('parseinline'));
160 }
161
162 /**
163 * @static
164 * @return HTML string
165 */
166 public static function actionText( $type, $action, $title = NULL, $skin = NULL,
167 $params = array(), $filterWikilinks = false )
168 {
169 global $wgLang, $wgContLang, $wgLogActions, $wgMessageCache;
170
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 );
176 }
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 );
189 }
190 } else {
191 $rightsnone = wfMsgForContent( 'rightsnone' );
192 }
193 if( !isset( $params[0] ) || trim( $params[0] ) == '' )
194 $params[0] = $rightsnone;
195 if( !isset( $params[1] ) || trim( $params[1] ) == '' )
196 $params[1] = $rightsnone;
197 }
198 if( count( $params ) == 0 ) {
199 if ( $skin ) {
200 $rv = wfMsg( $wgLogActions[$key], $titleLink );
201 } else {
202 $rv = wfMsgForContent( $wgLogActions[$key], $titleLink );
203 }
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] );
213 }
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').']';
220 }
221 } else if ( $type == 'move' && count( $params ) == 3 ) {
222 if( $params[2] ) {
223 $details .= ' [' . wfMsg( 'move-redirect-suppressed' ) . ']';
224 }
225 }
226 $rv = wfMsgReal( $wgLogActions[$key], $params, true, !$skin ) . $details;
227 }
228 }
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";
237 }
238 }
239 if( $filterWikilinks ) {
240 $rv = str_replace( "[[", "", $rv );
241 $rv = str_replace( "]]", "", $rv );
242 }
243 return $rv;
244 }
245
246 protected static function getTitleLink( $type, $skin, $title, &$params ) {
247 global $wgLang, $wgContLang;
248 if( !$skin ) {
249 return $title->getPrefixedText();
250 }
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] ) );
261 }
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 );
272 }
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 );
291 }
292 } else {
293 $titleLink = $skin->makeLinkObj( $title );
294 }
295 }
296 return $titleLink;
297 }
298
299 /**
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
306 */
307 public function addEntry( $action, $target, $comment, $params = array(), $doer = null ) {
308 if ( !is_array( $params ) ) {
309 $params = array( $params );
310 }
311
312 $this->action = $action;
313 $this->target = $target;
314 $this->comment = $comment;
315 $this->params = LogPage::makeParamBlob( $params );
316
317 if ($doer === null) {
318 global $wgUser;
319 $doer = $wgUser;
320 } elseif (!is_object( $doer ) ) {
321 $doer = User::newFromId( $doer );
322 }
323
324 $this->doer = $doer;
325
326 $this->actionText = LogPage::actionText( $this->type, $action, $target, NULL, $params );
327
328 return $this->saveContent();
329 }
330
331 /**
332 * Create a blob from a parameter array
333 * @static
334 */
335 public static function makeParamBlob( $params ) {
336 return implode( "\n", $params );
337 }
338
339 /**
340 * Extract a parameter array from a blob
341 * @static
342 */
343 public static function extractParams( $blob ) {
344 if ( $blob === '' ) {
345 return array();
346 } else {
347 return explode( "\n", $blob );
348 }
349 }
350
351 /**
352 * Convert a comma-delimited list of block log flags
353 * into a more readable (and translated) form
354 *
355 * @param $flags Flags to format
356 * @param $forContent Whether to localize the message depending of the user
357 * language
358 * @return string
359 */
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 '';
368 }
369 }
370
371 /**
372 * Translate a block log flag if possible
373 *
374 * @param $flag Flag to translate
375 * @param $forContent Whether to localize the message depending of the user
376 * language
377 * @return string
378 */
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 );
388 }
389 return $messages[$flag];
390 }
391 }
392
393 /**
394 * Aliases for backwards compatibility with 1.6
395 */
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 );