MediaWiki  master
ManualLogEntry.php
Go to the documentation of this file.
1 <?php
29 use Wikimedia\Assert\Assert;
30 use Wikimedia\IPUtils;
32 
42 class 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 = $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();
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 );
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 
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 
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  },
419  wfGetDB( DB_MASTER )
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 }
ManualLogEntry\setTimestamp
setTimestamp( $timestamp)
Set the timestamp of when the logged action took place.
Definition: ManualLogEntry.php:158
ManualLogEntry\__construct
__construct( $type, $subtype)
Stable to call.
Definition: ManualLogEntry.php:91
ManualLogEntry\getTarget
getTarget()
Definition: ManualLogEntry.php:454
ManualLogEntry\$deleted
int $deleted
Deletion state of the log entry.
Definition: ManualLogEntry.php:74
ManualLogEntry\getType
getType()
Definition: ManualLogEntry.php:426
ManualLogEntry\insert
insert(IDatabase $dbw=null)
Insert the entry into the logging table.
Definition: ManualLogEntry.php:261
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:181
ManualLogEntry\getParameters
getParameters()
Definition: ManualLogEntry.php:440
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1815
ManualLogEntry\getComment
getComment()
Definition: ManualLogEntry.php:470
RequestContext\newExtraneousContext
static newExtraneousContext(Title $title, $request=[])
Create a new extraneous context.
Definition: RequestContext.php:624
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:92
User\newFromIdentity
static newFromIdentity(UserIdentity $identity)
Returns a User object corresponding to the given UserIdentity.
Definition: User.php:616
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:128
ManualLogEntry\addTags
addTags( $tags)
Add change tags for the log entry.
Definition: ManualLogEntry.php:209
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:32
ManualLogEntry\getTimestamp
getTimestamp()
Definition: ManualLogEntry.php:461
ActorMigration\newMigration
static newMigration()
Static constructor.
Definition: ActorMigration.php:156
ManualLogEntry\$legacy
bool $legacy
Whether this is a legacy log entry.
Definition: ManualLogEntry.php:83
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:195
ManualLogEntry\$comment
string $comment
Comment for the log entry.
Definition: ManualLogEntry.php:65
ManualLogEntry\$parameters
array $parameters
Parameters for log entry.
Definition: ManualLogEntry.php:50
ManualLogEntry\getRecentChange
getRecentChange( $newId=0)
Get a RecentChanges object for the log entry.
Definition: ManualLogEntry.php:332
ManualLogEntry\isLegacy
isLegacy()
Definition: ManualLogEntry.php:504
MWException
MediaWiki exception.
Definition: MWException.php:29
ManualLogEntry\setComment
setComment( $comment)
Set a comment associated with the action being logged.
Definition: ManualLogEntry.php:168
ManualLogEntry\$tags
string[] $tags
Change tags add to the log entry.
Definition: ManualLogEntry.php:71
ManualLogEntry\$id
int $id
ID of the log entry.
Definition: ManualLogEntry.php:77
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2475
LogPage
Class to simplify the use of log pages.
Definition: LogPage.php:37
DeferredUpdates\POSTSEND
const POSTSEND
Definition: DeferredUpdates.php:85
ManualLogEntry\getSubtype
getSubtype()
Definition: ManualLogEntry.php:433
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:1844
DB_MASTER
const DB_MASTER
Definition: defines.php:26
ManualLogEntry\$subtype
string $subtype
Sub type of log entry.
Definition: ManualLogEntry.php:47
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:910
ManualLogEntry\setParameters
setParameters( $parameters)
Set extra log parameters.
Definition: ManualLogEntry.php:117
ManualLogEntry\publish
publish( $newId, $to='rcandudp')
Publish the log entry.
Definition: ManualLogEntry.php:372
ManualLogEntry\$relations
array $relations
Definition: ManualLogEntry.php:53
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:230
ManualLogEntry\setDeleted
setDeleted( $deleted)
Set the 'deleted' flag.
Definition: ManualLogEntry.php:250
Hooks\runner
static runner()
Get a HookRunner instance for calling hooks using the new interfaces.
Definition: Hooks.php:172
ManualLogEntry\$timestamp
string $timestamp
Timestamp of creation of the log entry.
Definition: ManualLogEntry.php:62
Title\newFromLinkTarget
static newFromLinkTarget(LinkTarget $linkTarget, $forceClone='')
Returns a Title given a LinkTarget.
Definition: Title.php:280
ManualLogEntry\getTags
getTags()
Definition: ManualLogEntry.php:486
ManualLogEntry\getPerformer
getPerformer()
Definition: ManualLogEntry.php:447
ManualLogEntry\setTarget
setTarget(LinkTarget $target)
Set the title of the object changed.
Definition: ManualLogEntry.php:148
RecentChange\newLogEntry
static newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action, $target, $logComment, $params, $newId=0, $actionCommentIRC='', $revId=0, $isPatrollable=false)
Definition: RecentChange.php:834
Title
Represents a title within MediaWiki.
Definition: Title.php:41
ManualLogEntry\getDeleted
getDeleted()
Definition: ManualLogEntry.php:511
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:68
ManualLogEntry\getIsPatrollable
getIsPatrollable()
Whether this log entry is patrollable.
Definition: ManualLogEntry.php:496
ManualLogEntry\$target
Title $target
Target title for the log entry.
Definition: ManualLogEntry.php:59
ManualLogEntry
Class for creating new log entries and inserting them into the database.
Definition: ManualLogEntry.php:42
ManualLogEntry\setLegacy
setLegacy( $legacy)
Set the 'legacy' flag.
Definition: ManualLogEntry.php:240
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
ManualLogEntry\getAssociatedRevId
getAssociatedRevId()
Definition: ManualLogEntry.php:478
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:138
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:56
DeferredUpdates\addCallableUpdate
static addCallableUpdate( $callable, $stage=self::POSTSEND, $dbw=null)
Add a callable update.
Definition: DeferredUpdates.php:145
ManualLogEntry\$type
string $type
Type of log entry.
Definition: ManualLogEntry.php:44
ManualLogEntry\$isPatrollable
bool $isPatrollable
Can this log entry be patrolled?
Definition: ManualLogEntry.php:80
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:248
ManualLogEntry\$performer
User $performer
Performer of the action for the log entry.
Definition: ManualLogEntry.php:56
LogFormatter\newFromEntry
static newFromEntry(LogEntry $entry)
Constructs a new formatter suitable for given entry.
Definition: LogFormatter.php:52