Commit d77e7532a89b98f9e6ec71d8343a39d3df53268b

Authored by Alexey Boroda
1 parent 60ebb441

-sitemap try

Showing 2 changed files with 49 additions and 103 deletions   Show diff stats
console/SiteMapController.php
... ... @@ -4,36 +4,24 @@
4 4  
5 5 use artweb\artbox\ecommerce\models\Brand;
6 6 use artweb\artbox\ecommerce\models\ProductVariant;
7   - use artweb\artbox\language\models\Language;
8 7 use artweb\artbox\seo\models\Seo;
9 8 use artweb\artbox\ecommerce\models\Category;
10   - use artweb\artbox\ecommerce\models\Product;
11   - use artweb\artbox\ecommerce\models\ProductFrontendSearch;
12 9 use Yii;
13 10 use artweb\artbox\models\Page;
14 11 use yii\helpers\ArrayHelper;
  12 + use yii\helpers\Console;
15 13 use yii\helpers\Url;
16 14 use yii\console\Controller;
17 15  
18   - /**
19   - * PageController implements the CRUD actions for Page model.
20   - */
21 16 class SiteMapController extends Controller
22 17 {
23 18  
24 19 private $urlList = [ 'https://dev.extremstyle.ua/ru' ];
25 20 private $count = 1;
26   -
27   - public function checkFilter($category, $filter)
28   - {
29   - $productModel = new ProductFrontendSearch();
30   - $productProvider = $productModel->search($category, $filter);
31   - if (!empty( $productProvider->models )) {
32   - return true;
33   - } else {
34   - return false;
35   - }
36   - }
  21 + public $fileName;
  22 + public $handle;
  23 + public $mapNumber = 1;
  24 + public $mainMap = '';
37 25  
38 26 public function getAddStatic()
39 27 {
... ... @@ -47,7 +35,7 @@
47 35 return ProductVariant::find()
48 36 ->with('lang')
49 37 ->with('product.lang')
50   - ->batch(1000);
  38 + ->batch(10000);
51 39  
52 40 }
53 41  
... ... @@ -79,7 +67,7 @@
79 67 ->with('lang')
80 68 ->joinWith('taxGroups.lang')
81 69 ->with('taxGroups.taxOptions.lang')
82   - ->where(['tax_group.meta_robots' => '',])
  70 + ->where([ 'tax_group.meta_robots' => '', ])
83 71 ->all();
84 72 }
85 73  
... ... @@ -90,25 +78,6 @@
90 78 ->all();
91 79 }
92 80  
93   - /**
94   - * @param $category Category;
95   - *
96   - * @return mixed
97   - */
98   -
99   - public function getFilters($category)
100   - {
101   - return $category->getActiveFilters()
102   - ->andWhere(
103   - [
104   -
105   - 'tax_group.meta_robots' => '',
106   - ]
107   - )
108   - ->all();
109   -
110   - }
111   -
112 81 public function checkUrl($url)
113 82 {
114 83 if (!in_array($url, $this->urlList)) {
... ... @@ -122,10 +91,30 @@
122 91 public function createRow($url, $priority, &$content)
123 92 {
124 93 if ($this->checkUrl($url)) {
125   - print $this->count++ . $url . "\n";
  94 + $this->stdout($this->count . " : ", Console::BOLD);
  95 + $this->stdout($url . "\n", Console::FG_YELLOW);
126 96 $content .= '<url>' . '<loc>' . $url . '</loc>' . '<lastmod>' . date(
127 97 'Y-m-d'
128 98 ) . '</lastmod>' . '<changefreq>Weekly</changefreq>' . '<priority>' . $priority . '</priority>' . '</url>';
  99 + $this->count++;
  100 + if ($this->count % 40000 == 0) {
  101 + $content .= '</urlset>';
  102 +
  103 + fwrite($this->handle, $content);
  104 + fclose($this->handle);
  105 + unset($content);
  106 + $this->mapNumber++;
  107 +
  108 + $this->mainMap .= '<sitemap>'.
  109 + '<loc>https://dev.extremstyle.ua/ru/' . $this->fileName . '</loc>'.
  110 + '<lastmod>' . date('Y-m-d') . '</lastmod>'.
  111 + '</sitemap>';
  112 +
  113 + $this->fileName = 'sitemap' . $this->mapNumber . '.xml';
  114 + $this->handle = fopen(Yii::getAlias('@frontend') . '/web' . '/' . $this->fileName, "w");
  115 +
  116 + $content = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
  117 + }
129 118 }
130 119 }
131 120  
... ... @@ -137,12 +126,11 @@
137 126 );
138 127  
139 128 Yii::$app->urlManager->addRules($config[ 'components' ][ 'urlManager' ][ 'rules' ]);
140   -
141   - $dirName = Yii::getAlias('@frontend') . '/web';
142   -
143   - $filename = 'test_sitemap.xml';
  129 + $this->mainMap = '<?xml version="1.0" encoding="UTF-8"?>';
  130 + $this->mainMap .= '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
  131 + $this->fileName = 'test_sitemap' . $this->mapNumber . '.xml';
144 132 setlocale(LC_ALL, 'ru_RU.CP1251');
145   - $handle = fopen($dirName . '/' . $filename, "w");
  133 + $this->handle = fopen(Yii::getAlias('@frontend') . '/web' . '/' . $this->fileName, "w");
146 134  
147 135 $content = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
148 136  
... ... @@ -184,7 +172,7 @@
184 172 }
185 173  
186 174 foreach ($this->getBrands() as $brand) {
187   -
  175 +
188 176 $url = Url::to(
189 177 [
190 178 'brand/view',
... ... @@ -192,7 +180,7 @@
192 180 ]
193 181 );
194 182 $this->createRow($url, 0.7, $content);
195   -
  183 +
196 184 }
197 185  
198 186 foreach ($this->getCategoriesWithFilters() as $category) {
... ... @@ -214,67 +202,25 @@
214 202 foreach ($this->getSeoLinks() as $link) {
215 203 $url = Yii::$app->urlManager->baseUrl . $link->url;
216 204 $this->createRow($url, 0.7, $content);
217   -
  205 +
218 206 }
219 207  
220   - // foreach ($this->getCategories() as $category) {
221   - // foreach ($this->getFilters($category) as $filter1) {
222   - // foreach ($this->getFilters($category) as $filter2) {
223   - // if ($this->checkFilter(
224   - // $category,
225   - // [
226   - // $filter1[ 'group_alias' ] => [ $filter1[ 'option_alias' ] ],
227   - // $filter2[ 'group_alias' ] => [ $filter2[ 'option_alias' ] ],
228   - // ]
229   - // )
230   - // ) {
231   - // $url = Url::to(
232   - // [
233   - // 'catalog/category',
234   - // 'category' => $category,
235   - // 'filters' => [
236   - // $filter1[ 'group_alias' ] => [ $filter1[ 'option_alias' ] ],
237   - // $filter2[ 'group_alias' ] => [ $filter2[ 'option_alias' ] ],
238   - // ],
239   - // ]
240   - // );
241   - // $this->createRow($url, 0.7, $content);
242   - // }
243   - //
244   - // }
245   - //
246   - // foreach ($this->getBrands($category) as $brand) {
247   - // if ($this->checkFilter(
248   - // $category,
249   - // [
250   - // 'brands' => [ $brand->id ],
251   - // $filter1[ 'group_alias' ] => [ $filter1[ 'option_alias' ] ],
252   - // ]
253   - // )
254   - // ) {
255   - // $url = Url::to(
256   - // [
257   - // 'catalog/category',
258   - // 'category' => $category,
259   - // 'filters' => [
260   - // 'brands' => [ $brand->alias ],
261   - // $filter1[ 'group_alias' ] => [ $filter1[ 'option_alias' ] ],
262   - // ],
263   - // ]
264   - // );
265   - // $this->createRow($url, 0.7, $content);
266   - // }
267   - //
268   - // }
269   - // }
270   - // }
271   -
272 208 $content .= '</urlset>';
273 209  
274   - fwrite($handle, $content);
275   - fclose($handle);
  210 + fwrite($this->handle, $content);
  211 + fclose($this->handle);
  212 +
  213 + $this->mainMap .= '<sitemap>'.
  214 + '<loc>https://dev.extremstyle.ua/ru/' . $this->fileName . '</loc>'.
  215 + '<lastmod>' . date('Y-m-d') . '</lastmod>'.
  216 + '</sitemap>'.
  217 + '</sitemapindex>';
  218 +
  219 + $mainHandle = fopen(Yii::getAlias('@frontend') . '/web/sitemap.xml', "w");
  220 + fwrite($mainHandle, $this->mainMap);
  221 + fclose($mainHandle);
276 222  
277   - print $dirName . '/' . $filename;
  223 + print Yii::getAlias('@frontend') . '/web' . '/' . $this->fileName;
278 224 }
279 225  
280 226 }
... ...
models/Category.php
... ... @@ -318,7 +318,7 @@
318 318 public function getTaxGroups()
319 319 {
320 320 return $this->hasMany(TaxGroup::className(), [ 'id' => 'tax_group_id' ])
321   - ->viaTable('tax_group_to_category', [ 'category_id' => 'id' ])->inverseOf('categories');
  321 + ->viaTable('tax_group_to_category', [ 'category_id' => 'id' ]);
322 322 }
323 323  
324 324 /**
... ...