1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* @package sitemaker |
5
|
|
|
* @copyright (c) 2013 Daniel A. (blitze) |
6
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace blitze\sitemaker\services\forum; |
11
|
|
|
|
12
|
|
|
class query_builder |
13
|
|
|
{ |
14
|
|
|
/** @var \phpbb\auth\auth */ |
15
|
|
|
protected $auth; |
16
|
|
|
|
17
|
|
|
/** @var \phpbb\config\config */ |
18
|
|
|
protected $config; |
19
|
|
|
|
20
|
|
|
/** @var \phpbb\content_visibility */ |
21
|
|
|
protected $content_visibility; |
22
|
|
|
|
23
|
|
|
/** @var \phpbb\db\driver\driver_interface */ |
24
|
|
|
protected $db; |
25
|
|
|
|
26
|
|
|
/** @var \phpbb\user */ |
27
|
|
|
protected $user; |
28
|
|
|
|
29
|
|
|
/** @var array */ |
30
|
|
|
protected $store; |
31
|
|
|
|
32
|
|
|
/** @var array */ |
33
|
|
|
protected $ex_fid_ary; |
34
|
|
|
|
35
|
|
|
/** @var integer */ |
36
|
|
|
protected $cache_time; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Constructor |
40
|
|
|
* |
41
|
|
|
* @param \phpbb\auth\auth $auth Auth object |
42
|
|
|
* @param \phpbb\config\config $config Config object |
43
|
|
|
* @param \phpbb\content_visibility $content_visibility Content visibility |
44
|
|
|
* @param \phpbb\db\driver\driver_interface $db Database connection |
45
|
|
|
* @param \phpbb\user $user User object |
46
|
|
|
* @param integer $cache_time Cache results for 3 hours by default |
47
|
|
|
*/ |
48
|
31 |
|
public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, \phpbb\content_visibility $content_visibility, \phpbb\db\driver\driver_interface $db, \phpbb\user $user, $cache_time) |
49
|
|
|
{ |
50
|
31 |
|
$this->auth = $auth; |
51
|
31 |
|
$this->config = $config; |
52
|
31 |
|
$this->content_visibility = $content_visibility; |
53
|
31 |
|
$this->db = $db; |
54
|
31 |
|
$this->user = $user; |
55
|
31 |
|
$this->cache_time = $cache_time; |
56
|
|
|
|
57
|
31 |
|
$this->ex_fid_ary = array_unique(array_keys($this->auth->acl_getf('!f_read', true))); |
58
|
31 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Begin query |
62
|
|
|
* |
63
|
|
|
* @param bool $track_topics |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
22 |
|
public function query($track_topics = true, $add_forum_data = true) |
67
|
|
|
{ |
68
|
22 |
|
$this->_reset(); |
69
|
|
|
|
70
|
22 |
|
$this->store['sql_array'] = array_fill_keys(array('SELECT', 'FROM', 'LEFT_JOIN', 'WHERE'), array()); |
71
|
|
|
|
72
|
|
|
if ($add_forum_data) |
73
|
22 |
|
{ |
74
|
21 |
|
$this->store['sql_array']['SELECT'][] = 'f.*'; |
75
|
21 |
|
$this->store['sql_array']['FROM'][FORUMS_TABLE] = 'f'; |
76
|
21 |
|
} |
77
|
|
|
|
78
|
22 |
|
$this->store['sql_array']['SELECT'][] = 't.*'; |
79
|
|
|
|
80
|
|
|
if ($track_topics) |
81
|
22 |
|
{ |
82
|
7 |
|
$this->fetch_tracking_info(); |
83
|
7 |
|
} |
84
|
|
|
|
85
|
22 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Fetch Forum by id(s) |
90
|
|
|
* |
91
|
|
|
* @param $forum_id |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
17 |
|
public function fetch_forum($forum_id) |
95
|
|
|
{ |
96
|
17 |
|
$this->_fetch($forum_id, 'f.forum_id'); |
97
|
|
|
|
98
|
17 |
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Fetch Topic by id(s) |
103
|
|
|
* |
104
|
|
|
* @param mixed $topic_id Limit by topic id: single id or array of topic ids |
105
|
|
|
* @return $this |
106
|
|
|
*/ |
107
|
11 |
|
public function fetch_topic($topic_id) |
108
|
|
|
{ |
109
|
11 |
|
$this->_fetch($topic_id, 't.topic_id'); |
110
|
|
|
|
111
|
11 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Fetch Topic by Poster id(s) |
116
|
|
|
* |
117
|
|
|
* @param mixed $user_id User id of topic poster: single id or array of user ids |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
3 |
|
public function fetch_topic_poster($user_id) |
121
|
|
|
{ |
122
|
3 |
|
$this->_fetch($user_id, 't.topic_poster'); |
123
|
|
|
|
124
|
3 |
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Fetch by Topic Type |
129
|
|
|
* |
130
|
|
|
* @param array $topic_type |
131
|
|
|
* @return $this |
132
|
|
|
*/ |
133
|
17 |
|
public function fetch_topic_type(array $topic_type) |
134
|
|
|
{ |
135
|
17 |
|
if (sizeof($topic_type)) |
136
|
17 |
|
{ |
137
|
5 |
|
$this->store['sql_array']['WHERE'][] = $this->db->sql_in_set('t.topic_type', $topic_type); |
138
|
5 |
|
} |
139
|
|
|
|
140
|
17 |
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Fetch Topic Watch info |
145
|
|
|
* |
146
|
|
|
* @param $type |
147
|
|
|
* @return $this |
148
|
|
|
*/ |
149
|
|
|
public function fetch_watch_status($type = 'topic') |
150
|
|
|
{ |
151
|
|
|
if ($this->user->data['is_registered']) |
152
|
|
|
{ |
153
|
|
|
$keys = array( |
154
|
|
|
'forum' => array( |
155
|
|
|
'table' => FORUMS_WATCH_TABLE, |
156
|
|
|
'cond' => 'ws.forum_id = f.forum_id', |
157
|
|
|
), |
158
|
|
|
'topic' => array( |
159
|
|
|
'table' => TOPICS_WATCH_TABLE, |
160
|
|
|
'cond' => 'ws.topic_id = t.topic_id', |
161
|
|
|
), |
162
|
|
|
); |
163
|
|
|
|
164
|
|
|
$this->store['sql_array']['SELECT'][] = 'ws.notify_status'; |
165
|
|
|
$this->store['sql_array']['LEFT_JOIN'][] = array( |
166
|
|
|
'FROM' => array($keys[$type]['table'] => 'ws'), |
167
|
|
|
'ON' => $keys[$type]['cond'] . ' AND ws.user_id = ' . (int) $this->user->data['user_id'], |
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Fetch Topic Bookmark Info |
176
|
|
|
* |
177
|
|
|
* @return $this |
178
|
|
|
*/ |
179
|
|
|
public function fetch_bookmark_status() |
180
|
|
|
{ |
181
|
|
|
if ($this->user->data['is_registered'] && $this->config['allow_bookmarks']) |
182
|
|
|
{ |
183
|
|
|
$this->store['sql_array']['SELECT'][] = 'bm.topic_id as bookmarked'; |
184
|
|
|
$this->store['sql_array']['LEFT_JOIN'][] = array( |
185
|
|
|
'FROM' => array(BOOKMARKS_TABLE => 'bm'), |
186
|
|
|
'ON' => 'bm.user_id = ' . (int) $this->user->data['user_id'] . ' AND t.topic_id = bm.topic_id' |
187
|
|
|
); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Fetch Topic Tracking Info |
195
|
|
|
* |
196
|
|
|
* @return $this |
197
|
|
|
*/ |
198
|
7 |
|
public function fetch_tracking_info() |
199
|
|
|
{ |
200
|
7 |
|
if ($this->user->data['is_registered'] && $this->config['load_db_lastread']) |
201
|
7 |
|
{ |
202
|
6 |
|
$this->cache_time = 0; |
203
|
|
|
|
204
|
6 |
|
$this->store['sql_array']['SELECT'][] = 'tt.mark_time, ft.mark_time as forum_mark_time'; |
205
|
6 |
|
$this->store['sql_array']['LEFT_JOIN'][] = array( |
206
|
6 |
|
'FROM' => array(TOPICS_TRACK_TABLE => 'tt'), |
207
|
6 |
|
'ON' => 'tt.user_id = ' . (int) $this->user->data['user_id'] . ' AND t.topic_id = tt.topic_id' |
208
|
6 |
|
); |
209
|
|
|
|
210
|
6 |
|
$this->store['sql_array']['LEFT_JOIN'][] = array( |
211
|
6 |
|
'FROM' => array(FORUMS_TRACK_TABLE => 'ft'), |
212
|
6 |
|
'ON' => 'ft.user_id = ' . (int) $this->user->data['user_id'] . ' AND t.forum_id = ft.forum_id' |
213
|
6 |
|
); |
214
|
6 |
|
} |
215
|
|
|
|
216
|
7 |
|
return $this; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Fetch by Date Range |
221
|
|
|
* |
222
|
|
|
* @param int $unix_start_time |
223
|
|
|
* @param int $unix_stop_time |
224
|
|
|
* @param string $mode |
225
|
|
|
* @return $this |
226
|
|
|
*/ |
227
|
14 |
|
public function fetch_date_range($unix_start_time, $unix_stop_time, $mode = 'topic') |
228
|
|
|
{ |
229
|
14 |
|
if ($unix_start_time && $unix_stop_time) |
230
|
14 |
|
{ |
231
|
2 |
|
$this->store['sql_array']['WHERE'][] = (($mode == 'topic') ? 't.topic_time' : 'p.post_time') . " BETWEEN $unix_start_time AND $unix_stop_time"; |
232
|
2 |
|
} |
233
|
|
|
|
234
|
14 |
|
return $this; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Fetch by Custom Query |
239
|
|
|
* |
240
|
|
|
* @param array $sql_array Array of elements to merge into query |
241
|
|
|
* array( |
242
|
|
|
* 'SELECT' => array('p.*'), |
243
|
|
|
* 'WHERE' => array('p.post_id = 2'), |
244
|
|
|
* ) |
245
|
|
|
* @param array $overwrite_keys Array of query keys to overwrite with yours instead of merging |
246
|
|
|
* e.g array('SELECT') will overwrite the 'SELECT' key with whatever is provided in $sql_array |
247
|
|
|
* @return $this |
248
|
|
|
*/ |
249
|
8 |
|
public function fetch_custom(array $sql_array, $overwrite_keys = array()) |
250
|
|
|
{ |
251
|
8 |
|
$this->store['sql_array'] = array_merge_recursive($this->store['sql_array'], $sql_array); |
252
|
|
|
|
253
|
8 |
|
foreach ($overwrite_keys as $key) |
254
|
|
|
{ |
255
|
|
|
$this->store['sql_array'][$key] = $sql_array[$key]; |
256
|
8 |
|
} |
257
|
|
|
|
258
|
8 |
|
return $this; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Set Sorting Order |
263
|
|
|
* |
264
|
|
|
* @param string $sort_key The sorting key e.g. t.topic_time |
265
|
|
|
* @param string $sort_dir Sort direction: ASC/DESC |
266
|
|
|
* @return $this |
267
|
|
|
*/ |
268
|
13 |
|
public function set_sorting($sort_key, $sort_dir = 'DESC') |
269
|
|
|
{ |
270
|
13 |
|
$this->store['sql_array']['ORDER_BY'] = $sort_key . ' ' . $sort_dir; |
271
|
|
|
|
272
|
13 |
|
return $this; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Build the query |
277
|
|
|
* |
278
|
|
|
* @param bool|true $check_visibility Should we only return data from forums the user is allowed to see? |
279
|
|
|
* @param bool|true $enable_caching Should the query be cached where possible? |
280
|
|
|
* @param bool|true $exclude_hidden_forums Leave out hidden forums? |
281
|
|
|
* @return $this |
282
|
|
|
*/ |
283
|
22 |
|
public function build($check_visibility = true, $enable_caching = true, $exclude_hidden_forums = true) |
284
|
|
|
{ |
285
|
22 |
|
$this->_set_cache_time($enable_caching); |
286
|
22 |
|
$this->_set_topic_visibility($check_visibility); |
287
|
|
|
|
288
|
|
|
if ($exclude_hidden_forums) |
289
|
22 |
|
{ |
290
|
11 |
|
$this->store['sql_array']['FROM'][FORUMS_TABLE] = 'f'; |
291
|
11 |
|
$this->store['sql_array']['WHERE'][] = 'f.hidden_forum = 0'; |
292
|
11 |
|
} |
293
|
|
|
|
294
|
22 |
|
if (isset($this->store['sql_array']['FROM'][FORUMS_TABLE])) |
295
|
22 |
|
{ |
296
|
22 |
|
$this->store['sql_array']['WHERE'][] = 'f.forum_id = t.forum_id'; |
297
|
22 |
|
} |
298
|
|
|
|
299
|
|
|
// Topics table need to be the last in the chain |
300
|
22 |
|
$this->store['sql_array']['FROM'][TOPICS_TABLE] = 't'; |
301
|
22 |
|
$this->store['sql_array']['WHERE'][] = 't.topic_moved_id = 0'; |
302
|
|
|
|
303
|
22 |
|
$this->store['sql_array']['SELECT'] = join(', ', array_filter($this->store['sql_array']['SELECT'])); |
304
|
22 |
|
$this->store['sql_array']['WHERE'] = join(' AND ', array_filter($this->store['sql_array']['WHERE'])); |
305
|
|
|
|
306
|
22 |
|
return $this; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Get the query array |
311
|
|
|
* |
312
|
|
|
* @return array The sql array that can be used with sql_build_query |
313
|
|
|
*/ |
314
|
8 |
|
public function get_sql_array() |
315
|
|
|
{ |
316
|
8 |
|
return $this->store['sql_array']; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* @param bool $enable_caching |
321
|
|
|
*/ |
322
|
22 |
|
protected function _set_cache_time($enable_caching) |
323
|
|
|
{ |
324
|
22 |
|
if ($enable_caching === false) |
325
|
22 |
|
{ |
326
|
5 |
|
$this->cache_time = 0; |
327
|
5 |
|
} |
328
|
22 |
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* @param int $column_id |
332
|
|
|
* @param string $column |
333
|
|
|
*/ |
334
|
17 |
|
private function _fetch($column_id, $column) |
335
|
|
|
{ |
336
|
17 |
|
if (!empty($column_id)) |
337
|
17 |
|
{ |
338
|
11 |
|
$this->store['sql_array']['WHERE'][] = (is_array($column_id)) ? $this->db->sql_in_set($column, $column_id) : $column . ' = ' . (int) $column_id; |
339
|
11 |
|
} |
340
|
17 |
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* @param bool $check_visibility |
344
|
|
|
*/ |
345
|
22 |
|
private function _set_topic_visibility($check_visibility) |
346
|
|
|
{ |
347
|
|
|
if ($check_visibility) |
348
|
22 |
|
{ |
349
|
22 |
|
$this->store['sql_array']['WHERE'][] = 't.topic_time <= ' . time(); |
350
|
22 |
|
$this->store['sql_array']['WHERE'][] = $this->content_visibility->get_global_visibility_sql('topic', $this->ex_fid_ary, 't.'); |
351
|
22 |
|
} |
352
|
22 |
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* Reset data |
356
|
|
|
*/ |
357
|
22 |
|
private function _reset() |
358
|
|
|
{ |
359
|
22 |
|
$this->store = array( |
360
|
22 |
|
'attachments' => array(), |
361
|
22 |
|
'post_ids' => array(), |
362
|
22 |
|
'poster_ids' => array(), |
363
|
22 |
|
'sql_array' => array(), |
364
|
22 |
|
'topic' => array(), |
365
|
22 |
|
'tracking' => array(), |
366
|
|
|
); |
367
|
22 |
|
} |
368
|
|
|
} |
369
|
|
|
|