Blame view

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