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