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