|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Service; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entity\Problem; |
|
6
|
|
|
use App\Repository\ImageRepository; |
|
7
|
|
|
use App\Repository\ProblemRepository; |
|
8
|
|
|
use App\Repository\WanderRepository; |
|
9
|
|
|
use Carbon\Carbon; |
|
10
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
11
|
|
|
use GuzzleHttp\Handler\Proxy; |
|
12
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
13
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
14
|
|
|
|
|
15
|
|
|
class ProblemService |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var ProblemRepository */ |
|
18
|
|
|
private $problemRepository; |
|
19
|
|
|
|
|
20
|
|
|
/** @var WanderRepository */ |
|
21
|
|
|
private $wanderRepository; |
|
22
|
|
|
|
|
23
|
|
|
/** @var ImageRepository */ |
|
24
|
|
|
private $imageRepository; |
|
25
|
|
|
|
|
26
|
|
|
/** @var RouterInterface */ |
|
27
|
|
|
private $router; |
|
28
|
|
|
|
|
29
|
|
|
/** @var MarkdownService */ |
|
30
|
|
|
private $markdownService; |
|
31
|
|
|
|
|
32
|
|
|
/** @var EntityManagerInterface */ |
|
33
|
|
|
private $entityManager; |
|
34
|
|
|
|
|
35
|
|
|
/** @var SpellingService */ |
|
36
|
|
|
private $spellingService; |
|
37
|
|
|
|
|
38
|
|
|
public function __construct( |
|
39
|
|
|
problemRepository $problemRepository, |
|
40
|
|
|
WanderRepository $wanderRepository, |
|
41
|
|
|
ImageRepository $imageRepository, |
|
42
|
|
|
RouterInterface $router, |
|
43
|
|
|
MarkdownService $markdownService, |
|
44
|
|
|
EntityManagerInterface $entityManager, |
|
45
|
|
|
SpellingService $spellingService |
|
46
|
|
|
) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->problemRepository = $problemRepository; |
|
49
|
|
|
$this->wanderRepository = $wanderRepository; |
|
50
|
|
|
$this->imageRepository = $imageRepository; |
|
51
|
|
|
$this->router = $router; |
|
52
|
|
|
$this->markdownService = $markdownService; |
|
53
|
|
|
$this->entityManager = $entityManager; |
|
54
|
|
|
$this->spellingService = $spellingService; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function createProblemReport(): void |
|
58
|
|
|
{ |
|
59
|
|
|
// I don't care about concurrency; there's only me using this and |
|
60
|
|
|
// I can always run it again if there's a problem; it's an unimportant |
|
61
|
|
|
// operation. |
|
62
|
|
|
$this->problemRepository->clearAllProblems(); |
|
63
|
|
|
|
|
64
|
|
|
// Build a hash of all possible valid URLs for wanders and |
|
65
|
|
|
// images to compare what we find in descriptions, etc. with. |
|
66
|
|
|
$validUris = []; |
|
67
|
|
|
$wanders = $this->wanderRepository->findAll(); |
|
68
|
|
|
foreach ($wanders as $wander) { |
|
69
|
|
|
$uri = $this->router->generate('wanders_show', [ 'id' => $wander->getId()], UrlGeneratorInterface::ABSOLUTE_URL); |
|
70
|
|
|
$validUris[$uri] = true; // Really I just want a hash that doesn't actually map |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$images = $this->imageRepository->findAll(); |
|
74
|
|
|
foreach ($images as $image) { |
|
75
|
|
|
$uri = $this->router->generate('image_show', [ 'id' => $image->getId()], UrlGeneratorInterface::ABSOLUTE_URL); |
|
76
|
|
|
$validUris[$uri] = true; // Really I just want a hash that doesn't actually map |
|
77
|
|
|
} |
|
78
|
|
|
$homepage = $this->router->generate('home', [], RouterInterface::ABSOLUTE_URL); |
|
79
|
|
|
|
|
80
|
|
|
// Okay, now we've got a list of all valid URIs, let's have a look through all our descriptions |
|
81
|
|
|
|
|
82
|
|
|
// Wanders first... |
|
83
|
|
|
foreach($wanders as $wander) { |
|
84
|
|
|
$description = $wander->getDescription(); |
|
85
|
|
|
|
|
86
|
|
|
// Broken links |
|
87
|
|
|
$links = $this->markdownService->findLinks($description); |
|
88
|
|
|
foreach ($links as $link) { |
|
89
|
|
|
if (substr($link['uri'], 0, strlen($homepage)) == $homepage) { |
|
90
|
|
|
if (!array_key_exists($link['uri'], $validUris)) { |
|
91
|
|
|
$problem = new Problem(); |
|
92
|
|
|
$problem->setDescription('Wander ' . $wander->getId() . ' links to invalid URI: ' . $link['uri'] . ' (text is: "' . $link['text'] . '")'); |
|
93
|
|
|
$problem->setUri($this->router->generate('wanders_show', [ 'id' => $wander->getId()], UrlGeneratorInterface::ABSOLUTE_URL)); |
|
94
|
|
|
$this->entityManager->persist($problem); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// Spell check recent wanders |
|
100
|
|
|
$date = $wander->getStartTime(); |
|
101
|
|
|
if ($date !== null && Carbon::now()->subWeeks(2)->lessThan($date)) { |
|
102
|
|
|
$misspelledWords = $this->spellingService->checkString($this->markdownService->markdownToText($description)); |
|
103
|
|
|
if (count($misspelledWords) > 0) { |
|
104
|
|
|
$problem = new Problem(); |
|
105
|
|
|
$problem->setDescription('Wander ' . $wander->getId() . ' has potential spelling mistakes: ' . implode(", ", $misspelledWords)); |
|
106
|
|
|
$problem->setUri($this->router->generate('wanders_show', [ 'id' => $wander->getId()], UrlGeneratorInterface::ABSOLUTE_URL)); |
|
107
|
|
|
$this->entityManager->persist($problem); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
$this->entityManager->flush(); |
|
112
|
|
|
// ...then Images: |
|
113
|
|
|
foreach($images as $image) { |
|
114
|
|
|
$description = $image->getDescription(); |
|
115
|
|
|
|
|
116
|
|
|
// Broken links |
|
117
|
|
|
$links = $this->markdownService->findLinks($image->getDescription()); |
|
118
|
|
|
foreach ($links as $link) { |
|
119
|
|
|
if (substr($link['uri'], 0, strlen($homepage)) == $homepage) { |
|
120
|
|
|
if (!array_key_exists($link['uri'], $validUris)) { |
|
121
|
|
|
$problem = new Problem(); |
|
122
|
|
|
$problem->setDescription('Image ' . $image->getId() . ' links to invalid URI: ' . $link['uri'] . ' (text is: "' . $link['text'] . '")'); |
|
123
|
|
|
$problem->setUri($this->router->generate('image_show', [ 'id' => $image->getId()], UrlGeneratorInterface::ABSOLUTE_URL)); |
|
124
|
|
|
$this->entityManager->persist($problem); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
// Spell check |
|
130
|
|
|
$date = $image->getCapturedAt(); |
|
131
|
|
|
if ($date !== null && Carbon::now()->subWeeks(2)->lessThan($date)) { |
|
132
|
|
|
$misspelledWords = $this->spellingService->checkString($this->markdownService->markdownToText($description)); |
|
133
|
|
|
if (count($misspelledWords) > 0) { |
|
134
|
|
|
$problem = new Problem(); |
|
135
|
|
|
$problem->setDescription('Image ' . $image->getId() . ' has potential spelling mistakes: ' . implode(", ", $misspelledWords)); |
|
136
|
|
|
$problem->setUri($this->router->generate('image_show', [ 'id' => $image->getId()], UrlGeneratorInterface::ABSOLUTE_URL)); |
|
137
|
|
|
$this->entityManager->persist($problem); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
$this->entityManager->flush(); |
|
142
|
|
|
} |
|
143
|
|
|
} |