forked from FiftyNine/scpper-crawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScpThreads.php
194 lines (173 loc) · 5.32 KB
/
ScpThreads.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
require_once "ScpUpdater.php";
class ScpThreadWorker extends Worker
{
protected $logger;
public function __construct(WikidotLogger $logger) {
$this->logger = $logger;
}
public function getLogger()
{
return $this->logger;
}
}
class ScpAbstractWork extends Threaded
{
protected $complete;
protected $success;
public function __construct()
{
$this->complete = false;
$this->success = false;
}
public function isComplete()
{
return $this->complete;
}
public function isSuccess()
{
return $this->success;
}
}
class ScpPageWork extends ScpAbstractWork
{
protected $page;
public function __construct(ScpPage $page)
{
parent::__construct();
$this->page = $page;
}
public function run()
{
$logger = $this->worker->getLogger();
$page = $this->page;
if (!$page->retrievePageInfo($logger)) {
$this->complete = true;
return;
}
$this->success =
$page->retrievePageVotes($logger)
&& $page->retrievePageHistory($logger)
&& $page->retrievePageSource($logger);
$this->page = $page;
$this->complete = true;
}
public function getPage()
{
return $this->page;
}
}
class ScpMemberListPageWork extends ScpAbstractWork
{
protected $siteName;
protected $pageIndex;
protected $pageHtml;
public function __construct($siteName, $pageIndex)
{
parent::__construct();
$this->siteName = $siteName;
$this->pageIndex = $pageIndex;
}
public function run()
{
$logger = $this->worker->getLogger();
$args = ['page' => $this->pageIndex];
$html = null;
try {
$status = WikidotUtils::requestModule($this->siteName, 'membership/MembersListModule', 0, $args, $html, $logger);
if ($status === WikidotStatus::OK) {
$this->pageHtml = $html;
$this->success = true;
}
} finally {
$this->complete = true;
}
}
public function getPageHtml()
{
return $this->pageHtml;
}
}
class ScpMultithreadUsersUpdater extends ScpUsersUpdater
{
// Retrieve all the users
protected function retrieveUsers()
{
$pool = new Pool(SCP_THREADS, ScpThreadWorker::class, [$this->logger]);
for ($i = 1; $i <= $this->pageCount; $i++) {
$pool->submit(new ScpMemberListPageWork($this->siteName, $i));
}
$left = $this->pageCount;
$failed = false;
while ($left > 0 && !$failed) {
$pool->collect(
function(ScpMemberListPageWork $task) use (&$left, &$failed)
{
if ($task->isComplete()) {
if ($task->isSuccess()) {
$users = new ScpUserList($this->siteName, $this->siteId);
$loaded = $users->addMembersFromListPage($task->getPageHtml(), $this->logger);
$users->saveToDb($this->link, $this->logger, false);
$users->saveMembershipToDBGreedy($this->link, $this->logger);
if (intdiv($this->total + $loaded, 1000) > intdiv($this->total, 1000)) {
WikidotLogger::logFormat(
$this->logger,
"%d members retrieved [%d kb used]...",
[intdiv($this->total + $loaded, 1000)*1000, round(memory_get_usage()/1024)]
);
}
unset($users);
$this->total += $loaded;
} else {
$failed = true;
}
$left--;
return true;
} else {
return false;
}
}
);
}
$this->failed = $this->failed || $failed;
}
}
class ScpMultithreadPagesUpdater extends ScpPagesUpdater
{
// Process all the pages
protected function processPages()
{
$pool = new Pool(SCP_THREADS, ScpThreadWorker::class, [$this->logger]);
// Iterate through all pages and process them one by one
for ($i = count($this->sitePages)-1; $i>=0; $i--) {
$page = $this->sitePages[$i];
$pool->submit(new ScpPageWork($page));
}
$left = count($this->sitePages);
while ($left > 0) {
$pool->collect(
function(ScpPageWork $task) use (&$left)
{
if ($task->isComplete()) {
$this->processPage($task->getPage(), $task->isSuccess());
$left--;
return true;
} else {
return false;
}
}
);
}
}
}
class ScpMultithreadedSiteUpdater extends ScpSiteUpdater
{
protected function getPagesUpdaterClass()
{
return 'ScpMultithreadPagesUpdater';
}
protected function getUsersUpdaterClass()
{
return 'ScpMultithreadUsersUpdater';
}
}