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.
34 var $type, $action, $comment, $params, $target;
36 var $updateRecentChanges;
41 * @param string $type One of '', 'block', 'protect', 'rights', 'delete',
43 * @param bool $rc Whether to update recent changes as well as the logging table
45 function __construct( $type, $rc = true ) {
47 $this->updateRecentChanges
= $rc;
50 function saveContent() {
51 if( wfReadOnly() ) return false;
53 global $wgUser, $wgLogRestrictions;
54 $fname = 'LogPage::saveContent';
56 $dbw = wfGetDB( DB_MASTER
);
57 $uid = $wgUser->getID();
58 $log_id = $dbw->nextSequenceValue( 'log_log_id_seq' );
60 $this->timestamp
= $now = wfTimestampNow();
62 'log_type' => $this->type
,
63 'log_action' => $this->action
,
64 'log_timestamp' => $dbw->timestamp( $now ),
66 'log_namespace' => $this->target
->getNamespace(),
67 'log_title' => $this->target
->getDBkey(),
68 'log_comment' => $this->comment
,
69 'log_params' => $this->params
71 $dbw->insert( 'logging', $data, $fname );
72 $newId = $dbw->insertId();
74 # And update recentchanges
75 if( $this->updateRecentChanges
) {
76 # Don't add private logs to RC!
77 if( !isset($wgLogRestrictions[$this->type
]) ||
$wgLogRestrictions[$this->type
]=='*' ) {
78 $titleObj = SpecialPage
::getTitleFor( 'Log', $this->type
);
79 $rcComment = $this->getRcComment();
80 RecentChange
::notifyLog( $now, $titleObj, $wgUser, $rcComment, '',
81 $this->type
, $this->action
, $this->target
, $this->comment
, $this->params
, $newId );
87 public function getRcComment() {
88 $rcComment = $this->actionText
;
89 if( '' != $this->comment
) {
91 $rcComment = $this->comment
;
93 $rcComment .= ': ' . $this->comment
;
101 public static function validTypes() {
109 public static function isLogType( $type ) {
110 return in_array( $type, LogPage
::validTypes() );
116 public static function logName( $type ) {
117 global $wgLogNames, $wgMessageCache;
119 if( isset( $wgLogNames[$type] ) ) {
120 $wgMessageCache->loadAllMessages();
121 return str_replace( '_', ' ', wfMsg( $wgLogNames[$type] ) );
123 // Bogus log types? Perhaps an extension was removed.
129 * @todo handle missing log types
132 static function logHeader( $type ) {
133 global $wgLogHeaders;
134 return wfMsgHtml( $wgLogHeaders[$type] );
140 static function actionText( $type, $action, $title = NULL, $skin = NULL, $params = array(), $filterWikilinks=false ) {
141 global $wgLang, $wgContLang, $wgLogActions;
143 $key = "$type/$action";
145 if( $key == 'patrol/patrol' )
146 return PatrolLog
::makeActionText( $title, $params, $skin );
148 if( isset( $wgLogActions[$key] ) ) {
149 if( is_null( $title ) ) {
150 $rv=wfMsg( $wgLogActions[$key] );
156 $titleLink = $skin->makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
157 $params[0] = $skin->makeLinkObj( Title
::newFromText( $params[0] ), htmlspecialchars( $params[0] ) );
160 if( substr( $title->getText(), 0, 1 ) == '#' ) {
161 $titleLink = $title->getText();
163 // TODO: Store the user identifier in the parameters
164 // to make this faster for future log entries
165 $id = User
::idFromName( $title->getText() );
166 $titleLink = $skin->userLink( $id, $title->getText() )
167 . $skin->userToolLinks( $id, $title->getText(), false, Linker
::TOOL_LINKS_NOBLOCK
);
171 $text = $wgContLang->ucfirst( $title->getText() );
172 $titleLink = $skin->makeLinkObj( Title
::makeTitle( NS_USER
, $text ) );
175 $titleLink = $skin->makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
176 $params[0] = $skin->makeLinkObj( Title
::newFromText( $params[0] ), htmlspecialchars( $params[0] ) );
177 $params[1] = $wgLang->timeanddate( $params[1] );
180 $titleLink = $skin->makeLinkObj( $title );
184 $titleLink = $title->getPrefixedText();
186 if( $key == 'rights/rights' ) {
188 $rightsnone = wfMsg( 'rightsnone' );
190 $rightsnone = wfMsgForContent( 'rightsnone' );
192 if( !isset( $params[0] ) ||
trim( $params[0] ) == '' )
193 $params[0] = $rightsnone;
194 if( !isset( $params[1] ) ||
trim( $params[1] ) == '' )
195 $params[1] = $rightsnone;
197 if( count( $params ) == 0 ) {
199 $rv = wfMsg( $wgLogActions[$key], $titleLink );
201 $rv = wfMsgForContent( $wgLogActions[$key], $titleLink );
204 array_unshift( $params, $titleLink );
205 if ( $key == 'block/block' ||
$key == 'oversight/block' ) {
207 $params[1] = '<span title="' . htmlspecialchars( $params[1] ). '">' . $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
209 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
211 $params[2] = isset( $params[2] )
212 ? self
::formatBlockFlags( $params[2] )
215 $rv = wfMsgReal( $wgLogActions[$key], $params, true, !$skin );
219 wfDebug( "LogPage::actionText - unknown action $key\n" );
222 if( $filterWikilinks ) {
223 $rv = str_replace( "[[", "", $rv );
224 $rv = str_replace( "]]", "", $rv );
231 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
232 * @param object &$target A title object.
233 * @param string $comment Description associated
234 * @param array $params Parameters passed later to wfMsg.* functions
236 function addEntry( $action, $target, $comment, $params = array() ) {
237 if ( !is_array( $params ) ) {
238 $params = array( $params );
241 $this->action
= $action;
242 $this->target
= $target;
243 $this->comment
= $comment;
244 $this->params
= LogPage
::makeParamBlob( $params );
246 $this->actionText
= LogPage
::actionText( $this->type
, $action, $target, NULL, $params );
248 return $this->saveContent();
252 * Create a blob from a parameter array
255 static function makeParamBlob( $params ) {
256 return implode( "\n", $params );
260 * Extract a parameter array from a blob
263 static function extractParams( $blob ) {
264 if ( $blob === '' ) {
267 return explode( "\n", $blob );
272 * Convert a comma-delimited list of block log flags
273 * into a more readable (and translated) form
275 * @param $flags Flags to format
278 public static function formatBlockFlags( $flags ) {
279 $flags = explode( ',', trim( $flags ) );
280 if( count( $flags ) > 0 ) {
281 for( $i = 0; $i < count( $flags ); $i++
)
282 $flags[$i] = self
::formatBlockFlag( $flags[$i] );
283 return '(' . implode( ', ', $flags ) . ')';
290 * Translate a block log flag if possible
292 * @param $flag Flag to translate
295 public static function formatBlockFlag( $flag ) {
296 static $messages = array();
297 if( !isset( $messages[$flag] ) ) {
298 $k = 'block-log-flags-' . $flag;
300 $messages[$flag] = htmlspecialchars( wfEmptyMsg( $k, $msg ) ?
$flag : $msg );
302 return $messages[$flag];