Completed
Push — master ( 1811ff...1fe2bc )
by mw
08:02
created

DatabaseLogReader::getLog()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 3
eloc 16
nc 3
nop 0
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
	private static $titleCache = [];
15
16
	/**
17
	 * @var DatabaseBase
18
	 */
19
	private $dbr;
20
21
	/**
22
	 * @var string
23
	 */
24
	private $query;
25
26
	/**
27
	 * @var string
28
	 */
29
	private $log;
30
31
	/**
32
	 * @var string
33
	 */
34
	private $dbKey;
35
36
	/**
37
	 * @var string
38
	 */
39
	private $type;
40
41
	/**
42
	 * @param DatabaseaBase $dbr injected connection
43
	 * @param string $dbKey from page name
0 ignored issues
show
Bug introduced by
There is no parameter named $dbKey. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
44
	 * @param string $type of log (default: approval)
45
	 */
46
	public function __construct( DatabaseBase $dbr, Title $title = null , $type = 'approval' ) {
47
		$this->dbr = $dbr;
48
		$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...
49
		$this->type = $type;
50
	}
51
52
    public function clearCache() {
53
        self::$titleCache = [];
54
    }
55
56
    /**
57
	 * Take care of loading from the cache or filling the query.
58
	 */
59
	private function init() {
60
61
		if ( $this->query ) {
62
			return;
63
		}
64
65
		if ( !isset( self::$titleCache[ $this->dbKey ] ) ) {
66
			$this->query = DatabaseLogEntry::getSelectQueryData();
67
68
			$this->query['conds'] = [
69
				'log_type' => $this->type,
70
				'log_title' => $this->dbKey
71
			];
72
			$this->query['options'] = [ 'ORDER BY' => 'log_timestamp desc' ];
73
			self::$titleCache[ $this->dbKey ] = $this;
74
		} else {
75
			$cache = self::$titleCache[ $this->dbKey ];
76
			$this->query = $cache->getQuery();
77
			$this->log = $cache->getLog();
78
		}
79
80
	}
81
82
	/**
83
	 * Fetch the results using our conditions
84
	 *
85
	 * @return IResultWrapper
86
	 * @throws DBError
87
	 */
88
	private function getLog() {
89
		if ( !$this->log ) {
90
91
            $query = $this->getQuery();
92
93
			$this->log = $this->dbr->select(
94
				$query['tables'],
95
				$query['fields'],
96
				$query['conds'],
97
				__METHOD__,
98
				$query['options'],
99
				$query['join_conds']
100
			);
101
102
			if ( $this->log === null ) {
103
				$this->log = new ArrayIterator( [ (object)[
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...
104
					'user_id' => null,
105
					'log_timestamp' => null,
106
					'log_action' => null
107
				] ] );
108
			}
109
		}
110
111
		return $this->log;
112
	}
113
114
	/**
115
	 * Fetch the query parameters for later calls
116
	 *
117
	 * @return array of parameters for SELECT call
118
	 */
119
	public function getQuery() {
120
		return $this->query;
121
	}
122
123
	/**
124
	 * @return User
125
	 */
126
	public function getUserForLogEntry() {
127
		$this->init();
128
		$logLine = $this->getLog()->current();
129
130
		if ( $logLine && $logLine->user_id ) {
131
			return User::newFromID( $logLine->user_id );
132
		}
133
	}
134
135
	/**
136
	 * @return Timestamp
137
	 */
138
	public function getDateOfLogEntry() {
139
		$this->init();
140
		$logLine = $this->getLog()->current();
141
142
		if ( $logLine && $logLine->log_timestamp ) {
143
			return new MWTimestamp( $logLine->log_timestamp );
144
		}
145
	}
146
147
	/**
148
	 * @return string
149
	 */
150
	public function getStatusOfLogEntry() {
151
		$this->init();
152
		$logLine = $this->getLog()->current();
153
154
		if ( $logLine && $logLine->log_action ) {
155
			return $logLine->log_action;
156
		}
157
	}
158
}
159