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