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
50 public function __construct( $type, $rc = true, $udp = true ) {
52 $this->updateRecentChanges
= $rc;
53 $this->sendToUDP
= $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_namespace' => $this->target
->getNamespace(),
70 'log_title' => $this->target
->getDBkey(),
71 'log_comment' => $this->comment
,
72 'log_params' => $this->params
74 $dbw->insert( 'logging', $data, __METHOD__
);
75 $newId = !is_null($log_id) ?
$log_id : $dbw->insertId();
77 # And update recentchanges
78 if( $this->updateRecentChanges
) {
79 $titleObj = SpecialPage
::getTitleFor( 'Log', $this->type
);
80 RecentChange
::notifyLog( $now, $titleObj, $this->doer
, $this->getRcComment(), '', $this->type
,
81 $this->action
, $this->target
, $this->comment
, $this->params
, $newId );
82 } else if( $this->sendToUDP
) {
83 # Don't send private logs to UDP
84 if( isset($wgLogRestrictions[$this->type
]) && $wgLogRestrictions[$this->type
] !='*' ) {
87 # Notify external application via UDP.
88 # We send this to IRC but do not want to add it the RC table.
89 global $wgRC2UDPAddress, $wgRC2UDPOmitBots;
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 );
93 if( $wgRC2UDPAddress && ( !$rc->getAttribute('rc_bot') ||
!$wgRC2UDPOmitBots ) ) {
94 RecentChange
::sendToUDP( $rc->getIRCLine() );
101 * Get the RC comment from the last addEntry() call
103 public function getRcComment() {
104 $rcComment = $this->actionText
;
105 if( '' != $this->comment
) {
106 if ($rcComment == '')
107 $rcComment = $this->comment
;
109 $rcComment .= wfMsgForContent( 'colon-separator' ) . $this->comment
;
115 * Get the comment from the last addEntry() call
117 public function getComment() {
118 return $this->comment
;
124 public static function validTypes() {
132 public static function isLogType( $type ) {
133 return in_array( $type, LogPage
::validTypes() );
139 public static function logName( $type ) {
140 global $wgLogNames, $wgMessageCache;
142 if( isset( $wgLogNames[$type] ) ) {
143 $wgMessageCache->loadAllMessages();
144 return str_replace( '_', ' ', wfMsg( $wgLogNames[$type] ) );
146 // Bogus log types? Perhaps an extension was removed.
152 * @todo handle missing log types
153 * @param string $type logtype
154 * @return string Headertext of this logtype
156 public static function logHeader( $type ) {
157 global $wgLogHeaders, $wgMessageCache;
158 $wgMessageCache->loadAllMessages();
159 return wfMsgExt($wgLogHeaders[$type],array('parseinline'));
164 * @return HTML string
166 public static function actionText( $type, $action, $title = NULL, $skin = NULL,
167 $params = array(), $filterWikilinks = false )
169 global $wgLang, $wgContLang, $wgLogActions, $wgMessageCache;
171 $wgMessageCache->loadAllMessages();
172 $key = "$type/$action";
173 # Defer patrol log to PatrolLog class
174 if( $key == 'patrol/patrol' ) {
175 return PatrolLog
::makeActionText( $title, $params, $skin );
177 if( isset( $wgLogActions[$key] ) ) {
178 if( is_null( $title ) ) {
179 $rv = wfMsg( $wgLogActions[$key] );
181 $titleLink = self
::getTitleLink( $type, $skin, $title, $params );
182 if( $key == 'rights/rights' ) {
184 $rightsnone = wfMsg( 'rightsnone' );
185 foreach ( $params as &$param ) {
186 $groupArray = array_map( 'trim', explode( ',', $param ) );
187 $groupArray = array_map( array( 'User', 'getGroupName' ), $groupArray );
188 $param = $wgLang->listToText( $groupArray );
191 $rightsnone = wfMsgForContent( 'rightsnone' );
193 if( !isset( $params[0] ) ||
trim( $params[0] ) == '' )
194 $params[0] = $rightsnone;
195 if( !isset( $params[1] ) ||
trim( $params[1] ) == '' )
196 $params[1] = $rightsnone;
198 if( count( $params ) == 0 ) {
200 $rv = wfMsg( $wgLogActions[$key], $titleLink );
202 $rv = wfMsgForContent( $wgLogActions[$key], $titleLink );
206 array_unshift( $params, $titleLink );
207 if ( $key == 'block/block' ||
$key == 'suppress/block' ||
$key == 'block/reblock' ) {
209 $params[1] = '<span title="' . htmlspecialchars( $params[1] ). '">' .
210 $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
212 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
214 $params[2] = isset( $params[2] ) ?
215 self
::formatBlockFlags( $params[2], is_null( $skin ) ) : '';
216 } else if ( $type == 'protect' && count($params) == 3 ) {
217 $details .= " {$params[1]}"; // restrictions and expiries
219 $details .= ' ['.wfMsg('protect-summary-cascade').']';
221 } else if ( $type == 'move' && count( $params ) == 3 ) {
223 $details .= ' [' . wfMsg( 'move-redirect-suppressed' ) . ']';
226 $rv = wfMsgReal( $wgLogActions[$key], $params, true, !$skin ) . $details;
230 global $wgLogActionsHandlers;
231 if( isset( $wgLogActionsHandlers[$key] ) ) {
232 $args = func_get_args();
233 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
235 wfDebug( "LogPage::actionText - unknown action $key\n" );
239 if( $filterWikilinks ) {
240 $rv = str_replace( "[[", "", $rv );
241 $rv = str_replace( "]]", "", $rv );
246 protected static function getTitleLink( $type, $skin, $title, &$params ) {
247 global $wgLang, $wgContLang;
249 return $title->getPrefixedText();
253 $titleLink = $skin->makeLinkObj( $title,
254 htmlspecialchars( $title->getPrefixedText() ), 'redirect=no' );
255 $targetTitle = Title
::newFromText( $params[0] );
256 if ( !$targetTitle ) {
257 # Workaround for broken database
258 $params[0] = htmlspecialchars( $params[0] );
260 $params[0] = $skin->makeLinkObj( $targetTitle, htmlspecialchars( $params[0] ) );
264 if( substr( $title->getText(), 0, 1 ) == '#' ) {
265 $titleLink = $title->getText();
267 // TODO: Store the user identifier in the parameters
268 // to make this faster for future log entries
269 $id = User
::idFromName( $title->getText() );
270 $titleLink = $skin->userLink( $id, $title->getText() )
271 . $skin->userToolLinks( $id, $title->getText(), false, Linker
::TOOL_LINKS_NOBLOCK
);
275 $text = $wgContLang->ucfirst( $title->getText() );
276 $titleLink = $skin->makeLinkObj( Title
::makeTitle( NS_USER
, $text ) );
279 $titleLink = $skin->makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
280 $params[0] = $skin->makeLinkObj( Title
::newFromText( $params[0] ), htmlspecialchars( $params[0] ) );
281 $params[1] = $wgLang->timeanddate( $params[1] );
284 if( $title->getNamespace() == NS_SPECIAL
) {
285 list( $name, $par ) = SpecialPage
::resolveAliasWithSubpage( $title->getDBKey() );
286 # Use the language name for log titles, rather than Log/X
287 if( $name == 'Log' ) {
288 $titleLink = '('.$skin->makeLinkObj( $title, LogPage
::logName( $par ) ).')';
290 $titleLink = $skin->makeLinkObj( $title );
293 $titleLink = $skin->makeLinkObj( $title );
301 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
302 * @param object &$target A title object.
303 * @param string $comment Description associated
304 * @param array $params Parameters passed later to wfMsg.* functions
305 * @param User $doer The user doing the action
307 public function addEntry( $action, $target, $comment, $params = array(), $doer = null ) {
308 if ( !is_array( $params ) ) {
309 $params = array( $params );
312 $this->action
= $action;
313 $this->target
= $target;
314 $this->comment
= $comment;
315 $this->params
= LogPage
::makeParamBlob( $params );
317 if ($doer === null) {
320 } elseif (!is_object( $doer ) ) {
321 $doer = User
::newFromId( $doer );
326 $this->actionText
= LogPage
::actionText( $this->type
, $action, $target, NULL, $params );
328 return $this->saveContent();
332 * Create a blob from a parameter array
335 public static function makeParamBlob( $params ) {
336 return implode( "\n", $params );
340 * Extract a parameter array from a blob
343 public static function extractParams( $blob ) {
344 if ( $blob === '' ) {
347 return explode( "\n", $blob );
352 * Convert a comma-delimited list of block log flags
353 * into a more readable (and translated) form
355 * @param $flags Flags to format
356 * @param $forContent Whether to localize the message depending of the user
360 public static function formatBlockFlags( $flags, $forContent = false ) {
361 $flags = explode( ',', trim( $flags ) );
362 if( count( $flags ) > 0 ) {
363 for( $i = 0; $i < count( $flags ); $i++
)
364 $flags[$i] = self
::formatBlockFlag( $flags[$i], $forContent );
365 return '(' . implode( ', ', $flags ) . ')';
372 * Translate a block log flag if possible
374 * @param $flag Flag to translate
375 * @param $forContent Whether to localize the message depending of the user
379 public static function formatBlockFlag( $flag, $forContent = false ) {
380 static $messages = array();
381 if( !isset( $messages[$flag] ) ) {
382 $k = 'block-log-flags-' . $flag;
384 $msg = wfMsgForContent( $k );
387 $messages[$flag] = htmlspecialchars( wfEmptyMsg( $k, $msg ) ?
$flag : $msg );
389 return $messages[$flag];
394 * Aliases for backwards compatibility with 1.6
396 define( 'MW_LOG_DELETED_ACTION', LogPage
::DELETED_ACTION
);
397 define( 'MW_LOG_DELETED_USER', LogPage
::DELETED_USER
);
398 define( 'MW_LOG_DELETED_COMMENT', LogPage
::DELETED_COMMENT
);
399 define( 'MW_LOG_DELETED_RESTRICTED', LogPage
::DELETED_RESTRICTED
);