Completed
Push — master ( 756515...e0d7bf )
by Jeroen De
08:10 queued 08:07
created

SimpleSubPageFinder::setIncludeRedirects()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 5
cp 0.8
crap 2.032
rs 9.4285
1
<?php
2
3
namespace SubPageList\Lister;
4
5
use InvalidArgumentException;
6
use SubPageList\Counter\SubPageCounter;
7
use SubPageList\DBConnectionProvider;
8
use Title;
9
use TitleArray;
10
11
/**
12
 * Simple subpage finder and counter that uses a like query
13
 * on the page table to find subpages.
14
 *
15
 * @since 1.2
16
 *
17
 * @licence GNU GPL v2+
18
 * @author Jeroen De Dauw < [email protected] >
19
 */
20
class SimpleSubPageFinder implements SubPageFinder, SubPageCounter {
21
22
	const OPT_INCLUDE_REDIRECTS = 'redirects';
23
	const OPT_LIMIT = 'limit';
24
	const OPT_OFFSET = 'offset';
25
26
	/**
27
	 * @var DBConnectionProvider
28
	 */
29
	private $connectionProvider;
30
31
	/**
32
	 * @var array
33
	 */
34
	private $options;
35
36
	/**
37
	 * @since 1.2
38
	 *
39
	 * @param DBConnectionProvider $connectionProvider
40
	 */
41 25
	public function __construct( DBConnectionProvider $connectionProvider ) {
42 25
		$this->connectionProvider = $connectionProvider;
43
44 25
		$this->options = [
45 25
			self::OPT_INCLUDE_REDIRECTS => false,
46 25
			self::OPT_LIMIT => 500,
47 25
			self::OPT_OFFSET => 0,
48
		];
49 25
	}
50
51
	/**
52
	 * @param string $option
53
	 * @param mixed $value
54
	 */
55 17
	private function setOption( $option, $value ) {
56 17
		$this->options[$option] = $value;
57 17
	}
58
59
	/**
60
	 * @see SubPageFinder::setLimit
61
	 *
62
	 * @since 1.2
63
	 *
64
	 * @param int $limit
65
	 *
66
	 * @throws InvalidArgumentException
67
	 */
68 17
	public function setLimit( $limit ) {
69 17
		if ( !is_int( $limit ) || $limit < 1 ) {
70
			throw new InvalidArgumentException( '$limit needs to be an int bigger than 0' );
71
		}
72
73 17
		$this->setOption( self::OPT_LIMIT, $limit );
74 17
	}
75
	
76
	/**
77
	 * @see SubPageFinder::setIncludeRedirects
78
	 *
79
	 * @since 1.4.0
80
	 *
81
	 * @param bool $includeRedirects
82
	 *
83
	 * @throws InvalidArgumentException
84
	 */
85 17
	public function setIncludeRedirects( $includeRedirects ) {
86 17
		if ( !is_bool( $includeRedirects ) ) {
87
			throw new InvalidArgumentException( '$includeRedirects needs to be boolean' );
88
		}
89
90 17
		$this->setOption( self::OPT_INCLUDE_REDIRECTS, $includeRedirects );
91 17
	}
92
	
93
	/**
94
	 * @see SubPageFinder::setOffset
95
	 *
96
	 * @since 1.2
97
	 *
98
	 * @param int $offset
99
	 *
100
	 * @throws InvalidArgumentException
101
	 */
102
	public function setOffset( $offset ) {
103
		if ( !is_int( $offset ) || $offset < 0 ) {
104
			throw new InvalidArgumentException( '$limit needs to be a positive int' );
105
		}
106
107
		$this->setOption( self::OPT_OFFSET, $offset );
108
	}
109
110
	/**
111
	 * @see SubPageFinder::getSubPagesFor
112
	 *
113
	 * @since 1.2
114
	 *
115
	 * @param Title $title
116
	 *
117
	 * @return Title[]
118
	 */
119 20
	public function getSubPagesFor( Title $title ) {
120
		/**
121
		 * @var \DatabaseBase $dbr
122
		 */
123 20
		$dbr = $this->connectionProvider->getConnection();
124
125 20
		$titleArray = TitleArray::newFromResult(
126 20
			$dbr->select( 'page',
127 20
				[ 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' ],
128 20
				$this->getConditions( $title ),
129 20
				__METHOD__,
130 20
				$this->getOptions()
131 20
			)
132 20
		);
133
134 20
		$this->connectionProvider->releaseConnection();
135
136 20
		return iterator_to_array( $titleArray );
137
	}
138
139
	/**
140
	 * @see SubPageCounter::countSubPages
141
	 *
142
	 * @since 1.2
143
	 *
144
	 * @param Title $title
145
	 *
146
	 * @return integer
147
	 */
148 4
	public function countSubPages( Title $title ) {
149
		/**
150
		 * @var \DatabaseBase $dbr
151
		 */
152 4
		$dbr = $this->connectionProvider->getConnection();
153
154 4
		$res = $dbr->selectRow(
155 4
			'page',
156 4
			'COUNT(*) AS rowcount',
157 4
			$this->getConditions( $title ),
158 4
			__METHOD__,
159 4
			$this->getOptions()
160 4
		);
161
162 4
		if ( is_object( $res ) ) {
163 4
			return (int)$res->rowcount;
164
		}
165
166
		return 0;
167
	}
168
169
	/**
170
	 * @since 1.2
171
	 *
172
	 * @param Title $title
173
	 *
174
	 * @return array
175
	 */
176 24
	private function getConditions( Title $title ) {
177
		/**
178
		 * @var \DatabaseBase $dbr
179
		 */
180 24
		$dbr = $this->connectionProvider->getConnection();
181
182
		$conditions = [
183 24
			'page_namespace' => $title->getNamespace(),
184 24
			'page_title'  . $dbr->buildLike( $title->getDBkey() . '/', $dbr->anyString() )
185 24
		];
186
187 24
		if ( !$this->options[self::OPT_INCLUDE_REDIRECTS] ) {
188 24
			$conditions['page_is_redirect'] = 0;
189 24
		}
190
191 24
		return $conditions;
192
	}
193
194
	/**
195
	 * @since 1.2
196
	 *
197
	 * @return array
198
	 */
199 24
	private function getOptions() {
200 24
		$options = [];
201
202 24
		$options['LIMIT'] = (int)$this->options[self::OPT_LIMIT];
203
204 24
		if ( $this->options[self::OPT_OFFSET] > 0 ) {
205
			$options['OFFSET'] = (int)$this->options[self::OPT_OFFSET];
206
		}
207
208 24
		return $options;
209
	}
210
211
}
212