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;
38 const SUPPRESSED_USER
= 12;
39 const SUPPRESSED_ACTION
= 9;
41 var $type, $action, $comment, $params;
54 var $updateRecentChanges, $sendToUDP;
59 * @param string $type one of '', 'block', 'protect', 'rights', 'delete',
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' ) {
66 $this->updateRecentChanges
= $rc;
67 $this->sendToUDP
= ( $udp == 'UDP' );
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();
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
] != '*' ) {
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()
126 * Get the RC comment from the last addEntry() call
130 public function getRcComment() {
131 $rcComment = $this->actionText
;
133 if ( $this->comment
!= '' ) {
134 if ( $rcComment == '' ) {
135 $rcComment = $this->comment
;
137 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
146 * Get the RC comment from the last addEntry() call for IRC
150 public function getRcCommentIRC() {
151 $rcComment = $this->ircActionText
;
153 if ( $this->comment
!= '' ) {
154 if ( $rcComment == '' ) {
155 $rcComment = $this->comment
;
157 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
166 * Get the comment from the last addEntry() call
168 public function getComment() {
169 return $this->comment
;
173 * Get the list of valid log types
175 * @return Array of strings
177 public static function validTypes() {
184 * Is $type a valid log type
186 * @param string $type log type to check
189 public static function isLogType( $type ) {
190 return in_array( $type, LogPage
::validTypes() );
194 * Get the name for the given log type
196 * @param string $type logtype
197 * @return String: log name
198 * @deprecated in 1.19, warnings in 1.21. Use getName()
200 public static function logName( $type ) {
203 if ( isset( $wgLogNames[$type] ) ) {
204 return str_replace( '_', ' ', wfMessage( $wgLogNames[$type] )->text() );
206 // Bogus log types? Perhaps an extension was removed.
212 * Get the log header for the given log type
214 * @todo handle missing log types
215 * @param string $type logtype
216 * @return String: headertext of this logtype
217 * @deprecated in 1.19, warnings in 1.21. Use getDescription()
219 public static function logHeader( $type ) {
220 global $wgLogHeaders;
222 return wfMessage( $wgLogHeaders[$type] )->parse();
226 * Generate text for a log entry.
227 * Only LogFormatter should call this function.
229 * @param string $type log type
230 * @param string $action log action
231 * @param $title Mixed: Title object or null
232 * @param $skin Mixed: Skin object or null. If null, we want to use the wiki
233 * content language, since that will go to the IRC feed.
234 * @param array $params parameters
235 * @param $filterWikilinks Boolean: whether to filter wiki links
236 * @return HTML string
238 public static function actionText( $type, $action, $title = null, $skin = null,
239 $params = array(), $filterWikilinks = false
241 global $wgLang, $wgContLang, $wgLogActions;
243 if ( is_null( $skin ) ) {
244 $langObj = $wgContLang;
245 $langObjOrNull = null;
248 $langObjOrNull = $wgLang;
251 $key = "$type/$action";
253 if ( isset( $wgLogActions[$key] ) ) {
254 if ( is_null( $title ) ) {
255 $rv = wfMessage( $wgLogActions[$key] )->inLanguage( $langObj )->escaped();
257 $titleLink = self
::getTitleLink( $type, $langObjOrNull, $title, $params );
259 if ( count( $params ) == 0 ) {
260 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $titleLink )->inLanguage( $langObj )->escaped();
263 array_unshift( $params, $titleLink );
266 if ( preg_match( '/^(block|suppress)\/(block|reblock)$/', $key ) ) {
268 // Localize the duration, and add a tooltip
269 // in English to help visitors from other wikis.
270 // The lrm is needed to make sure that the number
271 // is shown on the correct side of the tooltip text.
272 $durationTooltip = '‎' . htmlspecialchars( $params[1] );
273 $params[1] = "<span class='blockExpiry' title='$durationTooltip'>" .
274 $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
276 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
279 $params[2] = isset( $params[2] ) ?
280 self
::formatBlockFlags( $params[2], $langObj ) : '';
282 } elseif ( $type == 'protect' && count( $params ) == 3 ) {
283 // Restrictions and expiries
285 $details .= $wgLang->getDirMark() . htmlspecialchars( " {$params[1]}" );
287 $details .= " {$params[1]}";
292 $text = wfMessage( 'protect-summary-cascade' )
293 ->inLanguage( $langObj )->text();
295 $details .= wfMessage( 'brackets', $text )->inLanguage( $langObj )->text();
300 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $params )->inLanguage( $langObj )->escaped() . $details;
304 global $wgLogActionsHandlers;
306 if ( isset( $wgLogActionsHandlers[$key] ) ) {
307 $args = func_get_args();
308 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
310 wfDebug( "LogPage::actionText - unknown action $key\n" );
315 // For the perplexed, this feature was added in r7855 by Erik.
316 // The feature was added because we liked adding [[$1]] in our log entries
317 // but the log entries are parsed as Wikitext on RecentChanges but as HTML
318 // on Special:Log. The hack is essentially that [[$1]] represented a link
319 // to the title in question. The first parameter to the HTML version (Special:Log)
320 // is that link in HTML form, and so this just gets rid of the ugly [[]].
321 // However, this is a horrible hack and it doesn't work like you expect if, say,
322 // you want to link to something OTHER than the title of the log entry.
323 // The real problem, which Erik was trying to fix (and it sort-of works now) is
324 // that the same messages are being treated as both wikitext *and* HTML.
325 if ( $filterWikilinks ) {
326 $rv = str_replace( '[[', '', $rv );
327 $rv = str_replace( ']]', '', $rv );
335 * @param $type String
336 * @param $lang Language or null
337 * @param $title Title
338 * @param $params Array
341 protected static function getTitleLink( $type, $lang, $title, &$params ) {
343 return $title->getPrefixedText();
348 $titleLink = Linker
::link(
350 htmlspecialchars( $title->getPrefixedText() ),
352 array( 'redirect' => 'no' )
355 $targetTitle = Title
::newFromText( $params[0] );
357 if ( !$targetTitle ) {
358 # Workaround for broken database
359 $params[0] = htmlspecialchars( $params[0] );
361 $params[0] = Linker
::link(
363 htmlspecialchars( $params[0] )
368 if ( substr( $title->getText(), 0, 1 ) == '#' ) {
369 $titleLink = $title->getText();
371 // @todo Store the user identifier in the parameters
372 // to make this faster for future log entries
373 $id = User
::idFromName( $title->getText() );
374 $titleLink = Linker
::userLink( $id, $title->getText() )
375 . Linker
::userToolLinks( $id, $title->getText(), false, Linker
::TOOL_LINKS_NOBLOCK
);
379 $titleLink = Linker
::link(
381 $title->getPrefixedText(),
383 array( 'redirect' => 'no' )
385 $params[0] = Linker
::link(
386 Title
::newFromText( $params[0] ),
387 htmlspecialchars( $params[0] )
389 $params[1] = $lang->timeanddate( $params[1] );
392 if ( $title->isSpecialPage() ) {
393 list( $name, $par ) = SpecialPageFactory
::resolveAlias( $title->getDBkey() );
395 # Use the language name for log titles, rather than Log/X
396 if ( $name == 'Log' ) {
397 $logPage = new LogPage( $par );
398 $titleLink = Linker
::link( $title, $logPage->getName()->escaped() );
399 $titleLink = wfMessage( 'parentheses' )
400 ->inLanguage( $lang )
401 ->rawParams( $titleLink )
404 $titleLink = Linker
::link( $title );
407 $titleLink = Linker
::link( $title );
417 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
418 * @param $target Title object
419 * @param string $comment description associated
420 * @param array $params parameters passed later to wfMessage function
421 * @param $doer User object: the user doing the action
423 * @return int log_id of the inserted log entry
425 public function addEntry( $action, $target, $comment, $params = array(), $doer = null ) {
428 if ( !is_array( $params ) ) {
429 $params = array( $params );
432 if ( $comment === null ) {
436 # Trim spaces on user supplied text
437 $comment = trim( $comment );
439 # Truncate for whole multibyte characters.
440 $comment = $wgContLang->truncate( $comment, 255 );
442 $this->action
= $action;
443 $this->target
= $target;
444 $this->comment
= $comment;
445 $this->params
= LogPage
::makeParamBlob( $params );
447 if ( $doer === null ) {
450 } elseif ( !is_object( $doer ) ) {
451 $doer = User
::newFromId( $doer );
456 $logEntry = new ManualLogEntry( $this->type
, $action );
457 $logEntry->setTarget( $target );
458 $logEntry->setPerformer( $doer );
459 $logEntry->setParameters( $params );
461 $formatter = LogFormatter
::newFromEntry( $logEntry );
462 $context = RequestContext
::newExtraneousContext( $target );
463 $formatter->setContext( $context );
465 $this->actionText
= $formatter->getPlainActionText();
466 $this->ircActionText
= $formatter->getIRCActionText();
468 return $this->saveContent();
472 * Add relations to log_search table
474 * @param $field String
475 * @param $values Array
476 * @param $logid Integer
479 public function addRelations( $field, $values, $logid ) {
480 if ( !strlen( $field ) ||
empty( $values ) ) {
481 return false; // nothing
486 foreach ( $values as $value ) {
488 'ls_field' => $field,
489 'ls_value' => $value,
490 'ls_log_id' => $logid
494 $dbw = wfGetDB( DB_MASTER
);
495 $dbw->insert( 'log_search', $data, __METHOD__
, 'IGNORE' );
501 * Create a blob from a parameter array
503 * @param $params Array
506 public static function makeParamBlob( $params ) {
507 return implode( "\n", $params );
511 * Extract a parameter array from a blob
513 * @param $blob String
516 public static function extractParams( $blob ) {
517 if ( $blob === '' ) {
520 return explode( "\n", $blob );
525 * Convert a comma-delimited list of block log flags
526 * into a more readable (and translated) form
528 * @param string $flags Flags to format
529 * @param $lang Language object to use
532 public static function formatBlockFlags( $flags, $lang ) {
533 $flags = trim( $flags );
534 if ( $flags === '' ) {
535 return ''; //nothing to do
537 $flags = explode( ',', $flags );
539 for ( $i = 0; $i < count( $flags ); $i++
) {
540 $flags[$i] = self
::formatBlockFlag( $flags[$i], $lang );
543 return wfMessage( 'parentheses' )->inLanguage( $lang )
544 ->rawParams( $lang->commaList( $flags ) )->escaped();
548 * Translate a block log flag if possible
550 * @param int $flag Flag to translate
551 * @param $lang Language object to use
554 public static function formatBlockFlag( $flag, $lang ) {
555 static $messages = array();
557 if ( !isset( $messages[$flag] ) ) {
558 $messages[$flag] = htmlspecialchars( $flag ); // Fallback
560 // For grepping. The following core messages can be used here:
561 // * block-log-flags-angry-autoblock
562 // * block-log-flags-anononly
563 // * block-log-flags-hiddenname
564 // * block-log-flags-noautoblock
565 // * block-log-flags-nocreate
566 // * block-log-flags-noemail
567 // * block-log-flags-nousertalk
568 $msg = wfMessage( 'block-log-flags-' . $flag )->inLanguage( $lang );
570 if ( $msg->exists() ) {
571 $messages[$flag] = $msg->escaped();
575 return $messages[$flag];
583 public function getName() {
587 if ( isset( $wgLogNames[$this->type
] ) ) {
588 $key = $wgLogNames[$this->type
];
590 $key = 'log-name-' . $this->type
;
593 return wfMessage( $key );
597 * Description of this log type.
601 public function getDescription() {
602 global $wgLogHeaders;
604 if ( isset( $wgLogHeaders[$this->type
] ) ) {
605 $key = $wgLogHeaders[$this->type
];
607 $key = 'log-description-' . $this->type
;
610 return wfMessage( $key );
614 * Returns the right needed to read this log type.
618 public function getRestriction() {
619 global $wgLogRestrictions;
620 if ( isset( $wgLogRestrictions[$this->type
] ) ) {
621 $restriction = $wgLogRestrictions[$this->type
];
623 // '' always returns true with $user->isAllowed()
631 * Tells if this log is not viewable by all.
635 public function isRestricted() {
636 $restriction = $this->getRestriction();
638 return $restriction !== '' && $restriction !== '*';