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
|
|
|
namespace eZ\Bundle\EzPublishCoreBundle\Command; |
8
|
|
|
|
9
|
|
|
use eZ\Publish\API\Repository\URLService; |
10
|
|
|
use eZ\Publish\API\Repository\Values\URL\Query\Criterion; |
11
|
|
|
use eZ\Publish\API\Repository\Values\URL\Query\SortClause; |
12
|
|
|
use eZ\Publish\API\Repository\Values\URL\URLQuery; |
13
|
|
|
use RuntimeException; |
14
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
15
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
17
|
|
|
use Symfony\Component\Console\Input\InputOption; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
|
20
|
|
|
class CheckURLsCommand extends ContainerAwareCommand |
21
|
|
|
{ |
22
|
|
|
const DEFAULT_ITERATION_COUNT = 50; |
23
|
|
|
const DEFAULT_REPOSITORY_USER = 'admin'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
public function configure() |
29
|
|
|
{ |
30
|
|
|
$this->setName('ezplatform:check-urls'); |
31
|
|
|
$this->setDescription('Checks validity of external URLs'); |
32
|
|
|
$this->addOption( |
33
|
|
|
'iteration-count', |
34
|
|
|
'c', |
35
|
|
|
InputOption::VALUE_OPTIONAL, |
36
|
|
|
'Number of urls to be checked in a single iteration, for avoiding using too much memory', |
37
|
|
|
self::DEFAULT_ITERATION_COUNT |
38
|
|
|
); |
39
|
|
|
$this->addOption( |
40
|
|
|
'user', |
41
|
|
|
'u', |
42
|
|
|
InputOption::VALUE_OPTIONAL, |
43
|
|
|
'eZ Platform username (with Role containing at least Content policies: read, versionread, edit, remove, versionremove)', |
44
|
|
|
self::DEFAULT_REPOSITORY_USER |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
52
|
|
|
{ |
53
|
|
|
$repository = $this->getContainer()->get('ezpublish.api.repository'); |
54
|
|
|
$repository->getPermissionResolver()->setCurrentUserReference( |
55
|
|
|
$repository->getUserService()->loadUserByLogin($input->getOption('user')) |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
$limit = $input->getOption('iteration-count'); |
59
|
|
|
if (!is_numeric($limit) || (int)$limit < 1) { |
60
|
|
|
throw new RuntimeException("'--iteration-count' option should be > 0, got '{$limit}'"); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$query = new URLQuery(); |
64
|
|
|
$query->filter = new Criterion\VisibleOnly(); |
65
|
|
|
$query->sortClauses = [ |
66
|
|
|
new SortClause\URL(), |
67
|
|
|
]; |
68
|
|
|
$query->offset = 0; |
69
|
|
|
$query->limit = $limit; |
|
|
|
|
70
|
|
|
|
71
|
|
|
$totalCount = $this->getTotalCount(clone $query); |
72
|
|
|
|
73
|
|
|
$progress = new ProgressBar($output, $totalCount); |
74
|
|
|
$progress->start(); |
75
|
|
|
while ($query->offset < $totalCount) { |
76
|
|
|
$this->getUrlHandler()->check($query); |
77
|
|
|
|
78
|
|
|
$progress->advance(min($limit, $totalCount - $query->offset)); |
79
|
|
|
$query->offset += $limit; |
80
|
|
|
} |
81
|
|
|
$progress->finish(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function getTotalCount(URLQuery $query) |
85
|
|
|
{ |
86
|
|
|
$repository = $this->getContainer()->get('ezpublish.api.repository'); |
87
|
|
|
/** @var URLService $urlService */ |
88
|
|
|
$urlService = $repository->getURLService(); |
89
|
|
|
|
90
|
|
|
$query->limit = 0; |
91
|
|
|
|
92
|
|
|
return $urlService->findUrls($query)->totalCount; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return \eZ\Bundle\EzPublishCoreBundle\URLChecker\URLCheckerInterface |
97
|
|
|
*/ |
98
|
|
|
private function getUrlHandler() |
99
|
|
|
{ |
100
|
|
|
return $this->getContainer()->get('ezpublish.url_checker'); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.