Complex classes like Blog often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Blog, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Blog |
||
26 | { |
||
27 | public $db; |
||
28 | protected $theme; |
||
29 | protected $folder; |
||
30 | private $config; |
||
31 | private $ids; |
||
32 | |||
33 | /** |
||
34 | * Blog folder getter. |
||
35 | * |
||
36 | * @param string $name |
||
37 | * |
||
38 | * @return null|string |
||
39 | */ |
||
40 | 1 | public function __get($name) |
|
48 | |||
49 | 24 | public function __construct(Theme $theme, $folder = 'blog') |
|
115 | |||
116 | 21 | public function query($type, $params = null) |
|
117 | { |
||
118 | 18 | $posts = array(); |
|
119 | 18 | if (is_array($type)) { // listings |
|
120 | 12 | $vars = array(); |
|
121 | 12 | foreach (array('archives', 'authors', 'tags', 'categories', 'default') as $query) { |
|
122 | 12 | if (isset($type[$query])) { |
|
123 | 9 | $vars = $type[$query]; |
|
124 | switch ($query) { |
||
125 | 9 | case 'archives': |
|
126 | 4 | $vars = (is_array($vars) && count($vars) == 2) ? $vars : false; // array(from, to) |
|
127 | 4 | break; |
|
128 | 6 | case 'authors': |
|
129 | 6 | case 'tags': |
|
130 | 3 | $vars = (is_string($vars)) ? $vars : false; // path |
|
131 | 3 | break; |
|
132 | 4 | case 'categories': |
|
133 | 4 | $vars = (is_string($vars) || (is_array($vars) && ctype_digit(implode('', $vars)))) ? $vars : false; |
|
134 | 4 | break; // path (string) or category_id's (array) |
|
135 | } |
||
136 | 9 | break; |
|
137 | } |
||
138 | 12 | } |
|
139 | 12 | if ($vars === false) { |
|
140 | 1 | return; |
|
141 | } |
||
142 | 12 | $count = (is_object($params) && empty($params->limit)) ? false : true; // $params instanceof Pagination |
|
143 | 21 | if ($count && isset($type['count'])) { |
|
144 | 2 | return $type['count']; |
|
145 | } |
||
146 | switch ($query) { |
||
147 | 12 | case 'archives': |
|
148 | 3 | list($from, $to) = $vars; |
|
149 | 3 | $vars = array($to * -1, $from * -1); |
|
150 | 3 | if ($count) { |
|
151 | 12 | return $this->db->value(array( |
|
152 | 3 | 'SELECT COUNT(*) FROM blog', |
|
153 | 3 | 'WHERE featured <= 0 AND published >= ? AND published <= ?', |
|
154 | 3 | ), $vars); |
|
155 | } else { |
||
156 | 3 | $posts = $this->db->ids(array( |
|
157 | 3 | 'SELECT id FROM blog', |
|
158 | 3 | 'WHERE featured <= 0 AND published >= ? AND published <= ?', |
|
159 | 3 | 'ORDER BY featured, published ASC'.$params->limit, |
|
160 | 3 | ), $vars); |
|
161 | } |
||
162 | 3 | break; |
|
163 | |||
164 | 9 | case 'authors': |
|
165 | 1 | if ($count) { |
|
166 | 1 | return $this->db->value(array( |
|
167 | 1 | 'SELECT COUNT(*) FROM blog AS b', |
|
168 | 1 | 'INNER JOIN authors ON b.author_id = authors.id', |
|
169 | 1 | 'WHERE b.featured <= 0 AND b.published < 0 AND b.updated < 0 AND authors.path = ?', |
|
170 | 1 | ), $vars); |
|
171 | 1 | } else { |
|
172 | 1 | $posts = $this->db->ids(array( |
|
173 | 1 | 'SELECT b.id FROM blog AS b', |
|
174 | 1 | 'INNER JOIN authors ON b.author_id = authors.id', |
|
175 | 1 | 'WHERE b.featured <= 0 AND b.published < 0 AND b.updated < 0 AND authors.path = ?', |
|
176 | 1 | 'ORDER BY b.featured, b.published ASC'.$params->limit, |
|
177 | 1 | ), $vars); |
|
178 | } |
||
179 | 1 | break; |
|
180 | |||
181 | 8 | case 'tags': |
|
182 | 1 | if ($count) { |
|
183 | 1 | return $this->db->value(array( |
|
184 | 1 | 'SELECT COUNT(*) FROM tagged AS t', |
|
185 | 1 | 'INNER JOIN blog AS b ON t.blog_id = b.id', |
|
186 | 1 | 'INNER JOIN tags ON t.tag_id = tags.id', |
|
187 | 1 | 'WHERE b.featured <= 0 AND b.published != 0 AND tags.path = ?', |
|
188 | 1 | 'GROUP BY tags.id', |
|
189 | 1 | ), $vars); |
|
190 | } else { |
||
191 | 1 | $posts = $this->db->ids(array( |
|
192 | 1 | 'SELECT b.id FROM tagged AS t', |
|
193 | 1 | 'INNER JOIN blog AS b ON t.blog_id = b.id', |
|
194 | 1 | 'INNER JOIN tags ON t.tag_id = tags.id', |
|
195 | 1 | 'WHERE b.featured <= 0 AND b.published != 0 AND tags.path = ?', |
|
196 | 1 | 'ORDER BY b.featured, b.published, b.updated ASC'.$params->limit, |
|
197 | 1 | ), $vars); |
|
198 | } |
||
199 | 1 | break; |
|
200 | |||
201 | 7 | case 'categories': |
|
202 | 4 | if (isset($type['search'])) { |
|
203 | 2 | if (!is_string($vars)) { |
|
204 | 1 | $vars = $this->db->value('SELECT path FROM categories WHERE id = ?', array_shift($vars)); |
|
205 | 1 | } |
|
206 | 2 | if (empty($vars)) { |
|
207 | 1 | return; |
|
208 | } |
||
209 | 1 | $phrase = $type['search']; |
|
210 | 1 | $category = 'blog/'.$vars; |
|
211 | 2 | $weights = (isset($type['weights']) && is_array($type['weights'])) ? $type['weights'] : array(); |
|
212 | 1 | if ($count) { |
|
213 | 1 | $sitemap = new Sitemap(); |
|
214 | 1 | $count = $sitemap->count($phrase, $category); |
|
215 | 1 | unset($sitemap); |
|
216 | |||
217 | 1 | return $count; |
|
218 | } else { |
||
219 | 1 | $sitemap = new Sitemap(); |
|
220 | 1 | $includes = array(); |
|
221 | 1 | foreach ($sitemap->search($phrase, $category, $params->limit, $weights) as $row) { |
|
222 | 1 | $posts[] = $row['id']; |
|
223 | 1 | $includes[$row['id']] = array('snippet' => $row['snippet'], 'words' => $row['words']); |
|
224 | 1 | } |
|
225 | 1 | unset($sitemap); |
|
226 | 1 | $posts = $this->info($posts); |
|
227 | 1 | foreach ($posts as $id => $row) { |
|
228 | 1 | $posts[$id] = array_merge($row, $includes[$id]); |
|
229 | 1 | } |
|
230 | |||
231 | 1 | return $posts; |
|
232 | } |
||
233 | } |
||
234 | 3 | if (!is_array($vars)) { |
|
235 | 2 | $hier = new Hierarchy($this->db, 'categories'); |
|
236 | 2 | $vars = array_keys($hier->tree(array('path'), array('where' => 'path = '.$vars))); |
|
237 | 2 | unset($hier); |
|
238 | 2 | } |
|
239 | 3 | if (empty($vars)) { |
|
240 | 1 | return; |
|
241 | } |
||
242 | 2 | $categories = implode(', ', $vars); |
|
243 | 2 | if ($count) { |
|
244 | 2 | return $this->db->value(array( |
|
245 | 2 | 'SELECT COUNT(*) FROM blog', |
|
246 | 2 | 'WHERE featured <= 0 AND published != 0 AND category_id IN('.$categories.')', |
|
247 | 2 | ), $vars); |
|
248 | } else { |
||
249 | 2 | $posts = $this->db->ids(array( |
|
250 | 2 | 'SELECT id FROM blog', |
|
251 | 2 | 'WHERE featured <= 0 AND published != 0 AND category_id IN('.$categories.')', |
|
252 | 2 | 'ORDER BY featured, published, updated ASC'.$params->limit, |
|
253 | 2 | ), $vars); |
|
254 | } |
||
255 | 2 | break; |
|
256 | |||
257 | 4 | default: |
|
258 | 4 | if (isset($type['search'])) { |
|
259 | 1 | $phrase = $type['search']; |
|
260 | 1 | $weights = (isset($type['weights']) && is_array($type['weights'])) ? $type['weights'] : array(); |
|
261 | 1 | if ($count) { |
|
262 | 1 | $sitemap = new Sitemap(); |
|
263 | 1 | $count = $sitemap->count($phrase, 'blog'); |
|
264 | 1 | unset($sitemap); |
|
265 | |||
266 | 1 | return $count; |
|
267 | } else { |
||
268 | 1 | $sitemap = new Sitemap(); |
|
269 | 1 | $includes = array(); |
|
270 | 1 | foreach ($sitemap->search($phrase, 'blog', $params->limit, $weights) as $row) { |
|
271 | 1 | $posts[] = $row['id']; |
|
272 | 1 | $includes[$row['id']] = array('snippet' => $row['snippet'], 'words' => $row['words']); |
|
273 | 1 | } |
|
274 | 1 | unset($sitemap); |
|
275 | 1 | $posts = $this->info($posts); |
|
276 | 1 | foreach ($posts as $id => $row) { |
|
277 | 1 | $posts[$id] = array_merge($row, $includes[$id]); |
|
278 | 1 | } |
|
279 | |||
280 | 1 | return $posts; |
|
281 | } |
||
282 | } |
||
283 | 3 | if ($count) { |
|
284 | 2 | return $this->db->value(array( |
|
285 | 2 | 'SELECT COUNT(*) FROM blog', |
|
286 | 2 | 'WHERE featured <= 0 AND published < 0', |
|
287 | 2 | ), $vars); |
|
288 | } else { |
||
289 | 2 | $posts = $this->db->ids(array( |
|
290 | 2 | 'SELECT id FROM blog', |
|
291 | 2 | 'WHERE featured <= 0 AND published < 0', |
|
292 | 2 | 'ORDER BY featured, published ASC'.$params->limit, |
|
293 | 2 | ), $vars); |
|
294 | } |
||
295 | 2 | break; |
|
296 | 4 | } |
|
297 | |||
298 | 9 | return $this->info($posts); |
|
299 | } |
||
300 | switch ($type) { |
||
301 | 11 | case 'similar': // optional (string|array) keywords (ordered by RANK) - default Page::html()->keywords |
|
302 | 1 | $keywords = Page::html()->keywords; |
|
303 | 1 | $limit = $params; |
|
304 | 1 | if (is_array($params)) { |
|
305 | 1 | list($limit, $keywords) = (count($params) == 1) ? each($params) : $params; |
|
306 | 1 | } |
|
307 | 1 | if (!empty($keywords) && !empty($limit) && is_numeric($limit)) { |
|
308 | 1 | if (!is_array($keywords)) { |
|
309 | 1 | $keywords = array_map('trim', explode(',', $keywords)); |
|
310 | 1 | } |
|
311 | 1 | if (!empty($keywords)) { |
|
312 | 1 | $current = Page::html()->url['path']; |
|
313 | 1 | if (empty($current)) { |
|
314 | 1 | $current = 'index'; |
|
315 | 1 | } |
|
316 | 1 | $sitemap = new Sitemap(); |
|
317 | 1 | foreach ($sitemap->search('"'.implode('" OR "', $keywords).'"', 'blog', $limit, array(0, 0, 0, 1, 0), 'AND m.path != "'.$current.'"') as $row) { |
|
318 | 1 | $posts[] = $row['id']; |
|
319 | 1 | } |
|
320 | 1 | unset($sitemap); |
|
321 | 1 | $posts = $this->info($posts); |
|
322 | 1 | } |
|
323 | 1 | } |
|
324 | 1 | break; |
|
325 | |||
326 | 10 | case 'posts': // (array) paths (limit and order inherent) |
|
327 | 1 | if (!empty($params)) { |
|
328 | 1 | foreach ((array) $params as $path) { |
|
329 | 1 | $posts[$path] = ''; |
|
330 | 1 | } |
|
331 | 1 | foreach ($this->db->all(array( |
|
332 | 1 | 'SELECT path, id', |
|
333 | 1 | 'FROM blog', |
|
334 | 1 | 'WHERE path IN('.implode(', ', array_fill(0, count($posts), '?')).')', |
|
335 | 1 | ), array_keys($posts), 'assoc') as $row) { |
|
336 | 1 | $posts[$row['path']] = $row['id']; |
|
337 | 1 | } |
|
338 | 1 | $posts = $this->info(array_values(array_filter($posts))); |
|
339 | 1 | } |
|
340 | 1 | break; |
|
341 | |||
342 | 9 | case 'archives': // optional (array) years (ordered by month DESC, and only starts if count > 0) - default all, no limit |
|
343 | 4 | $years = (is_array($params)) ? $params : array(); |
|
344 | 1 | if (empty($years)) { |
|
345 | 1 | $times = $this->db->row('SELECT ABS(MAX(published)) AS begin, ABS(MIN(published)) AS end FROM blog WHERE featured <= 0 AND published < 0', '', 'assoc'); |
|
346 | 1 | if (!is_null($times['end'])) { |
|
347 | 1 | $years = range(date('Y', $times['begin']), date('Y', $times['end'])); |
|
348 | 1 | } |
|
349 | 1 | } |
|
350 | 1 | $months = array('Jan' => 1, 'Feb' => 2, 'Mar' => 3, 'Apr' => 4, 'May' => 5, 'Jun' => 6, 'Jul' => 7, 'Aug' => 8, 'Sep' => 9, 'Oct' => 10, 'Nov' => 11, 'Dec' => 12); |
|
351 | 1 | $archives = array(); |
|
352 | 1 | foreach ($years as $Y) { |
|
353 | 1 | foreach ($months as $M => $n) { |
|
354 | 1 | $to = mktime(23, 59, 59, $n + 1, 0, $Y) * -1; |
|
355 | 1 | $from = mktime(0, 0, 0, $n, 1, $Y) * -1; |
|
356 | 1 | $archives[] = "SUM(CASE WHEN featured <= 0 AND published >= {$to} AND published <= {$from} THEN 1 ELSE 0 END) AS {$M}{$Y}"; |
|
357 | 1 | } |
|
358 | 1 | } |
|
359 | 1 | if (!empty($archives) && $archives = $this->db->row(array('SELECT', implode(",\n", $archives), 'FROM blog'), '', 'assoc')) { |
|
360 | 1 | $page = Page::html(); |
|
361 | 1 | foreach ($archives as $date => $count) { |
|
362 | 1 | $time = mktime(0, 0, 0, $months[substr($date, 0, 3)], 15, substr($date, 3)); |
|
363 | 1 | list($Y, $M, $m) = explode(' ', date('Y M m', $time)); |
|
364 | 1 | if (!isset($posts[$Y])) { |
|
365 | 1 | $posts[$Y] = array('count' => 0, 'url' => $page->url('blog/listings', 'archives', $Y)); |
|
366 | 1 | } |
|
367 | 1 | $posts[$Y]['months'][$M] = array('url' => $page->url('blog/listings', 'archives', $Y, $m), 'count' => $count, 'time' => $time); |
|
368 | 1 | $posts[$Y]['count'] += $count; |
|
369 | 1 | } |
|
370 | 1 | } |
|
371 | 1 | break; |
|
372 | |||
373 | 8 | case 'authors': // optional (int) limit or (string) path (ordered by count DESC, then author name ASC) |
|
374 | 2 | $path = (!is_int($params) && !empty($params)) ? (string) $params : ''; |
|
375 | 2 | $operator = (!empty($path)) ? '=' : '!='; |
|
376 | 2 | $authors = $this->db->all(array( |
|
377 | 2 | 'SELECT COUNT(*) AS count, authors.path, authors.name, ABS(MIN(b.published)) AS latest', |
|
378 | 2 | 'FROM blog AS b', |
|
379 | 2 | 'INNER JOIN authors ON b.author_id = authors.id', |
|
380 | 2 | 'WHERE b.featured <= 0 AND b.published < 0 AND b.updated < 0 AND authors.path '.$operator.' ?', |
|
381 | 2 | 'GROUP BY authors.id', |
|
382 | 2 | 'ORDER BY authors.name ASC', |
|
383 | 2 | ), $path, 'assoc'); |
|
384 | 2 | $authored = array(); |
|
385 | 2 | foreach ($authors as $author) { |
|
386 | 2 | $authored[$author['path']] = $author['count']; |
|
387 | 2 | } |
|
388 | 2 | arsort($authored); |
|
389 | 2 | if (is_int($params)) { |
|
390 | 1 | $authored = array_slice($authored, 0, $params, true); |
|
391 | 1 | } |
|
392 | 2 | foreach ($authors as $author) { |
|
393 | 2 | if (isset($authored[$author['path']])) { |
|
394 | 2 | $info = $this->configInfo('authors', $author['path'], $author['name']); |
|
395 | 2 | $info['latest'] = $author['latest']; |
|
396 | 2 | $info['count'] = $author['count']; |
|
397 | 2 | $posts[] = $info; |
|
398 | 2 | } |
|
399 | 2 | } |
|
400 | 2 | if (!empty($path) && !empty($posts)) { |
|
401 | 1 | $posts = array_shift($posts); |
|
402 | 1 | } |
|
403 | 2 | break; |
|
404 | |||
405 | 6 | case 'tags': // optional (int) limit or (string) path (ordered by count DESC, then tag name ASC) |
|
406 | 2 | $path = (!is_int($params) && !empty($params)) ? (string) $params : ''; |
|
407 | 2 | $operator = (!empty($path)) ? '=' : '!='; |
|
408 | 2 | $tags = $this->db->all(array( |
|
409 | 2 | 'SELECT COUNT(*) AS count, tags.path, tags.name, ABS(MIN(b.published)) AS latest', |
|
410 | 2 | 'FROM tagged AS t', |
|
411 | 2 | 'INNER JOIN blog AS b ON t.blog_id = b.id', |
|
412 | 2 | 'INNER JOIN tags ON t.tag_id = tags.id', |
|
413 | 2 | 'WHERE b.featured <= 0 AND b.published != 0 AND tags.path '.$operator.' ?', |
|
414 | 2 | 'GROUP BY tags.id', |
|
415 | 2 | 'ORDER BY tags.name ASC', |
|
416 | 2 | ), $path, 'assoc'); |
|
417 | 2 | $tagged = array(); |
|
418 | 2 | foreach ($tags as $tag) { |
|
419 | 2 | $tagged[$tag['path']] = $tag['count']; |
|
420 | 2 | } |
|
421 | 2 | arsort($tagged); |
|
422 | 2 | if (is_int($params)) { |
|
423 | 1 | $tagged = array_slice($tagged, 0, $params, true); |
|
424 | 1 | } |
|
425 | 2 | if (count($tagged) > 0) { |
|
426 | // http://en.wikipedia.org/wiki/Tag_cloud |
||
427 | // http://stackoverflow.com/questions/18790677/what-algorithm-can-i-use-to-sort-tags-for-tag-cloud?rq=1 |
||
428 | // http://stackoverflow.com/questions/227/whats-the-best-way-to-generate-a-tag-cloud-from-an-array-using-h1-through-h6-fo |
||
429 | 2 | $min = min($tagged); |
|
430 | 2 | $range = max(.01, max($tagged) - $min) * 1.0001; |
|
431 | 2 | foreach ($tags as $tag) { |
|
432 | 2 | if (isset($tagged[$tag['path']])) { |
|
433 | 2 | $info = $this->configInfo('tags', $tag['path'], $tag['name']); |
|
434 | 2 | $info['latest'] = $tag['latest']; |
|
435 | 2 | $info['count'] = $tag['count']; |
|
436 | 2 | if (!empty($path)) { |
|
437 | 1 | $posts = $info; |
|
438 | 1 | break; |
|
439 | } |
||
440 | 1 | $info['rank'] = ceil(((4 * ($tag['count'] - $min)) / $range) + 1); |
|
441 | 1 | $posts[] = $info; |
|
442 | 1 | } |
|
443 | 2 | } |
|
444 | 2 | } |
|
445 | 2 | break; |
|
446 | |||
447 | 4 | case 'categories': // (ordered by category name ASC) - no limit |
|
448 | // http://www.smarty.net/docs/en/language.function.function.tpl |
||
449 | 4 | if (is_array($params) && isset($params['nest']) && isset($params['tree'])) { |
|
450 | 4 | foreach ($params['nest'] as $id => $subs) { |
|
451 | 4 | $category = $params['tree'][$id]; |
|
452 | 4 | $posts[$id] = $this->configInfo('categories', $category['path'], $category['name']); |
|
453 | 4 | $posts[$id]['count'] = $category['count']; |
|
454 | 4 | if (!empty($subs)) { |
|
455 | 3 | $posts[$id]['subs'] = $this->query('categories', array( |
|
456 | 3 | 'nest' => $subs, |
|
457 | 3 | 'tree' => $params['tree'], |
|
458 | 3 | )); |
|
459 | 3 | } |
|
460 | 4 | } |
|
461 | |||
462 | 4 | return array_values($posts); |
|
463 | } |
||
464 | 1 | $hier = new Hierarchy($this->db, 'categories'); |
|
465 | 1 | $tree = $hier->tree(array('path', 'name')); |
|
466 | 1 | $counts = $hier->counts('blog', 'category_id'); |
|
467 | 1 | foreach ($tree as $id => $fields) { |
|
468 | 1 | $tree[$id]['count'] = $counts[$id]; |
|
469 | 1 | } |
|
470 | 1 | $nest = $hier->nestify($tree); |
|
471 | 1 | $slice = array(); |
|
472 | 1 | foreach ($nest as $id => $subs) { |
|
473 | 1 | if ($tree[$id]['count'] > 0) { |
|
474 | 1 | $slice[$id] = $tree[$id]['count']; |
|
475 | 1 | } |
|
476 | 1 | } |
|
477 | 1 | arsort($slice); |
|
478 | 1 | if (is_int($params)) { |
|
479 | 1 | $slice = array_slice($slice, 0, $params, true); |
|
480 | 1 | } |
|
481 | 1 | foreach ($nest as $id => $subs) { |
|
482 | 1 | if (!isset($slice[$id])) { |
|
483 | 1 | unset($nest[$id]); |
|
484 | 1 | } |
|
485 | 1 | } |
|
486 | 1 | if (!empty($slice)) { |
|
487 | 1 | $posts = $this->query('categories', array( |
|
488 | 1 | 'nest' => $nest, |
|
489 | 1 | 'tree' => $tree, |
|
490 | 1 | )); |
|
491 | 1 | } |
|
492 | 1 | break; |
|
493 | } |
||
494 | |||
495 | 8 | return $posts; |
|
496 | } |
||
497 | |||
498 | /** |
||
499 | * Analyzes the blog file $path and performes any database CRUD operations that may be needed. We do this every time a blog page is visited, but this helps to speed up the process if you are doing things programatically. If you are making lots of changes, then just delete the Blog.db file and everything will be updated. |
||
500 | * |
||
501 | * @param string $path The url (seo) path, NOT a file system path. |
||
502 | * |
||
503 | * @return false|int False if the $path does not exist, or the database id if it does. |
||
504 | */ |
||
505 | 22 | public function file($path) |
|
506 | { |
||
507 | 22 | if (empty($path)) { |
|
508 | 2 | $path = 'index'; |
|
509 | 2 | } |
|
510 | 22 | $blog = $this->db->row('SELECT id, path, updated, search, content FROM blog WHERE path = ?', $path, 'assoc'); |
|
511 | 22 | if (!$current = $this->blogInfo($path)) { |
|
512 | 15 | if ($blog) { // then remove |
|
513 | 2 | $this->db->exec('DELETE FROM blog WHERE id = ?', $blog['id']); |
|
514 | 1 | $this->db->exec('DELETE FROM tagged WHERE blog_id = ?', $blog['id']); |
|
515 | 1 | $sitemap = new Sitemap(); |
|
516 | 1 | $sitemap->delete($blog['path']); |
|
517 | 1 | unset($sitemap); |
|
518 | 1 | } |
|
519 | |||
520 | 15 | return false; |
|
521 | } |
||
522 | |||
523 | 8 | if ($blog) { // update maybe |
|
524 | 8 | foreach (array('updated', 'search', 'content') as $field) { |
|
525 | 8 | if ($current[$field] != $blog[$field]) { |
|
526 | 1 | $updated = $this->db->update('blog', 'id', array($blog['id'] => $current)); |
|
527 | 1 | $this->db->exec('DELETE FROM tagged WHERE blog_id = ?', $blog['id']); |
|
528 | 1 | break; |
|
529 | } |
||
530 | 8 | } |
|
531 | 8 | if (!isset($updated)) { |
|
532 | 8 | return $blog['id']; |
|
533 | } |
||
534 | 1 | } else { // insert |
|
535 | 1 | $blog = $current; |
|
536 | 1 | $blog['id'] = $this->db->insert('blog', $current); |
|
537 | } |
||
538 | |||
539 | 1 | if (!empty($current['keywords'])) { |
|
540 | 1 | $tags = array_filter(array_map('trim', explode(',', $current['keywords']))); |
|
541 | 1 | foreach ($tags as $tag) { |
|
542 | 1 | $this->db->insert('tagged', array( |
|
543 | 1 | 'blog_id' => $blog['id'], |
|
544 | 1 | 'tag_id' => $this->getId('tags', $tag), |
|
545 | 1 | )); |
|
546 | 1 | } |
|
547 | 1 | } |
|
548 | 1 | $sitemap = new Sitemap(); |
|
549 | 1 | if (!$current['search']) { |
|
550 | 1 | $sitemap->delete($blog['path']); |
|
551 | 1 | } else { |
|
552 | 1 | $category = 'blog'; |
|
553 | 1 | if ($current['category_id'] > 0) { |
|
554 | 1 | $category .= '/'.array_search($current['category_id'], $this->ids['categories']); |
|
555 | 1 | } |
|
556 | 1 | $sitemap->upsert($category, array( |
|
557 | 1 | 'id' => $blog['id'], |
|
558 | 1 | 'path' => $blog['path'], |
|
559 | 1 | 'title' => $current['title'], |
|
560 | 1 | 'description' => $current['description'], |
|
561 | 1 | 'keywords' => $current['keywords'], |
|
562 | 1 | 'thumb' => $current['thumb'], |
|
563 | 1 | 'content' => $current['content'], |
|
564 | 1 | 'updated' => $current['updated'], |
|
565 | 1 | )); |
|
566 | } |
||
567 | 1 | unset($sitemap); |
|
568 | 1 | $this->updateConfig(); |
|
569 | |||
570 | 1 | return $blog['id']; |
|
571 | } |
||
572 | |||
573 | /** |
||
574 | * Gets all of the information we have for the blog $id(s) you supply. |
||
575 | * |
||
576 | * @param int|int[] $ids That correspond to the database. |
||
577 | * |
||
578 | * @return array A single row of information if $ids is not an array, or multiple rows foreach id in the same order given that you can loop through. |
||
579 | */ |
||
580 | 20 | public function info($ids) |
|
581 | { |
||
582 | 20 | $page = Page::html(); |
|
583 | 20 | $single = (is_array($ids)) ? false : true; |
|
584 | 20 | if (empty($ids)) { |
|
585 | 1 | return array(); |
|
586 | } |
||
587 | 20 | $ids = (array) $ids; |
|
588 | 20 | $posts = array_flip($ids); |
|
589 | 20 | foreach ($this->db->all(array( |
|
590 | 20 | 'SELECT b.id, b.path, b.thumb, b.title, b.description, b.content, ABS(b.updated) AS updated, ABS(b.featured) AS featured, ABS(b.published) AS published,', |
|
591 | 20 | ' a.id AS author_id, a.path AS author_path, a.name AS author_name,', |
|
592 | 20 | ' (SELECT p.path || "," || p.title FROM blog AS p WHERE p.featured = b.featured AND p.published > b.published AND p.published < 0 ORDER BY p.featured, p.published ASC LIMIT 1) AS previous,', |
|
593 | 20 | ' (SELECT n.path || "," || n.title FROM blog AS n WHERE n.featured = b.featured AND n.published < b.published AND n.published < 0 ORDER BY n.featured, n.published DESC LIMIT 1) AS next,', |
|
594 | 20 | ' (SELECT GROUP_CONCAT(p.path) FROM categories AS c INNER JOIN categories AS p WHERE c.lft BETWEEN p.lft AND p.rgt AND c.id = b.category_id ORDER BY c.lft) AS category_paths,', |
|
595 | 20 | ' (SELECT GROUP_CONCAT(p.name) FROM categories AS c INNER JOIN categories AS p WHERE c.lft BETWEEN p.lft AND p.rgt AND c.id = b.category_id ORDER BY c.lft) AS category_names,', |
|
596 | 20 | ' (SELECT GROUP_CONCAT(t.path) FROM tagged INNER JOIN tags AS t ON tagged.tag_id = t.id WHERE tagged.blog_id = b.id) AS tag_paths,', |
|
597 | 20 | ' (SELECT GROUP_CONCAT(t.name) FROM tagged INNER JOIN tags AS t ON tagged.tag_id = t.id WHERE tagged.blog_id = b.id) AS tag_names', |
|
598 | 20 | 'FROM blog AS b', |
|
599 | 20 | 'LEFT JOIN authors AS a ON b.author_id = a.id', |
|
600 | 20 | 'WHERE b.id IN('.implode(', ', $ids).')', |
|
601 | 20 | ), '', 'assoc') as $row) { |
|
602 | $post = array( |
||
603 | 20 | 'page' => true, // until proven otherwise |
|
604 | 20 | 'path' => $row['path'], |
|
605 | 20 | 'url' => $page->url('base', $row['path']), |
|
606 | 20 | 'thumb' => $row['thumb'], |
|
607 | 20 | 'title' => $row['title'], |
|
608 | 20 | 'description' => $row['description'], |
|
609 | 20 | 'content' => $row['content'], |
|
610 | 20 | 'updated' => $row['updated'], |
|
611 | 20 | 'featured' => ($row['featured'] > 0) ? true : false, |
|
612 | 20 | 'published' => ($row['published'] > 1) ? $row['published'] : (($row['published'] == 1) ? true : false), |
|
613 | 20 | 'categories' => array(), |
|
614 | 20 | 'tags' => array(), |
|
615 | 20 | ); |
|
616 | 20 | if (!empty($row['category_paths'])) { |
|
617 | 16 | $cats = array_combine(explode(',', $row['category_paths']), explode(',', $row['category_names'])); |
|
618 | 16 | foreach ($cats as $path => $name) { |
|
619 | 16 | $post['categories'][] = $this->configInfo('categories', $path, $name); |
|
620 | 16 | } |
|
621 | 16 | } |
|
622 | 20 | if (!empty($row['tag_paths'])) { |
|
623 | 17 | $tags = array_combine(explode(',', $row['tag_paths']), explode(',', $row['tag_names'])); |
|
624 | 17 | foreach ($tags as $path => $name) { |
|
625 | 17 | $post['tags'][] = $this->configInfo('tags', $path, $name); |
|
626 | 17 | } |
|
627 | 17 | } |
|
628 | 20 | if ($row['published'] > 1) { |
|
629 | 17 | $post['page'] = false; |
|
630 | 17 | $post['author'] = $this->configInfo('authors', $row['author_path'], $row['author_name']); |
|
631 | 17 | $post['archive'] = $page->url('blog/listings', 'archives', date('Y/m/d/', $row['published'])); |
|
632 | 17 | $post['previous'] = $row['previous']; |
|
633 | 17 | $post['next'] = $row['next']; |
|
634 | 17 | if ($post['previous']) { |
|
635 | 11 | $previous = explode(',', $post['previous']); |
|
636 | 11 | $post['previous'] = array( |
|
637 | 11 | 'url' => $page->url('base', array_shift($previous)), |
|
638 | 11 | 'title' => implode(',', $previous), |
|
639 | ); |
||
640 | 11 | } |
|
641 | 17 | if ($post['next']) { |
|
642 | 14 | $next = explode(',', $post['next']); |
|
643 | 14 | $post['next'] = array( |
|
644 | 14 | 'url' => $page->url('base', array_shift($next)), |
|
645 | 14 | 'title' => implode(',', $next), |
|
646 | ); |
||
647 | 14 | } |
|
648 | 17 | } |
|
649 | 20 | $posts[$row['id']] = $post; |
|
650 | 20 | } |
|
651 | |||
652 | 20 | return ($single) ? array_shift($posts) : $posts; |
|
653 | } |
||
654 | |||
655 | 27 | public function config($table = null, $path = null, $value = null) |
|
656 | { |
||
657 | 27 | if (is_null($this->config)) { |
|
658 | 24 | $file = $this->folder.'config.yml'; |
|
659 | 24 | $this->config = (is_file($file)) ? (array) Yaml::parse(file_get_contents($file)) : array(); |
|
660 | foreach (array( |
||
661 | 24 | 'listings' => 'blog', |
|
662 | 24 | 'breadcrumb' => 'Blog', |
|
663 | 24 | 'name' => '', |
|
664 | 24 | 'thumb' => '', |
|
665 | 24 | 'summary' => '', |
|
666 | 24 | ) as $key => $val) { |
|
667 | 24 | if (!isset($this->config['blog'][$key]) || is_array($this->config['blog'][$key])) { |
|
668 | 2 | $this->config['blog'][$key] = $val; |
|
669 | 2 | } |
|
670 | 24 | } |
|
671 | 24 | $page = Page::html(); |
|
672 | 24 | foreach (array('authors', 'categories', 'tags') as $param) { |
|
673 | 24 | if (!isset($this->config[$param]) || !is_array($this->config[$param])) { |
|
674 | 1 | $this->config[$param] = array(); |
|
675 | 1 | } |
|
676 | 24 | foreach ($this->config[$param] as $key => $val) { |
|
677 | 23 | if (is_string($val)) { |
|
678 | 23 | $this->config[$param][$key] = array('name' => $val); |
|
679 | 23 | } |
|
680 | 24 | } |
|
681 | 24 | } |
|
682 | 24 | } |
|
683 | 27 | switch (func_num_args()) { |
|
684 | 27 | case 0: |
|
685 | 1 | $param = $this->config; |
|
686 | 1 | break; |
|
687 | 27 | case 1: |
|
688 | 25 | $param = (isset($this->config[$table])) ? $this->config[$table] : null; |
|
689 | 25 | break; |
|
690 | 27 | case 2: |
|
691 | 27 | $param = (isset($this->config[$table][$path])) ? $this->config[$table][$path] : null; |
|
692 | 27 | break; |
|
693 | 3 | default: |
|
694 | 3 | $param = (isset($this->config[$table][$path][$value])) ? $this->config[$table][$path][$value] : null; |
|
695 | 3 | break; |
|
696 | 27 | } |
|
697 | |||
698 | 27 | return $param; |
|
699 | } |
||
700 | |||
701 | 22 | private function configInfo($table, $path, $name) |
|
723 | |||
724 | 23 | private function blogInfo($path) |
|
768 | |||
769 | 2 | private function updateDatabase() |
|
770 | { |
||
771 | 2 | $blog = $this->db->insert('blog', array('path', 'title', 'description', 'keywords', 'theme', 'thumb', 'featured', 'published', 'updated', 'author_id', 'category_id', 'search', 'content')); |
|
772 | 2 | $tagged = $this->db->insert('tagged', array('blog_id', 'tag_id')); |
|
773 | 2 | $sitemap = new Sitemap(); |
|
774 | 2 | $sitemap->reset('blog'); |
|
775 | 2 | $finder = new Finder(); |
|
776 | 2 | $finder->files()->in($this->folder.'content')->name('index.tpl')->sortByName(); |
|
777 | 2 | foreach ($finder as $file) { |
|
778 | 1 | $path = str_replace('\\', '/', $file->getRelativePath()); |
|
779 | 1 | if ($info = $this->blogInfo($path)) { |
|
780 | 1 | $id = $this->db->insert($blog, array_values($info)); |
|
781 | 1 | if (!empty($info['keywords'])) { |
|
782 | 1 | $tags = array_filter(array_map('trim', explode(',', $info['keywords']))); |
|
783 | 1 | foreach ($tags as $tag) { |
|
784 | 1 | $this->db->insert($tagged, array($id, $this->getId('tags', $tag))); |
|
785 | 1 | } |
|
786 | 1 | } |
|
787 | 1 | $category = 'blog'; |
|
788 | 1 | if ($info['category_id'] > 0) { |
|
789 | 1 | $category .= '/'.array_search($info['category_id'], $this->ids['categories']); |
|
790 | 1 | } |
|
791 | 1 | if ($info['search']) { |
|
792 | 1 | $sitemap->upsert($category, array( |
|
793 | 1 | 'id' => $id, |
|
794 | 1 | 'path' => $info['path'], |
|
795 | 1 | 'title' => $info['title'], |
|
796 | 1 | 'description' => $info['description'], |
|
797 | 1 | 'keywords' => $info['keywords'], |
|
798 | 1 | 'thumb' => $info['thumb'], |
|
799 | 1 | 'content' => $info['content'], |
|
800 | 1 | 'updated' => $info['updated'], |
|
801 | 1 | )); |
|
802 | 1 | } |
|
803 | 1 | } |
|
804 | 2 | } |
|
805 | 2 | $sitemap->delete(); |
|
806 | 2 | unset($sitemap); |
|
807 | 2 | $this->db->close($blog); |
|
808 | 2 | $this->db->close($tagged); |
|
809 | 2 | $this->updateConfig(); |
|
810 | 2 | } |
|
811 | |||
812 | 10 | private function getId($table, $value) |
|
870 | |||
871 | 3 | private function updateConfig() |
|
872 | { |
||
873 | 3 | if ($this->getId('updated', 'anything') === false) { |
|
874 | 2 | return; |
|
875 | } |
||
954 | } |
||
955 |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.