-
Notifications
You must be signed in to change notification settings - Fork 7
/
29.php
231 lines (200 loc) · 7.81 KB
/
29.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
use Symfony\Component\DomCrawler\Crawler;
require __DIR__ . '/vendor/autoload.php';
#####################################################
function flat_map(callable $func, array $array) {
return reduce('array_merge',
array_map($func, $array), []);
}
function parallel_map(callable $func, array $items) {
$childPids = [];
$result = [];
foreach ($items as $i => $item) {
$newPid = pcntl_fork();
if ($newPid == -1) {
die('Can\'t fork process');
} elseif ($newPid) {
$childPids[] = $newPid;
if ($i == count($items) - 1) {
foreach ($childPids as $childPid) {
pcntl_waitpid($childPid, $status);
$sharedId = shmop_open($childPid, 'a', 0, 0);
$shareData = shmop_read($sharedId, 0, shmop_size($sharedId));
$result[] = unserialize($shareData);
shmop_delete($sharedId);
shmop_close($sharedId);
}
}
} else {
$myPid = getmypid();
echo 'Start ' . $myPid . PHP_EOL;
$funcResult = $func($item);
$shareData = serialize($funcResult);
$sharedId = shmop_open($myPid, 'c', 0644, strlen($shareData));
shmop_write($sharedId, $shareData, 0);
echo 'Done ' . $myPid . ' ' . formatUsage(memory_get_peak_usage()) . PHP_EOL;
exit(0);
}
}
return $result;
}
function parallel_flat_map(callable $func, array $array) {
return reduce('array_merge',
parallel_map($func, $array), []);
}
function first($array) {
return reset($array);
}
function reduce(callable $func, array $array, $initial = null) {
return array_reduce($array, $func, $initial);
}
function clearUrl($url) {
return preg_replace('#\&sid=.{32}#s', '', $url);
}
function formatUsage($memory) {
return number_format($memory / 1024 / 1024, 2, '.', ' ') . ' Mb';
}
function fileCache(callable $func, $path) {
return function() use ($func, $path) {
$args = func_get_args();
$file = $path . '/' . md5(serialize($args));
if (file_exists($file)) {
return unserialize(file_get_contents($file));
} else {
$value = call_user_func_array($func, $args);
file_put_contents($file, serialize($value));
return $value;
}
};
}
#####################################################
function createNormalizeUrl($baseUrl) {
return function ($url) use ($baseUrl) {
return $baseUrl . ltrim($url, './');
};
}
function getHtml($url) {
return file_get_contents($url);
}
function createCrawler(callable $getContent, callable $normalizeUrl) {
return function ($url) use ($getContent, $normalizeUrl) {
return new Crawler($getContent($normalizeUrl($url)));
};
}
function createGetForumMaxPageNumber(callable $crawler) {
return function ($url) use ($crawler) {
return max(
first($crawler($url)
->filter('div.action-bar.bar-top .pagination li:nth-last-of-type(2)')
->each(function (Crawler $link) {
return intval($link->text());
})),
1
);
};
}
function createGetForumPages(callable $getMaxPageNumber, $perPage) {
return function ($forumUrl) use ($getMaxPageNumber, $perPage) {
echo 'Forum pages for ' . clearUrl($forumUrl) . PHP_EOL;
return array_map(function ($number) use ($forumUrl, $perPage) {
return $forumUrl . ($number > 1 ? '&start=' . ($perPage * ($number - 1)) : '');
}, range(1, $getMaxPageNumber($forumUrl)));
};
}
function createGetForumPageTopics(callable $crawler) {
return function ($forumPageUrl) use ($crawler) {
echo 'Forum page topics for ' . clearUrl($forumPageUrl) . PHP_EOL;
return $crawler($forumPageUrl)
->filter('ul.topiclist.topics li dl')
->each(function (Crawler $topic) {
$link = $topic->filter('div.list-inner a.topictitle');
return [
'title' => $link->html(),
'url' => $link->attr('href'),
'count' => intval($topic->filter('dd.posts')->text()) + 1,
];
});
};
}
function createGetTopicPages($perPage) {
return function ($topic) use ($perPage) {
return array_map(function ($number) use ($topic, $perPage) {
return $topic['url'] . ($number > 1 ? '&start=' . ($perPage * ($number - 1)) : '');
}, range(1, intval(($topic['count'] - 1) / $perPage) + 1));
};
}
function createGetTopicPageProfiles(callable $crawler) {
return function ($topicPageUrl) use ($crawler) {
echo 'Topic profiles for ' . clearUrl($topicPageUrl) . PHP_EOL;
return $crawler($topicPageUrl)
->filter('dl.postprofile')
->each(function (Crawler $profile) {
return [
'username' => $profile->filter('dt a.username, dt a.username-coloured')->text(),
'total' => $profile->filter('dd.profile-posts a')->text(),
];
});
};
}
function createBatchGetTopicPagesProfiles(callable $getTopicPageProfiles) {
return function (array $urls) use ($getTopicPageProfiles) {
return reduce('array_merge',
parallel_map($getTopicPageProfiles,
$urls), []);
};
}
function squashProfiles(array $total, array $current) {
$existsFilter = function ($item) use ($current) {
return $item['username'] === $current['username'];
};
$notExistsFilter = function ($item) use ($current) {
return $item['username'] !== $current['username'];
};
$increase = function ($exists) {
return [
'username' => $exists['username'],
'count' => $exists['count'] + 1,
'total' => $exists['total'],
];
};
$create = function ($current) {
return [
'username' => $current['username'],
'count' => 1,
'total' => $current['total'],
];
};
if ($exists = first(array_filter($total, $existsFilter))) {
return array_merge(array_filter($total, $notExistsFilter), [$increase($exists)]);
} else {
return array_merge($total, [$create($current)]);
}
}
function createParseForumProfiles($squashProfiles, $batchGetTopicPagesProfiles, $getTopicPages, $getForumPageTopics, $getForumPages, $chunkSize) {
return function ($url) use ($squashProfiles, $batchGetTopicPagesProfiles, $getTopicPages, $getForumPageTopics, $getForumPages, $chunkSize) {
return
reduce($squashProfiles,
flat_map($batchGetTopicPagesProfiles,
array_chunk(
flat_map($getTopicPages,
parallel_flat_map($getForumPageTopics,
$getForumPages($url))), $chunkSize)), []);
};
}
#####################################################
$crawler = createCrawler(
fileCache('getHtml', __DIR__ . '/cache'),
createNormalizeUrl('http://yiiframework.ru/forum/')
);
$getForumMaxPageNumber = createGetForumMaxPageNumber($crawler);
$getForumPages = createGetForumPages($getForumMaxPageNumber, 25);
$getForumPageTopics = createGetForumPageTopics($crawler);
$getTopicPages = createGetTopicPages(20);
$getTopicPageProfiles = createGetTopicPageProfiles($crawler);
$batchGetTopicPagesProfiles = createBatchGetTopicPagesProfiles($getTopicPageProfiles);
$parse = createParseForumProfiles('squashProfiles', $batchGetTopicPagesProfiles, $getTopicPages, $getForumPageTopics, $getForumPages, 10);
#####################################################
$profiles = $parse('./viewforum.php?f=34');
echo 'Done ' . formatUsage(memory_get_peak_usage()) . PHP_EOL;
echo clearUrl(print_r($profiles, true));
echo PHP_EOL;