MediaWiki REL1_35
ManualLogEntry.php
Go to the documentation of this file.
1<?php
29use Wikimedia\Assert\Assert;
30use Wikimedia\IPUtils;
32
42class ManualLogEntry extends LogEntryBase implements Taggable {
44 protected $type;
45
47 protected $subtype;
48
50 protected $parameters = [];
51
53 protected $relations = [];
54
56 protected $performer;
57
59 protected $target;
60
62 protected $timestamp;
63
65 protected $comment = '';
66
68 protected $revId = 0;
69
71 protected $tags = [];
72
74 protected $deleted;
75
77 protected $id;
78
80 protected $isPatrollable = false;
81
83 protected $legacy = false;
84
91 public function __construct( $type, $subtype ) {
92 $this->type = $type;
93 $this->subtype = $subtype;
94 }
95
117 public function setParameters( $parameters ) {
118 $this->parameters = $parameters;
119 }
120
128 public function setRelations( array $relations ) {
129 $this->relations = $relations;
130 }
131
138 public function setPerformer( UserIdentity $performer ) {
139 $this->performer = User::newFromIdentity( $performer );
140 }
141
148 public function setTarget( LinkTarget $target ) {
149 $this->target = Title::newFromLinkTarget( $target );
150 }
151
158 public function setTimestamp( $timestamp ) {
159 $this->timestamp = $timestamp;
160 }
161
168 public function setComment( $comment ) {
169 $this->comment = (string)$comment;
170 }
171
181 public function setAssociatedRevId( $revId ) {
182 $this->revId = $revId;
183 }
184
195 public function setTags( $tags ) {
196 if ( $this->tags ) {
197 wfDebug( 'Overwriting existing ManualLogEntry tags' );
198 }
199 $this->tags = [];
200 $this->addTags( $tags );
201 }
202
209 public function addTags( $tags ) {
210 if ( $tags === null ) {
211 return;
212 }
213
214 if ( is_string( $tags ) ) {
215 $tags = [ $tags ];
216 }
217 Assert::parameterElementType( 'string', $tags, 'tags' );
218 $this->tags = array_unique( array_merge( $this->tags, $tags ) );
219 }
220
230 public function setIsPatrollable( $patrollable ) {
231 $this->isPatrollable = (bool)$patrollable;
232 }
233
240 public function setLegacy( $legacy ) {
241 $this->legacy = $legacy;
242 }
243
250 public function setDeleted( $deleted ) {
251 $this->deleted = $deleted;
252 }
253
261 public function insert( IDatabase $dbw = null ) {
262 $dbw = $dbw ?: wfGetDB( DB_MASTER );
263
264 if ( $this->timestamp === null ) {
265 $this->timestamp = wfTimestampNow();
266 }
267
268 // Trim spaces on user supplied text
269 $comment = trim( $this->getComment() );
270
271 $params = $this->getParameters();
272 $relations = $this->relations;
273
274 // Additional fields for which there's no space in the database table schema
275 $revId = $this->getAssociatedRevId();
276 if ( $revId ) {
277 $params['associated_rev_id'] = $revId;
278 $relations['associated_rev_id'] = $revId;
279 }
280
281 $data = [
282 'log_type' => $this->getType(),
283 'log_action' => $this->getSubtype(),
284 'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
285 'log_namespace' => $this->getTarget()->getNamespace(),
286 'log_title' => $this->getTarget()->getDBkey(),
287 'log_page' => $this->getTarget()->getArticleID(),
288 'log_params' => LogEntryBase::makeParamBlob( $params ),
289 ];
290 if ( isset( $this->deleted ) ) {
291 $data['log_deleted'] = $this->deleted;
292 }
293 $data += CommentStore::getStore()->insert( $dbw, 'log_comment', $comment );
294 $data += ActorMigration::newMigration()
295 ->getInsertValues( $dbw, 'log_user', $this->getPerformer() );
296
297 $dbw->insert( 'logging', $data, __METHOD__ );
298 $this->id = $dbw->insertId();
299
300 $rows = [];
301 foreach ( $relations as $tag => $values ) {
302 if ( !strlen( $tag ) ) {
303 throw new MWException( "Got empty log search tag." );
304 }
305
306 if ( !is_array( $values ) ) {
307 $values = [ $values ];
308 }
309
310 foreach ( $values as $value ) {
311 $rows[] = [
312 'ls_field' => $tag,
313 'ls_value' => $value,
314 'ls_log_id' => $this->id
315 ];
316 }
317 }
318 if ( count( $rows ) ) {
319 $dbw->insert( 'log_search', $rows, __METHOD__, [ 'IGNORE' ] );
320 }
321
322 return $this->id;
323 }
324
332 public function getRecentChange( $newId = 0 ) {
333 $formatter = LogFormatter::newFromEntry( $this );
334 $context = RequestContext::newExtraneousContext( $this->getTarget() );
335 $formatter->setContext( $context );
336
337 $logpage = SpecialPage::getTitleFor( 'Log', $this->getType() );
338 $user = $this->getPerformer();
339 $ip = "";
340 if ( $user->isAnon() ) {
341 // "MediaWiki default" and friends may have
342 // no IP address in their name
343 if ( IPUtils::isIPAddress( $user->getName() ) ) {
344 $ip = $user->getName();
345 }
346 }
347
348 return RecentChange::newLogEntry(
349 $this->getTimestamp(),
350 $logpage,
351 $user,
352 $formatter->getPlainActionText(),
353 $ip,
354 $this->getType(),
355 $this->getSubtype(),
356 $this->getTarget(),
357 $this->getComment(),
358 LogEntryBase::makeParamBlob( $this->getParameters() ),
359 $newId,
360 $formatter->getIRCActionComment(), // Used for IRC feeds
361 $this->getAssociatedRevId(), // Used for e.g. moves and uploads
362 $this->getIsPatrollable()
363 );
364 }
365
372 public function publish( $newId, $to = 'rcandudp' ) {
373 $canAddTags = true;
374 // FIXME: this code should be removed once all callers properly call publish()
375 if ( $to === 'udp' && !$newId && !$this->getAssociatedRevId() ) {
376 \MediaWiki\Logger\LoggerFactory::getInstance( 'logging' )->warning(
377 'newId and/or revId must be set when calling ManualLogEntry::publish()',
378 [
379 'newId' => $newId,
380 'to' => $to,
381 'revId' => $this->getAssociatedRevId(),
382 // pass a new exception to register the stack trace
383 'exception' => new RuntimeException()
384 ]
385 );
386 $canAddTags = false;
387 }
388
389 DeferredUpdates::addCallableUpdate(
390 function () use ( $newId, $to, $canAddTags ) {
391 $log = new LogPage( $this->getType() );
392 if ( !$log->isRestricted() ) {
393 Hooks::runner()->onManualLogEntryBeforePublish( $this );
394 $rc = $this->getRecentChange( $newId );
395
396 if ( $to === 'rc' || $to === 'rcandudp' ) {
397 // save RC, passing tags so they are applied there
398 $rc->addTags( $this->getTags() );
399 $rc->save( $rc::SEND_NONE );
400 } else {
401 $tags = $this->getTags();
402 if ( $tags && $canAddTags ) {
403 $revId = $this->getAssociatedRevId();
405 $tags,
406 null,
407 $revId > 0 ? $revId : null,
408 $newId > 0 ? $newId : null
409 );
410 }
411 }
412
413 if ( $to === 'udp' || $to === 'rcandudp' ) {
414 $rc->notifyRCFeeds();
415 }
416 }
417 },
418 DeferredUpdates::POSTSEND,
420 );
421 }
422
426 public function getType() {
427 return $this->type;
428 }
429
433 public function getSubtype() {
434 return $this->subtype;
435 }
436
440 public function getParameters() {
441 return $this->parameters;
442 }
443
447 public function getPerformer() {
448 return $this->performer;
449 }
450
454 public function getTarget() {
455 return $this->target;
456 }
457
461 public function getTimestamp() {
462 $ts = $this->timestamp ?? wfTimestampNow();
463
464 return wfTimestamp( TS_MW, $ts );
465 }
466
470 public function getComment() {
471 return $this->comment;
472 }
473
478 public function getAssociatedRevId() {
479 return $this->revId;
480 }
481
486 public function getTags() {
487 return $this->tags;
488 }
489
496 public function getIsPatrollable() {
497 return $this->isPatrollable;
498 }
499
504 public function isLegacy() {
505 return $this->legacy;
506 }
507
511 public function getDeleted() {
512 return (int)$this->deleted;
513 }
514}
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.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
static addTags( $tags, $rc_id=null, $rev_id=null, $log_id=null, $params=null, RecentChange $rc=null)
Add tags to a change given its rc_id, rev_id and/or log_id.
Extends the LogEntry Interface with some basic functionality.
static makeParamBlob( $params)
Create a blob from a parameter array.
static newFromEntry(LogEntry $entry)
Constructs a new formatter suitable for given entry.
Class to simplify the use of log pages.
Definition LogPage.php:37
MediaWiki exception.
Class for creating new log entries and inserting them into the database.
getIsPatrollable()
Whether this log entry is patrollable.
setDeleted( $deleted)
Set the 'deleted' flag.
string $subtype
Sub type of log entry.
addTags( $tags)
Add change tags for the log entry.
int $id
ID of the log entry.
string[] $tags
Change tags add to the log entry.
string $type
Type of log entry.
setAssociatedRevId( $revId)
Set an associated revision id.
insert(IDatabase $dbw=null)
Insert the entry into the logging table.
string $comment
Comment for the log entry.
setTimestamp( $timestamp)
Set the timestamp of when the logged action took place.
array $parameters
Parameters for log entry.
int $revId
A rev id associated to the log entry.
setComment( $comment)
Set a comment associated with the action being logged.
setParameters( $parameters)
Set extra log parameters.
bool $isPatrollable
Can this log entry be patrolled?
Title $target
Target title for the log entry.
int $deleted
Deletion state of the log entry.
setLegacy( $legacy)
Set the 'legacy' flag.
setIsPatrollable( $patrollable)
Set whether this log entry should be made patrollable This shouldn't depend on config,...
bool $legacy
Whether this is a legacy log entry.
string $timestamp
Timestamp of creation of the log entry.
setPerformer(UserIdentity $performer)
Set the user that performed the action being logged.
getRecentChange( $newId=0)
Get a RecentChanges object for the log entry.
User $performer
Performer of the action for the log entry.
setRelations(array $relations)
Declare arbitrary tag/value relations to this log entry.
__construct( $type, $subtype)
Stable to call.
setTags( $tags)
Set change tags for the log entry.
setTarget(LinkTarget $target)
Set the title of the object changed.
publish( $newId, $to='rcandudp')
Publish the log entry.
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,...
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 newFromIdentity(UserIdentity $identity)
Returns a User object corresponding to the given UserIdentity.
Definition User.php:597
Interface that defines how to tag objects.
Definition Taggable.php:32
Interface for objects representing user identity.
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:38
const DB_MASTER
Definition defines.php:29