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 function saveContent() {
55 if( wfReadOnly() ) return false;
57 global $wgUser, $wgLogRestrictions;
58 $fname = 'LogPage::saveContent';
60 $dbw = wfGetDB( DB_MASTER
);
61 $uid = $wgUser->getID();
62 $log_id = $dbw->nextSequenceValue( 'log_log_id_seq' );
64 $this->timestamp
= $now = wfTimestampNow();
66 'log_type' => $this->type
,
67 'log_action' => $this->action
,
68 'log_timestamp' => $dbw->timestamp( $now ),
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, $fname );
76 $newId = $dbw->insertId();
78 # And update recentchanges
79 if( $this->updateRecentChanges
) {
80 # Don't add private logs to RC!
81 if( !isset($wgLogRestrictions[$this->type
]) ||
$wgLogRestrictions[$this->type
]=='*' ) {
82 $titleObj = SpecialPage
::getTitleFor( 'Log', $this->type
);
83 $rcComment = $this->getRcComment();
84 RecentChange
::notifyLog( $now, $titleObj, $wgUser, $rcComment, '',
85 $this->type
, $this->action
, $this->target
, $this->comment
, $this->params
, $newId );
91 public function getRcComment() {
92 $rcComment = $this->actionText
;
93 if( '' != $this->comment
) {
95 $rcComment = $this->comment
;
97 $rcComment .= ': ' . $this->comment
;
105 public static function validTypes() {
113 public static function isLogType( $type ) {
114 return in_array( $type, LogPage
::validTypes() );
120 public static function logName( $type ) {
121 global $wgLogNames, $wgMessageCache;
123 if( isset( $wgLogNames[$type] ) ) {
124 $wgMessageCache->loadAllMessages();
125 return str_replace( '_', ' ', wfMsg( $wgLogNames[$type] ) );
127 // Bogus log types? Perhaps an extension was removed.
133 * @todo handle missing log types
134 * @param string $type logtype
135 * @return string Headertext of this logtype
137 static function logHeader( $type ) {
138 global $wgLogHeaders;
139 return wfMsgExt($wgLogHeaders[$type],array('parseinline'));
145 static function actionText( $type, $action, $title = NULL, $skin = NULL, $params = array(), $filterWikilinks=false ) {
146 global $wgLang, $wgContLang, $wgLogActions;
148 $key = "$type/$action";
150 if( $key == 'patrol/patrol' )
151 return PatrolLog
::makeActionText( $title, $params, $skin );
153 if( isset( $wgLogActions[$key] ) ) {
154 if( is_null( $title ) ) {
155 $rv=wfMsg( $wgLogActions[$key] );
161 $titleLink = $skin->makeLinkObj( $title, htmlspecialchars( $title->getPrefixedText() ), 'redirect=no' );
162 $params[0] = $skin->makeLinkObj( Title
::newFromText( $params[0] ), htmlspecialchars( $params[0] ) );
165 if( substr( $title->getText(), 0, 1 ) == '#' ) {
166 $titleLink = $title->getText();
168 // TODO: Store the user identifier in the parameters
169 // to make this faster for future log entries
170 $id = User
::idFromName( $title->getText() );
171 $titleLink = $skin->userLink( $id, $title->getText() )
172 . $skin->userToolLinks( $id, $title->getText(), false, Linker
::TOOL_LINKS_NOBLOCK
);
176 $text = $wgContLang->ucfirst( $title->getText() );
177 $titleLink = $skin->makeLinkObj( Title
::makeTitle( NS_USER
, $text ) );
180 $titleLink = $skin->makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
181 $params[0] = $skin->makeLinkObj( Title
::newFromText( $params[0] ), htmlspecialchars( $params[0] ) );
182 $params[1] = $wgLang->timeanddate( $params[1] );
185 $titleLink = $skin->makeLinkObj( $title );
189 $titleLink = $title->getPrefixedText();
191 if( $key == 'rights/rights' ) {
193 $rightsnone = wfMsg( 'rightsnone' );
195 $rightsnone = wfMsgForContent( 'rightsnone' );
197 if( !isset( $params[0] ) ||
trim( $params[0] ) == '' )
198 $params[0] = $rightsnone;
199 if( !isset( $params[1] ) ||
trim( $params[1] ) == '' )
200 $params[1] = $rightsnone;
202 if( count( $params ) == 0 ) {
204 $rv = wfMsg( $wgLogActions[$key], $titleLink );
206 $rv = wfMsgForContent( $wgLogActions[$key], $titleLink );
209 array_unshift( $params, $titleLink );
210 if ( $key == 'block/block' ||
$key == 'suppress/block' ) {
212 $params[1] = '<span title="' . htmlspecialchars( $params[1] ). '">' . $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
214 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
216 $params[2] = isset( $params[2] )
217 ? self
::formatBlockFlags( $params[2], is_null( $skin ) )
220 $rv = wfMsgReal( $wgLogActions[$key], $params, true, !$skin );
224 wfDebug( "LogPage::actionText - unknown action $key\n" );
227 if( $filterWikilinks ) {
228 $rv = str_replace( "[[", "", $rv );
229 $rv = str_replace( "]]", "", $rv );
236 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
237 * @param object &$target A title object.
238 * @param string $comment Description associated
239 * @param array $params Parameters passed later to wfMsg.* functions
241 function addEntry( $action, $target, $comment, $params = array() ) {
242 if ( !is_array( $params ) ) {
243 $params = array( $params );
246 $this->action
= $action;
247 $this->target
= $target;
248 $this->comment
= $comment;
249 $this->params
= LogPage
::makeParamBlob( $params );
251 $this->actionText
= LogPage
::actionText( $this->type
, $action, $target, NULL, $params );
253 return $this->saveContent();
257 * Create a blob from a parameter array
260 static function makeParamBlob( $params ) {
261 return implode( "\n", $params );
265 * Extract a parameter array from a blob
268 static function extractParams( $blob ) {
269 if ( $blob === '' ) {
272 return explode( "\n", $blob );
277 * Convert a comma-delimited list of block log flags
278 * into a more readable (and translated) form
280 * @param $flags Flags to format
281 * @param $forContent Whether to localize the message depending of the user
285 public static function formatBlockFlags( $flags, $forContent = false ) {
286 $flags = explode( ',', trim( $flags ) );
287 if( count( $flags ) > 0 ) {
288 for( $i = 0; $i < count( $flags ); $i++
)
289 $flags[$i] = self
::formatBlockFlag( $flags[$i], $forContent );
290 return '(' . implode( ', ', $flags ) . ')';
297 * Translate a block log flag if possible
299 * @param $flag Flag to translate
300 * @param $forContent Whether to localize the message depending of the user
304 public static function formatBlockFlag( $flag, $forContent = false ) {
305 static $messages = array();
306 if( !isset( $messages[$flag] ) ) {
307 $k = 'block-log-flags-' . $flag;
309 $msg = wfMsgForContent( $k );
312 $messages[$flag] = htmlspecialchars( wfEmptyMsg( $k, $msg ) ?
$flag : $msg );
314 return $messages[$flag];
319 * Aliases for backwards compatibility with 1.6
321 define( 'MW_LOG_DELETED_ACTION', LogPage
::DELETED_ACTION
);
322 define( 'MW_LOG_DELETED_USER', LogPage
::DELETED_USER
);
323 define( 'MW_LOG_DELETED_COMMENT', LogPage
::DELETED_COMMENT
);
324 define( 'MW_LOG_DELETED_RESTRICTED', LogPage
::DELETED_RESTRICTED
);