MediaWiki  master
ManualLogEntry.php
Go to the documentation of this file.
1 <?php
30 use Wikimedia\Assert\Assert;
31 use Wikimedia\IPUtils;
33 
44 class ManualLogEntry extends LogEntryBase implements Taggable {
46  protected $type;
47 
49  protected $subtype;
50 
52  protected $parameters = [];
53 
55  protected $relations = [];
56 
58  protected $performer;
59 
61  protected $target;
62 
64  protected $timestamp;
65 
67  protected $comment = '';
68 
70  protected $revId = 0;
71 
73  protected $tags = [];
74 
76  protected $deleted;
77 
79  protected $id;
80 
82  protected $isPatrollable = false;
83 
85  protected $legacy = false;
86 
95  public function __construct( $type, $subtype ) {
96  $this->type = $type;
97  $this->subtype = $subtype;
98  }
99 
130  public function setParameters( $parameters ) {
131  $this->parameters = $parameters;
132  }
133 
141  public function setRelations( array $relations ) {
142  $this->relations = $relations;
143  }
144 
151  public function setPerformer( UserIdentity $performer ) {
152  $this->performer = $performer;
153  }
154 
162  public function setTarget( $target ) {
163  if ( $target instanceof PageReference ) {
164  $this->target = Title::castFromPageReference( $target );
165  } elseif ( $target instanceof LinkTarget ) {
166  $this->target = Title::newFromLinkTarget( $target );
167  } else {
168  throw new InvalidArgumentException( "Invalid target provided" );
169  }
170  }
171 
178  public function setTimestamp( $timestamp ) {
179  $this->timestamp = $timestamp;
180  }
181 
188  public function setComment( $comment ) {
189  $this->comment = $comment;
190  }
191 
201  public function setAssociatedRevId( $revId ) {
202  $this->revId = $revId;
203  }
204 
215  public function setTags( $tags ) {
216  if ( $this->tags ) {
217  wfDebug( 'Overwriting existing ManualLogEntry tags' );
218  }
219  $this->tags = [];
220  $this->addTags( $tags );
221  }
222 
229  public function addTags( $tags ) {
230  if ( $tags === null ) {
231  return;
232  }
233 
234  if ( is_string( $tags ) ) {
235  $tags = [ $tags ];
236  }
237  Assert::parameterElementType( 'string', $tags, 'tags' );
238  $this->tags = array_unique( array_merge( $this->tags, $tags ) );
239  }
240 
250  public function setIsPatrollable( $patrollable ) {
251  $this->isPatrollable = (bool)$patrollable;
252  }
253 
260  public function setLegacy( $legacy ) {
261  $this->legacy = $legacy;
262  }
263 
270  public function setDeleted( $deleted ) {
271  $this->deleted = $deleted;
272  }
273 
281  public function insert( IDatabase $dbw = null ) {
282  $dbw = $dbw ?: wfGetDB( DB_PRIMARY );
283 
284  if ( $this->timestamp === null ) {
285  $this->timestamp = wfTimestampNow();
286  }
287 
288  $actorId = \MediaWiki\MediaWikiServices::getInstance()->getActorStore()
289  ->acquireActorId( $this->getPerformerIdentity(), $dbw );
290 
291  // Trim spaces on user supplied text
292  $comment = trim( $this->getComment() );
293 
294  $params = $this->getParameters();
296 
297  // Additional fields for which there's no space in the database table schema
298  $revId = $this->getAssociatedRevId();
299  if ( $revId ) {
300  $params['associated_rev_id'] = $revId;
301  $relations['associated_rev_id'] = $revId;
302  }
303 
304  $data = [
305  'log_type' => $this->getType(),
306  'log_action' => $this->getSubtype(),
307  'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
308  'log_actor' => $actorId,
309  'log_namespace' => $this->getTarget()->getNamespace(),
310  'log_title' => $this->getTarget()->getDBkey(),
311  'log_page' => $this->getTarget()->getArticleID(),
312  'log_params' => LogEntryBase::makeParamBlob( $params ),
313  ];
314  if ( isset( $this->deleted ) ) {
315  $data['log_deleted'] = $this->deleted;
316  }
317  $data += CommentStore::getStore()->insert( $dbw, 'log_comment', $comment );
318 
319  $dbw->insert( 'logging', $data, __METHOD__ );
320  $this->id = $dbw->insertId();
321 
322  $rows = [];
323  foreach ( $relations as $tag => $values ) {
324  if ( !strlen( $tag ) ) {
325  throw new MWException( "Got empty log search tag." );
326  }
327 
328  if ( !is_array( $values ) ) {
329  $values = [ $values ];
330  }
331 
332  foreach ( $values as $value ) {
333  $rows[] = [
334  'ls_field' => $tag,
335  'ls_value' => $value,
336  'ls_log_id' => $this->id
337  ];
338  }
339  }
340  if ( count( $rows ) ) {
341  $dbw->insert( 'log_search', $rows, __METHOD__, [ 'IGNORE' ] );
342  }
343 
344  return $this->id;
345  }
346 
354  public function getRecentChange( $newId = 0 ) {
355  $formatter = LogFormatter::newFromEntry( $this );
356  $context = RequestContext::newExtraneousContext( $this->getTarget() );
357  $formatter->setContext( $context );
358 
359  $logpage = SpecialPage::getTitleFor( 'Log', $this->getType() );
360  $user = $this->getPerformerIdentity();
361  $ip = "";
362  if ( !$user->isRegistered() ) {
363  // "MediaWiki default" and friends may have
364  // no IP address in their name
365  if ( IPUtils::isIPAddress( $user->getName() ) ) {
366  $ip = $user->getName();
367  }
368  }
369 
371  $this->getTimestamp(),
372  $logpage,
373  $user,
374  $formatter->getPlainActionText(),
375  $ip,
376  $this->getType(),
377  $this->getSubtype(),
378  $this->getTarget(),
379  $this->getComment(),
380  LogEntryBase::makeParamBlob( $this->getParameters() ),
381  $newId,
382  $formatter->getIRCActionComment(), // Used for IRC feeds
383  $this->getAssociatedRevId(), // Used for e.g. moves and uploads
384  $this->getIsPatrollable()
385  );
386  }
387 
394  public function publish( $newId, $to = 'rcandudp' ) {
395  $canAddTags = true;
396  // FIXME: this code should be removed once all callers properly call publish()
397  if ( $to === 'udp' && !$newId && !$this->getAssociatedRevId() ) {
398  \MediaWiki\Logger\LoggerFactory::getInstance( 'logging' )->warning(
399  'newId and/or revId must be set when calling ManualLogEntry::publish()',
400  [
401  'newId' => $newId,
402  'to' => $to,
403  'revId' => $this->getAssociatedRevId(),
404  // pass a new exception to register the stack trace
405  'exception' => new RuntimeException()
406  ]
407  );
408  $canAddTags = false;
409  }
410 
412  function () use ( $newId, $to, $canAddTags ) {
413  $log = new LogPage( $this->getType() );
414  if ( !$log->isRestricted() ) {
415  Hooks::runner()->onManualLogEntryBeforePublish( $this );
416  $rc = $this->getRecentChange( $newId );
417 
418  if ( $to === 'rc' || $to === 'rcandudp' ) {
419  // save RC, passing tags so they are applied there
420  $rc->addTags( $this->getTags() );
421  $rc->save( $rc::SEND_NONE );
422  } else {
423  $tags = $this->getTags();
424  if ( $tags && $canAddTags ) {
425  $revId = $this->getAssociatedRevId();
427  $tags,
428  null,
429  $revId > 0 ? $revId : null,
430  $newId > 0 ? $newId : null
431  );
432  }
433  }
434 
435  if ( $to === 'udp' || $to === 'rcandudp' ) {
436  $rc->notifyRCFeeds();
437  }
438  }
439  },
440  DeferredUpdates::POSTSEND,
442  );
443  }
444 
448  public function getType() {
449  return $this->type;
450  }
451 
455  public function getSubtype() {
456  return $this->subtype;
457  }
458 
462  public function getParameters() {
463  return $this->parameters;
464  }
465 
469  public function getPerformerIdentity(): UserIdentity {
470  return $this->performer;
471  }
472 
476  public function getTarget() {
477  return $this->target;
478  }
479 
483  public function getTimestamp() {
484  $ts = $this->timestamp ?? wfTimestampNow();
485 
486  return wfTimestamp( TS_MW, $ts );
487  }
488 
492  public function getComment() {
493  return $this->comment;
494  }
495 
500  public function getAssociatedRevId() {
501  return $this->revId;
502  }
503 
508  public function getTags() {
509  return $this->tags;
510  }
511 
518  public function getIsPatrollable() {
519  return $this->isPatrollable;
520  }
521 
526  public function isLegacy() {
527  return $this->legacy;
528  }
529 
533  public function getDeleted() {
534  return (int)$this->deleted;
535  }
536 }
ManualLogEntry\setTimestamp
setTimestamp( $timestamp)
Set the timestamp of when the logged action took place.
Definition: ManualLogEntry.php:178
ManualLogEntry\__construct
__construct( $type, $subtype)
Definition: ManualLogEntry.php:95
ManualLogEntry\getTarget
getTarget()
Definition: ManualLogEntry.php:476
RecentChange\newLogEntry
static newLogEntry( $timestamp, $logPage, $user, $actionComment, $ip, $type, $action, $target, $logComment, $params, $newId=0, $actionCommentIRC='', $revId=0, $isPatrollable=false)
Definition: RecentChange.php:921
ManualLogEntry\$deleted
int $deleted
Deletion state of the log entry.
Definition: ManualLogEntry.php:76
ManualLogEntry\getType
getType()
Definition: ManualLogEntry.php:448
ManualLogEntry\insert
insert(IDatabase $dbw=null)
Insert the entry into the logging table.
Definition: ManualLogEntry.php:281
ManualLogEntry\$performer
UserIdentity $performer
Performer of the action for the log entry.
Definition: ManualLogEntry.php:58
MediaWiki\Logger\LoggerFactory\getInstance
static getInstance( $channel)
Get a named logger instance from the currently configured logger factory.
Definition: LoggerFactory.php:92
ManualLogEntry\setAssociatedRevId
setAssociatedRevId( $revId)
Set an associated revision id.
Definition: ManualLogEntry.php:201
ManualLogEntry\getParameters
getParameters()
Definition: ManualLogEntry.php:462
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1668
ManualLogEntry\getComment
getComment()
Definition: ManualLogEntry.php:492
RequestContext\newExtraneousContext
static newExtraneousContext(Title $title, $request=[])
Create a new extraneous context.
Definition: RequestContext.php:658
SpecialPage\getTitleFor
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,...
Definition: SpecialPage.php:107
MediaWiki\ChangeTags\Taggable
Interface that defines how to tag objects.
Definition: Taggable.php:32
ManualLogEntry\setRelations
setRelations(array $relations)
Declare arbitrary tag/value relations to this log entry.
Definition: ManualLogEntry.php:141
ManualLogEntry\addTags
addTags( $tags)
Add change tags for the log entry.
Definition: ManualLogEntry.php:229
Page\PageReference
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.
Definition: PageReference.php:49
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
ManualLogEntry\getTimestamp
getTimestamp()
Definition: ManualLogEntry.php:483
ManualLogEntry\$legacy
bool $legacy
Whether this is a legacy log entry.
Definition: ManualLogEntry.php:85
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
ManualLogEntry\setTags
setTags( $tags)
Set change tags for the log entry.
Definition: ManualLogEntry.php:215
ManualLogEntry\$comment
string $comment
Comment for the log entry.
Definition: ManualLogEntry.php:67
ManualLogEntry\$parameters
array $parameters
Parameters for log entry.
Definition: ManualLogEntry.php:52
ManualLogEntry\getRecentChange
getRecentChange( $newId=0)
Get a RecentChanges object for the log entry.
Definition: ManualLogEntry.php:354
ManualLogEntry\isLegacy
isLegacy()
Definition: ManualLogEntry.php:526
MWException
MediaWiki exception.
Definition: MWException.php:29
ManualLogEntry\setComment
setComment( $comment)
Set a comment associated with the action being logged.
Definition: ManualLogEntry.php:188
ManualLogEntry\$tags
string[] $tags
Change tags add to the log entry.
Definition: ManualLogEntry.php:73
ManualLogEntry\$id
int $id
ID of the log entry.
Definition: ManualLogEntry.php:79
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2202
ManualLogEntry\getPerformerIdentity
getPerformerIdentity()
Definition: ManualLogEntry.php:469
LogPage
Class to simplify the use of log pages.
Definition: LogPage.php:38
ManualLogEntry\getSubtype
getSubtype()
Definition: ManualLogEntry.php:455
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:1697
ManualLogEntry\$subtype
string $subtype
Sub type of log entry.
Definition: ManualLogEntry.php:49
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:894
ManualLogEntry\setParameters
setParameters( $parameters)
Set extra log parameters.
Definition: ManualLogEntry.php:130
ManualLogEntry\publish
publish( $newId, $to='rcandudp')
Publish the log entry.
Definition: ManualLogEntry.php:394
ManualLogEntry\$relations
array $relations
Definition: ManualLogEntry.php:55
LogEntryBase
Extends the LogEntry Interface with some basic functionality.
Definition: LogEntryBase.php:31
ManualLogEntry\setIsPatrollable
setIsPatrollable( $patrollable)
Set whether this log entry should be made patrollable This shouldn't depend on config,...
Definition: ManualLogEntry.php:250
DB_PRIMARY
const DB_PRIMARY
Definition: defines.php:27
ManualLogEntry\setDeleted
setDeleted( $deleted)
Set the 'deleted' flag.
Definition: ManualLogEntry.php:270
Hooks\runner
static runner()
Get a HookRunner instance for calling hooks using the new interfaces.
Definition: Hooks.php:173
ManualLogEntry\$timestamp
string $timestamp
Timestamp of creation of the log entry.
Definition: ManualLogEntry.php:64
Title\newFromLinkTarget
static newFromLinkTarget(LinkTarget $linkTarget, $forceClone='')
Returns a Title given a LinkTarget.
Definition: Title.php:295
ManualLogEntry\getTags
getTags()
Definition: ManualLogEntry.php:508
Title
Represents a title within MediaWiki.
Definition: Title.php:48
ManualLogEntry\getDeleted
getDeleted()
Definition: ManualLogEntry.php:533
LogEntryBase\makeParamBlob
static makeParamBlob( $params)
Create a blob from a parameter array.
Definition: LogEntryBase.php:58
ManualLogEntry\$revId
int $revId
A rev id associated to the log entry.
Definition: ManualLogEntry.php:70
ManualLogEntry\getIsPatrollable
getIsPatrollable()
Whether this log entry is patrollable.
Definition: ManualLogEntry.php:518
ManualLogEntry\$target
Title $target
Target title for the log entry.
Definition: ManualLogEntry.php:61
ManualLogEntry
Class for creating new log entries and inserting them into the database.
Definition: ManualLogEntry.php:44
Title\castFromPageReference
static castFromPageReference(?PageReference $pageReference)
Return a Title for a given Reference.
Definition: Title.php:345
ManualLogEntry\setLegacy
setLegacy( $legacy)
Set the 'legacy' flag.
Definition: ManualLogEntry.php:260
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
ManualLogEntry\getAssociatedRevId
getAssociatedRevId()
Definition: ManualLogEntry.php:500
CommentStore\getStore
static getStore()
Definition: CommentStore.php:120
ManualLogEntry\setPerformer
setPerformer(UserIdentity $performer)
Set the user that performed the action being logged.
Definition: ManualLogEntry.php:151
DeferredUpdates\addCallableUpdate
static addCallableUpdate( $callable, $stage=self::POSTSEND, $dbw=null)
Add an update to the pending update queue that invokes the specified callback when run.
Definition: DeferredUpdates.php:145
ManualLogEntry\setTarget
setTarget( $target)
Set the title of the object changed.
Definition: ManualLogEntry.php:162
ManualLogEntry\$type
string $type
Type of log entry.
Definition: ManualLogEntry.php:46
ManualLogEntry\$isPatrollable
bool $isPatrollable
Can this log entry be patrolled?
Definition: ManualLogEntry.php:82
ChangeTags\addTags
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.
Definition: ChangeTags.php:328
LogFormatter\newFromEntry
static newFromEntry(LogEntry $entry)
Constructs a new formatter suitable for given entry.
Definition: LogFormatter.php:54