Blame view

console/SiteMapController.php 12.5 KB
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;
d5c87d48   Administrator   site map
7
      use artweb\artbox\language\components\LanguageUrlManager;
29f1675f   Administrator   site map
8
      use artweb\artbox\language\models\Language;
8a7e6ecf   Yarik   Namespaces
9
10
      use artweb\artbox\seo\models\Seo;
      use artweb\artbox\ecommerce\models\Category;
8a7e6ecf   Yarik   Namespaces
11
12
13
      use Yii;
      use artweb\artbox\models\Page;
      use yii\helpers\ArrayHelper;
d77e7532   Alexey Boroda   -sitemap try
14
      use yii\helpers\Console;
8a7e6ecf   Yarik   Namespaces
15
16
17
      use yii\helpers\Url;
      use yii\console\Controller;
      
8a7e6ecf   Yarik   Namespaces
18
19
20
      class SiteMapController extends Controller
      {
          
ee2ceed3   Alexey Boroda   -Sitemap beta
21
          private $urlList = [ 'https://extremstyle.ua/ru' ];
8a7e6ecf   Yarik   Namespaces
22
          private $count = 1;
d77e7532   Alexey Boroda   -sitemap try
23
24
25
26
          public $fileName;
          public $handle;
          public $mapNumber = 1;
          public $mainMap = '';
ee2ceed3   Alexey Boroda   -Sitemap beta
27
          public $content = '';
240fe900   Yarik   Sitemap
28
          public $robots = [];
8a7e6ecf   Yarik   Namespaces
29
30
31
32
          
          public function getAddStatic()
          {
              return [
240fe900   Yarik   Sitemap
33
                  Yii::$app->urlManager->baseUrl . '/ru',
8a7e6ecf   Yarik   Namespaces
34
35
              ];
          }
240fe900   Yarik   Sitemap
36
37
38
39
          
          public function getHost()
          {
              return Yii::$app->urlManager->baseUrl . '/ru';
29f1675f   Administrator   site map
40
          }
8a7e6ecf   Yarik   Namespaces
41
          
9580e548   Alexey Boroda   -Site map in prog...
42
          public function getVariants()
8a7e6ecf   Yarik   Namespaces
43
          {
9580e548   Alexey Boroda   -Site map in prog...
44
45
              return ProductVariant::find()
                                   ->with('lang')
240fe900   Yarik   Sitemap
46
47
48
49
50
51
52
53
54
                                   ->with('product.lang')
                                   ->select('*', 'DISTINCT ON (product_id)')
                                   ->andWhere(
                                       [
                                           '!=',
                                           'stock',
                                           0,
                                       ]
                                   );
8a7e6ecf   Yarik   Namespaces
55
56
57
58
59
60
              
          }
          
          public function getSeoLinks()
          {
              return Seo::find()
240fe900   Yarik   Sitemap
61
62
63
64
65
66
67
                        ->where(
                            [
                                '!=',
                                'meta',
                                'noindex,nofollow',
                            ]
                        )
8a7e6ecf   Yarik   Namespaces
68
69
70
71
72
73
74
                        ->all();
              
          }
          
          public function getStaticPages()
          {
              return Page::find()
9580e548   Alexey Boroda   -Site map in prog...
75
                         ->with('lang')
8a7e6ecf   Yarik   Namespaces
76
77
78
79
80
81
                         ->all();
          }
          
          public function getCategories()
          {
              return Category::find()
240fe900   Yarik   Sitemap
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
                             ->joinWith('lang')
                             ->where(
                                 [
                                     '!=',
                                     'parent_id',
                                     0,
                                 ]
                             )
                             ->andWhere(
                                 [
                                     'not',
                                     [
                                         'meta_robots' => [
                                             'noindex,nofollow',
                                             'noindex, nofollow',
                                             'noindex,follow',
                                             'noindex, follow',
                                         ],
                                     ],
                                 ]
                             )
8a7e6ecf   Yarik   Namespaces
103
104
105
                             ->all();
          }
          
9580e548   Alexey Boroda   -Site map in prog...
106
          public function getCategoriesWithFilters()
8a7e6ecf   Yarik   Namespaces
107
          {
9580e548   Alexey Boroda   -Site map in prog...
108
109
110
111
              return Category::find()
                             ->with('lang')
                             ->joinWith('taxGroups.lang')
                             ->with('taxGroups.taxOptions.lang')
240fe900   Yarik   Sitemap
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
                             ->where(
                                 [
                                     '!=',
                                     'parent_id',
                                     0,
                                 ]
                             )
                             ->andWhere(
                                 [
                                     'not',
                                     [
                                         'tax_group.meta_robots' => [
                                             'noindex,nofollow',
                                             'noindex, nofollow',
                                             'noindex,follow',
                                             'noindex, follow',
                                         ],
                                     ],
                                 ]
                             )
                             ->andWhere([ 'tax_group.is_filter' => true ])
9580e548   Alexey Boroda   -Site map in prog...
133
134
135
136
137
138
139
                             ->all();
          }
          
          public function getBrands()
          {
              return Brand::find()
                          ->joinWith('lang')
240fe900   Yarik   Sitemap
140
141
142
143
144
145
146
147
148
149
150
151
152
                          ->andWhere(
                              [
                                  'not',
                                  [
                                      'meta_robots' => [
                                          'noindex,nofollow',
                                          'noindex, nofollow',
                                          'noindex,follow',
                                          'noindex, follow',
                                      ],
                                  ],
                              ]
                          )
9580e548   Alexey Boroda   -Site map in prog...
153
                          ->all();
8a7e6ecf   Yarik   Namespaces
154
155
          }
          
8a7e6ecf   Yarik   Namespaces
156
157
158
159
160
161
162
163
164
165
          public function checkUrl($url)
          {
              if (!in_array($url, $this->urlList)) {
                  $this->urlList[] = $url;
                  return true;
              } else {
                  return false;
              }
          }
          
240fe900   Yarik   Sitemap
166
          public function createRow($url, $priority, $freq = 'Weekly')
8a7e6ecf   Yarik   Namespaces
167
          {
240fe900   Yarik   Sitemap
168
169
170
171
172
              if(in_array($url, $this->robots)) {
                  return;
              }
              //            if ($this->checkUrl($url)) {
              if ($this->count % 500 == 0) {
d77e7532   Alexey Boroda   -sitemap try
173
174
                  $this->stdout($this->count . " : ", Console::BOLD);
                  $this->stdout($url . "\n", Console::FG_YELLOW);
ee2ceed3   Alexey Boroda   -Sitemap beta
175
              }
240fe900   Yarik   Sitemap
176
177
178
179
180
181
182
183
184
185
186
187
              $this->content .= '<url>' . '<loc>' . $url . '</loc>' . '<lastmod>' . date(
                      'Y-m-d'
                  ) . '</lastmod>' . '<changefreq>' . $freq . '</changefreq>' . '<priority>' . $priority . '</priority>' . '</url>';
              $this->count++;
              if ($this->count % 10000 == 0) {
                  $this->content .= '</urlset>';
                  $this->stdout('Added unset' . "\n", Console::FG_CYAN);
                  fwrite($this->handle, $this->content);
                  fclose($this->handle);
                  $this->mapNumber++;
                  
                  $this->mainMap .= '<sitemap>' . '<loc>https://extremstyle.ua/' . $this->fileName . '</loc>' . '<lastmod>' . date(
8a7e6ecf   Yarik   Namespaces
188
                          'Y-m-d'
240fe900   Yarik   Sitemap
189
190
191
192
193
194
195
196
                      ) . '</lastmod>' . '</sitemap>';
                  
                  $this->fileName = 'sitemap' . $this->mapNumber . '.xml';
                  $this->handle = fopen(Yii::getAlias('@frontend') . '/web' . '/' . $this->fileName, "w");
                  
                  $this->content = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
              }
              //            }
8a7e6ecf   Yarik   Namespaces
197
198
199
200
          }
          
          public function actionProcess()
          {
240fe900   Yarik   Sitemap
201
              
29f1675f   Administrator   site map
202
              Language::setCurrent('ru');
240fe900   Yarik   Sitemap
203
              
8a7e6ecf   Yarik   Namespaces
204
205
              $config = ArrayHelper::merge(
                  require( \Yii::getAlias('@frontend/config/') . 'main.php' ),
d5c87d48   Administrator   site map
206
                  require( \Yii::getAlias('@common/config/') . 'main.php' ),
240fe900   Yarik   Sitemap
207
                  [ 'components' => [ 'urlManager' => [ 'hostInfo' => 'https://extremstyle.ua' ] ] ]
8a7e6ecf   Yarik   Namespaces
208
              );
240fe900   Yarik   Sitemap
209
210
211
              
              if (isset($config[ 'components' ][ 'urlManager' ][ 'class' ])) {
                  unset($config[ 'components' ][ 'urlManager' ][ 'class' ]);
d5c87d48   Administrator   site map
212
213
              }
              //Yii::$app->urlManager = new LanguageUrlManager($config['components']['urlManager']);
240fe900   Yarik   Sitemap
214
215
216
              
              $urlManager = new LanguageUrlManager($config[ 'components' ][ 'urlManager' ]);
              
d77e7532   Alexey Boroda   -sitemap try
217
218
              $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
219
              $this->fileName = 'sitemap' . $this->mapNumber . '.xml';
8a7e6ecf   Yarik   Namespaces
220
              setlocale(LC_ALL, 'ru_RU.CP1251');
d77e7532   Alexey Boroda   -sitemap try
221
              $this->handle = fopen(Yii::getAlias('@frontend') . '/web' . '/' . $this->fileName, "w");
240fe900   Yarik   Sitemap
222
              
ee2ceed3   Alexey Boroda   -Sitemap beta
223
              $this->content = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
8a7e6ecf   Yarik   Namespaces
224
              
240fe900   Yarik   Sitemap
225
226
227
228
229
230
231
              $robots = file_get_contents(\Yii::getAlias('@frontend/web/robots.txt'));
              if (preg_match_all('/Disallow: (.*)\n/', $robots, $matches)) {
                  foreach ($matches[ 1 ] as $match) {
                      $this->robots[] = $this->getHost() . $match;
                  }
              }
              
8a7e6ecf   Yarik   Namespaces
232
              foreach ($this->getAddStatic() as $page) {
240fe900   Yarik   Sitemap
233
                  $this->createRow($page, 1, 'Daily');
8a7e6ecf   Yarik   Namespaces
234
235
236
              }
              
              foreach ($this->getStaticPages() as $page) {
d5c87d48   Administrator   site map
237
                  $url = $urlManager->createAbsoluteUrl(
8a7e6ecf   Yarik   Namespaces
238
                      [
9580e548   Alexey Boroda   -Site map in prog...
239
240
                          'site/page',
                          'slug' => $page->lang->alias,
8a7e6ecf   Yarik   Namespaces
241
242
                      ]
                  );
240fe900   Yarik   Sitemap
243
                  $this->createRow($url, 0.5);
8a7e6ecf   Yarik   Namespaces
244
245
246
              }
              
              foreach ($this->getCategories() as $category) {
240fe900   Yarik   Sitemap
247
                  $url = $urlManager->createAbsoluteUrl(
8a7e6ecf   Yarik   Namespaces
248
249
                      [
                          'catalog/category',
9580e548   Alexey Boroda   -Site map in prog...
250
                          'category' => $category->lang->alias,
8a7e6ecf   Yarik   Namespaces
251
252
                      ]
                  );
240fe900   Yarik   Sitemap
253
                  $this->createRow($url, 0.9, 'Daily');
9580e548   Alexey Boroda   -Site map in prog...
254
255
              }
              
240fe900   Yarik   Sitemap
256
257
              foreach ($this->getVariants()
                            ->batch(1000) as $rows) {
9580e548   Alexey Boroda   -Site map in prog...
258
                  foreach ($rows as $row) {
240fe900   Yarik   Sitemap
259
                      if (!preg_match("@^[a-zA-Z\d]+$@i", $row->sku)) {
ee2ceed3   Alexey Boroda   -Sitemap beta
260
261
                          continue;
                      }
240fe900   Yarik   Sitemap
262
                      $url = $urlManager->createAbsoluteUrl(
9580e548   Alexey Boroda   -Site map in prog...
263
264
265
266
267
268
                          [
                              'catalog/product',
                              'product' => $row->product->lang->alias,
                              'variant' => $row->sku,
                          ]
                      );
240fe900   Yarik   Sitemap
269
                      $this->createRow($url, 0.7, 'Daily');
9580e548   Alexey Boroda   -Site map in prog...
270
                  }
8a7e6ecf   Yarik   Namespaces
271
272
              }
              
9580e548   Alexey Boroda   -Site map in prog...
273
              foreach ($this->getBrands() as $brand) {
d77e7532   Alexey Boroda   -sitemap try
274
                  
d5c87d48   Administrator   site map
275
                  $url = $urlManager->createAbsoluteUrl(
8a7e6ecf   Yarik   Namespaces
276
                      [
9580e548   Alexey Boroda   -Site map in prog...
277
278
                          'brand/view',
                          'slug' => $brand->lang->alias,
8a7e6ecf   Yarik   Namespaces
279
280
                      ]
                  );
240fe900   Yarik   Sitemap
281
                  $this->createRow($url, 0.5);
d77e7532   Alexey Boroda   -sitemap try
282
                  
8a7e6ecf   Yarik   Namespaces
283
              }
240fe900   Yarik   Sitemap
284
              
9580e548   Alexey Boroda   -Site map in prog...
285
286
              foreach ($this->getCategoriesWithFilters() as $category) {
                  foreach ($category->taxGroups as $group) {
240fe900   Yarik   Sitemap
287
288
289
290
291
292
293
294
295
                      if (in_array(
                          $group->meta_robots,
                          [
                              'noindex,nofollow',
                              'noindex, nofollow',
                              'noindex,follow',
                              'noindex, follow',
                          ]
                      )) {
5cf1ef7b   Administrator   site map
296
297
                          continue;
                      }
240fe900   Yarik   Sitemap
298
                      if ($group->is_filter) {
154bea47   Administrator   site map
299
                          foreach ($group->options as $option) {
240fe900   Yarik   Sitemap
300
                              $url = $urlManager->createAbsoluteUrl(
fdb1317d   Administrator   site map
301
302
303
304
305
306
                                  [
                                      'catalog/category',
                                      'category' => $category,
                                      'filters'  => [ $group->lang->alias => [ $option->lang->alias ] ],
                                  ]
                              );
240fe900   Yarik   Sitemap
307
                              $this->createRow($url, 0.8, 'Daily');
fdb1317d   Administrator   site map
308
                          }
8a7e6ecf   Yarik   Namespaces
309
                      }
8a7e6ecf   Yarik   Namespaces
310
311
312
313
314
315
                      
                  }
              }
              
              foreach ($this->getSeoLinks() as $link) {
                  $url = Yii::$app->urlManager->baseUrl . $link->url;
ee2ceed3   Alexey Boroda   -Sitemap beta
316
                  $this->createRow($url, 0.7);
d77e7532   Alexey Boroda   -sitemap try
317
                  
8a7e6ecf   Yarik   Namespaces
318
319
              }
              
ee2ceed3   Alexey Boroda   -Sitemap beta
320
              $this->content .= '</urlset>';
8a7e6ecf   Yarik   Namespaces
321
              
ee2ceed3   Alexey Boroda   -Sitemap beta
322
              fwrite($this->handle, $this->content);
d77e7532   Alexey Boroda   -sitemap try
323
              fclose($this->handle);
240fe900   Yarik   Sitemap
324
325
326
327
              
              $this->mainMap .= '<sitemap>' . '<loc>' . 'https://extremstyle.ua/' . $this->fileName . '</loc>' . '<lastmod>' . date(
                      'Y-m-d'
                  ) . '</lastmod>' . '</sitemap>' . '</sitemapindex>';
d77e7532   Alexey Boroda   -sitemap try
328
329
330
331
              
              $mainHandle = fopen(Yii::getAlias('@frontend') . '/web/sitemap.xml', "w");
              fwrite($mainHandle, $this->mainMap);
              fclose($mainHandle);
8a7e6ecf   Yarik   Namespaces
332
              
ee2ceed3   Alexey Boroda   -Sitemap beta
333
              $this->stdout(Yii::getAlias('@frontend') . '/web' . '/' . $this->fileName . "\n", Console::FG_GREEN);
8a7e6ecf   Yarik   Namespaces
334
335
336
          }
          
      }