-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawler.php
182 lines (148 loc) · 7.01 KB
/
crawler.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
<?php
$_CONFIG['domain'] = ''; // ie: www.youdomain.com
$_CONFIG['host'] = 'localhost'; // database server
$_CONFIG['username'] = ''; // database user
$_CONFIG['password'] = ''; // database password
$_CONFIG['database'] = ''; // database Magento uses
$_CONFIG['concurrency'] = 8; // default is 8
$_CONFIG['store_id'] = '1'; // Magento store id for the store to be crawled
/*** YOU DO NOT NEED TO EDIT BELOW THIS LINE ***/
if(isset($_GET['start'])) {
$crawler = new crawler();
$crawler->connect($_CONFIG['host'], $_CONFIG['username'], $_CONFIG['password'], $_CONFIG['database']);
$crawler->start($_CONFIG['concurrency'], 'http://' . $_CONFIG['domain'] . '/', $_CONFIG['store_id']);
} else {
echo 'For best results, you should run System > Cache Management > Catalog Rewrites Refresh first<br />';
echo '<a href="?start=1">Click here</a> to start crawling';
}
class Crawler {
private $urls = array();
private $numOfUrls = 0;
private $numOfChunks = 0;
private $totalTime = 0;
private $chunkCount = 0;
public function __construct() {
set_time_limit(0);
ob_start();
$this->flushOutput();
}
public function connect($host, $user, $pass, $database) {
mysql_connect($host, $user, $pass) or exit('Could not connect to the database');
mysql_select_db($database) or exit('Could not connect to the database');
}
public function start($concurrency, $baseUrl, $storeId) {
// Crawl CMS pages
$this->startCmsCrawl($concurrency, $baseUrl, $storeId);
// Crawl categories
$this->startCategoryCrawl($concurrency, $baseUrl, $storeId);
// Crawl products
$this->startProductCrawl($concurrency, $baseUrl, $storeId);
}
public function startCmsCrawl($concurrency, $baseUrl, $storeId) {
$this->urls = array();
//cms urls
$result = mysql_query("SELECT p.identifier as url from cms_page p, cms_page_store s where p.page_id=s.page_id and s.store_id=" . $storeId);
while($row = mysql_fetch_assoc($result)) $this->urls[] = $row['url'];
$chunks = array_chunk($this->urls, $concurrency, true);
$this->numOfUrls = count($this->urls);
$this->numOfChunks = count($chunks);
$this->totalTime = 0;
$this->chunkCount = 0;
echo '<pre>';
echo "CRAWLING CMS: \n";
echo "--------------------------------------------------\n";
foreach($chunks as $chunk) {
$this->getChunk($chunk, $baseUrl);
}
echo "\n--------------------------------------------------\n";
echo 'TOTAL URLS: ' . $this->numOfUrls . "\n";
echo 'TOTAL SETS: ' . $this->numOfChunks . "\n";
echo 'TOTAL TIME: ' . round($this->totalTime, 3) . " seconds\n";
echo 'TIME / SET: ' . round($this->totalTime/$this->numOfChunks, 3) . " seconds\n";
echo 'TIME / URL: ' . round($this->totalTime/$this->numOfUrls, 3) . " seconds\n";
echo '</pre>';
$this->flushOutput();
}
public function startCategoryCrawl($concurrency, $baseUrl, $storeId) {
$this->urls = array();
//category urls
$result = mysql_query("SELECT request_path AS url FROM core_url_rewrite WHERE category_id IS NOT NULL AND product_id IS NULL and store_id=" . $storeId . " GROUP BY category_id ORDER BY request_path");
while($row = mysql_fetch_assoc($result)) $this->urls[] = $row['url'];
$chunks = array_chunk($this->urls, $concurrency, true);
$this->numOfUrls = count($this->urls);
$this->numOfChunks = count($chunks);
$this->totalTime = 0;
$this->chunkCount = 0;
echo '<pre>';
echo "CRAWLING CATEGORIES: \n";
echo "--------------------------------------------------\n";
foreach($chunks as $chunk) {
$this->getChunk($chunk, $baseUrl);
}
echo "\n--------------------------------------------------\n";
echo 'TOTAL URLS: ' . $this->numOfUrls . "\n";
echo 'TOTAL SETS: ' . $this->numOfChunks . "\n";
echo 'TOTAL TIME: ' . round($this->totalTime, 3) . " seconds\n";
echo 'TIME / SET: ' . round($this->totalTime/$this->numOfChunks, 3) . " seconds\n";
echo 'TIME / URL: ' . round($this->totalTime/$this->numOfUrls, 3) . " seconds\n";
echo '</pre>';
$this->flushOutput();
}
public function startProductCrawl($concurrency, $baseUrl, $storeId) {
$this->urls = array();
//product urls
$result = mysql_query("SELECT request_path AS url FROM core_url_rewrite WHERE product_id IS NOT NULL and store_id=" . $storeId . " GROUP BY product_id ORDER BY category_id, request_path");
while($row = mysql_fetch_assoc($result)) $this->urls[] = $row['url'];
$chunks = array_chunk($this->urls, $concurrency, true);
$this->numOfUrls = count($this->urls);
$this->numOfChunks = count($chunks);
$this->totalTime = 0;
$this->chunkCount = 0;
echo '<pre>';
echo "CRAWLING PRODUCTS: \n";
echo "--------------------------------------------------\n";
foreach($chunks as $chunk) {
$this->getChunk($chunk, $baseUrl);
}
echo "\n--------------------------------------------------\n";
echo 'TOTAL URLS: ' . $this->numOfUrls . "\n";
echo 'TOTAL SETS: ' . $this->numOfChunks . "\n";
echo 'TOTAL TIME: ' . round($this->totalTime, 3) . " seconds\n";
echo 'TIME / SET: ' . round($this->totalTime/$this->numOfChunks, 3) . " seconds\n";
echo 'TIME / URL: ' . round($this->totalTime/$this->numOfUrls, 3) . " seconds\n";
echo '</pre>';
$this->flushOutput();
}
private function getChunk($chunk, $baseUrl) {
$mh = curl_multi_init();
$time = microtime(true);
$this->chunkCount++;
echo "\n";
foreach($chunk as $x=>$URL) {
$url = $baseUrl . $URL;
echo $url . "\n";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_multi_add_handle($mh, $ch);
}
$running = 0;
do { curl_multi_exec($mh, $running); } while ($running > 0);
curl_multi_close($mh);
unset($mh);
$time = microtime(true) - $time;
$this->totalTime += $time;
echo 'SET ' . $this->chunkCount . ' OF ' . $this->numOfChunks . ' FINISHED: ' . round($time, 3) . ' seconds';
$this->flushOutput();
}
private function flushOutput() {
echo "\n<script type='text/javascript'> setTimeout('document.body.scrollTop = document.body.scrollHeight;', 100); </script>";
ob_end_flush();
ob_flush();
flush();
ob_start();
}
}