|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Modules\Tools\Handler { |
|
4
|
|
|
|
|
5
|
|
|
use Frames, Modules\Entitizer, Modules\Tools, Date, DB, Explorer; |
|
6
|
|
|
|
|
7
|
|
|
class Sitemap extends Frames\Tools\Sitemap { |
|
8
|
|
|
|
|
9
|
|
|
# Get last modified |
|
10
|
|
|
|
|
11
|
|
View Code Duplication |
private function getLastModified() { |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
$selection = 'MAX(time_modified) as last_modified'; |
|
14
|
|
|
|
|
15
|
|
|
if (!(DB::select(TABLE_PAGES, $selection) && (DB::getLast()->rows === 1))) return 0; |
|
16
|
|
|
|
|
17
|
|
|
# ------------------------ |
|
18
|
|
|
|
|
19
|
|
|
return intval(DB::getLast()->getRow()['last_modified']); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
# Get pages |
|
23
|
|
|
|
|
24
|
|
|
private function getPages() { |
|
25
|
|
|
|
|
26
|
|
|
$selection = ['id', 'slug', 'time_modified']; |
|
27
|
|
|
|
|
28
|
|
|
$condition = ['visibility' => VISIBILITY_PUBLISHED, 'access' => ACCESS_PUBLIC, 'locked' => 0]; |
|
29
|
|
|
|
|
30
|
|
|
if (!(DB::select(TABLE_PAGES, $selection, $condition, 'slug') && DB::getLast()->status)) return; |
|
31
|
|
|
|
|
32
|
|
|
# ------------------------ |
|
33
|
|
|
|
|
34
|
|
|
while (null !== ($data = DB::getLast()->getRow())) yield Entitizer::dataset(TABLE_PAGES, $data); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
# Handle request |
|
38
|
|
|
|
|
39
|
|
|
protected function handle() { |
|
40
|
|
|
|
|
41
|
|
|
# Create sitemap |
|
42
|
|
|
|
|
43
|
|
|
$sitemap = new Tools\Sitemap(); |
|
44
|
|
|
|
|
45
|
|
|
# Load sitemap |
|
46
|
|
|
|
|
47
|
|
|
$file_name = (DIR_SYSTEM_DATA . 'Sitemap.xml'); |
|
48
|
|
|
|
|
49
|
|
|
$modified = Explorer::getModified($file_name); |
|
50
|
|
|
|
|
51
|
|
|
if ((false !== $modified) && ($modified > $this->getLastModified())) { |
|
52
|
|
|
|
|
53
|
|
|
if (false !== $sitemap->load($file_name)) return $sitemap; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
# Fill sitemap |
|
57
|
|
|
|
|
58
|
|
|
foreach ($this->getPages() as $page) { |
|
59
|
|
|
|
|
60
|
|
|
$loc = $page->canonical; $lastmod = Date::get(DATE_FORMAT_W3C, $page->time_modified); |
|
61
|
|
|
|
|
62
|
|
|
$sitemap->add($loc, $lastmod, FREQUENCY_WEEKLY, 0.5); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
# Save sitemap |
|
66
|
|
|
|
|
67
|
|
|
$sitemap->save($file_name); |
|
68
|
|
|
|
|
69
|
|
|
# ------------------------ |
|
70
|
|
|
|
|
71
|
|
|
return $sitemap; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.