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