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