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();
59 $this->timestamp
= $now = wfTimestampNow();
60 $dbw->insert( 'logging',
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
73 # And update recentchanges
74 if ( $this->updateRecentChanges
) {
75 $titleObj = SpecialPage
::getTitleFor( 'Log', $this->type
);
76 $rcComment = $this->actionText
;
77 if( '' != $this->comment
) {
79 $rcComment = $this->comment
;
81 $rcComment .= ': ' . $this->comment
;
84 RecentChange
::notifyLog( $now, $titleObj, $wgUser, $rcComment, '',
85 $this->type
, $this->action
, $this->target
, $this->comment
, $this->params
);
93 function validTypes() {
101 function isLogType( $type ) {
102 return in_array( $type, LogPage
::validTypes() );
108 public static function logName( $type ) {
111 if( isset( $wgLogNames[$type] ) ) {
112 return str_replace( '_', ' ', wfMsg( $wgLogNames[$type] ) );
114 // Bogus log types? Perhaps an extension was removed.
120 * @fixme: handle missing log types
123 function logHeader( $type ) {
124 global $wgLogHeaders;
125 return wfMsg( $wgLogHeaders[$type] );
131 function actionText( $type, $action, $title = NULL, $skin = NULL, $params = array(), $filterWikilinks=false, $translate=false ) {
132 global $wgLang, $wgContLang, $wgLogActions;
134 $key = "$type/$action";
136 if( $key == 'patrol/patrol' )
137 return PatrolLog
::makeActionText( $title, $params, $skin );
139 if( isset( $wgLogActions[$key] ) ) {
140 if( is_null( $title ) ) {
141 $rv=wfMsg( $wgLogActions[$key] );
147 $titleLink = $skin->makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
148 $params[0] = $skin->makeLinkObj( Title
::newFromText( $params[0] ), $params[0] );
151 if( substr( $title->getText(), 0, 1 ) == '#' ) {
152 $titleLink = $title->getText();
154 $titleLink = $skin->makeLinkObj( $title, $title->getText() );
155 $titleLink .= ' (' . $skin->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Contributions', $title->getDBkey() ), wfMsg( 'contribslink' ) ) . ')';
159 $text = $wgContLang->ucfirst( $title->getText() );
160 $titleLink = $skin->makeLinkObj( Title
::makeTitle( NS_USER
, $text ) );
163 $titleLink = $skin->makeLinkObj( $title );
167 $titleLink = $title->getPrefixedText();
169 if( $key == 'rights/rights' ) {
171 $rightsnone = wfMsg( 'rightsnone' );
173 $rightsnone = wfMsgForContent( 'rightsnone' );
175 if( !isset( $params[0] ) ||
trim( $params[0] ) == '' )
176 $params[0] = $rightsnone;
177 if( !isset( $params[1] ) ||
trim( $params[1] ) == '' )
178 $params[1] = $rightsnone;
180 if( count( $params ) == 0 ) {
182 $rv = wfMsg( $wgLogActions[$key], $titleLink );
184 $rv = wfMsgForContent( $wgLogActions[$key], $titleLink );
187 array_unshift( $params, $titleLink );
188 if ( $translate && $key == 'block/block' ) {
189 $params[1] = $wgLang->translateBlockExpiry( $params[1] );
190 $params[2] = isset( $params[2] )
191 ? self
::formatBlockFlags( $params[2] )
194 $rv = wfMsgReal( $wgLogActions[$key], $params, true, !$skin );
198 wfDebug( "LogPage::actionText - unknown action $key\n" );
201 if( $filterWikilinks ) {
202 $rv = str_replace( "[[", "", $rv );
203 $rv = str_replace( "]]", "", $rv );
210 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
211 * @param object &$target A title object.
212 * @param string $comment Description associated
213 * @param array $params Parameters passed later to wfMsg.* functions
215 function addEntry( $action, $target, $comment, $params = array() ) {
216 if ( !is_array( $params ) ) {
217 $params = array( $params );
220 $this->action
= $action;
221 $this->target
= $target;
222 $this->comment
= $comment;
223 $this->params
= LogPage
::makeParamBlob( $params );
225 $this->actionText
= LogPage
::actionText( $this->type
, $action, $target, NULL, $params );
227 return $this->saveContent();
231 * Create a blob from a parameter array
234 function makeParamBlob( $params ) {
235 return implode( "\n", $params );
239 * Extract a parameter array from a blob
242 function extractParams( $blob ) {
243 if ( $blob === '' ) {
246 return explode( "\n", $blob );
251 * Convert a comma-delimited list of block log flags
252 * into a more readable (and translated) form
254 * @param $flags Flags to format
257 public static function formatBlockFlags( $flags ) {
258 $flags = explode( ',', trim( $flags ) );
259 if( count( $flags ) > 0 ) {
260 for( $i = 0; $i < count( $flags ); $i++
)
261 $flags[$i] = self
::formatBlockFlag( $flags[$i] );
262 return '(' . implode( ', ', $flags ) . ')';
269 * Translate a block log flag if possible
271 * @param $flag Flag to translate
274 public static function formatBlockFlag( $flag ) {
275 static $messages = array();
276 if( !isset( $messages[$flag] ) ) {
277 $k = 'block-log-flags-' . $flag;
279 $messages[$flag] = htmlspecialchars( wfEmptyMsg( $k, $msg ) ?
$flag : $msg );
281 return $messages[$flag];