MediaWiki  master
ManualLogEntry.php
Go to the documentation of this file.
1 <?php
29 use Wikimedia\Assert\Assert;
30 use Wikimedia\IPUtils;
32 
38 class ManualLogEntry extends LogEntryBase implements Taggable {
40  protected $type;
41 
43  protected $subtype;
44 
46  protected $parameters = [];
47 
49  protected $relations = [];
50 
52  protected $performer;
53 
55  protected $target;
56 
58  protected $timestamp;
59 
61  protected $comment = '';
62 
64  protected $revId = 0;
65 
67  protected $tags = [];
68 
70  protected $deleted;
71 
73  protected $id;
74 
76  protected $isPatrollable = false;
77 
79  protected $legacy = false;
80 
86  public function __construct( $type, $subtype ) {
87  $this->type = $type;
88  $this->subtype = $subtype;
89  }
90 
112  public function setParameters( $parameters ) {
113  $this->parameters = $parameters;
114  }
115 
123  public function setRelations( array $relations ) {
124  $this->relations = $relations;
125  }
126 
133  public function setPerformer( UserIdentity $performer ) {
134  $this->performer = User::newFromIdentity( $performer );
135  }
136 
143  public function setTarget( LinkTarget $target ) {
144  $this->target = Title::newFromLinkTarget( $target );
145  }
146 
153  public function setTimestamp( $timestamp ) {
154  $this->timestamp = $timestamp;
155  }
156 
163  public function setComment( $comment ) {
164  $this->comment = $comment;
165  }
166 
176  public function setAssociatedRevId( $revId ) {
177  $this->revId = $revId;
178  }
179 
190  public function setTags( $tags ) {
191  if ( $this->tags ) {
192  wfDebug( 'Overwriting existing ManualLogEntry tags' );
193  }
194  $this->tags = [];
195  $this->addTags( $tags );
196  }
197 
204  public function addTags( $tags ) {
205  if ( $tags === null ) {
206  return;
207  }
208 
209  if ( is_string( $tags ) ) {
210  $tags = [ $tags ];
211  }
212  Assert::parameterElementType( 'string', $tags, 'tags' );
213  $this->tags = array_unique( array_merge( $this->tags, $tags ) );
214  }
215 
225  public function setIsPatrollable( $patrollable ) {
226  $this->isPatrollable = (bool)$patrollable;
227  }
228 
235  public function setLegacy( $legacy ) {
236  $this->legacy = $legacy;
237  }
238 
245  public function setDeleted( $deleted ) {
246  $this->deleted = $deleted;
247  }
248 
256  public function insert( IDatabase $dbw = null ) {
257  $dbw = $dbw ?: wfGetDB( DB_MASTER );
258 
259  if ( $this->timestamp === null ) {
260  $this->timestamp = wfTimestampNow();
261  }
262 
263  // Trim spaces on user supplied text
264  $comment = trim( $this->getComment() );
265 
266  $params = $this->getParameters();
268 
269  // Additional fields for which there's no space in the database table schema
270  $revId = $this->getAssociatedRevId();
271  if ( $revId ) {
272  $params['associated_rev_id'] = $revId;
273  $relations['associated_rev_id'] = $revId;
274  }
275 
276  $data = [
277  'log_type' => $this->getType(),
278  'log_action' => $this->getSubtype(),
279  'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
280  'log_namespace' => $this->getTarget()->getNamespace(),
281  'log_title' => $this->getTarget()->getDBkey(),
282  'log_page' => $this->getTarget()->getArticleID(),
283  'log_params' => LogEntryBase::makeParamBlob( $params ),
284  ];
285  if ( isset( $this->deleted ) ) {
286  $data['log_deleted'] = $this->deleted;
287  }
288  $data += CommentStore::getStore()->insert( $dbw, 'log_comment', $comment );
290  ->getInsertValues( $dbw, 'log_user', $this->getPerformer() );
291 
292  $dbw->insert( 'logging', $data, __METHOD__ );
293  $this->id = $dbw->insertId();
294 
295  $rows = [];
296  foreach ( $relations as $tag => $values ) {
297  if ( !strlen( $tag ) ) {
298  throw new MWException( "Got empty log search tag." );
299  }
300 
301  if ( !is_array( $values ) ) {
302  $values = [ $values ];
303  }
304 
305  foreach ( $values as $value ) {
306  $rows[] = [
307  'ls_field' => $tag,
308  'ls_value' => $value,
309  'ls_log_id' => $this->id
310  ];
311  }
312  }
313  if ( count( $rows ) ) {
314  $dbw->insert( 'log_search', $rows, __METHOD__, [ 'IGNORE' ] );
315  }
316 
317  return $this->id;
318  }
319 
327  public function getRecentChange( $newId = 0 ) {
328  $formatter = LogFormatter::newFromEntry( $this );
330  $formatter->setContext( $context );
331 
332  $logpage = SpecialPage::getTitleFor( 'Log', $this->getType() );
333  $user = $this->getPerformer();
334  $ip = "";
335  if ( $user->isAnon() ) {
336  // "MediaWiki default" and friends may have
337  // no IP address in their name
338  if ( IPUtils::isIPAddress( $user->getName() ) ) {
339  $ip = $user->getName();
340  }
341  }
342 
344  $this->getTimestamp(),
345  $logpage,
346  $user,
347  $formatter->getPlainActionText(),
348  $ip,
349  $this->getType(),
350  $this->getSubtype(),
351  $this->getTarget(),
352  $this->getComment(),
353  LogEntryBase::makeParamBlob( $this->getParameters() ),
354  $newId,
355  $formatter->getIRCActionComment(), // Used for IRC feeds
356  $this->getAssociatedRevId(), // Used for e.g. moves and uploads
357  $this->getIsPatrollable()
358  );
359  }
360 
367  public function publish( $newId, $to = 'rcandudp' ) {
368  $canAddTags = true;
369  // FIXME: this code should be removed once all callers properly call publish()
370  if ( $to === 'udp' && !$newId && !$this->getAssociatedRevId() ) {
371  \MediaWiki\Logger\LoggerFactory::getInstance( 'logging' )->warning(
372  'newId and/or revId must be set when calling ManualLogEntry::publish()',
373  [
374  'newId' => $newId,
375  'to' => $to,
376  'revId' => $this->getAssociatedRevId(),
377  // pass a new exception to register the stack trace
378  'exception' => new RuntimeException()
379  ]
380  );
381  $canAddTags = false;
382  }
383 
385  function () use ( $newId, $to, $canAddTags ) {
386  $log = new LogPage( $this->getType() );
387  if ( !$log->isRestricted() ) {
388  Hooks::runWithoutAbort( 'ManualLogEntryBeforePublish', [ $this ] );
389  $rc = $this->getRecentChange( $newId );
390 
391  if ( $to === 'rc' || $to === 'rcandudp' ) {
392  // save RC, passing tags so they are applied there
393  $rc->addTags( $this->getTags() );
394  $rc->save( $rc::SEND_NONE );
395  } else {
396  $tags = $this->getTags();
397  if ( $tags && $canAddTags ) {
398  $revId = $this->getAssociatedRevId();
400  $tags,
401  null,
402  $revId > 0 ? $revId : null,
403  $newId > 0 ? $newId : null
404  );
405  }
406  }
407 
408  if ( $to === 'udp' || $to === 'rcandudp' ) {
409  $rc->notifyRCFeeds();
410  }
411  }
412  },
414  wfGetDB( DB_MASTER )
415  );
416  }
417 
421  public function getType() {
422  return $this->type;
423  }
424 
428  public function getSubtype() {
429  return $this->subtype;
430  }
431 
435  public function getParameters() {
436  return $this->parameters;
437  }
438 
442  public function getPerformer() {
443  return $this->performer;
444  }
445 
449  public function getTarget() {
450  return $this->target;
451  }
452 
456  public function getTimestamp() {
457  $ts = $this->timestamp ?? wfTimestampNow();
458 
459  return wfTimestamp( TS_MW, $ts );
460  }
461 
465  public function getComment() {
466  return $this->comment;
467  }
468 
473  public function getAssociatedRevId() {
474  return $this->revId;
475  }
476 
481  public function getTags() {
482  return $this->tags;
483  }
484 
491  public function getIsPatrollable() {
492  return $this->isPatrollable;
493  }
494 
499  public function isLegacy() {
500  return $this->legacy;
501  }
502 
506  public function getDeleted() {
507  return (int)$this->deleted;
508  }
509 }
ManualLogEntry\setTimestamp
setTimestamp( $timestamp)
Set the timestamp of when the logged action took place.
Definition: ManualLogEntry.php:153
ManualLogEntry\__construct
__construct( $type, $subtype)
Definition: ManualLogEntry.php:86
ManualLogEntry\getTarget
getTarget()
Definition: ManualLogEntry.php:449
ManualLogEntry\$deleted
int $deleted
Deletion state of the log entry.
Definition: ManualLogEntry.php:70
ManualLogEntry\getType
getType()
Definition: ManualLogEntry.php:421
ManualLogEntry\insert
insert(IDatabase $dbw=null)
Insert the entry into the logging table.
Definition: ManualLogEntry.php:256
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:176
ManualLogEntry\getParameters
getParameters()
Definition: ManualLogEntry.php:435
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1806
ManualLogEntry\getComment
getComment()
Definition: ManualLogEntry.php:465
RequestContext\newExtraneousContext
static newExtraneousContext(Title $title, $request=[])
Create a new extraneous context.
Definition: RequestContext.php:621
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:83
User\newFromIdentity
static newFromIdentity(UserIdentity $identity)
Returns a User object corresponding to the given UserIdentity.
Definition: User.php:589
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:123
ManualLogEntry\addTags
addTags( $tags)
Add change tags for the log entry.
Definition: ManualLogEntry.php:204
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:32
ManualLogEntry\getTimestamp
getTimestamp()
Definition: ManualLogEntry.php:456
ActorMigration\newMigration
static newMigration()
Static constructor.
Definition: ActorMigration.php:139
ManualLogEntry\$legacy
bool $legacy
Whether this is a legacy log entry.
Definition: ManualLogEntry.php:79
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:190
ManualLogEntry\$comment
string $comment
Comment for the log entry.
Definition: ManualLogEntry.php:61
ManualLogEntry\$parameters
array $parameters
Parameters for log entry.
Definition: ManualLogEntry.php:46
ManualLogEntry\getRecentChange
getRecentChange( $newId=0)
Get a RecentChanges object for the log entry.
Definition: ManualLogEntry.php:327
ManualLogEntry\isLegacy
isLegacy()
Definition: ManualLogEntry.php:499
MWException
MediaWiki exception.
Definition: MWException.php:26
ManualLogEntry\setComment
setComment( $comment)
Set a comment associated with the action being logged.
Definition: ManualLogEntry.php:163
ManualLogEntry\$tags
string[] $tags
Change tags add to the log entry.
Definition: ManualLogEntry.php:67
ManualLogEntry\$id
int $id
ID of the log entry.
Definition: ManualLogEntry.php:73
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2463
LogPage
Class to simplify the use of log pages.
Definition: LogPage.php:33
DeferredUpdates\POSTSEND
const POSTSEND
Definition: DeferredUpdates.php:85
ManualLogEntry\getSubtype
getSubtype()
Definition: ManualLogEntry.php:428
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:1835
DB_MASTER
const DB_MASTER
Definition: defines.php:26
ManualLogEntry\$subtype
string $subtype
Sub type of log entry.
Definition: ManualLogEntry.php:43
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:913
ManualLogEntry\setParameters
setParameters( $parameters)
Set extra log parameters.
Definition: ManualLogEntry.php:112
ManualLogEntry\publish
publish( $newId, $to='rcandudp')
Publish the log entry.
Definition: ManualLogEntry.php:367
ManualLogEntry\$relations
array $relations
Definition: ManualLogEntry.php:49
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:225
Hooks\runWithoutAbort
static runWithoutAbort( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:150
ManualLogEntry\setDeleted
setDeleted( $deleted)
Set the 'deleted' flag.
Definition: ManualLogEntry.php:245
ManualLogEntry\$timestamp
string $timestamp
Timestamp of creation of the log entry.
Definition: ManualLogEntry.php:58
Title\newFromLinkTarget
static newFromLinkTarget(LinkTarget $linkTarget, $forceClone='')
Returns a Title given a LinkTarget.
Definition: Title.php:284
ManualLogEntry\getTags
getTags()
Definition: ManualLogEntry.php:481
ManualLogEntry\getPerformer
getPerformer()
Definition: ManualLogEntry.php:442
$context
$context
Definition: load.php:43
ManualLogEntry\setTarget
setTarget(LinkTarget $target)
Set the title of the object changed.
Definition: ManualLogEntry.php:143
RecentChange\newLogEntry
static newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action, $target, $logComment, $params, $newId=0, $actionCommentIRC='', $revId=0, $isPatrollable=false)
Definition: RecentChange.php:819
Title
Represents a title within MediaWiki.
Definition: Title.php:42
ManualLogEntry\getDeleted
getDeleted()
Definition: ManualLogEntry.php:506
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:64
ManualLogEntry\getIsPatrollable
getIsPatrollable()
Whether this log entry is patrollable.
Definition: ManualLogEntry.php:491
ManualLogEntry\$target
Title $target
Target title for the log entry.
Definition: ManualLogEntry.php:55
ManualLogEntry
Class for creating new log entries and inserting them into the database.
Definition: ManualLogEntry.php:38
ManualLogEntry\setLegacy
setLegacy( $legacy)
Set the 'legacy' flag.
Definition: ManualLogEntry.php:235
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
ManualLogEntry\getAssociatedRevId
getAssociatedRevId()
Definition: ManualLogEntry.php:473
CommentStore\getStore
static getStore()
Definition: CommentStore.php:109
ManualLogEntry\setPerformer
setPerformer(UserIdentity $performer)
Set the user that performed the action being logged.
Definition: ManualLogEntry.php:133
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:54
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:40
ManualLogEntry\$isPatrollable
bool $isPatrollable
Can this log entry be patrolled?
Definition: ManualLogEntry.php:76
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:256
ManualLogEntry\$performer
User $performer
Performer of the action for the log entry.
Definition: ManualLogEntry.php:52
LogFormatter\newFromEntry
static newFromEntry(LogEntry $entry)
Constructs a new formatter suitable for given entry.
Definition: LogFormatter.php:50