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 bool|int|null
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
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
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 .= wfMsgForContent( 'colon-separator' ) . $this->comment
;
144 * Get the comment from the last addEntry() call
146 public function getComment() {
147 return $this->comment
;
151 * Get the list of valid log types
153 * @return Array of strings
155 public static function validTypes() {
161 * Is $type a valid log type
163 * @param $type String: log type to check
166 public static function isLogType( $type ) {
167 return in_array( $type, LogPage
::validTypes() );
171 * Get the name for the given log type
173 * @param $type String: logtype
174 * @return String: log name
175 * @deprecated in 1.19, warnings in 1.21. Use getName()
177 public static function logName( $type ) {
180 if( isset( $wgLogNames[$type] ) ) {
181 return str_replace( '_', ' ', wfMsg( $wgLogNames[$type] ) );
183 // Bogus log types? Perhaps an extension was removed.
189 * Get the log header for the given log type
191 * @todo handle missing log types
192 * @param $type String: logtype
193 * @return String: headertext of this logtype
194 * @deprecated in 1.19, warnings in 1.21. Use getDescription()
196 public static function logHeader( $type ) {
197 global $wgLogHeaders;
198 return wfMsgExt( $wgLogHeaders[$type], array( 'parseinline' ) );
202 * Generate text for a log entry
204 * @param $type String: log type
205 * @param $action String: log action
206 * @param $title Mixed: Title object or null
207 * @param $skin Mixed: Skin object or null. If null, we want to use the wiki
208 * content language, since that will go to the IRC feed.
209 * @param $params Array: parameters
210 * @param $filterWikilinks Boolean: whether to filter wiki links
211 * @return HTML string
213 public static function actionText( $type, $action, $title = null, $skin = null,
214 $params = array(), $filterWikilinks = false )
216 global $wgLang, $wgContLang, $wgLogActions;
218 if ( is_null( $skin ) ) {
219 $langObj = $wgContLang;
220 $langObjOrNull = null;
223 $langObjOrNull = $wgLang;
226 $key = "$type/$action";
228 if( isset( $wgLogActions[$key] ) ) {
229 if( is_null( $title ) ) {
230 $rv = wfMsgExt( $wgLogActions[$key], array( 'parsemag', 'escape', 'language' => $langObj ) );
232 $titleLink = self
::getTitleLink( $type, $langObjOrNull, $title, $params );
234 if( preg_match( '/^rights\/(rights|autopromote)/', $key ) ) {
235 $rightsnone = wfMsgExt( 'rightsnone', array( 'parsemag', 'language' => $langObj ) );
238 foreach ( $params as &$param ) {
239 $groupArray = array_map( 'trim', explode( ',', $param ) );
240 $groupArray = array_map( array( 'User', 'getGroupMember' ), $groupArray );
241 $param = $wgLang->listToText( $groupArray );
245 if( !isset( $params[0] ) ||
trim( $params[0] ) == '' ) {
246 $params[0] = $rightsnone;
249 if( !isset( $params[1] ) ||
trim( $params[1] ) == '' ) {
250 $params[1] = $rightsnone;
254 if( count( $params ) == 0 ) {
255 $rv = wfMsgExt( $wgLogActions[$key], array( 'parsemag', 'escape', 'replaceafter', 'language' => $langObj ), $titleLink );
258 array_unshift( $params, $titleLink );
261 if ( preg_match( '/^(block|suppress)\/(block|reblock)$/', $key ) ) {
263 $params[1] = '<span class="blockExpiry" dir="ltr" title="' . htmlspecialchars( $params[1] ). '">' .
264 $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
266 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
269 $params[2] = isset( $params[2] ) ?
270 self
::formatBlockFlags( $params[2], $langObj ) : '';
272 } elseif ( $type == 'protect' && count($params) == 3 ) {
273 // Restrictions and expiries
275 $details .= $wgLang->getDirMark() . htmlspecialchars( " {$params[1]}" );
277 $details .= " {$params[1]}";
282 $details .= ' [' . wfMsgExt( 'protect-summary-cascade', array( 'parsemag', 'language' => $langObj ) ) . ']';
286 $rv = wfMsgExt( $wgLogActions[$key], array( 'parsemag', 'escape', 'replaceafter', 'language' => $langObj ), $params ) . $details;
290 global $wgLogActionsHandlers;
292 if( isset( $wgLogActionsHandlers[$key] ) ) {
293 $args = func_get_args();
294 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
296 wfDebug( "LogPage::actionText - unknown action $key\n" );
301 // For the perplexed, this feature was added in r7855 by Erik.
302 // The feature was added because we liked adding [[$1]] in our log entries
303 // but the log entries are parsed as Wikitext on RecentChanges but as HTML
304 // on Special:Log. The hack is essentially that [[$1]] represented a link
305 // to the title in question. The first parameter to the HTML version (Special:Log)
306 // is that link in HTML form, and so this just gets rid of the ugly [[]].
307 // However, this is a horrible hack and it doesn't work like you expect if, say,
308 // you want to link to something OTHER than the title of the log entry.
309 // The real problem, which Erik was trying to fix (and it sort-of works now) is
310 // that the same messages are being treated as both wikitext *and* HTML.
311 if( $filterWikilinks ) {
312 $rv = str_replace( '[[', '', $rv );
313 $rv = str_replace( ']]', '', $rv );
321 * @param $type String
322 * @param $lang Language or null
323 * @param $title Title
324 * @param $params Array
327 protected static function getTitleLink( $type, $lang, $title, &$params ) {
328 global $wgContLang, $wgUserrightsInterwikiDelimiter;
331 return $title->getPrefixedText();
336 $titleLink = Linker
::link(
338 htmlspecialchars( $title->getPrefixedText() ),
340 array( 'redirect' => 'no' )
343 $targetTitle = Title
::newFromText( $params[0] );
345 if ( !$targetTitle ) {
346 # Workaround for broken database
347 $params[0] = htmlspecialchars( $params[0] );
349 $params[0] = Linker
::link(
351 htmlspecialchars( $params[0] )
356 if( substr( $title->getText(), 0, 1 ) == '#' ) {
357 $titleLink = $title->getText();
359 // @todo Store the user identifier in the parameters
360 // to make this faster for future log entries
361 $id = User
::idFromName( $title->getText() );
362 $titleLink = Linker
::userLink( $id, $title->getText() )
363 . Linker
::userToolLinks( $id, $title->getText(), false, Linker
::TOOL_LINKS_NOBLOCK
);
367 $text = $wgContLang->ucfirst( $title->getText() );
368 $parts = explode( $wgUserrightsInterwikiDelimiter, $text, 2 );
370 if ( count( $parts ) == 2 ) {
371 $titleLink = WikiMap
::foreignUserLink( $parts[1], $parts[0],
372 htmlspecialchars( $title->getPrefixedText() ) );
374 if ( $titleLink !== false ) {
378 $titleLink = Linker
::link( Title
::makeTitle( NS_USER
, $text ) );
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->getNamespace() == NS_SPECIAL
) {
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 $titleLink = '(' . Linker
::link( $title, LogPage
::logName( $par ) ) . ')';
401 $titleLink = Linker
::link( $title );
404 $titleLink = Linker
::link( $title );
414 * @param $action String: one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
415 * @param $target Title object
416 * @param $comment String: description associated
417 * @param $params Array: parameters passed later to wfMsg.* functions
418 * @param $doer User object: the user doing the action
420 * @return bool|int|null
422 public function addEntry( $action, $target, $comment, $params = array(), $doer = null ) {
425 if ( !is_array( $params ) ) {
426 $params = array( $params );
429 if ( $comment === null ) {
433 # Truncate for whole multibyte characters.
434 $comment = $wgContLang->truncate( $comment, 255 );
436 $this->action
= $action;
437 $this->target
= $target;
438 $this->comment
= $comment;
439 $this->params
= LogPage
::makeParamBlob( $params );
441 if ( $doer === null ) {
444 } elseif ( !is_object( $doer ) ) {
445 $doer = User
::newFromId( $doer );
450 $this->actionText
= LogPage
::actionText( $this->type
, $action, $target, null, $params );
452 return $this->saveContent();
456 * Add relations to log_search table
458 * @param $field String
459 * @param $values Array
460 * @param $logid Integer
463 public function addRelations( $field, $values, $logid ) {
464 if( !strlen( $field ) ||
empty( $values ) ) {
465 return false; // nothing
470 foreach( $values as $value ) {
472 'ls_field' => $field,
473 'ls_value' => $value,
474 'ls_log_id' => $logid
478 $dbw = wfGetDB( DB_MASTER
);
479 $dbw->insert( 'log_search', $data, __METHOD__
, 'IGNORE' );
485 * Create a blob from a parameter array
487 * @param $params Array
490 public static function makeParamBlob( $params ) {
491 return implode( "\n", $params );
495 * Extract a parameter array from a blob
497 * @param $blob String
500 public static function extractParams( $blob ) {
501 if ( $blob === '' ) {
504 return explode( "\n", $blob );
509 * Convert a comma-delimited list of block log flags
510 * into a more readable (and translated) form
512 * @param $flags Flags to format
513 * @param $lang Language object to use
516 public static function formatBlockFlags( $flags, $lang ) {
517 $flags = explode( ',', trim( $flags ) );
519 if( count( $flags ) > 0 ) {
520 for( $i = 0; $i < count( $flags ); $i++
) {
521 $flags[$i] = self
::formatBlockFlag( $flags[$i], $lang );
523 return '(' . $lang->commaList( $flags ) . ')';
530 * Translate a block log flag if possible
532 * @param $flag int Flag to translate
533 * @param $lang Language object to use
536 public static function formatBlockFlag( $flag, $lang ) {
537 static $messages = array();
539 if( !isset( $messages[$flag] ) ) {
540 $messages[$flag] = htmlspecialchars( $flag ); // Fallback
542 // For grepping. The following core messages can be used here:
543 // * block-log-flags-angry-autoblock
544 // * block-log-flags-anononly
545 // * block-log-flags-hiddenname
546 // * block-log-flags-noautoblock
547 // * block-log-flags-nocreate
548 // * block-log-flags-noemail
549 // * block-log-flags-nousertalk
550 $msg = wfMessage( 'block-log-flags-' . $flag )->inLanguage( $lang );
552 if ( $msg->exists() ) {
553 $messages[$flag] = $msg->escaped();
557 return $messages[$flag];
566 public function getName() {
570 if ( isset( $wgLogNames[$this->type
] ) ) {
571 $key = $wgLogNames[$this->type
];
573 $key = 'log-name-' . $this->type
;
576 return wfMessage( $key );
580 * Description of this log type.
584 public function getDescription() {
585 global $wgLogHeaders;
587 if ( isset( $wgLogHeaders[$this->type
] ) ) {
588 $key = $wgLogHeaders[$this->type
];
590 $key = 'log-description-' . $this->type
;
592 return wfMessage( $key );
596 * Returns the right needed to read this log type.
600 public function getRestriction() {
601 global $wgLogRestrictions;
602 if ( isset( $wgLogRestrictions[$this->type
] ) ) {
603 $restriction = $wgLogRestrictions[$this->type
];
605 // '' always returns true with $user->isAllowed()
612 * Tells if this log is not viewable by all.
616 public function isRestricted() {
617 $restriction = $this->getRestriction();
618 return $restriction !== '' && $restriction !== '*';