DatabaseLogReader::clearCache()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace SESP;
4
5
use ArrayIterator;
6
use DatabaseLogEntry;
7
use DatabaseBase;
8
use MWTimestamp;
9
use Title;
10
use User;
11
12
class DatabaseLogReader {
13
14
	/**
15
	 * @var array
16
	 */
17
	private static $titleCache = [];
18
19
	/**
20
	 * @var DatabaseBase
21
	 */
22
	private $dbr;
23
24
	/**
25
	 * @var string
26
	 */
27
	private $query;
28
29
	/**
30
	 * @var string
31
	 */
32
	private $log;
33
34
	/**
35
	 * @var string
36
	 */
37
	private $dbKey;
38
39
	/**
40
	 * @var string
41
	 */
42
	private $type;
43
44
	/**
45
	 * @since 2.0
46
	 *
47
	 * @param DatabaseaBase $dbr injected connection
48
	 * @param Title|null $title
49
	 * @param string $type of log (default: approval)
50
	 */
51 6
	public function __construct( $dbr, Title $title = null , $type = 'approval' ) {
52 6
		
53 6
		// Due to MW 1.31+ and MW 1.34+
54 6
		if (
55 6
			!$dbr instanceof \Wikimedia\Rdbms\IDatabase &&
0 ignored issues
show
Bug introduced by
The class Wikimedia\Rdbms\IDatabase does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
56
			!$dbr instanceof \IDatabase &&
0 ignored issues
show
Bug introduced by
The class IDatabase does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
57
			!$dbr instanceof \DatabaseBase ) {
0 ignored issues
show
Bug introduced by
The class DatabaseBase does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
58
			throw new \RuntimeException( "Invalid connection instance!" );
59
		}
60 1
61 1
		$this->dbr = $dbr;
62 1
		$this->dbKey = $title instanceof Title ? $title->getDBkey() : null;
0 ignored issues
show
Bug introduced by
The class Title does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
63
		$this->type = $type;
64
	}
65
66
	/**
67
	 * @since 2.0
68
	 */
69
	public function clearCache() {
70
		self::$titleCache = [];
71 5
	}
72 5
73
	/**
74
	 * Fetch the query parameters for later calls
75
	 *
76
	 * @since 2.0
77
	 *
78
	 * @return array of parameters for SELECT call
79
	 */
80 4
	public function getQuery() {
81 4
		return $this->query;
82 4
	}
83
84 4
	/**
85 2
	 * @since 2.0
86
	 *
87 2
	 * @return User
88
	 */
89
	public function getUserForLogEntry() {
90
		$this->init();
91
		$logLine = $this->getLog()->current();
92
93
		if ( $logLine && $logLine->user_id ) {
94 2
			return User::newFromID( $logLine->user_id );
95 2
		}
96 2
	}
97
98 2
	/**
99 1
	 * @since 2.0
100
	 *
101 1
	 * @return Timestamp
102
	 */
103
	public function getDateOfLogEntry() {
104
		$this->init();
105
		$logLine = $this->getLog()->current();
106
107
		if ( $logLine && $logLine->log_timestamp ) {
108 2
			return new MWTimestamp( $logLine->log_timestamp );
109 2
		}
110 2
	}
111
112 2
	/**
113 1
	 * @since 2.0
114
	 *
115 1
	 * @return string
116
	 */
117
	public function getStatusOfLogEntry() {
118
		$this->init();
119
		$logLine = $this->getLog()->current();
120 4
121
		if ( $logLine && $logLine->log_action ) {
122 4
			return $logLine->log_action;
123 3
		}
124
	}
125
126 4
	/**
127 3
	 * Take care of loading from the cache or filling the query.
128
	 */
129 3
	private function init() {
130 3
131 3
		if ( $this->query ) {
132 3
			return;
133 3
		}
134 3
135 3
		if ( !isset( self::$titleCache[ $this->dbKey ] ) ) {
136 1
			$this->query = DatabaseLogEntry::getSelectQueryData();
137 1
138 1
			$this->query['conds'] = [
139
				'log_type' => $this->type,
140
				'log_title' => $this->dbKey
141 4
			];
142
			$this->query['options'] = [ 'ORDER BY' => 'log_timestamp desc' ];
143
			self::$titleCache[ $this->dbKey ] = $this;
144
		} else {
145
			$cache = self::$titleCache[ $this->dbKey ];
146
			$this->query = $cache->getQuery();
147
			$this->log = $cache->getLog();
148
		}
149 4
150 4
	}
151
152 3
	/**
153
	 * Fetch the results using our conditions
154 3
	 *
155 3
	 * @return IResultWrapper
156 3
	 * @throws DBError
157 3
	 */
158 3
	private function getLog() {
159 3
		if ( !$this->log ) {
160 3
161 3
			$query = $this->getQuery();
162
163 3
			$this->log = $this->dbr->select(
164
				$query['tables'],
165
				$query['fields'],
166
				$query['conds'],
167
				__METHOD__,
168
				$query['options'],
169
				$query['join_conds']
170
			);
171
172 3
			if ( $this->log === null ) {
173
				$this->log = new ArrayIterator(
0 ignored issues
show
Documentation Bug introduced by
It seems like new \ArrayIterator(array...'log_action' => null))) of type object<ArrayIterator> is incompatible with the declared type string of property $log.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
174 4
					[ (object)[
175
						'user_id' => null,
176
						'log_timestamp' => null,
177
						'log_action' => null
178
					] ]
179
				);
180
			}
181
		}
182
183
		return $this->log;
184
	}
185
186
}
187