1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Comrade42\PhpBBParser\Command; |
4
|
|
|
|
5
|
|
|
use Goutte\Client; |
6
|
|
|
use Symfony\Component\BrowserKit\Cookie; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ParseForumsCommand |
13
|
|
|
* @package Comrade42\PhpBBParser\Command |
14
|
|
|
*/ |
15
|
|
|
class ParseForumsCommand extends ContainerAwareCommand |
16
|
|
|
{ |
17
|
|
|
protected function configure() |
18
|
|
|
{ |
19
|
|
|
$this->setName('parse:forums')->setDescription('Parse forums'); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
23
|
|
|
{ |
24
|
|
|
/** @var \Doctrine\ORM\EntityManager $entityManager */ |
25
|
|
|
$entityManager = $this->container->get('doctrine'); |
26
|
|
|
/** @var \Comrade42\PhpBBParser\Bridge\BridgeInterface $entityBridge */ |
27
|
|
|
$entityBridge = $this->container->get('bridge'); |
28
|
|
|
/** @var \Symfony\Component\Console\Helper\DialogHelper $dialogHelper */ |
29
|
|
|
$dialogHelper = $this->getHelperSet()->get('dialog'); |
30
|
|
|
|
31
|
|
|
$client = new Client(); |
32
|
|
|
$client->getCookieJar()->set( |
33
|
|
|
new Cookie( |
34
|
|
|
$this->container->getParameter('fa_sid_cookie_name'), |
35
|
|
|
$this->container->getParameter('fa_sid_cookie_value') |
36
|
|
|
) |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
$url = rtrim($this->container->getParameter('forum_url'), '/') . '/forum'; |
40
|
|
|
$output->write("<info>⇒ HTTP GET: {$url}</info>\t"); |
41
|
|
|
|
42
|
|
|
$crawler = $client->request('GET', $url); |
43
|
|
|
|
44
|
|
|
$status = $client->getInternalResponse()->getStatus(); |
45
|
|
|
$output->writeln("<info>[{$status}]</info>"); |
46
|
|
|
|
47
|
|
|
if ($status != 200) return; |
48
|
|
|
|
49
|
|
|
$crawler->filter('#main-content div.forabg')->each( |
50
|
|
|
function (Crawler $node, $index) use ($output, $entityManager, $entityBridge, $dialogHelper) |
51
|
|
|
{ |
52
|
|
|
$categoryId = $index + 1; |
53
|
|
|
$parsedName = $node->filter('ul.topiclist li.header dd.dterm h2')->text(); |
54
|
|
|
|
55
|
|
|
$entity = $entityBridge->getCategoryEntity($entityManager, $categoryId); |
56
|
|
|
$categoryTitle = $entity->getTitle(); |
57
|
|
|
|
58
|
|
|
if (!empty($categoryTitle) && $categoryTitle != $parsedName && !$dialogHelper->askConfirmation( |
59
|
|
|
$output, |
60
|
|
|
"<question>Category name doesn't match for #{$categoryId} ({$categoryTitle} → {$parsedName}). Update category? [Y/n]:</question> " |
61
|
|
|
) |
62
|
|
|
) { |
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$entity->fill($parsedName, $index); |
67
|
|
|
$entityManager->persist($entity); |
68
|
|
|
|
69
|
|
|
$node->filter('ul.forums li.row dd.dterm')->each( |
70
|
|
|
function (Crawler $node, $index) use ($output, $entityManager, $entityBridge, $dialogHelper, $categoryId) |
71
|
|
|
{ |
72
|
|
|
$link = $node->filter('a.forumtitle'); |
73
|
|
|
$forumId = substr($link->attr('href'), 2, -6); |
74
|
|
|
$parsedTitle = $link->text(); |
75
|
|
|
$description = trim(substr($node->text(), strlen($parsedTitle))); |
76
|
|
|
|
77
|
|
|
$entity = $entityBridge->getForumEntity($entityManager, $forumId); |
78
|
|
|
$forumTitle = $entity->getTitle(); |
79
|
|
|
|
80
|
|
|
if (!empty($forumTitle) && $forumTitle != $parsedTitle && !$dialogHelper->askConfirmation( |
81
|
|
|
$output, |
82
|
|
|
"<question>Forum name doesn't match for #{$forumId} ({$forumTitle} → {$parsedTitle}). Update forum? [Y/n]:</question> " |
83
|
|
|
) |
84
|
|
|
) { |
85
|
|
|
return; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$entity->fill($categoryId, $parsedTitle, $description, $index); |
89
|
|
|
$entityManager->persist($entity); |
90
|
|
|
} |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
$entityManager->flush(); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|