Clean up log restrictions check
[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 * @file
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, $doer;
39 /* @acess public */
40 var $updateRecentChanges, $sendToUDP;
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 * @param bool $udp Whether to send to the UDP feed
49 */
50 function __construct( $type, $rc = true, $udp = true ) {
51 $this->type = $type;
52 $this->updateRecentChanges = $rc;
53 $this->sendToUDP = $udp;
54 }
55
56 protected function saveContent() {
57 global $wgUser, $wgLogRestrictions;
58 $fname = 'LogPage::saveContent';
59
60 $dbw = wfGetDB( DB_MASTER );
61 $log_id = $dbw->nextSequenceValue( 'log_log_id_seq' );
62
63 $this->timestamp = $now = wfTimestampNow();
64 $data = array(
65 'log_id' => $log_id,
66 'log_type' => $this->type,
67 'log_action' => $this->action,
68 'log_timestamp' => $dbw->timestamp( $now ),
69 'log_user' => $this->doer->getId(),
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 = !is_null($log_id) ? $log_id : $dbw->insertId();
77
78 if( !($dbw->affectedRows() > 0) ) {
79 wfDebugLog( "logging", "LogPage::saveContent failed to insert row - Error {$dbw->lastErrno()}: {$dbw->lastError()}" );
80 }
81 # And update recentchanges
82 if( $this->updateRecentChanges ) {
83 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
84 $rcComment = $this->getRcComment();
85 RecentChange::notifyLog( $now, $titleObj, $this->doer, $rcComment, '', $this->type,
86 $this->action, $this->target, $this->comment, $this->params, $newId );
87 } else if( $this->sendToUDP ) {
88 # Notify external application via UDP.
89 # We send this to IRC but do not want to add it the RC table.
90 global $wgRC2UDPAddress, $wgRC2UDPOmitBots;
91 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
92 $rcComment = $this->getRcComment();
93 $rc = RecentChange::newLogEntry( $now, $titleObj, $this->doer, $rcComment, '',
94 $this->type, $this->action, $this->target, $this->comment, $this->params, $newId );
95 if( $wgRC2UDPAddress && ( !$rc->getAttribute('rc_bot') || !$wgRC2UDPOmitBots ) ) {
96 RecentChange::sendToUDP( $rc->getIRCLine() );
97 }
98 }
99 return true;
100 }
101
102 /**
103 * Get the RC comment from the last addEntry() call
104 */
105 public function getRcComment() {
106 $rcComment = $this->actionText;
107 if( '' != $this->comment ) {
108 if ($rcComment == '')
109 $rcComment = $this->comment;
110 else
111 $rcComment .= wfMsgForContent( 'colon-separator' ) . $this->comment;
112 }
113 return $rcComment;
114 }
115
116 /**
117 * Get the comment from the last addEntry() call
118 */
119 public function getComment() {
120 return $this->comment;
121 }
122
123 /**
124 * @static
125 */
126 public static function validTypes() {
127 global $wgLogTypes;
128 return $wgLogTypes;
129 }
130
131 /**
132 * @static
133 */
134 public static function isLogType( $type ) {
135 return in_array( $type, LogPage::validTypes() );
136 }
137
138 /**
139 * @static
140 */
141 public static function logName( $type ) {
142 global $wgLogNames, $wgMessageCache;
143
144 if( isset( $wgLogNames[$type] ) ) {
145 $wgMessageCache->loadAllMessages();
146 return str_replace( '_', ' ', wfMsg( $wgLogNames[$type] ) );
147 } else {
148 // Bogus log types? Perhaps an extension was removed.
149 return $type;
150 }
151 }
152
153 /**
154 * @todo handle missing log types
155 * @param string $type logtype
156 * @return string Headertext of this logtype
157 */
158 static function logHeader( $type ) {
159 global $wgLogHeaders, $wgMessageCache;
160 $wgMessageCache->loadAllMessages();
161 return wfMsgExt($wgLogHeaders[$type],array('parseinline'));
162 }
163
164 /**
165 * @static
166 * @return HTML string
167 */
168 static function actionText( $type, $action, $title = NULL, $skin = NULL,
169 $params = array(), $filterWikilinks = false )
170 {
171 global $wgLang, $wgContLang, $wgLogActions, $wgMessageCache;
172
173 $wgMessageCache->loadAllMessages();
174 $key = "$type/$action";
175 # Defer patrol log to PatrolLog class
176 if( $key == 'patrol/patrol' ) {
177 return PatrolLog::makeActionText( $title, $params, $skin );
178 }
179 if( isset( $wgLogActions[$key] ) ) {
180 if( is_null( $title ) ) {
181 $rv = wfMsg( $wgLogActions[$key] );
182 } else {
183 $titleLink = self::getTitleLink( $type, $skin, $title, $params );
184 if( $key == 'rights/rights' ) {
185 if( $skin ) {
186 $rightsnone = wfMsg( 'rightsnone' );
187 foreach ( $params as &$param ) {
188 $groupArray = array_map( 'trim', explode( ',', $param ) );
189 $groupArray = array_map( array( 'User', 'getGroupName' ), $groupArray );
190 $param = $wgLang->listToText( $groupArray );
191 }
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 $details = '';
208 array_unshift( $params, $titleLink );
209 if ( $key == 'block/block' || $key == 'suppress/block' || $key == 'block/reblock' ) {
210 if ( $skin ) {
211 $params[1] = '<span title="' . htmlspecialchars( $params[1] ). '">' .
212 $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
213 } else {
214 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
215 }
216 $params[2] = isset( $params[2] ) ?
217 self::formatBlockFlags( $params[2], is_null( $skin ) ) : '';
218 } else if ( $type == 'protect' && count($params) == 3 ) {
219 $details .= " {$params[1]}"; // restrictions and expiries
220 if( $params[2] ) {
221 $details .= ' ['.wfMsg('protect-summary-cascade').']';
222 }
223 } else if ( $type == 'move' && count( $params ) == 3 ) {
224 if( $params[2] ) {
225 $details .= ' [' . wfMsg( 'move-redirect-suppressed' ) . ']';
226 }
227 }
228 $rv = wfMsgReal( $wgLogActions[$key], $params, true, !$skin ) . $details;
229 }
230 }
231 } else {
232 global $wgLogActionsHandlers;
233 if( isset( $wgLogActionsHandlers[$key] ) ) {
234 $args = func_get_args();
235 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
236 } else {
237 wfDebug( "LogPage::actionText - unknown action $key\n" );
238 $rv = "$action";
239 }
240 }
241 if( $filterWikilinks ) {
242 $rv = str_replace( "[[", "", $rv );
243 $rv = str_replace( "]]", "", $rv );
244 }
245 return $rv;
246 }
247
248 protected static function getTitleLink( $type, $skin, $title, &$params ) {
249 global $wgLang, $wgContLang;
250 if( !$skin ) {
251 return $title->getPrefixedText();
252 }
253 switch( $type ) {
254 case 'move':
255 $titleLink = $skin->makeLinkObj( $title,
256 htmlspecialchars( $title->getPrefixedText() ), 'redirect=no' );
257 $targetTitle = Title::newFromText( $params[0] );
258 if ( !$targetTitle ) {
259 # Workaround for broken database
260 $params[0] = htmlspecialchars( $params[0] );
261 } else {
262 $params[0] = $skin->makeLinkObj( $targetTitle, htmlspecialchars( $params[0] ) );
263 }
264 break;
265 case 'block':
266 if( substr( $title->getText(), 0, 1 ) == '#' ) {
267 $titleLink = $title->getText();
268 } else {
269 // TODO: Store the user identifier in the parameters
270 // to make this faster for future log entries
271 $id = User::idFromName( $title->getText() );
272 $titleLink = $skin->userLink( $id, $title->getText() )
273 . $skin->userToolLinks( $id, $title->getText(), false, Linker::TOOL_LINKS_NOBLOCK );
274 }
275 break;
276 case 'rights':
277 $text = $wgContLang->ucfirst( $title->getText() );
278 $titleLink = $skin->makeLinkObj( Title::makeTitle( NS_USER, $text ) );
279 break;
280 case 'merge':
281 $titleLink = $skin->makeLinkObj( $title, $title->getPrefixedText(), 'redirect=no' );
282 $params[0] = $skin->makeLinkObj( Title::newFromText( $params[0] ), htmlspecialchars( $params[0] ) );
283 $params[1] = $wgLang->timeanddate( $params[1] );
284 break;
285 default:
286 if( $title->getNamespace() == NS_SPECIAL ) {
287 list( $name, $par ) = SpecialPage::resolveAliasWithSubpage( $title->getDBKey() );
288 # Use the language name for log titles, rather than Log/X
289 if( $name == 'Log' ) {
290 $titleLink = '('.$skin->makeLinkObj( $title, LogPage::logName( $par ) ).')';
291 } else {
292 $titleLink = $skin->makeLinkObj( $title );
293 }
294 } else {
295 $titleLink = $skin->makeLinkObj( $title );
296 }
297 }
298 return $titleLink;
299 }
300
301 /**
302 * Add a log entry
303 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
304 * @param object &$target A title object.
305 * @param string $comment Description associated
306 * @param array $params Parameters passed later to wfMsg.* functions
307 * @param User $doer The user doing the action
308 */
309 function addEntry( $action, $target, $comment, $params = array(), $doer = null ) {
310 if ( !is_array( $params ) ) {
311 $params = array( $params );
312 }
313
314 $this->action = $action;
315 $this->target = $target;
316 $this->comment = $comment;
317 $this->params = LogPage::makeParamBlob( $params );
318
319 if ($doer === null) {
320 global $wgUser;
321 $doer = $wgUser;
322 } elseif (!is_object( $doer ) ) {
323 $doer = User::newFromId( $doer );
324 }
325
326 $this->doer = $doer;
327
328 $this->actionText = LogPage::actionText( $this->type, $action, $target, NULL, $params );
329
330 return $this->saveContent();
331 }
332
333 /**
334 * Create a blob from a parameter array
335 * @static
336 */
337 static function makeParamBlob( $params ) {
338 return implode( "\n", $params );
339 }
340
341 /**
342 * Extract a parameter array from a blob
343 * @static
344 */
345 static function extractParams( $blob ) {
346 if ( $blob === '' ) {
347 return array();
348 } else {
349 return explode( "\n", $blob );
350 }
351 }
352
353 /**
354 * Convert a comma-delimited list of block log flags
355 * into a more readable (and translated) form
356 *
357 * @param $flags Flags to format
358 * @param $forContent Whether to localize the message depending of the user
359 * language
360 * @return string
361 */
362 public static function formatBlockFlags( $flags, $forContent = false ) {
363 $flags = explode( ',', trim( $flags ) );
364 if( count( $flags ) > 0 ) {
365 for( $i = 0; $i < count( $flags ); $i++ )
366 $flags[$i] = self::formatBlockFlag( $flags[$i], $forContent );
367 return '(' . implode( ', ', $flags ) . ')';
368 } else {
369 return '';
370 }
371 }
372
373 /**
374 * Translate a block log flag if possible
375 *
376 * @param $flag Flag to translate
377 * @param $forContent Whether to localize the message depending of the user
378 * language
379 * @return string
380 */
381 public static function formatBlockFlag( $flag, $forContent = false ) {
382 static $messages = array();
383 if( !isset( $messages[$flag] ) ) {
384 $k = 'block-log-flags-' . $flag;
385 if( $forContent )
386 $msg = wfMsgForContent( $k );
387 else
388 $msg = wfMsg( $k );
389 $messages[$flag] = htmlspecialchars( wfEmptyMsg( $k, $msg ) ? $flag : $msg );
390 }
391 return $messages[$flag];
392 }
393 }
394
395 /**
396 * Aliases for backwards compatibility with 1.6
397 */
398 define( 'MW_LOG_DELETED_ACTION', LogPage::DELETED_ACTION );
399 define( 'MW_LOG_DELETED_USER', LogPage::DELETED_USER );
400 define( 'MW_LOG_DELETED_COMMENT', LogPage::DELETED_COMMENT );
401 define( 'MW_LOG_DELETED_RESTRICTED', LogPage::DELETED_RESTRICTED );