Initialise wgDBadminuser and wgDBadminpassword to null in DefaultSettings.php, to...
[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 string $type One of '', 'block', 'protect', 'rights', 'delete',
49 * 'upload', 'move'
50 * @param bool $rc Whether to update recent changes as well as the logging table
51 * @param bool $udp Whether 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( 'log_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_comment' => $this->comment,
76 'log_params' => $this->params
77 );
78 $dbw->insert( 'logging', $data, __METHOD__ );
79 $newId = !is_null($log_id) ? $log_id : $dbw->insertId();
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 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
94 $rc = RecentChange::newLogEntry( $now, $titleObj, $this->doer, $this->getRcComment(), '',
95 $this->type, $this->action, $this->target, $this->comment, $this->params, $newId );
96 $rc->notifyRC2UDP();
97 }
98 return $newId;
99 }
100
101 /**
102 * Get the RC comment from the last addEntry() call
103 */
104 public function getRcComment() {
105 $rcComment = $this->actionText;
106 if( '' != $this->comment ) {
107 if ($rcComment == '')
108 $rcComment = $this->comment;
109 else
110 $rcComment .= wfMsgForContent( 'colon-separator' ) . $this->comment;
111 }
112 return $rcComment;
113 }
114
115 /**
116 * Get the comment from the last addEntry() call
117 */
118 public function getComment() {
119 return $this->comment;
120 }
121
122 /**
123 * @static
124 */
125 public static function validTypes() {
126 global $wgLogTypes;
127 return $wgLogTypes;
128 }
129
130 /**
131 * @static
132 */
133 public static function isLogType( $type ) {
134 return in_array( $type, LogPage::validTypes() );
135 }
136
137 /**
138 * @static
139 * @param string $type logtype
140 */
141 public static function logName( $type ) {
142 global $wgLogNames, $wgMessageCache;
143
144 if( isset( $wgLogNames[$type] ) ) {
145 $wgMessageCache->loadAllMessages();
146 return str_replace( '_', ' ', wfMsg( $wgLogNames[$type] ) );
147 } else {
148 // Bogus log types? Perhaps an extension was removed.
149 return $type;
150 }
151 }
152
153 /**
154 * @todo handle missing log types
155 * @param string $type logtype
156 * @return string Headertext of this logtype
157 */
158 public static function logHeader( $type ) {
159 global $wgLogHeaders, $wgMessageCache;
160 $wgMessageCache->loadAllMessages();
161 return wfMsgExt($wgLogHeaders[$type],array('parseinline'));
162 }
163
164 /**
165 * @static
166 * @return HTML string
167 */
168 public static function actionText( $type, $action, $title = NULL, $skin = NULL,
169 $params = array(), $filterWikilinks = false )
170 {
171 global $wgLang, $wgContLang, $wgLogActions, $wgMessageCache;
172
173 $wgMessageCache->loadAllMessages();
174 $key = "$type/$action";
175 # Defer patrol log to PatrolLog class
176 if( $key == 'patrol/patrol' ) {
177 return PatrolLog::makeActionText( $title, $params, $skin );
178 }
179 if( isset( $wgLogActions[$key] ) ) {
180 if( is_null( $title ) ) {
181 $rv = wfMsgHtml( $wgLogActions[$key] );
182 } else {
183 $titleLink = self::getTitleLink( $type, $skin, $title, $params );
184 if( $key == 'rights/rights' ) {
185 if( $skin ) {
186 $rightsnone = wfMsg( 'rightsnone' );
187 foreach ( $params as &$param ) {
188 $groupArray = array_map( 'trim', explode( ',', $param ) );
189 $groupArray = array_map( array( 'User', 'getGroupName' ), $groupArray );
190 $param = $wgLang->listToText( $groupArray );
191 }
192 } else {
193 $rightsnone = wfMsgForContent( 'rightsnone' );
194 }
195 if( !isset( $params[0] ) || trim( $params[0] ) == '' )
196 $params[0] = $rightsnone;
197 if( !isset( $params[1] ) || trim( $params[1] ) == '' )
198 $params[1] = $rightsnone;
199 }
200 if( count( $params ) == 0 ) {
201 if ( $skin ) {
202 $rv = wfMsgHtml( $wgLogActions[$key], $titleLink );
203 } else {
204 $rv = wfMsgExt( $wgLogActions[$key], array( 'parsemag', 'escape', 'replaceafter', 'content' ), $titleLink );
205 }
206 } else {
207 $details = '';
208 array_unshift( $params, $titleLink );
209 // User suppression
210 if ( preg_match( '/^(block|suppress)\/(block|reblock)$/', $key ) ) {
211 if ( $skin ) {
212 $params[1] = '<span title="' . htmlspecialchars( $params[1] ). '">' .
213 $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
214 } else {
215 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
216 }
217 $params[2] = isset( $params[2] ) ?
218 self::formatBlockFlags( $params[2], is_null( $skin ) ) : '';
219 // Page protections
220 } else if ( $type == 'protect' && count($params) == 3 ) {
221 if( $params[2] ) {
222 if ( $skin ) {
223 $details .= htmlspecialchars( " {$params[1]}" ); // restrictions and expiries
224 $details .= ' ['.wfMsg('protect-summary-cascade').']';
225 } else {
226 $details .= " {$params[1]}";
227 $details .= ' ['.wfMsgForContent('protect-summary-cascade').']';
228 }
229 }
230 // Page moves
231 } else if ( $type == 'move' && count( $params ) == 3 ) {
232 if( $params[2] ) {
233 if ( $skin ) {
234 $details .= ' [' . wfMsg( 'move-redirect-suppressed' ) . ']';
235 } else {
236 $details .= ' [' . wfMsgForContent( 'move-redirect-suppressed' ) . ']';
237 }
238 }
239 // Revision deletion
240 } else if ( preg_match( '/^(delete|suppress)\/revision$/', $key ) && count( $params ) == 5 ) {
241 $count = substr_count( $params[2], ',' ) + 1; // revisions
242 $ofield = intval( substr( $params[3], 7 ) ); // <ofield=x>
243 $nfield = intval( substr( $params[4], 7 ) ); // <nfield=x>
244 $details .= ': '.RevisionDeleter::getLogMessage( $count, $nfield, $ofield, false );
245 // Log deletion
246 } else if ( preg_match( '/^(delete|suppress)\/event$/', $key ) && count( $params ) == 4 ) {
247 $count = substr_count( $params[1], ',' ) + 1; // log items
248 $ofield = intval( substr( $params[2], 7 ) ); // <ofield=x>
249 $nfield = intval( substr( $params[3], 7 ) ); // <nfield=x>
250 $details .= ': '.RevisionDeleter::getLogMessage( $count, $nfield, $ofield, true );
251 }
252 if ( $skin ) {
253 $rv = wfMsgHtml( $wgLogActions[$key], $params ) . $details;
254 } else {
255 $rv = wfMsgExt( $wgLogActions[$key], array( 'parsemag', 'escape', 'replaceafter', 'content' ), $params ) . $details;
256 }
257 }
258 }
259 } else {
260 global $wgLogActionsHandlers;
261 if( isset( $wgLogActionsHandlers[$key] ) ) {
262 $args = func_get_args();
263 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
264 } else {
265 wfDebug( "LogPage::actionText - unknown action $key\n" );
266 $rv = "$action";
267 }
268 }
269
270 // For the perplexed, this feature was added in r7855 by Erik.
271 // The feature was added because we liked adding [[$1]] in our log entries
272 // but the log entries are parsed as Wikitext on RecentChanges but as HTML
273 // on Special:Log. The hack is essentially that [[$1]] represented a link
274 // to the title in question. The first parameter to the HTML version (Special:Log)
275 // is that link in HTML form, and so this just gets rid of the ugly [[]].
276 // However, this is a horrible hack and it doesn't work like you expect if, say,
277 // you want to link to something OTHER than the title of the log entry.
278 // The real problem, which Erik was trying to fix (and it sort-of works now) is
279 // that the same messages are being treated as both wikitext *and* HTML.
280 if( $filterWikilinks ) {
281 $rv = str_replace( "[[", "", $rv );
282 $rv = str_replace( "]]", "", $rv );
283 }
284 return $rv;
285 }
286
287 protected static function getTitleLink( $type, $skin, $title, &$params ) {
288 global $wgLang, $wgContLang;
289 if( !$skin ) {
290 return $title->getPrefixedText();
291 }
292 switch( $type ) {
293 case 'move':
294 $titleLink = $skin->link(
295 $title,
296 htmlspecialchars( $title->getPrefixedText() ),
297 array(),
298 array( 'redirect' => 'no' )
299 );
300 $targetTitle = Title::newFromText( $params[0] );
301 if ( !$targetTitle ) {
302 # Workaround for broken database
303 $params[0] = htmlspecialchars( $params[0] );
304 } else {
305 $params[0] = $skin->link(
306 $targetTitle,
307 htmlspecialchars( $params[0] )
308 );
309 }
310 break;
311 case 'block':
312 if( substr( $title->getText(), 0, 1 ) == '#' ) {
313 $titleLink = $title->getText();
314 } else {
315 // TODO: Store the user identifier in the parameters
316 // to make this faster for future log entries
317 $id = User::idFromName( $title->getText() );
318 $titleLink = $skin->userLink( $id, $title->getText() )
319 . $skin->userToolLinks( $id, $title->getText(), false, Linker::TOOL_LINKS_NOBLOCK );
320 }
321 break;
322 case 'rights':
323 $text = $wgContLang->ucfirst( $title->getText() );
324 $titleLink = $skin->link( Title::makeTitle( NS_USER, $text ) );
325 break;
326 case 'merge':
327 $titleLink = $skin->link(
328 $title,
329 $title->getPrefixedText(),
330 array(),
331 array( 'redirect' => 'no' )
332 );
333 $params[0] = $skin->link(
334 Title::newFromText( $params[0] ),
335 htmlspecialchars( $params[0] )
336 );
337 $params[1] = $wgLang->timeanddate( $params[1] );
338 break;
339 default:
340 if( $title->getNamespace() == NS_SPECIAL ) {
341 list( $name, $par ) = SpecialPage::resolveAliasWithSubpage( $title->getDBkey() );
342 # Use the language name for log titles, rather than Log/X
343 if( $name == 'Log' ) {
344 $titleLink = '('.$skin->link( $title, LogPage::logName( $par ) ).')';
345 } else {
346 $titleLink = $skin->link( $title );
347 }
348 } else {
349 $titleLink = $skin->link( $title );
350 }
351 }
352 return $titleLink;
353 }
354
355 /**
356 * Add a log entry
357 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
358 * @param object &$target A title object.
359 * @param string $comment Description associated
360 * @param array $params Parameters passed later to wfMsg.* functions
361 * @param User $doer The user doing the action
362 */
363 public function addEntry( $action, $target, $comment, $params = array(), $doer = null ) {
364 if ( !is_array( $params ) ) {
365 $params = array( $params );
366 }
367
368 $this->action = $action;
369 $this->target = $target;
370 $this->comment = $comment;
371 $this->params = LogPage::makeParamBlob( $params );
372
373 if ($doer === null) {
374 global $wgUser;
375 $doer = $wgUser;
376 } elseif (!is_object( $doer ) ) {
377 $doer = User::newFromId( $doer );
378 }
379
380 $this->doer = $doer;
381
382 $this->actionText = LogPage::actionText( $this->type, $action, $target, NULL, $params );
383
384 return $this->saveContent();
385 }
386
387 /**
388 * Add relations to log_search table
389 * @static
390 */
391 public function addRelations( $field, $values, $logid ) {
392 if( !strlen($field) || empty($values) )
393 return false; // nothing
394 $data = array();
395 foreach( $values as $value ) {
396 $data[] = array('ls_field' => $field,'ls_value' => $value,'ls_log_id' => $logid);
397 }
398 $dbw = wfGetDB( DB_MASTER );
399 $dbw->insert( 'log_search', $data, __METHOD__, 'IGNORE' );
400 return true;
401 }
402
403 /**
404 * Create a blob from a parameter array
405 * @static
406 */
407 public static function makeParamBlob( $params ) {
408 return implode( "\n", $params );
409 }
410
411 /**
412 * Extract a parameter array from a blob
413 * @static
414 */
415 public static function extractParams( $blob ) {
416 if ( $blob === '' ) {
417 return array();
418 } else {
419 return explode( "\n", $blob );
420 }
421 }
422
423 /**
424 * Convert a comma-delimited list of block log flags
425 * into a more readable (and translated) form
426 *
427 * @param $flags Flags to format
428 * @param $forContent Whether to localize the message depending of the user
429 * language
430 * @return string
431 */
432 public static function formatBlockFlags( $flags, $forContent = false ) {
433 global $wgLang;
434
435 $flags = explode( ',', trim( $flags ) );
436 if( count( $flags ) > 0 ) {
437 for( $i = 0; $i < count( $flags ); $i++ )
438 $flags[$i] = self::formatBlockFlag( $flags[$i], $forContent );
439 return '(' . $wgLang->commaList( $flags ) . ')';
440 } else {
441 return '';
442 }
443 }
444
445 /**
446 * Translate a block log flag if possible
447 *
448 * @param $flag Flag to translate
449 * @param $forContent Whether to localize the message depending of the user
450 * language
451 * @return string
452 */
453 public static function formatBlockFlag( $flag, $forContent = false ) {
454 static $messages = array();
455 if( !isset( $messages[$flag] ) ) {
456 $k = 'block-log-flags-' . $flag;
457 if( $forContent )
458 $msg = wfMsgForContent( $k );
459 else
460 $msg = wfMsg( $k );
461 $messages[$flag] = htmlspecialchars( wfEmptyMsg( $k, $msg ) ? $flag : $msg );
462 }
463 return $messages[$flag];
464 }
465 }
466
467 /**
468 * Aliases for backwards compatibility with 1.6
469 */
470 define( 'MW_LOG_DELETED_ACTION', LogPage::DELETED_ACTION );
471 define( 'MW_LOG_DELETED_USER', LogPage::DELETED_USER );
472 define( 'MW_LOG_DELETED_COMMENT', LogPage::DELETED_COMMENT );
473 define( 'MW_LOG_DELETED_RESTRICTED', LogPage::DELETED_RESTRICTED );