1: <?php defined('_JOOS_CORE') or exit();
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16: class modelSitemap
17: {
18: 19: 20: 21:
22: public $spaces = array();
23: 24: 25: 26:
27:
28: 29: 30: 31:
32: public $nodes = array();
33:
34: 35: 36: 37:
38: private $xml_nodes = array();
39:
40: 41: 42: 43: 44:
45: private $counters = 0;
46:
47: 48: 49: 50: 51:
52: private $config = array(
53: 'max_elemets_in_map' => 50000
54: );
55:
56: 57: 58:
59: public function __construct()
60: {
61: ini_set('memory_limit', '500M');
62: ini_set("max_execution_time", "16000");
63: set_time_limit(16000);
64:
65:
66:
67:
68: $this->spaces = array('blog', 'news', 'content', 'contacts');
69:
70: }
71:
72: 73: 74: 75: 76:
77: public static function get_map($xml = false)
78: {
79:
80: $map = new self;
81:
82:
83: foreach ($map->spaces as $space) {
84:
85:
86: require_once JPATH_APP_PLUGINS_BASE . DS . 'sitemap' . DS . $space . '.php';
87:
88:
89: $model = 'pluginSitemap' . joosInflector::camelize($space);
90:
91:
92: $params = $model::get_params();
93: $params['xml'] = $xml;
94:
95:
96:
97: foreach ($model::get_mapdata_scheme($params) as $map_block) {
98:
99:
100:
101:
102: $data = $map_block['type'] == 'single' ? null : call_user_func($map_block['call_from'], isset($map_block['call_params']) ? $map_block['call_params'] : null );
103:
104:
105: $map->add_mapblock($space, $map_block, $data);
106: }
107: }
108:
109: return $map;
110: }
111:
112: 113: 114: 115: 116: 117: 118: 119:
120: public function add_mapblock($space, $block, $data = null)
121: {
122: if ($data) {
123: $level = $block['level'];
124:
125: if ($block['type'] == 'single_children') {
126: $this->nodes[$space][] = '<a class="maplevel-' . $level . ' mapblock_id-' . $block['id'] . '" href="' . $block['link'] . '">' . $block['title'] . '</a>';
127: $block['level'] = $block['level'] + 1;
128: }
129:
130: foreach ($data as $item) {
131: if (is_array($item)) {
132:
133: $this->add_mapblock($space, $block, $item);
134: } else {
135: $level = isset($item->level_up) ? ( $block['level'] + $item->level_up ) : $block['level'];
136: $level = isset($item->level) ? ( $item->level ) : $level;
137:
138: $this->nodes[$space][] = '<a class="maplevel-' . $level . ' mapblock_id-' . $block['id'] . '" href="' . $item->loc . '">' . $item->title . '</a>';
139:
140: $this->xml_nodes[$space][] = '
141: <url>
142: <loc>' . $item->loc . '</loc>
143: <lastmod>' . $item->lastmod . '</lastmod>
144: <changefreq>' . $block['changefreq'] . '</changefreq>
145: <priority>' . $block['priority'] . '</priority>
146: </url>';
147: }
148: ++$this->counters;
149: }
150: } else {
151: $level = isset($item->level_up) ? ( $block['level'] + $item->level_up ) : $block['level'];
152: $level = isset($item->level) ? ( $item->level ) : $level;
153:
154: if ($block['link']) {
155: $this->nodes[$space][] = '<a class="maplevel-' . $level . ' mapblock_id-' . $block['id'] . '" href="' . $block['link'] . '">' . $block['title'] . '</a>';
156: } else {
157: $this->nodes[$space][] = '<span class="maplevel-' . $level . ' mapblock_id-' . $block['id'] . '" >' . $block['title'] . '</span>';
158: }
159:
160: $this->xml_nodes[$space][] = '
161: <url>
162: <loc>' . $block['link'] . '</loc>
163: <lastmod>' . date('Y-m-d') . '</lastmod>
164: <changefreq>' . $block['changefreq'] . '</changefreq>
165: <priority>' . $block['priority'] . '</priority>
166: </url>';
167: }
168: ++$this->counters;
169: }
170:
171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185:
186: public function xml_output()
187: {
188: is_dir(JPATH_BASE . '/cache/sitemaps') ? null : mkdir(JPATH_BASE . '/cache/sitemaps', 0777, true);
189:
190: $counter = 0;
191: $map_num = 0;
192:
193: $elemets = array();
194: foreach ($this->xml_nodes as $nodes) {
195: foreach ($nodes as $node) {
196: if ($counter == $this->config['max_elemets_in_map']) {
197: ++$map_num;
198: $this->xml_create($elemets, $map_num);
199: $elemets = array();
200: $counter = 0;
201: }
202: $elemets[] = $node;
203: ++$counter;
204: }
205: }
206:
207: $this->xml_create($elemets, $map_num + 1);
208:
209: if ($this->counters > $this->config['max_elemets_in_map']) {
210: $this->root_xml_create($this->counters);
211: }
212: }
213:
214: private function xml_create(array $sitemap, $num = 0)
215: {
216: $filename = ( $num == 0 || $this->counters < $this->config['max_elemets_in_map'] ) ? JPATH_BASE . DS . 'sitemap.xml' : JPATH_BASE . DS . 'cache/sitemaps/sitemap-' . $num . '.xml';
217:
218: $xml = array();
219: $xml[] = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
220: $xml[] = implode('', $sitemap);
221: $xml[] = "\n</urlset>";
222:
223: $xml = new SimpleXMLElement(implode("\n", $xml));
224: $xml->asXML($filename);
225: }
226:
227: private function root_xml_create($map_number)
228: {
229:
230:
231: $map_count = ceil($this->counters / $this->config['max_elemets_in_map']);
232:
233: $sitemap = array();
234: for ($index = 1; $index < $map_count + 1; $index++) {
235: $sitemap[] = sprintf('<sitemap><loc>%s/cache/sitemaps/sitemap-%s.xml</loc></sitemap>', JPATH_SITE, $index);
236: }
237:
238: $xml = array();
239: $xml[] = '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
240: $xml[] = implode("\n", $sitemap);
241: $xml[] = "</sitemapindex>";
242:
243:
244:
245: $xml = new SimpleXMLElement(implode("\n", $xml));
246: $xml->asXML(JPATH_BASE . DS . 'sitemap.xml');
247: }
248:
249: }
250: