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