51c41a8fe1b659be9ba8b4f28ae136e985653afc
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 )
261 ->inLanguage( $langObj )->escaped();
264 array_unshift( $params, $titleLink );
267 if ( preg_match( '/^(block|suppress)\/(block|reblock)$/', $key ) ) {
269 // Localize the duration, and add a tooltip
270 // in English to help visitors from other wikis.
271 // The lrm is needed to make sure that the number
272 // is shown on the correct side of the tooltip text.
273 $durationTooltip = '‎' . htmlspecialchars( $params[1] );
274 $params[1] = "<span class='blockExpiry' title='$durationTooltip'>" .
275 $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
277 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
280 $params[2] = isset( $params[2] ) ?
281 self
::formatBlockFlags( $params[2], $langObj ) : '';
283 } elseif ( $type == 'protect' && count( $params ) == 3 ) {
284 // Restrictions and expiries
286 $details .= $wgLang->getDirMark() . htmlspecialchars( " {$params[1]}" );
288 $details .= " {$params[1]}";
293 $text = wfMessage( 'protect-summary-cascade' )
294 ->inLanguage( $langObj )->text();
296 $details .= wfMessage( 'brackets', $text )->inLanguage( $langObj )->text();
301 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $params )
302 ->inLanguage( $langObj )->escaped() . $details;
306 global $wgLogActionsHandlers;
308 if ( isset( $wgLogActionsHandlers[$key] ) ) {
309 $args = func_get_args();
310 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
312 wfDebug( "LogPage::actionText - unknown action $key\n" );
317 // For the perplexed, this feature was added in r7855 by Erik.
318 // The feature was added because we liked adding [[$1]] in our log entries
319 // but the log entries are parsed as Wikitext on RecentChanges but as HTML
320 // on Special:Log. The hack is essentially that [[$1]] represented a link
321 // to the title in question. The first parameter to the HTML version (Special:Log)
322 // is that link in HTML form, and so this just gets rid of the ugly [[]].
323 // However, this is a horrible hack and it doesn't work like you expect if, say,
324 // you want to link to something OTHER than the title of the log entry.
325 // The real problem, which Erik was trying to fix (and it sort-of works now) is
326 // that the same messages are being treated as both wikitext *and* HTML.
327 if ( $filterWikilinks ) {
328 $rv = str_replace( '[[', '', $rv );
329 $rv = str_replace( ']]', '', $rv );
337 * @param $type String
338 * @param $lang Language or null
339 * @param $title Title
340 * @param $params Array
343 protected static function getTitleLink( $type, $lang, $title, &$params ) {
345 return $title->getPrefixedText();
350 $titleLink = Linker
::link(
352 htmlspecialchars( $title->getPrefixedText() ),
354 array( 'redirect' => 'no' )
357 $targetTitle = Title
::newFromText( $params[0] );
359 if ( !$targetTitle ) {
360 # Workaround for broken database
361 $params[0] = htmlspecialchars( $params[0] );
363 $params[0] = Linker
::link(
365 htmlspecialchars( $params[0] )
370 if ( substr( $title->getText(), 0, 1 ) == '#' ) {
371 $titleLink = $title->getText();
373 // @todo Store the user identifier in the parameters
374 // to make this faster for future log entries
375 $id = User
::idFromName( $title->getText() );
376 $titleLink = Linker
::userLink( $id, $title->getText() )
377 . Linker
::userToolLinks( $id, $title->getText(), false, Linker
::TOOL_LINKS_NOBLOCK
);
381 $titleLink = Linker
::link(
383 $title->getPrefixedText(),
385 array( 'redirect' => 'no' )
387 $params[0] = Linker
::link(
388 Title
::newFromText( $params[0] ),
389 htmlspecialchars( $params[0] )
391 $params[1] = $lang->timeanddate( $params[1] );
394 if ( $title->isSpecialPage() ) {
395 list( $name, $par ) = SpecialPageFactory
::resolveAlias( $title->getDBkey() );
397 # Use the language name for log titles, rather than Log/X
398 if ( $name == 'Log' ) {
399 $logPage = new LogPage( $par );
400 $titleLink = Linker
::link( $title, $logPage->getName()->escaped() );
401 $titleLink = wfMessage( 'parentheses' )
402 ->inLanguage( $lang )
403 ->rawParams( $titleLink )
406 $titleLink = Linker
::link( $title );
409 $titleLink = Linker
::link( $title );
419 * @param string $action one of '', 'block', 'protect', 'rights', 'delete',
420 * 'upload', 'move', 'move_redir'
421 * @param $target Title object
422 * @param string $comment description associated
423 * @param array $params parameters passed later to wfMessage function
424 * @param $doer User object: the user doing the action
426 * @return int log_id of the inserted log entry
428 public function addEntry( $action, $target, $comment, $params = array(), $doer = null ) {
431 if ( !is_array( $params ) ) {
432 $params = array( $params );
435 if ( $comment === null ) {
439 # Trim spaces on user supplied text
440 $comment = trim( $comment );
442 # Truncate for whole multibyte characters.
443 $comment = $wgContLang->truncate( $comment, 255 );
445 $this->action
= $action;
446 $this->target
= $target;
447 $this->comment
= $comment;
448 $this->params
= LogPage
::makeParamBlob( $params );
450 if ( $doer === null ) {
453 } elseif ( !is_object( $doer ) ) {
454 $doer = User
::newFromId( $doer );
459 $logEntry = new ManualLogEntry( $this->type
, $action );
460 $logEntry->setTarget( $target );
461 $logEntry->setPerformer( $doer );
462 $logEntry->setParameters( $params );
464 $formatter = LogFormatter
::newFromEntry( $logEntry );
465 $context = RequestContext
::newExtraneousContext( $target );
466 $formatter->setContext( $context );
468 $this->actionText
= $formatter->getPlainActionText();
469 $this->ircActionText
= $formatter->getIRCActionText();
471 return $this->saveContent();
475 * Add relations to log_search table
477 * @param $field String
478 * @param $values Array
479 * @param $logid Integer
482 public function addRelations( $field, $values, $logid ) {
483 if ( !strlen( $field ) ||
empty( $values ) ) {
484 return false; // nothing
489 foreach ( $values as $value ) {
491 'ls_field' => $field,
492 'ls_value' => $value,
493 'ls_log_id' => $logid
497 $dbw = wfGetDB( DB_MASTER
);
498 $dbw->insert( 'log_search', $data, __METHOD__
, 'IGNORE' );
504 * Create a blob from a parameter array
506 * @param $params Array
509 public static function makeParamBlob( $params ) {
510 return implode( "\n", $params );
514 * Extract a parameter array from a blob
516 * @param $blob String
519 public static function extractParams( $blob ) {
520 if ( $blob === '' ) {
523 return explode( "\n", $blob );
528 * Convert a comma-delimited list of block log flags
529 * into a more readable (and translated) form
531 * @param string $flags Flags to format
532 * @param $lang Language object to use
535 public static function formatBlockFlags( $flags, $lang ) {
536 $flags = trim( $flags );
537 if ( $flags === '' ) {
538 return ''; //nothing to do
540 $flags = explode( ',', $flags );
541 $flagsCount = count( $flags );
543 for ( $i = 0; $i < $flagsCount; $i++
) {
544 $flags[$i] = self
::formatBlockFlag( $flags[$i], $lang );
547 return wfMessage( 'parentheses' )->inLanguage( $lang )
548 ->rawParams( $lang->commaList( $flags ) )->escaped();
552 * Translate a block log flag if possible
554 * @param int $flag Flag to translate
555 * @param $lang Language object to use
558 public static function formatBlockFlag( $flag, $lang ) {
559 static $messages = array();
561 if ( !isset( $messages[$flag] ) ) {
562 $messages[$flag] = htmlspecialchars( $flag ); // Fallback
564 // For grepping. The following core messages can be used here:
565 // * block-log-flags-angry-autoblock
566 // * block-log-flags-anononly
567 // * block-log-flags-hiddenname
568 // * block-log-flags-noautoblock
569 // * block-log-flags-nocreate
570 // * block-log-flags-noemail
571 // * block-log-flags-nousertalk
572 $msg = wfMessage( 'block-log-flags-' . $flag )->inLanguage( $lang );
574 if ( $msg->exists() ) {
575 $messages[$flag] = $msg->escaped();
579 return $messages[$flag];
587 public function getName() {
591 if ( isset( $wgLogNames[$this->type
] ) ) {
592 $key = $wgLogNames[$this->type
];
594 $key = 'log-name-' . $this->type
;
597 return wfMessage( $key );
601 * Description of this log type.
605 public function getDescription() {
606 global $wgLogHeaders;
608 if ( isset( $wgLogHeaders[$this->type
] ) ) {
609 $key = $wgLogHeaders[$this->type
];
611 $key = 'log-description-' . $this->type
;
614 return wfMessage( $key );
618 * Returns the right needed to read this log type.
622 public function getRestriction() {
623 global $wgLogRestrictions;
624 if ( isset( $wgLogRestrictions[$this->type
] ) ) {
625 $restriction = $wgLogRestrictions[$this->type
];
627 // '' always returns true with $user->isAllowed()
635 * Tells if this log is not viewable by all.
639 public function isRestricted() {
640 $restriction = $this->getRestriction();
642 return $restriction !== '' && $restriction !== '*';