MediaWiki  master
DatabaseLogEntry.php
Go to the documentation of this file.
1 <?php
30 use Wikimedia\AtEase\AtEase;
32 
42 
54  public static function getSelectQueryData() {
55  $commentQuery = MediaWikiServices::getInstance()->getCommentStore()->getJoin( 'log_comment' );
56 
57  $tables = array_merge(
58  [
59  'logging',
60  'logging_actor' => 'actor',
61  'user'
62  ],
63  $commentQuery['tables']
64  );
65  $fields = [
66  'log_id', 'log_type', 'log_action', 'log_timestamp',
67  'log_namespace', 'log_title', // unused log_page
68  'log_params', 'log_deleted',
69  'user_id',
70  'user_name',
71  'log_actor',
72  'log_user' => 'logging_actor.actor_user',
73  'log_user_text' => 'logging_actor.actor_name'
74  ] + $commentQuery['fields'];
75 
76  $joins = [
77  'logging_actor' => [ 'JOIN', 'actor_id=log_actor' ],
78  // IPs don't have an entry in user table
79  'user' => [ 'LEFT JOIN', 'user_id=logging_actor.actor_user' ],
80  ] + $commentQuery['joins'];
81 
82  return [
83  'tables' => $tables,
84  'fields' => $fields,
85  'conds' => [],
86  'options' => [],
87  'join_conds' => $joins,
88  ];
89  }
90 
98  public static function newFromRow( $row ) {
99  $row = (object)$row;
100  if ( isset( $row->rc_logid ) ) {
101  return new RCDatabaseLogEntry( $row );
102  } else {
103  return new self( $row );
104  }
105  }
106 
114  public static function newFromId( $id, IDatabase $db ) {
115  $queryInfo = self::getSelectQueryData();
116  $queryInfo['conds'] += [ 'log_id' => $id ];
117  $row = $db->selectRow(
118  $queryInfo['tables'],
119  $queryInfo['fields'],
120  $queryInfo['conds'],
121  __METHOD__,
122  $queryInfo['options'],
123  $queryInfo['join_conds']
124  );
125  if ( !$row ) {
126  return null;
127  }
128  return self::newFromRow( $row );
129  }
130 
132  protected $row;
133 
135  protected $performer;
136 
138  protected $params;
139 
141  protected $revId = null;
142 
144  protected $legacy;
145 
146  protected function __construct( $row ) {
147  $this->row = $row;
148  }
149 
155  public function getId() {
156  return (int)( $this->row->log_id ?? 0 );
157  }
158 
164  protected function getRawParameters() {
165  return $this->row->log_params;
166  }
167 
168  public function isLegacy() {
169  // This extracts the property
170  $this->getParameters();
171  return $this->legacy;
172  }
173 
174  public function getType() {
175  return $this->row->log_type;
176  }
177 
178  public function getSubtype() {
179  return $this->row->log_action;
180  }
181 
182  public function getParameters() {
183  if ( !isset( $this->params ) ) {
184  $blob = $this->getRawParameters();
185  AtEase::suppressWarnings();
187  AtEase::restoreWarnings();
188  if ( $params !== false ) {
189  $this->params = $params;
190  $this->legacy = false;
191  } else {
192  $this->params = LogPage::extractParams( $blob );
193  $this->legacy = true;
194  }
195 
196  if ( isset( $this->params['associated_rev_id'] ) ) {
197  $this->revId = $this->params['associated_rev_id'];
198  unset( $this->params['associated_rev_id'] );
199  }
200  }
201 
202  return $this->params;
203  }
204 
205  public function getAssociatedRevId() {
206  // This extracts the property
207  $this->getParameters();
208  return $this->revId;
209  }
210 
211  public function getPerformerIdentity(): UserIdentity {
212  if ( !$this->performer ) {
213  $actorStore = MediaWikiServices::getInstance()->getActorStore();
214  try {
215  $this->performer = $actorStore->newActorFromRowFields(
216  $this->row->user_id ?? 0,
217  $this->row->log_user_text ?? null,
218  $this->row->log_actor ?? null
219  );
220  } catch ( InvalidArgumentException $e ) {
221  LoggerFactory::getInstance( 'logentry' )->warning(
222  'Failed to instantiate log entry performer', [
223  'exception' => $e,
224  'log_id' => $this->getId()
225  ]
226  );
227  $this->performer = $actorStore->getUnknownActor();
228  }
229  }
230  return $this->performer;
231  }
232 
233  public function getTarget() {
234  $namespace = $this->row->log_namespace;
235  $page = $this->row->log_title;
236  return Title::makeTitle( $namespace, $page );
237  }
238 
239  public function getTimestamp() {
240  return wfTimestamp( TS_MW, $this->row->log_timestamp );
241  }
242 
243  public function getComment() {
244  return MediaWikiServices::getInstance()->getCommentStore()
245  ->getComment( 'log_comment', $this->row )->text;
246  }
247 
248  public function getDeleted() {
249  return $this->row->log_deleted;
250  }
251 }
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
if(!defined('MW_SETUP_CALLBACK'))
The persistent session ID (if any) loaded at startup.
Definition: WebStart.php:88
A value class to process existing log entries.
getParameters()
Get the extra parameters stored for this message.
isLegacy()
Whether the parameters for this log are stored in new or old format.
array $params
Parameters for log entry.
getSubtype()
The log subtype.
getDeleted()
Get the access restriction.
static newFromRow( $row)
Constructs new LogEntry from database result row.
getComment()
Get the user provided comment.
bool $legacy
Whether the parameters for this log entry are stored in new or old format.
int $revId
A rev id associated to the log entry.
getRawParameters()
Returns whatever is stored in the database field.
static getSelectQueryData()
Returns array of information that is needed for querying log entries.
stdClass $row
Database result row.
getId()
Returns the unique database id.
UserIdentity $performer
getTarget()
Get the target page of this action.
getType()
The main log type.
static newFromId( $id, IDatabase $db)
Loads a LogEntry with the given id from database.
getTimestamp()
Get the timestamp when the action was executed.
Extends the LogEntry Interface with some basic functionality.
static extractParams( $blob)
Extract a parameter array from a blob.
static extractParams( $blob)
Extract a parameter array from a blob.
Definition: LogPage.php:426
PSR-3 logger instance factory.
Service locator for MediaWiki core services.
Represents a title within MediaWiki.
Definition: Title.php:82
A subclass of DatabaseLogEntry for objects constructed from entries in the recentchanges table (rathe...
Interface for objects representing user identity.
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:36
selectRow( $table, $vars, $conds, $fname=__METHOD__, $options=[], $join_conds=[])
Wrapper to IDatabase::select() that only fetches one row (via LIMIT)