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 |
||
| 63 | class CrawlerController |
||
| 64 | { |
||
| 65 | const CLI_STATUS_NOTHING_PROCCESSED = 0; |
||
| 66 | const CLI_STATUS_REMAIN = 1; //queue not empty |
||
| 67 | const CLI_STATUS_PROCESSED = 2; //(some) queue items where processed |
||
| 68 | const CLI_STATUS_ABORTED = 4; //instance didn't finish |
||
| 69 | const CLI_STATUS_POLLABLE_PROCESSED = 8; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var integer |
||
| 73 | */ |
||
| 74 | public $setID = 0; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | public $processID = ''; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * One hour is max stalled time for the CLI |
||
| 83 | * If the process had the status "start" for 3600 seconds, it will be regarded stalled and a new process is started |
||
| 84 | * |
||
| 85 | * @var integer |
||
| 86 | */ |
||
| 87 | public $max_CLI_exec_time = 3600; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | public $duplicateTrack = []; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var array |
||
| 96 | */ |
||
| 97 | public $downloadUrls = []; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var array |
||
| 101 | */ |
||
| 102 | public $incomingProcInstructions = []; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var array |
||
| 106 | */ |
||
| 107 | public $incomingConfigurationSelection = []; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var bool |
||
| 111 | */ |
||
| 112 | public $registerQueueEntriesInternallyOnly = false; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var array |
||
| 116 | */ |
||
| 117 | public $queueEntries = []; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var array |
||
| 121 | */ |
||
| 122 | public $urlList = []; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var boolean |
||
| 126 | */ |
||
| 127 | public $debugMode = false; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var array |
||
| 131 | */ |
||
| 132 | public $extensionSettings = []; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Mount Point |
||
| 136 | * |
||
| 137 | * @var boolean |
||
| 138 | */ |
||
| 139 | public $MP = false; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @var string |
||
| 143 | */ |
||
| 144 | protected $processFilename; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Holds the internal access mode can be 'gui','cli' or 'cli_im' |
||
| 148 | * |
||
| 149 | * @var string |
||
| 150 | */ |
||
| 151 | protected $accessMode; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @var DatabaseConnection |
||
| 155 | */ |
||
| 156 | private $db; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @var BackendUserAuthentication |
||
| 160 | */ |
||
| 161 | private $backendUser; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @var integer |
||
| 165 | */ |
||
| 166 | private $scheduledTime = 0; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @var integer |
||
| 170 | */ |
||
| 171 | private $reqMinute = 0; |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @var bool |
||
| 175 | */ |
||
| 176 | private $submitCrawlUrls = false; |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @var bool |
||
| 180 | */ |
||
| 181 | private $downloadCrawlUrls = false; |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @var QueueRepository |
||
| 185 | */ |
||
| 186 | protected $queueRepository; |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @var ProcessRepository |
||
| 190 | */ |
||
| 191 | protected $processRepository; |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @var ConfigurationRepository |
||
| 195 | */ |
||
| 196 | protected $configurationRepository; |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Method to set the accessMode can be gui, cli or cli_im |
||
| 200 | * |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | 1 | public function getAccessMode() |
|
| 204 | { |
||
| 205 | 1 | return $this->accessMode; |
|
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param string $accessMode |
||
| 210 | */ |
||
| 211 | 1 | public function setAccessMode($accessMode) |
|
| 212 | { |
||
| 213 | 1 | $this->accessMode = $accessMode; |
|
| 214 | 1 | } |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Set disabled status to prevent processes from being processed |
||
| 218 | * |
||
| 219 | * @param bool $disabled (optional, defaults to true) |
||
| 220 | * @return void |
||
| 221 | */ |
||
| 222 | 3 | public function setDisabled($disabled = true) |
|
| 223 | { |
||
| 224 | 3 | if ($disabled) { |
|
| 225 | 2 | GeneralUtility::writeFile($this->processFilename, ''); |
|
| 226 | } else { |
||
| 227 | 1 | if (is_file($this->processFilename)) { |
|
| 228 | 1 | unlink($this->processFilename); |
|
| 229 | } |
||
| 230 | } |
||
| 231 | 3 | } |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Get disable status |
||
| 235 | * |
||
| 236 | * @return bool true if disabled |
||
| 237 | */ |
||
| 238 | 3 | public function getDisabled() |
|
| 239 | { |
||
| 240 | 3 | if (is_file($this->processFilename)) { |
|
| 241 | 2 | return true; |
|
| 242 | } else { |
||
| 243 | 1 | return false; |
|
| 244 | } |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param string $filenameWithPath |
||
| 249 | * |
||
| 250 | * @return void |
||
| 251 | */ |
||
| 252 | 4 | public function setProcessFilename($filenameWithPath) |
|
| 253 | { |
||
| 254 | 4 | $this->processFilename = $filenameWithPath; |
|
| 255 | 4 | } |
|
| 256 | |||
| 257 | /** |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | 1 | public function getProcessFilename() |
|
| 261 | { |
||
| 262 | 1 | return $this->processFilename; |
|
| 263 | } |
||
| 264 | |||
| 265 | /************************************ |
||
| 266 | * |
||
| 267 | * Getting URLs based on Page TSconfig |
||
| 268 | * |
||
| 269 | ************************************/ |
||
| 270 | |||
| 271 | 47 | public function __construct() |
|
| 272 | { |
||
| 273 | 47 | $objectManager = GeneralUtility::makeInstance(ObjectManager::class); |
|
| 274 | 47 | $this->queueRepository = $objectManager->get(QueueRepository::class); |
|
| 275 | 47 | $this->configurationRepository = $objectManager->get(ConfigurationRepository::class); |
|
| 276 | 47 | $this->processRepository = $objectManager->get(ProcessRepository::class); |
|
| 277 | |||
| 278 | 47 | $this->db = $GLOBALS['TYPO3_DB']; |
|
| 279 | 47 | $this->backendUser = $GLOBALS['BE_USER']; |
|
| 280 | 47 | $this->processFilename = PATH_site . 'typo3temp/tx_crawler.proc'; |
|
| 281 | |||
| 282 | 47 | $settings = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['crawler']); |
|
| 283 | 47 | $settings = is_array($settings) ? $settings : []; |
|
| 284 | |||
| 285 | // read ext_em_conf_template settings and set |
||
| 286 | 47 | $this->setExtensionSettings($settings); |
|
| 287 | |||
| 288 | // set defaults: |
||
| 289 | 47 | if (MathUtility::convertToPositiveInteger($this->extensionSettings['countInARun']) == 0) { |
|
| 290 | 40 | $this->extensionSettings['countInARun'] = 100; |
|
| 291 | } |
||
| 292 | |||
| 293 | 47 | $this->extensionSettings['processLimit'] = MathUtility::forceIntegerInRange($this->extensionSettings['processLimit'], 1, 99, 1); |
|
| 294 | 47 | } |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Sets the extensions settings (unserialized pendant of $TYPO3_CONF_VARS['EXT']['extConf']['crawler']). |
||
| 298 | * |
||
| 299 | * @param array $extensionSettings |
||
| 300 | * @return void |
||
| 301 | */ |
||
| 302 | 56 | public function setExtensionSettings(array $extensionSettings) |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Check if the given page should be crawled |
||
| 309 | * |
||
| 310 | * @param array $pageRow |
||
| 311 | * @return false|string false if the page should be crawled (not excluded), true / skipMessage if it should be skipped |
||
| 312 | */ |
||
| 313 | 10 | public function checkIfPageShouldBeSkipped(array $pageRow) |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Wrapper method for getUrlsForPageId() |
||
| 373 | * It returns an array of configurations and no urls! |
||
| 374 | * |
||
| 375 | * @param array $pageRow Page record with at least dok-type and uid columns. |
||
| 376 | * @param string $skipMessage |
||
| 377 | * @return array |
||
| 378 | * @see getUrlsForPageId() |
||
| 379 | */ |
||
| 380 | 6 | public function getUrlsForPageRow(array $pageRow, &$skipMessage = '') |
|
| 395 | |||
| 396 | /** |
||
| 397 | * This method is used to count if there are ANY unprocessed queue entries |
||
| 398 | * of a given page_id and the configuration which matches a given hash. |
||
| 399 | * If there if none, we can skip an inner detail check |
||
| 400 | * |
||
| 401 | * @param int $uid |
||
| 402 | * @param string $configurationHash |
||
| 403 | * @return boolean |
||
| 404 | */ |
||
| 405 | 7 | protected function noUnprocessedQueueEntriesForPageWithConfigurationHashExist($uid, $configurationHash) |
|
| 406 | { |
||
| 407 | 7 | $configurationHash = $this->db->fullQuoteStr($configurationHash, 'tx_crawler_queue'); |
|
| 408 | 7 | $res = $this->db->exec_SELECTquery('count(*) as anz', 'tx_crawler_queue', "page_id=" . intval($uid) . " AND configuration_hash=" . $configurationHash . " AND exec_time=0"); |
|
| 409 | 7 | $row = $this->db->sql_fetch_assoc($res); |
|
| 410 | |||
| 411 | 7 | return ($row['anz'] == 0); |
|
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Creates a list of URLs from input array (and submits them to queue if asked for) |
||
| 416 | * See Web > Info module script + "indexed_search"'s crawler hook-client using this! |
||
| 417 | * |
||
| 418 | * @param array Information about URLs from pageRow to crawl. |
||
| 419 | * @param array Page row |
||
| 420 | * @param integer Unix time to schedule indexing to, typically time() |
||
| 421 | * @param integer Number of requests per minute (creates the interleave between requests) |
||
| 422 | * @param boolean If set, submits the URLs to queue |
||
| 423 | * @param boolean If set (and submitcrawlUrls is false) will fill $downloadUrls with entries) |
||
| 424 | * @param array Array which is passed by reference and contains the an id per url to secure we will not crawl duplicates |
||
| 425 | * @param array Array which will be filled with URLS for download if flag is set. |
||
| 426 | * @param array Array of processing instructions |
||
| 427 | * @return string List of URLs (meant for display in backend module) |
||
| 428 | * |
||
| 429 | */ |
||
| 430 | 4 | public function urlListFromUrlArray( |
|
| 431 | array $vv, |
||
| 432 | array $pageRow, |
||
| 433 | $scheduledTime, |
||
| 434 | $reqMinute, |
||
| 435 | $submitCrawlUrls, |
||
| 436 | $downloadCrawlUrls, |
||
| 437 | array &$duplicateTrack, |
||
| 438 | array &$downloadUrls, |
||
| 439 | array $incomingProcInstructions |
||
| 440 | ) { |
||
| 441 | 4 | $urlList = ''; |
|
| 442 | // realurl support (thanks to Ingo Renner) |
||
| 443 | 4 | if (ExtensionManagementUtility::isLoaded('realurl') && $vv['subCfg']['realurl']) { |
|
| 444 | |||
| 445 | /** @var tx_realurl $urlObj */ |
||
| 446 | $urlObj = GeneralUtility::makeInstance('tx_realurl'); |
||
| 447 | |||
| 448 | if (!empty($vv['subCfg']['baseUrl'])) { |
||
| 449 | $urlParts = parse_url($vv['subCfg']['baseUrl']); |
||
| 450 | $host = strtolower($urlParts['host']); |
||
| 451 | $urlObj->host = $host; |
||
| 452 | |||
| 453 | // First pass, finding configuration OR pointer string: |
||
| 454 | $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']; |
||
| 455 | |||
| 456 | // If it turned out to be a string pointer, then look up the real config: |
||
| 457 | if (is_string($urlObj->extConf)) { |
||
| 458 | $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']; |
||
| 459 | } |
||
| 460 | } |
||
| 461 | |||
| 462 | if (!$GLOBALS['TSFE']->sys_page) { |
||
| 463 | $GLOBALS['TSFE']->sys_page = GeneralUtility::makeInstance('TYPO3\CMS\Frontend\Page\PageRepository'); |
||
| 464 | } |
||
| 465 | |||
| 466 | if (!$GLOBALS['TSFE']->tmpl->rootLine[0]['uid']) { |
||
| 467 | $GLOBALS['TSFE']->tmpl->rootLine[0]['uid'] = $urlObj->extConf['pagePath']['rootpage_id']; |
||
| 468 | } |
||
| 469 | } |
||
| 470 | |||
| 471 | 4 | if (is_array($vv['URLs'])) { |
|
| 472 | 4 | $configurationHash = $this->getConfigurationHash($vv); |
|
| 473 | 4 | $skipInnerCheck = $this->noUnprocessedQueueEntriesForPageWithConfigurationHashExist($pageRow['uid'], $configurationHash); |
|
| 474 | |||
| 475 | 4 | foreach ($vv['URLs'] as $urlQuery) { |
|
| 476 | 4 | if ($this->drawURLs_PIfilter($vv['subCfg']['procInstrFilter'], $incomingProcInstructions)) { |
|
| 477 | |||
| 478 | // Calculate cHash: |
||
| 479 | 4 | if ($vv['subCfg']['cHash']) { |
|
| 480 | /* @var $cacheHash \TYPO3\CMS\Frontend\Page\CacheHashCalculator */ |
||
| 481 | $cacheHash = GeneralUtility::makeInstance('TYPO3\CMS\Frontend\Page\CacheHashCalculator'); |
||
| 482 | $urlQuery .= '&cHash=' . $cacheHash->generateForParameters($urlQuery); |
||
| 483 | } |
||
| 484 | |||
| 485 | // Create key by which to determine unique-ness: |
||
| 486 | 4 | $uKey = $urlQuery . '|' . $vv['subCfg']['userGroups'] . '|' . $vv['subCfg']['baseUrl'] . '|' . $vv['subCfg']['procInstrFilter']; |
|
| 487 | |||
| 488 | // realurl support (thanks to Ingo Renner) |
||
| 489 | 4 | $urlQuery = 'index.php' . $urlQuery; |
|
| 490 | 4 | if (ExtensionManagementUtility::isLoaded('realurl') && $vv['subCfg']['realurl']) { |
|
| 491 | $params = [ |
||
| 492 | 'LD' => [ |
||
| 493 | 'totalURL' => $urlQuery, |
||
| 494 | ], |
||
| 495 | 'TCEmainHook' => true, |
||
| 496 | ]; |
||
| 497 | $urlObj->encodeSpURL($params); |
||
|
|
|||
| 498 | $urlQuery = $params['LD']['totalURL']; |
||
| 499 | } |
||
| 500 | |||
| 501 | // Scheduled time: |
||
| 502 | 4 | $schTime = $scheduledTime + round(count($duplicateTrack) * (60 / $reqMinute)); |
|
| 503 | 4 | $schTime = floor($schTime / 60) * 60; |
|
| 504 | |||
| 505 | 4 | if (isset($duplicateTrack[$uKey])) { |
|
| 506 | |||
| 507 | //if the url key is registered just display it and do not resubmit is |
||
| 508 | $urlList = '<em><span class="typo3-dimmed">' . htmlspecialchars($urlQuery) . '</span></em><br/>'; |
||
| 509 | } else { |
||
| 510 | 4 | $urlList = '[' . date('d.m.y H:i', $schTime) . '] ' . htmlspecialchars($urlQuery); |
|
| 511 | 4 | $this->urlList[] = '[' . date('d.m.y H:i', $schTime) . '] ' . $urlQuery; |
|
| 512 | |||
| 513 | 4 | $theUrl = ($vv['subCfg']['baseUrl'] ? $vv['subCfg']['baseUrl'] : GeneralUtility::getIndpEnv('TYPO3_SITE_URL')) . $urlQuery; |
|
| 514 | |||
| 515 | // Submit for crawling! |
||
| 516 | 4 | if ($submitCrawlUrls) { |
|
| 517 | 4 | $added = $this->addUrl( |
|
| 518 | 4 | $pageRow['uid'], |
|
| 519 | 4 | $theUrl, |
|
| 520 | 4 | $vv['subCfg'], |
|
| 521 | 4 | $scheduledTime, |
|
| 522 | 4 | $configurationHash, |
|
| 523 | 4 | $skipInnerCheck |
|
| 524 | ); |
||
| 525 | 4 | if ($added === false) { |
|
| 526 | 4 | $urlList .= ' (Url already existed)'; |
|
| 527 | } |
||
| 528 | } elseif ($downloadCrawlUrls) { |
||
| 529 | $downloadUrls[$theUrl] = $theUrl; |
||
| 530 | } |
||
| 531 | |||
| 532 | 4 | $urlList .= '<br />'; |
|
| 533 | } |
||
| 534 | 4 | $duplicateTrack[$uKey] = true; |
|
| 535 | } |
||
| 536 | } |
||
| 537 | } else { |
||
| 538 | $urlList = 'ERROR - no URL generated'; |
||
| 539 | } |
||
| 540 | |||
| 541 | 4 | return $urlList; |
|
| 542 | } |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Returns true if input processing instruction is among registered ones. |
||
| 546 | * |
||
| 547 | * @param string $piString PI to test |
||
| 548 | * @param array $incomingProcInstructions Processing instructions |
||
| 549 | * @return boolean |
||
| 550 | */ |
||
| 551 | 5 | public function drawURLs_PIfilter($piString, array $incomingProcInstructions) |
|
| 552 | { |
||
| 553 | 5 | if (empty($incomingProcInstructions)) { |
|
| 554 | 1 | return true; |
|
| 555 | } |
||
| 556 | |||
| 557 | 4 | foreach ($incomingProcInstructions as $pi) { |
|
| 558 | 4 | if (GeneralUtility::inList($piString, $pi)) { |
|
| 559 | 4 | return true; |
|
| 560 | } |
||
| 561 | } |
||
| 562 | 2 | } |
|
| 563 | |||
| 564 | 5 | public function getPageTSconfigForId($id) |
|
| 565 | { |
||
| 566 | 5 | if (!$this->MP) { |
|
| 567 | 5 | $pageTSconfig = BackendUtility::getPagesTSconfig($id); |
|
| 568 | } else { |
||
| 569 | list(, $mountPointId) = explode('-', $this->MP); |
||
| 570 | $pageTSconfig = BackendUtility::getPagesTSconfig($mountPointId); |
||
| 571 | } |
||
| 572 | |||
| 573 | // Call a hook to alter configuration |
||
| 574 | 5 | if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['getPageTSconfigForId'])) { |
|
| 575 | $params = [ |
||
| 576 | 'pageId' => $id, |
||
| 577 | 'pageTSConfig' => &$pageTSconfig, |
||
| 578 | ]; |
||
| 579 | foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['getPageTSconfigForId'] as $userFunc) { |
||
| 580 | GeneralUtility::callUserFunction($userFunc, $params, $this); |
||
| 581 | } |
||
| 582 | } |
||
| 583 | |||
| 584 | 5 | return $pageTSconfig; |
|
| 585 | } |
||
| 586 | |||
| 587 | /** |
||
| 588 | * This methods returns an array of configurations. |
||
| 589 | * And no urls! |
||
| 590 | * |
||
| 591 | * @param integer $id Page ID |
||
| 592 | * @param bool $forceSsl Use https |
||
| 593 | * @return array |
||
| 594 | * |
||
| 595 | * TODO: Should be switched back to protected - TNM 2018-11-16 |
||
| 596 | */ |
||
| 597 | 4 | public function getUrlsForPageId($id, $forceSsl = false) |
|
| 598 | { |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Get configuration from tsConfig |
||
| 602 | */ |
||
| 603 | |||
| 604 | // Get page TSconfig for page ID: |
||
| 605 | 4 | $pageTSconfig = $this->getPageTSconfigForId($id); |
|
| 606 | |||
| 607 | 4 | $res = []; |
|
| 608 | |||
| 609 | 4 | if (is_array($pageTSconfig) && is_array($pageTSconfig['tx_crawler.']['crawlerCfg.'])) { |
|
| 610 | 3 | $crawlerCfg = $pageTSconfig['tx_crawler.']['crawlerCfg.']; |
|
| 611 | |||
| 612 | 3 | if (is_array($crawlerCfg['paramSets.'])) { |
|
| 613 | 3 | foreach ($crawlerCfg['paramSets.'] as $key => $values) { |
|
| 614 | 3 | if (is_array($values)) { |
|
| 615 | 3 | $key = str_replace('.', '', $key); |
|
| 616 | // Sub configuration for a single configuration string: |
||
| 617 | 3 | $subCfg = (array)$crawlerCfg['paramSets.'][$key . '.']; |
|
| 618 | 3 | $subCfg['key'] = $key; |
|
| 619 | |||
| 620 | 3 | if (strcmp($subCfg['procInstrFilter'], '')) { |
|
| 621 | 3 | $subCfg['procInstrFilter'] = implode(',', GeneralUtility::trimExplode(',', $subCfg['procInstrFilter'])); |
|
| 622 | } |
||
| 623 | 3 | $pidOnlyList = implode(',', GeneralUtility::trimExplode(',', $subCfg['pidsOnly'], true)); |
|
| 624 | |||
| 625 | // process configuration if it is not page-specific or if the specific page is the current page: |
||
| 626 | 3 | if (!strcmp($subCfg['pidsOnly'], '') || GeneralUtility::inList($pidOnlyList, $id)) { |
|
| 627 | |||
| 628 | // add trailing slash if not present |
||
| 629 | 3 | if (!empty($subCfg['baseUrl']) && substr($subCfg['baseUrl'], -1) != '/') { |
|
| 630 | $subCfg['baseUrl'] .= '/'; |
||
| 631 | } |
||
| 632 | |||
| 633 | // Explode, process etc.: |
||
| 634 | 3 | $res[$key] = []; |
|
| 635 | 3 | $res[$key]['subCfg'] = $subCfg; |
|
| 636 | 3 | $res[$key]['paramParsed'] = $this->parseParams($crawlerCfg['paramSets.'][$key]); |
|
| 637 | 3 | $res[$key]['paramExpanded'] = $this->expandParameters($res[$key]['paramParsed'], $id); |
|
| 638 | 3 | $res[$key]['origin'] = 'pagets'; |
|
| 639 | |||
| 640 | // recognize MP value |
||
| 641 | 3 | if (!$this->MP) { |
|
| 642 | 3 | $res[$key]['URLs'] = $this->compileUrls($res[$key]['paramExpanded'], ['?id=' . $id]); |
|
| 643 | } else { |
||
| 644 | 3 | $res[$key]['URLs'] = $this->compileUrls($res[$key]['paramExpanded'], ['?id=' . $id . '&MP=' . $this->MP]); |
|
| 645 | } |
||
| 646 | } |
||
| 647 | } |
||
| 648 | } |
||
| 649 | } |
||
| 650 | } |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Get configuration from tx_crawler_configuration records |
||
| 654 | */ |
||
| 655 | |||
| 656 | // get records along the rootline |
||
| 657 | 4 | $rootLine = BackendUtility::BEgetRootLine($id); |
|
| 658 | 4 | foreach ($rootLine as $page) { |
|
| 659 | 4 | $configurationRecordsForCurrentPage = $this->configurationRepository->getConfigurationRecordsPageUid($page['uid'])->toArray(); |
|
| 660 | |||
| 661 | /** @var Configuration $configurationRecord */ |
||
| 662 | 4 | foreach ($configurationRecordsForCurrentPage as $configurationRecord) { |
|
| 663 | |||
| 664 | // check access to the configuration record |
||
| 665 | 1 | if (empty($configurationRecord->getBeGroups()) || $GLOBALS['BE_USER']->isAdmin() || $this->hasGroupAccess($GLOBALS['BE_USER']->user['usergroup_cached_list'], $configurationRecord->getBeGroups())) { |
|
| 666 | 1 | $pidOnlyList = implode(',', GeneralUtility::trimExplode(',', $configurationRecord->getPidsOnly(), true)); |
|
| 667 | |||
| 668 | // process configuration if it is not page-specific or if the specific page is the current page: |
||
| 669 | 1 | if (!strcmp($configurationRecord->getPidsOnly(), '') || GeneralUtility::inList($pidOnlyList, $id)) { |
|
| 670 | 1 | $key = $configurationRecord->getName(); |
|
| 671 | |||
| 672 | // don't overwrite previously defined paramSets |
||
| 673 | 1 | if (!isset($res[$key])) { |
|
| 674 | |||
| 675 | /* @var $TSparserObject \TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser */ |
||
| 676 | 1 | $TSparserObject = GeneralUtility::makeInstance('TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser'); |
|
| 677 | // Todo: Check where the field processing_instructions_parameters_ts comes from. |
||
| 678 | 1 | $TSparserObject->parse($configurationRecord->getProcessingInstructionFilter()); //['processing_instruction_parameters_ts']); |
|
| 679 | |||
| 680 | 1 | $isCrawlingProtocolHttps = $this->isCrawlingProtocolHttps($configurationRecord->isForceSsl(), $forceSsl); |
|
| 681 | |||
| 682 | $subCfg = [ |
||
| 683 | 1 | 'procInstrFilter' => $configurationRecord->getProcessingInstructionFilter(), |
|
| 684 | 1 | 'procInstrParams.' => $TSparserObject->setup, |
|
| 685 | 1 | 'baseUrl' => $this->getBaseUrlForConfigurationRecord( |
|
| 686 | 1 | $configurationRecord->getBaseUrl(), |
|
| 687 | 1 | $configurationRecord->getSysDomainBaseUrl(), |
|
| 688 | 1 | $isCrawlingProtocolHttps |
|
| 689 | ), |
||
| 690 | 1 | 'realurl' => $configurationRecord->getRealUrl(), |
|
| 691 | 1 | 'cHash' => $configurationRecord->getCHash(), |
|
| 692 | 1 | 'userGroups' => $configurationRecord->getFeGroups(), |
|
| 693 | 1 | 'exclude' => $configurationRecord->getExclude(), |
|
| 694 | 1 | 'rootTemplatePid' => (int)$configurationRecord->getRootTemplatePid(), |
|
| 695 | 1 | 'key' => $key, |
|
| 696 | ]; |
||
| 697 | |||
| 698 | // add trailing slash if not present |
||
| 699 | 1 | if (!empty($subCfg['baseUrl']) && substr($subCfg['baseUrl'], -1) != '/') { |
|
| 700 | $subCfg['baseUrl'] .= '/'; |
||
| 701 | } |
||
| 702 | 1 | if (!in_array($id, $this->expandExcludeString($subCfg['exclude']))) { |
|
| 703 | 1 | $res[$key] = []; |
|
| 704 | 1 | $res[$key]['subCfg'] = $subCfg; |
|
| 705 | 1 | $res[$key]['paramParsed'] = $this->parseParams($configurationRecord->getConfiguration()); |
|
| 706 | 1 | $res[$key]['paramExpanded'] = $this->expandParameters($res[$key]['paramParsed'], $id); |
|
| 707 | 1 | $res[$key]['URLs'] = $this->compileUrls($res[$key]['paramExpanded'], ['?id=' . $id]); |
|
| 708 | 4 | $res[$key]['origin'] = 'tx_crawler_configuration_' . $configurationRecord->getUid(); |
|
| 709 | } |
||
| 710 | } |
||
| 711 | } |
||
| 712 | } |
||
| 713 | } |
||
| 714 | } |
||
| 715 | |||
| 716 | 4 | if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['processUrls'])) { |
|
| 717 | foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['processUrls'] as $func) { |
||
| 718 | $params = [ |
||
| 719 | 'res' => &$res, |
||
| 720 | ]; |
||
| 721 | GeneralUtility::callUserFunction($func, $params, $this); |
||
| 722 | } |
||
| 723 | } |
||
| 724 | |||
| 725 | 4 | return $res; |
|
| 726 | } |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Checks if a domain record exist and returns the base-url based on the record. If not the given baseUrl string is used. |
||
| 730 | * |
||
| 731 | * @param string $baseUrl |
||
| 732 | * @param integer $sysDomainUid |
||
| 733 | * @param bool $ssl |
||
| 734 | * @return string |
||
| 735 | */ |
||
| 736 | 4 | protected function getBaseUrlForConfigurationRecord($baseUrl, $sysDomainUid, $ssl = false) |
|
| 737 | { |
||
| 738 | 4 | $sysDomainUid = intval($sysDomainUid); |
|
| 739 | 4 | $urlScheme = ($ssl === false) ? 'http' : 'https'; |
|
| 740 | |||
| 741 | 4 | if ($sysDomainUid > 0) { |
|
| 742 | 2 | $res = $this->db->exec_SELECTquery( |
|
| 743 | 2 | '*', |
|
| 744 | 2 | 'sys_domain', |
|
| 745 | 2 | 'uid = ' . $sysDomainUid . |
|
| 746 | 2 | BackendUtility::BEenableFields('sys_domain') . |
|
| 747 | 2 | BackendUtility::deleteClause('sys_domain') |
|
| 748 | ); |
||
| 749 | 2 | $row = $this->db->sql_fetch_assoc($res); |
|
| 750 | 2 | if ($row['domainName'] != '') { |
|
| 751 | 1 | return $urlScheme . '://' . $row['domainName']; |
|
| 752 | } |
||
| 753 | } |
||
| 754 | 3 | return $baseUrl; |
|
| 755 | } |
||
| 756 | |||
| 757 | 1 | public function getConfigurationsForBranch($rootid, $depth) |
|
| 758 | { |
||
| 759 | 1 | $configurationsForBranch = []; |
|
| 760 | |||
| 761 | 1 | $pageTSconfig = $this->getPageTSconfigForId($rootid); |
|
| 762 | 1 | if (is_array($pageTSconfig) && is_array($pageTSconfig['tx_crawler.']['crawlerCfg.']) && is_array($pageTSconfig['tx_crawler.']['crawlerCfg.']['paramSets.'])) { |
|
| 763 | $sets = $pageTSconfig['tx_crawler.']['crawlerCfg.']['paramSets.']; |
||
| 764 | if (is_array($sets)) { |
||
| 765 | foreach ($sets as $key => $value) { |
||
| 766 | if (!is_array($value)) { |
||
| 767 | continue; |
||
| 768 | } |
||
| 769 | $configurationsForBranch[] = substr($key, -1) == '.' ? substr($key, 0, -1) : $key; |
||
| 770 | } |
||
| 771 | } |
||
| 772 | } |
||
| 773 | 1 | $pids = []; |
|
| 774 | 1 | $rootLine = BackendUtility::BEgetRootLine($rootid); |
|
| 775 | 1 | foreach ($rootLine as $node) { |
|
| 776 | 1 | $pids[] = $node['uid']; |
|
| 777 | } |
||
| 778 | /* @var PageTreeView $tree */ |
||
| 779 | 1 | $tree = GeneralUtility::makeInstance(PageTreeView::class); |
|
| 780 | 1 | $perms_clause = $GLOBALS['BE_USER']->getPagePermsClause(1); |
|
| 781 | 1 | $tree->init('AND ' . $perms_clause); |
|
| 782 | 1 | $tree->getTree($rootid, $depth, ''); |
|
| 783 | 1 | foreach ($tree->tree as $node) { |
|
| 784 | $pids[] = $node['row']['uid']; |
||
| 785 | } |
||
| 786 | |||
| 787 | 1 | $res = $this->db->exec_SELECTquery( |
|
| 788 | 1 | '*', |
|
| 789 | 1 | 'tx_crawler_configuration', |
|
| 790 | 1 | 'pid IN (' . implode(',', $pids) . ') ' . |
|
| 791 | 1 | BackendUtility::BEenableFields('tx_crawler_configuration') . |
|
| 792 | 1 | BackendUtility::deleteClause('tx_crawler_configuration') . ' ' . |
|
| 793 | 1 | BackendUtility::versioningPlaceholderClause('tx_crawler_configuration') . ' ' |
|
| 794 | ); |
||
| 795 | |||
| 796 | 1 | while ($row = $this->db->sql_fetch_assoc($res)) { |
|
| 797 | 1 | $configurationsForBranch[] = $row['name']; |
|
| 798 | } |
||
| 799 | 1 | $this->db->sql_free_result($res); |
|
| 800 | 1 | return $configurationsForBranch; |
|
| 801 | } |
||
| 802 | |||
| 803 | /** |
||
| 804 | * Check if a user has access to an item |
||
| 805 | * (e.g. get the group list of the current logged in user from $GLOBALS['TSFE']->gr_list) |
||
| 806 | * |
||
| 807 | * @see \TYPO3\CMS\Frontend\Page\PageRepository::getMultipleGroupsWhereClause() |
||
| 808 | * @param string $groupList Comma-separated list of (fe_)group UIDs from a user |
||
| 809 | * @param string $accessList Comma-separated list of (fe_)group UIDs of the item to access |
||
| 810 | * @return bool TRUE if at least one of the users group UIDs is in the access list or the access list is empty |
||
| 811 | */ |
||
| 812 | 3 | public function hasGroupAccess($groupList, $accessList) |
|
| 813 | { |
||
| 814 | 3 | if (empty($accessList)) { |
|
| 815 | 1 | return true; |
|
| 816 | } |
||
| 817 | 2 | foreach (GeneralUtility::intExplode(',', $groupList) as $groupUid) { |
|
| 818 | 2 | if (GeneralUtility::inList($accessList, $groupUid)) { |
|
| 819 | 2 | return true; |
|
| 820 | } |
||
| 821 | } |
||
| 822 | 1 | return false; |
|
| 823 | } |
||
| 824 | |||
| 825 | /** |
||
| 826 | * Parse GET vars of input Query into array with key=>value pairs |
||
| 827 | * |
||
| 828 | * @param string $inputQuery Input query string |
||
| 829 | * @return array |
||
| 830 | */ |
||
| 831 | 7 | public function parseParams($inputQuery) |
|
| 832 | { |
||
| 833 | // Extract all GET parameters into an ARRAY: |
||
| 834 | 7 | $paramKeyValues = []; |
|
| 835 | 7 | $GETparams = explode('&', $inputQuery); |
|
| 836 | |||
| 837 | 7 | foreach ($GETparams as $paramAndValue) { |
|
| 838 | 7 | list($p, $v) = explode('=', $paramAndValue, 2); |
|
| 839 | 7 | if (strlen($p)) { |
|
| 840 | 7 | $paramKeyValues[rawurldecode($p)] = rawurldecode($v); |
|
| 841 | } |
||
| 842 | } |
||
| 843 | |||
| 844 | 7 | return $paramKeyValues; |
|
| 845 | } |
||
| 846 | |||
| 847 | /** |
||
| 848 | * Will expand the parameters configuration to individual values. This follows a certain syntax of the value of each parameter. |
||
| 849 | * Syntax of values: |
||
| 850 | * - Basically: If the value is wrapped in [...] it will be expanded according to the following syntax, otherwise the value is taken literally |
||
| 851 | * - Configuration is splitted by "|" and the parts are processed individually and finally added together |
||
| 852 | * - For each configuration part: |
||
| 853 | * - "[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" |
||
| 854 | * - "_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" |
||
| 855 | * _ENABLELANG:1 picks only original records without their language overlays |
||
| 856 | * - Default: Literal value |
||
| 857 | * |
||
| 858 | * @param array $paramArray Array with key (GET var name) and values (value of GET var which is configuration for expansion) |
||
| 859 | * @param integer $pid Current page ID |
||
| 860 | * @return array |
||
| 861 | */ |
||
| 862 | 8 | public function expandParameters($paramArray, $pid) |
|
| 863 | { |
||
| 864 | 8 | global $TCA; |
|
| 865 | |||
| 866 | // Traverse parameter names: |
||
| 867 | 8 | foreach ($paramArray as $p => $v) { |
|
| 868 | 8 | $v = trim($v); |
|
| 869 | |||
| 870 | // If value is encapsulated in square brackets it means there are some ranges of values to find, otherwise the value is literal |
||
| 871 | 8 | if (substr($v, 0, 1) === '[' && substr($v, -1) === ']') { |
|
| 872 | // So, find the value inside brackets and reset the paramArray value as an array. |
||
| 873 | 8 | $v = substr($v, 1, -1); |
|
| 874 | 8 | $paramArray[$p] = []; |
|
| 875 | |||
| 876 | // Explode parts and traverse them: |
||
| 877 | 8 | $parts = explode('|', $v); |
|
| 878 | 8 | foreach ($parts as $pV) { |
|
| 879 | |||
| 880 | // Look for integer range: (fx. 1-34 or -40--30 // reads minus 40 to minus 30) |
||
| 881 | 8 | if (preg_match('/^(-?[0-9]+)\s*-\s*(-?[0-9]+)$/', trim($pV), $reg)) { |
|
| 882 | |||
| 883 | // Swap if first is larger than last: |
||
| 884 | 1 | if ($reg[1] > $reg[2]) { |
|
| 885 | $temp = $reg[2]; |
||
| 886 | $reg[2] = $reg[1]; |
||
| 887 | $reg[1] = $temp; |
||
| 888 | } |
||
| 889 | |||
| 890 | // Traverse range, add values: |
||
| 891 | 1 | $runAwayBrake = 1000; // Limit to size of range! |
|
| 892 | 1 | for ($a = $reg[1]; $a <= $reg[2];$a++) { |
|
| 893 | 1 | $paramArray[$p][] = $a; |
|
| 894 | 1 | $runAwayBrake--; |
|
| 895 | 1 | if ($runAwayBrake <= 0) { |
|
| 896 | break; |
||
| 897 | } |
||
| 898 | } |
||
| 899 | 7 | } elseif (substr(trim($pV), 0, 7) == '_TABLE:') { |
|
| 900 | |||
| 901 | // Parse parameters: |
||
| 902 | 3 | $subparts = GeneralUtility::trimExplode(';', $pV); |
|
| 903 | 3 | $subpartParams = []; |
|
| 904 | 3 | foreach ($subparts as $spV) { |
|
| 905 | 3 | list($pKey, $pVal) = GeneralUtility::trimExplode(':', $spV); |
|
| 906 | 3 | $subpartParams[$pKey] = $pVal; |
|
| 907 | } |
||
| 908 | |||
| 909 | // Table exists: |
||
| 910 | 3 | if (isset($TCA[$subpartParams['_TABLE']])) { |
|
| 911 | 3 | $lookUpPid = isset($subpartParams['_PID']) ? intval($subpartParams['_PID']) : $pid; |
|
| 912 | 3 | $pidField = isset($subpartParams['_PIDFIELD']) ? trim($subpartParams['_PIDFIELD']) : 'pid'; |
|
| 913 | 3 | $where = isset($subpartParams['_WHERE']) ? $subpartParams['_WHERE'] : ''; |
|
| 914 | 3 | $addTable = isset($subpartParams['_ADDTABLE']) ? $subpartParams['_ADDTABLE'] : ''; |
|
| 915 | |||
| 916 | 3 | $fieldName = $subpartParams['_FIELD'] ? $subpartParams['_FIELD'] : 'uid'; |
|
| 917 | 3 | if ($fieldName === 'uid' || $TCA[$subpartParams['_TABLE']]['columns'][$fieldName]) { |
|
| 918 | 3 | $andWhereLanguage = ''; |
|
| 919 | 3 | $transOrigPointerField = $TCA[$subpartParams['_TABLE']]['ctrl']['transOrigPointerField']; |
|
| 920 | |||
| 921 | 3 | if ($subpartParams['_ENABLELANG'] && $transOrigPointerField) { |
|
| 922 | $andWhereLanguage = ' AND ' . $this->db->quoteStr($transOrigPointerField, $subpartParams['_TABLE']) . ' <= 0 '; |
||
| 923 | } |
||
| 924 | |||
| 925 | 3 | $where = $this->db->quoteStr($pidField, $subpartParams['_TABLE']) . '=' . intval($lookUpPid) . ' ' . |
|
| 926 | 3 | $andWhereLanguage . $where; |
|
| 927 | |||
| 928 | 3 | $rows = $this->db->exec_SELECTgetRows( |
|
| 929 | 3 | $fieldName, |
|
| 930 | 3 | $subpartParams['_TABLE'] . $addTable, |
|
| 931 | 3 | $where . BackendUtility::deleteClause($subpartParams['_TABLE']), |
|
| 932 | 3 | '', |
|
| 933 | 3 | '', |
|
| 934 | 3 | '', |
|
| 935 | 3 | $fieldName |
|
| 936 | ); |
||
| 937 | |||
| 938 | 3 | if (is_array($rows)) { |
|
| 939 | 3 | $paramArray[$p] = array_merge($paramArray[$p], array_keys($rows)); |
|
| 940 | } |
||
| 941 | } |
||
| 942 | } |
||
| 943 | } else { // Just add value: |
||
| 944 | 4 | $paramArray[$p][] = $pV; |
|
| 945 | } |
||
| 946 | // Hook for processing own expandParameters place holder |
||
| 947 | 8 | if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['crawler/class.tx_crawler_lib.php']['expandParameters'])) { |
|
| 948 | $_params = [ |
||
| 949 | 'pObj' => &$this, |
||
| 950 | 'paramArray' => &$paramArray, |
||
| 951 | 'currentKey' => $p, |
||
| 952 | 'currentValue' => $pV, |
||
| 953 | 'pid' => $pid, |
||
| 954 | ]; |
||
| 955 | foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['crawler/class.tx_crawler_lib.php']['expandParameters'] as $key => $_funcRef) { |
||
| 956 | 8 | GeneralUtility::callUserFunction($_funcRef, $_params, $this); |
|
| 957 | } |
||
| 958 | } |
||
| 959 | } |
||
| 960 | |||
| 961 | // Make unique set of values and sort array by key: |
||
| 962 | 8 | $paramArray[$p] = array_unique($paramArray[$p]); |
|
| 963 | 8 | ksort($paramArray); |
|
| 964 | } else { |
||
| 965 | // Set the literal value as only value in array: |
||
| 966 | 8 | $paramArray[$p] = [$v]; |
|
| 967 | } |
||
| 968 | } |
||
| 969 | |||
| 970 | 8 | return $paramArray; |
|
| 971 | } |
||
| 972 | |||
| 973 | /** |
||
| 974 | * Compiling URLs from parameter array (output of expandParameters()) |
||
| 975 | * The number of URLs will be the multiplication of the number of parameter values for each key |
||
| 976 | * |
||
| 977 | * @param array $paramArray Output of expandParameters(): Array with keys (GET var names) and for each an array of values |
||
| 978 | * @param array $urls URLs accumulated in this array (for recursion) |
||
| 979 | * @return array |
||
| 980 | */ |
||
| 981 | 7 | public function compileUrls($paramArray, $urls = []) |
|
| 982 | { |
||
| 983 | 7 | if (count($paramArray) && is_array($urls)) { |
|
| 984 | // shift first off stack: |
||
| 985 | 6 | reset($paramArray); |
|
| 986 | 6 | $varName = key($paramArray); |
|
| 987 | 6 | $valueSet = array_shift($paramArray); |
|
| 988 | |||
| 989 | // Traverse value set: |
||
| 990 | 6 | $newUrls = []; |
|
| 991 | 6 | foreach ($urls as $url) { |
|
| 992 | 5 | foreach ($valueSet as $val) { |
|
| 993 | 5 | $newUrls[] = $url . (strcmp($val, '') ? '&' . rawurlencode($varName) . '=' . rawurlencode($val) : ''); |
|
| 994 | |||
| 995 | 5 | if (count($newUrls) > MathUtility::forceIntegerInRange($this->extensionSettings['maxCompileUrls'], 1, 1000000000, 10000)) { |
|
| 996 | 5 | break; |
|
| 997 | } |
||
| 998 | } |
||
| 999 | } |
||
| 1000 | 6 | $urls = $newUrls; |
|
| 1001 | 6 | $urls = $this->compileUrls($paramArray, $urls); |
|
| 1002 | } |
||
| 1003 | |||
| 1004 | 7 | return $urls; |
|
| 1005 | } |
||
| 1006 | |||
| 1007 | /************************************ |
||
| 1008 | * |
||
| 1009 | * Crawler log |
||
| 1010 | * |
||
| 1011 | ************************************/ |
||
| 1012 | |||
| 1013 | /** |
||
| 1014 | * Return array of records from crawler queue for input page ID |
||
| 1015 | * |
||
| 1016 | * @param integer $id Page ID for which to look up log entries. |
||
| 1017 | * @param string$filter Filter: "all" => all entries, "pending" => all that is not yet run, "finished" => all complete ones |
||
| 1018 | * @param boolean $doFlush If TRUE, then entries selected at DELETED(!) instead of selected! |
||
| 1019 | * @param boolean $doFullFlush |
||
| 1020 | * @param integer $itemsPerPage Limit the amount of entries per page default is 10 |
||
| 1021 | * @return array |
||
| 1022 | */ |
||
| 1023 | 4 | public function getLogEntriesForPageId($id, $filter = '', $doFlush = false, $doFullFlush = false, $itemsPerPage = 10) |
|
| 1024 | { |
||
| 1025 | switch ($filter) { |
||
| 1026 | 4 | case 'pending': |
|
| 1027 | $addWhere = ' AND exec_time=0'; |
||
| 1028 | break; |
||
| 1029 | 4 | case 'finished': |
|
| 1030 | $addWhere = ' AND exec_time>0'; |
||
| 1031 | break; |
||
| 1032 | default: |
||
| 1033 | 4 | $addWhere = ''; |
|
| 1034 | 4 | break; |
|
| 1035 | } |
||
| 1036 | |||
| 1037 | // FIXME: Write unit test that ensures that the right records are deleted. |
||
| 1038 | 4 | if ($doFlush) { |
|
| 1039 | 2 | $this->flushQueue(($doFullFlush ? '1=1' : ('page_id=' . intval($id))) . $addWhere); |
|
| 1040 | 2 | return []; |
|
| 1041 | } else { |
||
| 1042 | 2 | return $this->db->exec_SELECTgetRows( |
|
| 1043 | 2 | '*', |
|
| 1044 | 2 | 'tx_crawler_queue', |
|
| 1045 | 2 | 'page_id=' . intval($id) . $addWhere, |
|
| 1046 | 2 | '', |
|
| 1047 | 2 | 'scheduled DESC', |
|
| 1048 | 2 | (intval($itemsPerPage) > 0 ? intval($itemsPerPage) : '') |
|
| 1049 | ); |
||
| 1050 | } |
||
| 1051 | } |
||
| 1052 | |||
| 1053 | /** |
||
| 1054 | * Return array of records from crawler queue for input set ID |
||
| 1055 | * |
||
| 1056 | * @param integer $set_id Set ID for which to look up log entries. |
||
| 1057 | * @param string $filter Filter: "all" => all entries, "pending" => all that is not yet run, "finished" => all complete ones |
||
| 1058 | * @param boolean $doFlush If TRUE, then entries selected at DELETED(!) instead of selected! |
||
| 1059 | * @param integer $itemsPerPage Limit the amount of entires per page default is 10 |
||
| 1060 | * @return array |
||
| 1061 | */ |
||
| 1062 | 6 | public function getLogEntriesForSetId($set_id, $filter = '', $doFlush = false, $doFullFlush = false, $itemsPerPage = 10) |
|
| 1063 | { |
||
| 1064 | // FIXME: Write Unit tests for Filters |
||
| 1065 | switch ($filter) { |
||
| 1066 | 6 | case 'pending': |
|
| 1067 | 1 | $addWhere = ' AND exec_time=0'; |
|
| 1068 | 1 | break; |
|
| 1069 | 5 | case 'finished': |
|
| 1070 | 1 | $addWhere = ' AND exec_time>0'; |
|
| 1071 | 1 | break; |
|
| 1072 | default: |
||
| 1073 | 4 | $addWhere = ''; |
|
| 1074 | 4 | break; |
|
| 1075 | } |
||
| 1076 | // FIXME: Write unit test that ensures that the right records are deleted. |
||
| 1077 | 6 | if ($doFlush) { |
|
| 1078 | 4 | $this->flushQueue($doFullFlush ? '' : ('set_id=' . intval($set_id) . $addWhere)); |
|
| 1079 | 4 | return []; |
|
| 1080 | } else { |
||
| 1081 | 2 | return $this->db->exec_SELECTgetRows( |
|
| 1082 | 2 | '*', |
|
| 1083 | 2 | 'tx_crawler_queue', |
|
| 1084 | 2 | 'set_id=' . intval($set_id) . $addWhere, |
|
| 1085 | 2 | '', |
|
| 1086 | 2 | 'scheduled DESC', |
|
| 1087 | 2 | (intval($itemsPerPage) > 0 ? intval($itemsPerPage) : '') |
|
| 1088 | ); |
||
| 1089 | } |
||
| 1090 | } |
||
| 1091 | |||
| 1092 | /** |
||
| 1093 | * Removes queue entries |
||
| 1094 | * |
||
| 1095 | * @param string $where SQL related filter for the entries which should be removed |
||
| 1096 | * @return void |
||
| 1097 | */ |
||
| 1098 | 10 | protected function flushQueue($where = '') |
|
| 1099 | { |
||
| 1100 | 10 | $realWhere = strlen($where) > 0 ? $where : '1=1'; |
|
| 1101 | |||
| 1102 | 10 | if (EventDispatcher::getInstance()->hasObserver('queueEntryFlush') || SignalSlotUtility::hasSignal(__CLASS__, SignalSlotUtility::SIGNAL_QUEUE_ENTRY_FLUSH)) { |
|
| 1103 | $groups = $this->db->exec_SELECTgetRows('DISTINCT set_id', 'tx_crawler_queue', $realWhere); |
||
| 1104 | if (is_array($groups)) { |
||
| 1105 | foreach ($groups as $group) { |
||
| 1106 | |||
| 1107 | // The event dispatcher is deprecated since crawler v6.4.0, will be removed in crawler v7.0.0. |
||
| 1108 | // Please use the Signal instead. |
||
| 1109 | if (EventDispatcher::getInstance()->hasObserver('queueEntryFlush')) { |
||
| 1110 | EventDispatcher::getInstance()->post( |
||
| 1111 | 'queueEntryFlush', |
||
| 1112 | $group['set_id'], |
||
| 1113 | $this->db->exec_SELECTgetRows('uid, set_id', 'tx_crawler_queue', $realWhere . ' AND set_id="' . $group['set_id'] . '"') |
||
| 1114 | ); |
||
| 1115 | } |
||
| 1116 | |||
| 1117 | if (SignalSlotUtility::hasSignal(__CLASS__, SignalSlotUtility::SIGNAL_QUEUE_ENTRY_FLUSH)) { |
||
| 1118 | $signalInputArray = $this->db->exec_SELECTgetRows('uid, set_id', 'tx_crawler_queue', $realWhere . ' AND set_id="' . $group['set_id'] . '"'); |
||
| 1119 | SignalSlotUtility::emitSignal( |
||
| 1120 | __CLASS__, |
||
| 1121 | SignalSlotUtility::SIGNAL_QUEUE_ENTRY_FLUSH, |
||
| 1122 | $signalInputArray |
||
| 1123 | ); |
||
| 1124 | } |
||
| 1125 | } |
||
| 1126 | } |
||
| 1127 | } |
||
| 1128 | |||
| 1129 | 10 | $GLOBALS['TYPO3_DB']->exec_DELETEquery('tx_crawler_queue', $realWhere); |
|
| 1130 | 10 | } |
|
| 1131 | |||
| 1132 | /** |
||
| 1133 | * Adding call back entries to log (called from hooks typically, see indexed search class "class.crawler.php" |
||
| 1134 | * |
||
| 1135 | * @param integer $setId Set ID |
||
| 1136 | * @param array $params Parameters to pass to call back function |
||
| 1137 | * @param string $callBack Call back object reference, eg. 'EXT:indexed_search/class.crawler.php:&tx_indexedsearch_crawler' |
||
| 1138 | * @param integer $page_id Page ID to attach it to |
||
| 1139 | * @param integer $schedule Time at which to activate |
||
| 1140 | * @return void |
||
| 1141 | */ |
||
| 1142 | public function addQueueEntry_callBack($setId, $params, $callBack, $page_id = 0, $schedule = 0) |
||
| 1161 | |||
| 1162 | /************************************ |
||
| 1163 | * |
||
| 1164 | * URL setting |
||
| 1165 | * |
||
| 1166 | ************************************/ |
||
| 1167 | |||
| 1168 | /** |
||
| 1169 | * Setting a URL for crawling: |
||
| 1170 | * |
||
| 1171 | * @param integer $id Page ID |
||
| 1172 | * @param string $url Complete URL |
||
| 1173 | * @param array $subCfg Sub configuration array (from TS config) |
||
| 1174 | * @param integer $tstamp Scheduled-time |
||
| 1175 | * @param string $configurationHash (optional) configuration hash |
||
| 1176 | * @param bool $skipInnerDuplicationCheck (optional) skip inner duplication check |
||
| 1177 | * @return bool |
||
| 1178 | */ |
||
| 1179 | 8 | public function addUrl( |
|
| 1180 | $id, |
||
| 1181 | $url, |
||
| 1182 | array $subCfg, |
||
| 1265 | |||
| 1266 | /** |
||
| 1267 | * This method determines duplicates for a queue entry with the same parameters and this timestamp. |
||
| 1268 | * If the timestamp is in the past, it will check if there is any unprocessed queue entry in the past. |
||
| 1269 | * If the timestamp is in the future it will check, if the queued entry has exactly the same timestamp |
||
| 1270 | * |
||
| 1271 | * @param int $tstamp |
||
| 1272 | * @param array $fieldArray |
||
| 1273 | * |
||
| 1274 | * @return array |
||
| 1275 | */ |
||
| 1276 | 9 | protected function getDuplicateRowsIfExist($tstamp, $fieldArray) |
|
| 1316 | |||
| 1317 | /** |
||
| 1318 | * Returns the current system time |
||
| 1319 | * |
||
| 1320 | * @return int |
||
| 1321 | */ |
||
| 1322 | public function getCurrentTime() |
||
| 1326 | |||
| 1327 | /************************************ |
||
| 1328 | * |
||
| 1329 | * URL reading |
||
| 1330 | * |
||
| 1331 | ************************************/ |
||
| 1332 | |||
| 1333 | /** |
||
| 1334 | * Read URL for single queue entry |
||
| 1335 | * |
||
| 1336 | * @param integer $queueId |
||
| 1337 | * @param boolean $force If set, will process even if exec_time has been set! |
||
| 1338 | * @return integer |
||
| 1339 | */ |
||
| 1340 | public function readUrl($queueId, $force = false) |
||
| 1422 | |||
| 1423 | /** |
||
| 1424 | * Read URL for not-yet-inserted log-entry |
||
| 1425 | * |
||
| 1426 | * @param array $field_array Queue field array, |
||
| 1427 | * |
||
| 1428 | * @return string |
||
| 1429 | */ |
||
| 1430 | public function readUrlFromArray($field_array) |
||
| 1454 | |||
| 1455 | /** |
||
| 1456 | * Read URL for a queue record |
||
| 1457 | * |
||
| 1458 | * @param array $queueRec Queue record |
||
| 1459 | * @return string |
||
| 1460 | */ |
||
| 1461 | public function readUrl_exec($queueRec) |
||
| 1499 | |||
| 1500 | /** |
||
| 1501 | * Gets the content of a URL. |
||
| 1502 | * |
||
| 1503 | * @param string $originalUrl URL to read |
||
| 1504 | * @param string $crawlerId Crawler ID string (qid + hash to verify) |
||
| 1505 | * @param integer $timeout Timeout time |
||
| 1506 | * @param integer $recursion Recursion limiter for 302 redirects |
||
| 1507 | * @return array |
||
| 1508 | */ |
||
| 1509 | 2 | public function requestUrl($originalUrl, $crawlerId, $timeout = 2, $recursion = 10) |
|
| 1602 | |||
| 1603 | /** |
||
| 1604 | * Gets the base path of the website frontend. |
||
| 1605 | * (e.g. if you call http://mydomain.com/cms/index.php in |
||
| 1606 | * the browser the base path is "/cms/") |
||
| 1607 | * |
||
| 1608 | * @return string Base path of the website frontend |
||
| 1609 | */ |
||
| 1610 | protected function getFrontendBasePath() |
||
| 1633 | |||
| 1634 | /** |
||
| 1635 | * Executes a shell command and returns the outputted result. |
||
| 1636 | * |
||
| 1637 | * @param string $command Shell command to be executed |
||
| 1638 | * @return string Outputted result of the command execution |
||
| 1639 | */ |
||
| 1640 | protected function executeShellCommand($command) |
||
| 1645 | |||
| 1646 | /** |
||
| 1647 | * Reads HTTP response from the given stream. |
||
| 1648 | * |
||
| 1649 | * @param resource $streamPointer Pointer to connection stream. |
||
| 1650 | * @return array Associative array with the following items: |
||
| 1651 | * headers <array> Response headers sent by server. |
||
| 1652 | * content <array> Content, with each line as an array item. |
||
| 1653 | */ |
||
| 1654 | 1 | protected function getHttpResponseFromStream($streamPointer) |
|
| 1677 | |||
| 1678 | /** |
||
| 1679 | * @param message |
||
| 1680 | */ |
||
| 1681 | 2 | protected function log($message) |
|
| 1690 | |||
| 1691 | /** |
||
| 1692 | * Builds HTTP request headers. |
||
| 1693 | * |
||
| 1694 | * @param array $url |
||
| 1695 | * @param string $crawlerId |
||
| 1696 | * |
||
| 1697 | * @return array |
||
| 1698 | */ |
||
| 1699 | 6 | protected function buildRequestHeaderArray(array $url, $crawlerId) |
|
| 1715 | |||
| 1716 | /** |
||
| 1717 | * Check if the submitted HTTP-Header contains a redirect location and built new crawler-url |
||
| 1718 | * |
||
| 1719 | * @param array $headers HTTP Header |
||
| 1720 | * @param string $user HTTP Auth. User |
||
| 1721 | * @param string $pass HTTP Auth. Password |
||
| 1722 | * @return bool|string |
||
| 1723 | */ |
||
| 1724 | 12 | protected function getRequestUrlFrom302Header($headers, $user = '', $pass = '') |
|
| 1758 | |||
| 1759 | /************************** |
||
| 1760 | * |
||
| 1761 | * tslib_fe hooks: |
||
| 1762 | * |
||
| 1763 | **************************/ |
||
| 1764 | |||
| 1765 | /** |
||
| 1766 | * Initialization hook (called after database connection) |
||
| 1767 | * Takes the "HTTP_X_T3CRAWLER" header and looks up queue record and verifies if the session comes from the system (by comparing hashes) |
||
| 1768 | * |
||
| 1769 | * @param array $params Parameters from frontend |
||
| 1770 | * @param object $ref TSFE object (reference under PHP5) |
||
| 1771 | * @return void |
||
| 1772 | * |
||
| 1773 | * FIXME: Look like this is not used, in commit 9910d3f40cce15f4e9b7bcd0488bf21f31d53ebc it's added as public, |
||
| 1774 | * FIXME: I think this can be removed. (TNM) |
||
| 1775 | */ |
||
| 1776 | public function fe_init(&$params, $ref) |
||
| 1793 | |||
| 1794 | /***************************** |
||
| 1795 | * |
||
| 1796 | * Compiling URLs to crawl - tools |
||
| 1797 | * |
||
| 1798 | *****************************/ |
||
| 1799 | |||
| 1800 | /** |
||
| 1801 | * @param integer $id Root page id to start from. |
||
| 1802 | * @param integer $depth Depth of tree, 0=only id-page, 1= on sublevel, 99 = infinite |
||
| 1803 | * @param integer $scheduledTime Unix Time when the URL is timed to be visited when put in queue |
||
| 1804 | * @param integer $reqMinute Number of requests per minute (creates the interleave between requests) |
||
| 1805 | * @param boolean $submitCrawlUrls If set, submits the URLs to queue in database (real crawling) |
||
| 1806 | * @param boolean $downloadCrawlUrls If set (and submitcrawlUrls is false) will fill $downloadUrls with entries) |
||
| 1807 | * @param array $incomingProcInstructions Array of processing instructions |
||
| 1808 | * @param array $configurationSelection Array of configuration keys |
||
| 1809 | * @return string |
||
| 1810 | */ |
||
| 1811 | public function getPageTreeAndUrls( |
||
| 1898 | |||
| 1899 | /** |
||
| 1900 | * Expands exclude string |
||
| 1901 | * |
||
| 1902 | * @param string $excludeString Exclude string |
||
| 1903 | * @return array |
||
| 1904 | */ |
||
| 1905 | 1 | public function expandExcludeString($excludeString) |
|
| 1950 | |||
| 1951 | /** |
||
| 1952 | * Create the rows for display of the page tree |
||
| 1953 | * For each page a number of rows are shown displaying GET variable configuration |
||
| 1954 | * |
||
| 1955 | * @param array Page row |
||
| 1956 | * @param string Page icon and title for row |
||
| 1957 | * @return string HTML <tr> content (one or more) |
||
| 1958 | */ |
||
| 1959 | public function drawURLs_addRowsForPage(array $pageRow, $pageTitleAndIcon) |
||
| 2068 | |||
| 2069 | /***************************** |
||
| 2070 | * |
||
| 2071 | * CLI functions |
||
| 2072 | * |
||
| 2073 | *****************************/ |
||
| 2074 | |||
| 2075 | /** |
||
| 2076 | * Main function for running from Command Line PHP script (cron job) |
||
| 2077 | * See ext/crawler/cli/crawler_cli.phpsh for details |
||
| 2078 | * |
||
| 2079 | * @return int number of remaining items or false if error |
||
| 2080 | */ |
||
| 2081 | public function CLI_main() |
||
| 2122 | |||
| 2123 | /** |
||
| 2124 | * Function executed by crawler_im.php cli script. |
||
| 2125 | * |
||
| 2126 | * @return void |
||
| 2127 | */ |
||
| 2128 | public function CLI_main_im() |
||
| 2236 | |||
| 2237 | /** |
||
| 2238 | * Function executed by crawler_im.php cli script. |
||
| 2239 | * |
||
| 2240 | * @return bool |
||
| 2241 | */ |
||
| 2242 | public function CLI_main_flush() |
||
| 2280 | |||
| 2281 | /** |
||
| 2282 | * Obtains configuration keys from the CLI arguments |
||
| 2283 | * |
||
| 2284 | * @param QueueCommandLineController $cliObj |
||
| 2285 | * @return array |
||
| 2286 | * |
||
| 2287 | * @deprecated since crawler v6.3.0, will be removed in crawler v7.0.0. |
||
| 2288 | */ |
||
| 2289 | protected function getConfigurationKeys(QueueCommandLineController $cliObj) |
||
| 2294 | |||
| 2295 | /** |
||
| 2296 | * Running the functionality of the CLI (crawling URLs from queue) |
||
| 2297 | * |
||
| 2298 | * @param int $countInARun |
||
| 2299 | * @param int $sleepTime |
||
| 2300 | * @param int $sleepAfterFinish |
||
| 2301 | * @return string |
||
| 2302 | */ |
||
| 2303 | public function CLI_run($countInARun, $sleepTime, $sleepAfterFinish) |
||
| 2411 | |||
| 2412 | /** |
||
| 2413 | * Activate hooks |
||
| 2414 | * |
||
| 2415 | * @return void |
||
| 2416 | */ |
||
| 2417 | public function CLI_runHooks() |
||
| 2429 | |||
| 2430 | /** |
||
| 2431 | * Try to acquire a new process with the given id |
||
| 2432 | * also performs some auto-cleanup for orphan processes |
||
| 2433 | * @todo preemption might not be the most elegant way to clean up |
||
| 2434 | * |
||
| 2435 | * @param string $id identification string for the process |
||
| 2436 | * @return boolean |
||
| 2437 | */ |
||
| 2438 | public function CLI_checkAndAcquireNewProcess($id) |
||
| 2494 | |||
| 2495 | /** |
||
| 2496 | * Release a process and the required resources |
||
| 2497 | * |
||
| 2498 | * @param mixed $releaseIds string with a single process-id or array with multiple process-ids |
||
| 2499 | * @param boolean $withinLock show whether the DB-actions are included within an existing lock |
||
| 2500 | * @return boolean |
||
| 2501 | * |
||
| 2502 | * @deprecated since crawler v6.5.1, will be removed in crawler v9.0.0. |
||
| 2503 | */ |
||
| 2504 | public function CLI_releaseProcesses($releaseIds, $withinLock = false) |
||
| 2566 | |||
| 2567 | /** |
||
| 2568 | * Delete processes marked as deleted |
||
| 2569 | * |
||
| 2570 | * @return void |
||
| 2571 | * |
||
| 2572 | * @deprecated since crawler v6.5.1, will be removed in crawler v9.0.0. |
||
| 2573 | */ |
||
| 2574 | 1 | public function CLI_deleteProcessesMarkedDeleted() |
|
| 2578 | |||
| 2579 | /** |
||
| 2580 | * Check if there are still resources left for the process with the given id |
||
| 2581 | * Used to determine timeouts and to ensure a proper cleanup if there's a timeout |
||
| 2582 | * |
||
| 2583 | * @param string identification string for the process |
||
| 2584 | * @return boolean determines if the process is still active / has resources |
||
| 2585 | * |
||
| 2586 | * @deprecated since crawler v6.5.1, will be removed in crawler v9.0.0. |
||
| 2587 | * |
||
| 2588 | * FIXME: Please remove Transaction, not needed as only a select query. |
||
| 2589 | */ |
||
| 2590 | public function CLI_checkIfProcessIsActive($pid) |
||
| 2609 | |||
| 2610 | /** |
||
| 2611 | * Create a unique Id for the current process |
||
| 2612 | * |
||
| 2613 | * @return string the ID |
||
| 2614 | */ |
||
| 2615 | 2 | public function CLI_buildProcessId() |
|
| 2622 | |||
| 2623 | /** |
||
| 2624 | * @param bool $get_as_float |
||
| 2625 | * |
||
| 2626 | * @return mixed |
||
| 2627 | */ |
||
| 2628 | protected function microtime($get_as_float = false) |
||
| 2632 | |||
| 2633 | /** |
||
| 2634 | * Prints a message to the stdout (only if debug-mode is enabled) |
||
| 2635 | * |
||
| 2636 | * @param string $msg the message |
||
| 2637 | */ |
||
| 2638 | public function CLI_debug($msg) |
||
| 2645 | |||
| 2646 | /** |
||
| 2647 | * Get URL content by making direct request to TYPO3. |
||
| 2648 | * |
||
| 2649 | * @param string $url Page URL |
||
| 2650 | * @param int $crawlerId Crawler-ID |
||
| 2651 | * @return array |
||
| 2652 | */ |
||
| 2653 | 2 | protected function sendDirectRequest($url, $crawlerId) |
|
| 2684 | |||
| 2685 | /** |
||
| 2686 | * Cleans up entries that stayed for too long in the queue. These are: |
||
| 2687 | * - processed entries that are over 1.5 days in age |
||
| 2688 | * - scheduled entries that are over 7 days old |
||
| 2689 | * |
||
| 2690 | * @return void |
||
| 2691 | * |
||
| 2692 | * TODO: Should be switched back to protected - TNM 2018-11-16 |
||
| 2693 | */ |
||
| 2694 | public function cleanUpOldQueueEntries() |
||
| 2703 | |||
| 2704 | /** |
||
| 2705 | * Initializes a TypoScript Frontend necessary for using TypoScript and TypoLink functions |
||
| 2706 | * |
||
| 2707 | * @param int $id |
||
| 2708 | * @param int $typeNum |
||
| 2709 | * |
||
| 2710 | * @throws \TYPO3\CMS\Core\Error\Http\ServiceUnavailableException |
||
| 2711 | * |
||
| 2712 | * @return void |
||
| 2713 | */ |
||
| 2714 | protected function initTSFE($id = 1, $typeNum = 0) |
||
| 2739 | |||
| 2740 | /** |
||
| 2741 | * Returns a md5 hash generated from a serialized configuration array. |
||
| 2742 | * |
||
| 2743 | * @param array $configuration |
||
| 2744 | * |
||
| 2745 | * @return string |
||
| 2746 | */ |
||
| 2747 | 10 | protected function getConfigurationHash(array $configuration) |
|
| 2753 | |||
| 2754 | /** |
||
| 2755 | * Check whether the Crawling Protocol should be http or https |
||
| 2756 | * |
||
| 2757 | * @param $crawlerConfiguration |
||
| 2758 | * @param $pageConfiguration |
||
| 2759 | * |
||
| 2760 | * @return bool |
||
| 2761 | */ |
||
| 2762 | 10 | protected function isCrawlingProtocolHttps($crawlerConfiguration, $pageConfiguration) |
|
| 2775 | } |
||
| 2776 |
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: