Blame view

console/SiteMapController.php 14.1 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
          }
          
07d45dad   Alexey Boroda   -Sitemap dances
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
          public function getProducts()
          {
              $products = Product::find()
                                 ->innerJoinWith('variant')
                                 ->innerJoinWith('brand.lang')->with('lang')
                                 ->where([
                                                 '!=',
                                                 'product_variant.price',
                                                 0
                                             ])
                  ->andWhere([
                                 '!=',
                                 'product_variant.stock',
                                 0
                             ])
                                 ->all();
              
              return $products;
          }
          
8a7e6ecf   Yarik   Namespaces
70
71
72
          public function getSeoLinks()
          {
              return Seo::find()
240fe900   Yarik   Sitemap
73
74
75
76
77
78
79
                        ->where(
                            [
                                '!=',
                                'meta',
                                'noindex,nofollow',
                            ]
                        )
8a7e6ecf   Yarik   Namespaces
80
81
82
83
84
85
86
                        ->all();
              
          }
          
          public function getStaticPages()
          {
              return Page::find()
9580e548   Alexey Boroda   -Site map in prog...
87
                         ->with('lang')
8a7e6ecf   Yarik   Namespaces
88
89
90
91
92
93
                         ->all();
          }
          
          public function getCategories()
          {
              return Category::find()
240fe900   Yarik   Sitemap
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
                             ->joinWith('lang')
                             ->where(
                                 [
                                     '!=',
                                     'parent_id',
                                     0,
                                 ]
                             )
                             ->andWhere(
                                 [
                                     'not',
                                     [
                                         'meta_robots' => [
                                             'noindex,nofollow',
                                             'noindex, nofollow',
                                             'noindex,follow',
                                             'noindex, follow',
                                         ],
                                     ],
                                 ]
                             )
8a7e6ecf   Yarik   Namespaces
115
116
117
                             ->all();
          }
          
9580e548   Alexey Boroda   -Site map in prog...
118
          public function getCategoriesWithFilters()
8a7e6ecf   Yarik   Namespaces
119
          {
9580e548   Alexey Boroda   -Site map in prog...
120
121
122
123
              return Category::find()
                             ->with('lang')
                             ->joinWith('taxGroups.lang')
                             ->with('taxGroups.taxOptions.lang')
240fe900   Yarik   Sitemap
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
                             ->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...
145
146
147
148
149
150
151
                             ->all();
          }
          
          public function getBrands()
          {
              return Brand::find()
                          ->joinWith('lang')
240fe900   Yarik   Sitemap
152
153
154
155
156
157
158
159
160
161
162
163
164
                          ->andWhere(
                              [
                                  'not',
                                  [
                                      'meta_robots' => [
                                          'noindex,nofollow',
                                          'noindex, nofollow',
                                          'noindex,follow',
                                          'noindex, follow',
                                      ],
                                  ],
                              ]
                          )
9580e548   Alexey Boroda   -Site map in prog...
165
                          ->all();
8a7e6ecf   Yarik   Namespaces
166
167
          }
          
8a7e6ecf   Yarik   Namespaces
168
169
170
171
172
173
174
175
176
177
          public function checkUrl($url)
          {
              if (!in_array($url, $this->urlList)) {
                  $this->urlList[] = $url;
                  return true;
              } else {
                  return false;
              }
          }
          
240fe900   Yarik   Sitemap
178
          public function createRow($url, $priority, $freq = 'Weekly')
8a7e6ecf   Yarik   Namespaces
179
          {
240fe900   Yarik   Sitemap
180
181
182
183
184
              if(in_array($url, $this->robots)) {
                  return;
              }
              //            if ($this->checkUrl($url)) {
              if ($this->count % 500 == 0) {
d77e7532   Alexey Boroda   -sitemap try
185
186
                  $this->stdout($this->count . " : ", Console::BOLD);
                  $this->stdout($url . "\n", Console::FG_YELLOW);
ee2ceed3   Alexey Boroda   -Sitemap beta
187
              }
240fe900   Yarik   Sitemap
188
189
190
191
192
193
194
195
196
197
198
199
              $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
200
                          'Y-m-d'
240fe900   Yarik   Sitemap
201
202
203
204
205
206
207
208
                      ) . '</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
209
210
211
212
          }
          
          public function actionProcess()
          {
240fe900   Yarik   Sitemap
213
              
29f1675f   Administrator   site map
214
              Language::setCurrent('ru');
240fe900   Yarik   Sitemap
215
              
8a7e6ecf   Yarik   Namespaces
216
217
              $config = ArrayHelper::merge(
                  require( \Yii::getAlias('@frontend/config/') . 'main.php' ),
d5c87d48   Administrator   site map
218
                  require( \Yii::getAlias('@common/config/') . 'main.php' ),
240fe900   Yarik   Sitemap
219
                  [ 'components' => [ 'urlManager' => [ 'hostInfo' => 'https://extremstyle.ua' ] ] ]
8a7e6ecf   Yarik   Namespaces
220
              );
240fe900   Yarik   Sitemap
221
222
223
              
              if (isset($config[ 'components' ][ 'urlManager' ][ 'class' ])) {
                  unset($config[ 'components' ][ 'urlManager' ][ 'class' ]);
d5c87d48   Administrator   site map
224
225
              }
              //Yii::$app->urlManager = new LanguageUrlManager($config['components']['urlManager']);
240fe900   Yarik   Sitemap
226
227
228
              
              $urlManager = new LanguageUrlManager($config[ 'components' ][ 'urlManager' ]);
              
d77e7532   Alexey Boroda   -sitemap try
229
230
              $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
231
              $this->fileName = 'sitemap' . $this->mapNumber . '.xml';
8a7e6ecf   Yarik   Namespaces
232
              setlocale(LC_ALL, 'ru_RU.CP1251');
d77e7532   Alexey Boroda   -sitemap try
233
              $this->handle = fopen(Yii::getAlias('@frontend') . '/web' . '/' . $this->fileName, "w");
240fe900   Yarik   Sitemap
234
              
ee2ceed3   Alexey Boroda   -Sitemap beta
235
              $this->content = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
8a7e6ecf   Yarik   Namespaces
236
              
240fe900   Yarik   Sitemap
237
238
239
240
241
242
243
              $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;
                  }
              }
              
07d45dad   Alexey Boroda   -Sitemap dances
244
              $this->stdout('Add static' . PHP_EOL, Console::FG_BLUE);
8a7e6ecf   Yarik   Namespaces
245
              foreach ($this->getAddStatic() as $page) {
240fe900   Yarik   Sitemap
246
                  $this->createRow($page, 1, 'Daily');
8a7e6ecf   Yarik   Namespaces
247
              }
07d45dad   Alexey Boroda   -Sitemap dances
248
249
250
      
              $this->stdout('Add pages' . PHP_EOL, Console::FG_BLUE);
      
8a7e6ecf   Yarik   Namespaces
251
              foreach ($this->getStaticPages() as $page) {
d5c87d48   Administrator   site map
252
                  $url = $urlManager->createAbsoluteUrl(
8a7e6ecf   Yarik   Namespaces
253
                      [
9580e548   Alexey Boroda   -Site map in prog...
254
255
                          'site/page',
                          'slug' => $page->lang->alias,
8a7e6ecf   Yarik   Namespaces
256
257
                      ]
                  );
240fe900   Yarik   Sitemap
258
                  $this->createRow($url, 0.5);
8a7e6ecf   Yarik   Namespaces
259
              }
07d45dad   Alexey Boroda   -Sitemap dances
260
261
262
      
              $this->stdout('Add categories' . PHP_EOL, Console::FG_BLUE);
      
8a7e6ecf   Yarik   Namespaces
263
              foreach ($this->getCategories() as $category) {
240fe900   Yarik   Sitemap
264
                  $url = $urlManager->createAbsoluteUrl(
8a7e6ecf   Yarik   Namespaces
265
266
                      [
                          'catalog/category',
9580e548   Alexey Boroda   -Site map in prog...
267
                          'category' => $category->lang->alias,
8a7e6ecf   Yarik   Namespaces
268
269
                      ]
                  );
240fe900   Yarik   Sitemap
270
                  $this->createRow($url, 0.9, 'Daily');
9580e548   Alexey Boroda   -Site map in prog...
271
              }
07d45dad   Alexey Boroda   -Sitemap dances
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
              
  //            foreach ($this->getVariants()
  //                          ->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)) {
  //                        continue;
  //                    }
  //                    $url = $urlManager->createAbsoluteUrl(
  //                        [
  //                            'catalog/product',
  //                            'product' => $row->lang->alias,
  //                        ]
  //                    );
  //                    $this->createRow($url, 0.7, 'Daily');
  //                }
  //            }
              /**
               * New products generation
               */
      
              $this->stdout('Add products' . PHP_EOL, Console::FG_BLUE);
      
              foreach ($this->getProducts() as $product) {
15b6552f   Yarik   Sitemap
301
                  /**
07d45dad   Alexey Boroda   -Sitemap dances
302
                   * @var Product $product
15b6552f   Yarik   Sitemap
303
                   */
07d45dad   Alexey Boroda   -Sitemap dances
304
305
306
307
308
309
310
                  $url = $urlManager->createAbsoluteUrl(
                      [
                                  'catalog/product',
                                  'product' => $product->lang->alias,
                              ]
                  );
                  $this->createRow($url, 0.7, 'Daily');
8a7e6ecf   Yarik   Namespaces
311
              }
