Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
25.00% covered (danger)
25.00%
22 / 88
11.76% covered (danger)
11.76%
2 / 17
CRAP
0.00% covered (danger)
0.00%
0 / 1
DatabaseLogEntry
25.29% covered (danger)
25.29%
22 / 87
11.76% covered (danger)
11.76%
2 / 17
264.22
0.00% covered (danger)
0.00%
0 / 1
 getSelectQueryData
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 1
2
 newSelectQueryBuilder
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 newFromRow
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 newFromId
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRawParameters
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isLegacy
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSubtype
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getParameters
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
20
 getAssociatedRevId
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getPerformerIdentity
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
3
 getTarget
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getTimestamp
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getComment
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getDeleted
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Contains a class for dealing with database log entries
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @author Niklas Laxström
22 * @license GPL-2.0-or-later
23 * @since 1.19
24 */
25
26namespace MediaWiki\Logging;
27
28use InvalidArgumentException;
29use MediaWiki\Logger\LoggerFactory;
30use MediaWiki\MediaWikiServices;
31use MediaWiki\User\UserIdentity;
32use stdClass;
33use Wikimedia\AtEase\AtEase;
34use Wikimedia\Rdbms\IReadableDatabase;
35
36/**
37 * A value class to process existing log entries. In other words, this class caches a log
38 * entry from the database and provides an immutable object-oriented representation of it.
39 *
40 * This class should only be used in context of the LogFormatter class.
41 *
42 * @since 1.19
43 */
44class DatabaseLogEntry extends LogEntryBase {
45
46    /**
47     * Returns array of information that is needed for querying
48     * log entries. Array contains the following keys:
49     * tables, fields, conds, options and join_conds
50     *
51     * Since 1.34, log_user and log_user_text have not been present in the
52     * database, but they continue to be available in query results as
53     * aliases.
54     *
55     * @deprecated since 1.41 use ::newSelectQueryBuilder() instead
56     *
57     * @return array
58     */
59    public static function getSelectQueryData() {
60        $commentQuery = MediaWikiServices::getInstance()->getCommentStore()->getJoin( 'log_comment' );
61
62        $tables = array_merge(
63            [
64                'logging',
65                'logging_actor' => 'actor',
66                'user'
67            ],
68            $commentQuery['tables']
69        );
70        $fields = [
71            'log_id', 'log_type', 'log_action', 'log_timestamp',
72            'log_namespace', 'log_title', // unused log_page
73            'log_params', 'log_deleted',
74            'user_id',
75            'user_name',
76            'log_actor',
77            'log_user' => 'logging_actor.actor_user',
78            'log_user_text' => 'logging_actor.actor_name'
79        ] + $commentQuery['fields'];
80
81        $joins = [
82            'logging_actor' => [ 'JOIN', 'actor_id=log_actor' ],
83            // IPs don't have an entry in user table
84            'user' => [ 'LEFT JOIN', 'user_id=logging_actor.actor_user' ],
85        ] + $commentQuery['joins'];
86
87        return [
88            'tables' => $tables,
89            'fields' => $fields,
90            'conds' => [],
91            'options' => [],
92            'join_conds' => $joins,
93        ];
94    }
95
96    public static function newSelectQueryBuilder( IReadableDatabase $db ) {
97        return new LoggingSelectQueryBuilder( $db );
98    }
99
100    /**
101     * Constructs new LogEntry from database result row.
102     * Supports rows from both logging and recentchanges table.
103     *
104     * @param stdClass|array $row
105     * @return DatabaseLogEntry
106     */
107    public static function newFromRow( $row ) {
108        $row = (object)$row;
109        if ( isset( $row->rc_logid ) ) {
110            return new RCDatabaseLogEntry( $row );
111        }
112
113        return new self( $row );
114    }
115
116    /**
117     * Loads a LogEntry with the given id from database.
118     *
119     * @param int $id
120     * @param IReadableDatabase $db
121     * @return DatabaseLogEntry|null
122     */
123    public static function newFromId( $id, IReadableDatabase $db ) {
124        $row = self::newSelectQueryBuilder( $db )
125            ->where( [ 'log_id' => $id ] )
126            ->caller( __METHOD__ )->fetchRow();
127        if ( !$row ) {
128            return null;
129        }
130        return self::newFromRow( $row );
131    }
132
133    /** @var stdClass Database result row. */
134    protected $row;
135
136    /** @var UserIdentity */
137    protected $performer;
138
139    /** @var array|null Parameters for log entry */
140    protected $params;
141
142    /** @var int A rev id associated to the log entry */
143    protected $revId = null;
144
145    /** @var bool Whether the parameters for this log entry are stored in new or old format. */
146    protected $legacy;
147
148    protected function __construct( $row ) {
149        $this->row = $row;
150    }
151
152    /**
153     * Returns the unique database id.
154     *
155     * @return int
156     */
157    public function getId() {
158        return (int)( $this->row->log_id ?? 0 );
159    }
160
161    /**
162     * Returns whatever is stored in the database field (typically a serialized
163     * associative array but very old entries might have different formats).
164     *
165     * @return string
166     */
167    protected function getRawParameters() {
168        return $this->row->log_params;
169    }
170
171    public function isLegacy() {
172        // This extracts the property
173        $this->getParameters();
174        return $this->legacy;
175    }
176
177    public function getType() {
178        return $this->row->log_type;
179    }
180
181    public function getSubtype() {
182        return $this->row->log_action;
183    }
184
185    public function getParameters() {
186        if ( $this->params === null ) {
187            $blob = $this->getRawParameters();
188            AtEase::suppressWarnings();
189            $params = LogEntryBase::extractParams( $blob );
190            AtEase::restoreWarnings();
191            if ( $params !== false ) {
192                $this->params = $params;
193                $this->legacy = false;
194            } else {
195                $this->params = LogPage::extractParams( $blob );
196                $this->legacy = true;
197            }
198
199            if ( isset( $this->params['associated_rev_id'] ) ) {
200                $this->revId = $this->params['associated_rev_id'];
201                unset( $this->params['associated_rev_id'] );
202            }
203        }
204
205        return $this->params;
206    }
207
208    public function getAssociatedRevId() {
209        // This extracts the property
210        $this->getParameters();
211        return $this->revId;
212    }
213
214    public function getPerformerIdentity(): UserIdentity {
215        if ( !$this->performer ) {
216            $actorStore = MediaWikiServices::getInstance()->getActorStore();
217            try {
218                $this->performer = $actorStore->newActorFromRowFields(
219                    $this->row->user_id ?? 0,
220                    $this->row->log_user_text ?? null,
221                    $this->row->log_actor ?? null
222                );
223            } catch ( InvalidArgumentException $e ) {
224                LoggerFactory::getInstance( 'logentry' )->warning(
225                    'Failed to instantiate log entry performer', [
226                        'exception' => $e,
227                        'log_id' => $this->getId()
228                    ]
229                );
230                $this->performer = $actorStore->getUnknownActor();
231            }
232        }
233        return $this->performer;
234    }
235
236    public function getTarget() {
237        $namespace = $this->row->log_namespace;
238        $page = $this->row->log_title;
239        return MediaWikiServices::getInstance()->getTitleFactory()->makeTitle( $namespace, $page );
240    }
241
242    public function getTimestamp() {
243        return wfTimestamp( TS_MW, $this->row->log_timestamp );
244    }
245
246    public function getComment() {
247        return MediaWikiServices::getInstance()->getCommentStore()
248            ->getComment( 'log_comment', $this->row )->text;
249    }
250
251    public function getDeleted() {
252        return $this->row->log_deleted;
253    }
254}
255
256/** @deprecated class alias since 1.44 */
257class_alias( DatabaseLogEntry::class, 'DatabaseLogEntry' );