* Fixed E_NOTICE..
[mediawiki.git] / includes / LogPage.php
blobe58f65fdd95117720df95fd0b52a7591e8d6e8b4
1 <?php
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
21 /**
22 * Contain log classes
26 /**
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.
32 class LogPage {
33 /* @access private */
34 var $type, $action, $comment, $params, $target;
35 /* @acess public */
36 var $updateRecentChanges;
38 /**
39 * Constructor
41 * @param string $type One of '', 'block', 'protect', 'rights', 'delete',
42 * 'upload', 'move'
43 * @param bool $rc Whether to update recent changes as well as the logging table
45 function __construct( $type, $rc = true ) {
46 $this->type = $type;
47 $this->updateRecentChanges = $rc;
50 function saveContent() {
51 if( wfReadOnly() ) return false;
53 global $wgUser;
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();
61 $data = array(
62 'log_type' => $this->type,
63 'log_action' => $this->action,
64 'log_timestamp' => $dbw->timestamp( $now ),
65 'log_user' => $uid,
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 );
86 return true;
89 public function getRcComment() {
90 $rcComment = $this->actionText;
91 if( '' != $this->comment ) {
92 if ($rcComment == '')
93 $rcComment = $this->comment;
94 else
95 $rcComment .= ': ' . $this->comment;
97 return $rcComment;
101 * @static
103 public static function validTypes() {
104 global $wgLogTypes;
105 return $wgLogTypes;
109 * @static
111 public static function isLogType( $type ) {
112 return in_array( $type, LogPage::validTypes() );
116 * @static
118 public static function logName( $type ) {
119 global $wgLogNames, $wgMessageCache;
121 if( isset( $wgLogNames[$type] ) ) {
122 $wgMessageCache->loadAllMessages();
123 return str_replace( '_', ' ', wfMsg( $wgLogNames[$type] ) );
124 } else {
125 // Bogus log types? Perhaps an extension was removed.
126 return $type;
131 * @todo handle missing log types
132 * @static
134 static function logHeader( $type ) {
135 global $wgLogHeaders;
136 return wfMsg( $wgLogHeaders[$type] );
140 * @static
142 static function actionText( $type, $action, $title = NULL, $skin = NULL, $params = array(), $filterWikilinks=false ) {
143 global $wgLang, $wgContLang, $wgLogActions;
145 $key = "$type/$action";
147 if( $key == 'patrol/patrol' )
148 return PatrolLog::makeActionText( $title, $params, $skin );
150 if( isset( $wgLogActions[$key] ) ) {
151 if( is_null( $title ) ) {
152 $rv=wfMsg( $wgLogActions[$key] );
153 } else {
154 if( $skin ) {
156 switch( $type ) {
157 case 'move':
158 $titleLink = $skin->makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
159 $params[0] = $skin->makeLinkObj( Title::newFromText( $params[0] ), htmlspecialchars( $params[0] ) );
160 break;
161 case 'block':
162 if( substr( $title->getText(), 0, 1 ) == '#' ) {
163 $titleLink = $title->getText();
164 } else {
165 // TODO: Store the user identifier in the parameters
166 // to make this faster for future log entries
167 $id = User::idFromName( $title->getText() );
168 $titleLink = $skin->userLink( $id, $title->getText() )
169 . $skin->userToolLinks( $id, $title->getText(), false, Linker::TOOL_LINKS_NOBLOCK );
171 break;
172 case 'rights':
173 $text = $wgContLang->ucfirst( $title->getText() );
174 $titleLink = $skin->makeLinkObj( Title::makeTitle( NS_USER, $text ) );
175 break;
176 default:
177 $titleLink = $skin->makeLinkObj( $title );
180 } else {
181 $titleLink = $title->getPrefixedText();
183 if( $key == 'rights/rights' ) {
184 if ($skin) {
185 $rightsnone = wfMsg( 'rightsnone' );
186 } else {
187 $rightsnone = wfMsgForContent( 'rightsnone' );
189 if( !isset( $params[0] ) || trim( $params[0] ) == '' )
190 $params[0] = $rightsnone;
191 if( !isset( $params[1] ) || trim( $params[1] ) == '' )
192 $params[1] = $rightsnone;
194 if( count( $params ) == 0 ) {
195 if ( $skin ) {
196 $rv = wfMsg( $wgLogActions[$key], $titleLink );
197 } else {
198 $rv = wfMsgForContent( $wgLogActions[$key], $titleLink );
200 } else {
201 array_unshift( $params, $titleLink );
202 if ( $key == 'block/block' ) {
203 if ( $skin ) {
204 $params[1] = '<span title="' . htmlspecialchars( $params[1] ). '">' . $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
205 } else {
206 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
208 $params[2] = isset( $params[2] )
209 ? self::formatBlockFlags( $params[2] )
210 : '';
212 $rv = wfMsgReal( $wgLogActions[$key], $params, true, !$skin );
215 } else {
216 wfDebug( "LogPage::actionText - unknown action $key\n" );
217 $rv = "$action";
219 if( $filterWikilinks ) {
220 $rv = str_replace( "[[", "", $rv );
221 $rv = str_replace( "]]", "", $rv );
223 return $rv;
227 * Add a log entry
228 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
229 * @param object &$target A title object.
230 * @param string $comment Description associated
231 * @param array $params Parameters passed later to wfMsg.* functions
233 function addEntry( $action, $target, $comment, $params = array() ) {
234 if ( !is_array( $params ) ) {
235 $params = array( $params );
238 $this->action = $action;
239 $this->target = $target;
240 $this->comment = $comment;
241 $this->params = LogPage::makeParamBlob( $params );
243 $this->actionText = LogPage::actionText( $this->type, $action, $target, NULL, $params );
245 return $this->saveContent();
249 * Create a blob from a parameter array
250 * @static
252 static function makeParamBlob( $params ) {
253 return implode( "\n", $params );
257 * Extract a parameter array from a blob
258 * @static
260 static function extractParams( $blob ) {
261 if ( $blob === '' ) {
262 return array();
263 } else {
264 return explode( "\n", $blob );
269 * Convert a comma-delimited list of block log flags
270 * into a more readable (and translated) form
272 * @param $flags Flags to format
273 * @return string
275 public static function formatBlockFlags( $flags ) {
276 $flags = explode( ',', trim( $flags ) );
277 if( count( $flags ) > 0 ) {
278 for( $i = 0; $i < count( $flags ); $i++ )
279 $flags[$i] = self::formatBlockFlag( $flags[$i] );
280 return '(' . implode( ', ', $flags ) . ')';
281 } else {
282 return '';
287 * Translate a block log flag if possible
289 * @param $flag Flag to translate
290 * @return string
292 public static function formatBlockFlag( $flag ) {
293 static $messages = array();
294 if( !isset( $messages[$flag] ) ) {
295 $k = 'block-log-flags-' . $flag;
296 $msg = wfMsg( $k );
297 $messages[$flag] = htmlspecialchars( wfEmptyMsg( $k, $msg ) ? $flag : $msg );
299 return $messages[$flag];