Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

CopySubtreeCommand.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Bundle\EzPublishCoreBundle\Command;
8
9 use eZ\Publish\API\Repository\ContentTypeService;
10 use eZ\Publish\API\Repository\LocationService;
11 use eZ\Publish\API\Repository\PermissionResolver;
12 use eZ\Publish\API\Repository\SearchService;
13 use eZ\Publish\API\Repository\UserService;
14 use eZ\Publish\API\Repository\Values\Content\Location;
15 use eZ\Publish\API\Repository\Values\Content\LocationQuery;
16 use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
17 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
18 use Symfony\Component\Console\Command\Command;
19 use Symfony\Component\Console\Input\InputArgument;
20 use Symfony\Component\Console\Input\InputInterface;
21 use Symfony\Component\Console\Input\InputOption;
22 use Symfony\Component\Console\Output\OutputInterface;
23 use Symfony\Component\Console\Question\ConfirmationQuestion;
24
28 class CopySubtreeCommand extends Command
29 {
31 private $locationService;
32
34 private $permissionResolver;
35
37 private $userService;
38
40 private $contentTypeService;
41
43 private $searchService;
44
52 public function __construct(
53 LocationService $locationService,
54 PermissionResolver $permissionResolver,
55 UserService $userService,
56 ContentTypeService $contentTypeService,
57 SearchService $searchService
58 ) {
59 parent::__construct();
60 $this->locationService = $locationService;
61 $this->permissionResolver = $permissionResolver;
62 $this->userService = $userService;
63 $this->contentTypeService = $contentTypeService;
64 $this->searchService = $searchService;
65 }
66
67 protected function configure()
68 {
69 $this
70 ->setName('ezplatform:copy-subtree')
71 ->addArgument(
72 'source-location-id',
73 InputArgument::REQUIRED,
74 'Id of subtree root location'
75 )
76 ->addArgument(
77 'target-location-id',
78 InputArgument::REQUIRED,
79 'Id of target location'
80 )
81 ->addOption(
82 'user',
83 'u',
84 InputOption::VALUE_OPTIONAL,
85 'eZ Platform username (with Role containing at least Content policies: create, read)',
86 'admin'
87 )
88 ->setDescription('Copy subtree from one location to another');
89 }
90
97 protected function initialize(InputInterface $input, OutputInterface $output)
98 {
99 parent::initialize($input, $output);
100 $this->permissionResolver->setCurrentUserReference(
101 $this->userService->loadUserByLogin($input->getOption('user'))
102 );
103 }
104
116 protected function execute(InputInterface $input, OutputInterface $output)
117 {
118 $sourceLocationId = $input->getArgument('source-location-id');
119 $targetLocationId = $input->getArgument('target-location-id');
120
121 $sourceLocation = $this->locationService->loadLocation($sourceLocationId);
122 $targetLocation = $this->locationService->loadLocation($targetLocationId);
123
124 if (stripos($targetLocation->pathString, $sourceLocation->pathString) !== false) {
125 throw new InvalidArgumentException(
126 'target-location-id',
127 'Target parent location is a sub location of the source subtree'
128 );
129 }
130
131 $targetContentType = $this->contentTypeService->loadContentType(
132 $targetLocation->getContentInfo()->contentTypeId
133 );
134
135 if (!$targetContentType->isContainer) {
136 throw new InvalidArgumentException(
137 'target-location-id',
138 'Cannot copy location to a parent that is not a container'
139 );
140 }
141 $questionHelper = $this->getHelper('question');
142 $question = new ConfirmationQuestion(
143 sprintf(
144 'Are you sure you want to copy `%s` subtree (no. of children: %d) into `%s`? This make take a while for big number of nested children [Y/n]? ',
145 $sourceLocation->contentInfo->name,
146 $this->getAllChildrenCount($sourceLocation),
147 $targetLocation->contentInfo->name
148 )
149 );
150
151 if (!$input->getOption('no-interaction') && !$questionHelper->ask($input, $output, $question)) {
152 return 0;
153 }
154
155 $this->locationService->copySubtree(
156 $sourceLocation,
157 $targetLocation
158 );
159
160 $output->writeln(
161 '<info>Finished</info>'
162 );
163
164 return 0;
165 }
166
174 protected function getAllChildrenCount(Location $location): int
175 {
176 $query = new LocationQuery([
177 'filter' => new Criterion\Subtree($location->pathString),
178 ]);
179
180 $searchResults = $this->searchService->findLocations($query);
181
182 return $searchResults->totalCount;
183 }
184 }
eZ\Bundle\EzPublishCoreBundle\Command\CopySubtreeCommand
Console command for deep copying subtree from one location to another.
Definition: CopySubtreeCommand.php:29
eZ\Bundle\EzPublishCoreBundle\Command\CopySubtreeCommand\$contentTypeService
$contentTypeService
Definition: CopySubtreeCommand.php:40
eZ\Bundle\EzPublishCoreBundle\Command\CopySubtreeCommand\initialize
initialize(InputInterface $input, OutputInterface $output)
Definition: CopySubtreeCommand.php:97
eZ\Bundle\EzPublishCoreBundle\Command\CopySubtreeCommand\configure
configure()
Definition: CopySubtreeCommand.php:67
eZ\Bundle\EzPublishCoreBundle\Command\CopySubtreeCommand\getAllChildrenCount
getAllChildrenCount(Location $location)
Definition: CopySubtreeCommand.php:174
eZ\Bundle\EzPublishCoreBundle\Command\CopySubtreeCommand\$userService
$userService
Definition: CopySubtreeCommand.php:37
eZ\Bundle\EzPublishCoreBundle\Command\CopySubtreeCommand\$searchService
$searchService
Definition: CopySubtreeCommand.php:43
eZ\Bundle\EzPublishCoreBundle\Command\CopySubtreeCommand\$permissionResolver
$permissionResolver
Definition: CopySubtreeCommand.php:34
eZ\Bundle\EzPublishCoreBundle\Command\CopySubtreeCommand\$locationService
$locationService
Definition: CopySubtreeCommand.php:31
eZ\Bundle\EzPublishCoreBundle\Command\CopySubtreeCommand\__construct
__construct(LocationService $locationService, PermissionResolver $permissionResolver, UserService $userService, ContentTypeService $contentTypeService, SearchService $searchService)
Definition: CopySubtreeCommand.php:52
eZ\Bundle\EzPublishCoreBundle\Command\CopySubtreeCommand\execute
execute(InputInterface $input, OutputInterface $output)
Definition: CopySubtreeCommand.php:116
eZ\Publish\API\Repository\Values\Content\LocationQuery
This class is used to perform a Location query.
Definition: API/Repository/Values/Content/LocationQuery.php:13
eZ\Publish\API\Repository\Values\Content\Location
Definition: API/Repository/Values/Content/Location.php:32
eZ\Publish\API\Repository\Values\Content\Query\Criterion\Subtree
Criterion that matches content that belongs to a given (list of) Subtree(s).
Definition: API/Repository/Values/Content/Query/Criterion/Subtree.php:19
eZ\Publish\API\Repository\Values\Content\Query\Criterion
Definition: API/Repository/Values/Content/Query/Criterion.php:15
eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
Invalid Argument Type Exception implementation.
Definition: Core/Base/Exceptions/InvalidArgumentException.php:20
eZ\Publish\API\Repository\ContentTypeService
Definition: API/Repository/ContentTypeService.php:25
eZ\Publish\API\Repository\LocationService
Definition: API/Repository/LocationService.php:21
eZ\Publish\API\Repository\PermissionResolver
This service provides methods for resolving permissions.
Definition: API/Repository/PermissionResolver.php:17
eZ\Publish\API\Repository\SearchService
Search service.
Definition: API/Repository/SearchService.php:17
eZ\Publish\API\Repository\UserService
Definition: API/Repository/UserService.php:26
eZ\Bundle\EzPublishCoreBundle\Command
Definition: CheckURLsCommand.php:7
eZ\Publish\API\Repository\Values\Content\Query\Criterion
Definition: API/Repository/Values/Content/Query/Criterion/Ancestor.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.