Blame view

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