3 # Copyright (C) 2002, 2004 Brion Vibber <brion@pobox.com>
4 # http://www.mediawiki.org/
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with this program; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # 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 var $type, $action, $comment, $params, $target, $doer;
40 var $updateRecentChanges, $sendToUDP;
45 * @param string $type One of '', 'block', 'protect', 'rights', 'delete',
47 * @param bool $rc Whether to update recent changes as well as the logging table
48 * @param bool $udp Whether to send to the UDP feed if NOT sent to RC
50 public function __construct( $type, $rc = true, $udp = 'skipUDP' ) {
52 $this->updateRecentChanges
= $rc;
53 $this->sendToUDP
= ($udp == 'UDP');
56 protected function saveContent() {
57 global $wgLogRestrictions;
59 $dbw = wfGetDB( DB_MASTER
);
60 $log_id = $dbw->nextSequenceValue( 'log_log_id_seq' );
62 $this->timestamp
= $now = wfTimestampNow();
65 'log_type' => $this->type
,
66 'log_action' => $this->action
,
67 'log_timestamp' => $dbw->timestamp( $now ),
68 'log_user' => $this->doer
->getId(),
69 'log_user_text' => $this->doer
->getName(),
70 'log_namespace' => $this->target
->getNamespace(),
71 'log_title' => $this->target
->getDBkey(),
72 'log_comment' => $this->comment
,
73 'log_params' => $this->params
75 $dbw->insert( 'logging', $data, __METHOD__
);
76 $newId = !is_null($log_id) ?
$log_id : $dbw->insertId();
78 # And update recentchanges
79 if( $this->updateRecentChanges
) {
80 $titleObj = SpecialPage
::getTitleFor( 'Log', $this->type
);
81 RecentChange
::notifyLog( $now, $titleObj, $this->doer
, $this->getRcComment(), '', $this->type
,
82 $this->action
, $this->target
, $this->comment
, $this->params
, $newId );
83 } else if( $this->sendToUDP
) {
84 # Don't send private logs to UDP
85 if( isset($wgLogRestrictions[$this->type
]) && $wgLogRestrictions[$this->type
] !='*' ) {
88 # Notify external application via UDP.
89 # We send this to IRC but do not want to add it the RC table.
90 $titleObj = SpecialPage
::getTitleFor( 'Log', $this->type
);
91 $rc = RecentChange
::newLogEntry( $now, $titleObj, $this->doer
, $this->getRcComment(), '',
92 $this->type
, $this->action
, $this->target
, $this->comment
, $this->params
, $newId );
99 * Get the RC comment from the last addEntry() call
101 public function getRcComment() {
102 $rcComment = $this->actionText
;
103 if( '' != $this->comment
) {
104 if ($rcComment == '')
105 $rcComment = $this->comment
;
107 $rcComment .= wfMsgForContent( 'colon-separator' ) . $this->comment
;
113 * Get the comment from the last addEntry() call
115 public function getComment() {
116 return $this->comment
;
122 public static function validTypes() {
130 public static function isLogType( $type ) {
131 return in_array( $type, LogPage
::validTypes() );
137 public static function logName( $type ) {
138 global $wgLogNames, $wgMessageCache;
140 if( isset( $wgLogNames[$type] ) ) {
141 $wgMessageCache->loadAllMessages();
142 return str_replace( '_', ' ', wfMsg( $wgLogNames[$type] ) );
144 // Bogus log types? Perhaps an extension was removed.
150 * @todo handle missing log types
151 * @param string $type logtype
152 * @return string Headertext of this logtype
154 public static function logHeader( $type ) {
155 global $wgLogHeaders, $wgMessageCache;
156 $wgMessageCache->loadAllMessages();
157 return wfMsgExt($wgLogHeaders[$type],array('parseinline'));
162 * @return HTML string
164 public static function actionText( $type, $action, $title = NULL, $skin = NULL,
165 $params = array(), $filterWikilinks = false )
167 global $wgLang, $wgContLang, $wgLogActions, $wgMessageCache;
169 $wgMessageCache->loadAllMessages();
170 $key = "$type/$action";
171 # Defer patrol log to PatrolLog class
172 if( $key == 'patrol/patrol' ) {
173 return PatrolLog
::makeActionText( $title, $params, $skin );
175 if( isset( $wgLogActions[$key] ) ) {
176 if( is_null( $title ) ) {
177 $rv = wfMsgHtml( $wgLogActions[$key] );
179 $titleLink = self
::getTitleLink( $type, $skin, $title, $params );
180 if( $key == 'rights/rights' ) {
182 $rightsnone = wfMsg( 'rightsnone' );
183 foreach ( $params as &$param ) {
184 $groupArray = array_map( 'trim', explode( ',', $param ) );
185 $groupArray = array_map( array( 'User', 'getGroupName' ), $groupArray );
186 $param = $wgLang->listToText( $groupArray );
189 $rightsnone = wfMsgForContent( 'rightsnone' );
191 if( !isset( $params[0] ) ||
trim( $params[0] ) == '' )
192 $params[0] = $rightsnone;
193 if( !isset( $params[1] ) ||
trim( $params[1] ) == '' )
194 $params[1] = $rightsnone;
196 if( count( $params ) == 0 ) {
198 $rv = wfMsgHtml( $wgLogActions[$key], $titleLink );
200 $rv = wfMsgExt( $wgLogActions[$key], array( 'parsemag', 'escape', 'replaceafter', 'content' ), $titleLink );
204 array_unshift( $params, $titleLink );
206 if ( preg_match( '/^(block|suppress)\/(block|reblock)$/', $key ) ) {
208 $params[1] = '<span title="' . htmlspecialchars( $params[1] ). '">' .
209 $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
211 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
213 $params[2] = isset( $params[2] ) ?
214 self
::formatBlockFlags( $params[2], is_null( $skin ) ) : '';
216 } else if ( $type == 'protect' && count($params) == 3 ) {
219 $details .= htmlspecialchars( " {$params[1]}" ); // restrictions and expiries
220 $details .= ' ['.wfMsg('protect-summary-cascade').']';
222 $details .= " {$params[1]}";
223 $details .= ' ['.wfMsgForContent('protect-summary-cascade').']';
227 } else if ( $type == 'move' && count( $params ) == 3 ) {
230 $details .= ' [' . wfMsg( 'move-redirect-suppressed' ) . ']';
232 $details .= ' [' . wfMsgForContent( 'move-redirect-suppressed' ) . ']';
236 } else if ( preg_match( '/^(delete|suppress)\/revision$/', $key ) && count( $params ) == 5 ) {
237 $count = substr_count( $params[2], ',' ) +
1; // revisions
238 $ofield = intval( substr( $params[3], 7 ) ); // <ofield=x>
239 $nfield = intval( substr( $params[4], 7 ) ); // <nfield=x>
240 $details .= ': '.RevisionDeleter
::getLogMessage( $count, $nfield, $ofield, false );
242 } else if ( preg_match( '/^(delete|suppress)\/event$/', $key ) && count( $params ) == 4 ) {
243 $count = substr_count( $params[1], ',' ) +
1; // log items
244 $ofield = intval( substr( $params[2], 7 ) ); // <ofield=x>
245 $nfield = intval( substr( $params[3], 7 ) ); // <nfield=x>
246 $details .= ': '.RevisionDeleter
::getLogMessage( $count, $nfield, $ofield, true );
249 $rv = htmlspecialchars( wfMsg( $wgLogActions[$key], $params ) ) . $details;
251 $rv = wfMsgExt( $wgLogActions[$key], array( 'parsemag', 'escape', 'replaceafter', 'content' ), $params ) . $details;
256 global $wgLogActionsHandlers;
257 if( isset( $wgLogActionsHandlers[$key] ) ) {
258 $args = func_get_args();
259 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
261 wfDebug( "LogPage::actionText - unknown action $key\n" );
266 // For the perplexed, this feature was added in r7855 by Erik.
267 // The feature was added because we liked adding [[$1]] in our log entries
268 // but the log entries are parsed as Wikitext on RecentChanges but as HTML
269 // on Special:Log. The hack is essentially that [[$1]] represented a link
270 // to the title in question. The first parameter to the HTML version (Special:Log)
271 // is that link in HTML form, and so this just gets rid of the ugly [[]].
272 // However, this is a horrible hack and it doesn't work like you expect if, say,
273 // you want to link to something OTHER than the title of the log entry.
274 // The real problem, which Erik was trying to fix (and it sort-of works now) is
275 // that the same messages are being treated as both wikitext *and* HTML.
276 if( $filterWikilinks ) {
277 $rv = str_replace( "[[", "", $rv );
278 $rv = str_replace( "]]", "", $rv );
283 protected static function getTitleLink( $type, $skin, $title, &$params ) {
284 global $wgLang, $wgContLang;
286 return $title->getPrefixedText();
290 $titleLink = $skin->link(
292 htmlspecialchars( $title->getPrefixedText() ),
294 array( 'redirect' => 'no' )
296 $targetTitle = Title
::newFromText( $params[0] );
297 if ( !$targetTitle ) {
298 # Workaround for broken database
299 $params[0] = htmlspecialchars( $params[0] );
301 $params[0] = $skin->link(
303 htmlspecialchars( $params[0] )
308 if( substr( $title->getText(), 0, 1 ) == '#' ) {
309 $titleLink = $title->getText();
311 // TODO: Store the user identifier in the parameters
312 // to make this faster for future log entries
313 $id = User
::idFromName( $title->getText() );
314 $titleLink = $skin->userLink( $id, $title->getText() )
315 . $skin->userToolLinks( $id, $title->getText(), false, Linker
::TOOL_LINKS_NOBLOCK
);
319 $text = $wgContLang->ucfirst( $title->getText() );
320 $titleLink = $skin->link( Title
::makeTitle( NS_USER
, $text ) );
323 $titleLink = $skin->link(
325 $title->getPrefixedText(),
327 array( 'redirect' => 'no' )
329 $params[0] = $skin->link(
330 Title
::newFromText( $params[0] ),
331 htmlspecialchars( $params[0] )
333 $params[1] = $wgLang->timeanddate( $params[1] );
336 if( $title->getNamespace() == NS_SPECIAL
) {
337 list( $name, $par ) = SpecialPage
::resolveAliasWithSubpage( $title->getDBkey() );
338 # Use the language name for log titles, rather than Log/X
339 if( $name == 'Log' ) {
340 $titleLink = '('.$skin->link( $title, LogPage
::logName( $par ) ).')';
342 $titleLink = $skin->link( $title );
345 $titleLink = $skin->link( $title );
353 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
354 * @param object &$target A title object.
355 * @param string $comment Description associated
356 * @param array $params Parameters passed later to wfMsg.* functions
357 * @param User $doer The user doing the action
359 public function addEntry( $action, $target, $comment, $params = array(), $doer = null ) {
360 if ( !is_array( $params ) ) {
361 $params = array( $params );
364 $this->action
= $action;
365 $this->target
= $target;
366 $this->comment
= $comment;
367 $this->params
= LogPage
::makeParamBlob( $params );
369 if ($doer === null) {
372 } elseif (!is_object( $doer ) ) {
373 $doer = User
::newFromId( $doer );
378 $this->actionText
= LogPage
::actionText( $this->type
, $action, $target, NULL, $params );
380 return $this->saveContent();
384 * Add relations to log_search table
387 public function addRelations( $field, $values, $logid ) {
388 if( !strlen($field) ||
empty($values) )
389 return false; // nothing
391 foreach( $values as $value ) {
392 $data[] = array('ls_field' => $field,'ls_value' => $value,'ls_log_id' => $logid);
394 $dbw = wfGetDB( DB_MASTER
);
395 $dbw->insert( 'log_search', $data, __METHOD__
, 'IGNORE' );
400 * Create a blob from a parameter array
403 public static function makeParamBlob( $params ) {
404 return implode( "\n", $params );
408 * Extract a parameter array from a blob
411 public static function extractParams( $blob ) {
412 if ( $blob === '' ) {
415 return explode( "\n", $blob );
420 * Convert a comma-delimited list of block log flags
421 * into a more readable (and translated) form
423 * @param $flags Flags to format
424 * @param $forContent Whether to localize the message depending of the user
428 public static function formatBlockFlags( $flags, $forContent = false ) {
431 $flags = explode( ',', trim( $flags ) );
432 if( count( $flags ) > 0 ) {
433 for( $i = 0; $i < count( $flags ); $i++
)
434 $flags[$i] = self
::formatBlockFlag( $flags[$i], $forContent );
435 return '(' . $wgLang->commaList( $flags ) . ')';
442 * Translate a block log flag if possible
444 * @param $flag Flag to translate
445 * @param $forContent Whether to localize the message depending of the user
449 public static function formatBlockFlag( $flag, $forContent = false ) {
450 static $messages = array();
451 if( !isset( $messages[$flag] ) ) {
452 $k = 'block-log-flags-' . $flag;
454 $msg = wfMsgForContent( $k );
457 $messages[$flag] = htmlspecialchars( wfEmptyMsg( $k, $msg ) ?
$flag : $msg );
459 return $messages[$flag];
464 * Aliases for backwards compatibility with 1.6
466 define( 'MW_LOG_DELETED_ACTION', LogPage
::DELETED_ACTION
);
467 define( 'MW_LOG_DELETED_USER', LogPage
::DELETED_USER
);
468 define( 'MW_LOG_DELETED_COMMENT', LogPage
::DELETED_COMMENT
);
469 define( 'MW_LOG_DELETED_RESTRICTED', LogPage
::DELETED_RESTRICTED
);