(bug 19839) fix for r53173: Comments in log items are no more double escaped
[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 if NOT sent to RC
49 */
50 public function __construct( $type, $rc = true, $udp = 'skipUDP' ) {
51 $this->type = $type;
52 $this->updateRecentChanges = $rc;
53 $this->sendToUDP = ($udp == 'UDP');
54 }
55
56 protected function saveContent() {
57 global $wgLogRestrictions;
58
59 $dbw = wfGetDB( DB_MASTER );
60 $log_id = $dbw->nextSequenceValue( 'log_log_id_seq' );
61
62 $this->timestamp = $now = wfTimestampNow();
63 $data = array(
64 'log_id' => $log_id,
65 'log_type' => $this->type,
66 'log_action' => $this->action,
67 'log_timestamp' => $dbw->timestamp( $now ),
68 'log_user' => $this->doer->getId(),
69 'log_user_text' => $this->doer->getName(),
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, __METHOD__ );
76 $newId = !is_null($log_id) ? $log_id : $dbw->insertId();
77
78 # And update recentchanges
79 if( $this->updateRecentChanges ) {
80 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
81 RecentChange::notifyLog( $now, $titleObj, $this->doer, $this->getRcComment(), '', $this->type,
82 $this->action, $this->target, $this->comment, $this->params, $newId );
83 } else if( $this->sendToUDP ) {
84 # Don't send private logs to UDP
85 if( isset($wgLogRestrictions[$this->type]) && $wgLogRestrictions[$this->type] !='*' ) {
86 return true;
87 }
88 # Notify external application via UDP.
89 # We send this to IRC but do not want to add it the RC table.
90 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
91 $rc = RecentChange::newLogEntry( $now, $titleObj, $this->doer, $this->getRcComment(), '',
92 $this->type, $this->action, $this->target, $this->comment, $this->params, $newId );
93 $rc->notifyRC2UDP();
94 }
95 return $newId;
96 }
97
98 /**
99 * Get the RC comment from the last addEntry() call
100 */
101 public function getRcComment() {
102 $rcComment = $this->actionText;
103 if( '' != $this->comment ) {
104 if ($rcComment == '')
105 $rcComment = $this->comment;
106 else
107 $rcComment .= wfMsgForContent( 'colon-separator' ) . $this->comment;
108 }
109 return $rcComment;
110 }
111
112 /**
113 * Get the comment from the last addEntry() call
114 */
115 public function getComment() {
116 return $this->comment;
117 }
118
119 /**
120 * @static
121 */
122 public static function validTypes() {
123 global $wgLogTypes;
124 return $wgLogTypes;
125 }
126
127 /**
128 * @static
129 */
130 public static function isLogType( $type ) {
131 return in_array( $type, LogPage::validTypes() );
132 }
133
134 /**
135 * @static
136 * @param string $type logtype
137 */
138 public static function logName( $type ) {
139 global $wgLogNames, $wgMessageCache;
140
141 if( isset( $wgLogNames[$type] ) ) {
142 $wgMessageCache->loadAllMessages();
143 return str_replace( '_', ' ', wfMsg( $wgLogNames[$type] ) );
144 } else {
145 // Bogus log types? Perhaps an extension was removed.
146 return $type;
147 }
148 }
149
150 /**
151 * @todo handle missing log types
152 * @param string $type logtype
153 * @return string Headertext of this logtype
154 */
155 public static function logHeader( $type ) {
156 global $wgLogHeaders, $wgMessageCache;
157 $wgMessageCache->loadAllMessages();
158 return wfMsgExt($wgLogHeaders[$type],array('parseinline'));
159 }
160
161 /**
162 * @static
163 * @return HTML string
164 */
165 public static function actionText( $type, $action, $title = NULL, $skin = NULL,
166 $params = array(), $filterWikilinks = false )
167 {
168 global $wgLang, $wgContLang, $wgLogActions, $wgMessageCache;
169
170 $wgMessageCache->loadAllMessages();
171 $key = "$type/$action";
172 # Defer patrol log to PatrolLog class
173 if( $key == 'patrol/patrol' ) {
174 return PatrolLog::makeActionText( $title, $params, $skin );
175 }
176 if( isset( $wgLogActions[$key] ) ) {
177 if( is_null( $title ) ) {
178 $rv = wfMsgHtml( $wgLogActions[$key] );
179 } else {
180 $titleLink = self::getTitleLink( $type, $skin, $title, $params );
181 if( $key == 'rights/rights' ) {
182 if( $skin ) {
183 $rightsnone = wfMsg( 'rightsnone' );
184 foreach ( $params as &$param ) {
185 $groupArray = array_map( 'trim', explode( ',', $param ) );
186 $groupArray = array_map( array( 'User', 'getGroupName' ), $groupArray );
187 $param = $wgLang->listToText( $groupArray );
188 }
189 } else {
190 $rightsnone = wfMsgForContent( 'rightsnone' );
191 }
192 if( !isset( $params[0] ) || trim( $params[0] ) == '' )
193 $params[0] = $rightsnone;
194 if( !isset( $params[1] ) || trim( $params[1] ) == '' )
195 $params[1] = $rightsnone;
196 }
197 if( count( $params ) == 0 ) {
198 if ( $skin ) {
199 $rv = wfMsgHtml( $wgLogActions[$key], $titleLink );
200 } else {
201 $rv = wfMsgExt( $wgLogActions[$key], array( 'parsemag', 'escape', 'replaceafter', 'content' ), $titleLink );
202 }
203 } else {
204 $details = '';
205 array_unshift( $params, $titleLink );
206 // User suppression
207 if ( preg_match( '/^(block|suppress)\/(block|reblock)$/', $key ) ) {
208 if ( $skin ) {
209 $params[1] = '<span title="' . htmlspecialchars( $params[1] ). '">' .
210 $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 // Page protections
217 } else if ( $type == 'protect' && count($params) == 3 ) {
218 if( $params[2] ) {
219 if ( $skin ) {
220 $details .= htmlspecialchars( " {$params[1]}" ); // restrictions and expiries
221 $details .= ' ['.wfMsg('protect-summary-cascade').']';
222 } else {
223 $details .= " {$params[1]}";
224 $details .= ' ['.wfMsgForContent('protect-summary-cascade').']';
225 }
226 }
227 // Page moves
228 } else if ( $type == 'move' && count( $params ) == 3 ) {
229 if( $params[2] ) {
230 if ( $skin ) {
231 $details .= ' [' . wfMsg( 'move-redirect-suppressed' ) . ']';
232 } else {
233 $details .= ' [' . wfMsgForContent( 'move-redirect-suppressed' ) . ']';
234 }
235 }
236 // Revision deletion
237 } else if ( preg_match( '/^(delete|suppress)\/revision$/', $key ) && count( $params ) == 5 ) {
238 $count = substr_count( $params[2], ',' ) + 1; // revisions
239 $ofield = intval( substr( $params[3], 7 ) ); // <ofield=x>
240 $nfield = intval( substr( $params[4], 7 ) ); // <nfield=x>
241 $details .= ': '.RevisionDeleter::getLogMessage( $count, $nfield, $ofield, false );
242 // Log deletion
243 } else if ( preg_match( '/^(delete|suppress)\/event$/', $key ) && count( $params ) == 4 ) {
244 $count = substr_count( $params[1], ',' ) + 1; // log items
245 $ofield = intval( substr( $params[2], 7 ) ); // <ofield=x>
246 $nfield = intval( substr( $params[3], 7 ) ); // <nfield=x>
247 $details .= ': '.RevisionDeleter::getLogMessage( $count, $nfield, $ofield, true );
248 }
249 if ( $skin ) {
250 $rv = wfMsgHtml( $wgLogActions[$key], $params ) . $details;
251 } else {
252 $rv = wfMsgExt( $wgLogActions[$key], array( 'parsemag', 'escape', 'replaceafter', 'content' ), $params ) . $details;
253 }
254 }
255 }
256 } else {
257 global $wgLogActionsHandlers;
258 if( isset( $wgLogActionsHandlers[$key] ) ) {
259 $args = func_get_args();
260 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
261 } else {
262 wfDebug( "LogPage::actionText - unknown action $key\n" );
263 $rv = "$action";
264 }
265 }
266
267 // For the perplexed, this feature was added in r7855 by Erik.
268 // The feature was added because we liked adding [[$1]] in our log entries
269 // but the log entries are parsed as Wikitext on RecentChanges but as HTML
270 // on Special:Log. The hack is essentially that [[$1]] represented a link
271 // to the title in question. The first parameter to the HTML version (Special:Log)
272 // is that link in HTML form, and so this just gets rid of the ugly [[]].
273 // However, this is a horrible hack and it doesn't work like you expect if, say,
274 // you want to link to something OTHER than the title of the log entry.
275 // The real problem, which Erik was trying to fix (and it sort-of works now) is
276 // that the same messages are being treated as both wikitext *and* HTML.
277 if( $filterWikilinks ) {
278 $rv = str_replace( "[[", "", $rv );
279 $rv = str_replace( "]]", "", $rv );
280 }
281 return $rv;
282 }
283
284 protected static function getTitleLink( $type, $skin, $title, &$params ) {
285 global $wgLang, $wgContLang;
286 if( !$skin ) {
287 return $title->getPrefixedText();
288 }
289 switch( $type ) {
290 case 'move':
291 $titleLink = $skin->link(
292 $title,
293 htmlspecialchars( $title->getPrefixedText() ),
294 array(),
295 array( 'redirect' => 'no' )
296 );
297 $targetTitle = Title::newFromText( $params[0] );
298 if ( !$targetTitle ) {
299 # Workaround for broken database
300 $params[0] = htmlspecialchars( $params[0] );
301 } else {
302 $params[0] = $skin->link(
303 $targetTitle,
304 htmlspecialchars( $params[0] )
305 );
306 }
307 break;
308 case 'block':
309 if( substr( $title->getText(), 0, 1 ) == '#' ) {
310 $titleLink = $title->getText();
311 } else {
312 // TODO: Store the user identifier in the parameters
313 // to make this faster for future log entries
314 $id = User::idFromName( $title->getText() );
315 $titleLink = $skin->userLink( $id, $title->getText() )
316 . $skin->userToolLinks( $id, $title->getText(), false, Linker::TOOL_LINKS_NOBLOCK );
317 }
318 break;
319 case 'rights':
320 $text = $wgContLang->ucfirst( $title->getText() );
321 $titleLink = $skin->link( Title::makeTitle( NS_USER, $text ) );
322 break;
323 case 'merge':
324 $titleLink = $skin->link(
325 $title,
326 $title->getPrefixedText(),
327 array(),
328 array( 'redirect' => 'no' )
329 );
330 $params[0] = $skin->link(
331 Title::newFromText( $params[0] ),
332 htmlspecialchars( $params[0] )
333 );
334 $params[1] = $wgLang->timeanddate( $params[1] );
335 break;
336 default:
337 if( $title->getNamespace() == NS_SPECIAL ) {
338 list( $name, $par ) = SpecialPage::resolveAliasWithSubpage( $title->getDBkey() );
339 # Use the language name for log titles, rather than Log/X
340 if( $name == 'Log' ) {
341 $titleLink = '('.$skin->link( $title, LogPage::logName( $par ) ).')';
342 } else {
343 $titleLink = $skin->link( $title );
344 }
345 } else {
346 $titleLink = $skin->link( $title );
347 }
348 }
349 return $titleLink;
350 }
351
352 /**
353 * Add a log entry
354 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
355 * @param object &$target A title object.
356 * @param string $comment Description associated
357 * @param array $params Parameters passed later to wfMsg.* functions
358 * @param User $doer The user doing the action
359 */
360 public function addEntry( $action, $target, $comment, $params = array(), $doer = null ) {
361 if ( !is_array( $params ) ) {
362 $params = array( $params );
363 }
364
365 $this->action = $action;
366 $this->target = $target;
367 $this->comment = $comment;
368 $this->params = LogPage::makeParamBlob( $params );
369
370 if ($doer === null) {
371 global $wgUser;
372 $doer = $wgUser;
373 } elseif (!is_object( $doer ) ) {
374 $doer = User::newFromId( $doer );
375 }
376
377 $this->doer = $doer;
378
379 $this->actionText = LogPage::actionText( $this->type, $action, $target, NULL, $params );
380
381 return $this->saveContent();
382 }
383
384 /**
385 * Add relations to log_search table
386 * @static
387 */
388 public function addRelations( $field, $values, $logid ) {
389 if( !strlen($field) || empty($values) )
390 return false; // nothing
391 $data = array();
392 foreach( $values as $value ) {
393 $data[] = array('ls_field' => $field,'ls_value' => $value,'ls_log_id' => $logid);
394 }
395 $dbw = wfGetDB( DB_MASTER );
396 $dbw->insert( 'log_search', $data, __METHOD__, 'IGNORE' );
397 return true;
398 }
399
400 /**
401 * Create a blob from a parameter array
402 * @static
403 */
404 public static function makeParamBlob( $params ) {
405 return implode( "\n", $params );
406 }
407
408 /**
409 * Extract a parameter array from a blob
410 * @static
411 */
412 public static function extractParams( $blob ) {
413 if ( $blob === '' ) {
414 return array();
415 } else {
416 return explode( "\n", $blob );
417 }
418 }
419
420 /**
421 * Convert a comma-delimited list of block log flags
422 * into a more readable (and translated) form
423 *
424 * @param $flags Flags to format
425 * @param $forContent Whether to localize the message depending of the user
426 * language
427 * @return string
428 */
429 public static function formatBlockFlags( $flags, $forContent = false ) {
430 global $wgLang;
431
432 $flags = explode( ',', trim( $flags ) );
433 if( count( $flags ) > 0 ) {
434 for( $i = 0; $i < count( $flags ); $i++ )
435 $flags[$i] = self::formatBlockFlag( $flags[$i], $forContent );
436 return '(' . $wgLang->commaList( $flags ) . ')';
437 } else {
438 return '';
439 }
440 }
441
442 /**
443 * Translate a block log flag if possible
444 *
445 * @param $flag Flag to translate
446 * @param $forContent Whether to localize the message depending of the user
447 * language
448 * @return string
449 */
450 public static function formatBlockFlag( $flag, $forContent = false ) {
451 static $messages = array();
452 if( !isset( $messages[$flag] ) ) {
453 $k = 'block-log-flags-' . $flag;
454 if( $forContent )
455 $msg = wfMsgForContent( $k );
456 else
457 $msg = wfMsg( $k );
458 $messages[$flag] = htmlspecialchars( wfEmptyMsg( $k, $msg ) ? $flag : $msg );
459 }
460 return $messages[$flag];
461 }
462 }
463
464 /**
465 * Aliases for backwards compatibility with 1.6
466 */
467 define( 'MW_LOG_DELETED_ACTION', LogPage::DELETED_ACTION );
468 define( 'MW_LOG_DELETED_USER', LogPage::DELETED_USER );
469 define( 'MW_LOG_DELETED_COMMENT', LogPage::DELETED_COMMENT );
470 define( 'MW_LOG_DELETED_RESTRICTED', LogPage::DELETED_RESTRICTED );