1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK ---------------------------------- |
---|
3 | # |
---|
4 | # This file is part of Sitemaps, a plugin for DotClear2. |
---|
5 | # Copyright (c) 2006-2009 Pep and contributors. |
---|
6 | # Licensed under the GPL version 2.0 license. |
---|
7 | # See LICENSE file or |
---|
8 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
9 | # |
---|
10 | # -- END LICENSE BLOCK ------------------------------------ |
---|
11 | class dcSitemaps |
---|
12 | { |
---|
13 | protected $core; |
---|
14 | protected $blog; |
---|
15 | protected $urls; |
---|
16 | protected $freqs; |
---|
17 | protected $post_types; |
---|
18 | |
---|
19 | public function __construct($core) |
---|
20 | { |
---|
21 | $this->core = $core; |
---|
22 | $this->blog = $core->blog; |
---|
23 | |
---|
24 | $this->urls = array(); |
---|
25 | $this->freqs = array('','always','hourly','daily','weekly','monthly','never'); |
---|
26 | $post_types = array(); |
---|
27 | |
---|
28 | // Default post types |
---|
29 | $this->addPostType( |
---|
30 | 'post', |
---|
31 | $this->blog->url.$this->core->url->getBase('post').'/', |
---|
32 | $this->blog->settings->sitemaps_posts_fq, |
---|
33 | $this->blog->settings->sitemaps_posts_pr |
---|
34 | ); |
---|
35 | $this->addPostType( |
---|
36 | 'page', |
---|
37 | $this->blog->url.$this->core->url->getBase('pages').'/', |
---|
38 | $this->blog->settings->sitemaps_pages_fq, |
---|
39 | $this->blog->settings->sitemaps_pages_pr |
---|
40 | ); |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | public function getURLs() |
---|
45 | { |
---|
46 | if ($this->blog->settings->sitemaps_active && empty($this->urls)) { |
---|
47 | $this->collectURLs(); |
---|
48 | } |
---|
49 | return $this->urls; |
---|
50 | } |
---|
51 | |
---|
52 | public function addPostType($type,$base_url,$freq = 0, $priority = 0.3) |
---|
53 | { |
---|
54 | if (preg_match('!^([a-z_-]+)$!',$type)) { |
---|
55 | $this->post_types[$type]['base_url'] = $base_url; |
---|
56 | $this->post_types[$type]['frequency'] = $this->getFrequency($freq); |
---|
57 | $this->post_types[$type]['priority'] = $this->getPriority($priority); |
---|
58 | return true; |
---|
59 | } |
---|
60 | return false; |
---|
61 | } |
---|
62 | |
---|
63 | public function addEntry($loc,$priority,$frequency,$lastmod = '') |
---|
64 | { |
---|
65 | $this->urls[] = array( |
---|
66 | 'loc' => $loc, |
---|
67 | 'priority' => $priority, |
---|
68 | 'frequency' => ($frequency == '')?null:$frequency, |
---|
69 | 'lastmod' => ($lastmod == '')?null:$lastmod |
---|
70 | ); |
---|
71 | } |
---|
72 | |
---|
73 | public function getPriority($value) |
---|
74 | { |
---|
75 | return(sprintf('%.1f',min(abs((float)$value),1))); |
---|
76 | } |
---|
77 | |
---|
78 | public function getFrequency($value) |
---|
79 | { |
---|
80 | return $this->freqs[min(abs(intval($value)),6)]; |
---|
81 | } |
---|
82 | |
---|
83 | public function collectEntriesURLs($type = 'post') |
---|
84 | { |
---|
85 | if (!array_key_exists($type,$this->post_types)) { |
---|
86 | return; |
---|
87 | } |
---|
88 | |
---|
89 | $freq = $this->post_types[$type]['frequency']; |
---|
90 | $prio = $this->post_types[$type]['priority']; |
---|
91 | $base_url = $this->post_types[$type]['base_url']; |
---|
92 | |
---|
93 | // Let's have fun ! |
---|
94 | $query = |
---|
95 | "SELECT p.post_id, p.post_url, p.post_tz, ". |
---|
96 | "p.post_upddt, MAX(c.comment_upddt) AS comments_dt ". |
---|
97 | "FROM ".$this->blog->prefix."post AS p ". |
---|
98 | "LEFT OUTER JOIN ".$this->blog->prefix."comment AS c ON c.post_id = p.post_id ". |
---|
99 | "WHERE p.blog_id = '".$this->blog->con->escape($this->blog->id)."' ". |
---|
100 | "AND p.post_type = '".$type."' AND p.post_status = 1 AND p.post_password IS NULL ". |
---|
101 | 'GROUP BY p.post_id, p.post_url, p.post_tz, p.post_upddt, p.post_dt '. |
---|
102 | 'ORDER BY p.post_dt ASC'; |
---|
103 | |
---|
104 | $rs = $this->blog->con->select($query); |
---|
105 | while ($rs->fetch()) { |
---|
106 | if ($rs->comments_dt !== null) { |
---|
107 | $last_ts = max(strtotime($rs->post_upddt),strtotime($rs->comments_dt)); |
---|
108 | } else { |
---|
109 | $last_ts = strtotime($rs->post_upddt); |
---|
110 | } |
---|
111 | $last_dt = dt::iso8601($last_ts,$rs->post_tz); |
---|
112 | $url = $base_url.html::sanitizeURL($rs->post_url); |
---|
113 | $this->addEntry($url,$prio,$freq,$last_dt); |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | protected function collectURLs() |
---|
118 | { |
---|
119 | // Homepage URL |
---|
120 | if ($this->blog->settings->sitemaps_home_url) |
---|
121 | { |
---|
122 | $freq = $this->getFrequency($this->blog->settings->sitemaps_home_fq); |
---|
123 | $prio = $this->getPriority($this->blog->settings->sitemaps_home_pr); |
---|
124 | |
---|
125 | $this->addEntry($this->blog->url,$prio,$freq); |
---|
126 | } |
---|
127 | |
---|
128 | // Main syndication feeds URLs |
---|
129 | if ($this->core->blog->settings->sitemaps_feeds_url) |
---|
130 | { |
---|
131 | $freq = $this->getFrequency($this->blog->settings->sitemaps_feeds_fq); |
---|
132 | $prio = $this->getPriority($this->blog->settings->sitemaps_feeds_pr); |
---|
133 | |
---|
134 | $this->addEntry( |
---|
135 | $this->blog->url.$this->core->url->getBase('feed').'/rss2', |
---|
136 | $prio,$freq); |
---|
137 | $this->addEntry( |
---|
138 | $this->blog->url.$this->core->url->getBase('feed').'/atom', |
---|
139 | $prio,$freq); |
---|
140 | } |
---|
141 | |
---|
142 | // Posts entries URLs |
---|
143 | if ($this->core->blog->settings->sitemaps_posts_url) { |
---|
144 | $this->collectEntriesURLs('post'); |
---|
145 | } |
---|
146 | |
---|
147 | // Pages entries URLs |
---|
148 | if ($this->core->plugins->moduleExists('pages') && $this->core->blog->settings->sitemaps_pages_url) { |
---|
149 | $this->collectEntriesURLs('page'); |
---|
150 | } |
---|
151 | |
---|
152 | // Categories URLs |
---|
153 | if ($this->core->blog->settings->sitemaps_cats_url) |
---|
154 | { |
---|
155 | $freq = $this->getFrequency($this->blog->settings->sitemaps_cats_fq); |
---|
156 | $prio = $this->getPriority($this->blog->settings->sitemaps_cats_pr); |
---|
157 | |
---|
158 | $cats = $this->blog->getCategories(); |
---|
159 | while ($cats->fetch()) { |
---|
160 | $this->addEntry( |
---|
161 | $this->blog->url.$this->core->url->getBase("category")."/".$cats->cat_url, |
---|
162 | $prio,$freq); |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | if ($this->core->plugins->moduleExists('metadata') && $this->core->blog->settings->sitemaps_tags_url) |
---|
167 | { |
---|
168 | $freq = $this->getFrequency($this->blog->settings->sitemaps_tags_fq); |
---|
169 | $prio = $this->getPriority($this->blog->settings->sitemaps_tags_pr); |
---|
170 | |
---|
171 | $meta = new dcMeta($this->core); |
---|
172 | $tags = $meta->getMeta('tag'); |
---|
173 | while ($tags->fetch()) { |
---|
174 | $this->addEntry( |
---|
175 | $this->blog->url.$this->core->url->getBase("tag")."/".rawurlencode($tags->meta_id), |
---|
176 | $prio,$freq); |
---|
177 | } |
---|
178 | } |
---|
179 | |
---|
180 | // External parts ? |
---|
181 | # --BEHAVIOR-- packagerAfterCreate |
---|
182 | $this->core->callBehavior('sitemapsURLsCollect', $this); |
---|
183 | } |
---|
184 | } |
---|
185 | ?> |
---|