3 # Copyright (C) 2002, 2004 Brion Vibber <brion@pobox.com>
4 # http://www.mediawiki.org/
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.
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.
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
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.
33 const DELETED_ACTION
= 1;
34 const DELETED_COMMENT
= 2;
35 const DELETED_USER
= 4;
36 const DELETED_RESTRICTED
= 8;
38 const SUPPRESSED_USER
= 12;
39 const SUPPRESSED_ACTION
= 9;
41 var $type, $action, $comment, $params, $target, $doer;
43 var $updateRecentChanges, $sendToUDP;
48 * @param string $type One of '', 'block', 'protect', 'rights', 'delete',
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
53 public function __construct( $type, $rc = true, $udp = 'skipUDP' ) {
55 $this->updateRecentChanges
= $rc;
56 $this->sendToUDP
= ($udp == 'UDP');
59 protected function saveContent() {
60 global $wgLogRestrictions;
62 $dbw = wfGetDB( DB_MASTER
);
63 $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
65 $this->timestamp
= $now = wfTimestampNow();
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
79 $dbw->insert( 'logging', $data, __METHOD__
);
80 $newId = !is_null($log_id) ?
$log_id : $dbw->insertId();
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
] !='*' ) {
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 );
103 * Get the RC comment from the last addEntry() call
105 public function getRcComment() {
106 $rcComment = $this->actionText
;
107 if( '' != $this->comment
) {
108 if ($rcComment == '')
109 $rcComment = $this->comment
;
111 $rcComment .= wfMsgForContent( 'colon-separator' ) . $this->comment
;
117 * Get the comment from the last addEntry() call
119 public function getComment() {
120 return $this->comment
;
126 public static function validTypes() {
134 public static function isLogType( $type ) {
135 return in_array( $type, LogPage
::validTypes() );
140 * @param string $type logtype
142 public static function logName( $type ) {
143 global $wgLogNames, $wgMessageCache;
145 if( isset( $wgLogNames[$type] ) ) {
146 $wgMessageCache->loadAllMessages();
147 return str_replace( '_', ' ', wfMsg( $wgLogNames[$type] ) );
149 // Bogus log types? Perhaps an extension was removed.
155 * @todo handle missing log types
156 * @param string $type logtype
157 * @return string Headertext of this logtype
159 public static function logHeader( $type ) {
160 global $wgLogHeaders, $wgMessageCache;
161 $wgMessageCache->loadAllMessages();
162 return wfMsgExt($wgLogHeaders[$type],array('parseinline'));
167 * @return HTML string
169 public static function actionText( $type, $action, $title = null, $skin = null,
170 $params = array(), $filterWikilinks = false )
172 global $wgLang, $wgContLang, $wgLogActions, $wgMessageCache;
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 );
180 if( isset( $wgLogActions[$key] ) ) {
181 if( is_null( $title ) ) {
182 $rv = wfMsgHtml( $wgLogActions[$key] );
184 $titleLink = self
::getTitleLink( $type, $skin, $title, $params );
185 if( $key == 'rights/rights' ) {
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 );
194 $rightsnone = wfMsgForContent( 'rightsnone' );
196 if( !isset( $params[0] ) ||
trim( $params[0] ) == '' )
197 $params[0] = $rightsnone;
198 if( !isset( $params[1] ) ||
trim( $params[1] ) == '' )
199 $params[1] = $rightsnone;
201 if( count( $params ) == 0 ) {
203 $rv = wfMsgHtml( $wgLogActions[$key], $titleLink );
205 $rv = wfMsgExt( $wgLogActions[$key], array( 'parsemag', 'escape', 'replaceafter', 'content' ), $titleLink );
209 array_unshift( $params, $titleLink );
211 if ( preg_match( '/^(block|suppress)\/(block|reblock)$/', $key ) ) {
213 $params[1] = '<span title="' . htmlspecialchars( $params[1] ). '">' .
214 $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
216 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
218 $params[2] = isset( $params[2] ) ?
219 self
::formatBlockFlags( $params[2], is_null( $skin ) ) : '';
221 } else if ( $type == 'protect' && count($params) == 3 ) {
222 // Restrictions and expiries
224 $details .= htmlspecialchars( " {$params[1]}" );
226 $details .= " {$params[1]}";
231 $details .= ' ['.wfMsg('protect-summary-cascade').']';
233 $details .= ' ['.wfMsgForContent('protect-summary-cascade').']';
237 } else if ( $type == 'move' && count( $params ) == 3 ) {
240 $details .= ' [' . wfMsg( 'move-redirect-suppressed' ) . ']';
242 $details .= ' [' . wfMsgForContent( 'move-redirect-suppressed' ) . ']';
246 } else if ( preg_match( '/^(delete|suppress)\/revision$/', $key ) && count( $params ) == 5 ) {
247 $count = substr_count( $params[2], ',' ) +
1; // revisions
248 $ofield = intval( substr( $params[3], 7 ) ); // <ofield=x>
249 $nfield = intval( substr( $params[4], 7 ) ); // <nfield=x>
250 $details .= ': '.RevisionDeleter
::getLogMessage( $count, $nfield, $ofield, false );
252 } else if ( preg_match( '/^(delete|suppress)\/event$/', $key ) && count( $params ) == 4 ) {
253 $count = substr_count( $params[1], ',' ) +
1; // log items
254 $ofield = intval( substr( $params[2], 7 ) ); // <ofield=x>
255 $nfield = intval( substr( $params[3], 7 ) ); // <nfield=x>
256 $details .= ': '.RevisionDeleter
::getLogMessage( $count, $nfield, $ofield, true );
259 $rv = wfMsgHtml( $wgLogActions[$key], $params ) . $details;
261 $rv = wfMsgExt( $wgLogActions[$key], array( 'parsemag', 'escape', 'replaceafter', 'content' ), $params ) . $details;
266 global $wgLogActionsHandlers;
267 if( isset( $wgLogActionsHandlers[$key] ) ) {
268 $args = func_get_args();
269 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
271 wfDebug( "LogPage::actionText - unknown action $key\n" );
276 // For the perplexed, this feature was added in r7855 by Erik.
277 // The feature was added because we liked adding [[$1]] in our log entries
278 // but the log entries are parsed as Wikitext on RecentChanges but as HTML
279 // on Special:Log. The hack is essentially that [[$1]] represented a link
280 // to the title in question. The first parameter to the HTML version (Special:Log)
281 // is that link in HTML form, and so this just gets rid of the ugly [[]].
282 // However, this is a horrible hack and it doesn't work like you expect if, say,
283 // you want to link to something OTHER than the title of the log entry.
284 // The real problem, which Erik was trying to fix (and it sort-of works now) is
285 // that the same messages are being treated as both wikitext *and* HTML.
286 if( $filterWikilinks ) {
287 $rv = str_replace( "[[", "", $rv );
288 $rv = str_replace( "]]", "", $rv );
293 protected static function getTitleLink( $type, $skin, $title, &$params ) {
294 global $wgLang, $wgContLang;
296 return $title->getPrefixedText();
300 $titleLink = $skin->link(
302 htmlspecialchars( $title->getPrefixedText() ),
304 array( 'redirect' => 'no' )
306 $targetTitle = Title
::newFromText( $params[0] );
307 if ( !$targetTitle ) {
308 # Workaround for broken database
309 $params[0] = htmlspecialchars( $params[0] );
311 $params[0] = $skin->link(
313 htmlspecialchars( $params[0] )
318 if( substr( $title->getText(), 0, 1 ) == '#' ) {
319 $titleLink = $title->getText();
321 // TODO: Store the user identifier in the parameters
322 // to make this faster for future log entries
323 $id = User
::idFromName( $title->getText() );
324 $titleLink = $skin->userLink( $id, $title->getText() )
325 . $skin->userToolLinks( $id, $title->getText(), false, Linker
::TOOL_LINKS_NOBLOCK
);
329 $text = $wgContLang->ucfirst( $title->getText() );
330 $titleLink = $skin->link( Title
::makeTitle( NS_USER
, $text ) );
333 $titleLink = $skin->link(
335 $title->getPrefixedText(),
337 array( 'redirect' => 'no' )
339 $params[0] = $skin->link(
340 Title
::newFromText( $params[0] ),
341 htmlspecialchars( $params[0] )
343 $params[1] = $wgLang->timeanddate( $params[1] );
346 if( $title->getNamespace() == NS_SPECIAL
) {
347 list( $name, $par ) = SpecialPage
::resolveAliasWithSubpage( $title->getDBkey() );
348 # Use the language name for log titles, rather than Log/X
349 if( $name == 'Log' ) {
350 $titleLink = '('.$skin->link( $title, LogPage
::logName( $par ) ).')';
352 $titleLink = $skin->link( $title );
355 $titleLink = $skin->link( $title );
363 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
364 * @param object &$target A title object.
365 * @param string $comment Description associated
366 * @param array $params Parameters passed later to wfMsg.* functions
367 * @param User $doer The user doing the action
369 public function addEntry( $action, $target, $comment, $params = array(), $doer = null ) {
370 if ( !is_array( $params ) ) {
371 $params = array( $params );
374 $this->action
= $action;
375 $this->target
= $target;
376 $this->comment
= $comment;
377 $this->params
= LogPage
::makeParamBlob( $params );
379 if ($doer === null) {
382 } elseif (!is_object( $doer ) ) {
383 $doer = User
::newFromId( $doer );
388 $this->actionText
= LogPage
::actionText( $this->type
, $action, $target, null, $params );
390 return $this->saveContent();
394 * Add relations to log_search table
397 public function addRelations( $field, $values, $logid ) {
398 if( !strlen($field) ||
empty($values) )
399 return false; // nothing
401 foreach( $values as $value ) {
402 $data[] = array('ls_field' => $field,'ls_value' => $value,'ls_log_id' => $logid);
404 $dbw = wfGetDB( DB_MASTER
);
405 $dbw->insert( 'log_search', $data, __METHOD__
, 'IGNORE' );
410 * Create a blob from a parameter array
413 public static function makeParamBlob( $params ) {
414 return implode( "\n", $params );
418 * Extract a parameter array from a blob
421 public static function extractParams( $blob ) {
422 if ( $blob === '' ) {
425 return explode( "\n", $blob );
430 * Convert a comma-delimited list of block log flags
431 * into a more readable (and translated) form
433 * @param $flags Flags to format
434 * @param $forContent Whether to localize the message depending of the user
438 public static function formatBlockFlags( $flags, $forContent = false ) {
441 $flags = explode( ',', trim( $flags ) );
442 if( count( $flags ) > 0 ) {
443 for( $i = 0; $i < count( $flags ); $i++
)
444 $flags[$i] = self
::formatBlockFlag( $flags[$i], $forContent );
445 return '(' . $wgLang->commaList( $flags ) . ')';
452 * Translate a block log flag if possible
454 * @param $flag Flag to translate
455 * @param $forContent Whether to localize the message depending of the user
459 public static function formatBlockFlag( $flag, $forContent = false ) {
460 static $messages = array();
461 if( !isset( $messages[$flag] ) ) {
462 $k = 'block-log-flags-' . $flag;
464 $msg = wfMsgForContent( $k );
467 $messages[$flag] = htmlspecialchars( wfEmptyMsg( $k, $msg ) ?
$flag : $msg );
469 return $messages[$flag];
474 * Aliases for backwards compatibility with 1.6
476 define( 'MW_LOG_DELETED_ACTION', LogPage
::DELETED_ACTION
);
477 define( 'MW_LOG_DELETED_USER', LogPage
::DELETED_USER
);
478 define( 'MW_LOG_DELETED_COMMENT', LogPage
::DELETED_COMMENT
);
479 define( 'MW_LOG_DELETED_RESTRICTED', LogPage
::DELETED_RESTRICTED
);