@@ -15,200 +15,200 @@ |
||
15 | 15 | |
16 | 16 | abstract class SearchHelperBase |
17 | 17 | { |
18 | - /** @var PdoDatabase */ |
|
19 | - protected $database; |
|
20 | - /** @var array */ |
|
21 | - protected $parameterList = array(); |
|
22 | - /** @var null|int */ |
|
23 | - private $limit = null; |
|
24 | - /** @var null|int */ |
|
25 | - private $offset = null; |
|
26 | - private $orderBy = null; |
|
27 | - /** |
|
28 | - * @var string The where clause. |
|
29 | - * |
|
30 | - * (the 1=1 condition will be optimised out of the query by the query planner, and simplifies our code here). Note |
|
31 | - * that we use positional parameters instead of named parameters because we don't know many times different options |
|
32 | - * will be called (looking at excluding() here, but there's the option for others). |
|
33 | - */ |
|
34 | - protected $whereClause = ' WHERE 1 = 1'; |
|
35 | - /** @var string */ |
|
36 | - protected $table; |
|
37 | - protected $joinClause = ''; |
|
38 | - private $targetClass; |
|
39 | - |
|
40 | - /** |
|
41 | - * SearchHelperBase constructor. |
|
42 | - * |
|
43 | - * @param PdoDatabase $database |
|
44 | - * @param string $table |
|
45 | - * @param $targetClass |
|
46 | - * @param null|string $order Order by clause, excluding ORDER BY. |
|
47 | - */ |
|
48 | - protected function __construct(PdoDatabase $database, $table, $targetClass, $order = null) |
|
49 | - { |
|
50 | - $this->database = $database; |
|
51 | - $this->table = $table; |
|
52 | - $this->orderBy = $order; |
|
53 | - $this->targetClass = $targetClass; |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Finalises the database query, and executes it, returning a set of objects. |
|
58 | - * |
|
59 | - * @return DataObject[] |
|
60 | - */ |
|
61 | - public function fetch() |
|
62 | - { |
|
63 | - $statement = $this->getData(); |
|
64 | - |
|
65 | - /** @var DataObject[] $returnedObjects */ |
|
66 | - $returnedObjects = $statement->fetchAll(PDO::FETCH_CLASS, $this->targetClass); |
|
67 | - foreach ($returnedObjects as $req) { |
|
68 | - $req->setDatabase($this->database); |
|
69 | - } |
|
70 | - |
|
71 | - return $returnedObjects; |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Finalises the database query, and executes it, returning only the requested column. |
|
76 | - * |
|
77 | - * @param string $column The required column |
|
78 | - * @return array |
|
79 | - */ |
|
80 | - public function fetchColumn($column){ |
|
81 | - $statement = $this->getData(array($column)); |
|
82 | - |
|
83 | - return $statement->fetchAll(PDO::FETCH_COLUMN); |
|
84 | - } |
|
85 | - |
|
86 | - public function fetchMap($column){ |
|
87 | - $statement = $this->getData(array('id', $column)); |
|
88 | - |
|
89 | - $data = $statement->fetchAll(PDO::FETCH_ASSOC); |
|
90 | - $map = array(); |
|
91 | - |
|
92 | - foreach ($data as $row) { |
|
93 | - $map[$row['id']] = $row[$column]; |
|
94 | - } |
|
95 | - |
|
96 | - return $map; |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * @param int $count Returns the record count of the result set |
|
101 | - * |
|
102 | - * @return $this |
|
103 | - */ |
|
104 | - public function getRecordCount(&$count) |
|
105 | - { |
|
106 | - $query = 'SELECT /* SearchHelper */ COUNT(*) FROM ' . $this->table . ' origin '; |
|
107 | - $query .= $this->joinClause . $this->whereClause; |
|
108 | - |
|
109 | - $statement = $this->database->prepare($query); |
|
110 | - $statement->execute($this->parameterList); |
|
111 | - |
|
112 | - $count = $statement->fetchColumn(0); |
|
113 | - $statement->closeCursor(); |
|
114 | - |
|
115 | - return $this; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * Limits the results |
|
120 | - * |
|
121 | - * @param integer $limit |
|
122 | - * @param integer|null $offset |
|
123 | - * |
|
124 | - * @return $this |
|
125 | - * |
|
126 | - */ |
|
127 | - public function limit($limit, $offset = null) |
|
128 | - { |
|
129 | - $this->limit = $limit; |
|
130 | - $this->offset = $offset; |
|
131 | - |
|
132 | - return $this; |
|
133 | - } |
|
134 | - |
|
135 | - private function applyLimit() |
|
136 | - { |
|
137 | - $clause = ''; |
|
138 | - if ($this->limit !== null) { |
|
139 | - $clause = ' LIMIT ?'; |
|
140 | - $this->parameterList[] = $this->limit; |
|
141 | - |
|
142 | - if ($this->offset !== null) { |
|
143 | - $clause .= ' OFFSET ?'; |
|
144 | - $this->parameterList[] = $this->offset; |
|
145 | - } |
|
146 | - } |
|
147 | - |
|
148 | - return $clause; |
|
149 | - } |
|
150 | - |
|
151 | - private function applyOrder() |
|
152 | - { |
|
153 | - if ($this->orderBy !== null) { |
|
154 | - return ' ORDER BY ' . $this->orderBy; |
|
155 | - } |
|
156 | - |
|
157 | - return ''; |
|
158 | - } |
|
159 | - |
|
160 | - /** |
|
161 | - * @param array $columns |
|
162 | - * |
|
163 | - * @return PDOStatement |
|
164 | - */ |
|
165 | - private function getData($columns = array('*')) |
|
166 | - { |
|
167 | - $query = $this->buildQuery($columns); |
|
168 | - $query .= $this->applyOrder(); |
|
169 | - $query .= $this->applyLimit(); |
|
170 | - |
|
171 | - $statement = $this->database->prepare($query); |
|
172 | - $statement->execute($this->parameterList); |
|
173 | - |
|
174 | - return $statement; |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * @param array $columns |
|
179 | - * |
|
180 | - * @return string |
|
181 | - */ |
|
182 | - protected function buildQuery($columns) |
|
183 | - { |
|
184 | - $colData = array(); |
|
185 | - foreach ($columns as $c) { |
|
186 | - $colData[] = 'origin.' . $c; |
|
187 | - } |
|
188 | - |
|
189 | - $query = 'SELECT /* SearchHelper */ ' . implode(', ', $colData) . ' FROM ' . $this->table . ' origin '; |
|
190 | - $query .= $this->joinClause . $this->whereClause; |
|
191 | - |
|
192 | - return $query; |
|
193 | - } |
|
194 | - |
|
195 | - public function inIds($idList) { |
|
196 | - $this->inClause('id', $idList); |
|
197 | - return $this; |
|
198 | - } |
|
199 | - |
|
200 | - protected function inClause($column, $values) { |
|
201 | - if (count($values) === 0) { |
|
202 | - return; |
|
203 | - } |
|
204 | - |
|
205 | - // Urgh. OK. You can't use IN() with parameters directly, so let's munge something together. |
|
206 | - $valueCount = count($values); |
|
207 | - |
|
208 | - // Firstly, let's create a string of question marks, which will do as positional parameters. |
|
209 | - $inSection = str_repeat('?,', $valueCount - 1) . '?'; |
|
210 | - |
|
211 | - $this->whereClause .= " AND {$column} IN ({$inSection})"; |
|
212 | - $this->parameterList = array_merge($this->parameterList, $values); |
|
213 | - } |
|
18 | + /** @var PdoDatabase */ |
|
19 | + protected $database; |
|
20 | + /** @var array */ |
|
21 | + protected $parameterList = array(); |
|
22 | + /** @var null|int */ |
|
23 | + private $limit = null; |
|
24 | + /** @var null|int */ |
|
25 | + private $offset = null; |
|
26 | + private $orderBy = null; |
|
27 | + /** |
|
28 | + * @var string The where clause. |
|
29 | + * |
|
30 | + * (the 1=1 condition will be optimised out of the query by the query planner, and simplifies our code here). Note |
|
31 | + * that we use positional parameters instead of named parameters because we don't know many times different options |
|
32 | + * will be called (looking at excluding() here, but there's the option for others). |
|
33 | + */ |
|
34 | + protected $whereClause = ' WHERE 1 = 1'; |
|
35 | + /** @var string */ |
|
36 | + protected $table; |
|
37 | + protected $joinClause = ''; |
|
38 | + private $targetClass; |
|
39 | + |
|
40 | + /** |
|
41 | + * SearchHelperBase constructor. |
|
42 | + * |
|
43 | + * @param PdoDatabase $database |
|
44 | + * @param string $table |
|
45 | + * @param $targetClass |
|
46 | + * @param null|string $order Order by clause, excluding ORDER BY. |
|
47 | + */ |
|
48 | + protected function __construct(PdoDatabase $database, $table, $targetClass, $order = null) |
|
49 | + { |
|
50 | + $this->database = $database; |
|
51 | + $this->table = $table; |
|
52 | + $this->orderBy = $order; |
|
53 | + $this->targetClass = $targetClass; |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Finalises the database query, and executes it, returning a set of objects. |
|
58 | + * |
|
59 | + * @return DataObject[] |
|
60 | + */ |
|
61 | + public function fetch() |
|
62 | + { |
|
63 | + $statement = $this->getData(); |
|
64 | + |
|
65 | + /** @var DataObject[] $returnedObjects */ |
|
66 | + $returnedObjects = $statement->fetchAll(PDO::FETCH_CLASS, $this->targetClass); |
|
67 | + foreach ($returnedObjects as $req) { |
|
68 | + $req->setDatabase($this->database); |
|
69 | + } |
|
70 | + |
|
71 | + return $returnedObjects; |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * Finalises the database query, and executes it, returning only the requested column. |
|
76 | + * |
|
77 | + * @param string $column The required column |
|
78 | + * @return array |
|
79 | + */ |
|
80 | + public function fetchColumn($column){ |
|
81 | + $statement = $this->getData(array($column)); |
|
82 | + |
|
83 | + return $statement->fetchAll(PDO::FETCH_COLUMN); |
|
84 | + } |
|
85 | + |
|
86 | + public function fetchMap($column){ |
|
87 | + $statement = $this->getData(array('id', $column)); |
|
88 | + |
|
89 | + $data = $statement->fetchAll(PDO::FETCH_ASSOC); |
|
90 | + $map = array(); |
|
91 | + |
|
92 | + foreach ($data as $row) { |
|
93 | + $map[$row['id']] = $row[$column]; |
|
94 | + } |
|
95 | + |
|
96 | + return $map; |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * @param int $count Returns the record count of the result set |
|
101 | + * |
|
102 | + * @return $this |
|
103 | + */ |
|
104 | + public function getRecordCount(&$count) |
|
105 | + { |
|
106 | + $query = 'SELECT /* SearchHelper */ COUNT(*) FROM ' . $this->table . ' origin '; |
|
107 | + $query .= $this->joinClause . $this->whereClause; |
|
108 | + |
|
109 | + $statement = $this->database->prepare($query); |
|
110 | + $statement->execute($this->parameterList); |
|
111 | + |
|
112 | + $count = $statement->fetchColumn(0); |
|
113 | + $statement->closeCursor(); |
|
114 | + |
|
115 | + return $this; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * Limits the results |
|
120 | + * |
|
121 | + * @param integer $limit |
|
122 | + * @param integer|null $offset |
|
123 | + * |
|
124 | + * @return $this |
|
125 | + * |
|
126 | + */ |
|
127 | + public function limit($limit, $offset = null) |
|
128 | + { |
|
129 | + $this->limit = $limit; |
|
130 | + $this->offset = $offset; |
|
131 | + |
|
132 | + return $this; |
|
133 | + } |
|
134 | + |
|
135 | + private function applyLimit() |
|
136 | + { |
|
137 | + $clause = ''; |
|
138 | + if ($this->limit !== null) { |
|
139 | + $clause = ' LIMIT ?'; |
|
140 | + $this->parameterList[] = $this->limit; |
|
141 | + |
|
142 | + if ($this->offset !== null) { |
|
143 | + $clause .= ' OFFSET ?'; |
|
144 | + $this->parameterList[] = $this->offset; |
|
145 | + } |
|
146 | + } |
|
147 | + |
|
148 | + return $clause; |
|
149 | + } |
|
150 | + |
|
151 | + private function applyOrder() |
|
152 | + { |
|
153 | + if ($this->orderBy !== null) { |
|
154 | + return ' ORDER BY ' . $this->orderBy; |
|
155 | + } |
|
156 | + |
|
157 | + return ''; |
|
158 | + } |
|
159 | + |
|
160 | + /** |
|
161 | + * @param array $columns |
|
162 | + * |
|
163 | + * @return PDOStatement |
|
164 | + */ |
|
165 | + private function getData($columns = array('*')) |
|
166 | + { |
|
167 | + $query = $this->buildQuery($columns); |
|
168 | + $query .= $this->applyOrder(); |
|
169 | + $query .= $this->applyLimit(); |
|
170 | + |
|
171 | + $statement = $this->database->prepare($query); |
|
172 | + $statement->execute($this->parameterList); |
|
173 | + |
|
174 | + return $statement; |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * @param array $columns |
|
179 | + * |
|
180 | + * @return string |
|
181 | + */ |
|
182 | + protected function buildQuery($columns) |
|
183 | + { |
|
184 | + $colData = array(); |
|
185 | + foreach ($columns as $c) { |
|
186 | + $colData[] = 'origin.' . $c; |
|
187 | + } |
|
188 | + |
|
189 | + $query = 'SELECT /* SearchHelper */ ' . implode(', ', $colData) . ' FROM ' . $this->table . ' origin '; |
|
190 | + $query .= $this->joinClause . $this->whereClause; |
|
191 | + |
|
192 | + return $query; |
|
193 | + } |
|
194 | + |
|
195 | + public function inIds($idList) { |
|
196 | + $this->inClause('id', $idList); |
|
197 | + return $this; |
|
198 | + } |
|
199 | + |
|
200 | + protected function inClause($column, $values) { |
|
201 | + if (count($values) === 0) { |
|
202 | + return; |
|
203 | + } |
|
204 | + |
|
205 | + // Urgh. OK. You can't use IN() with parameters directly, so let's munge something together. |
|
206 | + $valueCount = count($values); |
|
207 | + |
|
208 | + // Firstly, let's create a string of question marks, which will do as positional parameters. |
|
209 | + $inSection = str_repeat('?,', $valueCount - 1) . '?'; |
|
210 | + |
|
211 | + $this->whereClause .= " AND {$column} IN ({$inSection})"; |
|
212 | + $this->parameterList = array_merge($this->parameterList, $values); |
|
213 | + } |
|
214 | 214 | } |
@@ -77,13 +77,13 @@ discard block |
||
77 | 77 | * @param string $column The required column |
78 | 78 | * @return array |
79 | 79 | */ |
80 | - public function fetchColumn($column){ |
|
80 | + public function fetchColumn($column) { |
|
81 | 81 | $statement = $this->getData(array($column)); |
82 | 82 | |
83 | 83 | return $statement->fetchAll(PDO::FETCH_COLUMN); |
84 | 84 | } |
85 | 85 | |
86 | - public function fetchMap($column){ |
|
86 | + public function fetchMap($column) { |
|
87 | 87 | $statement = $this->getData(array('id', $column)); |
88 | 88 | |
89 | 89 | $data = $statement->fetchAll(PDO::FETCH_ASSOC); |
@@ -103,8 +103,8 @@ discard block |
||
103 | 103 | */ |
104 | 104 | public function getRecordCount(&$count) |
105 | 105 | { |
106 | - $query = 'SELECT /* SearchHelper */ COUNT(*) FROM ' . $this->table . ' origin '; |
|
107 | - $query .= $this->joinClause . $this->whereClause; |
|
106 | + $query = 'SELECT /* SearchHelper */ COUNT(*) FROM '.$this->table.' origin '; |
|
107 | + $query .= $this->joinClause.$this->whereClause; |
|
108 | 108 | |
109 | 109 | $statement = $this->database->prepare($query); |
110 | 110 | $statement->execute($this->parameterList); |
@@ -151,7 +151,7 @@ discard block |
||
151 | 151 | private function applyOrder() |
152 | 152 | { |
153 | 153 | if ($this->orderBy !== null) { |
154 | - return ' ORDER BY ' . $this->orderBy; |
|
154 | + return ' ORDER BY '.$this->orderBy; |
|
155 | 155 | } |
156 | 156 | |
157 | 157 | return ''; |
@@ -183,11 +183,11 @@ discard block |
||
183 | 183 | { |
184 | 184 | $colData = array(); |
185 | 185 | foreach ($columns as $c) { |
186 | - $colData[] = 'origin.' . $c; |
|
186 | + $colData[] = 'origin.'.$c; |
|
187 | 187 | } |
188 | 188 | |
189 | - $query = 'SELECT /* SearchHelper */ ' . implode(', ', $colData) . ' FROM ' . $this->table . ' origin '; |
|
190 | - $query .= $this->joinClause . $this->whereClause; |
|
189 | + $query = 'SELECT /* SearchHelper */ '.implode(', ', $colData).' FROM '.$this->table.' origin '; |
|
190 | + $query .= $this->joinClause.$this->whereClause; |
|
191 | 191 | |
192 | 192 | return $query; |
193 | 193 | } |
@@ -206,7 +206,7 @@ discard block |
||
206 | 206 | $valueCount = count($values); |
207 | 207 | |
208 | 208 | // Firstly, let's create a string of question marks, which will do as positional parameters. |
209 | - $inSection = str_repeat('?,', $valueCount - 1) . '?'; |
|
209 | + $inSection = str_repeat('?,', $valueCount - 1).'?'; |
|
210 | 210 | |
211 | 211 | $this->whereClause .= " AND {$column} IN ({$inSection})"; |
212 | 212 | $this->parameterList = array_merge($this->parameterList, $values); |
@@ -77,13 +77,15 @@ discard block |
||
77 | 77 | * @param string $column The required column |
78 | 78 | * @return array |
79 | 79 | */ |
80 | - public function fetchColumn($column){ |
|
80 | + public function fetchColumn($column) |
|
81 | + { |
|
81 | 82 | $statement = $this->getData(array($column)); |
82 | 83 | |
83 | 84 | return $statement->fetchAll(PDO::FETCH_COLUMN); |
84 | 85 | } |
85 | 86 | |
86 | - public function fetchMap($column){ |
|
87 | + public function fetchMap($column) |
|
88 | + { |
|
87 | 89 | $statement = $this->getData(array('id', $column)); |
88 | 90 | |
89 | 91 | $data = $statement->fetchAll(PDO::FETCH_ASSOC); |
@@ -192,12 +194,14 @@ discard block |
||
192 | 194 | return $query; |
193 | 195 | } |
194 | 196 | |
195 | - public function inIds($idList) { |
|
197 | + public function inIds($idList) |
|
198 | + { |
|
196 | 199 | $this->inClause('id', $idList); |
197 | 200 | return $this; |
198 | 201 | } |
199 | 202 | |
200 | - protected function inClause($column, $values) { |
|
203 | + protected function inClause($column, $values) |
|
204 | + { |
|
201 | 205 | if (count($values) === 0) { |
202 | 206 | return; |
203 | 207 | } |
@@ -13,25 +13,25 @@ |
||
13 | 13 | |
14 | 14 | class ClearOldDataTask extends ConsoleTaskBase |
15 | 15 | { |
16 | - public function execute() |
|
17 | - { |
|
18 | - $dataClearInterval = $this->getSiteConfiguration()->getDataClearInterval(); |
|
16 | + public function execute() |
|
17 | + { |
|
18 | + $dataClearInterval = $this->getSiteConfiguration()->getDataClearInterval(); |
|
19 | 19 | |
20 | - $query = $this->getDatabase()->prepare(<<<SQL |
|
20 | + $query = $this->getDatabase()->prepare(<<<SQL |
|
21 | 21 | UPDATE request |
22 | 22 | SET ip = :ip, forwardedip = null, email = :mail, useragent = '' |
23 | 23 | WHERE date < DATE_SUB(curdate(), INTERVAL {$dataClearInterval}) |
24 | 24 | AND status = 'Closed'; |
25 | 25 | SQL |
26 | - ); |
|
26 | + ); |
|
27 | 27 | |
28 | - $success = $query->execute(array( |
|
29 | - ":ip" => $this->getSiteConfiguration()->getDataClearIp(), |
|
30 | - ":mail" => $this->getSiteConfiguration()->getDataClearEmail(), |
|
31 | - )); |
|
28 | + $success = $query->execute(array( |
|
29 | + ":ip" => $this->getSiteConfiguration()->getDataClearIp(), |
|
30 | + ":mail" => $this->getSiteConfiguration()->getDataClearEmail(), |
|
31 | + )); |
|
32 | 32 | |
33 | - if (!$success) { |
|
34 | - throw new Exception("Error in transaction: Could not clear data."); |
|
35 | - } |
|
36 | - } |
|
33 | + if (!$success) { |
|
34 | + throw new Exception("Error in transaction: Could not clear data."); |
|
35 | + } |
|
36 | + } |
|
37 | 37 | } |
38 | 38 | \ No newline at end of file |