MediaWiki REL1_37
LogPage.php
Go to the documentation of this file.
1<?php
28
38class LogPage {
39 public const DELETED_ACTION = 1;
40 public const DELETED_COMMENT = 2;
41 public const DELETED_USER = 4;
42 public const DELETED_RESTRICTED = 8;
43
44 // Convenience fields
45 public const SUPPRESSED_USER = self::DELETED_USER | self::DELETED_RESTRICTED;
46 public const SUPPRESSED_ACTION = self::DELETED_ACTION | self::DELETED_RESTRICTED;
47
50
52 public $sendToUDP;
53
56
58 private $actionText;
59
63 private $type;
64
68 private $action;
69
71 private $comment;
72
74 private $params;
75
77 private $performer;
78
80 private $target;
81
89 public function __construct( $type, $rc = true, $udp = 'skipUDP' ) {
90 $this->type = $type;
91 $this->updateRecentChanges = $rc;
92 $this->sendToUDP = ( $udp == 'UDP' );
93 }
94
98 protected function saveContent() {
99 global $wgLogRestrictions;
100
101 $dbw = wfGetDB( DB_PRIMARY );
102
103 $now = wfTimestampNow();
104 $actorId = MediaWikiServices::getInstance()->getActorNormalization()
105 ->acquireActorId( $this->performer, $dbw );
106 $data = [
107 'log_type' => $this->type,
108 'log_action' => $this->action,
109 'log_timestamp' => $dbw->timestamp( $now ),
110 'log_actor' => $actorId,
111 'log_namespace' => $this->target->getNamespace(),
112 'log_title' => $this->target->getDBkey(),
113 'log_page' => $this->target->getArticleID(),
114 'log_params' => $this->params
115 ];
116 $data += MediaWikiServices::getInstance()->getCommentStore()->insert(
117 $dbw,
118 'log_comment',
119 $this->comment
120 );
121 $dbw->insert( 'logging', $data, __METHOD__ );
122 $newId = $dbw->insertId();
123
124 # And update recentchanges
125 if ( $this->updateRecentChanges ) {
126 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
127
128 RecentChange::notifyLog(
129 $now, $titleObj, $this->performer, $this->getRcComment(), '',
130 $this->type, $this->action, $this->target, $this->comment,
131 $this->params, $newId, $this->getRcCommentIRC()
132 );
133 } elseif ( $this->sendToUDP ) {
134 # Don't send private logs to UDP
135 if ( isset( $wgLogRestrictions[$this->type] ) && $wgLogRestrictions[$this->type] != '*' ) {
136 return $newId;
137 }
138
139 # Notify external application via UDP.
140 # We send this to IRC but do not want to add it the RC table.
141 $titleObj = SpecialPage::getTitleFor( 'Log', $this->type );
142 $rc = RecentChange::newLogEntry(
143 $now, $titleObj, $this->performer, $this->getRcComment(), '',
144 $this->type, $this->action, $this->target, $this->comment,
145 $this->params, $newId, $this->getRcCommentIRC()
146 );
147 $rc->notifyRCFeeds();
148 }
149
150 return $newId;
151 }
152
158 public function getRcComment() {
159 $rcComment = $this->actionText;
160
161 if ( $this->comment != '' ) {
162 if ( $rcComment == '' ) {
163 $rcComment = $this->comment;
164 } else {
165 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
167 }
168 }
169
170 return $rcComment;
171 }
172
178 public function getRcCommentIRC() {
179 $rcComment = $this->ircActionText;
180
181 if ( $this->comment != '' ) {
182 if ( $rcComment == '' ) {
183 $rcComment = $this->comment;
184 } else {
185 $rcComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() .
187 }
188 }
189
190 return $rcComment;
191 }
192
197 public function getComment() {
198 return $this->comment;
199 }
200
206 public static function validTypes() {
207 global $wgLogTypes;
208
209 return $wgLogTypes;
210 }
211
218 public static function isLogType( $type ) {
219 return in_array( $type, self::validTypes() );
220 }
221
235 public static function actionText( $type, $action, $title = null, $skin = null,
236 $params = [], $filterWikilinks = false
237 ) {
238 global $wgLang, $wgLogActions;
239
240 $key = "$type/$action";
241
242 if ( isset( $wgLogActions[$key] ) ) {
243 if ( $skin === null ) {
244 $langObj = MediaWikiServices::getInstance()->getContentLanguage();
245 $langObjOrNull = null;
246 } else {
247 // TODO Is $skin->getLanguage() safe here?
249 $langObj = $wgLang;
250 $langObjOrNull = $wgLang;
251 }
252 if ( $title === null ) {
253 $rv = wfMessage( $wgLogActions[$key] )->inLanguage( $langObj )->escaped();
254 } else {
255 $titleLink = self::getTitleLink( $title, $langObjOrNull );
256
257 if ( count( $params ) == 0 ) {
258 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $titleLink )
259 ->inLanguage( $langObj )->escaped();
260 } else {
261 array_unshift( $params, $titleLink );
262
263 $rv = wfMessage( $wgLogActions[$key] )->rawParams( $params )
264 ->inLanguage( $langObj )->escaped();
265 }
266 }
267 } else {
269
270 if ( isset( $wgLogActionsHandlers[$key] ) ) {
271 $args = func_get_args();
272 // @phan-suppress-next-line PhanTypeMismatchArgumentInternal
273 $rv = call_user_func_array( $wgLogActionsHandlers[$key], $args );
274 } else {
275 wfDebug( "LogPage::actionText - unknown action $key" );
276 $rv = "$action";
277 }
278 }
279
280 // For the perplexed, this feature was added in r7855 by Erik.
281 // The feature was added because we liked adding [[$1]] in our log entries
282 // but the log entries are parsed as Wikitext on RecentChanges but as HTML
283 // on Special:Log. The hack is essentially that [[$1]] represented a link
284 // to the title in question. The first parameter to the HTML version (Special:Log)
285 // is that link in HTML form, and so this just gets rid of the ugly [[]].
286 // However, this is a horrible hack and it doesn't work like you expect if, say,
287 // you want to link to something OTHER than the title of the log entry.
288 // The real problem, which Erik was trying to fix (and it sort-of works now) is
289 // that the same messages are being treated as both wikitext *and* HTML.
290 if ( $filterWikilinks ) {
291 $rv = str_replace( '[[', '', $rv );
292 $rv = str_replace( ']]', '', $rv );
293 }
294
295 return $rv;
296 }
297
303 private static function getTitleLink( Title $title, ?Language $lang ): string {
304 if ( !$lang ) {
305 return $title->getPrefixedText();
306 }
307
308 $services = MediaWikiServices::getInstance();
309 $linkRenderer = $services->getLinkRenderer();
310
311 if ( $title->isSpecialPage() ) {
312 [ $name, $par ] = $services->getSpecialPageFactory()->resolveAlias( $title->getDBkey() );
313
314 if ( $name === 'Log' ) {
315 $logPage = new LogPage( $par );
316 return wfMessage( 'parentheses' )
317 ->rawParams( $linkRenderer->makeLink( $title, $logPage->getName()->text() ) )
318 ->inLanguage( $lang )
319 ->escaped();
320 }
321 }
322
323 return $linkRenderer->makeLink( $title );
324 }
325
339 public function addEntry( $action, $target, $comment, $params, $performer ) {
340 // FIXME $params is only documented to accept an array
341 if ( !is_array( $params ) ) {
342 $params = [ $params ];
343 }
344
345 if ( $comment === null ) {
346 $comment = '';
347 }
348
349 # Trim spaces on user supplied text
350 $comment = trim( $comment );
351
352 $this->action = $action;
353 $this->target = $target;
354 $this->comment = $comment;
355 $this->params = self::makeParamBlob( $params );
356
357 if ( !is_object( $performer ) ) {
358 $performer = User::newFromId( $performer );
359 }
360
361 $this->performer = $performer;
362
363 $logEntry = new ManualLogEntry( $this->type, $action );
364 $logEntry->setTarget( $target );
365 $logEntry->setPerformer( $performer );
366 $logEntry->setParameters( $params );
367 // All log entries using the LogPage to insert into the logging table
368 // are using the old logging system and therefore the legacy flag is
369 // needed to say the LogFormatter the parameters have numeric keys
370 $logEntry->setLegacy( true );
371
372 $formatter = LogFormatter::newFromEntry( $logEntry );
373 $context = RequestContext::newExtraneousContext( $target );
374 $formatter->setContext( $context );
375
376 $this->actionText = $formatter->getPlainActionText();
377 $this->ircActionText = $formatter->getIRCActionText();
378
379 return $this->saveContent();
380 }
381
390 public function addRelations( $field, $values, $logid ) {
391 if ( !strlen( $field ) || empty( $values ) ) {
392 return false;
393 }
394
395 $data = [];
396
397 foreach ( $values as $value ) {
398 $data[] = [
399 'ls_field' => $field,
400 'ls_value' => $value,
401 'ls_log_id' => $logid
402 ];
403 }
404
405 $dbw = wfGetDB( DB_PRIMARY );
406 $dbw->insert( 'log_search', $data, __METHOD__, [ 'IGNORE' ] );
407
408 return true;
409 }
410
417 public static function makeParamBlob( $params ) {
418 return implode( "\n", $params );
419 }
420
427 public static function extractParams( $blob ) {
428 if ( $blob === '' ) {
429 return [];
430 } else {
431 return explode( "\n", $blob );
432 }
433 }
434
440 public function getName() {
441 global $wgLogNames;
442
443 // BC
444 $key = $wgLogNames[$this->type] ?? 'log-name-' . $this->type;
445
446 return wfMessage( $key );
447 }
448
454 public function getDescription() {
455 global $wgLogHeaders;
456 // BC
457 $key = $wgLogHeaders[$this->type] ?? 'log-description-' . $this->type;
458
459 return wfMessage( $key );
460 }
461
467 public function getRestriction() {
468 global $wgLogRestrictions;
469 // The empty string fallback will
470 // always return true in permission check
471 return $wgLogRestrictions[$this->type] ?? '';
472 }
473
479 public function isRestricted() {
480 $restriction = $this->getRestriction();
481
482 return $restriction !== '' && $restriction !== '*';
483 }
484}
$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.
$wgLang
Definition Setup.php:831
if(ini_get('mbstring.func_overload')) if(!defined('MW_ENTRY_POINT'))
Pre-config setup: Before loading LocalSettings.php.
Definition Setup.php:88
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition Language.php:42
static newFromEntry(LogEntry $entry)
Constructs a new formatter suitable for given entry.
Class to simplify the use of log pages.
Definition LogPage.php:38
static getTitleLink(Title $title, ?Language $lang)
Definition LogPage.php:303
static actionText( $type, $action, $title=null, $skin=null, $params=[], $filterWikilinks=false)
Generate text for a log entry.
Definition LogPage.php:235
getRestriction()
Returns the right needed to read this log type.
Definition LogPage.php:467
const SUPPRESSED_USER
Definition LogPage.php:45
__construct( $type, $rc=true, $udp='skipUDP')
Definition LogPage.php:89
const DELETED_USER
Definition LogPage.php:41
static makeParamBlob( $params)
Create a blob from a parameter array.
Definition LogPage.php:417
const DELETED_RESTRICTED
Definition LogPage.php:42
string $comment
Comment associated with action.
Definition LogPage.php:71
bool $sendToUDP
Definition LogPage.php:52
string $ircActionText
Plaintext version of the message for IRC.
Definition LogPage.php:55
UserIdentity $performer
The user doing the action.
Definition LogPage.php:77
string $action
One of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move', 'move_redir'.
Definition LogPage.php:68
isRestricted()
Tells if this log is not viewable by all.
Definition LogPage.php:479
static extractParams( $blob)
Extract a parameter array from a blob.
Definition LogPage.php:427
static isLogType( $type)
Is $type a valid log type.
Definition LogPage.php:218
getName()
Name of the log.
Definition LogPage.php:440
string $actionText
Plaintext version of the message.
Definition LogPage.php:58
saveContent()
Definition LogPage.php:98
const DELETED_COMMENT
Definition LogPage.php:40
getComment()
Get the comment from the last addEntry() call.
Definition LogPage.php:197
bool $updateRecentChanges
Definition LogPage.php:49
getRcComment()
Get the RC comment from the last addEntry() call.
Definition LogPage.php:158
string $type
One of '', 'block', 'protect', 'rights', 'delete', 'upload', 'move'.
Definition LogPage.php:63
addRelations( $field, $values, $logid)
Add relations to log_search table.
Definition LogPage.php:390
static validTypes()
Get the list of valid log types.
Definition LogPage.php:206
getRcCommentIRC()
Get the RC comment from the last addEntry() call for IRC.
Definition LogPage.php:178
Title $target
Definition LogPage.php:80
string $params
Blob made of a parameters array.
Definition LogPage.php:74
getDescription()
Description of this log type.
Definition LogPage.php:454
const DELETED_ACTION
Definition LogPage.php:39
const SUPPRESSED_ACTION
Definition LogPage.php:46
addEntry( $action, $target, $comment, $params, $performer)
Add a log entry.
Definition LogPage.php:339
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:48
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition User.php:648
Interface for objects representing user identity.
if( $line===false) $args
Definition mcc.php:124
const DB_PRIMARY
Definition defines.php:27
if(!isset( $args[0])) $lang