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;
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
72 # log_id doesn't exist on Wikimedia servers yet, and it's a tricky
73 # schema update to do. Hack it for now to ignore the field on MySQL.
74 if ( !is_null( $log_id ) ) {
75 $data['log_id'] = $log_id;
77 $dbw->insert( 'logging', $data, $fname );
79 # And update recentchanges
80 if ( $this->updateRecentChanges
) {
81 $titleObj = SpecialPage
::getTitleFor( 'Log', $this->type
);
82 $rcComment = $this->getRcComment();
83 RecentChange
::notifyLog( $now, $titleObj, $wgUser, $rcComment, '',
84 $this->type
, $this->action
, $this->target
, $this->comment
, $this->params
);
89 public function getRcComment() {
90 $rcComment = $this->actionText
;
91 if( '' != $this->comment
) {
93 $rcComment = $this->comment
;
95 $rcComment .= ': ' . $this->comment
;
103 public static function validTypes() {
111 public static function isLogType( $type ) {
112 return in_array( $type, LogPage
::validTypes() );
118 public static function logName( $type ) {
121 if( isset( $wgLogNames[$type] ) ) {
122 return str_replace( '_', ' ', wfMsg( $wgLogNames[$type] ) );
124 // Bogus log types? Perhaps an extension was removed.
130 * @todo handle missing log types
133 static function logHeader( $type ) {
134 global $wgLogHeaders;
135 return wfMsg( $wgLogHeaders[$type] );
141 static function actionText( $type, $action, $title = NULL, $skin = NULL, $params = array(), $filterWikilinks=false, $translate=false ) {
142 global $wgLang, $wgContLang, $wgLogActions;
144 $key = "$type/$action";
146 if( $key == 'patrol/patrol' )
147 return PatrolLog
::makeActionText( $title, $params, $skin );
149 if( isset( $wgLogActions[$key] ) ) {
150 if( is_null( $title ) ) {
151 $rv=wfMsg( $wgLogActions[$key] );
157 $titleLink = $skin->makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
158 $params[0] = $skin->makeLinkObj( Title
::newFromText( $params[0] ), $params[0] );
161 if( substr( $title->getText(), 0, 1 ) == '#' ) {
162 $titleLink = $title->getText();
164 $titleLink = $skin->makeLinkObj( $title, $title->getText() );
165 $titleLink .= ' (' . $skin->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Contributions', $title->getDBkey() ), wfMsg( 'contribslink' ) ) . ')';
169 $text = $wgContLang->ucfirst( $title->getText() );
170 $titleLink = $skin->makeLinkObj( Title
::makeTitle( NS_USER
, $text ) );
173 $titleLink = $skin->makeLinkObj( $title );
177 $titleLink = $title->getPrefixedText();
179 if( $key == 'rights/rights' ) {
181 $rightsnone = wfMsg( 'rightsnone' );
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 );
197 array_unshift( $params, $titleLink );
198 if ( $key == 'block/block' ) {
200 $params[1] = $wgLang->translateBlockExpiry( $params[1] );
202 $params[2] = isset( $params[2] )
203 ? self
::formatBlockFlags( $params[2] )
206 $rv = wfMsgReal( $wgLogActions[$key], $params, true, !$skin );
210 wfDebug( "LogPage::actionText - unknown action $key\n" );
213 if( $filterWikilinks ) {
214 $rv = str_replace( "[[", "", $rv );
215 $rv = str_replace( "]]", "", $rv );
222 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
223 * @param object &$target A title object.
224 * @param string $comment Description associated
225 * @param array $params Parameters passed later to wfMsg.* functions
227 function addEntry( $action, $target, $comment, $params = array() ) {
228 if ( !is_array( $params ) ) {
229 $params = array( $params );
232 $this->action
= $action;
233 $this->target
= $target;
234 $this->comment
= $comment;
235 $this->params
= LogPage
::makeParamBlob( $params );
237 $this->actionText
= LogPage
::actionText( $this->type
, $action, $target, NULL, $params );
239 return $this->saveContent();
243 * Create a blob from a parameter array
246 static function makeParamBlob( $params ) {
247 return implode( "\n", $params );
251 * Extract a parameter array from a blob
254 static function extractParams( $blob ) {
255 if ( $blob === '' ) {
258 return explode( "\n", $blob );
263 * Convert a comma-delimited list of block log flags
264 * into a more readable (and translated) form
266 * @param $flags Flags to format
269 public static function formatBlockFlags( $flags ) {
270 $flags = explode( ',', trim( $flags ) );
271 if( count( $flags ) > 0 ) {
272 for( $i = 0; $i < count( $flags ); $i++
)
273 $flags[$i] = self
::formatBlockFlag( $flags[$i] );
274 return '(' . implode( ', ', $flags ) . ')';
281 * Translate a block log flag if possible
283 * @param $flag Flag to translate
286 public static function formatBlockFlag( $flag ) {
287 static $messages = array();
288 if( !isset( $messages[$flag] ) ) {
289 $k = 'block-log-flags-' . $flag;
291 $messages[$flag] = htmlspecialchars( wfEmptyMsg( $k, $msg ) ?
$flag : $msg );
293 return $messages[$flag];