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