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;
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
49 function __construct( $type, $rc = true ) {
51 $this->updateRecentChanges
= $rc;
54 protected function saveContent() {
55 global $wgUser, $wgLogRestrictions;
56 $fname = 'LogPage::saveContent';
58 $dbw = wfGetDB( DB_MASTER
);
59 $log_id = $dbw->nextSequenceValue( 'log_log_id_seq' );
61 $this->timestamp
= $now = wfTimestampNow();
64 'log_type' => $this->type
,
65 'log_action' => $this->action
,
66 'log_timestamp' => $dbw->timestamp( $now ),
67 'log_user' => $this->doer
->getId(),
68 'log_namespace' => $this->target
->getNamespace(),
69 'log_title' => $this->target
->getDBkey(),
70 'log_comment' => $this->comment
,
71 'log_params' => $this->params
73 $dbw->insert( 'logging', $data, $fname );
74 $newId = !is_null($log_id) ?
$log_id : $dbw->insertId();
76 if( !($dbw->affectedRows() > 0) ) {
77 wfDebugLog( "logging", "LogPage::saveContent failed to insert row - Error {$dbw->lastErrno()}: {$dbw->lastError()}" );
79 # And update recentchanges
80 if( $this->updateRecentChanges
) {
81 # Don't add private logs to RC!
82 if( !isset($wgLogRestrictions[$this->type
]) ||
$wgLogRestrictions[$this->type
]=='*' ) {
83 $titleObj = SpecialPage
::getTitleFor( 'Log', $this->type
);
84 $rcComment = $this->getRcComment();
85 RecentChange
::notifyLog( $now, $titleObj, $this->doer
, $rcComment, '',
86 $this->type
, $this->action
, $this->target
, $this->comment
, $this->params
, $newId );
92 public function getRcComment() {
93 $rcComment = $this->actionText
;
94 if( '' != $this->comment
) {
96 $rcComment = $this->comment
;
98 $rcComment .= ': ' . $this->comment
;
106 public static function validTypes() {
114 public static function isLogType( $type ) {
115 return in_array( $type, LogPage
::validTypes() );
121 public static function logName( $type ) {
122 global $wgLogNames, $wgMessageCache;
124 if( isset( $wgLogNames[$type] ) ) {
125 $wgMessageCache->loadAllMessages();
126 return str_replace( '_', ' ', wfMsg( $wgLogNames[$type] ) );
128 // Bogus log types? Perhaps an extension was removed.
134 * @todo handle missing log types
135 * @param string $type logtype
136 * @return string Headertext of this logtype
138 static function logHeader( $type ) {
139 global $wgLogHeaders;
140 return wfMsgExt($wgLogHeaders[$type],array('parseinline'));
145 * @return HTML string
147 static function actionText( $type, $action, $title = NULL, $skin = NULL, $params = array(), $filterWikilinks=false ) {
148 global $wgLang, $wgContLang, $wgLogActions;
150 $key = "$type/$action";
152 if( $key == 'patrol/patrol' )
153 return PatrolLog
::makeActionText( $title, $params, $skin );
155 if( isset( $wgLogActions[$key] ) ) {
156 if( is_null( $title ) ) {
157 $rv=wfMsg( $wgLogActions[$key] );
163 $titleLink = $skin->makeLinkObj( $title, htmlspecialchars( $title->getPrefixedText() ), 'redirect=no' );
164 $params[0] = $skin->makeLinkObj( Title
::newFromText( $params[0] ), htmlspecialchars( $params[0] ) );
167 if( substr( $title->getText(), 0, 1 ) == '#' ) {
168 $titleLink = $title->getText();
170 // TODO: Store the user identifier in the parameters
171 // to make this faster for future log entries
172 $id = User
::idFromName( $title->getText() );
173 $titleLink = $skin->userLink( $id, $title->getText() )
174 . $skin->userToolLinks( $id, $title->getText(), false, Linker
::TOOL_LINKS_NOBLOCK
);
178 $text = $wgContLang->ucfirst( $title->getText() );
179 $titleLink = $skin->makeLinkObj( Title
::makeTitle( NS_USER
, $text ) );
182 $titleLink = $skin->makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
183 $params[0] = $skin->makeLinkObj( Title
::newFromText( $params[0] ), htmlspecialchars( $params[0] ) );
184 $params[1] = $wgLang->timeanddate( $params[1] );
187 $titleLink = $skin->makeLinkObj( $title );
191 $titleLink = $title->getPrefixedText();
193 if( $key == 'rights/rights' ) {
195 $rightsnone = wfMsg( 'rightsnone' );
196 foreach ( $params as &$param ) {
197 $groupArray = array_map( 'trim', explode( ',', $param ) );
198 $groupArray = array_map( array( 'User', 'getGroupName' ), $groupArray );
199 $param = $wgLang->listToText( $groupArray );
202 $rightsnone = wfMsgForContent( 'rightsnone' );
204 if( !isset( $params[0] ) ||
trim( $params[0] ) == '' )
205 $params[0] = $rightsnone;
206 if( !isset( $params[1] ) ||
trim( $params[1] ) == '' )
207 $params[1] = $rightsnone;
209 if( count( $params ) == 0 ) {
211 $rv = wfMsg( $wgLogActions[$key], $titleLink );
213 $rv = wfMsgForContent( $wgLogActions[$key], $titleLink );
216 array_unshift( $params, $titleLink );
217 if ( $key == 'block/block' ||
$key == 'suppress/block' ) {
219 $params[1] = '<span title="' . htmlspecialchars( $params[1] ). '">' . $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
221 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
223 $params[2] = isset( $params[2] )
224 ? self
::formatBlockFlags( $params[2], is_null( $skin ) )
227 $rv = wfMsgReal( $wgLogActions[$key], $params, true, !$skin );
231 global $wgLogActionsHandlers;
232 if( isset( $wgLogActionsHandlers[$key] ) ) {
233 $args = func_get_args();
234 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
236 wfDebug( "LogPage::actionText - unknown action $key\n" );
240 if( $filterWikilinks ) {
241 $rv = str_replace( "[[", "", $rv );
242 $rv = str_replace( "]]", "", $rv );
249 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
250 * @param object &$target A title object.
251 * @param string $comment Description associated
252 * @param array $params Parameters passed later to wfMsg.* functions
253 * @param User $doer The user doing the action
255 function addEntry( $action, $target, $comment, $params = array(), $doer = null ) {
256 if ( !is_array( $params ) ) {
257 $params = array( $params );
260 $this->action
= $action;
261 $this->target
= $target;
262 $this->comment
= $comment;
263 $this->params
= LogPage
::makeParamBlob( $params );
265 if ($doer === null) {
268 } elseif (!is_object( $doer ) ) {
269 $doer = User
::newFromId( $doer );
274 $this->actionText
= LogPage
::actionText( $this->type
, $action, $target, NULL, $params );
276 return $this->saveContent();
280 * Create a blob from a parameter array
283 static function makeParamBlob( $params ) {
284 return implode( "\n", $params );
288 * Extract a parameter array from a blob
291 static function extractParams( $blob ) {
292 if ( $blob === '' ) {
295 return explode( "\n", $blob );
300 * Convert a comma-delimited list of block log flags
301 * into a more readable (and translated) form
303 * @param $flags Flags to format
304 * @param $forContent Whether to localize the message depending of the user
308 public static function formatBlockFlags( $flags, $forContent = false ) {
309 $flags = explode( ',', trim( $flags ) );
310 if( count( $flags ) > 0 ) {
311 for( $i = 0; $i < count( $flags ); $i++
)
312 $flags[$i] = self
::formatBlockFlag( $flags[$i], $forContent );
313 return '(' . implode( ', ', $flags ) . ')';
320 * Translate a block log flag if possible
322 * @param $flag Flag to translate
323 * @param $forContent Whether to localize the message depending of the user
327 public static function formatBlockFlag( $flag, $forContent = false ) {
328 static $messages = array();
329 if( !isset( $messages[$flag] ) ) {
330 $k = 'block-log-flags-' . $flag;
332 $msg = wfMsgForContent( $k );
335 $messages[$flag] = htmlspecialchars( wfEmptyMsg( $k, $msg ) ?
$flag : $msg );
337 return $messages[$flag];
342 * Aliases for backwards compatibility with 1.6
344 define( 'MW_LOG_DELETED_ACTION', LogPage
::DELETED_ACTION
);
345 define( 'MW_LOG_DELETED_USER', LogPage
::DELETED_USER
);
346 define( 'MW_LOG_DELETED_COMMENT', LogPage
::DELETED_COMMENT
);
347 define( 'MW_LOG_DELETED_RESTRICTED', LogPage
::DELETED_RESTRICTED
);