1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
<?php
/**
* Contains a class for dealing with database log entries
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @author Niklas Laxström
* @license GPL-2.0-or-later
* @since 1.19
*/
use Wikimedia\Rdbms\IDatabase;
/**
* A value class to process existing log entries. In other words, this class caches a log
* entry from the database and provides an immutable object-oriented representation of it.
*
* This class should only be used in context of the LogFormatter class.
*
* @since 1.19
*/
class DatabaseLogEntry extends LogEntryBase {
/**
* Returns array of information that is needed for querying
* log entries. Array contains the following keys:
* tables, fields, conds, options and join_conds
*
* @return array
*/
public static function getSelectQueryData() {
$commentQuery = CommentStore::getStore()->getJoin( 'log_comment' );
$actorQuery = ActorMigration::newMigration()->getJoin( 'log_user' );
$tables = array_merge(
[ 'logging' ], $commentQuery['tables'], $actorQuery['tables'], [ 'user' ]
);
$fields = [
'log_id', 'log_type', 'log_action', 'log_timestamp',
'log_namespace', 'log_title', // unused log_page
'log_params', 'log_deleted',
'user_id', 'user_name', 'user_editcount',
] + $commentQuery['fields'] + $actorQuery['fields'];
$joins = [
// IPs don't have an entry in user table
'user' => [ 'LEFT JOIN', 'user_id=' . $actorQuery['fields']['log_user'] ],
] + $commentQuery['joins'] + $actorQuery['joins'];
return [
'tables' => $tables,
'fields' => $fields,
'conds' => [],
'options' => [],
'join_conds' => $joins,
];
}
/**
* Constructs new LogEntry from database result row.
* Supports rows from both logging and recentchanges table.
*
* @param stdClass|array $row
* @return DatabaseLogEntry
*/
public static function newFromRow( $row ) {
$row = (object)$row;
if ( isset( $row->rc_logid ) ) {
return new RCDatabaseLogEntry( $row );
} else {
return new self( $row );
}
}
/**
* Loads a LogEntry with the given id from database
*
* @param int $id
* @param IDatabase $db
* @return DatabaseLogEntry|null
*/
public static function newFromId( $id, IDatabase $db ) {
$queryInfo = self::getSelectQueryData();
$queryInfo['conds'] += [ 'log_id' => $id ];
$row = $db->selectRow(
$queryInfo['tables'],
$queryInfo['fields'],
$queryInfo['conds'],
__METHOD__,
$queryInfo['options'],
$queryInfo['join_conds']
);
if ( !$row ) {
return null;
}
return self::newFromRow( $row );
}
/** @var stdClass Database result row. */
protected $row;
/** @var User */
protected $performer;
/** @var array Parameters for log entry */
protected $params;
/** @var int A rev id associated to the log entry */
protected $revId = null;
/** @var bool Whether the parameters for this log entry are stored in new or old format. */
protected $legacy;
protected function __construct( $row ) {
$this->row = $row;
}
/**
* Returns the unique database id.
*
* @return int
*/
public function getId() {
return (int)$this->row->log_id;
}
/**
* Returns whatever is stored in the database field.
*
* @return string
*/
protected function getRawParameters() {
return $this->row->log_params;
}
public function isLegacy() {
// This extracts the property
$this->getParameters();
return $this->legacy;
}
public function getType() {
return $this->row->log_type;
}
public function getSubtype() {
return $this->row->log_action;
}
public function getParameters() {
if ( !isset( $this->params ) ) {
$blob = $this->getRawParameters();
Wikimedia\suppressWarnings();
$params = LogEntryBase::extractParams( $blob );
Wikimedia\restoreWarnings();
if ( $params !== false ) {
$this->params = $params;
$this->legacy = false;
} else {
$this->params = LogPage::extractParams( $blob );
$this->legacy = true;
}
if ( isset( $this->params['associated_rev_id'] ) ) {
$this->revId = $this->params['associated_rev_id'];
unset( $this->params['associated_rev_id'] );
}
}
return $this->params;
}
public function getAssociatedRevId() {
// This extracts the property
$this->getParameters();
return $this->revId;
}
public function getPerformer() {
if ( !$this->performer ) {
$actorId = isset( $this->row->log_actor ) ? (int)$this->row->log_actor : 0;
$userId = (int)$this->row->log_user;
if ( $userId !== 0 || $actorId !== 0 ) {
// logged-in users
if ( isset( $this->row->user_name ) ) {
$this->performer = User::newFromRow( $this->row );
} elseif ( $actorId !== 0 ) {
$this->performer = User::newFromActorId( $actorId );
} else {
$this->performer = User::newFromId( $userId );
}
} else {
// IP users
$userText = $this->row->log_user_text;
$this->performer = User::newFromName( $userText, false );
}
}
return $this->performer;
}
public function getTarget() {
$namespace = $this->row->log_namespace;
$page = $this->row->log_title;
return Title::makeTitle( $namespace, $page );
}
public function getTimestamp() {
return wfTimestamp( TS_MW, $this->row->log_timestamp );
}
public function getComment() {
return CommentStore::getStore()->getComment( 'log_comment', $this->row )->text;
}
public function getDeleted() {
return $this->row->log_deleted;
}
}
|