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;
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 $uid = $wgUser->getId();
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 ),
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, $fname );
75 $newId = !is_null($log_id) ?
$log_id : $dbw->insertId();
77 if( !($dbw->affectedRows() > 0) ) {
78 wfDebugLog( "logging", "LogPage::saveContent failed to insert row - Error {$dbw->lastErrno()}: {$dbw->lastError()}" );
80 # And update recentchanges
81 if( $this->updateRecentChanges
) {
82 # Don't add private logs to RC!
83 if( !isset($wgLogRestrictions[$this->type
]) ||
$wgLogRestrictions[$this->type
]=='*' ) {
84 $titleObj = SpecialPage
::getTitleFor( 'Log', $this->type
);
85 $rcComment = $this->getRcComment();
86 RecentChange
::notifyLog( $now, $titleObj, $wgUser, $rcComment, '',
87 $this->type
, $this->action
, $this->target
, $this->comment
, $this->params
, $newId );
93 public function getRcComment() {
94 $rcComment = $this->actionText
;
95 if( '' != $this->comment
) {
97 $rcComment = $this->comment
;
99 $rcComment .= ': ' . $this->comment
;
107 public static function validTypes() {
115 public static function isLogType( $type ) {
116 return in_array( $type, LogPage
::validTypes() );
122 public static function logName( $type ) {
123 global $wgLogNames, $wgMessageCache;
125 if( isset( $wgLogNames[$type] ) ) {
126 $wgMessageCache->loadAllMessages();
127 return str_replace( '_', ' ', wfMsg( $wgLogNames[$type] ) );
129 // Bogus log types? Perhaps an extension was removed.
135 * @todo handle missing log types
136 * @param string $type logtype
137 * @return string Headertext of this logtype
139 static function logHeader( $type ) {
140 global $wgLogHeaders;
141 return wfMsgExt($wgLogHeaders[$type],array('parseinline'));
146 * @return HTML string
148 static function actionText( $type, $action, $title = NULL, $skin = NULL, $params = array(), $filterWikilinks=false ) {
149 global $wgLang, $wgContLang, $wgLogActions;
151 $key = "$type/$action";
153 if( $key == 'patrol/patrol' )
154 return PatrolLog
::makeActionText( $title, $params, $skin );
156 if( isset( $wgLogActions[$key] ) ) {
157 if( is_null( $title ) ) {
158 $rv=wfMsg( $wgLogActions[$key] );
164 $titleLink = $skin->makeLinkObj( $title, htmlspecialchars( $title->getPrefixedText() ), 'redirect=no' );
165 $params[0] = $skin->makeLinkObj( Title
::newFromText( $params[0] ), htmlspecialchars( $params[0] ) );
168 if( substr( $title->getText(), 0, 1 ) == '#' ) {
169 $titleLink = $title->getText();
171 // TODO: Store the user identifier in the parameters
172 // to make this faster for future log entries
173 $id = User
::idFromName( $title->getText() );
174 $titleLink = $skin->userLink( $id, $title->getText() )
175 . $skin->userToolLinks( $id, $title->getText(), false, Linker
::TOOL_LINKS_NOBLOCK
);
179 $text = $wgContLang->ucfirst( $title->getText() );
180 $titleLink = $skin->makeLinkObj( Title
::makeTitle( NS_USER
, $text ) );
183 $titleLink = $skin->makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
184 $params[0] = $skin->makeLinkObj( Title
::newFromText( $params[0] ), htmlspecialchars( $params[0] ) );
185 $params[1] = $wgLang->timeanddate( $params[1] );
188 $titleLink = $skin->makeLinkObj( $title );
192 $titleLink = $title->getPrefixedText();
194 if( $key == 'rights/rights' ) {
196 $rightsnone = wfMsg( 'rightsnone' );
198 $rightsnone = wfMsgForContent( 'rightsnone' );
200 if( !isset( $params[0] ) ||
trim( $params[0] ) == '' )
201 $params[0] = $rightsnone;
202 if( !isset( $params[1] ) ||
trim( $params[1] ) == '' )
203 $params[1] = $rightsnone;
205 if( count( $params ) == 0 ) {
207 $rv = wfMsg( $wgLogActions[$key], $titleLink );
209 $rv = wfMsgForContent( $wgLogActions[$key], $titleLink );
212 array_unshift( $params, $titleLink );
213 if ( $key == 'block/block' ||
$key == 'suppress/block' ) {
215 $params[1] = '<span title="' . htmlspecialchars( $params[1] ). '">' . $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
217 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
219 $params[2] = isset( $params[2] )
220 ? self
::formatBlockFlags( $params[2], is_null( $skin ) )
223 $rv = wfMsgReal( $wgLogActions[$key], $params, true, !$skin );
227 global $wgLogActionsHandlers;
228 if( isset( $wgLogActionsHandlers[$key] ) ) {
229 $args = func_get_args();
230 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
232 wfDebug( "LogPage::actionText - unknown action $key\n" );
236 if( $filterWikilinks ) {
237 $rv = str_replace( "[[", "", $rv );
238 $rv = str_replace( "]]", "", $rv );
245 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
246 * @param object &$target A title object.
247 * @param string $comment Description associated
248 * @param array $params Parameters passed later to wfMsg.* functions
250 function addEntry( $action, $target, $comment, $params = array() ) {
251 if ( !is_array( $params ) ) {
252 $params = array( $params );
255 $this->action
= $action;
256 $this->target
= $target;
257 $this->comment
= $comment;
258 $this->params
= LogPage
::makeParamBlob( $params );
260 $this->actionText
= LogPage
::actionText( $this->type
, $action, $target, NULL, $params );
262 return $this->saveContent();
266 * Create a blob from a parameter array
269 static function makeParamBlob( $params ) {
270 return implode( "\n", $params );
274 * Extract a parameter array from a blob
277 static function extractParams( $blob ) {
278 if ( $blob === '' ) {
281 return explode( "\n", $blob );
286 * Convert a comma-delimited list of block log flags
287 * into a more readable (and translated) form
289 * @param $flags Flags to format
290 * @param $forContent Whether to localize the message depending of the user
294 public static function formatBlockFlags( $flags, $forContent = false ) {
295 $flags = explode( ',', trim( $flags ) );
296 if( count( $flags ) > 0 ) {
297 for( $i = 0; $i < count( $flags ); $i++
)
298 $flags[$i] = self
::formatBlockFlag( $flags[$i], $forContent );
299 return '(' . implode( ', ', $flags ) . ')';
306 * Translate a block log flag if possible
308 * @param $flag Flag to translate
309 * @param $forContent Whether to localize the message depending of the user
313 public static function formatBlockFlag( $flag, $forContent = false ) {
314 static $messages = array();
315 if( !isset( $messages[$flag] ) ) {
316 $k = 'block-log-flags-' . $flag;
318 $msg = wfMsgForContent( $k );
321 $messages[$flag] = htmlspecialchars( wfEmptyMsg( $k, $msg ) ?
$flag : $msg );
323 return $messages[$flag];
328 * Aliases for backwards compatibility with 1.6
330 define( 'MW_LOG_DELETED_ACTION', LogPage
::DELETED_ACTION
);
331 define( 'MW_LOG_DELETED_USER', LogPage
::DELETED_USER
);
332 define( 'MW_LOG_DELETED_COMMENT', LogPage
::DELETED_COMMENT
);
333 define( 'MW_LOG_DELETED_RESTRICTED', LogPage
::DELETED_RESTRICTED
);