-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path23.php
161 lines (136 loc) · 5.21 KB
/
23.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
<?php
use Symfony\Component\DomCrawler\Crawler;
require __DIR__ . '/vendor/autoload.php';
#####################################################
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 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));
};
}
#####################################################
$getContent = fileCache('getHtml', __DIR__ . '/cache');
$normalizeUrl = createNormalizeUrl('http://yiiframework.ru/forum/');
$crawler = createCrawler($getContent, $normalizeUrl);
$getForumMaxPageNumber = createGetForumMaxPageNumber($crawler);
$getForumPages = createGetForumPages($getForumMaxPageNumber, 25);
$getForumPageTopics = createGetForumPageTopics($crawler);
$getTopicPages = createGetTopicPages(20);
#####################################################
$forumUrl = './viewforum.php?f=28';
$topicPages =
reduce('array_merge',
array_map($getTopicPages,
reduce('array_merge',
parallel_map($getForumPageTopics,
$getForumPages($forumUrl)), [])), []);
echo 'Done ' . formatUsage(memory_get_peak_usage()) . PHP_EOL;
echo clearUrl(print_r($topicPages, true));
echo PHP_EOL;