Blame view

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