Complex classes like CrawlerController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CrawlerController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 55 | class CrawlerController |
||
| 56 | { |
||
| 57 | const CLI_STATUS_NOTHING_PROCCESSED = 0; |
||
| 58 | const CLI_STATUS_REMAIN = 1; //queue not empty |
||
| 59 | const CLI_STATUS_PROCESSED = 2; //(some) queue items where processed |
||
| 60 | const CLI_STATUS_ABORTED = 4; //instance didn't finish |
||
| 61 | const CLI_STATUS_POLLABLE_PROCESSED = 8; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var integer |
||
| 65 | */ |
||
| 66 | public $setID = 0; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | public $processID = ''; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * One hour is max stalled time for the CLI |
||
| 75 | * If the process had the status "start" for 3600 seconds, it will be regarded stalled and a new process is started |
||
| 76 | * |
||
| 77 | * @var integer |
||
| 78 | */ |
||
| 79 | public $max_CLI_exec_time = 3600; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | public $duplicateTrack = []; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var array |
||
| 88 | */ |
||
| 89 | public $downloadUrls = []; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var array |
||
| 93 | */ |
||
| 94 | public $incomingProcInstructions = []; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var array |
||
| 98 | */ |
||
| 99 | public $incomingConfigurationSelection = []; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var bool |
||
| 103 | */ |
||
| 104 | public $registerQueueEntriesInternallyOnly = false; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var array |
||
| 108 | */ |
||
| 109 | public $queueEntries = []; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var array |
||
| 113 | */ |
||
| 114 | public $urlList = []; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var boolean |
||
| 118 | */ |
||
| 119 | public $debugMode = false; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var array |
||
| 123 | */ |
||
| 124 | public $extensionSettings = []; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Mount Point |
||
| 128 | * |
||
| 129 | * @var boolean |
||
| 130 | */ |
||
| 131 | public $MP = false; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var string |
||
| 135 | */ |
||
| 136 | protected $processFilename; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Holds the internal access mode can be 'gui','cli' or 'cli_im' |
||
| 140 | * |
||
| 141 | * @var string |
||
| 142 | */ |
||
| 143 | protected $accessMode; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @var DatabaseConnection |
||
| 147 | */ |
||
| 148 | private $db; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @var BackendUserAuthentication |
||
| 152 | */ |
||
| 153 | private $backendUser; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @var integer |
||
| 157 | */ |
||
| 158 | private $scheduledTime = 0; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @var integer |
||
| 162 | */ |
||
| 163 | private $reqMinute = 0; |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @var bool |
||
| 167 | */ |
||
| 168 | private $submitCrawlUrls = false; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @var bool |
||
| 172 | */ |
||
| 173 | private $downloadCrawlUrls = false; |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Method to set the accessMode can be gui, cli or cli_im |
||
| 177 | * |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | 1 | public function getAccessMode() |
|
| 184 | |||
| 185 | /** |
||
| 186 | * @param string $accessMode |
||
| 187 | */ |
||
| 188 | 1 | public function setAccessMode($accessMode) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Set disabled status to prevent processes from being processed |
||
| 195 | * |
||
| 196 | * @param bool $disabled (optional, defaults to true) |
||
| 197 | * @return void |
||
| 198 | */ |
||
| 199 | 3 | public function setDisabled($disabled = true) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Get disable status |
||
| 212 | * |
||
| 213 | * @return bool true if disabled |
||
| 214 | */ |
||
| 215 | 3 | public function getDisabled() |
|
| 223 | |||
| 224 | /** |
||
| 225 | * @param string $filenameWithPath |
||
| 226 | * |
||
| 227 | * @return void |
||
| 228 | */ |
||
| 229 | 4 | public function setProcessFilename($filenameWithPath) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * @return string |
||
| 236 | */ |
||
| 237 | 1 | public function getProcessFilename() |
|
| 241 | |||
| 242 | /************************************ |
||
| 243 | * |
||
| 244 | * Getting URLs based on Page TSconfig |
||
| 245 | * |
||
| 246 | ************************************/ |
||
| 247 | |||
| 248 | 53 | public function __construct() |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Sets the extensions settings (unserialized pendant of $TYPO3_CONF_VARS['EXT']['extConf']['crawler']). |
||
| 270 | * |
||
| 271 | * @param array $extensionSettings |
||
| 272 | * @return void |
||
| 273 | */ |
||
| 274 | 61 | public function setExtensionSettings(array $extensionSettings) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Check if the given page should be crawled |
||
| 281 | * |
||
| 282 | * @param array $pageRow |
||
| 283 | * @return false|string false if the page should be crawled (not excluded), true / skipMessage if it should be skipped |
||
| 284 | */ |
||
| 285 | 10 | public function checkIfPageShouldBeSkipped(array $pageRow) |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Wrapper method for getUrlsForPageId() |
||
| 345 | * It returns an array of configurations and no urls! |
||
| 346 | * |
||
| 347 | * @param array $pageRow Page record with at least dok-type and uid columns. |
||
| 348 | * @param string $skipMessage |
||
| 349 | * @return array |
||
| 350 | * @see getUrlsForPageId() |
||
| 351 | */ |
||
| 352 | 6 | public function getUrlsForPageRow(array $pageRow, &$skipMessage = '') |
|
| 367 | |||
| 368 | /** |
||
| 369 | * This method is used to count if there are ANY unprocessed queue entries |
||
| 370 | * of a given page_id and the configuration which matches a given hash. |
||
| 371 | * If there if none, we can skip an inner detail check |
||
| 372 | * |
||
| 373 | * @param int $uid |
||
| 374 | * @param string $configurationHash |
||
| 375 | * @return boolean |
||
| 376 | */ |
||
| 377 | 7 | protected function noUnprocessedQueueEntriesForPageWithConfigurationHashExist($uid, $configurationHash) |
|
| 378 | { |
||
| 379 | 7 | $configurationHash = $this->db->fullQuoteStr($configurationHash, 'tx_crawler_queue'); |
|
| 380 | 7 | $res = $this->db->exec_SELECTquery('count(*) as anz', 'tx_crawler_queue', "page_id=" . intval($uid) . " AND configuration_hash=" . $configurationHash . " AND exec_time=0"); |
|
| 381 | 7 | $row = $this->db->sql_fetch_assoc($res); |
|
| 382 | |||
| 383 | 7 | return ($row['anz'] == 0); |
|
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Creates a list of URLs from input array (and submits them to queue if asked for) |
||
| 388 | * See Web > Info module script + "indexed_search"'s crawler hook-client using this! |
||
| 389 | * |
||
| 390 | * @param array Information about URLs from pageRow to crawl. |
||
| 391 | * @param array Page row |
||
| 392 | * @param integer Unix time to schedule indexing to, typically time() |
||
| 393 | * @param integer Number of requests per minute (creates the interleave between requests) |
||
| 394 | * @param boolean If set, submits the URLs to queue |
||
| 395 | * @param boolean If set (and submitcrawlUrls is false) will fill $downloadUrls with entries) |
||
| 396 | * @param array Array which is passed by reference and contains the an id per url to secure we will not crawl duplicates |
||
| 397 | * @param array Array which will be filled with URLS for download if flag is set. |
||
| 398 | * @param array Array of processing instructions |
||
| 399 | * @return string List of URLs (meant for display in backend module) |
||
| 400 | * |
||
| 401 | */ |
||
| 402 | 4 | public function urlListFromUrlArray( |
|
| 403 | array $vv, |
||
| 404 | array $pageRow, |
||
| 405 | $scheduledTime, |
||
| 406 | $reqMinute, |
||
| 407 | $submitCrawlUrls, |
||
| 408 | $downloadCrawlUrls, |
||
| 409 | array &$duplicateTrack, |
||
| 410 | array &$downloadUrls, |
||
| 411 | array $incomingProcInstructions |
||
| 412 | ) { |
||
| 413 | 4 | $urlList = ''; |
|
| 414 | // realurl support (thanks to Ingo Renner) |
||
| 415 | 4 | if (ExtensionManagementUtility::isLoaded('realurl') && $vv['subCfg']['realurl']) { |
|
| 416 | |||
| 417 | /** @var tx_realurl $urlObj */ |
||
| 418 | $urlObj = GeneralUtility::makeInstance('tx_realurl'); |
||
| 419 | |||
| 420 | if (!empty($vv['subCfg']['baseUrl'])) { |
||
| 421 | $urlParts = parse_url($vv['subCfg']['baseUrl']); |
||
| 422 | $host = strtolower($urlParts['host']); |
||
| 423 | $urlObj->host = $host; |
||
| 424 | |||
| 425 | // First pass, finding configuration OR pointer string: |
||
| 426 | $urlObj->extConf = isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['realurl'][$urlObj->host]) ? $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['realurl'][$urlObj->host] : $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['realurl']['_DEFAULT']; |
||
| 427 | |||
| 428 | // If it turned out to be a string pointer, then look up the real config: |
||
| 429 | if (is_string($urlObj->extConf)) { |
||
| 430 | $urlObj->extConf = is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['realurl'][$urlObj->extConf]) ? $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['realurl'][$urlObj->extConf] : $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['realurl']['_DEFAULT']; |
||
| 431 | } |
||
| 432 | } |
||
| 433 | |||
| 434 | if (!$GLOBALS['TSFE']->sys_page) { |
||
| 435 | $GLOBALS['TSFE']->sys_page = GeneralUtility::makeInstance('TYPO3\CMS\Frontend\Page\PageRepository'); |
||
| 436 | } |
||
| 437 | if (!$GLOBALS['TSFE']->csConvObj) { |
||
| 438 | $GLOBALS['TSFE']->csConvObj = GeneralUtility::makeInstance('TYPO3\CMS\Core\Charset\CharsetConverter'); |
||
| 439 | } |
||
| 440 | if (!$GLOBALS['TSFE']->tmpl->rootLine[0]['uid']) { |
||
| 441 | $GLOBALS['TSFE']->tmpl->rootLine[0]['uid'] = $urlObj->extConf['pagePath']['rootpage_id']; |
||
| 442 | } |
||
| 443 | } |
||
| 444 | |||
| 445 | 4 | if (is_array($vv['URLs'])) { |
|
| 446 | 4 | $configurationHash = $this->getConfigurationHash($vv); |
|
| 447 | 4 | $skipInnerCheck = $this->noUnprocessedQueueEntriesForPageWithConfigurationHashExist($pageRow['uid'], $configurationHash); |
|
| 448 | |||
| 449 | 4 | foreach ($vv['URLs'] as $urlQuery) { |
|
| 450 | 4 | if ($this->drawURLs_PIfilter($vv['subCfg']['procInstrFilter'], $incomingProcInstructions)) { |
|
| 451 | |||
| 452 | // Calculate cHash: |
||
| 453 | 4 | if ($vv['subCfg']['cHash']) { |
|
| 454 | /* @var $cacheHash \TYPO3\CMS\Frontend\Page\CacheHashCalculator */ |
||
| 455 | $cacheHash = GeneralUtility::makeInstance('TYPO3\CMS\Frontend\Page\CacheHashCalculator'); |
||
| 456 | $urlQuery .= '&cHash=' . $cacheHash->generateForParameters($urlQuery); |
||
| 457 | } |
||
| 458 | |||
| 459 | // Create key by which to determine unique-ness: |
||
| 460 | 4 | $uKey = $urlQuery . '|' . $vv['subCfg']['userGroups'] . '|' . $vv['subCfg']['baseUrl'] . '|' . $vv['subCfg']['procInstrFilter']; |
|
| 461 | |||
| 462 | // realurl support (thanks to Ingo Renner) |
||
| 463 | 4 | $urlQuery = 'index.php' . $urlQuery; |
|
| 464 | 4 | if (ExtensionManagementUtility::isLoaded('realurl') && $vv['subCfg']['realurl']) { |
|
| 465 | $params = [ |
||
| 466 | 'LD' => [ |
||
| 467 | 'totalURL' => $urlQuery |
||
| 468 | ], |
||
| 469 | 'TCEmainHook' => true |
||
| 470 | ]; |
||
| 471 | $urlObj->encodeSpURL($params); |
||
|
|
|||
| 472 | $urlQuery = $params['LD']['totalURL']; |
||
| 473 | } |
||
| 474 | |||
| 475 | // Scheduled time: |
||
| 476 | 4 | $schTime = $scheduledTime + round(count($duplicateTrack) * (60 / $reqMinute)); |
|
| 477 | 4 | $schTime = floor($schTime / 60) * 60; |
|
| 478 | |||
| 479 | 4 | if (isset($duplicateTrack[$uKey])) { |
|
| 480 | |||
| 481 | //if the url key is registered just display it and do not resubmit is |
||
| 482 | $urlList = '<em><span class="typo3-dimmed">' . htmlspecialchars($urlQuery) . '</span></em><br/>'; |
||
| 483 | } else { |
||
| 484 | 4 | $urlList = '[' . date('d.m.y H:i', $schTime) . '] ' . htmlspecialchars($urlQuery); |
|
| 485 | 4 | $this->urlList[] = '[' . date('d.m.y H:i', $schTime) . '] ' . $urlQuery; |
|
| 486 | |||
| 487 | 4 | $theUrl = ($vv['subCfg']['baseUrl'] ? $vv['subCfg']['baseUrl'] : GeneralUtility::getIndpEnv('TYPO3_SITE_URL')) . $urlQuery; |
|
| 488 | |||
| 489 | // Submit for crawling! |
||
| 490 | 4 | if ($submitCrawlUrls) { |
|
| 491 | 4 | $added = $this->addUrl( |
|
| 492 | 4 | $pageRow['uid'], |
|
| 493 | 4 | $theUrl, |
|
| 494 | 4 | $vv['subCfg'], |
|
| 495 | 4 | $scheduledTime, |
|
| 496 | 4 | $configurationHash, |
|
| 497 | 4 | $skipInnerCheck |
|
| 498 | ); |
||
| 499 | 4 | if ($added === false) { |
|
| 500 | 4 | $urlList .= ' (Url already existed)'; |
|
| 501 | } |
||
| 502 | } elseif ($downloadCrawlUrls) { |
||
| 503 | $downloadUrls[$theUrl] = $theUrl; |
||
| 504 | } |
||
| 505 | |||
| 506 | 4 | $urlList .= '<br />'; |
|
| 507 | } |
||
| 508 | 4 | $duplicateTrack[$uKey] = true; |
|
| 509 | } |
||
| 510 | } |
||
| 511 | } else { |
||
| 512 | $urlList = 'ERROR - no URL generated'; |
||
| 513 | } |
||
| 514 | |||
| 515 | 4 | return $urlList; |
|
| 516 | } |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Returns true if input processing instruction is among registered ones. |
||
| 520 | * |
||
| 521 | * @param string $piString PI to test |
||
| 522 | * @param array $incomingProcInstructions Processing instructions |
||
| 523 | * @return boolean |
||
| 524 | */ |
||
| 525 | 5 | public function drawURLs_PIfilter($piString, array $incomingProcInstructions) |
|
| 537 | |||
| 538 | 4 | public function getPageTSconfigForId($id) |
|
| 539 | { |
||
| 540 | 4 | if (!$this->MP) { |
|
| 541 | 4 | $pageTSconfig = BackendUtility::getPagesTSconfig($id); |
|
| 542 | } else { |
||
| 543 | list(, $mountPointId) = explode('-', $this->MP); |
||
| 544 | $pageTSconfig = BackendUtility::getPagesTSconfig($mountPointId); |
||
| 545 | } |
||
| 546 | |||
| 547 | // Call a hook to alter configuration |
||
| 548 | 4 | if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['getPageTSconfigForId'])) { |
|
| 549 | $params = [ |
||
| 550 | 'pageId' => $id, |
||
| 551 | 'pageTSConfig' => &$pageTSconfig |
||
| 552 | ]; |
||
| 553 | foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['getPageTSconfigForId'] as $userFunc) { |
||
| 554 | GeneralUtility::callUserFunction($userFunc, $params, $this); |
||
| 555 | } |
||
| 556 | } |
||
| 557 | |||
| 558 | 4 | return $pageTSconfig; |
|
| 559 | } |
||
| 560 | |||
| 561 | /** |
||
| 562 | * This methods returns an array of configurations. |
||
| 563 | * And no urls! |
||
| 564 | * |
||
| 565 | * @param integer $id Page ID |
||
| 566 | * @param bool $forceSsl Use https |
||
| 567 | * @return array |
||
| 568 | */ |
||
| 569 | 4 | protected function getUrlsForPageId($id, $forceSsl = false) |
|
| 570 | { |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Get configuration from tsConfig |
||
| 574 | */ |
||
| 575 | |||
| 576 | // Get page TSconfig for page ID: |
||
| 577 | 4 | $pageTSconfig = $this->getPageTSconfigForId($id); |
|
| 578 | |||
| 579 | 4 | $res = []; |
|
| 580 | |||
| 581 | 4 | if (is_array($pageTSconfig) && is_array($pageTSconfig['tx_crawler.']['crawlerCfg.'])) { |
|
| 582 | 3 | $crawlerCfg = $pageTSconfig['tx_crawler.']['crawlerCfg.']; |
|
| 583 | |||
| 584 | 3 | if (is_array($crawlerCfg['paramSets.'])) { |
|
| 585 | 3 | foreach ($crawlerCfg['paramSets.'] as $key => $values) { |
|
| 586 | 3 | if (is_array($values)) { |
|
| 587 | 3 | $key = str_replace('.', '', $key); |
|
| 588 | // Sub configuration for a single configuration string: |
||
| 589 | 3 | $subCfg = (array)$crawlerCfg['paramSets.'][$key . '.']; |
|
| 590 | 3 | $subCfg['key'] = $key; |
|
| 591 | |||
| 592 | 3 | if (strcmp($subCfg['procInstrFilter'], '')) { |
|
| 593 | 3 | $subCfg['procInstrFilter'] = implode(',', GeneralUtility::trimExplode(',', $subCfg['procInstrFilter'])); |
|
| 594 | } |
||
| 595 | 3 | $pidOnlyList = implode(',', GeneralUtility::trimExplode(',', $subCfg['pidsOnly'], true)); |
|
| 596 | |||
| 597 | // process configuration if it is not page-specific or if the specific page is the current page: |
||
| 598 | 3 | if (!strcmp($subCfg['pidsOnly'], '') || GeneralUtility::inList($pidOnlyList, $id)) { |
|
| 599 | |||
| 600 | // add trailing slash if not present |
||
| 601 | 3 | if (!empty($subCfg['baseUrl']) && substr($subCfg['baseUrl'], -1) != '/') { |
|
| 602 | $subCfg['baseUrl'] .= '/'; |
||
| 603 | } |
||
| 604 | |||
| 605 | // Explode, process etc.: |
||
| 606 | 3 | $res[$key] = []; |
|
| 607 | 3 | $res[$key]['subCfg'] = $subCfg; |
|
| 608 | 3 | $res[$key]['paramParsed'] = $this->parseParams($values); |
|
| 609 | 3 | $res[$key]['paramExpanded'] = $this->expandParameters($res[$key]['paramParsed'], $id); |
|
| 610 | 3 | $res[$key]['origin'] = 'pagets'; |
|
| 611 | |||
| 612 | // recognize MP value |
||
| 613 | 3 | if (!$this->MP) { |
|
| 614 | 3 | $res[$key]['URLs'] = $this->compileUrls($res[$key]['paramExpanded'], ['?id=' . $id]); |
|
| 615 | } else { |
||
| 616 | 3 | $res[$key]['URLs'] = $this->compileUrls($res[$key]['paramExpanded'], ['?id=' . $id . '&MP=' . $this->MP]); |
|
| 617 | } |
||
| 618 | } |
||
| 619 | } |
||
| 620 | } |
||
| 621 | } |
||
| 622 | } |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Get configuration from tx_crawler_configuration records |
||
| 626 | */ |
||
| 627 | |||
| 628 | // get records along the rootline |
||
| 629 | 4 | $rootLine = BackendUtility::BEgetRootLine($id); |
|
| 630 | |||
| 631 | 4 | foreach ($rootLine as $page) { |
|
| 632 | 4 | $configurationRecordsForCurrentPage = BackendUtility::getRecordsByField( |
|
| 633 | 4 | 'tx_crawler_configuration', |
|
| 634 | 4 | 'pid', |
|
| 635 | 4 | intval($page['uid']), |
|
| 636 | 4 | BackendUtility::BEenableFields('tx_crawler_configuration') . BackendUtility::deleteClause('tx_crawler_configuration') |
|
| 637 | ); |
||
| 638 | |||
| 639 | 4 | if (is_array($configurationRecordsForCurrentPage)) { |
|
| 640 | 1 | foreach ($configurationRecordsForCurrentPage as $configurationRecord) { |
|
| 641 | |||
| 642 | // check access to the configuration record |
||
| 643 | 1 | if (empty($configurationRecord['begroups']) || $GLOBALS['BE_USER']->isAdmin() || $this->hasGroupAccess($GLOBALS['BE_USER']->user['usergroup_cached_list'], $configurationRecord['begroups'])) { |
|
| 644 | 1 | $pidOnlyList = implode(',', GeneralUtility::trimExplode(',', $configurationRecord['pidsonly'], true)); |
|
| 645 | |||
| 646 | // process configuration if it is not page-specific or if the specific page is the current page: |
||
| 647 | 1 | if (!strcmp($configurationRecord['pidsonly'], '') || GeneralUtility::inList($pidOnlyList, $id)) { |
|
| 648 | 1 | $key = $configurationRecord['name']; |
|
| 649 | |||
| 650 | // don't overwrite previously defined paramSets |
||
| 651 | 1 | if (!isset($res[$key])) { |
|
| 652 | |||
| 653 | /* @var $TSparserObject \TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser */ |
||
| 654 | 1 | $TSparserObject = GeneralUtility::makeInstance('TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser'); |
|
| 655 | 1 | $TSparserObject->parse($configurationRecord['processing_instruction_parameters_ts']); |
|
| 656 | |||
| 657 | $subCfg = [ |
||
| 658 | 1 | 'procInstrFilter' => $configurationRecord['processing_instruction_filter'], |
|
| 659 | 1 | 'procInstrParams.' => $TSparserObject->setup, |
|
| 660 | 1 | 'baseUrl' => $this->getBaseUrlForConfigurationRecord( |
|
| 661 | 1 | $configurationRecord['base_url'], |
|
| 662 | 1 | $configurationRecord['sys_domain_base_url'], |
|
| 663 | 1 | $forceSsl |
|
| 664 | ), |
||
| 665 | 1 | 'realurl' => $configurationRecord['realurl'], |
|
| 666 | 1 | 'cHash' => $configurationRecord['chash'], |
|
| 667 | 1 | 'userGroups' => $configurationRecord['fegroups'], |
|
| 668 | 1 | 'exclude' => $configurationRecord['exclude'], |
|
| 669 | 1 | 'rootTemplatePid' => (int) $configurationRecord['root_template_pid'], |
|
| 670 | 1 | 'key' => $key, |
|
| 671 | ]; |
||
| 672 | |||
| 673 | // add trailing slash if not present |
||
| 674 | 1 | if (!empty($subCfg['baseUrl']) && substr($subCfg['baseUrl'], -1) != '/') { |
|
| 675 | $subCfg['baseUrl'] .= '/'; |
||
| 676 | } |
||
| 677 | 1 | if (!in_array($id, $this->expandExcludeString($subCfg['exclude']))) { |
|
| 678 | 1 | $res[$key] = []; |
|
| 679 | 1 | $res[$key]['subCfg'] = $subCfg; |
|
| 680 | 1 | $res[$key]['paramParsed'] = $this->parseParams($configurationRecord['configuration']); |
|
| 681 | 1 | $res[$key]['paramExpanded'] = $this->expandParameters($res[$key]['paramParsed'], $id); |
|
| 682 | 1 | $res[$key]['URLs'] = $this->compileUrls($res[$key]['paramExpanded'], ['?id=' . $id]); |
|
| 683 | 4 | $res[$key]['origin'] = 'tx_crawler_configuration_' . $configurationRecord['uid']; |
|
| 684 | } |
||
| 685 | } |
||
| 686 | } |
||
| 687 | } |
||
| 688 | } |
||
| 689 | } |
||
| 690 | } |
||
| 691 | |||
| 692 | 4 | if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['processUrls'])) { |
|
| 693 | foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['processUrls'] as $func) { |
||
| 694 | $params = [ |
||
| 695 | 'res' => &$res, |
||
| 696 | ]; |
||
| 697 | GeneralUtility::callUserFunction($func, $params, $this); |
||
| 698 | } |
||
| 699 | } |
||
| 700 | |||
| 701 | 4 | return $res; |
|
| 702 | } |
||
| 703 | |||
| 704 | /** |
||
| 705 | * Checks if a domain record exist and returns the base-url based on the record. If not the given baseUrl string is used. |
||
| 706 | * |
||
| 707 | * @param string $baseUrl |
||
| 708 | * @param integer $sysDomainUid |
||
| 709 | * @param bool $ssl |
||
| 710 | * @return string |
||
| 711 | */ |
||
| 712 | 4 | protected function getBaseUrlForConfigurationRecord($baseUrl, $sysDomainUid, $ssl = false) |
|
| 713 | { |
||
| 714 | 4 | $sysDomainUid = intval($sysDomainUid); |
|
| 715 | 4 | $urlScheme = ($ssl === false) ? 'http' : 'https'; |
|
| 716 | |||
| 717 | 4 | if ($sysDomainUid > 0) { |
|
| 718 | 2 | $res = $this->db->exec_SELECTquery( |
|
| 719 | 2 | '*', |
|
| 720 | 2 | 'sys_domain', |
|
| 721 | 2 | 'uid = ' . $sysDomainUid . |
|
| 722 | 2 | BackendUtility::BEenableFields('sys_domain') . |
|
| 723 | 2 | BackendUtility::deleteClause('sys_domain') |
|
| 724 | ); |
||
| 725 | 2 | $row = $this->db->sql_fetch_assoc($res); |
|
| 726 | 2 | if ($row['domainName'] != '') { |
|
| 727 | 1 | return $urlScheme . '://' . $row['domainName']; |
|
| 728 | } |
||
| 729 | } |
||
| 730 | 3 | return $baseUrl; |
|
| 731 | } |
||
| 732 | |||
| 733 | public function getConfigurationsForBranch($rootid, $depth) |
||
| 778 | |||
| 779 | /** |
||
| 780 | * Check if a user has access to an item |
||
| 781 | * (e.g. get the group list of the current logged in user from $GLOBALS['TSFE']->gr_list) |
||
| 782 | * |
||
| 783 | * @see \TYPO3\CMS\Frontend\Page\PageRepository::getMultipleGroupsWhereClause() |
||
| 784 | * @param string $groupList Comma-separated list of (fe_)group UIDs from a user |
||
| 785 | * @param string $accessList Comma-separated list of (fe_)group UIDs of the item to access |
||
| 786 | * @return bool TRUE if at least one of the users group UIDs is in the access list or the access list is empty |
||
| 787 | */ |
||
| 788 | 3 | public function hasGroupAccess($groupList, $accessList) |
|
| 800 | |||
| 801 | /** |
||
| 802 | * Parse GET vars of input Query into array with key=>value pairs |
||
| 803 | * |
||
| 804 | * @param string $inputQuery Input query string |
||
| 805 | * @return array |
||
| 806 | */ |
||
| 807 | 7 | public function parseParams($inputQuery) |
|
| 822 | |||
| 823 | /** |
||
| 824 | * Will expand the parameters configuration to individual values. This follows a certain syntax of the value of each parameter. |
||
| 825 | * Syntax of values: |
||
| 826 | * - Basically: If the value is wrapped in [...] it will be expanded according to the following syntax, otherwise the value is taken literally |
||
| 827 | * - Configuration is splitted by "|" and the parts are processed individually and finally added together |
||
| 828 | * - For each configuration part: |
||
| 829 | * - "[int]-[int]" = Integer range, will be expanded to all values in between, values included, starting from low to high (max. 1000). Example "1-34" or "-40--30" |
||
| 830 | * - "_TABLE:[TCA table name];[_PID:[optional page id, default is current page]];[_ENABLELANG:1]" = Look up of table records from PID, filtering out deleted records. Example "_TABLE:tt_content; _PID:123" |
||
| 831 | * _ENABLELANG:1 picks only original records without their language overlays |
||
| 832 | * - Default: Literal value |
||
| 833 | * |
||
| 834 | * @param array $paramArray Array with key (GET var name) and values (value of GET var which is configuration for expansion) |
||
| 835 | * @param integer $pid Current page ID |
||
| 836 | * @return array |
||
| 837 | */ |
||
| 838 | 4 | public function expandParameters($paramArray, $pid) |
|
| 839 | { |
||
| 840 | 4 | global $TCA; |
|
| 841 | |||
| 842 | // Traverse parameter names: |
||
| 843 | 4 | foreach ($paramArray as $p => $v) { |
|
| 844 | 1 | $v = trim($v); |
|
| 845 | |||
| 846 | // If value is encapsulated in square brackets it means there are some ranges of values to find, otherwise the value is literal |
||
| 847 | 1 | if (substr($v, 0, 1) === '[' && substr($v, -1) === ']') { |
|
| 848 | // So, find the value inside brackets and reset the paramArray value as an array. |
||
| 849 | 1 | $v = substr($v, 1, -1); |
|
| 850 | 1 | $paramArray[$p] = []; |
|
| 851 | |||
| 852 | // Explode parts and traverse them: |
||
| 853 | 1 | $parts = explode('|', $v); |
|
| 854 | 1 | foreach ($parts as $pV) { |
|
| 855 | |||
| 856 | // Look for integer range: (fx. 1-34 or -40--30 // reads minus 40 to minus 30) |
||
| 857 | 1 | if (preg_match('/^(-?[0-9]+)\s*-\s*(-?[0-9]+)$/', trim($pV), $reg)) { |
|
| 858 | |||
| 859 | // Swap if first is larger than last: |
||
| 860 | if ($reg[1] > $reg[2]) { |
||
| 861 | $temp = $reg[2]; |
||
| 862 | $reg[2] = $reg[1]; |
||
| 863 | $reg[1] = $temp; |
||
| 864 | } |
||
| 865 | |||
| 866 | // Traverse range, add values: |
||
| 867 | $runAwayBrake = 1000; // Limit to size of range! |
||
| 868 | for ($a = $reg[1]; $a <= $reg[2];$a++) { |
||
| 869 | $paramArray[$p][] = $a; |
||
| 870 | $runAwayBrake--; |
||
| 871 | if ($runAwayBrake <= 0) { |
||
| 872 | break; |
||
| 873 | } |
||
| 874 | } |
||
| 875 | 1 | } elseif (substr(trim($pV), 0, 7) == '_TABLE:') { |
|
| 876 | |||
| 877 | // Parse parameters: |
||
| 878 | $subparts = GeneralUtility::trimExplode(';', $pV); |
||
| 879 | $subpartParams = []; |
||
| 880 | foreach ($subparts as $spV) { |
||
| 881 | list($pKey, $pVal) = GeneralUtility::trimExplode(':', $spV); |
||
| 882 | $subpartParams[$pKey] = $pVal; |
||
| 883 | } |
||
| 884 | |||
| 885 | // Table exists: |
||
| 886 | if (isset($TCA[$subpartParams['_TABLE']])) { |
||
| 887 | $lookUpPid = isset($subpartParams['_PID']) ? intval($subpartParams['_PID']) : $pid; |
||
| 888 | $pidField = isset($subpartParams['_PIDFIELD']) ? trim($subpartParams['_PIDFIELD']) : 'pid'; |
||
| 889 | $where = isset($subpartParams['_WHERE']) ? $subpartParams['_WHERE'] : ''; |
||
| 890 | $addTable = isset($subpartParams['_ADDTABLE']) ? $subpartParams['_ADDTABLE'] : ''; |
||
| 891 | |||
| 892 | $fieldName = $subpartParams['_FIELD'] ? $subpartParams['_FIELD'] : 'uid'; |
||
| 893 | if ($fieldName === 'uid' || $TCA[$subpartParams['_TABLE']]['columns'][$fieldName]) { |
||
| 894 | $andWhereLanguage = ''; |
||
| 895 | $transOrigPointerField = $TCA[$subpartParams['_TABLE']]['ctrl']['transOrigPointerField']; |
||
| 896 | |||
| 897 | if ($subpartParams['_ENABLELANG'] && $transOrigPointerField) { |
||
| 898 | $andWhereLanguage = ' AND ' . $this->db->quoteStr($transOrigPointerField, $subpartParams['_TABLE']) . ' <= 0 '; |
||
| 899 | } |
||
| 900 | |||
| 901 | $where = $this->db->quoteStr($pidField, $subpartParams['_TABLE']) . '=' . intval($lookUpPid) . ' ' . |
||
| 902 | $andWhereLanguage . $where; |
||
| 903 | |||
| 904 | $rows = $this->db->exec_SELECTgetRows( |
||
| 905 | $fieldName, |
||
| 906 | $subpartParams['_TABLE'] . $addTable, |
||
| 907 | $where . BackendUtility::deleteClause($subpartParams['_TABLE']), |
||
| 908 | '', |
||
| 909 | '', |
||
| 910 | '', |
||
| 911 | $fieldName |
||
| 912 | ); |
||
| 913 | |||
| 914 | if (is_array($rows)) { |
||
| 915 | $paramArray[$p] = array_merge($paramArray[$p], array_keys($rows)); |
||
| 916 | } |
||
| 917 | } |
||
| 918 | } |
||
| 919 | } else { // Just add value: |
||
| 920 | 1 | $paramArray[$p][] = $pV; |
|
| 921 | } |
||
| 922 | // Hook for processing own expandParameters place holder |
||
| 923 | 1 | if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['crawler/class.tx_crawler_lib.php']['expandParameters'])) { |
|
| 924 | $_params = [ |
||
| 925 | 'pObj' => &$this, |
||
| 926 | 'paramArray' => &$paramArray, |
||
| 927 | 'currentKey' => $p, |
||
| 928 | 'currentValue' => $pV, |
||
| 929 | 'pid' => $pid |
||
| 930 | ]; |
||
| 931 | foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['crawler/class.tx_crawler_lib.php']['expandParameters'] as $key => $_funcRef) { |
||
| 932 | 1 | GeneralUtility::callUserFunction($_funcRef, $_params, $this); |
|
| 933 | } |
||
| 934 | } |
||
| 935 | } |
||
| 936 | |||
| 937 | // Make unique set of values and sort array by key: |
||
| 938 | 1 | $paramArray[$p] = array_unique($paramArray[$p]); |
|
| 939 | 1 | ksort($paramArray); |
|
| 940 | } else { |
||
| 941 | // Set the literal value as only value in array: |
||
| 942 | 1 | $paramArray[$p] = [$v]; |
|
| 943 | } |
||
| 944 | } |
||
| 945 | |||
| 946 | 4 | return $paramArray; |
|
| 947 | } |
||
| 948 | |||
| 949 | /** |
||
| 950 | * Compiling URLs from parameter array (output of expandParameters()) |
||
| 951 | * The number of URLs will be the multiplication of the number of parameter values for each key |
||
| 952 | * |
||
| 953 | * @param array $paramArray Output of expandParameters(): Array with keys (GET var names) and for each an array of values |
||
| 954 | * @param array $urls URLs accumulated in this array (for recursion) |
||
| 955 | * @return array |
||
| 956 | */ |
||
| 957 | 7 | public function compileUrls($paramArray, $urls = []) |
|
| 982 | |||
| 983 | /************************************ |
||
| 984 | * |
||
| 985 | * Crawler log |
||
| 986 | * |
||
| 987 | ************************************/ |
||
| 988 | |||
| 989 | /** |
||
| 990 | * Return array of records from crawler queue for input page ID |
||
| 991 | * |
||
| 992 | * @param integer $id Page ID for which to look up log entries. |
||
| 993 | * @param string$filter Filter: "all" => all entries, "pending" => all that is not yet run, "finished" => all complete ones |
||
| 994 | * @param boolean $doFlush If TRUE, then entries selected at DELETED(!) instead of selected! |
||
| 995 | * @param boolean $doFullFlush |
||
| 996 | * @param integer $itemsPerPage Limit the amount of entries per page default is 10 |
||
| 997 | * @return array |
||
| 998 | */ |
||
| 999 | 4 | public function getLogEntriesForPageId($id, $filter = '', $doFlush = false, $doFullFlush = false, $itemsPerPage = 10) |
|
| 1000 | { |
||
| 1001 | // FIXME: Write Unit tests for Filters |
||
| 1002 | switch ($filter) { |
||
| 1003 | 4 | case 'pending': |
|
| 1004 | $addWhere = ' AND exec_time=0'; |
||
| 1005 | break; |
||
| 1006 | 4 | case 'finished': |
|
| 1007 | $addWhere = ' AND exec_time>0'; |
||
| 1008 | break; |
||
| 1009 | default: |
||
| 1010 | 4 | $addWhere = ''; |
|
| 1011 | 4 | break; |
|
| 1012 | } |
||
| 1013 | |||
| 1014 | // FIXME: Write unit test that ensures that the right records are deleted. |
||
| 1015 | 4 | if ($doFlush) { |
|
| 1016 | 2 | $this->flushQueue(($doFullFlush ? '1=1' : ('page_id=' . intval($id))) . $addWhere); |
|
| 1017 | 2 | return []; |
|
| 1018 | } else { |
||
| 1019 | 2 | return $this->db->exec_SELECTgetRows( |
|
| 1020 | 2 | '*', |
|
| 1021 | 2 | 'tx_crawler_queue', |
|
| 1022 | 2 | 'page_id=' . intval($id) . $addWhere, |
|
| 1023 | 2 | '', |
|
| 1024 | 2 | 'scheduled DESC', |
|
| 1025 | 2 | (intval($itemsPerPage) > 0 ? intval($itemsPerPage) : '') |
|
| 1026 | ); |
||
| 1027 | } |
||
| 1028 | } |
||
| 1029 | |||
| 1030 | /** |
||
| 1031 | * Return array of records from crawler queue for input set ID |
||
| 1032 | * |
||
| 1033 | * @param integer $set_id Set ID for which to look up log entries. |
||
| 1034 | * @param string $filter Filter: "all" => all entries, "pending" => all that is not yet run, "finished" => all complete ones |
||
| 1035 | * @param boolean $doFlush If TRUE, then entries selected at DELETED(!) instead of selected! |
||
| 1036 | * @param integer $itemsPerPage Limit the amount of entires per page default is 10 |
||
| 1037 | * @return array |
||
| 1038 | */ |
||
| 1039 | 4 | public function getLogEntriesForSetId($set_id, $filter = '', $doFlush = false, $doFullFlush = false, $itemsPerPage = 10) |
|
| 1040 | { |
||
| 1041 | // FIXME: Write Unit tests for Filters |
||
| 1042 | switch ($filter) { |
||
| 1043 | 4 | case 'pending': |
|
| 1044 | $addWhere = ' AND exec_time=0'; |
||
| 1045 | break; |
||
| 1046 | 4 | case 'finished': |
|
| 1047 | $addWhere = ' AND exec_time>0'; |
||
| 1048 | break; |
||
| 1049 | default: |
||
| 1050 | 4 | $addWhere = ''; |
|
| 1051 | 4 | break; |
|
| 1052 | } |
||
| 1053 | // FIXME: Write unit test that ensures that the right records are deleted. |
||
| 1054 | 4 | if ($doFlush) { |
|
| 1055 | 2 | $this->flushQueue($doFullFlush ? '' : ('set_id=' . intval($set_id) . $addWhere)); |
|
| 1056 | 2 | return []; |
|
| 1057 | } else { |
||
| 1058 | 2 | return $this->db->exec_SELECTgetRows( |
|
| 1059 | 2 | '*', |
|
| 1060 | 2 | 'tx_crawler_queue', |
|
| 1061 | 2 | 'set_id=' . intval($set_id) . $addWhere, |
|
| 1062 | 2 | '', |
|
| 1063 | 2 | 'scheduled DESC', |
|
| 1064 | 2 | (intval($itemsPerPage) > 0 ? intval($itemsPerPage) : '') |
|
| 1065 | ); |
||
| 1066 | } |
||
| 1067 | } |
||
| 1068 | |||
| 1069 | /** |
||
| 1070 | * Removes queue entries |
||
| 1071 | * |
||
| 1072 | * @param string $where SQL related filter for the entries which should be removed |
||
| 1073 | * @return void |
||
| 1074 | */ |
||
| 1075 | 8 | protected function flushQueue($where = '') |
|
| 1076 | { |
||
| 1077 | 8 | $realWhere = strlen($where) > 0 ? $where : '1=1'; |
|
| 1078 | |||
| 1079 | 8 | if (EventDispatcher::getInstance()->hasObserver('queueEntryFlush')) { |
|
| 1080 | $groups = $this->db->exec_SELECTgetRows('DISTINCT set_id', 'tx_crawler_queue', $realWhere); |
||
| 1081 | if (is_array($groups)) { |
||
| 1082 | foreach ($groups as $group) { |
||
| 1083 | EventDispatcher::getInstance()->post('queueEntryFlush', $group['set_id'], $this->db->exec_SELECTgetRows('uid, set_id', 'tx_crawler_queue', $realWhere . ' AND set_id="' . $group['set_id'] . '"')); |
||
| 1084 | } |
||
| 1085 | } |
||
| 1086 | } |
||
| 1087 | |||
| 1088 | 8 | $this->db->exec_DELETEquery('tx_crawler_queue', $realWhere); |
|
| 1089 | 8 | } |
|
| 1090 | |||
| 1091 | /** |
||
| 1092 | * Adding call back entries to log (called from hooks typically, see indexed search class "class.crawler.php" |
||
| 1093 | * |
||
| 1094 | * @param integer $setId Set ID |
||
| 1095 | * @param array $params Parameters to pass to call back function |
||
| 1096 | * @param string $callBack Call back object reference, eg. 'EXT:indexed_search/class.crawler.php:&tx_indexedsearch_crawler' |
||
| 1097 | * @param integer $page_id Page ID to attach it to |
||
| 1098 | * @param integer $schedule Time at which to activate |
||
| 1099 | * @return void |
||
| 1100 | */ |
||
| 1101 | public function addQueueEntry_callBack($setId, $params, $callBack, $page_id = 0, $schedule = 0) |
||
| 1120 | |||
| 1121 | /************************************ |
||
| 1122 | * |
||
| 1123 | * URL setting |
||
| 1124 | * |
||
| 1125 | ************************************/ |
||
| 1126 | |||
| 1127 | /** |
||
| 1128 | * Setting a URL for crawling: |
||
| 1129 | * |
||
| 1130 | * @param integer $id Page ID |
||
| 1131 | * @param string $url Complete URL |
||
| 1132 | * @param array $subCfg Sub configuration array (from TS config) |
||
| 1133 | * @param integer $tstamp Scheduled-time |
||
| 1134 | * @param string $configurationHash (optional) configuration hash |
||
| 1135 | * @param bool $skipInnerDuplicationCheck (optional) skip inner duplication check |
||
| 1136 | * @return bool |
||
| 1137 | */ |
||
| 1138 | 4 | public function addUrl( |
|
| 1139 | $id, |
||
| 1140 | $url, |
||
| 1141 | array $subCfg, |
||
| 1142 | $tstamp, |
||
| 1143 | $configurationHash = '', |
||
| 1144 | $skipInnerDuplicationCheck = false |
||
| 1145 | ) { |
||
| 1146 | 4 | $urlAdded = false; |
|
| 1147 | 4 | $rows = []; |
|
| 1148 | |||
| 1149 | // Creating parameters: |
||
| 1150 | $parameters = [ |
||
| 1151 | 4 | 'url' => $url |
|
| 1152 | ]; |
||
| 1153 | |||
| 1154 | // fe user group simulation: |
||
| 1155 | 4 | $uGs = implode(',', array_unique(GeneralUtility::intExplode(',', $subCfg['userGroups'], true))); |
|
| 1156 | 4 | if ($uGs) { |
|
| 1157 | $parameters['feUserGroupList'] = $uGs; |
||
| 1158 | } |
||
| 1159 | |||
| 1160 | // Setting processing instructions |
||
| 1161 | 4 | $parameters['procInstructions'] = GeneralUtility::trimExplode(',', $subCfg['procInstrFilter']); |
|
| 1162 | 4 | if (is_array($subCfg['procInstrParams.'])) { |
|
| 1163 | 4 | $parameters['procInstrParams'] = $subCfg['procInstrParams.']; |
|
| 1164 | } |
||
| 1165 | |||
| 1166 | // Possible TypoScript Template Parents |
||
| 1167 | 4 | $parameters['rootTemplatePid'] = $subCfg['rootTemplatePid']; |
|
| 1168 | |||
| 1169 | // Compile value array: |
||
| 1170 | 4 | $parameters_serialized = serialize($parameters); |
|
| 1171 | $fieldArray = [ |
||
| 1172 | 4 | 'page_id' => intval($id), |
|
| 1173 | 4 | 'parameters' => $parameters_serialized, |
|
| 1174 | 4 | 'parameters_hash' => GeneralUtility::shortMD5($parameters_serialized), |
|
| 1175 | 4 | 'configuration_hash' => $configurationHash, |
|
| 1176 | 4 | 'scheduled' => $tstamp, |
|
| 1177 | 4 | 'exec_time' => 0, |
|
| 1178 | 4 | 'set_id' => intval($this->setID), |
|
| 1179 | 4 | 'result_data' => '', |
|
| 1180 | 4 | 'configuration' => $subCfg['key'], |
|
| 1181 | ]; |
||
| 1182 | |||
| 1183 | 4 | if ($this->registerQueueEntriesInternallyOnly) { |
|
| 1184 | //the entries will only be registered and not stored to the database |
||
| 1185 | $this->queueEntries[] = $fieldArray; |
||
| 1186 | } else { |
||
| 1187 | 4 | if (!$skipInnerDuplicationCheck) { |
|
| 1188 | // check if there is already an equal entry |
||
| 1189 | 4 | $rows = $this->getDuplicateRowsIfExist($tstamp, $fieldArray); |
|
| 1190 | } |
||
| 1191 | |||
| 1192 | 4 | if (count($rows) == 0) { |
|
| 1193 | 4 | $this->db->exec_INSERTquery('tx_crawler_queue', $fieldArray); |
|
| 1194 | 4 | $uid = $this->db->sql_insert_id(); |
|
| 1195 | 4 | $rows[] = $uid; |
|
| 1196 | 4 | $urlAdded = true; |
|
| 1197 | 4 | EventDispatcher::getInstance()->post('urlAddedToQueue', $this->setID, ['uid' => $uid, 'fieldArray' => $fieldArray]); |
|
| 1198 | } else { |
||
| 1199 | 2 | EventDispatcher::getInstance()->post('duplicateUrlInQueue', $this->setID, ['rows' => $rows, 'fieldArray' => $fieldArray]); |
|
| 1200 | } |
||
| 1201 | } |
||
| 1202 | |||
| 1203 | 4 | return $urlAdded; |
|
| 1204 | } |
||
| 1205 | |||
| 1206 | /** |
||
| 1207 | * This method determines duplicates for a queue entry with the same parameters and this timestamp. |
||
| 1208 | * If the timestamp is in the past, it will check if there is any unprocessed queue entry in the past. |
||
| 1209 | * If the timestamp is in the future it will check, if the queued entry has exactly the same timestamp |
||
| 1210 | * |
||
| 1211 | * @param int $tstamp |
||
| 1212 | * @param array $fieldArray |
||
| 1213 | * |
||
| 1214 | * @return array |
||
| 1215 | */ |
||
| 1216 | 4 | protected function getDuplicateRowsIfExist($tstamp, $fieldArray) |
|
| 1217 | { |
||
| 1218 | 4 | $rows = []; |
|
| 1219 | |||
| 1220 | 4 | $currentTime = $this->getCurrentTime(); |
|
| 1221 | |||
| 1222 | //if this entry is scheduled with "now" |
||
| 1223 | 4 | if ($tstamp <= $currentTime) { |
|
| 1224 | 1 | if ($this->extensionSettings['enableTimeslot']) { |
|
| 1225 | 1 | $timeBegin = $currentTime - 100; |
|
| 1226 | 1 | $timeEnd = $currentTime + 100; |
|
| 1227 | 1 | $where = ' ((scheduled BETWEEN ' . $timeBegin . ' AND ' . $timeEnd . ' ) OR scheduled <= ' . $currentTime . ') '; |
|
| 1228 | } else { |
||
| 1229 | 1 | $where = 'scheduled <= ' . $currentTime; |
|
| 1230 | } |
||
| 1231 | 3 | } elseif ($tstamp > $currentTime) { |
|
| 1232 | //entry with a timestamp in the future need to have the same schedule time |
||
| 1233 | 3 | $where = 'scheduled = ' . $tstamp ; |
|
| 1234 | } |
||
| 1235 | |||
| 1236 | 4 | if (!empty($where)) { |
|
| 1237 | 4 | $result = $this->db->exec_SELECTgetRows( |
|
| 1238 | 4 | 'qid', |
|
| 1239 | 4 | 'tx_crawler_queue', |
|
| 1240 | $where . |
||
| 1241 | 4 | ' AND NOT exec_time' . |
|
| 1242 | 4 | ' AND NOT process_id ' . |
|
| 1243 | 4 | ' AND page_id=' . intval($fieldArray['page_id']) . |
|
| 1244 | 4 | ' AND parameters_hash = ' . $this->db->fullQuoteStr($fieldArray['parameters_hash'], 'tx_crawler_queue') |
|
| 1245 | ); |
||
| 1246 | |||
| 1247 | 4 | if (is_array($result)) { |
|
| 1248 | 4 | foreach ($result as $value) { |
|
| 1249 | 2 | $rows[] = $value['qid']; |
|
| 1250 | } |
||
| 1251 | } |
||
| 1252 | } |
||
| 1253 | |||
| 1254 | 4 | return $rows; |
|
| 1255 | } |
||
| 1256 | |||
| 1257 | /** |
||
| 1258 | * Returns the current system time |
||
| 1259 | * |
||
| 1260 | * @return int |
||
| 1261 | */ |
||
| 1262 | 1 | public function getCurrentTime() |
|
| 1263 | { |
||
| 1264 | 1 | return time(); |
|
| 1265 | } |
||
| 1266 | |||
| 1267 | /************************************ |
||
| 1268 | * |
||
| 1269 | * URL reading |
||
| 1270 | * |
||
| 1271 | ************************************/ |
||
| 1272 | |||
| 1273 | /** |
||
| 1274 | * Read URL for single queue entry |
||
| 1275 | * |
||
| 1276 | * @param integer $queueId |
||
| 1277 | * @param boolean $force If set, will process even if exec_time has been set! |
||
| 1278 | * @return integer |
||
| 1279 | */ |
||
| 1280 | public function readUrl($queueId, $force = false) |
||
| 1360 | |||
| 1361 | /** |
||
| 1362 | * Read URL for not-yet-inserted log-entry |
||
| 1363 | * |
||
| 1364 | * @param array $field_array Queue field array, |
||
| 1365 | * |
||
| 1366 | * @return string |
||
| 1367 | */ |
||
| 1368 | public function readUrlFromArray($field_array) |
||
| 1391 | |||
| 1392 | /** |
||
| 1393 | * Read URL for a queue record |
||
| 1394 | * |
||
| 1395 | * @param array $queueRec Queue record |
||
| 1396 | * @return string |
||
| 1397 | */ |
||
| 1398 | public function readUrl_exec($queueRec) |
||
| 1427 | |||
| 1428 | /** |
||
| 1429 | * Gets the content of a URL. |
||
| 1430 | * |
||
| 1431 | * @param string $originalUrl URL to read |
||
| 1432 | * @param string $crawlerId Crawler ID string (qid + hash to verify) |
||
| 1433 | * @param integer $timeout Timeout time |
||
| 1434 | * @param integer $recursion Recursion limiter for 302 redirects |
||
| 1435 | * @return array |
||
| 1436 | */ |
||
| 1437 | 2 | public function requestUrl($originalUrl, $crawlerId, $timeout = 2, $recursion = 10) |
|
| 1530 | |||
| 1531 | /** |
||
| 1532 | * Gets the base path of the website frontend. |
||
| 1533 | * (e.g. if you call http://mydomain.com/cms/index.php in |
||
| 1534 | * the browser the base path is "/cms/") |
||
| 1535 | * |
||
| 1536 | * @return string Base path of the website frontend |
||
| 1537 | */ |
||
| 1538 | protected function getFrontendBasePath() |
||
| 1561 | |||
| 1562 | /** |
||
| 1563 | * Executes a shell command and returns the outputted result. |
||
| 1564 | * |
||
| 1565 | * @param string $command Shell command to be executed |
||
| 1566 | * @return string Outputted result of the command execution |
||
| 1567 | */ |
||
| 1568 | protected function executeShellCommand($command) |
||
| 1573 | |||
| 1574 | /** |
||
| 1575 | * Reads HTTP response from the given stream. |
||
| 1576 | * |
||
| 1577 | * @param resource $streamPointer Pointer to connection stream. |
||
| 1578 | * @return array Associative array with the following items: |
||
| 1579 | * headers <array> Response headers sent by server. |
||
| 1580 | * content <array> Content, with each line as an array item. |
||
| 1581 | */ |
||
| 1582 | 1 | protected function getHttpResponseFromStream($streamPointer) |
|
| 1583 | { |
||
| 1584 | 1 | $response = ['headers' => [], 'content' => []]; |
|
| 1585 | |||
| 1586 | 1 | if (is_resource($streamPointer)) { |
|
| 1587 | // read headers |
||
| 1588 | 1 | while ($line = fgets($streamPointer, '2048')) { |
|
| 1589 | 1 | $line = trim($line); |
|
| 1590 | 1 | if ($line !== '') { |
|
| 1591 | 1 | $response['headers'][] = $line; |
|
| 1592 | } else { |
||
| 1593 | 1 | break; |
|
| 1594 | } |
||
| 1595 | } |
||
| 1596 | |||
| 1597 | // read content |
||
| 1598 | 1 | while ($line = fgets($streamPointer, '2048')) { |
|
| 1599 | 1 | $response['content'][] = $line; |
|
| 1600 | } |
||
| 1601 | } |
||
| 1602 | |||
| 1603 | 1 | return $response; |
|
| 1604 | } |
||
| 1605 | |||
| 1606 | /** |
||
| 1607 | * @param message |
||
| 1608 | */ |
||
| 1609 | 2 | protected function log($message) |
|
| 1618 | |||
| 1619 | /** |
||
| 1620 | * Builds HTTP request headers. |
||
| 1621 | * |
||
| 1622 | * @param array $url |
||
| 1623 | * @param string $crawlerId |
||
| 1624 | * |
||
| 1625 | * @return array |
||
| 1626 | */ |
||
| 1627 | 6 | protected function buildRequestHeaderArray(array $url, $crawlerId) |
|
| 1643 | |||
| 1644 | /** |
||
| 1645 | * Check if the submitted HTTP-Header contains a redirect location and built new crawler-url |
||
| 1646 | * |
||
| 1647 | * @param array $headers HTTP Header |
||
| 1648 | * @param string $user HTTP Auth. User |
||
| 1649 | * @param string $pass HTTP Auth. Password |
||
| 1650 | * @return bool|string |
||
| 1651 | */ |
||
| 1652 | 12 | protected function getRequestUrlFrom302Header($headers, $user = '', $pass = '') |
|
| 1686 | |||
| 1687 | /************************** |
||
| 1688 | * |
||
| 1689 | * tslib_fe hooks: |
||
| 1690 | * |
||
| 1691 | **************************/ |
||
| 1692 | |||
| 1693 | /** |
||
| 1694 | * Initialization hook (called after database connection) |
||
| 1695 | * Takes the "HTTP_X_T3CRAWLER" header and looks up queue record and verifies if the session comes from the system (by comparing hashes) |
||
| 1696 | * |
||
| 1697 | * @param array $params Parameters from frontend |
||
| 1698 | * @param object $ref TSFE object (reference under PHP5) |
||
| 1699 | * @return void |
||
| 1700 | * |
||
| 1701 | * FIXME: Look like this is not used, in commit 9910d3f40cce15f4e9b7bcd0488bf21f31d53ebc it's added as public, |
||
| 1702 | * FIXME: I think this can be removed. (TNM) |
||
| 1703 | */ |
||
| 1704 | public function fe_init(&$params, $ref) |
||
| 1721 | |||
| 1722 | /***************************** |
||
| 1723 | * |
||
| 1724 | * Compiling URLs to crawl - tools |
||
| 1725 | * |
||
| 1726 | *****************************/ |
||
| 1727 | |||
| 1728 | /** |
||
| 1729 | * @param integer $id Root page id to start from. |
||
| 1730 | * @param integer $depth Depth of tree, 0=only id-page, 1= on sublevel, 99 = infinite |
||
| 1731 | * @param integer $scheduledTime Unix Time when the URL is timed to be visited when put in queue |
||
| 1732 | * @param integer $reqMinute Number of requests per minute (creates the interleave between requests) |
||
| 1733 | * @param boolean $submitCrawlUrls If set, submits the URLs to queue in database (real crawling) |
||
| 1734 | * @param boolean $downloadCrawlUrls If set (and submitcrawlUrls is false) will fill $downloadUrls with entries) |
||
| 1735 | * @param array $incomingProcInstructions Array of processing instructions |
||
| 1736 | * @param array $configurationSelection Array of configuration keys |
||
| 1737 | * @return string |
||
| 1738 | */ |
||
| 1739 | public function getPageTreeAndUrls( |
||
| 1826 | |||
| 1827 | /** |
||
| 1828 | * Expands exclude string |
||
| 1829 | * |
||
| 1830 | * @param string $excludeString Exclude string |
||
| 1831 | * @return array |
||
| 1832 | */ |
||
| 1833 | 1 | public function expandExcludeString($excludeString) |
|
| 1834 | { |
||
| 1835 | // internal static caches; |
||
| 1836 | 1 | static $expandedExcludeStringCache; |
|
| 1837 | 1 | static $treeCache; |
|
| 1838 | |||
| 1839 | 1 | if (empty($expandedExcludeStringCache[$excludeString])) { |
|
| 1840 | 1 | $pidList = []; |
|
| 1841 | |||
| 1842 | 1 | if (!empty($excludeString)) { |
|
| 1843 | /** @var PageTreeView $tree */ |
||
| 1844 | $tree = GeneralUtility::makeInstance(PageTreeView::class); |
||
| 1845 | $tree->init('AND ' . $this->backendUser->getPagePermsClause(1)); |
||
| 1846 | |||
| 1847 | $excludeParts = GeneralUtility::trimExplode(',', $excludeString); |
||
| 1848 | |||
| 1849 | foreach ($excludeParts as $excludePart) { |
||
| 1850 | list($pid, $depth) = GeneralUtility::trimExplode('+', $excludePart); |
||
| 1851 | |||
| 1852 | // default is "page only" = "depth=0" |
||
| 1853 | if (empty($depth)) { |
||
| 1854 | $depth = (stristr($excludePart, '+')) ? 99 : 0; |
||
| 1855 | } |
||
| 1856 | |||
| 1857 | $pidList[] = $pid; |
||
| 1858 | |||
| 1859 | if ($depth > 0) { |
||
| 1860 | if (empty($treeCache[$pid][$depth])) { |
||
| 1861 | $tree->reset(); |
||
| 1862 | $tree->getTree($pid, $depth); |
||
| 1863 | $treeCache[$pid][$depth] = $tree->tree; |
||
| 1864 | } |
||
| 1865 | |||
| 1866 | foreach ($treeCache[$pid][$depth] as $data) { |
||
| 1867 | $pidList[] = $data['row']['uid']; |
||
| 1868 | } |
||
| 1869 | } |
||
| 1870 | } |
||
| 1871 | } |
||
| 1872 | |||
| 1873 | 1 | $expandedExcludeStringCache[$excludeString] = array_unique($pidList); |
|
| 1874 | } |
||
| 1875 | |||
| 1876 | 1 | return $expandedExcludeStringCache[$excludeString]; |
|
| 1877 | } |
||
| 1878 | |||
| 1879 | /** |
||
| 1880 | * Create the rows for display of the page tree |
||
| 1881 | * For each page a number of rows are shown displaying GET variable configuration |
||
| 1882 | * |
||
| 1883 | * @param array Page row |
||
| 1884 | * @param string Page icon and title for row |
||
| 1885 | * @return string HTML <tr> content (one or more) |
||
| 1886 | */ |
||
| 1887 | public function drawURLs_addRowsForPage(array $pageRow, $pageTitleAndIcon) |
||
| 1996 | |||
| 1997 | /** |
||
| 1998 | * @return int |
||
| 1999 | */ |
||
| 2000 | 1 | public function getUnprocessedItemsCount() |
|
| 2001 | { |
||
| 2002 | 1 | $res = $this->db->exec_SELECTquery( |
|
| 2003 | 1 | 'count(*) as num', |
|
| 2004 | 1 | 'tx_crawler_queue', |
|
| 2005 | 1 | 'exec_time=0 AND process_scheduled=0 AND scheduled<=' . $this->getCurrentTime() |
|
| 2006 | ); |
||
| 2007 | |||
| 2008 | 1 | $count = $this->db->sql_fetch_assoc($res); |
|
| 2009 | 1 | return $count['num']; |
|
| 2010 | } |
||
| 2011 | |||
| 2012 | /***************************** |
||
| 2013 | * |
||
| 2014 | * CLI functions |
||
| 2015 | * |
||
| 2016 | *****************************/ |
||
| 2017 | |||
| 2018 | /** |
||
| 2019 | * Main function for running from Command Line PHP script (cron job) |
||
| 2020 | * See ext/crawler/cli/crawler_cli.phpsh for details |
||
| 2021 | * |
||
| 2022 | * @return int number of remaining items or false if error |
||
| 2023 | */ |
||
| 2024 | public function CLI_main() |
||
| 2065 | |||
| 2066 | /** |
||
| 2067 | * Function executed by crawler_im.php cli script. |
||
| 2068 | * |
||
| 2069 | * @return void |
||
| 2070 | */ |
||
| 2071 | public function CLI_main_im() |
||
| 2169 | |||
| 2170 | /** |
||
| 2171 | * Function executed by crawler_im.php cli script. |
||
| 2172 | * |
||
| 2173 | * @return bool |
||
| 2174 | */ |
||
| 2175 | public function CLI_main_flush() |
||
| 2213 | |||
| 2214 | /** |
||
| 2215 | * Obtains configuration keys from the CLI arguments |
||
| 2216 | * |
||
| 2217 | * @param QueueCommandLineController $cliObj Command line object |
||
| 2218 | * @return mixed Array of keys or null if no keys found |
||
| 2219 | */ |
||
| 2220 | protected function getConfigurationKeys(QueueCommandLineController &$cliObj) |
||
| 2225 | |||
| 2226 | /** |
||
| 2227 | * Running the functionality of the CLI (crawling URLs from queue) |
||
| 2228 | * |
||
| 2229 | * @param int $countInARun |
||
| 2230 | * @param int $sleepTime |
||
| 2231 | * @param int $sleepAfterFinish |
||
| 2232 | * @return string |
||
| 2233 | */ |
||
| 2234 | public function CLI_run($countInARun, $sleepTime, $sleepAfterFinish) |
||
| 2341 | |||
| 2342 | /** |
||
| 2343 | * Activate hooks |
||
| 2344 | * |
||
| 2345 | * @return void |
||
| 2346 | */ |
||
| 2347 | public function CLI_runHooks() |
||
| 2359 | |||
| 2360 | /** |
||
| 2361 | * Try to acquire a new process with the given id |
||
| 2362 | * also performs some auto-cleanup for orphan processes |
||
| 2363 | * @todo preemption might not be the most elegant way to clean up |
||
| 2364 | * |
||
| 2365 | * @param string $id identification string for the process |
||
| 2366 | * @return boolean |
||
| 2367 | */ |
||
| 2368 | public function CLI_checkAndAcquireNewProcess($id) |
||
| 2424 | |||
| 2425 | /** |
||
| 2426 | * Release a process and the required resources |
||
| 2427 | * |
||
| 2428 | * @param mixed $releaseIds string with a single process-id or array with multiple process-ids |
||
| 2429 | * @param boolean $withinLock show whether the DB-actions are included within an existing lock |
||
| 2430 | * @return boolean |
||
| 2431 | */ |
||
| 2432 | public function CLI_releaseProcesses($releaseIds, $withinLock = false) |
||
| 2494 | |||
| 2495 | /** |
||
| 2496 | * Delete processes marked as deleted |
||
| 2497 | * |
||
| 2498 | * @return void |
||
| 2499 | */ |
||
| 2500 | 1 | public function CLI_deleteProcessesMarkedDeleted() |
|
| 2501 | { |
||
| 2502 | 1 | $this->db->exec_DELETEquery('tx_crawler_process', 'deleted = 1'); |
|
| 2503 | 1 | } |
|
| 2504 | |||
| 2505 | /** |
||
| 2506 | * Check if there are still resources left for the process with the given id |
||
| 2507 | * Used to determine timeouts and to ensure a proper cleanup if there's a timeout |
||
| 2508 | * |
||
| 2509 | * @param string identification string for the process |
||
| 2510 | * @return boolean determines if the process is still active / has resources |
||
| 2511 | * |
||
| 2512 | * FIXME: Please remove Transaction, not needed as only a select query. |
||
| 2513 | */ |
||
| 2514 | public function CLI_checkIfProcessIsActive($pid) |
||
| 2533 | |||
| 2534 | /** |
||
| 2535 | * Create a unique Id for the current process |
||
| 2536 | * |
||
| 2537 | * @return string the ID |
||
| 2538 | */ |
||
| 2539 | 2 | public function CLI_buildProcessId() |
|
| 2546 | |||
| 2547 | /** |
||
| 2548 | * @param bool $get_as_float |
||
| 2549 | * |
||
| 2550 | * @return mixed |
||
| 2551 | */ |
||
| 2552 | protected function microtime($get_as_float = false) |
||
| 2556 | |||
| 2557 | /** |
||
| 2558 | * Prints a message to the stdout (only if debug-mode is enabled) |
||
| 2559 | * |
||
| 2560 | * @param string $msg the message |
||
| 2561 | */ |
||
| 2562 | public function CLI_debug($msg) |
||
| 2569 | |||
| 2570 | /** |
||
| 2571 | * Get URL content by making direct request to TYPO3. |
||
| 2572 | * |
||
| 2573 | * @param string $url Page URL |
||
| 2574 | * @param int $crawlerId Crawler-ID |
||
| 2575 | * @return array |
||
| 2576 | */ |
||
| 2577 | 2 | protected function sendDirectRequest($url, $crawlerId) |
|
| 2608 | |||
| 2609 | /** |
||
| 2610 | * Cleans up entries that stayed for too long in the queue. These are: |
||
| 2611 | * - processed entries that are over 1.5 days in age |
||
| 2612 | * - scheduled entries that are over 7 days old |
||
| 2613 | * |
||
| 2614 | * @return void |
||
| 2615 | */ |
||
| 2616 | protected function cleanUpOldQueueEntries() |
||
| 2625 | |||
| 2626 | /** |
||
| 2627 | * Initializes a TypoScript Frontend necessary for using TypoScript and TypoLink functions |
||
| 2628 | * |
||
| 2629 | * @param int $id |
||
| 2630 | * @param int $typeNum |
||
| 2631 | * |
||
| 2632 | * @return void |
||
| 2633 | */ |
||
| 2634 | protected function initTSFE($id = 1, $typeNum = 0) |
||
| 2653 | |||
| 2654 | /** |
||
| 2655 | * Returns a md5 hash generated from a serialized configuration array. |
||
| 2656 | * |
||
| 2657 | * @param array $configuration |
||
| 2658 | * |
||
| 2659 | * @return string |
||
| 2660 | */ |
||
| 2661 | 9 | protected function getConfigurationHash(array $configuration) { |
|
| 2666 | } |
||
| 2667 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: