Blame view

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