58a0268781f2192235e3dfc8b3cd2c1879a76923
[lhc/web/wiklou.git] / includes / LogPage.php
1 <?php
2 /**
3 * Contain log classes
4 *
5 * Copyright © 2002, 2004 Brion Vibber <brion@pobox.com>
6 * http://www.mediawiki.org/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
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 $type String: one of '', 'block', 'protect', 'rights', 'delete',
49 * 'upload', 'move'
50 * @param $rc Boolean: whether to update recent changes as well as the logging table
51 * @param $udp String: pass 'UDP' 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 * Get the list of valid log types
125 *
126 * @return Array of strings
127 */
128 public static function validTypes() {
129 global $wgLogTypes;
130 return $wgLogTypes;
131 }
132
133 /**
134 * Is $type a valid log type
135 *
136 * @param $type String: log type to check
137 * @return Boolean
138 */
139 public static function isLogType( $type ) {
140 return in_array( $type, LogPage::validTypes() );
141 }
142
143 /**
144 * Get the name for the given log type
145 *
146 * @param $type String: logtype
147 * @return String: log name
148 */
149 public static function logName( $type ) {
150 global $wgLogNames;
151
152 if( isset( $wgLogNames[$type] ) ) {
153 return str_replace( '_', ' ', wfMsg( $wgLogNames[$type] ) );
154 } else {
155 // Bogus log types? Perhaps an extension was removed.
156 return $type;
157 }
158 }
159
160 /**
161 * Get the log header for the given log type
162 *
163 * @todo handle missing log types
164 * @param $type String: logtype
165 * @return String: headertext of this logtype
166 */
167 public static function logHeader( $type ) {
168 global $wgLogHeaders;
169 return wfMsgExt($wgLogHeaders[$type], array( 'parseinline' ) );
170 }
171
172 /**
173 * Generate text for a log entry
174 *
175 * @param $type String: log type
176 * @param $action String: log action
177 * @param $title Mixed: Title object or null
178 * @param $skin Mixed: Skin object or null. If null, we want to use the wiki
179 * content language, since that will go to the irc feed.
180 * @param $params Array: parameters
181 * @param $filterWikilinks Boolean: whether to filter wiki links
182 * @return HTML string
183 */
184 public static function actionText( $type, $action, $title = null, $skin = null,
185 $params = array(), $filterWikilinks = false )
186 {
187 global $wgLang, $wgContLang, $wgLogActions;
188
189 $key = "$type/$action";
190 # Defer patrol log to PatrolLog class
191 if( $key == 'patrol/patrol' ) {
192 return PatrolLog::makeActionText( $title, $params, $skin );
193 }
194 if( isset( $wgLogActions[$key] ) ) {
195 if( is_null( $title ) ) {
196 $rv = wfMsgHtml( $wgLogActions[$key] );
197 } else {
198 $titleLink = self::getTitleLink( $type, $skin, $title, $params );
199 if( $key == 'rights/rights' ) {
200 if( $skin ) {
201 $rightsnone = wfMsg( 'rightsnone' );
202 foreach ( $params as &$param ) {
203 $groupArray = array_map( 'trim', explode( ',', $param ) );
204 $groupArray = array_map( array( 'User', 'getGroupMember' ), $groupArray );
205 $param = $wgLang->listToText( $groupArray );
206 }
207 } else {
208 $rightsnone = wfMsgForContent( 'rightsnone' );
209 }
210 if( !isset( $params[0] ) || trim( $params[0] ) == '' )
211 $params[0] = $rightsnone;
212 if( !isset( $params[1] ) || trim( $params[1] ) == '' )
213 $params[1] = $rightsnone;
214 }
215 if( count( $params ) == 0 ) {
216 if ( $skin ) {
217 $rv = wfMsgHtml( $wgLogActions[$key], $titleLink );
218 } else {
219 $rv = wfMsgExt( $wgLogActions[$key], array( 'parsemag', 'escape', 'replaceafter', 'content' ), $titleLink );
220 }
221 } else {
222 $details = '';
223 array_unshift( $params, $titleLink );
224 // User suppression
225 if ( preg_match( '/^(block|suppress)\/(block|reblock)$/', $key ) ) {
226 if ( $skin ) {
227 $params[1] = '<span title="' . htmlspecialchars( $params[1] ). '">' .
228 $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
229 } else {
230 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
231 }
232 $params[2] = isset( $params[2] ) ?
233 self::formatBlockFlags( $params[2], is_null( $skin ) ) : '';
234
235 // Page protections
236 } else if ( $type == 'protect' && count($params) == 3 ) {
237 // Restrictions and expiries
238 if( $skin ) {
239 $details .= htmlspecialchars( " {$params[1]}" );
240 } else {
241 $details .= " {$params[1]}";
242 }
243 // Cascading flag...
244 if( $params[2] ) {
245 if ( $skin ) {
246 $details .= ' ['.wfMsg('protect-summary-cascade').']';
247 } else {
248 $details .= ' ['.wfMsgForContent('protect-summary-cascade').']';
249 }
250 }
251
252 // Page moves
253 } else if ( $type == 'move' && count( $params ) == 3 && $action != 'move_rev' ) {
254 if( $params[2] ) {
255 if ( $skin ) {
256 $details .= ' [' . wfMsg( 'move-redirect-suppressed' ) . ']';
257 } else {
258 $details .= ' [' . wfMsgForContent( 'move-redirect-suppressed' ) . ']';
259 }
260 }
261
262 // Revision deletion
263 } else if ( preg_match( '/^(delete|suppress)\/revision$/', $key ) && count( $params ) == 5 ) {
264 $count = substr_count( $params[2], ',' ) + 1; // revisions
265 $ofield = intval( substr( $params[3], 7 ) ); // <ofield=x>
266 $nfield = intval( substr( $params[4], 7 ) ); // <nfield=x>
267 $details .= ': ' . RevisionDeleter::getLogMessage( $count, $nfield, $ofield, false, is_null($skin) );
268
269 // Log deletion
270 } else if ( preg_match( '/^(delete|suppress)\/event$/', $key ) && count( $params ) == 4 ) {
271 $count = substr_count( $params[1], ',' ) + 1; // log items
272 $ofield = intval( substr( $params[2], 7 ) ); // <ofield=x>
273 $nfield = intval( substr( $params[3], 7 ) ); // <nfield=x>
274 $details .= ': ' . RevisionDeleter::getLogMessage( $count, $nfield, $ofield, true, is_null($skin) );
275 }
276
277 if ( $skin ) {
278 $rv = wfMsgHtml( $wgLogActions[$key], $params ) . $details;
279 } else {
280 $rv = wfMsgExt( $wgLogActions[$key], array( 'parsemag', 'escape', 'replaceafter', 'content' ), $params ) . $details;
281 }
282 }
283 }
284 } else {
285 global $wgLogActionsHandlers;
286 if( isset( $wgLogActionsHandlers[$key] ) ) {
287 $args = func_get_args();
288 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
289 } else {
290 wfDebug( "LogPage::actionText - unknown action $key\n" );
291 $rv = "$action";
292 }
293 }
294
295 // For the perplexed, this feature was added in r7855 by Erik.
296 // The feature was added because we liked adding [[$1]] in our log entries
297 // but the log entries are parsed as Wikitext on RecentChanges but as HTML
298 // on Special:Log. The hack is essentially that [[$1]] represented a link
299 // to the title in question. The first parameter to the HTML version (Special:Log)
300 // is that link in HTML form, and so this just gets rid of the ugly [[]].
301 // However, this is a horrible hack and it doesn't work like you expect if, say,
302 // you want to link to something OTHER than the title of the log entry.
303 // The real problem, which Erik was trying to fix (and it sort-of works now) is
304 // that the same messages are being treated as both wikitext *and* HTML.
305 if( $filterWikilinks ) {
306 $rv = str_replace( "[[", "", $rv );
307 $rv = str_replace( "]]", "", $rv );
308 }
309 return $rv;
310 }
311
312 protected static function getTitleLink( $type, $skin, $title, &$params ) {
313 global $wgLang, $wgContLang, $wgUserrightsInterwikiDelimiter;
314 if( !$skin ) {
315 return $title->getPrefixedText();
316 }
317 switch( $type ) {
318 case 'move':
319 $titleLink = $skin->link(
320 $title,
321 htmlspecialchars( $title->getPrefixedText() ),
322 array(),
323 array( 'redirect' => 'no' )
324 );
325 $targetTitle = Title::newFromText( $params[0] );
326 if ( !$targetTitle ) {
327 # Workaround for broken database
328 $params[0] = htmlspecialchars( $params[0] );
329 } else {
330 $params[0] = $skin->link(
331 $targetTitle,
332 htmlspecialchars( $params[0] )
333 );
334 }
335 break;
336 case 'block':
337 if( substr( $title->getText(), 0, 1 ) == '#' ) {
338 $titleLink = $title->getText();
339 } else {
340 // TODO: Store the user identifier in the parameters
341 // to make this faster for future log entries
342 $id = User::idFromName( $title->getText() );
343 $titleLink = $skin->userLink( $id, $title->getText() )
344 . $skin->userToolLinks( $id, $title->getText(), false, Linker::TOOL_LINKS_NOBLOCK );
345 }
346 break;
347 case 'rights':
348 $text = $wgContLang->ucfirst( $title->getText() );
349 $parts = explode( $wgUserrightsInterwikiDelimiter, $text, 2 );
350 if ( count( $parts ) == 2 ) {
351 $titleLink = WikiMap::foreignUserLink( $parts[1], $parts[0],
352 htmlspecialchars( $title->getPrefixedText() ) );
353 if ( $titleLink !== false )
354 break;
355 }
356 $titleLink = $skin->link( Title::makeTitle( NS_USER, $text ) );
357 break;
358 case 'merge':
359 $titleLink = $skin->link(
360 $title,
361 $title->getPrefixedText(),
362 array(),
363 array( 'redirect' => 'no' )
364 );
365 $params[0] = $skin->link(
366 Title::newFromText( $params[0] ),
367 htmlspecialchars( $params[0] )
368 );
369 $params[1] = $wgLang->timeanddate( $params[1] );
370 break;
371 default:
372 if( $title->getNamespace() == NS_SPECIAL ) {
373 list( $name, $par ) = SpecialPage::resolveAliasWithSubpage( $title->getDBkey() );
374 # Use the language name for log titles, rather than Log/X
375 if( $name == 'Log' ) {
376 $titleLink = '('.$skin->link( $title, LogPage::logName( $par ) ).')';
377 } else {
378 $titleLink = $skin->link( $title );
379 }
380 } else {
381 $titleLink = $skin->link( $title );
382 }
383 }
384 return $titleLink;
385 }
386
387 /**
388 * Add a log entry
389 *
390 * @param $action String: one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
391 * @param $target Title object
392 * @param $comment String: description associated
393 * @param $params Array: parameters passed later to wfMsg.* functions
394 * @param $doer User object: the user doing the action
395 */
396 public function addEntry( $action, $target, $comment, $params = array(), $doer = null ) {
397 if ( !is_array( $params ) ) {
398 $params = array( $params );
399 }
400
401 if ( $comment === null ) $comment = "";
402
403 $this->action = $action;
404 $this->target = $target;
405 $this->comment = $comment;
406 $this->params = LogPage::makeParamBlob( $params );
407
408 if ($doer === null) {
409 global $wgUser;
410 $doer = $wgUser;
411 } elseif (!is_object( $doer ) ) {
412 $doer = User::newFromId( $doer );
413 }
414
415 $this->doer = $doer;
416
417 $this->actionText = LogPage::actionText( $this->type, $action, $target, null, $params );
418
419 return $this->saveContent();
420 }
421
422 /**
423 * Add relations to log_search table
424 *
425 * @param $field String
426 * @param $values Array
427 * @param $logid Integer
428 * @return Boolean
429 */
430 public function addRelations( $field, $values, $logid ) {
431 if( !strlen($field) || empty($values) )
432 return false; // nothing
433 $data = array();
434 foreach( $values as $value ) {
435 $data[] = array('ls_field' => $field,'ls_value' => $value,'ls_log_id' => $logid);
436 }
437 $dbw = wfGetDB( DB_MASTER );
438 $dbw->insert( 'log_search', $data, __METHOD__, 'IGNORE' );
439 return true;
440 }
441
442 /**
443 * Create a blob from a parameter array
444 *
445 * @param $params Array
446 * @return String
447 */
448 public static function makeParamBlob( $params ) {
449 return implode( "\n", $params );
450 }
451
452 /**
453 * Extract a parameter array from a blob
454 *
455 * @param $blob String
456 * @return Array
457 */
458 public static function extractParams( $blob ) {
459 if ( $blob === '' ) {
460 return array();
461 } else {
462 return explode( "\n", $blob );
463 }
464 }
465
466 /**
467 * Convert a comma-delimited list of block log flags
468 * into a more readable (and translated) form
469 *
470 * @param $flags Flags to format
471 * @param $forContent Whether to localize the message depending of the user
472 * language
473 * @return String
474 */
475 public static function formatBlockFlags( $flags, $forContent = false ) {
476 global $wgLang;
477
478 $flags = explode( ',', trim( $flags ) );
479 if( count( $flags ) > 0 ) {
480 for( $i = 0; $i < count( $flags ); $i++ )
481 $flags[$i] = self::formatBlockFlag( $flags[$i], $forContent );
482 return '(' . $wgLang->commaList( $flags ) . ')';
483 } else {
484 return '';
485 }
486 }
487
488 /**
489 * Translate a block log flag if possible
490 *
491 * @param $flag Flag to translate
492 * @param $forContent Whether to localize the message depending of the user
493 * language
494 * @return String
495 */
496 public static function formatBlockFlag( $flag, $forContent = false ) {
497 static $messages = array();
498 if( !isset( $messages[$flag] ) ) {
499 $k = 'block-log-flags-' . $flag;
500 if( $forContent )
501 $msg = wfMsgForContent( $k );
502 else
503 $msg = wfMsg( $k );
504 $messages[$flag] = htmlspecialchars( wfEmptyMsg( $k, $msg ) ? $flag : $msg );
505 }
506 return $messages[$flag];
507 }
508 }
509
510 /**
511 * Aliases for backwards compatibility with 1.6
512 */
513 define( 'MW_LOG_DELETED_ACTION', LogPage::DELETED_ACTION );
514 define( 'MW_LOG_DELETED_USER', LogPage::DELETED_USER );
515 define( 'MW_LOG_DELETED_COMMENT', LogPage::DELETED_COMMENT );
516 define( 'MW_LOG_DELETED_RESTRICTED', LogPage::DELETED_RESTRICTED );