MediaWiki REL1_35
LogPage.php
Go to the documentation of this file.
1<?php
27
37class LogPage {
38 public const DELETED_ACTION = 1;
39 public const DELETED_COMMENT = 2;
40 public const DELETED_USER = 4;
41 public const DELETED_RESTRICTED = 8;
42
43 // Convenience fields
44 public const SUPPRESSED_USER = self::DELETED_USER | self::DELETED_RESTRICTED;
45 public const SUPPRESSED_ACTION = self::DELETED_ACTION | self::DELETED_RESTRICTED;
46
49
51 public $sendToUDP;
52
55
57 private $actionText;
58
62 private $type;
63
67 private $action;
68
70 private $comment;
71
73 private $params;
74
76 private $doer;
77
79 private $target;
80
88 public function __construct( $type, $rc = true, $udp = 'skipUDP' ) {
89 $this->type = $type;
90 $this->updateRecentChanges = $rc;
91 $this->sendToUDP = ( $udp == 'UDP' );
92 }
93
97 protected function saveContent() {
98 global $wgLogRestrictions;
99
100 $dbw = wfGetDB( DB_MASTER );
101
102 $now = wfTimestampNow();
103 $data = [
104 'log_type' => $this->type,
105 'log_action' => $this->action,
106 'log_timestamp' => $dbw->timestamp( $now ),
107 'log_namespace' => $this->target->getNamespace(),
108 'log_title' => $this->target->getDBkey(),
109 'log_page' => $this->target->getArticleID(),
110 'log_params' => $this->params
111 ];
112 $data += MediaWikiServices::getInstance()->getCommentStore()->insert(
113 $dbw,
114 'log_comment',
115 $this->comment
116 );
117 $data += ActorMigration::newMigration()->getInsertValues( $dbw, 'log_user', $this->doer );
118 $dbw->insert( 'logging', $data, __METHOD__ );
119 $newId = $dbw->insertId();
120
121 # And update recentchanges
122 if ( $this->updateRecentChanges ) {
123 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
124
125 RecentChange::notifyLog(
126 $now, $titleObj, $this->doer, $this->getRcComment(), '',
127 $this->type, $this->action, $this->target, $this->comment,
128 $this->params, $newId, $this->getRcCommentIRC()
129 );
130 } elseif ( $this->sendToUDP ) {
131 # Don't send private logs to UDP
132 if ( isset( $wgLogRestrictions[$this->type] ) && $wgLogRestrictions[$this->type] != '*' ) {
133 return $newId;
134 }
135
136 # Notify external application via UDP.
137 # We send this to IRC but do not want to add it the RC table.
138 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
139 $rc = RecentChange::newLogEntry(
140 $now, $titleObj, $this->doer, $this->getRcComment(), '',
141 $this->type, $this->action, $this->target, $this->comment,
142 $this->params, $newId, $this->getRcCommentIRC()
143 );
144 $rc->notifyRCFeeds();
145 }
146
147 return $newId;
148 }
149
155 public function getRcComment() {
156 $rcComment = $this->actionText;
157
158 if ( $this->comment != '' ) {
159 if ( $rcComment == '' ) {
160 $rcComment = $this->comment;
161 } else {
162 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
164 }
165 }
166
167 return $rcComment;
168 }
169
175 public function getRcCommentIRC() {
176 $rcComment = $this->ircActionText;
177
178 if ( $this->comment != '' ) {
179 if ( $rcComment == '' ) {
180 $rcComment = $this->comment;
181 } else {
182 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
184 }
185 }
186
187 return $rcComment;
188 }
189
194 public function getComment() {
195 return $this->comment;
196 }
197
203 public static function validTypes() {
204 global $wgLogTypes;
205
206 return $wgLogTypes;
207 }
208
215 public static function isLogType( $type ) {
216 return in_array( $type, self::validTypes() );
217 }
218
232 public static function actionText( $type, $action, $title = null, $skin = null,
233 $params = [], $filterWikilinks = false
234 ) {
235 global $wgLang, $wgLogActions;
236
237 $key = "$type/$action";
238
239 if ( isset( $wgLogActions[$key] ) ) {
240 if ( $skin === null ) {
241 $langObj = MediaWikiServices::getInstance()->getContentLanguage();
242 $langObjOrNull = null;
243 } else {
244 // TODO Is $skin->getLanguage() safe here?
246 $langObj = $wgLang;
247 $langObjOrNull = $wgLang;
248 }
249 if ( $title === null ) {
250 $rv = wfMessage( $wgLogActions[$key] )->inLanguage( $langObj )->escaped();
251 } else {
252 $titleLink = self::getTitleLink( $title, $langObjOrNull );
253
254 if ( count( $params ) == 0 ) {
255 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $titleLink )
256 ->inLanguage( $langObj )->escaped();
257 } else {
258 array_unshift( $params, $titleLink );
259
260 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $params )
261 ->inLanguage( $langObj )->escaped();
262 }
263 }
264 } else {
266
267 if ( isset( $wgLogActionsHandlers[$key] ) ) {
268 $args = func_get_args();
269 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
270 } else {
271 wfDebug( "LogPage::actionText - unknown action $key" );
272 $rv = "$action";
273 }
274 }
275
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 );
289 }
290
291 return $rv;
292 }
293
299 private static function getTitleLink( Title $title, ?Language $lang ) : string {
300 if ( !$lang ) {
301 return $title->getPrefixedText();
302 }
303
304 $services = MediaWikiServices::getInstance();
305 $linkRenderer = $services->getLinkRenderer();
306
307 if ( $title->isSpecialPage() ) {
308 [ $name, $par ] = $services->getSpecialPageFactory()->resolveAlias( $title->getDBkey() );
309
310 if ( $name === 'Log' ) {
311 $logPage = new LogPage( $par );
312 return wfMessage( 'parentheses' )
313 ->rawParams( $linkRenderer->makeLink( $title, $logPage->getName()->text() ) )
314 ->inLanguage( $lang )
315 ->escaped();
316 }
317 }
318
319 return $linkRenderer->makeLink( $title );
320 }
321
335 public function addEntry( $action, $target, $comment, $params = [], $doer = null ) {
336 if ( !is_array( $params ) ) {
337 $params = [ $params ];
338 }
339
340 if ( $comment === null ) {
341 $comment = '';
342 }
343
344 # Trim spaces on user supplied text
345 $comment = trim( $comment );
346
347 $this->action = $action;
348 $this->target = $target;
349 $this->comment = $comment;
350 $this->params = self::makeParamBlob( $params );
351
352 if ( $doer === null ) {
353 wfDeprecated( __METHOD__ . ' without passing a $user parameter', '1.35' );
354 global $wgUser;
355 $doer = $wgUser;
356 } elseif ( !is_object( $doer ) ) {
357 $doer = User::newFromId( $doer );
358 }
359
360 $this->doer = $doer;
361
362 $logEntry = new ManualLogEntry( $this->type, $action );
363 $logEntry->setTarget( $target );
364 $logEntry->setPerformer( $doer );
365 $logEntry->setParameters( $params );
366 // All log entries using the LogPage to insert into the logging table
367 // are using the old logging system and therefore the legacy flag is
368 // needed to say the LogFormatter the parameters have numeric keys
369 $logEntry->setLegacy( true );
370
371 $formatter = LogFormatter::newFromEntry( $logEntry );
372 $context = RequestContext::newExtraneousContext( $target );
373 $formatter->setContext( $context );
374
375 $this->actionText = $formatter->getPlainActionText();
376 $this->ircActionText = $formatter->getIRCActionText();
377
378 return $this->saveContent();
379 }
380
389 public function addRelations( $field, $values, $logid ) {
390 if ( !strlen( $field ) || empty( $values ) ) {
391 return false;
392 }
393
394 $data = [];
395
396 foreach ( $values as $value ) {
397 $data[] = [
398 'ls_field' => $field,
399 'ls_value' => $value,
400 'ls_log_id' => $logid
401 ];
402 }
403
404 $dbw = wfGetDB( DB_MASTER );
405 $dbw->insert( 'log_search', $data, __METHOD__, [ 'IGNORE' ] );
406
407 return true;
408 }
409
416 public static function makeParamBlob( $params ) {
417 return implode( "\n", $params );
418 }
419
426 public static function extractParams( $blob ) {
427 if ( $blob === '' ) {
428 return [];
429 } else {
430 return explode( "\n", $blob );
431 }
432 }
433
439 public function getName() {
440 global $wgLogNames;
441
442 // BC
443 $key = $wgLogNames[$this->type] ?? 'log-name-' . $this->type;
444
445 return wfMessage( $key );
446 }
447
453 public function getDescription() {
454 global $wgLogHeaders;
455 // BC
456 $key = $wgLogHeaders[$this->type] ?? 'log-description-' . $this->type;
457
458 return wfMessage( $key );
459 }
460
466 public function getRestriction() {
467 global $wgLogRestrictions;
468 // '' always returns true with $user->isAllowed()
469 return $wgLogRestrictions[$this->type] ?? '';
470 }
471
477 public function isRestricted() {
478 $restriction = $this->getRestriction();
479
480 return $restriction !== '' && $restriction !== '*';
481 }
482}
$wgLogActions
Lists the message key string for formatting individual events of each type and action when listed in ...
$wgLogNames
Lists the message key string for each log type.
$wgLogTypes
The logging system has two levels: an event type, which describes the general category and can be vie...
$wgLogRestrictions
This restricts log access to those who have a certain right Users without this will not see it in the...
$wgLogHeaders
Lists the message key string for descriptive text to be shown at the top of each log type.
$wgLogActionsHandlers
The same as above, but here values are names of classes, not messages.
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
$wgLang
Definition Setup.php:781
if(ini_get('mbstring.func_overload')) if(!defined('MW_ENTRY_POINT'))
Pre-config setup: Before loading LocalSettings.php.
Definition Setup.php:85
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition Language.php:41
static newFromEntry(LogEntry $entry)
Constructs a new formatter suitable for given entry.
Class to simplify the use of log pages.
Definition LogPage.php:37
static getTitleLink(Title $title, ?Language $lang)
Definition LogPage.php:299
static actionText( $type, $action, $title=null, $skin=null, $params=[], $filterWikilinks=false)
Generate text for a log entry.
Definition LogPage.php:232
getRestriction()
Returns the right needed to read this log type.
Definition LogPage.php:466
const SUPPRESSED_USER
Definition LogPage.php:44
__construct( $type, $rc=true, $udp='skipUDP')
Stable to call.
Definition LogPage.php:88
const DELETED_USER
Definition LogPage.php:40
static makeParamBlob( $params)
Create a blob from a parameter array.
Definition LogPage.php:416
const DELETED_RESTRICTED
Definition LogPage.php:41
string $comment
Comment associated with action.
Definition LogPage.php:70
bool $sendToUDP
Definition LogPage.php:51
string $ircActionText
Plaintext version of the message for IRC.
Definition LogPage.php:54
User $doer
The user doing the action.
Definition LogPage.php:76
string $action
One of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'.
Definition LogPage.php:67
isRestricted()
Tells if this log is not viewable by all.
Definition LogPage.php:477
static extractParams( $blob)
Extract a parameter array from a blob.
Definition LogPage.php:426
static isLogType( $type)
Is $type a valid log type.
Definition LogPage.php:215
getName()
Name of the log.
Definition LogPage.php:439
string $actionText
Plaintext version of the message.
Definition LogPage.php:57
addEntry( $action, $target, $comment, $params=[], $doer=null)
Add a log entry.
Definition LogPage.php:335
saveContent()
Definition LogPage.php:97
const DELETED_COMMENT
Definition LogPage.php:39
getComment()
Get the comment from the last addEntry() call.
Definition LogPage.php:194
bool $updateRecentChanges
Definition LogPage.php:48
getRcComment()
Get the RC comment from the last addEntry() call.
Definition LogPage.php:155
string $type
One of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move'.
Definition LogPage.php:62
addRelations( $field, $values, $logid)
Add relations to log_search table.
Definition LogPage.php:389
static validTypes()
Get the list of valid log types.
Definition LogPage.php:203
getRcCommentIRC()
Get the RC comment from the last addEntry() call for IRC.
Definition LogPage.php:175
Title $target
Definition LogPage.php:79
string $params
Blob made of a parameters array.
Definition LogPage.php:73
getDescription()
Description of this log type.
Definition LogPage.php:453
const DELETED_ACTION
Definition LogPage.php:38
const SUPPRESSED_ACTION
Definition LogPage.php:45
Class for creating new log entries and inserting them into the database.
MediaWikiServices is the service locator for the application scope of MediaWiki.
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
static unstub(&$obj)
Unstubs an object, if it is a stub object.
Represents a title within MediaWiki.
Definition Title.php:42
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:60
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition User.php:565
if( $line===false) $args
Definition mcc.php:124
const DB_MASTER
Definition defines.php:29
if(!isset( $args[0])) $lang