Completed
Push — master ( 1fe2bc...e619ac )
by mw
02:21
created

src/DatabaseLogReader.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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;
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