07d45dad   Alexey Boroda   -Sitemap dances
312
313
314
      
              $this->stdout('Add brands' . PHP_EOL, Console::FG_BLUE);
      
9580e548   Alexey Boroda   -Site map in prog...
315
              foreach ($this->getBrands() as $brand) {
d77e7532   Alexey Boroda   -sitemap try
316
                  
d5c87d48   Administrator   site map
317
                  $url = $urlManager->createAbsoluteUrl(
8a7e6ecf   Yarik   Namespaces
318
                      [
9580e548   Alexey Boroda   -Site map in prog...
319
320
                          'brand/view',
                          'slug' => $brand->lang->alias,
8a7e6ecf   Yarik   Namespaces
321
322
                      ]
                  );
240fe900   Yarik   Sitemap
323
                  $this->createRow($url, 0.5);
d77e7532   Alexey Boroda   -sitemap try
324
                  
8a7e6ecf   Yarik   Namespaces
325
              }
07d45dad   Alexey Boroda   -Sitemap dances
326
327
328
      
              $this->stdout('Add filters' . PHP_EOL, Console::FG_BLUE);
      
9580e548   Alexey Boroda   -Site map in prog...
329
330
              foreach ($this->getCategoriesWithFilters() as $category) {
                  foreach ($category->taxGroups as $group) {
240fe900   Yarik   Sitemap
331
332
333
334
335
336
337
338
339
                      if (in_array(
                          $group->meta_robots,
                          [
                              'noindex,nofollow',
                              'noindex, nofollow',
                              'noindex,follow',
                              'noindex, follow',
                          ]
                      )) {
5cf1ef7b   Administrator   site map
340
341
                          continue;
                      }
240fe900   Yarik   Sitemap
342
                      if ($group->is_filter) {
154bea47   Administrator   site map
343
                          foreach ($group->options as $option) {
240fe900   Yarik   Sitemap
344
                              $url = $urlManager->createAbsoluteUrl(
fdb1317d   Administrator   site map
345
346
347
348
349
350
                                  [
                                      'catalog/category',
                                      'category' => $category,
                                      'filters'  => [ $group->lang->alias => [ $option->lang->alias ] ],
                                  ]
                              );
240fe900   Yarik   Sitemap
351
                              $this->createRow($url, 0.8, 'Daily');
fdb1317d   Administrator   site map
352
                          }
8a7e6ecf   Yarik   Namespaces
353
                      }
8a7e6ecf   Yarik   Namespaces
354
355
356
                      
                  }
              }
07d45dad   Alexey Boroda   -Sitemap dances
357
358
359
      
              $this->stdout('Add seo links' . PHP_EOL, Console::FG_BLUE);
      
8a7e6ecf   Yarik   Namespaces
360
361
              foreach ($this->getSeoLinks() as $link) {
                  $url = Yii::$app->urlManager->baseUrl . $link->url;
ee2ceed3   Alexey Boroda   -Sitemap beta
362
                  $this->createRow($url, 0.7);
d77e7532   Alexey Boroda   -sitemap try
363
                  
8a7e6ecf   Yarik   Namespaces
364
365
              }
              
ee2ceed3   Alexey Boroda   -Sitemap beta
366
              $this->content .= '</urlset>';
8a7e6ecf   Yarik   Namespaces
367
              
ee2ceed3   Alexey Boroda   -Sitemap beta
368
              fwrite($this->handle, $this->content);
d77e7532   Alexey Boroda   -sitemap try
369
              fclose($this->handle);
240fe900   Yarik   Sitemap
370
371
372
373
              
              $this->mainMap .= '<sitemap>' . '<loc>' . 'https://extremstyle.ua/' . $this->fileName . '</loc>' . '<lastmod>' . date(
                      'Y-m-d'
                  ) . '</lastmod>' . '</sitemap>' . '</sitemapindex>';
d77e7532   Alexey Boroda   -sitemap try
374
375
376
377
              
              $mainHandle = fopen(Yii::getAlias('@frontend') . '/web/sitemap.xml', "w");
              fwrite($mainHandle, $this->mainMap);
              fclose($mainHandle);
8a7e6ecf   Yarik   Namespaces
378
              
ee2ceed3   Alexey Boroda   -Sitemap beta
379
              $this->stdout(Yii::getAlias('@frontend') . '/web' . '/' . $this->fileName . "\n", Console::FG_GREEN);
8a7e6ecf   Yarik   Namespaces
380
381
382
          }
          
      }