Blame view

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