forked from TYPO3-extensions/crawler
/
class.tx_crawler_api.php
591 lines (505 loc) · 16 KB
/
class.tx_crawler_api.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
<?php
/***************************************************************
* Copyright notice
*
* (c) 2009 AOE media (dev@aoemedia.de)
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* This api class can be used to add pages to the crawlerqueue.
* It uses internally the class tx_crawler_lib modify the queue.
*/
require_once t3lib_extMgm::extPath('crawler').'system/class.tx_crawler_system_validator.php';
require_once t3lib_extMgm::extPath('crawler').'domain/queue/class.tx_crawler_domain_queue_repository.php';
require_once t3lib_extMgm::extPath('crawler').'domain/process/class.tx_crawler_domain_process_repository.php';
class tx_crawler_api {
/**
* @var tx_crawler_lib
*/
private $crawlerObj;
/**
* @var tx_crawler_system_validator validator
*/
private $validator;
/**
* @var tx_crawler_domain_queue_repository queue repository
*/
protected $queueRepository;
/**
* @var $allowedConfigrations array
*/
protected $allowedConfigrations = array();
/**
* Returns an instance of the validator
*
* @return tx_crawler_system_validator
*/
protected function findValidator() {
if(!$this->validator instanceof tx_crawler_system_validator) {
$this->validator = t3lib_div::makeInstance('tx_crawler_system_validator');
}
return $this->validator;
}
/**
* Each crawlerrun has a setid, this facade method delegates
* the it to the crawler object
*
* @param int
*/
public function overwriteSetId($id) {
$this->findCrawler()->setID = intval($id);
}
/**
* This method is used to limit the configuration selection to
* a set of configurations.
*
* @param array $allowedConfigurations
*/
public function setAllowedConfigurations(array $allowedConfigurations){
$this->allowedConfigrations = $allowedConfigurations;
}
/**
* Returns the setID of the crawler
*
* @return int
*/
public function getSetId() {
return $this->findCrawler()->setID;
}
/**
* Method to get an instance of the internal crawler singleton
*
* @return tx_crawler_lib Instance of the crawler lib
*/
protected function findCrawler() {
if(!is_object($this->crawlerObj)) {
$this->crawlerObj = t3lib_div::makeInstance('tx_crawler_lib');
$this->crawlerObj->setID = t3lib_div::md5int(microtime());
}
if (is_object($this->crawlerObj)) {
return $this->crawlerObj;
} else {
throw new Exception("no crawler object");
}
}
/**
* Adds a page to the crawlerqueue by uid
*
* @param int uid
*/
public function addPageToQueue($uid) {
$uid = intval($uid);
//non timed elements will be added with timestamp 0
$this->addPageToQueueTimed($uid,0);
}
/**
* This method is used to limit the processing instructions to the processing instructions
* that are allowed.
*
* @return array
*/
protected function filterUnallowedConfigurations($configurations) {
if(count($this->allowedConfigrations) > 0){
// remove configuration that does not match the current selection
foreach ($configurations as $confKey => $confArray) {
if (!in_array($confKey, $this->allowedConfigrations)) {
unset($configurations[$confKey]);
}
}
}
return $configurations;
}
/**
* Adds a page to the crawlerqueue by uid and sets a
* timestamp when the page should be crawled.
*
* @param int pageid
* @param int timestamp
*/
public function addPageToQueueTimed($uid,$time) {
$uid = intval($uid);
$time = intval($time);
$crawler = $this->findCrawler();
$pageData = t3lib_div::makeInstance('t3lib_pageSelect')->getPage($uid);
$configurations = $crawler->getUrlsForPageRow($pageData);
$configurations = $this->filterUnallowedConfigurations($configurations);
$downloadUrls = array();
$duplicateTrack = array();
if (is_array($configurations)) {
foreach ($configurations as $cv) {
//enable inserting of entries
$crawler->registerQueueEntriesInternallyOnly=false;
$crawler->urlListFromUrlArray(
$cv,
$pageData,
$time,
300,
true,
false,
$duplicateTrack,
$downloadUrls,
array_keys($this->getCrawlerProcInstructions())
);
//reset the queue because the entries have been written to the db
unset($crawler->queueEntries);
}
} else {
//no configuration found
}
}
/**
* Counts all entrys in the database which are scheduled for a given page id and a schedule timestamp.
*
* @param int $page_uid
* @param int $timestamp
* @return int
*/
protected function countEntriesInQueueForPageByScheduletime($page_uid,$schedule_timestamp) {
//if the same page is scheduled for the same time and has not be executed?
if($schedule_timestamp == 0) {
//untimed elements need an exec_time with 0 because they can occure multiple times
$where = 'page_id='.$page_uid.' AND exec_time = 0 AND scheduled='.$schedule_timestamp;
} else {
//timed elementes have got a fixed schedule time, if a record with this time
//exists it is maybe queued for the future, or is has been queue for the past and therefore
//also been processed.
$where = 'page_id='.$page_uid.' AND scheduled='.$schedule_timestamp;
}
$row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($GLOBALS['TYPO3_DB']->exec_SELECTquery('count(*) as cnt','tx_crawler_queue',$where));
return intval($row['cnt']);
}
/**
* Determines if a page is queued.
*
* @param int uid
* @param boolean configure the method, to handle only unprocessed items as queued
*/
public function isPageInQueue($uid,$unprocessed_only = true,$timed_only=false,$timestamp=false) {
$uid = intval($uid);
if($timestamp != false) { $timestamp = intval($timestamp);}
if(!$this->findValidator()->isInt($uid)) { die('wrong parameter type'); }
$query = 'count(*) as anz';
$where = 'page_id = '.$uid;
if($unprocessed_only) {
$where .= ' AND exec_time = 0';
}
if($timed_only) {
$where .= ' AND scheduled != 0';
}
if($timestamp) {
$where .= ' AND scheduled = '.$timestamp;
}
$rs = $GLOBALS['TYPO3_DB']->exec_SELECTquery($query,'tx_crawler_queue',$where);
$row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($rs);
$entriesInDB = intval($row['anz']);
$is_queued = $entriesInDB > 0;
return $is_queued;
}
/**
* Method to return the latest Crawle Timestamp for a page.
*
* @param uid id of the page
*/
public function getLatestCrawlTimestampForPage($uid,$future_crawldates_only=false, $unprocessed_only = false) {
$uid = intval($uid);
$query = 'max(scheduled) as latest';
$where = ' page_id = '.$uid;
if($future_crawldates_only) {
$where .= ' AND scheduled > '.time();
}
if($unprocessed_only) {
$where .= ' AND exec_time = 0';
}
$rs = $GLOBALS['TYPO3_DB']->exec_SELECTquery($query,'tx_crawler_queue',$where);
if($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($rs)) {
$res = $row['latest'];
} else {
$res = 0;
}
return $res;
}
/**
* Returns an array with timestamps when the page has been scheduled for crawling and
* at what time the scheduled crawl has been executed. The array also contains items that are
* scheduled but have note been crawled yet.
*
* @param int $uid uid of the page
* @param bool $limit
* @return array array with the crawlhistory of a page => 0 : scheduled time , 1 : execuded_time, 2 : set_id
*/
public function getCrawlHistoryForPage($uid, $limit = FALSE) {
$uid = intval($uid);
$limit = $GLOBALS['TYPO3_DB']->fullQuoteStr($limit, 'tx_crawler_queue');
$query = 'scheduled, exec_time, set_id';
$where = ' page_id = ' . $uid;
$limit_query = ($limit) ? $limit : NULL;
$rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows($query, 'tx_crawler_queue', $where, NULL, NULL, $limit_query);
return $rows;
}
/**
* Method to determine unprocessed Items in the crawler queue.
*
* @return array
*/
public function getUnprocessedItems() {
$query = '*';
$where = 'exec_time = 0';
$rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows($query,'tx_crawler_queue',$where,'','page_id, scheduled');
return $rows;
}
/**
* Method to get the number of unprocessed items in the crawler
*
* @param int number of unprocessed items in the queue
*/
public function countUnprocessedItems() {
$query = 'count(page_id) as anz';
$where = 'exec_time = 0';
$rs = $GLOBALS['TYPO3_DB']->exec_SELECTquery($query,'tx_crawler_queue',$where);
$row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($rs);
return $row['anz'];
}
/**
* Method to check if a page is in the queue which is timed for a
* date when it should be crawled
*
* @param int uid of the page
* @param boolean only respect unprocessed pages
*/
public function isPageInQueueTimed($uid,$show_unprocessed = true) {
$uid = intval($uid);
return $this->isPageInQueue($uid,$show_unprocessed,show_timed_only);
}
/**
* Reads the registered processingInstructions of the crawler
*
* @return unknown
*/
private function getCrawlerProcInstructions() {
return $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['procInstructions'];
}
/**
* Removes an queue entry with a given queue id
*
* @param int $qid
*/
public function removeQueueEntrie($qid) {
$qid = intval($qid);
$table = 'tx_crawler_queue';
$where = ' qid='.$qid;
$GLOBALS['TYPO3_DB']->exec_DELETEquery($table,$where);
}
/**
* Get queue statistics
*
* @param void
* @return array array('assignedButUnprocessed' => <>, 'unprocessed' => <>);
* @author Fabrizio Branca <fabrizio.branca@aoemedia.de>
* @since 2009-09-02
*/
public function getQueueStatistics() {
return array(
'assignedButUnprocessed' => $this->getQueueRepository()->countAllAssignedPendingItems(),
'unprocessed' => $this->getQueueRepository()->countAllPendingItems()
);
}
/**
* Get queue repository
*
* @param void
* @return tx_crawler_domain_queue_repository queue repository
* @author Fabrizio Branca <fabrizio.branca@aoemedia.de>
* @since 2009-09-03
*/
protected function getQueueRepository() {
if (!$this->queueRepository instanceof tx_crawler_domain_queue_repository) {
$this->queueRepository = new tx_crawler_domain_queue_repository();
}
return $this->queueRepository;
}
/**
* Get queue statistics by configuration
*
* @param void
* @return array array of array('configuration' => <>, 'assignedButUnprocessed' => <>, 'unprocessed' => <>)
* @author Fabrizio Branca <fabrizio.branca@aoemedia.de>
* @since 2009-09-02
*/
public function getQueueStatisticsByConfiguration() {
$statistics = $this->getQueueRepository()->countPendingItemsGroupedByConfigurationKey();
$setIds = $this->getQueueRepository()->getSetIdWithUnprocessedEntries();
$totals = $this->getQueueRepository()->getTotalQueueEntriesByConfiguration($setIds);
// "merge" arrays
foreach ($statistics as $key => &$value) {
$value['total'] = $totals[$value['configuration']];
}
return $statistics;
}
/**
* Get active processes count
*
* @param void
* @return int
* @author Fabrizio Branca <fabrizio.branca@aoemedia.de>
* @since 2009-09-03
*/
public function getActiveProcessesCount() {
$processRepository = new tx_crawler_domain_process_repository();
return $processRepository->countActive();
}
/**
* Get last processed entries
*
* @param int limit
* @return array
*/
public function getLastProcessedQueueEntries($limit) {
return $this->getQueueRepository()->getLastProcessedEntries('*', $limit);
}
/**
* Get current crawling speed
*
* @param float|false page speed in pages per minute
* @author Fabrizio Branca <fabrizio.branca@aoemedia.de>
* @since 2009-09-03
*/
public function getCurrentCrawlingSpeed() {
$lastProcessedEntries = $this->getQueueRepository()->getLastProcessedEntriesTimestamps();
if (count($lastProcessedEntries) < 10) {
// not enough information
return false;
}
$tooOldDelta = 60; // time between two entries is "too old"
$compareValue = time();
$startTime = $lastProcessedEntries[0];
$pages = 0;
reset($lastProcessedEntries);
while (list($key, $timestamp) = each($lastProcessedEntries)) {
if ($compareValue - $timestamp > $tooOldDelta) break;
$compareValue = $timestamp;
$pages++;
}
if ($pages < 10) {
// not enough information
return false;
}
$oldestTimestampThatIsNotTooOld = $compareValue;
$time = $startTime - $oldestTimestampThatIsNotTooOld;
$speed = $pages / ($time / 60);
return $speed;
}
/**
* Get some performance data
*
* @param integer $start
* @param integer $end
* @param integer $resolution
* @return array data
* @author Fabrizio Branca <fabrizio.branca@aoemedia.de>
* @since 2009-09-08
*/
public function getPerformanceData($start, $end, $resolution) {
$data = array();
$data['urlcount'] = 0;
$data['start'] = $start;
$data['end'] = $end;
$data['duration'] = $data['end'] - $data['start'];
if ($data['duration'] < 1) {
throw new Exception('End timestamp must be after start timestamp');
}
for ($slotStart = $start; $slotStart < $end; $slotStart += $resolution) {
$slotEnd = min($slotStart+$resolution-1, $end);
$slotData = $this->getQueueRepository()->getPerformanceData($slotStart, $slotEnd);
$slotUrlCount = 0;
foreach($slotData as $processId => &$processData) {
$duration = $processData['end'] - $processData['start'];
if ($processData['urlcount'] > 5 && $duration > 0) {
$processData['speed'] = 60 * 1 / ($duration/$processData['urlcount']);
}
$slotUrlCount += $processData['urlcount'];
}
$data['urlcount'] += $slotUrlCount;
$data['slots'][$slotEnd] = array(
'amountProcesses' => count($slotData),
'urlcount' => $slotUrlCount,
'processes' => $slotData,
);
if ($slotUrlCount > 5) {
$data['slots'][$slotEnd]['speed'] = 60 * 1 / ($slotEnd-$slotStart / $slotUrlCount);
} else {
$data['slots'][$slotEnd]['speed'] = 0;
}
}
if ($data['urlcount'] > 5) {
$data['speed'] = 60 * 1 / ($data['duration'] / $data['urlcount']);
} else {
$data['speed'] = 0;
}
return $data;
}
/**
* Wrapper to support old and new method to test integer values.
*
* @param integer $value
* @return integer
*/
static public function convertToPositiveInteger($value) {
if (version_compare(TYPO3_version, '4.6.0', '>=')) {
$result = t3lib_utility_Math::convertToPositiveInteger($value);
} else {
$result = t3lib_div::intval_positive($value);
}
return $result;
}
/**
* Wrapper to support old an new method to test integer value.
*
* @param integer $value
* @param integer $min
* @param integer $max
* @param integer $default
* @return integer
*/
static public function forceIntegerInRange($value, $min, $max = 2000000, $default = 0) {
if (version_compare(TYPO3_version, '4.6.0', '>=')) {
$result = t3lib_utility_Math::forceIntegerInRange($value, $min, $max, $default);
} else {
$result = t3lib_div::intInRange($value, $min, $max, $default);
}
return $result;
}
/**
* Wrapper to support old an new method to test integer value.
*
* @param integer $value
* @return bool
*/
static public function canBeInterpretedAsInteger($value) {
if (version_compare(TYPO3_version, '4.6.0', '>=')) {
$result = t3lib_utility_Math::canBeInterpretedAsInteger($value);
} else {
$result = t3lib_div::testInt($value);
}
return $result;
}
}
if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/crawler/class.tx_crawler_api.php']) {
include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/crawler/class.tx_crawler_api.php']);
}
?>