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