8a7e6ecf
Yarik
Namespaces
|
1
2
|
<?php
|
7c3b817e
Administrator
full commit
|
3
|
namespace artweb\artbox\ecommerce\console;
|
8a7e6ecf
Yarik
Namespaces
|
4
|
|
9580e548
Alexey Boroda
-Site map in prog...
|
5
6
|
use artweb\artbox\ecommerce\models\Brand;
use artweb\artbox\ecommerce\models\ProductVariant;
|
29f1675f
Administrator
site map
|
7
|
use artweb\artbox\language\models\Language;
|
8a7e6ecf
Yarik
Namespaces
|
8
9
|
use artweb\artbox\seo\models\Seo;
use artweb\artbox\ecommerce\models\Category;
|
8a7e6ecf
Yarik
Namespaces
|
10
11
12
|
use Yii;
use artweb\artbox\models\Page;
use yii\helpers\ArrayHelper;
|
d77e7532
Alexey Boroda
-sitemap try
|
13
|
use yii\helpers\Console;
|
8a7e6ecf
Yarik
Namespaces
|
14
15
16
|
use yii\helpers\Url;
use yii\console\Controller;
|
8a7e6ecf
Yarik
Namespaces
|
17
18
19
|
class SiteMapController extends Controller
{
|
ee2ceed3
Alexey Boroda
-Sitemap beta
|
20
|
private $urlList = [ 'https://extremstyle.ua/ru' ];
|
8a7e6ecf
Yarik
Namespaces
|
21
|
private $count = 1;
|
d77e7532
Alexey Boroda
-sitemap try
|
22
23
24
25
|
public $fileName;
public $handle;
public $mapNumber = 1;
public $mainMap = '';
|
ee2ceed3
Alexey Boroda
-Sitemap beta
|
26
|
public $content = '';
|
8a7e6ecf
Yarik
Namespaces
|
27
28
29
30
|
public function getAddStatic()
{
return [
|
4b7c8f53
Administrator
site map
|
31
|
Yii::$app->urlManager->baseUrl.'/ru',
|
8a7e6ecf
Yarik
Namespaces
|
32
33
|
];
}
|
29f1675f
Administrator
site map
|
34
35
36
37
38
|
public function getHost(){
return Yii::$app->urlManager->baseUrl.'/ru';
}
|
8a7e6ecf
Yarik
Namespaces
|
39
|
|
9580e548
Alexey Boroda
-Site map in prog...
|
40
|
public function getVariants()
|
8a7e6ecf
Yarik
Namespaces
|
41
|
{
|
9580e548
Alexey Boroda
-Site map in prog...
|
42
43
|
return ProductVariant::find()
->with('lang')
|
b2f4bd83
Administrator
add variantSku
|
44
|
->with('product.lang');
|
8a7e6ecf
Yarik
Namespaces
|
45
46
47
48
49
50
|
}
public function getSeoLinks()
{
return Seo::find()
|
db3f8716
Administrator
add variantSku
|
51
|
->where(['!=', 'meta', 'noindex,nofollow' ])
|
8a7e6ecf
Yarik
Namespaces
|
52
53
54
55
56
57
58
|
->all();
}
public function getStaticPages()
{
return Page::find()
|
9580e548
Alexey Boroda
-Site map in prog...
|
59
|
->with('lang')
|
8a7e6ecf
Yarik
Namespaces
|
60
61
62
63
64
65
|
->all();
}
public function getCategories()
{
return Category::find()
|
9580e548
Alexey Boroda
-Site map in prog...
|
66
|
->with('lang')
|
8a7e6ecf
Yarik
Namespaces
|
67
68
69
|
->all();
}
|
9580e548
Alexey Boroda
-Site map in prog...
|
70
|
public function getCategoriesWithFilters()
|
8a7e6ecf
Yarik
Namespaces
|
71
|
{
|
9580e548
Alexey Boroda
-Site map in prog...
|
72
73
74
75
|
return Category::find()
->with('lang')
->joinWith('taxGroups.lang')
->with('taxGroups.taxOptions.lang')
|
db3f8716
Administrator
add variantSku
|
76
|
->where(['!=', 'tax_group.meta_robots', 'noindex,nofollow' ])
|
4b7c8f53
Administrator
site map
|
77
|
->where(['tax_group.is_filter'=>true ])
|
9580e548
Alexey Boroda
-Site map in prog...
|
78
79
80
81
82
83
84
85
|
->all();
}
public function getBrands()
{
return Brand::find()
->joinWith('lang')
->all();
|
8a7e6ecf
Yarik
Namespaces
|
86
87
|
}
|
8a7e6ecf
Yarik
Namespaces
|
88
89
90
91
92
93
94
95
96
97
|
public function checkUrl($url)
{
if (!in_array($url, $this->urlList)) {
$this->urlList[] = $url;
return true;
} else {
return false;
}
}
|
ee2ceed3
Alexey Boroda
-Sitemap beta
|
98
|
public function createRow($url, $priority)
|
8a7e6ecf
Yarik
Namespaces
|
99
|
{
|
ee2ceed3
Alexey Boroda
-Sitemap beta
|
100
101
|
// if ($this->checkUrl($url)) {
if($this->count % 500 == 0) {
|
d77e7532
Alexey Boroda
-sitemap try
|
102
103
|
$this->stdout($this->count . " : ", Console::BOLD);
$this->stdout($url . "\n", Console::FG_YELLOW);
|
ee2ceed3
Alexey Boroda
-Sitemap beta
|
104
105
|
}
$this->content .= '<url>' . '<loc>' . $url . '</loc>' . '<lastmod>' . date(
|
8a7e6ecf
Yarik
Namespaces
|
106
|
'Y-m-d'
|
9580e548
Alexey Boroda
-Site map in prog...
|
107
|
) . '</lastmod>' . '<changefreq>Weekly</changefreq>' . '<priority>' . $priority . '</priority>' . '</url>';
|
d77e7532
Alexey Boroda
-sitemap try
|
108
|
$this->count++;
|
ee2ceed3
Alexey Boroda
-Sitemap beta
|
109
110
111
112
|
if ($this->count % 10000 == 0) {
$this->content .= '</urlset>';
$this->stdout('Added unset' . "\n", Console::FG_CYAN);
fwrite($this->handle, $this->content);
|
d77e7532
Alexey Boroda
-sitemap try
|
113
|
fclose($this->handle);
|
d77e7532
Alexey Boroda
-sitemap try
|
114
115
116
|
$this->mapNumber++;
$this->mainMap .= '<sitemap>'.
|
ee2ceed3
Alexey Boroda
-Sitemap beta
|
117
|
'<loc>https://extremstyle.ua/ru/' . $this->fileName . '</loc>'.
|
d77e7532
Alexey Boroda
-sitemap try
|
118
119
120
121
122
123
|
'<lastmod>' . date('Y-m-d') . '</lastmod>'.
'</sitemap>';
$this->fileName = 'sitemap' . $this->mapNumber . '.xml';
$this->handle = fopen(Yii::getAlias('@frontend') . '/web' . '/' . $this->fileName, "w");
|
ee2ceed3
Alexey Boroda
-Sitemap beta
|
124
|
$this->content = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
|
d77e7532
Alexey Boroda
-sitemap try
|
125
|
}
|
ee2ceed3
Alexey Boroda
-Sitemap beta
|
126
|
// }
|
8a7e6ecf
Yarik
Namespaces
|
127
128
129
130
|
}
public function actionProcess()
{
|
29f1675f
Administrator
site map
|
131
132
133
134
|
Language::setCurrent('ru');
|
8a7e6ecf
Yarik
Namespaces
|
135
136
137
138
139
140
|
$config = ArrayHelper::merge(
require( \Yii::getAlias('@frontend/config/') . 'main.php' ),
require( \Yii::getAlias('@common/config/') . 'main.php' )
);
Yii::$app->urlManager->addRules($config[ 'components' ][ 'urlManager' ][ 'rules' ]);
|
d77e7532
Alexey Boroda
-sitemap try
|
141
142
|
$this->mainMap = '<?xml version="1.0" encoding="UTF-8"?>';
$this->mainMap .= '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
|
ee2ceed3
Alexey Boroda
-Sitemap beta
|
143
|
$this->fileName = 'sitemap' . $this->mapNumber . '.xml';
|
8a7e6ecf
Yarik
Namespaces
|
144
|
setlocale(LC_ALL, 'ru_RU.CP1251');
|
d77e7532
Alexey Boroda
-sitemap try
|
145
|
$this->handle = fopen(Yii::getAlias('@frontend') . '/web' . '/' . $this->fileName, "w");
|
8a7e6ecf
Yarik
Namespaces
|
146
|
|
ee2ceed3
Alexey Boroda
-Sitemap beta
|
147
|
$this->content = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
|
8a7e6ecf
Yarik
Namespaces
|
148
149
|
foreach ($this->getAddStatic() as $page) {
|
ee2ceed3
Alexey Boroda
-Sitemap beta
|
150
|
$this->createRow($page, 1);
|
8a7e6ecf
Yarik
Namespaces
|
151
152
153
154
155
|
}
foreach ($this->getStaticPages() as $page) {
$url = Url::to(
[
|
9580e548
Alexey Boroda
-Site map in prog...
|
156
157
|
'site/page',
'slug' => $page->lang->alias,
|
8a7e6ecf
Yarik
Namespaces
|
158
159
|
]
);
|
ee2ceed3
Alexey Boroda
-Sitemap beta
|
160
|
$this->createRow($url, 1);
|
8a7e6ecf
Yarik
Namespaces
|
161
162
163
164
165
166
|
}
foreach ($this->getCategories() as $category) {
$url = Url::to(
[
'catalog/category',
|
9580e548
Alexey Boroda
-Site map in prog...
|
167
|
'category' => $category->lang->alias,
|
8a7e6ecf
Yarik
Namespaces
|
168
169
|
]
);
|
ee2ceed3
Alexey Boroda
-Sitemap beta
|
170
|
$this->createRow($url, 0.8);
|
9580e548
Alexey Boroda
-Site map in prog...
|
171
172
|
}
|
b2f4bd83
Administrator
add variantSku
|
173
|
foreach ($this->getVariants()->batch(1000) as $rows) {
|
9580e548
Alexey Boroda
-Site map in prog...
|
174
|
foreach ($rows as $row) {
|
ee2ceed3
Alexey Boroda
-Sitemap beta
|
175
176
177
|
if(!preg_match("@^[a-zA-Z\d]+$@i", $row->sku)) {
continue;
}
|
9580e548
Alexey Boroda
-Site map in prog...
|
178
179
180
181
182
183
184
|
$url = Url::to(
[
'catalog/product',
'product' => $row->product->lang->alias,
'variant' => $row->sku,
]
);
|
ee2ceed3
Alexey Boroda
-Sitemap beta
|
185
|
$this->createRow($url, 0.9);
|
9580e548
Alexey Boroda
-Site map in prog...
|
186
|
}
|
8a7e6ecf
Yarik
Namespaces
|
187
188
|
}
|
9580e548
Alexey Boroda
-Site map in prog...
|
189
|
foreach ($this->getBrands() as $brand) {
|
d77e7532
Alexey Boroda
-sitemap try
|
190
|
|
8a7e6ecf
Yarik
Namespaces
|
191
192
|
$url = Url::to(
[
|
9580e548
Alexey Boroda
-Site map in prog...
|
193
194
|
'brand/view',
'slug' => $brand->lang->alias,
|
8a7e6ecf
Yarik
Namespaces
|
195
196
|
]
);
|
ee2ceed3
Alexey Boroda
-Sitemap beta
|
197
|
$this->createRow($url, 0.7);
|
d77e7532
Alexey Boroda
-sitemap try
|
198
|
|
8a7e6ecf
Yarik
Namespaces
|
199
200
|
}
|
9580e548
Alexey Boroda
-Site map in prog...
|
201
202
203
|
foreach ($this->getCategoriesWithFilters() as $category) {
foreach ($category->taxGroups as $group) {
foreach ($group->taxOptions as $option) {
|
8a7e6ecf
Yarik
Namespaces
|
204
205
206
207
|
$url = Url::to(
[
'catalog/category',
'category' => $category,
|
9580e548
Alexey Boroda
-Site map in prog...
|
208
|
'filters' => [ $group->lang->alias => [ $option->lang->alias ] ],
|
8a7e6ecf
Yarik
Namespaces
|
209
210
|
]
);
|
ee2ceed3
Alexey Boroda
-Sitemap beta
|
211
|
$this->createRow($url, 0.8);
|
8a7e6ecf
Yarik
Namespaces
|
212
213
214
215
216
217
218
|
}
}
}
foreach ($this->getSeoLinks() as $link) {
$url = Yii::$app->urlManager->baseUrl . $link->url;
|
ee2ceed3
Alexey Boroda
-Sitemap beta
|
219
|
$this->createRow($url, 0.7);
|
d77e7532
Alexey Boroda
-sitemap try
|
220
|
|
8a7e6ecf
Yarik
Namespaces
|
221
222
|
}
|
ee2ceed3
Alexey Boroda
-Sitemap beta
|
223
|
$this->content .= '</urlset>';
|
8a7e6ecf
Yarik
Namespaces
|
224
|
|
ee2ceed3
Alexey Boroda
-Sitemap beta
|
225
|
fwrite($this->handle, $this->content);
|
d77e7532
Alexey Boroda
-sitemap try
|
226
227
228
|
fclose($this->handle);
$this->mainMap .= '<sitemap>'.
|
29f1675f
Administrator
site map
|
229
|
'<loc>'.$this->getHost().'/ru/' . $this->fileName . '</loc>'.
|
d77e7532
Alexey Boroda
-sitemap try
|
230
231
232
233
234
235
236
|
'<lastmod>' . date('Y-m-d') . '</lastmod>'.
'</sitemap>'.
'</sitemapindex>';
$mainHandle = fopen(Yii::getAlias('@frontend') . '/web/sitemap.xml', "w");
fwrite($mainHandle, $this->mainMap);
fclose($mainHandle);
|
8a7e6ecf
Yarik
Namespaces
|
237
|
|
ee2ceed3
Alexey Boroda
-Sitemap beta
|
238
|
$this->stdout(Yii::getAlias('@frontend') . '/web' . '/' . $this->fileName . "\n", Console::FG_GREEN);
|
8a7e6ecf
Yarik
Namespaces
|
239
240
241
|
}
}
|