-
Notifications
You must be signed in to change notification settings - Fork 0
/
sitemap.php
161 lines (135 loc) · 4.54 KB
/
sitemap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/**
* Created by PhpStorm.
* User: myindexlike
* Date: 09.03.2016
* Time: 3:37
*/
class sitemap
{
const COUNTINFILE = 5000;
private $folder;
private $parent;
function __construct($parent,$folder='xml') {
$this->parent=$parent;
$this->path_folder=MODX_BASE_PATH.$folder.'/';
$this->url_folder=MODX_SITE_URL.$folder.'/';
}
/**
* Создаем файл xml по родителю
*
* @return bool|string
*/
public function creatingXML (){
global $modx;
$list = array();
$mes ='';
// получаем количество товаров
$count = $this->CountChildren ($this->parent);
//echo '$count='.$count;
if ($count!=FALSE){
$iteration = ceil($count/self::COUNTINFILE); // получаем количество итераций - файлов
for ($x=0; $x<$iteration; $x++) {
$list=$this->getURLlist($this->parent, $x*self::COUNTINFILE, self::COUNTINFILE);
if (count($list)>0){
$li='';
foreach ($list as $key => $value) {
$li .= implode('', $value);
}
// var_dump($list);
$str ='<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'.$li.'</urlset>';
echo $str;
$mes = $this->saveFile ($str,$this->parent.'-'.$x);
}
}
}
// return $mes;
}
/**
* подсчитываем количество дочерних документов (прямых потомков)
* @param $parents
*
* @return bool
*/
private function CountChildren ($parents){
global $modx;
$result=$modx->db->query("SELECT count(*) as cc
FROM " . $modx->getFullTableName('site_content') . "
WHERE `parent` in (".$parents.") and `deleted`=0 and `published`=1");
while ($row=$modx->db->getRow($result)) {
return $row['cc'];
}
return FALSE;
}
/**
* Формируем список ссылок
*
* @param $parents
* @param int $start
* @param int $kolvo
*
* @return array
*/
function getURLlist ($parents, $start=0, $kolvo=self::COUNTINFILE, $k=0){
global $modx;
$k ++;
$list = array();
$result=$modx->db->query("SELECT `id`, `isfolder`, `parent`, `template`,
FROM_UNIXTIME(`editedon`, '%Y-%m-%d') as lastmod
FROM " . $modx->getFullTableName('site_content') . "
WHERE `parent` in (".$parents.") and `deleted`=0 and `published`=1 limit ".$start.", ".$kolvo." ");
while ($row=$modx->db->getRow($result)) {
$url = $modx->makeUrl($row['id'], '', '', 'full');
$changefreq = $this->getChangefreq($row['template']);
$priority = $this->getPriority($row['template'],$k);
$list[$row['parent']][] ='<url><loc>'.$url.'</loc><lastmod>'.$row['lastmod'].'</lastmod>'.$changefreq.''.$priority.'</url>';
if ($row['isfolder']==1){
$list_dop = $this->getURLlist($row['id'], 0, self::COUNTINFILE, $k);
if (is_array($list_dop)){
$list=array_merge($list, $list_dop);
}
}
}
return $list;
}
function getChangefreq($template){
return '<changefreq>weekly</changefreq>';
}
function getPriority($template, $k){
$p = 1;
switch ($k) {
case 0:
$p='1';
break;
case 1:
$p='0.9';
break;
case 2:
$p='0.8';
break;
case 3:
$p='0.7';
break;
case 4:
$p='0.6';
break;
}
return '<priority>'.$p.'</priority>';
}
/**
* @param $list
* @param $parentid
*
* @return string
*/
function saveFile ($list,$parentid){
$fp = fopen($this->path_folder.$parentid.'.xml', 'w');
// var_dump($this->path_folder.$parentid.'.xml');
$test = fwrite($fp, $list);
if ($test) $mes= 'Данные в файл успешно занесены.';
else $mes= 'Ошибка при записи в файл.';
fclose($fp); //Закрытие файла
return $mes;
}
}