3 * Contains a class for dealing with database log entries
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @author Niklas Laxström
22 * @license GPL-2.0-or-later
26 use Wikimedia\Rdbms\IDatabase
;
29 * A value class to process existing log entries. In other words, this class caches a log
30 * entry from the database and provides an immutable object-oriented representation of it.
34 class DatabaseLogEntry
extends LogEntryBase
{
37 * Returns array of information that is needed for querying
38 * log entries. Array contains the following keys:
39 * tables, fields, conds, options and join_conds
43 public static function getSelectQueryData() {
44 $commentQuery = CommentStore
::getStore()->getJoin( 'log_comment' );
45 $actorQuery = ActorMigration
::newMigration()->getJoin( 'log_user' );
47 $tables = array_merge(
48 [ 'logging' ], $commentQuery['tables'], $actorQuery['tables'], [ 'user' ]
51 'log_id', 'log_type', 'log_action', 'log_timestamp',
52 'log_namespace', 'log_title', // unused log_page
53 'log_params', 'log_deleted',
54 'user_id', 'user_name', 'user_editcount',
55 ] +
$commentQuery['fields'] +
$actorQuery['fields'];
58 // IPs don't have an entry in user table
59 'user' => [ 'LEFT JOIN', 'user_id=' . $actorQuery['fields']['log_user'] ],
60 ] +
$commentQuery['joins'] +
$actorQuery['joins'];
67 'join_conds' => $joins,
72 * Constructs new LogEntry from database result row.
73 * Supports rows from both logging and recentchanges table.
75 * @param stdClass|array $row
76 * @return DatabaseLogEntry
78 public static function newFromRow( $row ) {
80 if ( isset( $row->rc_logid
) ) {
81 return new RCDatabaseLogEntry( $row );
83 return new self( $row );
88 * Loads a LogEntry with the given id from database
91 * @param IDatabase $db
92 * @return DatabaseLogEntry|null
94 public static function newFromId( $id, IDatabase
$db ) {
95 $queryInfo = self
::getSelectQueryData();
96 $queryInfo['conds'] +
= [ 'log_id' => $id ];
97 $row = $db->selectRow(
102 $queryInfo['options'],
103 $queryInfo['join_conds']
108 return self
::newFromRow( $row );
111 /** @var stdClass Database result row. */
115 protected $performer;
117 /** @var array Parameters for log entry */
120 /** @var int A rev id associated to the log entry */
121 protected $revId = null;
123 /** @var bool Whether the parameters for this log entry are stored in new or old format. */
126 protected function __construct( $row ) {
131 * Returns the unique database id.
135 public function getId() {
136 return (int)$this->row
->log_id
;
140 * Returns whatever is stored in the database field.
144 protected function getRawParameters() {
145 return $this->row
->log_params
;
148 public function isLegacy() {
149 // This extracts the property
150 $this->getParameters();
151 return $this->legacy
;
154 public function getType() {
155 return $this->row
->log_type
;
158 public function getSubtype() {
159 return $this->row
->log_action
;
162 public function getParameters() {
163 if ( !isset( $this->params
) ) {
164 $blob = $this->getRawParameters();
165 Wikimedia\
suppressWarnings();
166 $params = LogEntryBase
::extractParams( $blob );
167 Wikimedia\restoreWarnings
();
168 if ( $params !== false ) {
169 $this->params
= $params;
170 $this->legacy
= false;
172 $this->params
= LogPage
::extractParams( $blob );
173 $this->legacy
= true;
176 if ( isset( $this->params
['associated_rev_id'] ) ) {
177 $this->revId
= $this->params
['associated_rev_id'];
178 unset( $this->params
['associated_rev_id'] );
182 return $this->params
;
185 public function getAssociatedRevId() {
186 // This extracts the property
187 $this->getParameters();
191 public function getPerformer() {
192 if ( !$this->performer
) {
193 $actorId = isset( $this->row
->log_actor
) ?
(int)$this->row
->log_actor
: 0;
194 $userId = (int)$this->row
->log_user
;
195 if ( $userId !== 0 ||
$actorId !== 0 ) {
197 if ( isset( $this->row
->user_name
) ) {
198 $this->performer
= User
::newFromRow( $this->row
);
199 } elseif ( $actorId !== 0 ) {
200 $this->performer
= User
::newFromActorId( $actorId );
202 $this->performer
= User
::newFromId( $userId );
206 $userText = $this->row
->log_user_text
;
207 $this->performer
= User
::newFromName( $userText, false );
211 return $this->performer
;
214 public function getTarget() {
215 $namespace = $this->row
->log_namespace
;
216 $page = $this->row
->log_title
;
217 return Title
::makeTitle( $namespace, $page );
220 public function getTimestamp() {
221 return wfTimestamp( TS_MW
, $this->row
->log_timestamp
);
224 public function getComment() {
225 return CommentStore
::getStore()->getComment( 'log_comment', $this->row
)->text
;
228 public function getDeleted() {
229 return $this->row
->log_deleted
;