vendor/sulu/sulu/src/Sulu/Component/Content/Query/ContentQueryExecutor.php line 119

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of Sulu.
  4. *
  5. * (c) Sulu GmbH
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Sulu\Component\Content\Query;
  11. use Jackalope\Query\Row;
  12. use Sulu\Component\Content\Mapper\ContentMapperInterface;
  13. use Sulu\Component\PHPCR\SessionManager\SessionManagerInterface;
  14. use Symfony\Component\Stopwatch\Stopwatch;
  15. /**
  16. * Executes a query over the content.
  17. */
  18. class ContentQueryExecutor implements ContentQueryExecutorInterface
  19. {
  20. public function __construct(
  21. private SessionManagerInterface $sessionManager,
  22. private ContentMapperInterface $contentMapper,
  23. private ?Stopwatch $stopwatch = null,
  24. ) {
  25. }
  26. public function execute(
  27. $webspaceKey,
  28. $locales,
  29. ContentQueryBuilderInterface $contentQueryBuilder,
  30. $flat = true,
  31. $depth = -1,
  32. $limit = null,
  33. $offset = null,
  34. $moveUp = false,
  35. $permission = null
  36. ) {
  37. if ($this->stopwatch) {
  38. $this->stopwatch->start('ContentQuery::execute.build-query');
  39. }
  40. list($sql2, $fields) = $contentQueryBuilder->build($webspaceKey, $locales);
  41. if ($this->stopwatch) {
  42. $this->stopwatch->stop('ContentQuery::execute.build-query');
  43. $this->stopwatch->start('ContentQuery::execute.execute-query');
  44. }
  45. $query = $this->createSql2Query($sql2, $limit, $offset);
  46. $queryResult = $query->execute();
  47. if ($this->stopwatch) {
  48. $this->stopwatch->stop('ContentQuery::execute.execute-query');
  49. $this->stopwatch->start('ContentQuery::execute.preload-nodes.get-paths');
  50. }
  51. // this preloads all node which should are selected in the statement before
  52. // prevent the system to load each node individual
  53. $rootDepth = \substr_count($this->sessionManager->getContentPath($webspaceKey), '/');
  54. $paths = [];
  55. /** @var Row $row */
  56. foreach ($queryResult as $row) {
  57. $pageDepth = \substr_count($row->getPath('page'), '/') - $rootDepth;
  58. if (null === $depth || $depth < 0 || ($depth > 0 && $pageDepth <= $depth)) {
  59. $paths[] = $row->getPath('page');
  60. }
  61. }
  62. if ($this->stopwatch) {
  63. $this->stopwatch->stop('ContentQuery::execute.preload-nodes.get-paths');
  64. $this->stopwatch->start('ContentQuery::execute.preload-nodes.execute');
  65. }
  66. $this->sessionManager->getSession()->getNodes($paths);
  67. if ($this->stopwatch) {
  68. $this->stopwatch->stop('ContentQuery::execute.preload-nodes.execute');
  69. $this->stopwatch->start('ContentQuery::execute.rowsToList');
  70. }
  71. $result = $this->contentMapper->convertQueryResultToArray(
  72. $queryResult,
  73. $webspaceKey,
  74. $locales,
  75. $fields,
  76. $depth,
  77. $contentQueryBuilder->getPublished(),
  78. $permission
  79. );
  80. if ($this->stopwatch) {
  81. $this->stopwatch->stop('ContentQuery::execute.rowsToList');
  82. }
  83. if (!$flat) {
  84. if ($this->stopwatch) {
  85. $this->stopwatch->start('ContentQuery::execute.build-tree');
  86. }
  87. $converter = new ListToTreeConverter($moveUp);
  88. $result = $converter->convert($result);
  89. if ($this->stopwatch) {
  90. $this->stopwatch->stop('ContentQuery::execute.build-tree');
  91. }
  92. }
  93. return $result;
  94. }
  95. /**
  96. * returns a sql2 query.
  97. */
  98. private function createSql2Query($sql2, $limit = null, $offset = null)
  99. {
  100. $queryManager = $this->sessionManager->getSession()->getWorkspace()->getQueryManager();
  101. $query = $queryManager->createQuery($sql2, 'JCR-SQL2');
  102. if ($limit) {
  103. $query->setLimit($limit);
  104. }
  105. if ($offset) {
  106. $query->setOffset($offset);
  107. }
  108. return $query;
  109. }
  110. }