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