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

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. /**
  21. * @var SessionManagerInterface
  22. */
  23. private $sessionManager;
  24. /**
  25. * @var ContentMapperInterface
  26. */
  27. private $contentMapper;
  28. /**
  29. * @var Stopwatch
  30. */
  31. private $stopwatch;
  32. public function __construct(
  33. SessionManagerInterface $sessionManager,
  34. ContentMapperInterface $contentMapper,
  35. ?Stopwatch $stopwatch = null
  36. ) {
  37. $this->sessionManager = $sessionManager;
  38. $this->contentMapper = $contentMapper;
  39. $this->stopwatch = $stopwatch;
  40. }
  41. public function execute(
  42. $webspaceKey,
  43. $locales,
  44. ContentQueryBuilderInterface $contentQueryBuilder,
  45. $flat = true,
  46. $depth = -1,
  47. $limit = null,
  48. $offset = null,
  49. $moveUp = false,
  50. $permission = null
  51. ) {
  52. if ($this->stopwatch) {
  53. $this->stopwatch->start('ContentQuery::execute.build-query');
  54. }
  55. list($sql2, $fields) = $contentQueryBuilder->build($webspaceKey, $locales);
  56. if ($this->stopwatch) {
  57. $this->stopwatch->stop('ContentQuery::execute.build-query');
  58. $this->stopwatch->start('ContentQuery::execute.execute-query');
  59. }
  60. $query = $this->createSql2Query($sql2, $limit, $offset);
  61. $queryResult = $query->execute();
  62. if ($this->stopwatch) {
  63. $this->stopwatch->stop('ContentQuery::execute.execute-query');
  64. $this->stopwatch->start('ContentQuery::execute.preload-nodes.get-paths');
  65. }
  66. // this preloads all node which should are selected in the statement before
  67. // prevent the system to load each node individual
  68. $rootDepth = \substr_count($this->sessionManager->getContentPath($webspaceKey), '/');
  69. $paths = [];
  70. /** @var Row $row */
  71. foreach ($queryResult as $row) {
  72. $pageDepth = \substr_count($row->getPath('page'), '/') - $rootDepth;
  73. if (null === $depth || $depth < 0 || ($depth > 0 && $pageDepth <= $depth)) {
  74. $paths[] = $row->getPath('page');
  75. }
  76. }
  77. if ($this->stopwatch) {
  78. $this->stopwatch->stop('ContentQuery::execute.preload-nodes.get-paths');
  79. $this->stopwatch->start('ContentQuery::execute.preload-nodes.execute');
  80. }
  81. $this->sessionManager->getSession()->getNodes($paths);
  82. if ($this->stopwatch) {
  83. $this->stopwatch->stop('ContentQuery::execute.preload-nodes.execute');
  84. $this->stopwatch->start('ContentQuery::execute.rowsToList');
  85. }
  86. $result = $this->contentMapper->convertQueryResultToArray(
  87. $queryResult,
  88. $webspaceKey,
  89. $locales,
  90. $fields,
  91. $depth,
  92. $contentQueryBuilder->getPublished(),
  93. $permission
  94. );
  95. if ($this->stopwatch) {
  96. $this->stopwatch->stop('ContentQuery::execute.rowsToList');
  97. }
  98. if (!$flat) {
  99. if ($this->stopwatch) {
  100. $this->stopwatch->start('ContentQuery::execute.build-tree');
  101. }
  102. $converter = new ListToTreeConverter($moveUp);
  103. $result = $converter->convert($result);
  104. if ($this->stopwatch) {
  105. $this->stopwatch->stop('ContentQuery::execute.build-tree');
  106. }
  107. }
  108. return $result;
  109. }
  110. /**
  111. * returns a sql2 query.
  112. */
  113. private function createSql2Query($sql2, $limit = null, $offset = null)
  114. {
  115. $queryManager = $this->sessionManager->getSession()->getWorkspace()->getQueryManager();
  116. $query = $queryManager->createQuery($sql2, 'JCR-SQL2');
  117. if ($limit) {
  118. $query->setLimit($limit);
  119. }
  120. if ($offset) {
  121. $query->setOffset($offset);
  122. }
  123. return $query;
  124. }
  125. }