Completed
Pull Request — master (#98)
by Mark A.
06:25
created

DatabaseLogReader   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 18
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 112
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A clearCache() 0 3 1
A init() 0 18 3
A getLog() 0 17 3
A getQuery() 0 3 1
A getUserForLogEntry() 0 7 3
A getDateOfLogEntry() 0 7 3
A getStatusOfLogEntry() 0 7 3
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
    public function clearCache() {
33
        self::$titleCache = [];
34
    }
35
36
    /**
37
	 * Take care of loading from the cache or filling the query.
38
	 */
39
	private function init() {
40
		if ( !$this->query ) {
41
			if ( !isset( self::$titleCache[ $this->titleKey ] ) ) {
42
				$this->query = DatabaseLogEntry::getSelectQueryData();
43
44
				$this->query['conds'] = [
45
					'log_type' => $this->type,
46
					'log_title' => $this->titleKey
47
				];
48
				$this->query['options'] = [ 'ORDER BY' => 'log_timestamp desc' ];
49
				self::$titleCache[ $this->titleKey ] = $this;
50
			} else {
51
				$cache = self::$titleCache[ $this->titleKey ];
52
				$this->query = $cache->getQuery();
53
				$this->log = $cache->getLog();
54
			}
55
		}
56
	}
57
58
	/**
59
	 * Fetch the results using our conditions
60
	 *
61
	 * @return IResultWrapper
62
	 * @throws DBError
63
	 */
64
	private function getLog() {
65
		if ( !$this->log ) {
66
            $query = $this->getQuery();
67
			$this->log = $this->dbr->select(
68
				$query['tables'], $query['fields'], $query['conds'],
69
				__METHOD__, $query['options'], $query['join_conds']
70
			);
71
			if ( $this->log === null ) {
72
				$this->log = new ArrayIterator( [ (object)[
73
					'user_id' => null,
74
					'log_timestamp' => null,
75
					'log_action' => null
76
				] ] );
77
			}
78
		}
79
		return $this->log;
80
	}
81
82
	/**
83
	 * Fetch the query parameters for later calls
84
	 *
85
	 * @return array of parameters for SELECT call
86
	 */
87
	public function getQuery() {
88
		return $this->query;
89
	}
90
91
	/**
92
	 * @return User
93
	 */
94
	public function getUserForLogEntry() {
95
		$this->init();
96
		$logLine = $this->getLog()->current();
97
		if ( $logLine && $logLine->user_id ) {
98
			return User::newFromID( $logLine->user_id );
99
		}
100
	}
101
102
	/**
103
	 * @return Timestamp
104
	 */
105
	public function getDateOfLogEntry() {
106
		$this->init();
107
		$logLine = $this->getLog()->current();
108
		if ( $logLine && $logLine->log_timestamp ) {
109
			return new MWTimestamp( $logLine->log_timestamp );
110
		}
111
	}
112
113
	/**
114
	 * @return string
115
	 */
116
	public function getStatusOfLogEntry() {
117
		$this->init();
118
		$logLine = $this->getLog()->current();
119
		if ( $logLine && $logLine->log_action ) {
120
			return $logLine->log_action;
121
		}
122
	}
123
}
124