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 );
93 * Get the RC comment from the last addEntry() call
95 public function getRcComment() {
96 $rcComment = $this->actionText
;
97 if( '' != $this->comment
) {
99 $rcComment = $this->comment
;
101 $rcComment .= ': ' . $this->comment
;
107 * Get the comment from the last addEntry() call
109 public function getComment() {
110 return $this->comment
;
116 public static function validTypes() {
124 public static function isLogType( $type ) {
125 return in_array( $type, LogPage
::validTypes() );
131 public static function logName( $type ) {
132 global $wgLogNames, $wgMessageCache;
134 if( isset( $wgLogNames[$type] ) ) {
135 $wgMessageCache->loadAllMessages();
136 return str_replace( '_', ' ', wfMsg( $wgLogNames[$type] ) );
138 // Bogus log types? Perhaps an extension was removed.
144 * @todo handle missing log types
145 * @param string $type logtype
146 * @return string Headertext of this logtype
148 static function logHeader( $type ) {
149 global $wgLogHeaders, $wgMessageCache;
150 $wgMessageCache->loadAllMessages();
151 return wfMsgExt($wgLogHeaders[$type],array('parseinline'));
156 * @return HTML string
158 static function actionText( $type, $action, $title = NULL, $skin = NULL,
159 $params = array(), $filterWikilinks = false )
161 global $wgLang, $wgContLang, $wgLogActions, $wgMessageCache;
163 $wgMessageCache->loadAllMessages();
164 $key = "$type/$action";
165 # Defer patrol log to PatrolLog class
166 if( $key == 'patrol/patrol' ) {
167 return PatrolLog
::makeActionText( $title, $params, $skin );
169 if( isset( $wgLogActions[$key] ) ) {
170 if( is_null( $title ) ) {
171 $rv = wfMsg( $wgLogActions[$key] );
173 $titleLink = self
::getTitleLink( $type, $skin, $title, $params );
174 if( $key == 'rights/rights' ) {
176 $rightsnone = wfMsg( 'rightsnone' );
177 foreach ( $params as &$param ) {
178 $groupArray = array_map( 'trim', explode( ',', $param ) );
179 $groupArray = array_map( array( 'User', 'getGroupName' ), $groupArray );
180 $param = $wgLang->listToText( $groupArray );
183 $rightsnone = wfMsgForContent( 'rightsnone' );
185 if( !isset( $params[0] ) ||
trim( $params[0] ) == '' )
186 $params[0] = $rightsnone;
187 if( !isset( $params[1] ) ||
trim( $params[1] ) == '' )
188 $params[1] = $rightsnone;
190 if( count( $params ) == 0 ) {
192 $rv = wfMsg( $wgLogActions[$key], $titleLink );
194 $rv = wfMsgForContent( $wgLogActions[$key], $titleLink );
198 array_unshift( $params, $titleLink );
199 if ( $key == 'block/block' ||
$key == 'suppress/block' ) {
201 $params[1] = '<span title="' . htmlspecialchars( $params[1] ). '">' .
202 $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
204 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
206 $params[2] = isset( $params[2] ) ?
207 self
::formatBlockFlags( $params[2], is_null( $skin ) ) : '';
208 } else if ( $type == 'protect' && count($params) == 3 ) {
209 $details .= " {$params[1]}"; // restrictions and expiries
211 $details .= ' ['.wfMsg('protect-summary-cascade').']';
214 $rv = wfMsgReal( $wgLogActions[$key], $params, true, !$skin ) . $details;
218 global $wgLogActionsHandlers;
219 if( isset( $wgLogActionsHandlers[$key] ) ) {
220 $args = func_get_args();
221 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
223 wfDebug( "LogPage::actionText - unknown action $key\n" );
227 if( $filterWikilinks ) {
228 $rv = str_replace( "[[", "", $rv );
229 $rv = str_replace( "]]", "", $rv );
234 protected static function getTitleLink( $type, $skin, $title, &$params ) {
235 global $wgLang, $wgContLang;
237 return $title->getPrefixedText();
241 $titleLink = $skin->makeLinkObj( $title,
242 htmlspecialchars( $title->getPrefixedText() ), 'redirect=no' );
243 $targetTitle = Title
::newFromText( $params[0] );
244 if ( !$targetTitle ) {
245 # Workaround for broken database
246 $params[0] = htmlspecialchars( $params[0] );
248 $params[0] = $skin->makeLinkObj( $targetTitle, htmlspecialchars( $params[0] ) );
252 if( substr( $title->getText(), 0, 1 ) == '#' ) {
253 $titleLink = $title->getText();
255 // TODO: Store the user identifier in the parameters
256 // to make this faster for future log entries
257 $id = User
::idFromName( $title->getText() );
258 $titleLink = $skin->userLink( $id, $title->getText() )
259 . $skin->userToolLinks( $id, $title->getText(), false, Linker
::TOOL_LINKS_NOBLOCK
);
263 $text = $wgContLang->ucfirst( $title->getText() );
264 $titleLink = $skin->makeLinkObj( Title
::makeTitle( NS_USER
, $text ) );
267 $titleLink = $skin->makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
268 $params[0] = $skin->makeLinkObj( Title
::newFromText( $params[0] ), htmlspecialchars( $params[0] ) );
269 $params[1] = $wgLang->timeanddate( $params[1] );
272 if( $title->getNamespace() == NS_SPECIAL
) {
273 list( $name, $par ) = SpecialPage
::resolveAliasWithSubpage( $title->getDBKey() );
274 # Use the language name for log titles, rather than Log/X
275 if( $name == 'Log' ) {
276 $titleLink = '('.$skin->makeLinkObj( $title, LogPage
::logName( $par ) ).')';
278 $titleLink = $skin->makeLinkObj( $title );
281 $titleLink = $skin->makeLinkObj( $title );
289 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
290 * @param object &$target A title object.
291 * @param string $comment Description associated
292 * @param array $params Parameters passed later to wfMsg.* functions
293 * @param User $doer The user doing the action
295 function addEntry( $action, $target, $comment, $params = array(), $doer = null ) {
296 if ( !is_array( $params ) ) {
297 $params = array( $params );
300 $this->action
= $action;
301 $this->target
= $target;
302 $this->comment
= $comment;
303 $this->params
= LogPage
::makeParamBlob( $params );
305 if ($doer === null) {
308 } elseif (!is_object( $doer ) ) {
309 $doer = User
::newFromId( $doer );
314 $this->actionText
= LogPage
::actionText( $this->type
, $action, $target, NULL, $params );
316 return $this->saveContent();
320 * Create a blob from a parameter array
323 static function makeParamBlob( $params ) {
324 return implode( "\n", $params );
328 * Extract a parameter array from a blob
331 static function extractParams( $blob ) {
332 if ( $blob === '' ) {
335 return explode( "\n", $blob );
340 * Convert a comma-delimited list of block log flags
341 * into a more readable (and translated) form
343 * @param $flags Flags to format
344 * @param $forContent Whether to localize the message depending of the user
348 public static function formatBlockFlags( $flags, $forContent = false ) {
349 $flags = explode( ',', trim( $flags ) );
350 if( count( $flags ) > 0 ) {
351 for( $i = 0; $i < count( $flags ); $i++
)
352 $flags[$i] = self
::formatBlockFlag( $flags[$i], $forContent );
353 return '(' . implode( ', ', $flags ) . ')';
360 * Translate a block log flag if possible
362 * @param $flag Flag to translate
363 * @param $forContent Whether to localize the message depending of the user
367 public static function formatBlockFlag( $flag, $forContent = false ) {
368 static $messages = array();
369 if( !isset( $messages[$flag] ) ) {
370 $k = 'block-log-flags-' . $flag;
372 $msg = wfMsgForContent( $k );
375 $messages[$flag] = htmlspecialchars( wfEmptyMsg( $k, $msg ) ?
$flag : $msg );
377 return $messages[$flag];
382 * Aliases for backwards compatibility with 1.6
384 define( 'MW_LOG_DELETED_ACTION', LogPage
::DELETED_ACTION
);
385 define( 'MW_LOG_DELETED_USER', LogPage
::DELETED_USER
);
386 define( 'MW_LOG_DELETED_COMMENT', LogPage
::DELETED_COMMENT
);
387 define( 'MW_LOG_DELETED_RESTRICTED', LogPage
::DELETED_RESTRICTED
);