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( 'log_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_comment' => $this->comment
,
76 'log_params' => $this->params
78 $dbw->insert( 'logging', $data, __METHOD__
);
79 $newId = !is_null($log_id) ?
$log_id : $dbw->insertId();
81 # And update recentchanges
82 if( $this->updateRecentChanges
) {
83 $titleObj = SpecialPage
::getTitleFor( 'Log', $this->type
);
84 RecentChange
::notifyLog( $now, $titleObj, $this->doer
, $this->getRcComment(), '', $this->type
,
85 $this->action
, $this->target
, $this->comment
, $this->params
, $newId );
86 } else if( $this->sendToUDP
) {
87 # Don't send private logs to UDP
88 if( isset($wgLogRestrictions[$this->type
]) && $wgLogRestrictions[$this->type
] !='*' ) {
91 # Notify external application via UDP.
92 # We send this to IRC but do not want to add it the RC table.
93 $titleObj = SpecialPage
::getTitleFor( 'Log', $this->type
);
94 $rc = RecentChange
::newLogEntry( $now, $titleObj, $this->doer
, $this->getRcComment(), '',
95 $this->type
, $this->action
, $this->target
, $this->comment
, $this->params
, $newId );
102 * Get the RC comment from the last addEntry() call
104 public function getRcComment() {
105 $rcComment = $this->actionText
;
106 if( '' != $this->comment
) {
107 if ($rcComment == '')
108 $rcComment = $this->comment
;
110 $rcComment .= wfMsgForContent( 'colon-separator' ) . $this->comment
;
116 * Get the comment from the last addEntry() call
118 public function getComment() {
119 return $this->comment
;
125 public static function validTypes() {
133 public static function isLogType( $type ) {
134 return in_array( $type, LogPage
::validTypes() );
139 * @param string $type logtype
141 public static function logName( $type ) {
142 global $wgLogNames, $wgMessageCache;
144 if( isset( $wgLogNames[$type] ) ) {
145 $wgMessageCache->loadAllMessages();
146 return str_replace( '_', ' ', wfMsg( $wgLogNames[$type] ) );
148 // Bogus log types? Perhaps an extension was removed.
154 * @todo handle missing log types
155 * @param string $type logtype
156 * @return string Headertext of this logtype
158 public static function logHeader( $type ) {
159 global $wgLogHeaders, $wgMessageCache;
160 $wgMessageCache->loadAllMessages();
161 return wfMsgExt($wgLogHeaders[$type],array('parseinline'));
166 * @return HTML string
168 public static function actionText( $type, $action, $title = NULL, $skin = NULL,
169 $params = array(), $filterWikilinks = false )
171 global $wgLang, $wgContLang, $wgLogActions, $wgMessageCache;
173 $wgMessageCache->loadAllMessages();
174 $key = "$type/$action";
175 # Defer patrol log to PatrolLog class
176 if( $key == 'patrol/patrol' ) {
177 return PatrolLog
::makeActionText( $title, $params, $skin );
179 if( isset( $wgLogActions[$key] ) ) {
180 if( is_null( $title ) ) {
181 $rv = wfMsgHtml( $wgLogActions[$key] );
183 $titleLink = self
::getTitleLink( $type, $skin, $title, $params );
184 if( $key == 'rights/rights' ) {
186 $rightsnone = wfMsg( 'rightsnone' );
187 foreach ( $params as &$param ) {
188 $groupArray = array_map( 'trim', explode( ',', $param ) );
189 $groupArray = array_map( array( 'User', 'getGroupName' ), $groupArray );
190 $param = $wgLang->listToText( $groupArray );
193 $rightsnone = wfMsgForContent( 'rightsnone' );
195 if( !isset( $params[0] ) ||
trim( $params[0] ) == '' )
196 $params[0] = $rightsnone;
197 if( !isset( $params[1] ) ||
trim( $params[1] ) == '' )
198 $params[1] = $rightsnone;
200 if( count( $params ) == 0 ) {
202 $rv = wfMsgHtml( $wgLogActions[$key], $titleLink );
204 $rv = wfMsgExt( $wgLogActions[$key], array( 'parsemag', 'escape', 'replaceafter', 'content' ), $titleLink );
208 array_unshift( $params, $titleLink );
210 if ( preg_match( '/^(block|suppress)\/(block|reblock)$/', $key ) ) {
212 $params[1] = '<span title="' . htmlspecialchars( $params[1] ). '">' .
213 $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
215 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
217 $params[2] = isset( $params[2] ) ?
218 self
::formatBlockFlags( $params[2], is_null( $skin ) ) : '';
220 } else if ( $type == 'protect' && count($params) == 3 ) {
223 $details .= htmlspecialchars( " {$params[1]}" ); // restrictions and expiries
224 $details .= ' ['.wfMsg('protect-summary-cascade').']';
226 $details .= " {$params[1]}";
227 $details .= ' ['.wfMsgForContent('protect-summary-cascade').']';
231 } else if ( $type == 'move' && count( $params ) == 3 ) {
234 $details .= ' [' . wfMsg( 'move-redirect-suppressed' ) . ']';
236 $details .= ' [' . wfMsgForContent( 'move-redirect-suppressed' ) . ']';
240 } else if ( preg_match( '/^(delete|suppress)\/revision$/', $key ) && count( $params ) == 5 ) {
241 $count = substr_count( $params[2], ',' ) +
1; // revisions
242 $ofield = intval( substr( $params[3], 7 ) ); // <ofield=x>
243 $nfield = intval( substr( $params[4], 7 ) ); // <nfield=x>
244 $details .= ': '.RevisionDeleter
::getLogMessage( $count, $nfield, $ofield, false );
246 } else if ( preg_match( '/^(delete|suppress)\/event$/', $key ) && count( $params ) == 4 ) {
247 $count = substr_count( $params[1], ',' ) +
1; // log items
248 $ofield = intval( substr( $params[2], 7 ) ); // <ofield=x>
249 $nfield = intval( substr( $params[3], 7 ) ); // <nfield=x>
250 $details .= ': '.RevisionDeleter
::getLogMessage( $count, $nfield, $ofield, true );
253 $rv = wfMsgHtml( $wgLogActions[$key], $params ) . $details;
255 $rv = wfMsgExt( $wgLogActions[$key], array( 'parsemag', 'escape', 'replaceafter', 'content' ), $params ) . $details;
260 global $wgLogActionsHandlers;
261 if( isset( $wgLogActionsHandlers[$key] ) ) {
262 $args = func_get_args();
263 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
265 wfDebug( "LogPage::actionText - unknown action $key\n" );
270 // For the perplexed, this feature was added in r7855 by Erik.
271 // The feature was added because we liked adding [[$1]] in our log entries
272 // but the log entries are parsed as Wikitext on RecentChanges but as HTML
273 // on Special:Log. The hack is essentially that [[$1]] represented a link
274 // to the title in question. The first parameter to the HTML version (Special:Log)
275 // is that link in HTML form, and so this just gets rid of the ugly [[]].
276 // However, this is a horrible hack and it doesn't work like you expect if, say,
277 // you want to link to something OTHER than the title of the log entry.
278 // The real problem, which Erik was trying to fix (and it sort-of works now) is
279 // that the same messages are being treated as both wikitext *and* HTML.
280 if( $filterWikilinks ) {
281 $rv = str_replace( "[[", "", $rv );
282 $rv = str_replace( "]]", "", $rv );
287 protected static function getTitleLink( $type, $skin, $title, &$params ) {
288 global $wgLang, $wgContLang;
290 return $title->getPrefixedText();
294 $titleLink = $skin->link(
296 htmlspecialchars( $title->getPrefixedText() ),
298 array( 'redirect' => 'no' )
300 $targetTitle = Title
::newFromText( $params[0] );
301 if ( !$targetTitle ) {
302 # Workaround for broken database
303 $params[0] = htmlspecialchars( $params[0] );
305 $params[0] = $skin->link(
307 htmlspecialchars( $params[0] )
312 if( substr( $title->getText(), 0, 1 ) == '#' ) {
313 $titleLink = $title->getText();
315 // TODO: Store the user identifier in the parameters
316 // to make this faster for future log entries
317 $id = User
::idFromName( $title->getText() );
318 $titleLink = $skin->userLink( $id, $title->getText() )
319 . $skin->userToolLinks( $id, $title->getText(), false, Linker
::TOOL_LINKS_NOBLOCK
);
323 $text = $wgContLang->ucfirst( $title->getText() );
324 $titleLink = $skin->link( Title
::makeTitle( NS_USER
, $text ) );
327 $titleLink = $skin->link(
329 $title->getPrefixedText(),
331 array( 'redirect' => 'no' )
333 $params[0] = $skin->link(
334 Title
::newFromText( $params[0] ),
335 htmlspecialchars( $params[0] )
337 $params[1] = $wgLang->timeanddate( $params[1] );
340 if( $title->getNamespace() == NS_SPECIAL
) {
341 list( $name, $par ) = SpecialPage
::resolveAliasWithSubpage( $title->getDBkey() );
342 # Use the language name for log titles, rather than Log/X
343 if( $name == 'Log' ) {
344 $titleLink = '('.$skin->link( $title, LogPage
::logName( $par ) ).')';
346 $titleLink = $skin->link( $title );
349 $titleLink = $skin->link( $title );
357 * @param string $action one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
358 * @param object &$target A title object.
359 * @param string $comment Description associated
360 * @param array $params Parameters passed later to wfMsg.* functions
361 * @param User $doer The user doing the action
363 public function addEntry( $action, $target, $comment, $params = array(), $doer = null ) {
364 if ( !is_array( $params ) ) {
365 $params = array( $params );
368 $this->action
= $action;
369 $this->target
= $target;
370 $this->comment
= $comment;
371 $this->params
= LogPage
::makeParamBlob( $params );
373 if ($doer === null) {
376 } elseif (!is_object( $doer ) ) {
377 $doer = User
::newFromId( $doer );
382 $this->actionText
= LogPage
::actionText( $this->type
, $action, $target, NULL, $params );
384 return $this->saveContent();
388 * Add relations to log_search table
391 public function addRelations( $field, $values, $logid ) {
392 if( !strlen($field) ||
empty($values) )
393 return false; // nothing
395 foreach( $values as $value ) {
396 $data[] = array('ls_field' => $field,'ls_value' => $value,'ls_log_id' => $logid);
398 $dbw = wfGetDB( DB_MASTER
);
399 $dbw->insert( 'log_search', $data, __METHOD__
, 'IGNORE' );
404 * Create a blob from a parameter array
407 public static function makeParamBlob( $params ) {
408 return implode( "\n", $params );
412 * Extract a parameter array from a blob
415 public static function extractParams( $blob ) {
416 if ( $blob === '' ) {
419 return explode( "\n", $blob );
424 * Convert a comma-delimited list of block log flags
425 * into a more readable (and translated) form
427 * @param $flags Flags to format
428 * @param $forContent Whether to localize the message depending of the user
432 public static function formatBlockFlags( $flags, $forContent = false ) {
435 $flags = explode( ',', trim( $flags ) );
436 if( count( $flags ) > 0 ) {
437 for( $i = 0; $i < count( $flags ); $i++
)
438 $flags[$i] = self
::formatBlockFlag( $flags[$i], $forContent );
439 return '(' . $wgLang->commaList( $flags ) . ')';
446 * Translate a block log flag if possible
448 * @param $flag Flag to translate
449 * @param $forContent Whether to localize the message depending of the user
453 public static function formatBlockFlag( $flag, $forContent = false ) {
454 static $messages = array();
455 if( !isset( $messages[$flag] ) ) {
456 $k = 'block-log-flags-' . $flag;
458 $msg = wfMsgForContent( $k );
461 $messages[$flag] = htmlspecialchars( wfEmptyMsg( $k, $msg ) ?
$flag : $msg );
463 return $messages[$flag];
468 * Aliases for backwards compatibility with 1.6
470 define( 'MW_LOG_DELETED_ACTION', LogPage
::DELETED_ACTION
);
471 define( 'MW_LOG_DELETED_USER', LogPage
::DELETED_USER
);
472 define( 'MW_LOG_DELETED_COMMENT', LogPage
::DELETED_COMMENT
);
473 define( 'MW_LOG_DELETED_RESTRICTED', LogPage
::DELETED_RESTRICTED
);