Commit ee2ceed332ad301defa23cf207b5592f6914615a

Authored by Alexey Boroda
1 parent 00a6457b

-Sitemap beta

Showing 1 changed file with 31 additions and 26 deletions   Show diff stats
console/SiteMapController.php
... ... @@ -16,17 +16,18 @@
16 16 class SiteMapController extends Controller
17 17 {
18 18  
19   - private $urlList = [ 'https://dev.extremstyle.ua/ru' ];
  19 + private $urlList = [ 'https://extremstyle.ua/ru' ];
20 20 private $count = 1;
21 21 public $fileName;
22 22 public $handle;
23 23 public $mapNumber = 1;
24 24 public $mainMap = '';
  25 + public $content = '';
25 26  
26 27 public function getAddStatic()
27 28 {
28 29 return [
29   - 'https://dev.extremstyle.ua/ru',
  30 + 'https://extremstyle.ua/ru',
30 31 ];
31 32 }
32 33  
... ... @@ -88,34 +89,35 @@
88 89 }
89 90 }
90 91  
91   - public function createRow($url, $priority, &$content)
  92 + public function createRow($url, $priority)
92 93 {
93   - if ($this->checkUrl($url)) {
  94 +// if ($this->checkUrl($url)) {
  95 + if($this->count % 500 == 0) {
94 96 $this->stdout($this->count . " : ", Console::BOLD);
95 97 $this->stdout($url . "\n", Console::FG_YELLOW);
96   - $content .= '<url>' . '<loc>' . $url . '</loc>' . '<lastmod>' . date(
  98 + }
  99 + $this->content .= '<url>' . '<loc>' . $url . '</loc>' . '<lastmod>' . date(
97 100 'Y-m-d'
98 101 ) . '</lastmod>' . '<changefreq>Weekly</changefreq>' . '<priority>' . $priority . '</priority>' . '</url>';
99 102 $this->count++;
100   - if ($this->count % 40000 == 0) {
101   - $content .= '</urlset>';
102   -
103   - fwrite($this->handle, $content);
  103 + if ($this->count % 10000 == 0) {
  104 + $this->content .= '</urlset>';
  105 + $this->stdout('Added unset' . "\n", Console::FG_CYAN);
  106 + fwrite($this->handle, $this->content);
104 107 fclose($this->handle);
105   - unset($content);
106 108 $this->mapNumber++;
107 109  
108 110 $this->mainMap .= '<sitemap>'.
109   - '<loc>https://dev.extremstyle.ua/ru/' . $this->fileName . '</loc>'.
  111 + '<loc>https://extremstyle.ua/ru/' . $this->fileName . '</loc>'.
110 112 '<lastmod>' . date('Y-m-d') . '</lastmod>'.
111 113 '</sitemap>';
112 114  
113 115 $this->fileName = 'sitemap' . $this->mapNumber . '.xml';
114 116 $this->handle = fopen(Yii::getAlias('@frontend') . '/web' . '/' . $this->fileName, "w");
115 117  
116   - $content = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
  118 + $this->content = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
117 119 }
118   - }
  120 +// }
119 121 }
120 122  
121 123 public function actionProcess()
... ... @@ -128,14 +130,14 @@
128 130 Yii::$app->urlManager->addRules($config[ 'components' ][ 'urlManager' ][ 'rules' ]);
129 131 $this->mainMap = '<?xml version="1.0" encoding="UTF-8"?>';
130 132 $this->mainMap .= '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
131   - $this->fileName = 'test_sitemap' . $this->mapNumber . '.xml';
  133 + $this->fileName = 'sitemap' . $this->mapNumber . '.xml';
132 134 setlocale(LC_ALL, 'ru_RU.CP1251');
133 135 $this->handle = fopen(Yii::getAlias('@frontend') . '/web' . '/' . $this->fileName, "w");
134 136  
135   - $content = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
  137 + $this->content = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
136 138  
137 139 foreach ($this->getAddStatic() as $page) {
138   - $this->createRow($page, 1, $content);
  140 + $this->createRow($page, 1);
139 141 }
140 142  
141 143 foreach ($this->getStaticPages() as $page) {
... ... @@ -145,7 +147,7 @@
145 147 'slug' => $page->lang->alias,
146 148 ]
147 149 );
148   - $this->createRow($url, 1, $content);
  150 + $this->createRow($url, 1);
149 151 }
150 152  
151 153 foreach ($this->getCategories() as $category) {
... ... @@ -155,11 +157,14 @@
155 157 'category' => $category->lang->alias,
156 158 ]
157 159 );
158   - $this->createRow($url, 0.8, $content);
  160 + $this->createRow($url, 0.8);
159 161 }
160 162  
161 163 foreach ($this->getVariants() as $rows) {
162 164 foreach ($rows as $row) {
  165 + if(!preg_match("@^[a-zA-Z\d]+$@i", $row->sku)) {
  166 + continue;
  167 + }
163 168 $url = Url::to(
164 169 [
165 170 'catalog/product',
... ... @@ -167,7 +172,7 @@
167 172 'variant' => $row->sku,
168 173 ]
169 174 );
170   - $this->createRow($url, 0.9, $content);
  175 + $this->createRow($url, 0.9);
171 176 }
172 177 }
173 178  
... ... @@ -179,7 +184,7 @@
179 184 'slug' => $brand->lang->alias,
180 185 ]
181 186 );
182   - $this->createRow($url, 0.7, $content);
  187 + $this->createRow($url, 0.7);
183 188  
184 189 }
185 190  
... ... @@ -193,7 +198,7 @@
193 198 'filters' => [ $group->lang->alias => [ $option->lang->alias ] ],
194 199 ]
195 200 );
196   - $this->createRow($url, 0.8, $content);
  201 + $this->createRow($url, 0.8);
197 202 }
198 203  
199 204 }
... ... @@ -201,17 +206,17 @@
201 206  
202 207 foreach ($this->getSeoLinks() as $link) {
203 208 $url = Yii::$app->urlManager->baseUrl . $link->url;
204   - $this->createRow($url, 0.7, $content);
  209 + $this->createRow($url, 0.7);
205 210  
206 211 }
207 212  
208   - $content .= '</urlset>';
  213 + $this->content .= '</urlset>';
209 214  
210   - fwrite($this->handle, $content);
  215 + fwrite($this->handle, $this->content);
211 216 fclose($this->handle);
212 217  
213 218 $this->mainMap .= '<sitemap>'.
214   - '<loc>https://dev.extremstyle.ua/ru/' . $this->fileName . '</loc>'.
  219 + '<loc>https://extremstyle.ua/ru/' . $this->fileName . '</loc>'.
215 220 '<lastmod>' . date('Y-m-d') . '</lastmod>'.
216 221 '</sitemap>'.
217 222 '</sitemapindex>';
... ... @@ -220,7 +225,7 @@
220 225 fwrite($mainHandle, $this->mainMap);
221 226 fclose($mainHandle);
222 227  
223   - print Yii::getAlias('@frontend') . '/web' . '/' . $this->fileName;
  228 + $this->stdout(Yii::getAlias('@frontend') . '/web' . '/' . $this->fileName . "\n", Console::FG_GREEN);
224 229 }
225 230  
226 231 }
... ...