d96a5ea51a93b02f64f714dc6f72be279abc1c3f
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 $type String: one of '', 'block', 'protect', 'rights', 'delete',
61 * @param $rc Boolean: whether to update recent changes as well as the logging table
62 * @param $udp String: 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()
125 * Get the RC comment from the last addEntry() call
129 public function getRcComment() {
130 $rcComment = $this->actionText
;
132 if( $this->comment
!= '' ) {
133 if ( $rcComment == '' ) {
134 $rcComment = $this->comment
;
136 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
145 * Get the RC comment from the last addEntry() call for IRC
149 public function getRcCommentIRC() {
150 $rcComment = $this->ircActionText
;
152 if( $this->comment
!= '' ) {
153 if ( $rcComment == '' ) {
154 $rcComment = $this->comment
;
156 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
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() {
182 * Is $type a valid log type
184 * @param $type String: log type to check
187 public static function isLogType( $type ) {
188 return in_array( $type, LogPage
::validTypes() );
192 * Get the name for the given log type
194 * @param $type String: logtype
195 * @return String: log name
196 * @deprecated in 1.19, warnings in 1.21. Use getName()
198 public static function logName( $type ) {
201 if( isset( $wgLogNames[$type] ) ) {
202 return str_replace( '_', ' ', wfMessage( $wgLogNames[$type] )->text() );
204 // Bogus log types? Perhaps an extension was removed.
210 * Get the log header for the given log type
212 * @todo handle missing log types
213 * @param $type String: 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 $type String: log type
227 * @param $action String: 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 $params Array: 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;
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();
254 $titleLink = self
::getTitleLink( $type, $langObjOrNull, $title, $params );
256 if( preg_match( '/^rights\/(rights|autopromote)/', $key ) ) {
257 $rightsnone = wfMessage( 'rightsnone' )->inLanguage( $langObj )->text();
260 $username = $title->getText();
261 foreach ( $params as &$param ) {
262 $groupArray = array_map( 'trim', explode( ',', $param ) );
263 foreach( $groupArray as &$group ) {
264 $group = User
::getGroupMember( $group, $username );
266 $param = $wgLang->listToText( $groupArray );
270 if( !isset( $params[0] ) ||
trim( $params[0] ) == '' ) {
271 $params[0] = $rightsnone;
274 if( !isset( $params[1] ) ||
trim( $params[1] ) == '' ) {
275 $params[1] = $rightsnone;
279 if( count( $params ) == 0 ) {
280 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $titleLink )->inLanguage( $langObj )->escaped();
283 array_unshift( $params, $titleLink );
286 if ( preg_match( '/^(block|suppress)\/(block|reblock)$/', $key ) ) {
288 $params[1] = '<span class="blockExpiry" dir="ltr" title="' . htmlspecialchars( $params[1] ). '">' .
289 $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
291 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
294 $params[2] = isset( $params[2] ) ?
295 self
::formatBlockFlags( $params[2], $langObj ) : '';
297 } elseif ( $type == 'protect' && count($params) == 3 ) {
298 // Restrictions and expiries
300 $details .= $wgLang->getDirMark() . htmlspecialchars( " {$params[1]}" );
302 $details .= " {$params[1]}";
307 $details .= ' [' . wfMessage( 'protect-summary-cascade' )->inLanguage( $langObj )->text() . ']';
311 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $params )->inLanguage( $langObj )->escaped() . $details;
315 global $wgLogActionsHandlers;
317 if( isset( $wgLogActionsHandlers[$key] ) ) {
318 $args = func_get_args();
319 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
321 wfDebug( "LogPage::actionText - unknown action $key\n" );
326 // For the perplexed, this feature was added in r7855 by Erik.
327 // The feature was added because we liked adding [[$1]] in our log entries
328 // but the log entries are parsed as Wikitext on RecentChanges but as HTML
329 // on Special:Log. The hack is essentially that [[$1]] represented a link
330 // to the title in question. The first parameter to the HTML version (Special:Log)
331 // is that link in HTML form, and so this just gets rid of the ugly [[]].
332 // However, this is a horrible hack and it doesn't work like you expect if, say,
333 // you want to link to something OTHER than the title of the log entry.
334 // The real problem, which Erik was trying to fix (and it sort-of works now) is
335 // that the same messages are being treated as both wikitext *and* HTML.
336 if( $filterWikilinks ) {
337 $rv = str_replace( '[[', '', $rv );
338 $rv = str_replace( ']]', '', $rv );
346 * @param $type String
347 * @param $lang Language or null
348 * @param $title Title
349 * @param $params Array
352 protected static function getTitleLink( $type, $lang, $title, &$params ) {
353 global $wgContLang, $wgUserrightsInterwikiDelimiter;
356 return $title->getPrefixedText();
361 $titleLink = Linker
::link(
363 htmlspecialchars( $title->getPrefixedText() ),
365 array( 'redirect' => 'no' )
368 $targetTitle = Title
::newFromText( $params[0] );
370 if ( !$targetTitle ) {
371 # Workaround for broken database
372 $params[0] = htmlspecialchars( $params[0] );
374 $params[0] = Linker
::link(
376 htmlspecialchars( $params[0] )
381 if( substr( $title->getText(), 0, 1 ) == '#' ) {
382 $titleLink = $title->getText();
384 // @todo Store the user identifier in the parameters
385 // to make this faster for future log entries
386 $id = User
::idFromName( $title->getText() );
387 $titleLink = Linker
::userLink( $id, $title->getText() )
388 . Linker
::userToolLinks( $id, $title->getText(), false, Linker
::TOOL_LINKS_NOBLOCK
);
392 $text = $wgContLang->ucfirst( $title->getText() );
393 $parts = explode( $wgUserrightsInterwikiDelimiter, $text, 2 );
395 if ( count( $parts ) == 2 ) {
396 $titleLink = WikiMap
::foreignUserLink( $parts[1], $parts[0],
397 htmlspecialchars( $title->getPrefixedText() ) );
399 if ( $titleLink !== false ) {
403 $titleLink = Linker
::link( Title
::makeTitle( NS_USER
, $text ) );
406 $titleLink = Linker
::link(
408 $title->getPrefixedText(),
410 array( 'redirect' => 'no' )
412 $params[0] = Linker
::link(
413 Title
::newFromText( $params[0] ),
414 htmlspecialchars( $params[0] )
416 $params[1] = $lang->timeanddate( $params[1] );
419 if( $title->isSpecialPage() ) {
420 list( $name, $par ) = SpecialPageFactory
::resolveAlias( $title->getDBkey() );
422 # Use the language name for log titles, rather than Log/X
423 if( $name == 'Log' ) {
424 $logPage = new LogPage( $par );
425 $titleLink = Linker
::link( $title, $logPage->getName()->escaped() );
426 $titleLink = wfMessage( 'parentheses' )
427 ->inLanguage( $lang )
428 ->rawParams( $titleLink )
431 $titleLink = Linker
::link( $title );
434 $titleLink = Linker
::link( $title );
444 * @param $action String: one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
445 * @param $target Title object
446 * @param $comment String: description associated
447 * @param $params Array: parameters passed later to wfMessage function
448 * @param $doer User object: the user doing the action
450 * @return int log_id of the inserted log entry
452 public function addEntry( $action, $target, $comment, $params = array(), $doer = null ) {
455 if ( !is_array( $params ) ) {
456 $params = array( $params );
459 if ( $comment === null ) {
463 # Truncate for whole multibyte characters.
464 $comment = $wgContLang->truncate( $comment, 255 );
466 $this->action
= $action;
467 $this->target
= $target;
468 $this->comment
= $comment;
469 $this->params
= LogPage
::makeParamBlob( $params );
471 if ( $doer === null ) {
474 } elseif ( !is_object( $doer ) ) {
475 $doer = User
::newFromId( $doer );
480 $logEntry = new ManualLogEntry( $this->type
, $action );
481 $logEntry->setTarget( $target );
482 $logEntry->setPerformer( $doer );
483 $logEntry->setParameters( $params );
485 $formatter = LogFormatter
::newFromEntry( $logEntry );
486 $context = RequestContext
::newExtraneousContext( $target );
487 $formatter->setContext( $context );
489 $this->actionText
= $formatter->getPlainActionText();
490 $this->ircActionText
= $formatter->getIRCActionText();
492 return $this->saveContent();
496 * Add relations to log_search table
498 * @param $field String
499 * @param $values Array
500 * @param $logid Integer
503 public function addRelations( $field, $values, $logid ) {
504 if( !strlen( $field ) ||
empty( $values ) ) {
505 return false; // nothing
510 foreach( $values as $value ) {
512 'ls_field' => $field,
513 'ls_value' => $value,
514 'ls_log_id' => $logid
518 $dbw = wfGetDB( DB_MASTER
);
519 $dbw->insert( 'log_search', $data, __METHOD__
, 'IGNORE' );
525 * Create a blob from a parameter array
527 * @param $params Array
530 public static function makeParamBlob( $params ) {
531 return implode( "\n", $params );
535 * Extract a parameter array from a blob
537 * @param $blob String
540 public static function extractParams( $blob ) {
541 if ( $blob === '' ) {
544 return explode( "\n", $blob );
549 * Convert a comma-delimited list of block log flags
550 * into a more readable (and translated) form
552 * @param $flags string Flags to format
553 * @param $lang Language object to use
556 public static function formatBlockFlags( $flags, $lang ) {
557 $flags = explode( ',', trim( $flags ) );
559 if( count( $flags ) > 0 ) {
560 for( $i = 0; $i < count( $flags ); $i++
) {
561 $flags[$i] = self
::formatBlockFlag( $flags[$i], $lang );
563 return wfMessage( 'parentheses' )->inLanguage( $lang )
564 ->rawParams( $lang->commaList( $flags ) )->escaped();
571 * Translate a block log flag if possible
573 * @param $flag int Flag to translate
574 * @param $lang Language object to use
577 public static function formatBlockFlag( $flag, $lang ) {
578 static $messages = array();
580 if( !isset( $messages[$flag] ) ) {
581 $messages[$flag] = htmlspecialchars( $flag ); // Fallback
583 // For grepping. The following core messages can be used here:
584 // * block-log-flags-angry-autoblock
585 // * block-log-flags-anononly
586 // * block-log-flags-hiddenname
587 // * block-log-flags-noautoblock
588 // * block-log-flags-nocreate
589 // * block-log-flags-noemail
590 // * block-log-flags-nousertalk
591 $msg = wfMessage( 'block-log-flags-' . $flag )->inLanguage( $lang );
593 if ( $msg->exists() ) {
594 $messages[$flag] = $msg->escaped();
598 return $messages[$flag];
607 public function getName() {
611 if ( isset( $wgLogNames[$this->type
] ) ) {
612 $key = $wgLogNames[$this->type
];
614 $key = 'log-name-' . $this->type
;
617 return wfMessage( $key );
621 * Description of this log type.
625 public function getDescription() {
626 global $wgLogHeaders;
628 if ( isset( $wgLogHeaders[$this->type
] ) ) {
629 $key = $wgLogHeaders[$this->type
];
631 $key = 'log-description-' . $this->type
;
633 return wfMessage( $key );
637 * Returns the right needed to read this log type.
641 public function getRestriction() {
642 global $wgLogRestrictions;
643 if ( isset( $wgLogRestrictions[$this->type
] ) ) {
644 $restriction = $wgLogRestrictions[$this->type
];
646 // '' always returns true with $user->isAllowed()
653 * Tells if this log is not viewable by all.
657 public function isRestricted() {
658 $restriction = $this->getRestriction();
659 return $restriction !== '' && $restriction !== '*';