From 9ddd699effc7b976cd4c47a583eaeae5e0fba871 Mon Sep 17 00:00:00 2001 From: hideki_okajima Date: Thu, 22 Apr 2021 14:14:14 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat=20SiteMap:=20=E5=95=86=E5=93=81?= =?UTF-8?q?=E3=81=AE=E5=8F=96=E5=BE=97=E3=82=92=20QueryBuilder=20=E3=81=AE?= =?UTF-8?q?=E3=82=AB=E3=82=B9=E3=82=BF=E3=83=9E=E3=82=A4=E3=82=BA=E3=82=92?= =?UTF-8?q?=E8=80=83=E6=85=AE=E3=81=97=E3=81=9F=E5=AE=9F=E8=A3=85=E3=81=AB?= =?UTF-8?q?=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Eccube/Controller/SitemapController.php | 69 ++++++++++++------- .../template/default/sitemap_index.xml.twig | 2 +- .../Tests/Web/SitemapControllerTest.php | 6 ++ 3 files changed, 52 insertions(+), 25 deletions(-) diff --git a/src/Eccube/Controller/SitemapController.php b/src/Eccube/Controller/SitemapController.php index 5db3a32546c..4c3fdaca547 100644 --- a/src/Eccube/Controller/SitemapController.php +++ b/src/Eccube/Controller/SitemapController.php @@ -15,8 +15,11 @@ use Eccube\Entity\Page; use Eccube\Repository\CategoryRepository; +use Eccube\Repository\Master\ProductListOrderByRepository; use Eccube\Repository\PageRepository; use Eccube\Repository\ProductRepository; +use Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination; +use Knp\Component\Pager\Paginator; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; @@ -25,11 +28,6 @@ class SitemapController extends AbstractController { - /** - * @var ProductRepository - */ - private $productRepository; - /** * @var CategoryRepository */ @@ -40,6 +38,16 @@ class SitemapController extends AbstractController */ private $pageRepository; + /** + * @var ProductListOrderByRepository + */ + private $productListOrderByRepository; + + /** + * @var ProductRepository + */ + private $productRepository; + /** * @var RouterInterface */ @@ -54,14 +62,16 @@ class SitemapController extends AbstractController * SitemapController constructor. */ public function __construct( - ProductRepository $productRepository, CategoryRepository $categoryRepository, PageRepository $pageRepository, + ProductListOrderByRepository $productListOrderByRepository, + ProductRepository $productRepository, RouterInterface $router ) { - $this->productRepository = $productRepository; $this->categoryRepository = $categoryRepository; $this->pageRepository = $pageRepository; + $this->productListOrderByRepository = $productListOrderByRepository; + $this->productRepository = $productRepository; $this->router = $router; } @@ -70,10 +80,10 @@ public function __construct( * * @Route("/sitemap.xml", name="sitemap_xml") */ - public function index() + public function index(Paginator $paginator) { - $qb = $this->pageRepository->createQueryBuilder('p'); - $Page = $qb->select('p') + $pageQueryBuilder = $this->pageRepository->createQueryBuilder('p'); + $Page = $pageQueryBuilder->select('p') ->where("((p.meta_robots not like '%noindex%' and p.meta_robots not like '%none%') or p.meta_robots IS NULL)") ->andWhere('p.id <> 0') ->andWhere('p.MasterPage is null') @@ -83,7 +93,17 @@ public function index() ->getSingleResult(); $Product = $this->productRepository->findOneBy(['Status' => 1], ['update_date' => 'DESC']); - $ProductCount = $this->productRepository->count(['Status' => 1]); + + // フロントの商品一覧の条件で商品情報を取得 + $ProductListOrder = $this->productListOrderByRepository->find($this->eccubeConfig['eccube_product_order_newer']); + $productQueryBuilder = $this->productRepository->getQueryBuilderBySearchData(['orderby' => $ProductListOrder]); + /** @var SlidingPagination $pagination */ + $pagination = $paginator->paginate( + $productQueryBuilder, + 1, + $this->item_per_page + ); + $paginationData = $pagination->getPaginationData(); $Category = $this->categoryRepository->findOneBy([], ['update_date' => 'DESC']); @@ -91,7 +111,7 @@ public function index() [ 'Category' => $Category, 'Product' => $Product, - 'ProductPageCount' => ceil($ProductCount / $this->item_per_page), + 'productPageCount' => $paginationData['pageCount'], 'Page' => $Page, ], 'sitemap_index.xml.twig' @@ -116,25 +136,27 @@ public function category() * Output sitemap of products as status is 1 * * @Route("/sitemap_product_{page}.xml", name="sitemap_product_xml", requirements={"page" = "\d+"}) - * @param Request $request + * * @return Response */ - public function product(Request $request) + public function product(Request $request, Paginator $paginator) { - $page = (int)$request->get('page'); - - $Products = $this->productRepository->findBy( - ['Status' => 1], - ['update_date' => 'DESC'], - $this->item_per_page, - ($page - 1) * $this->item_per_page + // フロントの商品一覧の条件で商品情報を取得 + $ProductListOrder = $this->productListOrderByRepository->find($this->eccubeConfig['eccube_product_order_newer']); + $productQueryBuilder = $this->productRepository->getQueryBuilderBySearchData(['orderby' => $ProductListOrder]); + /** @var SlidingPagination $pagination */ + $pagination = $paginator->paginate( + $productQueryBuilder, + $request->get('page'), + $this->item_per_page ); + $paginationData = $pagination->getPaginationData(); - if (!$Products) { + if ($paginationData['currentItemCount'] === 0) { throw new NotFoundHttpException(); } - return $this->outputXml(['Products' => $Products]); + return $this->outputXml(['Products' => $pagination]); } /** @@ -163,7 +185,6 @@ public function page() /** * Output XML response by data. * - * @param array $data * @param string $template_name * * @return Response diff --git a/src/Eccube/Resource/template/default/sitemap_index.xml.twig b/src/Eccube/Resource/template/default/sitemap_index.xml.twig index 2ea48ba8409..e342f757372 100644 --- a/src/Eccube/Resource/template/default/sitemap_index.xml.twig +++ b/src/Eccube/Resource/template/default/sitemap_index.xml.twig @@ -8,7 +8,7 @@ {{url('sitemap_category_xml')}} {{Category.update_date|date_format('','c')}} - {% for p in 1..ProductPageCount %} + {% for p in 1..productPageCount %} {{url('sitemap_product_xml', {page: p})}} {{Product.update_date|date_format('','c')}} diff --git a/tests/Eccube/Tests/Web/SitemapControllerTest.php b/tests/Eccube/Tests/Web/SitemapControllerTest.php index 90104eed057..9114c87641e 100644 --- a/tests/Eccube/Tests/Web/SitemapControllerTest.php +++ b/tests/Eccube/Tests/Web/SitemapControllerTest.php @@ -27,6 +27,12 @@ public function testProduct() $this->assertTrue($this->client->getResponse()->isSuccessful()); } + public function testProduct404() + { + $this->client->request('GET', $this->generateUrl('sitemap_product_xml', ['page' => 9999])); + $this->assertEquals(404, $this->client->getResponse()->getStatusCode()); + } + public function testCategory() { $this->client->request('GET', $this->generateUrl('sitemap_category_xml')); From 54a0b9fcb84a4e0e8af15b7beed0ea3b4d8f60b2 Mon Sep 17 00:00:00 2001 From: hideki_okajima Date: Thu, 22 Apr 2021 14:15:34 +0900 Subject: [PATCH 2/3] =?UTF-8?q?feat=20SiteMap:=20=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E7=94=BB=E9=9D=A2=E3=81=8B=E3=82=89=E4=BD=9C=E6=88=90=E3=81=95?= =?UTF-8?q?=E3=82=8C=E3=81=9F=E3=83=9A=E3=83=BC=E3=82=B8=E3=81=AB=E5=AF=BE?= =?UTF-8?q?=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Eccube/Controller/SitemapController.php | 17 +++++++++++++-- .../template/default/sitemap.xml.twig | 21 +++++++++++++------ 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/Eccube/Controller/SitemapController.php b/src/Eccube/Controller/SitemapController.php index 4c3fdaca547..155246ff76e 100644 --- a/src/Eccube/Controller/SitemapController.php +++ b/src/Eccube/Controller/SitemapController.php @@ -171,7 +171,12 @@ public function page() $Pages = $this->pageRepository->getPageList("((p.meta_robots not like '%noindex%' and p.meta_robots not like '%none%') or p.meta_robots IS NULL)"); // URL に変数が含まれる場合は URL の生成ができないためここで除外する - $Pages = array_filter($Pages, function (Page $Page) { + $DefaultPages = array_filter($Pages, function (Page $Page) { + // 管理画面から作成されたページは除外 + if ($Page->getEditType() === Page::EDIT_TYPE_USER) { + return false; + } + $route = $this->router->getRouteCollection()->get($Page->getUrl()); if (is_null($route)) { return false; @@ -179,7 +184,15 @@ public function page() return count($route->compile()->getPathVariables()) < 1; }); - return $this->outputXml(['Pages' => $Pages]); + // 管理画面から作成されたページ + $UserPages = array_filter($Pages, function (Page $Page) { + return $Page->getEditType() === Page::EDIT_TYPE_USER; + }); + + return $this->outputXml([ + 'DefaultPages' => $DefaultPages, + 'UserPages' => $UserPages, + ]); } /** diff --git a/src/Eccube/Resource/template/default/sitemap.xml.twig b/src/Eccube/Resource/template/default/sitemap.xml.twig index 55858da3d5a..7fa945ca705 100644 --- a/src/Eccube/Resource/template/default/sitemap.xml.twig +++ b/src/Eccube/Resource/template/default/sitemap.xml.twig @@ -1,17 +1,26 @@ {# Pages #} -{% if Pages is defined %} -{% for Page in Pages %} - {% if Page.url != 'product_detail' and Page.url != 'product_list' %} +{% if DefaultPages is defined %} +{% for DefaultPage in DefaultPages %} + {% if DefaultPage.url != 'product_detail' and DefaultPage.url != 'product_list' %} - {{url(Page.url)}} - {{Page.update_date|date_format('','c')}} + {{url(DefaultPage.url)}} + {{DefaultPage.update_date|date_format('','c')}} daily {% endif %} {% endfor %} {% endif %} +{% if UserPages is defined %} +{% for UserPage in UserPages %} + + {{url('user_data', {route: UserPage.url})}} + {{UserPage.update_date|date_format('','c')}} + daily + +{% endfor %} +{% endif %} {# Categories #} {% if Categories is defined %} {% for Category in Categories %} @@ -37,4 +46,4 @@ {% endfor %} {% endif %} - + From 257e99aacb139b4ad1927aa2bb7e56c317ff3006 Mon Sep 17 00:00:00 2001 From: hideki_okajima Date: Thu, 22 Apr 2021 14:29:34 +0900 Subject: [PATCH 3/3] =?UTF-8?q?feat=20SiteMap:=20=E3=83=9A=E3=83=BC?= =?UTF-8?q?=E3=82=B8=E3=81=82=E3=81=9F=E3=82=8A=E3=81=AE=E5=95=86=E5=93=81?= =?UTF-8?q?=E6=95=B0=E3=81=AE=E8=A8=AD=E5=AE=9A=E3=82=92=20eccube.yaml=20?= =?UTF-8?q?=E3=81=AB=E7=A7=BB=E5=8B=95=E3=81=97=E3=81=BE=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/config/eccube/packages/eccube.yaml | 1 + src/Eccube/Controller/SitemapController.php | 9 ++------- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/app/config/eccube/packages/eccube.yaml b/app/config/eccube/packages/eccube.yaml index 202725d6902..14cc3bbe889 100644 --- a/app/config/eccube/packages/eccube.yaml +++ b/app/config/eccube/packages/eccube.yaml @@ -64,6 +64,7 @@ parameters: plugin_temp_realdir: /PATH/TO/WEB_ROOT/src/Eccube/Repository/Master/upload/temp_plugin/ # upload_tmp_dir に任せればよい? eccube_price_len: 8 # 最大値で制御したい eccube_search_pmax: 12 + eccube_sitemap_products_per_page: 1000 eccube_stext_len: 255 eccube_sltext_len: 500 eccube_smtext_len: 100 diff --git a/src/Eccube/Controller/SitemapController.php b/src/Eccube/Controller/SitemapController.php index 155246ff76e..b0d6c9ed2c6 100644 --- a/src/Eccube/Controller/SitemapController.php +++ b/src/Eccube/Controller/SitemapController.php @@ -53,11 +53,6 @@ class SitemapController extends AbstractController */ private $router; - /** - * @var int - */ - private $item_per_page = 1000; - /** * SitemapController constructor. */ @@ -101,7 +96,7 @@ public function index(Paginator $paginator) $pagination = $paginator->paginate( $productQueryBuilder, 1, - $this->item_per_page + $this->eccubeConfig['eccube_sitemap_products_per_page'] ); $paginationData = $pagination->getPaginationData(); @@ -148,7 +143,7 @@ public function product(Request $request, Paginator $paginator) $pagination = $paginator->paginate( $productQueryBuilder, $request->get('page'), - $this->item_per_page + $this->eccubeConfig['eccube_sitemap_products_per_page'] ); $paginationData = $pagination->getPaginationData();