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
28 * Class to simplify the use of log pages.
29 * The logs are now kept in a table which is easier to manage and trim
30 * than ever-growing wiki pages.
36 var $type, $action, $comment, $params, $target;
38 var $updateRecentChanges;
43 * @param string $type One of '', 'block', 'protect', 'rights', 'delete',
45 * @param bool $rc Whether to update recent changes as well as the logging table
47 function LogPage( $type, $rc = true ) {
49 $this->updateRecentChanges
= $rc;
52 function saveContent() {
53 if( wfReadOnly() ) return false;
56 $fname = 'LogPage::saveContent';
58 $dbw =& wfGetDB( DB_MASTER
);
59 $uid = $wgUser->getID();
61 $this->timestamp
= $now = wfTimestampNow();
62 $dbw->insert( 'logging',
64 'log_type' => $this->type
,
65 'log_action' => $this->action
,
66 'log_timestamp' => $dbw->timestamp( $now ),
68 'log_namespace' => $this->target
->getNamespace(),
69 'log_title' => $this->target
->getDBkey(),
70 'log_comment' => $this->comment
,
71 'log_params' => $this->params
75 # And update recentchanges
76 if ( $this->updateRecentChanges
) {
77 $titleObj = SpecialPage
::getTitleFor( 'Log', $this->type
);
78 $rcComment = $this->actionText
;
79 if( '' != $this->comment
) {
81 $rcComment = $this->comment
;
83 $rcComment .= ': ' . $this->comment
;
86 RecentChange
::notifyLog( $now, $titleObj, $wgUser, $rcComment, '',
87 $this->type
, $this->action
, $this->target
, $this->comment
, $this->params
);
95 function validTypes() {
103 function isLogType( $type ) {
104 return in_array( $type, LogPage
::validTypes() );
110 public static function logName( $type ) {
113 if( isset( $wgLogNames[$type] ) ) {
114 return str_replace( '_', ' ', wfMsg( $wgLogNames[$type] ) );
116 // Bogus log types? Perhaps an extension was removed.
122 * @fixme: handle missing log types
125 function logHeader( $type ) {
126 global $wgLogHeaders;
127 return wfMsg( $wgLogHeaders[$type] );
133 function actionText( $type, $action, $title = NULL, $skin = NULL, $params = array(), $filterWikilinks=false, $translate=false ) {
134 global $wgLang, $wgContLang, $wgLogActions;
136 $key = "$type/$action";
137 if( isset( $wgLogActions[$key] ) ) {
138 if( is_null( $title ) ) {
139 $rv=wfMsg( $wgLogActions[$key] );
145 $titleLink = $skin->makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
146 $params[0] = $skin->makeLinkObj( Title
::newFromText( $params[0] ), $params[0] );
149 if( substr( $title->getText(), 0, 1 ) == '#' ) {
150 $titleLink = $title->getText();
152 $titleLink = $skin->makeLinkObj( $title, $title->getText() );
153 $titleLink .= ' (' . $skin->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Contributions', $title->getDBkey() ), wfMsg( 'contribslink' ) ) . ')';
157 $text = $wgContLang->ucfirst( $title->getText() );
158 $titleLink = $skin->makeLinkObj( Title
::makeTitle( NS_USER
, $text ) );
161 $titleLink = $skin->makeLinkObj( $title );
165 $titleLink = $title->getPrefixedText();
167 if( $key == 'rights/rights' ) {
169 $rightsnone = wfMsg( 'rightsnone' );
171 $rightsnone = wfMsgForContent( 'rightsnone' );
173 if( !isset( $params[0] ) ||
trim( $params[0] ) == '' )
174 $params[0] = $rightsnone;
175 if( !isset( $params[1] ) ||
trim( $params[1] ) == '' )
176 $params[1] = $rightsnone;
178 if( count( $params ) == 0 ) {
180 $rv = wfMsg( $wgLogActions[$key], $titleLink );
182 $rv = wfMsgForContent( $wgLogActions[$key], $titleLink );
185 array_unshift( $params, $titleLink );
186 if ( $translate && $key == 'block/block' ) {
187 $params[1] = $wgLang->translateBlockExpiry( $params[1] );
188 $params[2] = isset( $params[2] )
189 ? self
::formatBlockFlags( $params[2] )
192 $rv = wfMsgReal( $wgLogActions[$key], $params, true, !$skin );
196 wfDebug( "LogPage::actionText - unknown action $key\n" );
199 if( $filterWikilinks ) {
200 $rv = str_replace( "[[", "", $rv );
201 $rv = str_replace( "]]", "", $rv );
208 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
209 * @param object &$target A title object.
210 * @param string $comment Description associated
211 * @param array $params Parameters passed later to wfMsg.* functions
213 function addEntry( $action, $target, $comment, $params = array() ) {
214 if ( !is_array( $params ) ) {
215 $params = array( $params );
218 $this->action
= $action;
219 $this->target
= $target;
220 $this->comment
= $comment;
221 $this->params
= LogPage
::makeParamBlob( $params );
223 $this->actionText
= LogPage
::actionText( $this->type
, $action, $target, NULL, $params );
225 return $this->saveContent();
229 * Create a blob from a parameter array
232 function makeParamBlob( $params ) {
233 return implode( "\n", $params );
237 * Extract a parameter array from a blob
240 function extractParams( $blob ) {
241 if ( $blob === '' ) {
244 return explode( "\n", $blob );
249 * Convert a comma-delimited list of block log flags
250 * into a more readable (and translated) form
252 * @param $flags Flags to format
255 public static function formatBlockFlags( $flags ) {
256 $flags = explode( ',', trim( $flags ) );
257 if( count( $flags ) > 0 ) {
258 for( $i = 0; $i < count( $flags ); $i++
)
259 $flags[$i] = self
::formatBlockFlag( $flags[$i] );
260 return '(' . implode( ', ', $flags ) . ')';
267 * Translate a block log flag if possible
269 * @param $flag Flag to translate
272 public static function formatBlockFlag( $flag ) {
273 static $messages = array();
274 if( !isset( $messages[$flag] ) ) {
275 $k = 'block-log-flags-' . $flag;
277 $messages[$flag] = htmlspecialchars( wfEmptyMsg( $k, $msg ) ?
$flag : $msg );
279 return $messages[$flag];