1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Comrade42\PhpBBParser\Command; |
4
|
|
|
|
5
|
|
|
use Goutte\Client; |
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
8
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class ParseUsersCommand |
12
|
|
|
* @package Comrade42\PhpBBParser\Command |
13
|
|
|
*/ |
14
|
|
|
class ParseUsersCommand extends ContainerAwareCommand |
15
|
|
|
{ |
16
|
|
|
const USERS_PER_PAGE = 50; |
17
|
|
|
|
18
|
|
|
protected function configure() |
19
|
|
|
{ |
20
|
|
|
$this->setName('parse:users')->setDescription('Parse users'); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
24
|
|
|
{ |
25
|
|
|
/** @var \Doctrine\ORM\EntityManager $entityManager */ |
26
|
|
|
$entityManager = $this->container->get('doctrine'); |
27
|
|
|
/** @var \Comrade42\PhpBBParser\Bridge\BridgeInterface $entityBridge */ |
28
|
|
|
$entityBridge = $this->container->get('bridge'); |
29
|
|
|
/** @var \Symfony\Component\Console\Helper\DialogHelper $dialogHelper */ |
30
|
|
|
$dialogHelper = $this->getHelperSet()->get('dialog'); |
31
|
|
|
|
32
|
|
|
$client = new Client(); |
33
|
|
|
$baseUrl = rtrim($this->container->getParameter('forum_url'), '/'); |
34
|
|
|
|
35
|
|
|
for ($offset = 0; ; $offset += static::USERS_PER_PAGE) |
36
|
|
|
{ |
37
|
|
|
$url = $baseUrl . '/memberlist?mode=joined&start=' . $offset; |
38
|
|
|
$output->write(str_pad("<info>⇒ HTTP GET: {$url}</info> ", 90)); |
39
|
|
|
|
40
|
|
|
$crawler = $client->request('GET', $url); |
41
|
|
|
|
42
|
|
|
$status = $client->getInternalResponse()->getStatus(); |
43
|
|
|
$output->writeln("<info>[{$status}]</info>"); |
44
|
|
|
|
45
|
|
|
if ($status != 200) |
46
|
|
|
{ |
47
|
|
|
$offset -= static::USERS_PER_PAGE; |
48
|
|
|
sleep(1); |
49
|
|
|
continue; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$crawler = $crawler->filter('#memberlist tbody tr'); |
53
|
|
|
$crawler->each(function (Crawler $node) use ($output, $entityManager, $entityBridge, $dialogHelper) { |
54
|
|
|
$columns = $node->children(); |
55
|
|
|
$id = substr($columns->eq(1)->filter('a')->attr('href'), 2); |
56
|
|
|
$nickname = substr($columns->eq(1)->text(), 2); |
57
|
|
|
|
58
|
|
|
$entity = $entityBridge->getMemberEntity($entityManager, $id); |
59
|
|
|
$memberName = $entity->getNickname(); |
60
|
|
|
|
61
|
|
|
$question = "<question>Member name doesn't match for #{$id} ({$memberName} → {$nickname}). Update entity? [Y/n]:</question> "; |
62
|
|
|
if (!empty($memberName) && $memberName != $nickname && !$dialogHelper->askConfirmation($output, $question)) return; |
63
|
|
|
|
64
|
|
|
$regDate = \DateTime::createFromFormat('Y-m-d H:i:s', $columns->eq(3)->text() . ' 00:00:00'); |
65
|
|
|
$avatarUrl = $columns->eq(1)->filter('a img')->attr('src'); |
66
|
|
|
$uuid = uniqid(); |
67
|
|
|
|
68
|
|
|
$entity->setNickname($nickname) |
69
|
|
|
->setRealName($nickname) |
70
|
|
|
->setRegDate($regDate) |
|
|
|
|
71
|
|
|
->setPassword($uuid) |
72
|
|
|
->setEmailAddress($uuid . '@mailforspam.com') |
73
|
|
|
->setAvatarUrl($avatarUrl); |
74
|
|
|
|
75
|
|
|
$entityManager->persist($entity); |
76
|
|
|
}); |
77
|
|
|
|
78
|
|
|
$entityManager->flush(); |
79
|
|
|
|
80
|
|
|
if ($crawler->count() < static::USERS_PER_PAGE) break; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.