Completed
Pull Request — master (#98)
by Mark A.
02:24
created

DatabaseLogReader::getDateOfLogEntry()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
crap 6
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
	private $query;
16
	private $log;
17
	private $dbr;
18
	private $titleKey;
19
	private $type;
20
21
	/**
22
	 * @param DatabaseaBase $dbr injected connection
23
	 * @param string $titleKey from page name
24
	 * @param string $type of log (default: approval)
25
	 */
26
	public function __construct( DatabaseBase $dbr, $titleKey, $type = 'approval' ) {
27
		$this->dbr = $dbr;
28
		$this->titleKey = $titleKey;
29
		$this->type = $type;
30
	}
31
32
	/**
33
	 * Take care of loading from the cache or filling the query.
34
	 */
35
	private function init() {
36
		if ( !$this->query ) {
37
			if ( !isset( self::$titleCache[ $this->titleKey ] ) ) {
38
				$this->query = DatabaseLogEntry::getSelectQueryData();
39
40
				$this->query['conds'] = [
41
					'log_type' => $this->type,
42
					'log_title' => $this->titleKey
43
				];
44
				$this->query['options'] = [ 'ORDER BY' => 'log_timestamp desc' ];
45
				self::$titleCache[ $this->titleKey ] = $this;
46
			} elseif ( $this->query ) {
47
				$cache = self::$titleCache[ $this->titleKey ];
48
				$this->query = $cache->getQuery();
49
				$this->log = $cache->getLog();
50
			}
51
		}
52
	}
53
54
	/**
55
	 * Fetch the results using our conditions
56
	 *
57
	 * @return IResultWrapper
58
	 * @throws DBError
59
	 */
60
	private function getLog() {
61
		if ( !$this->log ) {
62
			$this->log = $this->dbr->select(
63
				$this->query['tables'], $this->query['fields'], $this->query['conds'],
64
				__METHOD__, $this->query['options'], $this->query['join_conds']
65
			);
66
			if ( $this->log === null ) {
67
				$this->log = new ArrayIterator( [
68
					'user_id' => null,
69
					'log_timestamp' => null,
70
					'log_action' => null
71
				] );
72
			}
73
		}
74
		return $this->log;
75
	}
76
77
	/**
78
	 * Fetch the query parameters for later calls
79
	 *
80
	 * @return array of parameters for SELECT call
81
	 */
82
	public function getQuery() {
83
		return $this->query;
84
	}
85
86
	/**
87
	 * @return User
88
	 */
89
	public function getUserForLogEntry() {
90
		$this->init();
91
		$logLine = $this->getLog()->current();
92
		if ( $logLine ) {
93
			return User::newFromID( $logLine->user_id );
94
		}
95
	}
96
97
	/**
98
	 * @return Timestamp
99
	 */
100
	public function getDateOfLogEntry() {
101
		$this->init();
102
		$logLine = $this->getLog()->current();
103
		if ( $logLine ) {
104
			return new MWTimestamp( $logLine->log_timestamp );
105
		}
106
	}
107
108
	/**
109
	 * @return string
110
	 */
111
	public function getStatusOfLogEntry() {
112
		$this->init();
113
		$logLine = $this->getLog()->current();
114
		if ( $logLine ) {
115
			return $logLine->log_action;
116
		}
117
	}
118
}
119