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
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 var $updateRecentChanges;
48 /** @var string One of '', 'block', 'protect', 'rights', 'delete',
53 /** @var string One of '', 'block', 'protect', 'rights', 'delete',
54 * 'upload', 'move', 'move_redir' */
57 /** @var string Comment associated with action */
60 /** @var string Blob made of a parameters array */
63 /** @var User The user doing the action */
72 * @param string $type One of '', 'block', 'protect', 'rights', 'delete',
74 * @param bool $rc Whether to update recent changes as well as the logging table
75 * @param string $udp Pass 'UDP' to send to the UDP feed if NOT sent to RC
77 public function __construct( $type, $rc = true, $udp = 'skipUDP' ) {
79 $this->updateRecentChanges
= $rc;
80 $this->sendToUDP
= ( $udp == 'UDP' );
84 * @return int log_id of the inserted log entry
86 protected function saveContent() {
87 global $wgLogRestrictions;
89 $dbw = wfGetDB( DB_MASTER
);
90 $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
92 $this->timestamp
= $now = wfTimestampNow();
95 'log_type' => $this->type
,
96 'log_action' => $this->action
,
97 'log_timestamp' => $dbw->timestamp( $now ),
98 'log_user' => $this->doer
->getId(),
99 'log_user_text' => $this->doer
->getName(),
100 'log_namespace' => $this->target
->getNamespace(),
101 'log_title' => $this->target
->getDBkey(),
102 'log_page' => $this->target
->getArticleID(),
103 'log_comment' => $this->comment
,
104 'log_params' => $this->params
106 $dbw->insert( 'logging', $data, __METHOD__
);
107 $newId = !is_null( $log_id ) ?
$log_id : $dbw->insertId();
109 # And update recentchanges
110 if ( $this->updateRecentChanges
) {
111 $titleObj = SpecialPage
::getTitleFor( 'Log', $this->type
);
113 RecentChange
::notifyLog(
114 $now, $titleObj, $this->doer
, $this->getRcComment(), '',
115 $this->type
, $this->action
, $this->target
, $this->comment
,
116 $this->params
, $newId, $this->getRcCommentIRC()
118 } elseif ( $this->sendToUDP
) {
119 # Don't send private logs to UDP
120 if ( isset( $wgLogRestrictions[$this->type
] ) && $wgLogRestrictions[$this->type
] != '*' ) {
124 # Notify external application via UDP.
125 # We send this to IRC but do not want to add it the RC table.
126 $titleObj = SpecialPage
::getTitleFor( 'Log', $this->type
);
127 $rc = RecentChange
::newLogEntry(
128 $now, $titleObj, $this->doer
, $this->getRcComment(), '',
129 $this->type
, $this->action
, $this->target
, $this->comment
,
130 $this->params
, $newId, $this->getRcCommentIRC()
132 $rc->notifyRCFeeds();
139 * Get the RC comment from the last addEntry() call
143 public function getRcComment() {
144 $rcComment = $this->actionText
;
146 if ( $this->comment
!= '' ) {
147 if ( $rcComment == '' ) {
148 $rcComment = $this->comment
;
150 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
159 * Get the RC comment from the last addEntry() call for IRC
163 public function getRcCommentIRC() {
164 $rcComment = $this->ircActionText
;
166 if ( $this->comment
!= '' ) {
167 if ( $rcComment == '' ) {
168 $rcComment = $this->comment
;
170 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
179 * Get the comment from the last addEntry() call
181 public function getComment() {
182 return $this->comment
;
186 * Get the list of valid log types
188 * @return array of strings
190 public static function validTypes() {
197 * Is $type a valid log type
199 * @param string $type Log type to check
202 public static function isLogType( $type ) {
203 return in_array( $type, LogPage
::validTypes() );
207 * Get the name for the given log type
209 * @param string $type Log type
210 * @return string Log name
211 * @deprecated in 1.19, warnings in 1.21. Use getName()
213 public static function logName( $type ) {
216 wfDeprecated( __METHOD__
, '1.21' );
218 if ( isset( $wgLogNames[$type] ) ) {
219 return str_replace( '_', ' ', wfMessage( $wgLogNames[$type] )->text() );
221 // Bogus log types? Perhaps an extension was removed.
227 * Get the log header for the given log type
229 * @todo handle missing log types
230 * @param string $type logtype
231 * @return string Header text of this logtype
232 * @deprecated in 1.19, warnings in 1.21. Use getDescription()
234 public static function logHeader( $type ) {
235 global $wgLogHeaders;
237 wfDeprecated( __METHOD__
, '1.21' );
239 return wfMessage( $wgLogHeaders[$type] )->parse();
243 * Generate text for a log entry.
244 * Only LogFormatter should call this function.
246 * @param string $type log type
247 * @param string $action log action
248 * @param Title|null $title Title object or null
249 * @param Skin|null $skin Skin object or null. If null, we want to use the wiki
250 * content language, since that will go to the IRC feed.
251 * @param array $params parameters
252 * @param bool $filterWikilinks Whether to filter wiki links
253 * @return string HTML
255 public static function actionText( $type, $action, $title = null, $skin = null,
256 $params = array(), $filterWikilinks = false
258 global $wgLang, $wgContLang, $wgLogActions;
260 if ( is_null( $skin ) ) {
261 $langObj = $wgContLang;
262 $langObjOrNull = null;
265 $langObjOrNull = $wgLang;
268 $key = "$type/$action";
270 if ( isset( $wgLogActions[$key] ) ) {
271 if ( is_null( $title ) ) {
272 $rv = wfMessage( $wgLogActions[$key] )->inLanguage( $langObj )->escaped();
274 $titleLink = self
::getTitleLink( $type, $langObjOrNull, $title, $params );
276 if ( count( $params ) == 0 ) {
277 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $titleLink )
278 ->inLanguage( $langObj )->escaped();
281 array_unshift( $params, $titleLink );
284 if ( preg_match( '/^(block|suppress)\/(block|reblock)$/', $key ) ) {
286 // Localize the duration, and add a tooltip
287 // in English to help visitors from other wikis.
288 // The lrm is needed to make sure that the number
289 // is shown on the correct side of the tooltip text.
290 $durationTooltip = '‎' . htmlspecialchars( $params[1] );
291 $params[1] = "<span class='blockExpiry' title='$durationTooltip'>" .
292 $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
294 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
297 $params[2] = isset( $params[2] ) ?
298 self
::formatBlockFlags( $params[2], $langObj ) : '';
300 } elseif ( $type == 'protect' && count( $params ) == 3 ) {
301 // Restrictions and expiries
303 $details .= $wgLang->getDirMark() . htmlspecialchars( " {$params[1]}" );
305 $details .= " {$params[1]}";
310 $text = wfMessage( 'protect-summary-cascade' )
311 ->inLanguage( $langObj )->text();
313 $details .= wfMessage( 'brackets', $text )->inLanguage( $langObj )->text();
318 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $params )
319 ->inLanguage( $langObj )->escaped() . $details;
323 global $wgLogActionsHandlers;
325 if ( isset( $wgLogActionsHandlers[$key] ) ) {
326 $args = func_get_args();
327 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
329 wfDebug( "LogPage::actionText - unknown action $key\n" );
334 // For the perplexed, this feature was added in r7855 by Erik.
335 // The feature was added because we liked adding [[$1]] in our log entries
336 // but the log entries are parsed as Wikitext on RecentChanges but as HTML
337 // on Special:Log. The hack is essentially that [[$1]] represented a link
338 // to the title in question. The first parameter to the HTML version (Special:Log)
339 // is that link in HTML form, and so this just gets rid of the ugly [[]].
340 // However, this is a horrible hack and it doesn't work like you expect if, say,
341 // you want to link to something OTHER than the title of the log entry.
342 // The real problem, which Erik was trying to fix (and it sort-of works now) is
343 // that the same messages are being treated as both wikitext *and* HTML.
344 if ( $filterWikilinks ) {
345 $rv = str_replace( '[[', '', $rv );
346 $rv = str_replace( ']]', '', $rv );
354 * @param string $type
355 * @param Language|null $lang
356 * @param Title $title
357 * @param array $params
360 protected static function getTitleLink( $type, $lang, $title, &$params ) {
362 return $title->getPrefixedText();
367 $titleLink = Linker
::link(
369 htmlspecialchars( $title->getPrefixedText() ),
371 array( 'redirect' => 'no' )
374 $targetTitle = Title
::newFromText( $params[0] );
376 if ( !$targetTitle ) {
377 # Workaround for broken database
378 $params[0] = htmlspecialchars( $params[0] );
380 $params[0] = Linker
::link(
382 htmlspecialchars( $params[0] )
387 if ( substr( $title->getText(), 0, 1 ) == '#' ) {
388 $titleLink = $title->getText();
390 // @todo Store the user identifier in the parameters
391 // to make this faster for future log entries
392 $id = User
::idFromName( $title->getText() );
393 $titleLink = Linker
::userLink( $id, $title->getText() )
394 . Linker
::userToolLinks( $id, $title->getText(), false, Linker
::TOOL_LINKS_NOBLOCK
);
398 $titleLink = Linker
::link(
400 $title->getPrefixedText(),
402 array( 'redirect' => 'no' )
404 $params[0] = Linker
::link(
405 Title
::newFromText( $params[0] ),
406 htmlspecialchars( $params[0] )
408 $params[1] = $lang->timeanddate( $params[1] );
411 if ( $title->isSpecialPage() ) {
412 list( $name, $par ) = SpecialPageFactory
::resolveAlias( $title->getDBkey() );
414 # Use the language name for log titles, rather than Log/X
415 if ( $name == 'Log' ) {
416 $logPage = new LogPage( $par );
417 $titleLink = Linker
::link( $title, $logPage->getName()->escaped() );
418 $titleLink = wfMessage( 'parentheses' )
419 ->inLanguage( $lang )
420 ->rawParams( $titleLink )
423 $titleLink = Linker
::link( $title );
426 $titleLink = Linker
::link( $title );
436 * @param string $action one of '', 'block', 'protect', 'rights', 'delete',
437 * 'upload', 'move', 'move_redir'
438 * @param Title $target Title object
439 * @param string $comment description associated
440 * @param array $params parameters passed later to wfMessage function
441 * @param null|int|User $doer The user doing the action. null for $wgUser
443 * @return int log_id of the inserted log entry
445 public function addEntry( $action, $target, $comment, $params = array(), $doer = null ) {
448 if ( !is_array( $params ) ) {
449 $params = array( $params );
452 if ( $comment === null ) {
456 # Trim spaces on user supplied text
457 $comment = trim( $comment );
459 # Truncate for whole multibyte characters.
460 $comment = $wgContLang->truncate( $comment, 255 );
462 $this->action
= $action;
463 $this->target
= $target;
464 $this->comment
= $comment;
465 $this->params
= LogPage
::makeParamBlob( $params );
467 if ( $doer === null ) {
470 } elseif ( !is_object( $doer ) ) {
471 $doer = User
::newFromId( $doer );
476 $logEntry = new ManualLogEntry( $this->type
, $action );
477 $logEntry->setTarget( $target );
478 $logEntry->setPerformer( $doer );
479 $logEntry->setParameters( $params );
481 $formatter = LogFormatter
::newFromEntry( $logEntry );
482 $context = RequestContext
::newExtraneousContext( $target );
483 $formatter->setContext( $context );
485 $this->actionText
= $formatter->getPlainActionText();
486 $this->ircActionText
= $formatter->getIRCActionText();
488 return $this->saveContent();
492 * Add relations to log_search table
494 * @param string $field
495 * @param array $values
499 public function addRelations( $field, $values, $logid ) {
500 if ( !strlen( $field ) ||
empty( $values ) ) {
501 return false; // nothing
506 foreach ( $values as $value ) {
508 'ls_field' => $field,
509 'ls_value' => $value,
510 'ls_log_id' => $logid
514 $dbw = wfGetDB( DB_MASTER
);
515 $dbw->insert( 'log_search', $data, __METHOD__
, 'IGNORE' );
521 * Create a blob from a parameter array
523 * @param array $params
526 public static function makeParamBlob( $params ) {
527 return implode( "\n", $params );
531 * Extract a parameter array from a blob
533 * @param string $blob
536 public static function extractParams( $blob ) {
537 if ( $blob === '' ) {
540 return explode( "\n", $blob );
545 * Convert a comma-delimited list of block log flags
546 * into a more readable (and translated) form
548 * @param string $flags Flags to format
549 * @param Language $lang
552 public static function formatBlockFlags( $flags, $lang ) {
553 $flags = trim( $flags );
554 if ( $flags === '' ) {
555 return ''; //nothing to do
557 $flags = explode( ',', $flags );
558 $flagsCount = count( $flags );
560 for ( $i = 0; $i < $flagsCount; $i++
) {
561 $flags[$i] = self
::formatBlockFlag( $flags[$i], $lang );
564 return wfMessage( 'parentheses' )->inLanguage( $lang )
565 ->rawParams( $lang->commaList( $flags ) )->escaped();
569 * Translate a block log flag if possible
571 * @param int $flag Flag to translate
572 * @param Language $lang Language object to use
575 public static function formatBlockFlag( $flag, $lang ) {
576 static $messages = array();
578 if ( !isset( $messages[$flag] ) ) {
579 $messages[$flag] = htmlspecialchars( $flag ); // Fallback
581 // For grepping. The following core messages can be used here:
582 // * block-log-flags-angry-autoblock
583 // * block-log-flags-anononly
584 // * block-log-flags-hiddenname
585 // * block-log-flags-noautoblock
586 // * block-log-flags-nocreate
587 // * block-log-flags-noemail
588 // * block-log-flags-nousertalk
589 $msg = wfMessage( 'block-log-flags-' . $flag )->inLanguage( $lang );
591 if ( $msg->exists() ) {
592 $messages[$flag] = $msg->escaped();
596 return $messages[$flag];
604 public function getName() {
608 if ( isset( $wgLogNames[$this->type
] ) ) {
609 $key = $wgLogNames[$this->type
];
611 $key = 'log-name-' . $this->type
;
614 return wfMessage( $key );
618 * Description of this log type.
622 public function getDescription() {
623 global $wgLogHeaders;
625 if ( isset( $wgLogHeaders[$this->type
] ) ) {
626 $key = $wgLogHeaders[$this->type
];
628 $key = 'log-description-' . $this->type
;
631 return wfMessage( $key );
635 * Returns the right needed to read this log type.
639 public function getRestriction() {
640 global $wgLogRestrictions;
641 if ( isset( $wgLogRestrictions[$this->type
] ) ) {
642 $restriction = $wgLogRestrictions[$this->type
];
644 // '' always returns true with $user->isAllowed()
652 * Tells if this log is not viewable by all.
656 public function isRestricted() {
657 $restriction = $this->getRestriction();
659 return $restriction !== '' && $restriction !== '*';