Add debug log entry
[lhc/web/wiklou.git] / includes / LogPage.php
1 <?php
2 #
3 # Copyright (C) 2002, 2004 Brion Vibber <brion@pobox.com>
4 # http://www.mediawiki.org/
5 #
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.
10 #
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.
15 #
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
20
21 /**
22 * Contain log classes
23 *
24 */
25
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.
30 *
31 */
32 class LogPage {
33 const DELETED_ACTION = 1;
34 const DELETED_COMMENT = 2;
35 const DELETED_USER = 4;
36 const DELETED_RESTRICTED = 8;
37 /* @access private */
38 var $type, $action, $comment, $params, $target;
39 /* @acess public */
40 var $updateRecentChanges;
41
42 /**
43 * Constructor
44 *
45 * @param string $type One of '', 'block', 'protect', 'rights', 'delete',
46 * 'upload', 'move'
47 * @param bool $rc Whether to update recent changes as well as the logging table
48 */
49 function __construct( $type, $rc = true ) {
50 $this->type = $type;
51 $this->updateRecentChanges = $rc;
52 }
53
54 function saveContent() {
55 if( wfReadOnly() ) return false;
56
57 global $wgUser, $wgLogRestrictions;
58 $fname = 'LogPage::saveContent';
59
60 $dbw = wfGetDB( DB_MASTER );
61 $uid = $wgUser->getID();
62 $log_id = $dbw->nextSequenceValue( 'log_log_id_seq' );
63
64 $this->timestamp = $now = wfTimestampNow();
65 $data = array(
66 'log_type' => $this->type,
67 'log_action' => $this->action,
68 'log_timestamp' => $dbw->timestamp( $now ),
69 'log_user' => $uid,
70 'log_namespace' => $this->target->getNamespace(),
71 'log_title' => $this->target->getDBkey(),
72 'log_comment' => $this->comment,
73 'log_params' => $this->params
74 );
75 $dbw->insert( 'logging', $data, $fname );
76 $newId = $dbw->insertId();
77
78 $ok = ($dbw->affectedRows() > 0);
79 # And update recentchanges
80 if( $ok && $this->updateRecentChanges ) {
81 # Don't add private logs to RC!
82 if( !isset($wgLogRestrictions[$this->type]) || $wgLogRestrictions[$this->type]=='*' ) {
83 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
84 $rcComment = $this->getRcComment();
85 RecentChange::notifyLog( $now, $titleObj, $wgUser, $rcComment, '',
86 $this->type, $this->action, $this->target, $this->comment, $this->params, $newId );
87 }
88 } else {
89 wfDebug( "LogPage::saveContent failed to insert row - Error {$dbw->lastErrno()}: {$dbw->lastError()}" );
90 }
91 return $ok;
92 }
93
94 public function getRcComment() {
95 $rcComment = $this->actionText;
96 if( '' != $this->comment ) {
97 if ($rcComment == '')
98 $rcComment = $this->comment;
99 else
100 $rcComment .= ': ' . $this->comment;
101 }
102 return $rcComment;
103 }
104
105 /**
106 * @static
107 */
108 public static function validTypes() {
109 global $wgLogTypes;
110 return $wgLogTypes;
111 }
112
113 /**
114 * @static
115 */
116 public static function isLogType( $type ) {
117 return in_array( $type, LogPage::validTypes() );
118 }
119
120 /**
121 * @static
122 */
123 public static function logName( $type ) {
124 global $wgLogNames, $wgMessageCache;
125
126 if( isset( $wgLogNames[$type] ) ) {
127 $wgMessageCache->loadAllMessages();
128 return str_replace( '_', ' ', wfMsg( $wgLogNames[$type] ) );
129 } else {
130 // Bogus log types? Perhaps an extension was removed.
131 return $type;
132 }
133 }
134
135 /**
136 * @todo handle missing log types
137 * @param string $type logtype
138 * @return string Headertext of this logtype
139 */
140 static function logHeader( $type ) {
141 global $wgLogHeaders;
142 return wfMsgExt($wgLogHeaders[$type],array('parseinline'));
143 }
144
145 /**
146 * @static
147 */
148 static function actionText( $type, $action, $title = NULL, $skin = NULL, $params = array(), $filterWikilinks=false ) {
149 global $wgLang, $wgContLang, $wgLogActions;
150
151 $key = "$type/$action";
152
153 if( $key == 'patrol/patrol' )
154 return PatrolLog::makeActionText( $title, $params, $skin );
155
156 if( isset( $wgLogActions[$key] ) ) {
157 if( is_null( $title ) ) {
158 $rv=wfMsg( $wgLogActions[$key] );
159 } else {
160 if( $skin ) {
161
162 switch( $type ) {
163 case 'move':
164 $titleLink = $skin->makeLinkObj( $title, htmlspecialchars( $title->getPrefixedText() ), 'redirect=no' );
165 $params[0] = $skin->makeLinkObj( Title::newFromText( $params[0] ), htmlspecialchars( $params[0] ) );
166 break;
167 case 'block':
168 if( substr( $title->getText(), 0, 1 ) == '#' ) {
169 $titleLink = $title->getText();
170 } else {
171 // TODO: Store the user identifier in the parameters
172 // to make this faster for future log entries
173 $id = User::idFromName( $title->getText() );
174 $titleLink = $skin->userLink( $id, $title->getText() )
175 . $skin->userToolLinks( $id, $title->getText(), false, Linker::TOOL_LINKS_NOBLOCK );
176 }
177 break;
178 case 'rights':
179 $text = $wgContLang->ucfirst( $title->getText() );
180 $titleLink = $skin->makeLinkObj( Title::makeTitle( NS_USER, $text ) );
181 break;
182 case 'merge':
183 $titleLink = $skin->makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
184 $params[0] = $skin->makeLinkObj( Title::newFromText( $params[0] ), htmlspecialchars( $params[0] ) );
185 $params[1] = $wgLang->timeanddate( $params[1] );
186 break;
187 default:
188 $titleLink = $skin->makeLinkObj( $title );
189 }
190
191 } else {
192 $titleLink = $title->getPrefixedText();
193 }
194 if( $key == 'rights/rights' ) {
195 if ($skin) {
196 $rightsnone = wfMsg( 'rightsnone' );
197 } else {
198 $rightsnone = wfMsgForContent( 'rightsnone' );
199 }
200 if( !isset( $params[0] ) || trim( $params[0] ) == '' )
201 $params[0] = $rightsnone;
202 if( !isset( $params[1] ) || trim( $params[1] ) == '' )
203 $params[1] = $rightsnone;
204 }
205 if( count( $params ) == 0 ) {
206 if ( $skin ) {
207 $rv = wfMsg( $wgLogActions[$key], $titleLink );
208 } else {
209 $rv = wfMsgForContent( $wgLogActions[$key], $titleLink );
210 }
211 } else {
212 array_unshift( $params, $titleLink );
213 if ( $key == 'block/block' || $key == 'suppress/block' ) {
214 if ( $skin ) {
215 $params[1] = '<span title="' . htmlspecialchars( $params[1] ). '">' . $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
216 } else {
217 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
218 }
219 $params[2] = isset( $params[2] )
220 ? self::formatBlockFlags( $params[2], is_null( $skin ) )
221 : '';
222 }
223 $rv = wfMsgReal( $wgLogActions[$key], $params, true, !$skin );
224 }
225 }
226 } else {
227 wfDebug( "LogPage::actionText - unknown action $key\n" );
228 $rv = "$action";
229 }
230 if( $filterWikilinks ) {
231 $rv = str_replace( "[[", "", $rv );
232 $rv = str_replace( "]]", "", $rv );
233 }
234 return $rv;
235 }
236
237 /**
238 * Add a log entry
239 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
240 * @param object &$target A title object.
241 * @param string $comment Description associated
242 * @param array $params Parameters passed later to wfMsg.* functions
243 */
244 function addEntry( $action, $target, $comment, $params = array() ) {
245 if ( !is_array( $params ) ) {
246 $params = array( $params );
247 }
248
249 $this->action = $action;
250 $this->target = $target;
251 $this->comment = $comment;
252 $this->params = LogPage::makeParamBlob( $params );
253
254 $this->actionText = LogPage::actionText( $this->type, $action, $target, NULL, $params );
255
256 return $this->saveContent();
257 }
258
259 /**
260 * Create a blob from a parameter array
261 * @static
262 */
263 static function makeParamBlob( $params ) {
264 return implode( "\n", $params );
265 }
266
267 /**
268 * Extract a parameter array from a blob
269 * @static
270 */
271 static function extractParams( $blob ) {
272 if ( $blob === '' ) {
273 return array();
274 } else {
275 return explode( "\n", $blob );
276 }
277 }
278
279 /**
280 * Convert a comma-delimited list of block log flags
281 * into a more readable (and translated) form
282 *
283 * @param $flags Flags to format
284 * @param $forContent Whether to localize the message depending of the user
285 * language
286 * @return string
287 */
288 public static function formatBlockFlags( $flags, $forContent = false ) {
289 $flags = explode( ',', trim( $flags ) );
290 if( count( $flags ) > 0 ) {
291 for( $i = 0; $i < count( $flags ); $i++ )
292 $flags[$i] = self::formatBlockFlag( $flags[$i], $forContent );
293 return '(' . implode( ', ', $flags ) . ')';
294 } else {
295 return '';
296 }
297 }
298
299 /**
300 * Translate a block log flag if possible
301 *
302 * @param $flag Flag to translate
303 * @param $forContent Whether to localize the message depending of the user
304 * language
305 * @return string
306 */
307 public static function formatBlockFlag( $flag, $forContent = false ) {
308 static $messages = array();
309 if( !isset( $messages[$flag] ) ) {
310 $k = 'block-log-flags-' . $flag;
311 if( $forContent )
312 $msg = wfMsgForContent( $k );
313 else
314 $msg = wfMsg( $k );
315 $messages[$flag] = htmlspecialchars( wfEmptyMsg( $k, $msg ) ? $flag : $msg );
316 }
317 return $messages[$flag];
318 }
319 }
320
321 /**
322 * Aliases for backwards compatibility with 1.6
323 */
324 define( 'MW_LOG_DELETED_ACTION', LogPage::DELETED_ACTION );
325 define( 'MW_LOG_DELETED_USER', LogPage::DELETED_USER );
326 define( 'MW_LOG_DELETED_COMMENT', LogPage::DELETED_COMMENT );
327 define( 'MW_LOG_DELETED_RESTRICTED', LogPage::DELETED_RESTRICTED );