MediaWiki  1.34.0
ManualLogEntry.php
Go to the documentation of this file.
1 <?php
30 use Wikimedia\Assert\Assert;
31 
37 class ManualLogEntry extends LogEntryBase implements Taggable {
39  protected $type;
40 
42  protected $subtype;
43 
45  protected $parameters = [];
46 
48  protected $relations = [];
49 
51  protected $performer;
52 
54  protected $target;
55 
57  protected $timestamp;
58 
60  protected $comment = '';
61 
63  protected $revId = 0;
64 
66  protected $tags = [];
67 
69  protected $deleted;
70 
72  protected $id;
73 
75  protected $isPatrollable = false;
76 
78  protected $legacy = false;
79 
85  public function __construct( $type, $subtype ) {
86  $this->type = $type;
87  $this->subtype = $subtype;
88  }
89 
111  public function setParameters( $parameters ) {
112  $this->parameters = $parameters;
113  }
114 
122  public function setRelations( array $relations ) {
123  $this->relations = $relations;
124  }
125 
132  public function setPerformer( UserIdentity $performer ) {
133  $this->performer = User::newFromIdentity( $performer );
134  }
135 
142  public function setTarget( LinkTarget $target ) {
143  $this->target = Title::newFromLinkTarget( $target );
144  }
145 
152  public function setTimestamp( $timestamp ) {
153  $this->timestamp = $timestamp;
154  }
155 
162  public function setComment( $comment ) {
163  $this->comment = $comment;
164  }
165 
175  public function setAssociatedRevId( $revId ) {
176  $this->revId = $revId;
177  }
178 
189  public function setTags( $tags ) {
190  if ( $this->tags ) {
191  wfDebug( 'Overwriting existing ManualLogEntry tags' );
192  }
193  $this->tags = [];
194  $this->addTags( $tags );
195  }
196 
203  public function addTags( $tags ) {
204  if ( $tags === null ) {
205  return;
206  }
207 
208  if ( is_string( $tags ) ) {
209  $tags = [ $tags ];
210  }
211  Assert::parameterElementType( 'string', $tags, 'tags' );
212  $this->tags = array_unique( array_merge( $this->tags, $tags ) );
213  }
214 
224  public function setIsPatrollable( $patrollable ) {
225  $this->isPatrollable = (bool)$patrollable;
226  }
227 
234  public function setLegacy( $legacy ) {
235  $this->legacy = $legacy;
236  }
237 
244  public function setDeleted( $deleted ) {
245  $this->deleted = $deleted;
246  }
247 
255  public function insert( IDatabase $dbw = null ) {
256  $dbw = $dbw ?: wfGetDB( DB_MASTER );
257 
258  if ( $this->timestamp === null ) {
259  $this->timestamp = wfTimestampNow();
260  }
261 
262  // Trim spaces on user supplied text
263  $comment = trim( $this->getComment() );
264 
265  $params = $this->getParameters();
267 
268  // Additional fields for which there's no space in the database table schema
269  $revId = $this->getAssociatedRevId();
270  if ( $revId ) {
271  $params['associated_rev_id'] = $revId;
272  $relations['associated_rev_id'] = $revId;
273  }
274 
275  $data = [
276  'log_type' => $this->getType(),
277  'log_action' => $this->getSubtype(),
278  'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
279  'log_namespace' => $this->getTarget()->getNamespace(),
280  'log_title' => $this->getTarget()->getDBkey(),
281  'log_page' => $this->getTarget()->getArticleID(),
282  'log_params' => LogEntryBase::makeParamBlob( $params ),
283  ];
284  if ( isset( $this->deleted ) ) {
285  $data['log_deleted'] = $this->deleted;
286  }
287  $data += CommentStore::getStore()->insert( $dbw, 'log_comment', $comment );
289  ->getInsertValues( $dbw, 'log_user', $this->getPerformer() );
290 
291  $dbw->insert( 'logging', $data, __METHOD__ );
292  $this->id = $dbw->insertId();
293 
294  $rows = [];
295  foreach ( $relations as $tag => $values ) {
296  if ( !strlen( $tag ) ) {
297  throw new MWException( "Got empty log search tag." );
298  }
299 
300  if ( !is_array( $values ) ) {
301  $values = [ $values ];
302  }
303 
304  foreach ( $values as $value ) {
305  $rows[] = [
306  'ls_field' => $tag,
307  'ls_value' => $value,
308  'ls_log_id' => $this->id
309  ];
310  }
311  }
312  if ( count( $rows ) ) {
313  $dbw->insert( 'log_search', $rows, __METHOD__, [ 'IGNORE' ] );
314  }
315 
316  return $this->id;
317  }
318 
326  public function getRecentChange( $newId = 0 ) {
327  $formatter = LogFormatter::newFromEntry( $this );
329  $formatter->setContext( $context );
330 
331  $logpage = SpecialPage::getTitleFor( 'Log', $this->getType() );
332  $user = $this->getPerformer();
333  $ip = "";
334  if ( $user->isAnon() ) {
335  // "MediaWiki default" and friends may have
336  // no IP address in their name
337  if ( IP::isIPAddress( $user->getName() ) ) {
338  $ip = $user->getName();
339  }
340  }
341 
343  $this->getTimestamp(),
344  $logpage,
345  $user,
346  $formatter->getPlainActionText(),
347  $ip,
348  $this->getType(),
349  $this->getSubtype(),
350  $this->getTarget(),
351  $this->getComment(),
353  $newId,
354  $formatter->getIRCActionComment(), // Used for IRC feeds
355  $this->getAssociatedRevId(), // Used for e.g. moves and uploads
356  $this->getIsPatrollable()
357  );
358  }
359 
366  public function publish( $newId, $to = 'rcandudp' ) {
367  $canAddTags = true;
368  // FIXME: this code should be removed once all callers properly call publish()
369  if ( $to === 'udp' && !$newId && !$this->getAssociatedRevId() ) {
370  \MediaWiki\Logger\LoggerFactory::getInstance( 'logging' )->warning(
371  'newId and/or revId must be set when calling ManualLogEntry::publish()',
372  [
373  'newId' => $newId,
374  'to' => $to,
375  'revId' => $this->getAssociatedRevId(),
376  // pass a new exception to register the stack trace
377  'exception' => new RuntimeException()
378  ]
379  );
380  $canAddTags = false;
381  }
382 
384  function () use ( $newId, $to, $canAddTags ) {
385  $log = new LogPage( $this->getType() );
386  if ( !$log->isRestricted() ) {
387  Hooks::runWithoutAbort( 'ManualLogEntryBeforePublish', [ $this ] );
388  $rc = $this->getRecentChange( $newId );
389 
390  if ( $to === 'rc' || $to === 'rcandudp' ) {
391  // save RC, passing tags so they are applied there
392  $rc->addTags( $this->getTags() );
393  $rc->save( $rc::SEND_NONE );
394  } else {
395  $tags = $this->getTags();
396  if ( $tags && $canAddTags ) {
397  $revId = $this->getAssociatedRevId();
399  $tags,
400  null,
401  $revId > 0 ? $revId : null,
402  $newId > 0 ? $newId : null
403  );
404  }
405  }
406 
407  if ( $to === 'udp' || $to === 'rcandudp' ) {
408  $rc->notifyRCFeeds();
409  }
410  }
411  },
413  wfGetDB( DB_MASTER )
414  );
415  }
416 
417  public function getType() {
418  return $this->type;
419  }
420 
421  public function getSubtype() {
422  return $this->subtype;
423  }
424 
425  public function getParameters() {
426  return $this->parameters;
427  }
428 
432  public function getPerformer() {
433  return $this->performer;
434  }
435 
439  public function getTarget() {
440  return $this->target;
441  }
442 
443  public function getTimestamp() {
444  $ts = $this->timestamp ?? wfTimestampNow();
445 
446  return wfTimestamp( TS_MW, $ts );
447  }
448 
449  public function getComment() {
450  return $this->comment;
451  }
452 
457  public function getAssociatedRevId() {
458  return $this->revId;
459  }
460 
465  public function getTags() {
466  return $this->tags;
467  }
468 
475  public function getIsPatrollable() {
476  return $this->isPatrollable;
477  }
478 
483  public function isLegacy() {
484  return $this->legacy;
485  }
486 
487  public function getDeleted() {
488  return (int)$this->deleted;
489  }
490 }
ManualLogEntry\setTimestamp
setTimestamp( $timestamp)
Set the timestamp of when the logged action took place.
Definition: ManualLogEntry.php:152
ManualLogEntry\__construct
__construct( $type, $subtype)
Definition: ManualLogEntry.php:85
ManualLogEntry\getTarget
getTarget()
Definition: ManualLogEntry.php:439
ManualLogEntry\$deleted
int $deleted
Deletion state of the log entry.
Definition: ManualLogEntry.php:69
ManualLogEntry\getType
getType()
The main log type.
Definition: ManualLogEntry.php:417
ManualLogEntry\insert
insert(IDatabase $dbw=null)
Insert the entry into the logging table.
Definition: ManualLogEntry.php:255
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:175
ManualLogEntry\getParameters
getParameters()
Get the extra parameters stored for this message.
Definition: ManualLogEntry.php:425
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1869
ManualLogEntry\getComment
getComment()
Get the user provided comment.
Definition: ManualLogEntry.php:449
RequestContext\newExtraneousContext
static newExtraneousContext(Title $title, $request=[])
Create a new extraneous context.
Definition: RequestContext.php:601
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:571
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:122
ManualLogEntry\addTags
addTags( $tags)
Add change tags for the log entry.
Definition: ManualLogEntry.php:203
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:32
ManualLogEntry\getTimestamp
getTimestamp()
Get the timestamp when the action was executed.
Definition: ManualLogEntry.php:443
ActorMigration\newMigration
static newMigration()
Static constructor.
Definition: ActorMigration.php:136
ManualLogEntry\$legacy
bool $legacy
Whether this is a legacy log entry.
Definition: ManualLogEntry.php:78
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:189
ManualLogEntry\$comment
string $comment
Comment for the log entry.
Definition: ManualLogEntry.php:60
ManualLogEntry\$parameters
array $parameters
Parameters for log entry.
Definition: ManualLogEntry.php:45
ManualLogEntry\getRecentChange
getRecentChange( $newId=0)
Get a RecentChanges object for the log entry.
Definition: ManualLogEntry.php:326
ManualLogEntry\isLegacy
isLegacy()
Definition: ManualLogEntry.php:483
MWException
MediaWiki exception.
Definition: MWException.php:26
ManualLogEntry\setComment
setComment( $comment)
Set a comment associated with the action being logged.
Definition: ManualLogEntry.php:162
ManualLogEntry\$tags
string[] $tags
Change tags add to the log entry.
Definition: ManualLogEntry.php:66
ManualLogEntry\$id
int $id
ID of the log entry.
Definition: ManualLogEntry.php:72
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2575
LogPage
Class to simplify the use of log pages.
Definition: LogPage.php:33
DeferredUpdates\POSTSEND
const POSTSEND
Definition: DeferredUpdates.php:70
ManualLogEntry\getSubtype
getSubtype()
The log subtype.
Definition: ManualLogEntry.php:421
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:1898
DB_MASTER
const DB_MASTER
Definition: defines.php:26
ManualLogEntry\$subtype
string $subtype
Sub type of log entry.
Definition: ManualLogEntry.php:42
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:111
ManualLogEntry\publish
publish( $newId, $to='rcandudp')
Publish the log entry.
Definition: ManualLogEntry.php:366
ManualLogEntry\$relations
array $relations
Definition: ManualLogEntry.php:48
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:224
Hooks\runWithoutAbort
static runWithoutAbort( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:231
ManualLogEntry\setDeleted
setDeleted( $deleted)
Set the 'deleted' flag.
Definition: ManualLogEntry.php:244
ManualLogEntry\$timestamp
string $timestamp
Timestamp of creation of the log entry.
Definition: ManualLogEntry.php:57
Title\newFromLinkTarget
static newFromLinkTarget(LinkTarget $linkTarget, $forceClone='')
Returns a Title given a LinkTarget.
Definition: Title.php:268
ManualLogEntry\getTags
getTags()
Definition: ManualLogEntry.php:465
ManualLogEntry\getPerformer
getPerformer()
Definition: ManualLogEntry.php:432
$context
$context
Definition: load.php:45
ManualLogEntry\setTarget
setTarget(LinkTarget $target)
Set the title of the object changed.
Definition: ManualLogEntry.php:142
RecentChange\newLogEntry
static newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action, $target, $logComment, $params, $newId=0, $actionCommentIRC='', $revId=0, $isPatrollable=false)
Definition: RecentChange.php:810
Title
Represents a title within MediaWiki.
Definition: Title.php:42
ManualLogEntry\getDeleted
getDeleted()
Get the access restriction.
Definition: ManualLogEntry.php:487
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:63
ManualLogEntry\getIsPatrollable
getIsPatrollable()
Whether this log entry is patrollable.
Definition: ManualLogEntry.php:475
ManualLogEntry\$target
Title $target
Target title for the log entry.
Definition: ManualLogEntry.php:54
ManualLogEntry
Class for creating new log entries and inserting them into the database.
Definition: ManualLogEntry.php:37
ManualLogEntry\setLegacy
setLegacy( $legacy)
Set the 'legacy' flag.
Definition: ManualLogEntry.php:234
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
ManualLogEntry\getAssociatedRevId
getAssociatedRevId()
Definition: ManualLogEntry.php:457
CommentStore\getStore
static getStore()
Definition: CommentStore.php:139
ManualLogEntry\setPerformer
setPerformer(UserIdentity $performer)
Set the user that performed the action being logged.
Definition: ManualLogEntry.php:132
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:51
DeferredUpdates\addCallableUpdate
static addCallableUpdate( $callable, $stage=self::POSTSEND, $dbw=null)
Add a callable update.
Definition: DeferredUpdates.php:124
ManualLogEntry\$type
string $type
Type of log entry.
Definition: ManualLogEntry.php:39
ManualLogEntry\$isPatrollable
bool $isPatrollable
Can this log entry be patrolled?
Definition: ManualLogEntry.php:75
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:251
ManualLogEntry\$performer
User $performer
Performer of the action for the log entry.
Definition: ManualLogEntry.php:51
IP\isIPAddress
static isIPAddress( $ip)
Determine if a string is as valid IP address or network (CIDR prefix).
Definition: IP.php:77
LogFormatter\newFromEntry
static newFromEntry(LogEntry $entry)
Constructs a new formatter suitable for given entry.
Definition: LogFormatter.php:50