5 * Copyright © 2002, 2004 Brion Vibber <brion@pobox.com>
6 * https://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
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.
33 const DELETED_ACTION
= 1;
34 const DELETED_COMMENT
= 2;
35 const DELETED_USER
= 4;
36 const DELETED_RESTRICTED
= 8;
39 const SUPPRESSED_USER
= 12;
40 const SUPPRESSED_ACTION
= 9;
43 public $updateRecentChanges;
48 /** @var string Plaintext version of the message for IRC */
49 private $ircActionText;
51 /** @var string Plaintext version of the message */
54 /** @var string One of '', 'block', 'protect', 'rights', 'delete',
59 /** @var string One of '', 'block', 'protect', 'rights', 'delete',
60 * 'upload', 'move', 'move_redir' */
63 /** @var string Comment associated with action */
66 /** @var string Blob made of a parameters array */
69 /** @var User The user doing the action */
78 * @param string $type One of '', 'block', 'protect', 'rights', 'delete',
80 * @param bool $rc Whether to update recent changes as well as the logging table
81 * @param string $udp Pass 'UDP' to send to the UDP feed if NOT sent to RC
83 public function __construct( $type, $rc = true, $udp = 'skipUDP' ) {
85 $this->updateRecentChanges
= $rc;
86 $this->sendToUDP
= ( $udp == 'UDP' );
90 * @return int The log_id of the inserted log entry
92 protected function saveContent() {
93 global $wgLogRestrictions;
95 $dbw = wfGetDB( DB_MASTER
);
96 $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
98 // @todo FIXME private/protected/public property?
99 $this->timestamp
= $now = wfTimestampNow();
102 'log_type' => $this->type
,
103 'log_action' => $this->action
,
104 'log_timestamp' => $dbw->timestamp( $now ),
105 'log_user' => $this->doer
->getId(),
106 'log_user_text' => $this->doer
->getName(),
107 'log_namespace' => $this->target
->getNamespace(),
108 'log_title' => $this->target
->getDBkey(),
109 'log_page' => $this->target
->getArticleID(),
110 'log_comment' => $this->comment
,
111 'log_params' => $this->params
113 $dbw->insert( 'logging', $data, __METHOD__
);
114 $newId = !is_null( $log_id ) ?
$log_id : $dbw->insertId();
116 # And update recentchanges
117 if ( $this->updateRecentChanges
) {
118 $titleObj = SpecialPage
::getTitleFor( 'Log', $this->type
);
120 RecentChange
::notifyLog(
121 $now, $titleObj, $this->doer
, $this->getRcComment(), '',
122 $this->type
, $this->action
, $this->target
, $this->comment
,
123 $this->params
, $newId, $this->getRcCommentIRC()
125 } elseif ( $this->sendToUDP
) {
126 # Don't send private logs to UDP
127 if ( isset( $wgLogRestrictions[$this->type
] ) && $wgLogRestrictions[$this->type
] != '*' ) {
131 # Notify external application via UDP.
132 # We send this to IRC but do not want to add it the RC table.
133 $titleObj = SpecialPage
::getTitleFor( 'Log', $this->type
);
134 $rc = RecentChange
::newLogEntry(
135 $now, $titleObj, $this->doer
, $this->getRcComment(), '',
136 $this->type
, $this->action
, $this->target
, $this->comment
,
137 $this->params
, $newId, $this->getRcCommentIRC()
139 $rc->notifyRCFeeds();
146 * Get the RC comment from the last addEntry() call
150 public function getRcComment() {
151 $rcComment = $this->actionText
;
153 if ( $this->comment
!= '' ) {
154 if ( $rcComment == '' ) {
155 $rcComment = $this->comment
;
157 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
166 * Get the RC comment from the last addEntry() call for IRC
170 public function getRcCommentIRC() {
171 $rcComment = $this->ircActionText
;
173 if ( $this->comment
!= '' ) {
174 if ( $rcComment == '' ) {
175 $rcComment = $this->comment
;
177 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
186 * Get the comment from the last addEntry() call
189 public function getComment() {
190 return $this->comment
;
194 * Get the list of valid log types
196 * @return array Array of strings
198 public static function validTypes() {
205 * Is $type a valid log type
207 * @param string $type Log type to check
210 public static function isLogType( $type ) {
211 return in_array( $type, LogPage
::validTypes() );
215 * Get the name for the given log type
217 * @param string $type Log type
218 * @return string Log name
219 * @deprecated since 1.19, warnings in 1.21. Use getName()
221 public static function logName( $type ) {
224 wfDeprecated( __METHOD__
, '1.21' );
226 if ( isset( $wgLogNames[$type] ) ) {
227 return str_replace( '_', ' ', wfMessage( $wgLogNames[$type] )->text() );
229 // Bogus log types? Perhaps an extension was removed.
235 * Get the log header for the given log type
237 * @todo handle missing log types
238 * @param string $type Logtype
239 * @return string Header text of this logtype
240 * @deprecated since 1.19, warnings in 1.21. Use getDescription()
242 public static function logHeader( $type ) {
243 global $wgLogHeaders;
245 wfDeprecated( __METHOD__
, '1.21' );
247 return wfMessage( $wgLogHeaders[$type] )->parse();
251 * Generate text for a log entry.
252 * Only LogFormatter should call this function.
254 * @param string $type Log type
255 * @param string $action Log action
256 * @param Title|null $title Title object or null
257 * @param Skin|null $skin Skin object or null. If null, we want to use the wiki
258 * content language, since that will go to the IRC feed.
259 * @param array $params Parameters
260 * @param bool $filterWikilinks Whether to filter wiki links
261 * @return string HTML
263 public static function actionText( $type, $action, $title = null, $skin = null,
264 $params = array(), $filterWikilinks = false
266 global $wgLang, $wgContLang, $wgLogActions;
268 if ( is_null( $skin ) ) {
269 $langObj = $wgContLang;
270 $langObjOrNull = null;
273 $langObjOrNull = $wgLang;
276 $key = "$type/$action";
278 if ( isset( $wgLogActions[$key] ) ) {
279 if ( is_null( $title ) ) {
280 $rv = wfMessage( $wgLogActions[$key] )->inLanguage( $langObj )->escaped();
282 $titleLink = self
::getTitleLink( $type, $langObjOrNull, $title, $params );
284 if ( count( $params ) == 0 ) {
285 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $titleLink )
286 ->inLanguage( $langObj )->escaped();
289 array_unshift( $params, $titleLink );
292 if ( preg_match( '/^(block|suppress)\/(block|reblock)$/', $key ) ) {
294 // Localize the duration, and add a tooltip
295 // in English to help visitors from other wikis.
296 // The lrm is needed to make sure that the number
297 // is shown on the correct side of the tooltip text.
298 $durationTooltip = '‎' . htmlspecialchars( $params[1] );
299 $params[1] = "<span class='blockExpiry' title='$durationTooltip'>" .
300 $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
302 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
305 $params[2] = isset( $params[2] ) ?
306 self
::formatBlockFlags( $params[2], $langObj ) : '';
308 } elseif ( $type == 'protect' && count( $params ) == 3 ) {
309 // Restrictions and expiries
311 $details .= $wgLang->getDirMark() . htmlspecialchars( " {$params[1]}" );
313 $details .= " {$params[1]}";
318 $text = wfMessage( 'protect-summary-cascade' )
319 ->inLanguage( $langObj )->text();
321 $details .= wfMessage( 'brackets', $text )->inLanguage( $langObj )->text();
326 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $params )
327 ->inLanguage( $langObj )->escaped() . $details;
331 global $wgLogActionsHandlers;
333 if ( isset( $wgLogActionsHandlers[$key] ) ) {
334 $args = func_get_args();
335 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
337 wfDebug( "LogPage::actionText - unknown action $key\n" );
342 // For the perplexed, this feature was added in r7855 by Erik.
343 // The feature was added because we liked adding [[$1]] in our log entries
344 // but the log entries are parsed as Wikitext on RecentChanges but as HTML
345 // on Special:Log. The hack is essentially that [[$1]] represented a link
346 // to the title in question. The first parameter to the HTML version (Special:Log)
347 // is that link in HTML form, and so this just gets rid of the ugly [[]].
348 // However, this is a horrible hack and it doesn't work like you expect if, say,
349 // you want to link to something OTHER than the title of the log entry.
350 // The real problem, which Erik was trying to fix (and it sort-of works now) is
351 // that the same messages are being treated as both wikitext *and* HTML.
352 if ( $filterWikilinks ) {
353 $rv = str_replace( '[[', '', $rv );
354 $rv = str_replace( ']]', '', $rv );
362 * @param string $type
363 * @param Language|null $lang
364 * @param Title $title
365 * @param array $params
368 protected static function getTitleLink( $type, $lang, $title, &$params ) {
370 return $title->getPrefixedText();
375 if ( substr( $title->getText(), 0, 1 ) == '#' ) {
376 $titleLink = $title->getText();
378 // @todo Store the user identifier in the parameters
379 // to make this faster for future log entries
380 $id = User
::idFromName( $title->getText() );
381 $titleLink = Linker
::userLink( $id, $title->getText() )
382 . Linker
::userToolLinks( $id, $title->getText(), false, Linker
::TOOL_LINKS_NOBLOCK
);
386 if ( $title->isSpecialPage() ) {
387 list( $name, $par ) = SpecialPageFactory
::resolveAlias( $title->getDBkey() );
389 # Use the language name for log titles, rather than Log/X
390 if ( $name == 'Log' ) {
391 $logPage = new LogPage( $par );
392 $titleLink = Linker
::link( $title, $logPage->getName()->escaped() );
393 $titleLink = wfMessage( 'parentheses' )
394 ->inLanguage( $lang )
395 ->rawParams( $titleLink )
398 $titleLink = Linker
::link( $title );
401 $titleLink = Linker
::link( $title );
411 * @param string $action One of '', 'block', 'protect', 'rights', 'delete',
412 * 'upload', 'move', 'move_redir'
413 * @param Title $target Title object
414 * @param string $comment Description associated
415 * @param array $params Parameters passed later to wfMessage function
416 * @param null|int|User $doer The user doing the action. null for $wgUser
418 * @return int The log_id of the inserted log entry
420 public function addEntry( $action, $target, $comment, $params = array(), $doer = null ) {
423 if ( !is_array( $params ) ) {
424 $params = array( $params );
427 if ( $comment === null ) {
431 # Trim spaces on user supplied text
432 $comment = trim( $comment );
434 # Truncate for whole multibyte characters.
435 $comment = $wgContLang->truncate( $comment, 255 );
437 $this->action
= $action;
438 $this->target
= $target;
439 $this->comment
= $comment;
440 $this->params
= LogPage
::makeParamBlob( $params );
442 if ( $doer === null ) {
445 } elseif ( !is_object( $doer ) ) {
446 $doer = User
::newFromId( $doer );
451 $logEntry = new ManualLogEntry( $this->type
, $action );
452 $logEntry->setTarget( $target );
453 $logEntry->setPerformer( $doer );
454 $logEntry->setParameters( $params );
456 $formatter = LogFormatter
::newFromEntry( $logEntry );
457 $context = RequestContext
::newExtraneousContext( $target );
458 $formatter->setContext( $context );
460 $this->actionText
= $formatter->getPlainActionText();
461 $this->ircActionText
= $formatter->getIRCActionText();
463 return $this->saveContent();
467 * Add relations to log_search table
469 * @param string $field
470 * @param array $values
474 public function addRelations( $field, $values, $logid ) {
475 if ( !strlen( $field ) ||
empty( $values ) ) {
476 return false; // nothing
481 foreach ( $values as $value ) {
483 'ls_field' => $field,
484 'ls_value' => $value,
485 'ls_log_id' => $logid
489 $dbw = wfGetDB( DB_MASTER
);
490 $dbw->insert( 'log_search', $data, __METHOD__
, 'IGNORE' );
496 * Create a blob from a parameter array
498 * @param array $params
501 public static function makeParamBlob( $params ) {
502 return implode( "\n", $params );
506 * Extract a parameter array from a blob
508 * @param string $blob
511 public static function extractParams( $blob ) {
512 if ( $blob === '' ) {
515 return explode( "\n", $blob );
520 * Convert a comma-delimited list of block log flags
521 * into a more readable (and translated) form
523 * @param string $flags Flags to format
524 * @param Language $lang
527 public static function formatBlockFlags( $flags, $lang ) {
528 $flags = trim( $flags );
529 if ( $flags === '' ) {
530 return ''; //nothing to do
532 $flags = explode( ',', $flags );
533 $flagsCount = count( $flags );
535 for ( $i = 0; $i < $flagsCount; $i++
) {
536 $flags[$i] = self
::formatBlockFlag( $flags[$i], $lang );
539 return wfMessage( 'parentheses' )->inLanguage( $lang )
540 ->rawParams( $lang->commaList( $flags ) )->escaped();
544 * Translate a block log flag if possible
546 * @param int $flag Flag to translate
547 * @param Language $lang Language object to use
550 public static function formatBlockFlag( $flag, $lang ) {
551 static $messages = array();
553 if ( !isset( $messages[$flag] ) ) {
554 $messages[$flag] = htmlspecialchars( $flag ); // Fallback
556 // For grepping. The following core messages can be used here:
557 // * block-log-flags-angry-autoblock
558 // * block-log-flags-anononly
559 // * block-log-flags-hiddenname
560 // * block-log-flags-noautoblock
561 // * block-log-flags-nocreate
562 // * block-log-flags-noemail
563 // * block-log-flags-nousertalk
564 $msg = wfMessage( 'block-log-flags-' . $flag )->inLanguage( $lang );
566 if ( $msg->exists() ) {
567 $messages[$flag] = $msg->escaped();
571 return $messages[$flag];
579 public function getName() {
583 if ( isset( $wgLogNames[$this->type
] ) ) {
584 $key = $wgLogNames[$this->type
];
586 $key = 'log-name-' . $this->type
;
589 return wfMessage( $key );
593 * Description of this log type.
597 public function getDescription() {
598 global $wgLogHeaders;
600 if ( isset( $wgLogHeaders[$this->type
] ) ) {
601 $key = $wgLogHeaders[$this->type
];
603 $key = 'log-description-' . $this->type
;
606 return wfMessage( $key );
610 * Returns the right needed to read this log type.
614 public function getRestriction() {
615 global $wgLogRestrictions;
616 if ( isset( $wgLogRestrictions[$this->type
] ) ) {
617 $restriction = $wgLogRestrictions[$this->type
];
619 // '' always returns true with $user->isAllowed()
627 * Tells if this log is not viewable by all.
631 public function isRestricted() {
632 $restriction = $this->getRestriction();
634 return $restriction !== '' && $restriction !== '*';