MediaWiki  master
ManualLogEntry.php
Go to the documentation of this file.
1 <?php
29 use Wikimedia\Assert\Assert;
30 use Wikimedia\IPUtils;
32 
43 class ManualLogEntry extends LogEntryBase implements Taggable {
45  protected $type;
46 
48  protected $subtype;
49 
51  protected $parameters = [];
52 
54  protected $relations = [];
55 
57  protected $performer;
58 
60  protected $target;
61 
63  protected $timestamp;
64 
66  protected $comment = '';
67 
69  protected $revId = 0;
70 
72  protected $tags = [];
73 
75  protected $deleted;
76 
78  protected $id;
79 
81  protected $isPatrollable = false;
82 
84  protected $legacy = false;
85 
94  public function __construct( $type, $subtype ) {
95  $this->type = $type;
96  $this->subtype = $subtype;
97  }
98 
129  public function setParameters( $parameters ) {
130  $this->parameters = $parameters;
131  }
132 
140  public function setRelations( array $relations ) {
141  $this->relations = $relations;
142  }
143 
150  public function setPerformer( UserIdentity $performer ) {
151  $this->performer = $performer;
152  }
153 
160  public function setTarget( LinkTarget $target ) {
161  $this->target = Title::newFromLinkTarget( $target );
162  }
163 
170  public function setTimestamp( $timestamp ) {
171  $this->timestamp = $timestamp;
172  }
173 
180  public function setComment( $comment ) {
181  $this->comment = $comment;
182  }
183 
193  public function setAssociatedRevId( $revId ) {
194  $this->revId = $revId;
195  }
196 
207  public function setTags( $tags ) {
208  if ( $this->tags ) {
209  wfDebug( 'Overwriting existing ManualLogEntry tags' );
210  }
211  $this->tags = [];
212  $this->addTags( $tags );
213  }
214 
221  public function addTags( $tags ) {
222  if ( $tags === null ) {
223  return;
224  }
225 
226  if ( is_string( $tags ) ) {
227  $tags = [ $tags ];
228  }
229  Assert::parameterElementType( 'string', $tags, 'tags' );
230  $this->tags = array_unique( array_merge( $this->tags, $tags ) );
231  }
232 
242  public function setIsPatrollable( $patrollable ) {
243  $this->isPatrollable = (bool)$patrollable;
244  }
245 
252  public function setLegacy( $legacy ) {
253  $this->legacy = $legacy;
254  }
255 
262  public function setDeleted( $deleted ) {
263  $this->deleted = $deleted;
264  }
265 
273  public function insert( IDatabase $dbw = null ) {
274  $dbw = $dbw ?: wfGetDB( DB_MASTER );
275 
276  if ( $this->timestamp === null ) {
277  $this->timestamp = wfTimestampNow();
278  }
279 
280  // Trim spaces on user supplied text
281  $comment = trim( $this->getComment() );
282 
283  $params = $this->getParameters();
285 
286  // Additional fields for which there's no space in the database table schema
287  $revId = $this->getAssociatedRevId();
288  if ( $revId ) {
289  $params['associated_rev_id'] = $revId;
290  $relations['associated_rev_id'] = $revId;
291  }
292 
293  $data = [
294  'log_type' => $this->getType(),
295  'log_action' => $this->getSubtype(),
296  'log_timestamp' => $dbw->timestamp( $this->getTimestamp() ),
297  'log_namespace' => $this->getTarget()->getNamespace(),
298  'log_title' => $this->getTarget()->getDBkey(),
299  'log_page' => $this->getTarget()->getArticleID(),
300  'log_params' => LogEntryBase::makeParamBlob( $params ),
301  ];
302  if ( isset( $this->deleted ) ) {
303  $data['log_deleted'] = $this->deleted;
304  }
305  $data += CommentStore::getStore()->insert( $dbw, 'log_comment', $comment );
307  ->getInsertValues( $dbw, 'log_user', $this->getPerformerIdentity() );
308 
309  $dbw->insert( 'logging', $data, __METHOD__ );
310  $this->id = $dbw->insertId();
311 
312  $rows = [];
313  foreach ( $relations as $tag => $values ) {
314  if ( !strlen( $tag ) ) {
315  throw new MWException( "Got empty log search tag." );
316  }
317 
318  if ( !is_array( $values ) ) {
319  $values = [ $values ];
320  }
321 
322  foreach ( $values as $value ) {
323  $rows[] = [
324  'ls_field' => $tag,
325  'ls_value' => $value,
326  'ls_log_id' => $this->id
327  ];
328  }
329  }
330  if ( count( $rows ) ) {
331  $dbw->insert( 'log_search', $rows, __METHOD__, [ 'IGNORE' ] );
332  }
333 
334  return $this->id;
335  }
336 
344  public function getRecentChange( $newId = 0 ) {
345  $formatter = LogFormatter::newFromEntry( $this );
346  $context = RequestContext::newExtraneousContext( $this->getTarget() );
347  $formatter->setContext( $context );
348 
349  $logpage = SpecialPage::getTitleFor( 'Log', $this->getType() );
350  $user = $this->getPerformerIdentity();
351  $ip = "";
352  if ( !$user->isRegistered() ) {
353  // "MediaWiki default" and friends may have
354  // no IP address in their name
355  if ( IPUtils::isIPAddress( $user->getName() ) ) {
356  $ip = $user->getName();
357  }
358  }
359 
361  $this->getTimestamp(),
362  $logpage,
363  $user,
364  $formatter->getPlainActionText(),
365  $ip,
366  $this->getType(),
367  $this->getSubtype(),
368  $this->getTarget(),
369  $this->getComment(),
370  LogEntryBase::makeParamBlob( $this->getParameters() ),
371  $newId,
372  $formatter->getIRCActionComment(), // Used for IRC feeds
373  $this->getAssociatedRevId(), // Used for e.g. moves and uploads
374  $this->getIsPatrollable()
375  );
376  }
377 
384  public function publish( $newId, $to = 'rcandudp' ) {
385  $canAddTags = true;
386  // FIXME: this code should be removed once all callers properly call publish()
387  if ( $to === 'udp' && !$newId && !$this->getAssociatedRevId() ) {
388  \MediaWiki\Logger\LoggerFactory::getInstance( 'logging' )->warning(
389  'newId and/or revId must be set when calling ManualLogEntry::publish()',
390  [
391  'newId' => $newId,
392  'to' => $to,
393  'revId' => $this->getAssociatedRevId(),
394  // pass a new exception to register the stack trace
395  'exception' => new RuntimeException()
396  ]
397  );
398  $canAddTags = false;
399  }
400 
402  function () use ( $newId, $to, $canAddTags ) {
403  $log = new LogPage( $this->getType() );
404  if ( !$log->isRestricted() ) {
405  Hooks::runner()->onManualLogEntryBeforePublish( $this );
406  $rc = $this->getRecentChange( $newId );
407 
408  if ( $to === 'rc' || $to === 'rcandudp' ) {
409  // save RC, passing tags so they are applied there
410  $rc->addTags( $this->getTags() );
411  $rc->save( $rc::SEND_NONE );
412  } else {
413  $tags = $this->getTags();
414  if ( $tags && $canAddTags ) {
415  $revId = $this->getAssociatedRevId();
417  $tags,
418  null,
419  $revId > 0 ? $revId : null,
420  $newId > 0 ? $newId : null
421  );
422  }
423  }
424 
425  if ( $to === 'udp' || $to === 'rcandudp' ) {
426  $rc->notifyRCFeeds();
427  }
428  }
429  },
430  DeferredUpdates::POSTSEND,
431  wfGetDB( DB_MASTER )
432  );
433  }
434 
438  public function getType() {
439  return $this->type;
440  }
441 
445  public function getSubtype() {
446  return $this->subtype;
447  }
448 
452  public function getParameters() {
453  return $this->parameters;
454  }
455 
459  public function getPerformer() {
460  return User::newFromIdentity( $this->performer );
461  }
462 
466  public function getPerformerIdentity() : UserIdentity {
467  return $this->performer;
468  }
469 
473  public function getTarget() {
474  return $this->target;
475  }
476 
480  public function getTimestamp() {
481  $ts = $this->timestamp ?? wfTimestampNow();
482 
483  return wfTimestamp( TS_MW, $ts );
484  }
485 
489  public function getComment() {
490  return $this->comment;
491  }
492 
497  public function getAssociatedRevId() {
498  return $this->revId;
499  }
500 
505  public function getTags() {
506  return $this->tags;
507  }
508 
515  public function getIsPatrollable() {
516  return $this->isPatrollable;
517  }
518 
523  public function isLegacy() {
524  return $this->legacy;
525  }
526 
530  public function getDeleted() {
531  return (int)$this->deleted;
532  }
533 }
ManualLogEntry\setTimestamp
setTimestamp( $timestamp)
Set the timestamp of when the logged action took place.
Definition: ManualLogEntry.php:170
ManualLogEntry\__construct
__construct( $type, $subtype)
Stable to call.
Definition: ManualLogEntry.php:94
ManualLogEntry\getTarget
getTarget()
Definition: ManualLogEntry.php:473
ManualLogEntry\$deleted
int $deleted
Deletion state of the log entry.
Definition: ManualLogEntry.php:75
ManualLogEntry\getType
getType()
Definition: ManualLogEntry.php:438
ManualLogEntry\insert
insert(IDatabase $dbw=null)
Insert the entry into the logging table.
Definition: ManualLogEntry.php:273
ManualLogEntry\$performer
UserIdentity $performer
Performer of the action for the log entry.
Definition: ManualLogEntry.php:57
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:193
ManualLogEntry\getParameters
getParameters()
Definition: ManualLogEntry.php:452
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1831
ManualLogEntry\getComment
getComment()
Definition: ManualLogEntry.php:489
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:106
User\newFromIdentity
static newFromIdentity(UserIdentity $identity)
Returns a User object corresponding to the given UserIdentity.
Definition: User.php:619
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:140
ManualLogEntry\addTags
addTags( $tags)
Add change tags for the log entry.
Definition: ManualLogEntry.php:221
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:32
ManualLogEntry\getTimestamp
getTimestamp()
Definition: ManualLogEntry.php:480
ActorMigration\newMigration
static newMigration()
Static constructor.
Definition: ActorMigration.php:156
ManualLogEntry\$legacy
bool $legacy
Whether this is a legacy log entry.
Definition: ManualLogEntry.php:84
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:207
ManualLogEntry\$comment
string $comment
Comment for the log entry.
Definition: ManualLogEntry.php:66
ManualLogEntry\$parameters
array $parameters
Parameters for log entry.
Definition: ManualLogEntry.php:51
ManualLogEntry\getRecentChange
getRecentChange( $newId=0)
Get a RecentChanges object for the log entry.
Definition: ManualLogEntry.php:344
ManualLogEntry\isLegacy
isLegacy()
Definition: ManualLogEntry.php:523
MWException
MediaWiki exception.
Definition: MWException.php:29
ManualLogEntry\setComment
setComment( $comment)
Set a comment associated with the action being logged.
Definition: ManualLogEntry.php:180
ManualLogEntry\$tags
string[] $tags
Change tags add to the log entry.
Definition: ManualLogEntry.php:72
ManualLogEntry\$id
int $id
ID of the log entry.
Definition: ManualLogEntry.php:78
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2466
ManualLogEntry\getPerformerIdentity
getPerformerIdentity()
Definition: ManualLogEntry.php:466
LogPage
Class to simplify the use of log pages.
Definition: LogPage.php:37
ManualLogEntry\getSubtype
getSubtype()
Definition: ManualLogEntry.php:445
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:1860
DB_MASTER
const DB_MASTER
Definition: defines.php:26
ManualLogEntry\$subtype
string $subtype
Sub type of log entry.
Definition: ManualLogEntry.php:48
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:914
ManualLogEntry\setParameters
setParameters( $parameters)
Set extra log parameters.
Definition: ManualLogEntry.php:129
ManualLogEntry\publish
publish( $newId, $to='rcandudp')
Publish the log entry.
Definition: ManualLogEntry.php:384
ManualLogEntry\$relations
array $relations
Definition: ManualLogEntry.php:54
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:242
ManualLogEntry\setDeleted
setDeleted( $deleted)
Set the 'deleted' flag.
Definition: ManualLogEntry.php:262
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:63
Title\newFromLinkTarget
static newFromLinkTarget(LinkTarget $linkTarget, $forceClone='')
Returns a Title given a LinkTarget.
Definition: Title.php:289
ManualLogEntry\getTags
getTags()
Definition: ManualLogEntry.php:505
ManualLogEntry\getPerformer
getPerformer()
Definition: ManualLogEntry.php:459
ManualLogEntry\setTarget
setTarget(LinkTarget $target)
Set the title of the object changed.
Definition: ManualLogEntry.php:160
RecentChange\newLogEntry
static newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action, $target, $logComment, $params, $newId=0, $actionCommentIRC='', $revId=0, $isPatrollable=false)
Definition: RecentChange.php:835
Title
Represents a title within MediaWiki.
Definition: Title.php:46
ManualLogEntry\getDeleted
getDeleted()
Definition: ManualLogEntry.php:530
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:69
ManualLogEntry\getIsPatrollable
getIsPatrollable()
Whether this log entry is patrollable.
Definition: ManualLogEntry.php:515
ManualLogEntry\$target
Title $target
Target title for the log entry.
Definition: ManualLogEntry.php:60
ManualLogEntry
Class for creating new log entries and inserting them into the database.
Definition: ManualLogEntry.php:43
ManualLogEntry\setLegacy
setLegacy( $legacy)
Set the 'legacy' flag.
Definition: ManualLogEntry.php:252
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
ManualLogEntry\getAssociatedRevId
getAssociatedRevId()
Definition: ManualLogEntry.php:497
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:150
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\$type
string $type
Type of log entry.
Definition: ManualLogEntry.php:45
ManualLogEntry\$isPatrollable
bool $isPatrollable
Can this log entry be patrolled?
Definition: ManualLogEntry.php:81
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:308
LogFormatter\newFromEntry
static newFromEntry(LogEntry $entry)
Constructs a new formatter suitable for given entry.
Definition: LogFormatter.php:53