From d77e7532a89b98f9e6ec71d8343a39d3df53268b Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 2 Dec 2016 18:28:25 +0200 Subject: [PATCH] -sitemap try --- console/SiteMapController.php | 150 ++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------------------------------------------ models/Category.php | 2 +- 2 files changed, 49 insertions(+), 103 deletions(-) diff --git a/console/SiteMapController.php b/console/SiteMapController.php index 61f4498..92ed672 100755 --- a/console/SiteMapController.php +++ b/console/SiteMapController.php @@ -4,36 +4,24 @@ use artweb\artbox\ecommerce\models\Brand; use artweb\artbox\ecommerce\models\ProductVariant; - use artweb\artbox\language\models\Language; use artweb\artbox\seo\models\Seo; use artweb\artbox\ecommerce\models\Category; - use artweb\artbox\ecommerce\models\Product; - use artweb\artbox\ecommerce\models\ProductFrontendSearch; use Yii; use artweb\artbox\models\Page; use yii\helpers\ArrayHelper; + use yii\helpers\Console; use yii\helpers\Url; use yii\console\Controller; - /** - * PageController implements the CRUD actions for Page model. - */ class SiteMapController extends Controller { private $urlList = [ 'https://dev.extremstyle.ua/ru' ]; private $count = 1; - - public function checkFilter($category, $filter) - { - $productModel = new ProductFrontendSearch(); - $productProvider = $productModel->search($category, $filter); - if (!empty( $productProvider->models )) { - return true; - } else { - return false; - } - } + public $fileName; + public $handle; + public $mapNumber = 1; + public $mainMap = ''; public function getAddStatic() { @@ -47,7 +35,7 @@ return ProductVariant::find() ->with('lang') ->with('product.lang') - ->batch(1000); + ->batch(10000); } @@ -79,7 +67,7 @@ ->with('lang') ->joinWith('taxGroups.lang') ->with('taxGroups.taxOptions.lang') - ->where(['tax_group.meta_robots' => '',]) + ->where([ 'tax_group.meta_robots' => '', ]) ->all(); } @@ -90,25 +78,6 @@ ->all(); } - /** - * @param $category Category; - * - * @return mixed - */ - - public function getFilters($category) - { - return $category->getActiveFilters() - ->andWhere( - [ - - 'tax_group.meta_robots' => '', - ] - ) - ->all(); - - } - public function checkUrl($url) { if (!in_array($url, $this->urlList)) { @@ -122,10 +91,30 @@ public function createRow($url, $priority, &$content) { if ($this->checkUrl($url)) { - print $this->count++ . $url . "\n"; + $this->stdout($this->count . " : ", Console::BOLD); + $this->stdout($url . "\n", Console::FG_YELLOW); $content .= '' . '' . $url . '' . '' . date( 'Y-m-d' ) . '' . 'Weekly' . '' . $priority . '' . ''; + $this->count++; + if ($this->count % 40000 == 0) { + $content .= ''; + + fwrite($this->handle, $content); + fclose($this->handle); + unset($content); + $this->mapNumber++; + + $this->mainMap .= ''. + 'https://dev.extremstyle.ua/ru/' . $this->fileName . ''. + '' . date('Y-m-d') . ''. + ''; + + $this->fileName = 'sitemap' . $this->mapNumber . '.xml'; + $this->handle = fopen(Yii::getAlias('@frontend') . '/web' . '/' . $this->fileName, "w"); + + $content = ''; + } } } @@ -137,12 +126,11 @@ ); Yii::$app->urlManager->addRules($config[ 'components' ][ 'urlManager' ][ 'rules' ]); - - $dirName = Yii::getAlias('@frontend') . '/web'; - - $filename = 'test_sitemap.xml'; + $this->mainMap = ''; + $this->mainMap .= ''; + $this->fileName = 'test_sitemap' . $this->mapNumber . '.xml'; setlocale(LC_ALL, 'ru_RU.CP1251'); - $handle = fopen($dirName . '/' . $filename, "w"); + $this->handle = fopen(Yii::getAlias('@frontend') . '/web' . '/' . $this->fileName, "w"); $content = ''; @@ -184,7 +172,7 @@ } foreach ($this->getBrands() as $brand) { - + $url = Url::to( [ 'brand/view', @@ -192,7 +180,7 @@ ] ); $this->createRow($url, 0.7, $content); - + } foreach ($this->getCategoriesWithFilters() as $category) { @@ -214,67 +202,25 @@ foreach ($this->getSeoLinks() as $link) { $url = Yii::$app->urlManager->baseUrl . $link->url; $this->createRow($url, 0.7, $content); - + } - // foreach ($this->getCategories() as $category) { - // foreach ($this->getFilters($category) as $filter1) { - // foreach ($this->getFilters($category) as $filter2) { - // if ($this->checkFilter( - // $category, - // [ - // $filter1[ 'group_alias' ] => [ $filter1[ 'option_alias' ] ], - // $filter2[ 'group_alias' ] => [ $filter2[ 'option_alias' ] ], - // ] - // ) - // ) { - // $url = Url::to( - // [ - // 'catalog/category', - // 'category' => $category, - // 'filters' => [ - // $filter1[ 'group_alias' ] => [ $filter1[ 'option_alias' ] ], - // $filter2[ 'group_alias' ] => [ $filter2[ 'option_alias' ] ], - // ], - // ] - // ); - // $this->createRow($url, 0.7, $content); - // } - // - // } - // - // foreach ($this->getBrands($category) as $brand) { - // if ($this->checkFilter( - // $category, - // [ - // 'brands' => [ $brand->id ], - // $filter1[ 'group_alias' ] => [ $filter1[ 'option_alias' ] ], - // ] - // ) - // ) { - // $url = Url::to( - // [ - // 'catalog/category', - // 'category' => $category, - // 'filters' => [ - // 'brands' => [ $brand->alias ], - // $filter1[ 'group_alias' ] => [ $filter1[ 'option_alias' ] ], - // ], - // ] - // ); - // $this->createRow($url, 0.7, $content); - // } - // - // } - // } - // } - $content .= ''; - fwrite($handle, $content); - fclose($handle); + fwrite($this->handle, $content); + fclose($this->handle); + + $this->mainMap .= ''. + 'https://dev.extremstyle.ua/ru/' . $this->fileName . ''. + '' . date('Y-m-d') . ''. + ''. + ''; + + $mainHandle = fopen(Yii::getAlias('@frontend') . '/web/sitemap.xml', "w"); + fwrite($mainHandle, $this->mainMap); + fclose($mainHandle); - print $dirName . '/' . $filename; + print Yii::getAlias('@frontend') . '/web' . '/' . $this->fileName; } } diff --git a/models/Category.php b/models/Category.php index 8c6c0b6..d181cb0 100755 --- a/models/Category.php +++ b/models/Category.php @@ -318,7 +318,7 @@ public function getTaxGroups() { return $this->hasMany(TaxGroup::className(), [ 'id' => 'tax_group_id' ]) - ->viaTable('tax_group_to_category', [ 'category_id' => 'id' ])->inverseOf('categories'); + ->viaTable('tax_group_to_category', [ 'category_id' => 'id' ]); } /** -- libgit2 0.21.4