|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
5
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace eZ\Bundle\EzPublishCoreBundle\Command; |
|
10
|
|
|
|
|
11
|
|
|
use eZ\Bundle\EzPublishCoreBundle\URLChecker\URLCheckerInterface; |
|
12
|
|
|
use eZ\Publish\API\Repository\PermissionResolver; |
|
13
|
|
|
use eZ\Publish\API\Repository\URLService; |
|
14
|
|
|
use eZ\Publish\API\Repository\UserService; |
|
15
|
|
|
use eZ\Publish\API\Repository\Values\URL\Query\Criterion; |
|
16
|
|
|
use eZ\Publish\API\Repository\Values\URL\Query\SortClause; |
|
17
|
|
|
use eZ\Publish\API\Repository\Values\URL\URLQuery; |
|
18
|
|
|
use RuntimeException; |
|
19
|
|
|
use Symfony\Component\Console\Command\Command; |
|
20
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
|
21
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
22
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
23
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
24
|
|
|
|
|
25
|
|
|
class CheckURLsCommand extends Command |
|
26
|
|
|
{ |
|
27
|
|
|
private const DEFAULT_ITERATION_COUNT = 50; |
|
28
|
|
|
private const DEFAULT_REPOSITORY_USER = 'admin'; |
|
29
|
|
|
|
|
30
|
|
|
/** @var \eZ\Publish\API\Repository\UserService */ |
|
31
|
|
|
private $userService; |
|
32
|
|
|
|
|
33
|
|
|
/** @var \eZ\Publish\API\Repository\PermissionResolver */ |
|
34
|
|
|
private $permissionResolver; |
|
35
|
|
|
|
|
36
|
|
|
/** @var \eZ\Publish\API\Repository\URLService */ |
|
37
|
|
|
private $urlService; |
|
38
|
|
|
|
|
39
|
|
|
/** @var \eZ\Bundle\EzPublishCoreBundle\URLChecker\URLCheckerInterface */ |
|
40
|
|
|
private $urlChecker; |
|
41
|
|
|
|
|
42
|
|
|
public function __construct( |
|
43
|
|
|
UserService $userService, |
|
44
|
|
|
PermissionResolver $permissionResolver, |
|
45
|
|
|
URLService $urlService, |
|
46
|
|
|
URLCheckerInterface $urlChecker |
|
47
|
|
|
) { |
|
48
|
|
|
parent::__construct('ezplatform:check-urls'); |
|
49
|
|
|
|
|
50
|
|
|
$this->userService = $userService; |
|
51
|
|
|
$this->permissionResolver = $permissionResolver; |
|
52
|
|
|
$this->urlService = $urlService; |
|
53
|
|
|
$this->urlChecker = $urlChecker; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function configure(): void |
|
57
|
|
|
{ |
|
58
|
|
|
$this->setDescription('Checks validity of external URLs'); |
|
59
|
|
|
|
|
60
|
|
|
$this->addOption( |
|
61
|
|
|
'iteration-count', |
|
62
|
|
|
'c', |
|
63
|
|
|
InputOption::VALUE_OPTIONAL, |
|
64
|
|
|
'Number of urls to be checked in a single iteration, for avoiding using too much memory', |
|
65
|
|
|
self::DEFAULT_ITERATION_COUNT |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
|
|
$this->addOption( |
|
69
|
|
|
'user', |
|
70
|
|
|
'u', |
|
71
|
|
|
InputOption::VALUE_OPTIONAL, |
|
72
|
|
|
'eZ Platform username (with Role containing at least Content policies: read, versionread, edit, remove, versionremove)', |
|
73
|
|
|
self::DEFAULT_REPOSITORY_USER |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): void |
|
78
|
|
|
{ |
|
79
|
|
|
$this->permissionResolver->setCurrentUserReference( |
|
80
|
|
|
$this->userService->loadUserByLogin($input->getOption('user')) |
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
$limit = $input->getOption('iteration-count'); |
|
84
|
|
|
if (!ctype_digit($limit) || (int)$limit < 1) { |
|
85
|
|
|
throw new RuntimeException("'--iteration-count' option should be > 0, got '{$limit}'"); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$limit = (int)$limit; |
|
89
|
|
|
|
|
90
|
|
|
$query = new URLQuery(); |
|
91
|
|
|
$query->filter = new Criterion\VisibleOnly(); |
|
92
|
|
|
$query->sortClauses = [ |
|
93
|
|
|
new SortClause\URL(), |
|
94
|
|
|
]; |
|
95
|
|
|
$query->offset = 0; |
|
96
|
|
|
$query->limit = $limit; |
|
97
|
|
|
|
|
98
|
|
|
$totalCount = $this->getTotalCount(); |
|
99
|
|
|
|
|
100
|
|
|
$progress = new ProgressBar($output, $totalCount); |
|
101
|
|
|
$progress->start(); |
|
102
|
|
|
while ($query->offset < $totalCount) { |
|
103
|
|
|
$this->urlChecker->check($query); |
|
104
|
|
|
|
|
105
|
|
|
$progress->advance(min($limit, $totalCount - $query->offset)); |
|
106
|
|
|
$query->offset += $limit; |
|
107
|
|
|
} |
|
108
|
|
|
$progress->finish(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
private function getTotalCount(): int |
|
112
|
|
|
{ |
|
113
|
|
|
$query = new URLQuery(); |
|
114
|
|
|
$query->filter = new Criterion\VisibleOnly(); |
|
115
|
|
|
$query->limit = 0; |
|
116
|
|
|
|
|
117
|
|
|
return $this->urlService->findUrls($query)->totalCount; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|