Fixed spacing in db/debug/diff/externalstore/objectcache folder
[mediawiki.git] / includes / logging / LogPage.php
blob3fe91fc1e2036a58ee6ccf6df211965c361fdb9b
1 <?php
2 /**
3 * Contain log classes
5 * Copyright © 2002, 2004 Brion Vibber <brion@pobox.com>
6 * http://www.mediawiki.org/
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.
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.
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
23 * @file
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.
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;
43 /**
44 * @var User
46 var $doer;
48 /**
49 * @var Title
51 var $target;
53 /* @access public */
54 var $updateRecentChanges, $sendToUDP;
56 /**
57 * Constructor
59 * @param string $type one of '', 'block', 'protect', 'rights', 'delete',
60 * 'upload', 'move'
61 * @param $rc Boolean: whether to update recent changes as well as the logging table
62 * @param string $udp pass 'UDP' to send to the UDP feed if NOT sent to RC
64 public function __construct( $type, $rc = true, $udp = 'skipUDP' ) {
65 $this->type = $type;
66 $this->updateRecentChanges = $rc;
67 $this->sendToUDP = ( $udp == 'UDP' );
70 /**
71 * @return int log_id of the inserted log entry
73 protected function saveContent() {
74 global $wgLogRestrictions;
76 $dbw = wfGetDB( DB_MASTER );
77 $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
79 $this->timestamp = $now = wfTimestampNow();
80 $data = array(
81 'log_id' => $log_id,
82 'log_type' => $this->type,
83 'log_action' => $this->action,
84 'log_timestamp' => $dbw->timestamp( $now ),
85 'log_user' => $this->doer->getId(),
86 'log_user_text' => $this->doer->getName(),
87 'log_namespace' => $this->target->getNamespace(),
88 'log_title' => $this->target->getDBkey(),
89 'log_page' => $this->target->getArticleID(),
90 'log_comment' => $this->comment,
91 'log_params' => $this->params
93 $dbw->insert( 'logging', $data, __METHOD__ );
94 $newId = !is_null( $log_id ) ? $log_id : $dbw->insertId();
96 # And update recentchanges
97 if ( $this->updateRecentChanges ) {
98 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
100 RecentChange::notifyLog(
101 $now, $titleObj, $this->doer, $this->getRcComment(), '',
102 $this->type, $this->action, $this->target, $this->comment,
103 $this->params, $newId, $this->getRcCommentIRC()
105 } elseif ( $this->sendToUDP ) {
106 # Don't send private logs to UDP
107 if ( isset( $wgLogRestrictions[$this->type] ) && $wgLogRestrictions[$this->type] != '*' ) {
108 return $newId;
111 # Notify external application via UDP.
112 # We send this to IRC but do not want to add it the RC table.
113 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
114 $rc = RecentChange::newLogEntry(
115 $now, $titleObj, $this->doer, $this->getRcComment(), '',
116 $this->type, $this->action, $this->target, $this->comment,
117 $this->params, $newId, $this->getRcCommentIRC()
119 $rc->notifyRC2UDP();
121 return $newId;
125 * Get the RC comment from the last addEntry() call
127 * @return string
129 public function getRcComment() {
130 $rcComment = $this->actionText;
132 if ( $this->comment != '' ) {
133 if ( $rcComment == '' ) {
134 $rcComment = $this->comment;
135 } else {
136 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
137 $this->comment;
141 return $rcComment;
145 * Get the RC comment from the last addEntry() call for IRC
147 * @return string
149 public function getRcCommentIRC() {
150 $rcComment = $this->ircActionText;
152 if ( $this->comment != '' ) {
153 if ( $rcComment == '' ) {
154 $rcComment = $this->comment;
155 } else {
156 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
157 $this->comment;
161 return $rcComment;
165 * Get the comment from the last addEntry() call
167 public function getComment() {
168 return $this->comment;
172 * Get the list of valid log types
174 * @return Array of strings
176 public static function validTypes() {
177 global $wgLogTypes;
178 return $wgLogTypes;
182 * Is $type a valid log type
184 * @param string $type log type to check
185 * @return Boolean
187 public static function isLogType( $type ) {
188 return in_array( $type, LogPage::validTypes() );
192 * Get the name for the given log type
194 * @param string $type logtype
195 * @return String: log name
196 * @deprecated in 1.19, warnings in 1.21. Use getName()
198 public static function logName( $type ) {
199 global $wgLogNames;
201 if ( isset( $wgLogNames[$type] ) ) {
202 return str_replace( '_', ' ', wfMessage( $wgLogNames[$type] )->text() );
203 } else {
204 // Bogus log types? Perhaps an extension was removed.
205 return $type;
210 * Get the log header for the given log type
212 * @todo handle missing log types
213 * @param string $type logtype
214 * @return String: headertext of this logtype
215 * @deprecated in 1.19, warnings in 1.21. Use getDescription()
217 public static function logHeader( $type ) {
218 global $wgLogHeaders;
219 return wfMessage( $wgLogHeaders[$type] )->parse();
223 * Generate text for a log entry.
224 * Only LogFormatter should call this function.
226 * @param string $type log type
227 * @param string $action log action
228 * @param $title Mixed: Title object or null
229 * @param $skin Mixed: Skin object or null. If null, we want to use the wiki
230 * content language, since that will go to the IRC feed.
231 * @param array $params parameters
232 * @param $filterWikilinks Boolean: whether to filter wiki links
233 * @return HTML string
235 public static function actionText( $type, $action, $title = null, $skin = null,
236 $params = array(), $filterWikilinks = false )
238 global $wgLang, $wgContLang, $wgLogActions;
240 if ( is_null( $skin ) ) {
241 $langObj = $wgContLang;
242 $langObjOrNull = null;
243 } else {
244 $langObj = $wgLang;
245 $langObjOrNull = $wgLang;
248 $key = "$type/$action";
250 if ( isset( $wgLogActions[$key] ) ) {
251 if ( is_null( $title ) ) {
252 $rv = wfMessage( $wgLogActions[$key] )->inLanguage( $langObj )->escaped();
253 } else {
254 $titleLink = self::getTitleLink( $type, $langObjOrNull, $title, $params );
256 if ( count( $params ) == 0 ) {
257 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $titleLink )->inLanguage( $langObj )->escaped();
258 } else {
259 $details = '';
260 array_unshift( $params, $titleLink );
262 // User suppression
263 if ( preg_match( '/^(block|suppress)\/(block|reblock)$/', $key ) ) {
264 if ( $skin ) {
265 $params[1] = '<span class="blockExpiry" dir="ltr" title="' . htmlspecialchars( $params[1] ) . '">' .
266 $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
267 } else {
268 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
271 $params[2] = isset( $params[2] ) ?
272 self::formatBlockFlags( $params[2], $langObj ) : '';
273 // Page protections
274 } elseif ( $type == 'protect' && count( $params ) == 3 ) {
275 // Restrictions and expiries
276 if ( $skin ) {
277 $details .= $wgLang->getDirMark() . htmlspecialchars( " {$params[1]}" );
278 } else {
279 $details .= " {$params[1]}";
282 // Cascading flag...
283 if ( $params[2] ) {
284 $details .= ' [' . wfMessage( 'protect-summary-cascade' )->inLanguage( $langObj )->text() . ']';
288 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $params )->inLanguage( $langObj )->escaped() . $details;
291 } else {
292 global $wgLogActionsHandlers;
294 if ( isset( $wgLogActionsHandlers[$key] ) ) {
295 $args = func_get_args();
296 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
297 } else {
298 wfDebug( "LogPage::actionText - unknown action $key\n" );
299 $rv = "$action";
303 // For the perplexed, this feature was added in r7855 by Erik.
304 // The feature was added because we liked adding [[$1]] in our log entries
305 // but the log entries are parsed as Wikitext on RecentChanges but as HTML
306 // on Special:Log. The hack is essentially that [[$1]] represented a link
307 // to the title in question. The first parameter to the HTML version (Special:Log)
308 // is that link in HTML form, and so this just gets rid of the ugly [[]].
309 // However, this is a horrible hack and it doesn't work like you expect if, say,
310 // you want to link to something OTHER than the title of the log entry.
311 // The real problem, which Erik was trying to fix (and it sort-of works now) is
312 // that the same messages are being treated as both wikitext *and* HTML.
313 if ( $filterWikilinks ) {
314 $rv = str_replace( '[[', '', $rv );
315 $rv = str_replace( ']]', '', $rv );
318 return $rv;
322 * TODO document
323 * @param $type String
324 * @param $lang Language or null
325 * @param $title Title
326 * @param $params Array
327 * @return String
329 protected static function getTitleLink( $type, $lang, $title, &$params ) {
330 if ( !$lang ) {
331 return $title->getPrefixedText();
334 switch( $type ) {
335 case 'move':
336 $titleLink = Linker::link(
337 $title,
338 htmlspecialchars( $title->getPrefixedText() ),
339 array(),
340 array( 'redirect' => 'no' )
343 $targetTitle = Title::newFromText( $params[0] );
345 if ( !$targetTitle ) {
346 # Workaround for broken database
347 $params[0] = htmlspecialchars( $params[0] );
348 } else {
349 $params[0] = Linker::link(
350 $targetTitle,
351 htmlspecialchars( $params[0] )
354 break;
355 case 'block':
356 if ( substr( $title->getText(), 0, 1 ) == '#' ) {
357 $titleLink = $title->getText();
358 } else {
359 // @todo Store the user identifier in the parameters
360 // to make this faster for future log entries
361 $id = User::idFromName( $title->getText() );
362 $titleLink = Linker::userLink( $id, $title->getText() )
363 . Linker::userToolLinks( $id, $title->getText(), false, Linker::TOOL_LINKS_NOBLOCK );
365 break;
366 case 'merge':
367 $titleLink = Linker::link(
368 $title,
369 $title->getPrefixedText(),
370 array(),
371 array( 'redirect' => 'no' )
373 $params[0] = Linker::link(
374 Title::newFromText( $params[0] ),
375 htmlspecialchars( $params[0] )
377 $params[1] = $lang->timeanddate( $params[1] );
378 break;
379 default:
380 if ( $title->isSpecialPage() ) {
381 list( $name, $par ) = SpecialPageFactory::resolveAlias( $title->getDBkey() );
383 # Use the language name for log titles, rather than Log/X
384 if ( $name == 'Log' ) {
385 $logPage = new LogPage( $par );
386 $titleLink = Linker::link( $title, $logPage->getName()->escaped() );
387 $titleLink = wfMessage( 'parentheses' )
388 ->inLanguage( $lang )
389 ->rawParams( $titleLink )
390 ->escaped();
391 } else {
392 $titleLink = Linker::link( $title );
394 } else {
395 $titleLink = Linker::link( $title );
399 return $titleLink;
403 * Add a log entry
405 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
406 * @param $target Title object
407 * @param string $comment description associated
408 * @param array $params parameters passed later to wfMessage function
409 * @param $doer User object: the user doing the action
411 * @return int log_id of the inserted log entry
413 public function addEntry( $action, $target, $comment, $params = array(), $doer = null ) {
414 global $wgContLang;
416 if ( !is_array( $params ) ) {
417 $params = array( $params );
420 if ( $comment === null ) {
421 $comment = '';
424 # Trim spaces on user supplied text
425 $comment = trim( $comment );
427 # Truncate for whole multibyte characters.
428 $comment = $wgContLang->truncate( $comment, 255 );
430 $this->action = $action;
431 $this->target = $target;
432 $this->comment = $comment;
433 $this->params = LogPage::makeParamBlob( $params );
435 if ( $doer === null ) {
436 global $wgUser;
437 $doer = $wgUser;
438 } elseif ( !is_object( $doer ) ) {
439 $doer = User::newFromId( $doer );
442 $this->doer = $doer;
444 $logEntry = new ManualLogEntry( $this->type, $action );
445 $logEntry->setTarget( $target );
446 $logEntry->setPerformer( $doer );
447 $logEntry->setParameters( $params );
449 $formatter = LogFormatter::newFromEntry( $logEntry );
450 $context = RequestContext::newExtraneousContext( $target );
451 $formatter->setContext( $context );
453 $this->actionText = $formatter->getPlainActionText();
454 $this->ircActionText = $formatter->getIRCActionText();
456 return $this->saveContent();
460 * Add relations to log_search table
462 * @param $field String
463 * @param $values Array
464 * @param $logid Integer
465 * @return Boolean
467 public function addRelations( $field, $values, $logid ) {
468 if ( !strlen( $field ) || empty( $values ) ) {
469 return false; // nothing
472 $data = array();
474 foreach ( $values as $value ) {
475 $data[] = array(
476 'ls_field' => $field,
477 'ls_value' => $value,
478 'ls_log_id' => $logid
482 $dbw = wfGetDB( DB_MASTER );
483 $dbw->insert( 'log_search', $data, __METHOD__, 'IGNORE' );
485 return true;
489 * Create a blob from a parameter array
491 * @param $params Array
492 * @return String
494 public static function makeParamBlob( $params ) {
495 return implode( "\n", $params );
499 * Extract a parameter array from a blob
501 * @param $blob String
502 * @return Array
504 public static function extractParams( $blob ) {
505 if ( $blob === '' ) {
506 return array();
507 } else {
508 return explode( "\n", $blob );
513 * Convert a comma-delimited list of block log flags
514 * into a more readable (and translated) form
516 * @param string $flags Flags to format
517 * @param $lang Language object to use
518 * @return String
520 public static function formatBlockFlags( $flags, $lang ) {
521 $flags = explode( ',', trim( $flags ) );
523 if ( count( $flags ) > 0 ) {
524 for ( $i = 0; $i < count( $flags ); $i++ ) {
525 $flags[$i] = self::formatBlockFlag( $flags[$i], $lang );
527 return wfMessage( 'parentheses' )->inLanguage( $lang )
528 ->rawParams( $lang->commaList( $flags ) )->escaped();
529 } else {
530 return '';
535 * Translate a block log flag if possible
537 * @param int $flag Flag to translate
538 * @param $lang Language object to use
539 * @return String
541 public static function formatBlockFlag( $flag, $lang ) {
542 static $messages = array();
544 if ( !isset( $messages[$flag] ) ) {
545 $messages[$flag] = htmlspecialchars( $flag ); // Fallback
547 // For grepping. The following core messages can be used here:
548 // * block-log-flags-angry-autoblock
549 // * block-log-flags-anononly
550 // * block-log-flags-hiddenname
551 // * block-log-flags-noautoblock
552 // * block-log-flags-nocreate
553 // * block-log-flags-noemail
554 // * block-log-flags-nousertalk
555 $msg = wfMessage( 'block-log-flags-' . $flag )->inLanguage( $lang );
557 if ( $msg->exists() ) {
558 $messages[$flag] = $msg->escaped();
562 return $messages[$flag];
566 * Name of the log.
567 * @return Message
568 * @since 1.19
570 public function getName() {
571 global $wgLogNames;
573 // BC
574 if ( isset( $wgLogNames[$this->type] ) ) {
575 $key = $wgLogNames[$this->type];
576 } else {
577 $key = 'log-name-' . $this->type;
580 return wfMessage( $key );
584 * Description of this log type.
585 * @return Message
586 * @since 1.19
588 public function getDescription() {
589 global $wgLogHeaders;
590 // BC
591 if ( isset( $wgLogHeaders[$this->type] ) ) {
592 $key = $wgLogHeaders[$this->type];
593 } else {
594 $key = 'log-description-' . $this->type;
596 return wfMessage( $key );
600 * Returns the right needed to read this log type.
601 * @return string
602 * @since 1.19
604 public function getRestriction() {
605 global $wgLogRestrictions;
606 if ( isset( $wgLogRestrictions[$this->type] ) ) {
607 $restriction = $wgLogRestrictions[$this->type];
608 } else {
609 // '' always returns true with $user->isAllowed()
610 $restriction = '';
612 return $restriction;
616 * Tells if this log is not viewable by all.
617 * @return bool
618 * @since 1.19
620 public function isRestricted() {
621 $restriction = $this->getRestriction();
622 return $restriction !== '' && $restriction !== '*';