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, $target, $doer;
43 var $updateRecentChanges, $sendToUDP;
48 * @param $type String: one of '', 'block', 'protect', 'rights', 'delete',
50 * @param $rc Boolean: whether to update recent changes as well as the logging table
51 * @param $udp String: pass 'UDP' to send to the UDP feed if NOT sent to RC
53 public function __construct( $type, $rc = true, $udp = 'skipUDP' ) {
55 $this->updateRecentChanges
= $rc;
56 $this->sendToUDP
= ( $udp == 'UDP' );
60 * @return bool|int|null
62 protected function saveContent() {
63 global $wgLogRestrictions;
65 $dbw = wfGetDB( DB_MASTER
);
66 $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
68 $this->timestamp
= $now = wfTimestampNow();
71 'log_type' => $this->type
,
72 'log_action' => $this->action
,
73 'log_timestamp' => $dbw->timestamp( $now ),
74 'log_user' => $this->doer
->getId(),
75 'log_user_text' => $this->doer
->getName(),
76 'log_namespace' => $this->target
->getNamespace(),
77 'log_title' => $this->target
->getDBkey(),
78 'log_page' => $this->target
->getArticleId(),
79 'log_comment' => $this->comment
,
80 'log_params' => $this->params
82 $dbw->insert( 'logging', $data, __METHOD__
);
83 $newId = !is_null( $log_id ) ?
$log_id : $dbw->insertId();
85 # And update recentchanges
86 if( $this->updateRecentChanges
) {
87 $titleObj = SpecialPage
::getTitleFor( 'Log', $this->type
);
88 RecentChange
::notifyLog(
89 $now, $titleObj, $this->doer
, $this->getRcComment(), '',
90 $this->type
, $this->action
, $this->target
, $this->comment
,
93 } elseif( $this->sendToUDP
) {
94 # Don't send private logs to UDP
95 if( isset( $wgLogRestrictions[$this->type
] ) && $wgLogRestrictions[$this->type
] != '*' ) {
98 # Notify external application via UDP.
99 # We send this to IRC but do not want to add it the RC table.
100 $titleObj = SpecialPage
::getTitleFor( 'Log', $this->type
);
101 $rc = RecentChange
::newLogEntry(
102 $now, $titleObj, $this->doer
, $this->getRcComment(), '',
103 $this->type
, $this->action
, $this->target
, $this->comment
,
104 $this->params
, $newId
112 * Get the RC comment from the last addEntry() call
114 public function getRcComment() {
115 $rcComment = $this->actionText
;
116 if( $this->comment
!= '' ) {
117 if ( $rcComment == '' ) {
118 $rcComment = $this->comment
;
120 $rcComment .= wfMsgForContent( 'colon-separator' ) . $this->comment
;
127 * Get the comment from the last addEntry() call
129 public function getComment() {
130 return $this->comment
;
134 * Get the list of valid log types
136 * @return Array of strings
138 public static function validTypes() {
144 * Is $type a valid log type
146 * @param $type String: log type to check
149 public static function isLogType( $type ) {
150 return in_array( $type, LogPage
::validTypes() );
154 * Get the name for the given log type
156 * @param $type String: logtype
157 * @return String: log name
159 public static function logName( $type ) {
162 if( isset( $wgLogNames[$type] ) ) {
163 return str_replace( '_', ' ', wfMsg( $wgLogNames[$type] ) );
165 // Bogus log types? Perhaps an extension was removed.
171 * Get the log header for the given log type
173 * @todo handle missing log types
174 * @param $type String: logtype
175 * @return String: headertext of this logtype
177 public static function logHeader( $type ) {
178 global $wgLogHeaders;
179 return wfMsgExt( $wgLogHeaders[$type], array( 'parseinline' ) );
183 * Generate text for a log entry
185 * @param $type String: log type
186 * @param $action String: log action
187 * @param $title Mixed: Title object or null
188 * @param $skin Mixed: Skin object or null. If null, we want to use the wiki
189 * content language, since that will go to the IRC feed.
190 * @param $params Array: parameters
191 * @param $filterWikilinks Boolean: whether to filter wiki links
192 * @return HTML string
194 public static function actionText( $type, $action, $title = null, $skin = null,
195 $params = array(), $filterWikilinks = false )
197 global $wgLang, $wgContLang, $wgLogActions;
199 $key = "$type/$action";
200 # Defer patrol log to PatrolLog class
201 if( $key == 'patrol/patrol' ) {
202 return PatrolLog
::makeActionText( $title, $params, $skin );
204 if( isset( $wgLogActions[$key] ) ) {
205 if( is_null( $title ) ) {
206 $rv = wfMsgHtml( $wgLogActions[$key] );
208 $titleLink = self
::getTitleLink( $type, $skin, $title, $params );
209 if( $key == 'rights/rights' ) {
211 $rightsnone = wfMsg( 'rightsnone' );
212 foreach ( $params as &$param ) {
213 $groupArray = array_map( 'trim', explode( ',', $param ) );
214 $groupArray = array_map( array( 'User', 'getGroupMember' ), $groupArray );
215 $param = $wgLang->listToText( $groupArray );
218 $rightsnone = wfMsgForContent( 'rightsnone' );
220 if( !isset( $params[0] ) ||
trim( $params[0] ) == '' ) {
221 $params[0] = $rightsnone;
223 if( !isset( $params[1] ) ||
trim( $params[1] ) == '' ) {
224 $params[1] = $rightsnone;
227 if( count( $params ) == 0 ) {
229 $rv = wfMsgHtml( $wgLogActions[$key], $titleLink );
231 $rv = wfMsgExt( $wgLogActions[$key], array( 'parsemag', 'escape', 'replaceafter', 'content' ), $titleLink );
235 array_unshift( $params, $titleLink );
237 if ( preg_match( '/^(block|suppress)\/(block|reblock)$/', $key ) ) {
239 $params[1] = '<span class="blockExpiry" dir="ltr" title="' . htmlspecialchars( $params[1] ). '">' .
240 $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
242 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
244 $params[2] = isset( $params[2] ) ?
245 self
::formatBlockFlags( $params[2], is_null( $skin ) ) : '';
248 } elseif ( $type == 'protect' && count($params) == 3 ) {
249 // Restrictions and expiries
251 $details .= htmlspecialchars( " {$params[1]}" );
253 $details .= " {$params[1]}";
258 $details .= ' [' . wfMsg( 'protect-summary-cascade' ) . ']';
260 $details .= ' [' . wfMsgForContent( 'protect-summary-cascade' ) . ']';
265 } elseif ( $type == 'move' && count( $params ) == 3 ) {
268 $details .= ' [' . wfMsg( 'move-redirect-suppressed' ) . ']';
270 $details .= ' [' . wfMsgForContent( 'move-redirect-suppressed' ) . ']';
275 } elseif ( preg_match( '/^(delete|suppress)\/revision$/', $key ) && count( $params ) == 5 ) {
276 $count = substr_count( $params[2], ',' ) +
1; // revisions
277 $ofield = intval( substr( $params[3], 7 ) ); // <ofield=x>
278 $nfield = intval( substr( $params[4], 7 ) ); // <nfield=x>
279 $details .= ': ' . RevisionDeleter
::getLogMessage( $count, $nfield, $ofield, false, is_null( $skin ) );
282 } elseif ( preg_match( '/^(delete|suppress)\/event$/', $key ) && count( $params ) == 4 ) {
283 $count = substr_count( $params[1], ',' ) +
1; // log items
284 $ofield = intval( substr( $params[2], 7 ) ); // <ofield=x>
285 $nfield = intval( substr( $params[3], 7 ) ); // <nfield=x>
286 $details .= ': ' . RevisionDeleter
::getLogMessage( $count, $nfield, $ofield, true, is_null( $skin ) );
290 $rv = wfMsgHtml( $wgLogActions[$key], $params ) . $details;
292 $rv = wfMsgExt( $wgLogActions[$key], array( 'parsemag', 'escape', 'replaceafter', 'content' ), $params ) . $details;
297 global $wgLogActionsHandlers;
298 if( isset( $wgLogActionsHandlers[$key] ) ) {
299 $args = func_get_args();
300 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
302 wfDebug( "LogPage::actionText - unknown action $key\n" );
307 // For the perplexed, this feature was added in r7855 by Erik.
308 // The feature was added because we liked adding [[$1]] in our log entries
309 // but the log entries are parsed as Wikitext on RecentChanges but as HTML
310 // on Special:Log. The hack is essentially that [[$1]] represented a link
311 // to the title in question. The first parameter to the HTML version (Special:Log)
312 // is that link in HTML form, and so this just gets rid of the ugly [[]].
313 // However, this is a horrible hack and it doesn't work like you expect if, say,
314 // you want to link to something OTHER than the title of the log entry.
315 // The real problem, which Erik was trying to fix (and it sort-of works now) is
316 // that the same messages are being treated as both wikitext *and* HTML.
317 if( $filterWikilinks ) {
318 $rv = str_replace( '[[', '', $rv );
319 $rv = str_replace( ']]', '', $rv );
326 * @param $type String
328 * @param $title Title
329 * @param $params Array
332 protected static function getTitleLink( $type, $skin, $title, &$params ) {
333 global $wgLang, $wgContLang, $wgUserrightsInterwikiDelimiter;
335 return $title->getPrefixedText();
339 $titleLink = Linker
::link(
341 htmlspecialchars( $title->getPrefixedText() ),
343 array( 'redirect' => 'no' )
345 $targetTitle = Title
::newFromText( $params[0] );
346 if ( !$targetTitle ) {
347 # Workaround for broken database
348 $params[0] = htmlspecialchars( $params[0] );
350 $params[0] = Linker
::link(
352 htmlspecialchars( $params[0] )
357 if( substr( $title->getText(), 0, 1 ) == '#' ) {
358 $titleLink = $title->getText();
360 // TODO: Store the user identifier in the parameters
361 // to make this faster for future log entries
362 $id = User
::idFromName( $title->getText() );
363 $titleLink = Linker
::userLink( $id, $title->getText() )
364 . Linker
::userToolLinks( $id, $title->getText(), false, Linker
::TOOL_LINKS_NOBLOCK
);
368 $text = $wgContLang->ucfirst( $title->getText() );
369 $parts = explode( $wgUserrightsInterwikiDelimiter, $text, 2 );
370 if ( count( $parts ) == 2 ) {
371 $titleLink = WikiMap
::foreignUserLink( $parts[1], $parts[0],
372 htmlspecialchars( $title->getPrefixedText() ) );
373 if ( $titleLink !== false ) {
377 $titleLink = Linker
::link( Title
::makeTitle( NS_USER
, $text ) );
380 $titleLink = Linker
::link(
382 $title->getPrefixedText(),
384 array( 'redirect' => 'no' )
386 $params[0] = Linker
::link(
387 Title
::newFromText( $params[0] ),
388 htmlspecialchars( $params[0] )
390 $params[1] = $wgLang->timeanddate( $params[1] );
393 if( $title->getNamespace() == NS_SPECIAL
) {
394 list( $name, $par ) = SpecialPageFactory
::resolveAlias( $title->getDBkey() );
395 # Use the language name for log titles, rather than Log/X
396 if( $name == 'Log' ) {
397 $titleLink = '(' . Linker
::link( $title, LogPage
::logName( $par ) ) . ')';
399 $titleLink = Linker
::link( $title );
402 $titleLink = Linker
::link( $title );
411 * @param $action String: one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
412 * @param $target Title object
413 * @param $comment String: description associated
414 * @param $params Array: parameters passed later to wfMsg.* functions
415 * @param $doer User object: the user doing the action
417 public function addEntry( $action, $target, $comment, $params = array(), $doer = null ) {
418 if ( !is_array( $params ) ) {
419 $params = array( $params );
422 if ( $comment === null ) {
426 $this->action
= $action;
427 $this->target
= $target;
428 $this->comment
= $comment;
429 $this->params
= LogPage
::makeParamBlob( $params );
431 if ( $doer === null ) {
434 } elseif ( !is_object( $doer ) ) {
435 $doer = User
::newFromId( $doer );
440 $this->actionText
= LogPage
::actionText( $this->type
, $action, $target, null, $params );
442 return $this->saveContent();
446 * Add relations to log_search table
448 * @param $field String
449 * @param $values Array
450 * @param $logid Integer
453 public function addRelations( $field, $values, $logid ) {
454 if( !strlen( $field ) ||
empty( $values ) ) {
455 return false; // nothing
458 foreach( $values as $value ) {
460 'ls_field' => $field,
461 'ls_value' => $value,
462 'ls_log_id' => $logid
465 $dbw = wfGetDB( DB_MASTER
);
466 $dbw->insert( 'log_search', $data, __METHOD__
, 'IGNORE' );
471 * Create a blob from a parameter array
473 * @param $params Array
476 public static function makeParamBlob( $params ) {
477 return implode( "\n", $params );
481 * Extract a parameter array from a blob
483 * @param $blob String
486 public static function extractParams( $blob ) {
487 if ( $blob === '' ) {
490 return explode( "\n", $blob );
495 * Convert a comma-delimited list of block log flags
496 * into a more readable (and translated) form
498 * @param $flags Flags to format
499 * @param $forContent Whether to localize the message depending of the user
503 public static function formatBlockFlags( $flags, $forContent = false ) {
506 $flags = explode( ',', trim( $flags ) );
507 if( count( $flags ) > 0 ) {
508 for( $i = 0; $i < count( $flags ); $i++
) {
509 $flags[$i] = self
::formatBlockFlag( $flags[$i], $forContent );
511 return '(' . $wgLang->commaList( $flags ) . ')';
518 * Translate a block log flag if possible
520 * @param $flag int Flag to translate
521 * @param $forContent Whether to localize the message depending of the user
525 public static function formatBlockFlag( $flag, $forContent = false ) {
526 static $messages = array();
527 if( !isset( $messages[$flag] ) ) {
528 $messages[$flag] = htmlspecialchars( $flag ); // Fallback
530 $msg = wfMessage( 'block-log-flags-' . $flag );
532 $msg->inContentLanguage();
534 if ( $msg->exists() ) {
535 $messages[$flag] = $msg->escaped();
538 return $messages[$flag];
543 * Aliases for backwards compatibility with 1.6
545 define( 'MW_LOG_DELETED_ACTION', LogPage
::DELETED_ACTION
);
546 define( 'MW_LOG_DELETED_USER', LogPage
::DELETED_USER
);
547 define( 'MW_LOG_DELETED_COMMENT', LogPage
::DELETED_COMMENT
);
548 define( 'MW_LOG_DELETED_RESTRICTED', LogPage
::DELETED_RESTRICTED
);