5 * Copyright © 2002, 2004 Brion Vibber <brion@pobox.com>
6 * http://www.mediawiki.org/
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * 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;
54 var $updateRecentChanges, $sendToUDP;
59 * @param $type String: one of '', 'block', 'protect', 'rights', 'delete',
61 * @param $rc Boolean: whether to update recent changes as well as the logging table
62 * @param $udp String: pass 'UDP' to send to the UDP feed if NOT sent to RC
64 public function __construct( $type, $rc = true, $udp = 'skipUDP' ) {
66 $this->updateRecentChanges
= $rc;
67 $this->sendToUDP
= ( $udp == 'UDP' );
71 * @return bool|int|null
73 protected function saveContent() {
74 global $wgLogRestrictions;
76 $dbw = wfGetDB( DB_MASTER
);
77 $log_id = $dbw->nextSequenceValue( 'logging_log_id_seq' );
79 $this->timestamp
= $now = wfTimestampNow();
82 'log_type' => $this->type
,
83 'log_action' => $this->action
,
84 'log_timestamp' => $dbw->timestamp( $now ),
85 'log_user' => $this->doer
->getId(),
86 'log_user_text' => $this->doer
->getName(),
87 'log_namespace' => $this->target
->getNamespace(),
88 'log_title' => $this->target
->getDBkey(),
89 'log_page' => $this->target
->getArticleId(),
90 'log_comment' => $this->comment
,
91 'log_params' => $this->params
93 $dbw->insert( 'logging', $data, __METHOD__
);
94 $newId = !is_null( $log_id ) ?
$log_id : $dbw->insertId();
96 # And update recentchanges
97 if( $this->updateRecentChanges
) {
98 $titleObj = SpecialPage
::getTitleFor( 'Log', $this->type
);
99 RecentChange
::notifyLog(
100 $now, $titleObj, $this->doer
, $this->getRcComment(), '',
101 $this->type
, $this->action
, $this->target
, $this->comment
,
102 $this->params
, $newId
104 } elseif( $this->sendToUDP
) {
105 # Don't send private logs to UDP
106 if( isset( $wgLogRestrictions[$this->type
] ) && $wgLogRestrictions[$this->type
] != '*' ) {
109 # Notify external application via UDP.
110 # We send this to IRC but do not want to add it the RC table.
111 $titleObj = SpecialPage
::getTitleFor( 'Log', $this->type
);
112 $rc = RecentChange
::newLogEntry(
113 $now, $titleObj, $this->doer
, $this->getRcComment(), '',
114 $this->type
, $this->action
, $this->target
, $this->comment
,
115 $this->params
, $newId
123 * Get the RC comment from the last addEntry() call
125 public function getRcComment() {
126 $rcComment = $this->actionText
;
127 if( $this->comment
!= '' ) {
128 if ( $rcComment == '' ) {
129 $rcComment = $this->comment
;
131 $rcComment .= wfMsgForContent( 'colon-separator' ) . $this->comment
;
138 * Get the comment from the last addEntry() call
140 public function getComment() {
141 return $this->comment
;
145 * Get the list of valid log types
147 * @return Array of strings
149 public static function validTypes() {
155 * Is $type a valid log type
157 * @param $type String: log type to check
160 public static function isLogType( $type ) {
161 return in_array( $type, LogPage
::validTypes() );
165 * Get the name for the given log type
167 * @param $type String: logtype
168 * @return String: log name
170 public static function logName( $type ) {
173 if( isset( $wgLogNames[$type] ) ) {
174 return str_replace( '_', ' ', wfMsg( $wgLogNames[$type] ) );
176 // Bogus log types? Perhaps an extension was removed.
182 * Get the log header for the given log type
184 * @todo handle missing log types
185 * @param $type String: logtype
186 * @return String: headertext of this logtype
188 public static function logHeader( $type ) {
189 global $wgLogHeaders;
190 return wfMsgExt( $wgLogHeaders[$type], array( 'parseinline' ) );
194 * Generate text for a log entry
196 * @param $type String: log type
197 * @param $action String: log action
198 * @param $title Mixed: Title object or null
199 * @param $skin Mixed: Skin object or null. If null, we want to use the wiki
200 * content language, since that will go to the IRC feed.
201 * @param $params Array: parameters
202 * @param $filterWikilinks Boolean: whether to filter wiki links
203 * @return HTML string
205 public static function actionText( $type, $action, $title = null, $skin = null,
206 $params = array(), $filterWikilinks = false )
208 global $wgLang, $wgContLang, $wgLogActions;
210 $key = "$type/$action";
211 # Defer patrol log to PatrolLog class
212 if( $key == 'patrol/patrol' ) {
213 return PatrolLog
::makeActionText( $title, $params, $skin );
215 if( isset( $wgLogActions[$key] ) ) {
216 if( is_null( $title ) ) {
217 $rv = wfMsgHtml( $wgLogActions[$key] );
219 $titleLink = self
::getTitleLink( $type, $skin, $title, $params );
220 if( $key == 'rights/rights' ) {
222 $rightsnone = wfMsg( 'rightsnone' );
223 foreach ( $params as &$param ) {
224 $groupArray = array_map( 'trim', explode( ',', $param ) );
225 $groupArray = array_map( array( 'User', 'getGroupMember' ), $groupArray );
226 $param = $wgLang->listToText( $groupArray );
229 $rightsnone = wfMsgForContent( 'rightsnone' );
231 if( !isset( $params[0] ) ||
trim( $params[0] ) == '' ) {
232 $params[0] = $rightsnone;
234 if( !isset( $params[1] ) ||
trim( $params[1] ) == '' ) {
235 $params[1] = $rightsnone;
238 if( count( $params ) == 0 ) {
240 $rv = wfMsgHtml( $wgLogActions[$key], $titleLink );
242 $rv = wfMsgExt( $wgLogActions[$key], array( 'parsemag', 'escape', 'replaceafter', 'content' ), $titleLink );
246 array_unshift( $params, $titleLink );
248 if ( preg_match( '/^(block|suppress)\/(block|reblock)$/', $key ) ) {
250 $params[1] = '<span class="blockExpiry" dir="ltr" title="' . htmlspecialchars( $params[1] ). '">' .
251 $wgLang->translateBlockExpiry( $params[1] ) . '</span>';
253 $params[1] = $wgContLang->translateBlockExpiry( $params[1] );
255 $params[2] = isset( $params[2] ) ?
256 self
::formatBlockFlags( $params[2], is_null( $skin ) ) : '';
259 } elseif ( $type == 'protect' && count($params) == 3 ) {
260 // Restrictions and expiries
262 $details .= htmlspecialchars( " {$params[1]}" );
264 $details .= " {$params[1]}";
269 $details .= ' [' . wfMsg( 'protect-summary-cascade' ) . ']';
271 $details .= ' [' . wfMsgForContent( 'protect-summary-cascade' ) . ']';
276 } elseif ( $type == 'move' && count( $params ) == 3 ) {
279 $details .= ' [' . wfMsg( 'move-redirect-suppressed' ) . ']';
281 $details .= ' [' . wfMsgForContent( 'move-redirect-suppressed' ) . ']';
286 } elseif ( preg_match( '/^(delete|suppress)\/revision$/', $key ) && count( $params ) == 5 ) {
287 $count = substr_count( $params[2], ',' ) +
1; // revisions
288 $ofield = intval( substr( $params[3], 7 ) ); // <ofield=x>
289 $nfield = intval( substr( $params[4], 7 ) ); // <nfield=x>
290 $details .= ': ' . RevisionDeleter
::getLogMessage( $count, $nfield, $ofield, false, is_null( $skin ) );
293 } elseif ( preg_match( '/^(delete|suppress)\/event$/', $key ) && count( $params ) == 4 ) {
294 $count = substr_count( $params[1], ',' ) +
1; // log items
295 $ofield = intval( substr( $params[2], 7 ) ); // <ofield=x>
296 $nfield = intval( substr( $params[3], 7 ) ); // <nfield=x>
297 $details .= ': ' . RevisionDeleter
::getLogMessage( $count, $nfield, $ofield, true, is_null( $skin ) );
301 $rv = wfMsgHtml( $wgLogActions[$key], $params ) . $details;
303 $rv = wfMsgExt( $wgLogActions[$key], array( 'parsemag', 'escape', 'replaceafter', 'content' ), $params ) . $details;
308 global $wgLogActionsHandlers;
309 if( isset( $wgLogActionsHandlers[$key] ) ) {
310 $args = func_get_args();
311 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
313 wfDebug( "LogPage::actionText - unknown action $key\n" );
318 // For the perplexed, this feature was added in r7855 by Erik.
319 // The feature was added because we liked adding [[$1]] in our log entries
320 // but the log entries are parsed as Wikitext on RecentChanges but as HTML
321 // on Special:Log. The hack is essentially that [[$1]] represented a link
322 // to the title in question. The first parameter to the HTML version (Special:Log)
323 // is that link in HTML form, and so this just gets rid of the ugly [[]].
324 // However, this is a horrible hack and it doesn't work like you expect if, say,
325 // you want to link to something OTHER than the title of the log entry.
326 // The real problem, which Erik was trying to fix (and it sort-of works now) is
327 // that the same messages are being treated as both wikitext *and* HTML.
328 if( $filterWikilinks ) {
329 $rv = str_replace( '[[', '', $rv );
330 $rv = str_replace( ']]', '', $rv );
337 * @param $type String
339 * @param $title Title
340 * @param $params Array
343 protected static function getTitleLink( $type, $skin, $title, &$params ) {
344 global $wgLang, $wgContLang, $wgUserrightsInterwikiDelimiter;
346 return $title->getPrefixedText();
350 $titleLink = Linker
::link(
352 htmlspecialchars( $title->getPrefixedText() ),
354 array( 'redirect' => 'no' )
356 $targetTitle = Title
::newFromText( $params[0] );
357 if ( !$targetTitle ) {
358 # Workaround for broken database
359 $params[0] = htmlspecialchars( $params[0] );
361 $params[0] = Linker
::link(
363 htmlspecialchars( $params[0] )
368 if( substr( $title->getText(), 0, 1 ) == '#' ) {
369 $titleLink = $title->getText();
371 // TODO: Store the user identifier in the parameters
372 // to make this faster for future log entries
373 $id = User
::idFromName( $title->getText() );
374 $titleLink = Linker
::userLink( $id, $title->getText() )
375 . Linker
::userToolLinks( $id, $title->getText(), false, Linker
::TOOL_LINKS_NOBLOCK
);
379 $text = $wgContLang->ucfirst( $title->getText() );
380 $parts = explode( $wgUserrightsInterwikiDelimiter, $text, 2 );
381 if ( count( $parts ) == 2 ) {
382 $titleLink = WikiMap
::foreignUserLink( $parts[1], $parts[0],
383 htmlspecialchars( $title->getPrefixedText() ) );
384 if ( $titleLink !== false ) {
388 $titleLink = Linker
::link( Title
::makeTitle( NS_USER
, $text ) );
391 $titleLink = Linker
::link(
393 $title->getPrefixedText(),
395 array( 'redirect' => 'no' )
397 $params[0] = Linker
::link(
398 Title
::newFromText( $params[0] ),
399 htmlspecialchars( $params[0] )
401 $params[1] = $wgLang->timeanddate( $params[1] );
404 if( $title->getNamespace() == NS_SPECIAL
) {
405 list( $name, $par ) = SpecialPageFactory
::resolveAlias( $title->getDBkey() );
406 # Use the language name for log titles, rather than Log/X
407 if( $name == 'Log' ) {
408 $titleLink = '(' . Linker
::link( $title, LogPage
::logName( $par ) ) . ')';
410 $titleLink = Linker
::link( $title );
413 $titleLink = Linker
::link( $title );
422 * @param $action String: one of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'
423 * @param $target Title object
424 * @param $comment String: description associated
425 * @param $params Array: parameters passed later to wfMsg.* functions
426 * @param $doer User object: the user doing the action
428 public function addEntry( $action, $target, $comment, $params = array(), $doer = null ) {
429 if ( !is_array( $params ) ) {
430 $params = array( $params );
433 if ( $comment === null ) {
437 $this->action
= $action;
438 $this->target
= $target;
439 $this->comment
= $comment;
440 $this->params
= LogPage
::makeParamBlob( $params );
442 if ( $doer === null ) {
445 } elseif ( !is_object( $doer ) ) {
446 $doer = User
::newFromId( $doer );
451 $this->actionText
= LogPage
::actionText( $this->type
, $action, $target, null, $params );
453 return $this->saveContent();
457 * Add relations to log_search table
459 * @param $field String
460 * @param $values Array
461 * @param $logid Integer
464 public function addRelations( $field, $values, $logid ) {
465 if( !strlen( $field ) ||
empty( $values ) ) {
466 return false; // nothing
469 foreach( $values as $value ) {
471 'ls_field' => $field,
472 'ls_value' => $value,
473 'ls_log_id' => $logid
476 $dbw = wfGetDB( DB_MASTER
);
477 $dbw->insert( 'log_search', $data, __METHOD__
, 'IGNORE' );
482 * Create a blob from a parameter array
484 * @param $params Array
487 public static function makeParamBlob( $params ) {
488 return implode( "\n", $params );
492 * Extract a parameter array from a blob
494 * @param $blob String
497 public static function extractParams( $blob ) {
498 if ( $blob === '' ) {
501 return explode( "\n", $blob );
506 * Convert a comma-delimited list of block log flags
507 * into a more readable (and translated) form
509 * @param $flags Flags to format
510 * @param $forContent Whether to localize the message depending of the user
514 public static function formatBlockFlags( $flags, $forContent = false ) {
517 $flags = explode( ',', trim( $flags ) );
518 if( count( $flags ) > 0 ) {
519 for( $i = 0; $i < count( $flags ); $i++
) {
520 $flags[$i] = self
::formatBlockFlag( $flags[$i], $forContent );
522 return '(' . $wgLang->commaList( $flags ) . ')';
529 * Translate a block log flag if possible
531 * @param $flag int Flag to translate
532 * @param $forContent Whether to localize the message depending of the user
536 public static function formatBlockFlag( $flag, $forContent = false ) {
537 static $messages = array();
538 if( !isset( $messages[$flag] ) ) {
539 $messages[$flag] = htmlspecialchars( $flag ); // Fallback
541 $msg = wfMessage( 'block-log-flags-' . $flag );
543 $msg->inContentLanguage();
545 if ( $msg->exists() ) {
546 $messages[$flag] = $msg->escaped();
549 return $messages[$flag];
554 * Aliases for backwards compatibility with 1.6
556 define( 'MW_LOG_DELETED_ACTION', LogPage
::DELETED_ACTION
);
557 define( 'MW_LOG_DELETED_USER', LogPage
::DELETED_USER
);
558 define( 'MW_LOG_DELETED_COMMENT', LogPage
::DELETED_COMMENT
);
559 define( 'MW_LOG_DELETED_RESTRICTED', LogPage
::DELETED_RESTRICTED
);