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 |
||
| 61 | class CrawlerController implements LoggerAwareInterface |
||
| 62 | { |
||
| 63 | use LoggerAwareTrait; |
||
| 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 | * @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 array |
||
| 118 | */ |
||
| 119 | public $extensionSettings = []; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Mount Point |
||
| 123 | * |
||
| 124 | * @var boolean |
||
| 125 | */ |
||
| 126 | public $MP = false; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @var string |
||
| 130 | */ |
||
| 131 | protected $processFilename; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Holds the internal access mode can be 'gui','cli' or 'cli_im' |
||
| 135 | * |
||
| 136 | * @var string |
||
| 137 | */ |
||
| 138 | protected $accessMode; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @var BackendUserAuthentication |
||
| 142 | */ |
||
| 143 | private $backendUser; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @var integer |
||
| 147 | */ |
||
| 148 | private $scheduledTime = 0; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @var integer |
||
| 152 | */ |
||
| 153 | private $reqMinute = 0; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @var bool |
||
| 157 | */ |
||
| 158 | private $submitCrawlUrls = false; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @var bool |
||
| 162 | */ |
||
| 163 | private $downloadCrawlUrls = false; |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @var QueueRepository |
||
| 167 | */ |
||
| 168 | protected $queueRepository; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @var ProcessRepository |
||
| 172 | */ |
||
| 173 | protected $processRepository; |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @var ConfigurationRepository |
||
| 177 | */ |
||
| 178 | protected $configurationRepository; |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @var string |
||
| 182 | */ |
||
| 183 | protected $tableName = 'tx_crawler_queue'; |
||
| 184 | |||
| 185 | |||
| 186 | /** |
||
| 187 | * @var int |
||
| 188 | */ |
||
| 189 | protected $maximumUrlsToCompile = 10000; |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Method to set the accessMode can be gui, cli or cli_im |
||
| 193 | * |
||
| 194 | * @return string |
||
| 195 | */ |
||
| 196 | 1 | public function getAccessMode() |
|
| 197 | { |
||
| 198 | 1 | return $this->accessMode; |
|
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @param string $accessMode |
||
| 203 | */ |
||
| 204 | 1 | public function setAccessMode($accessMode) |
|
| 205 | { |
||
| 206 | 1 | $this->accessMode = $accessMode; |
|
| 207 | 1 | } |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Set disabled status to prevent processes from being processed |
||
| 211 | * |
||
| 212 | * @param bool $disabled (optional, defaults to true) |
||
| 213 | * @return void |
||
| 214 | */ |
||
| 215 | 3 | public function setDisabled($disabled = true) |
|
| 216 | { |
||
| 217 | 3 | if ($disabled) { |
|
| 218 | 2 | GeneralUtility::writeFile($this->processFilename, ''); |
|
| 219 | } else { |
||
| 220 | 1 | if (is_file($this->processFilename)) { |
|
| 221 | 1 | unlink($this->processFilename); |
|
| 222 | } |
||
| 223 | } |
||
| 224 | 3 | } |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Get disable status |
||
| 228 | * |
||
| 229 | * @return bool true if disabled |
||
| 230 | */ |
||
| 231 | 3 | public function getDisabled() |
|
| 232 | { |
||
| 233 | 3 | return is_file($this->processFilename); |
|
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param string $filenameWithPath |
||
| 238 | * |
||
| 239 | * @return void |
||
| 240 | */ |
||
| 241 | 4 | public function setProcessFilename($filenameWithPath) |
|
| 242 | { |
||
| 243 | 4 | $this->processFilename = $filenameWithPath; |
|
| 244 | 4 | } |
|
| 245 | |||
| 246 | /** |
||
| 247 | * @return string |
||
| 248 | */ |
||
| 249 | 1 | public function getProcessFilename() |
|
| 250 | { |
||
| 251 | 1 | return $this->processFilename; |
|
| 252 | } |
||
| 253 | |||
| 254 | /************************************ |
||
| 255 | * |
||
| 256 | * Getting URLs based on Page TSconfig |
||
| 257 | * |
||
| 258 | ************************************/ |
||
| 259 | |||
| 260 | 31 | public function __construct() |
|
| 261 | { |
||
| 262 | 31 | $objectManager = GeneralUtility::makeInstance(ObjectManager::class); |
|
| 263 | 31 | $this->queueRepository = $objectManager->get(QueueRepository::class); |
|
| 264 | 31 | $this->processRepository = $objectManager->get(ProcessRepository::class); |
|
| 265 | 31 | $this->configurationRepository = $objectManager->get(ConfigurationRepository::class); |
|
| 266 | |||
| 267 | 31 | $this->backendUser = $GLOBALS['BE_USER']; |
|
| 268 | 31 | $this->processFilename = Environment::getVarPath() . '/locks/tx_crawler.proc'; |
|
| 269 | |||
| 270 | /** @var ExtensionConfigurationProvider $configurationProvider */ |
||
| 271 | 31 | $configurationProvider = GeneralUtility::makeInstance(ExtensionConfigurationProvider::class); |
|
| 272 | 31 | $settings = $configurationProvider->getExtensionConfiguration(); |
|
| 273 | 31 | $this->extensionSettings = is_array($settings) ? $settings : []; |
|
| 274 | |||
| 275 | // set defaults: |
||
| 276 | 31 | if (MathUtility::convertToPositiveInteger($this->extensionSettings['countInARun']) == 0) { |
|
| 277 | $this->extensionSettings['countInARun'] = 100; |
||
| 278 | } |
||
| 279 | |||
| 280 | 31 | $this->extensionSettings['processLimit'] = MathUtility::forceIntegerInRange($this->extensionSettings['processLimit'], 1, 99, 1); |
|
| 281 | 31 | $this->maximumUrlsToCompile = MathUtility::forceIntegerInRange($this->extensionSettings['maxCompileUrls'], 1, 1000000000, 10000); |
|
| 282 | 31 | } |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Sets the extensions settings (unserialized pendant of $TYPO3_CONF_VARS['EXT']['extConf']['crawler']). |
||
| 286 | * |
||
| 287 | * @param array $extensionSettings |
||
| 288 | * @return void |
||
| 289 | */ |
||
| 290 | 9 | public function setExtensionSettings(array $extensionSettings) |
|
| 291 | { |
||
| 292 | 9 | $this->extensionSettings = $extensionSettings; |
|
| 293 | 9 | } |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Check if the given page should be crawled |
||
| 297 | * |
||
| 298 | * @param array $pageRow |
||
| 299 | * @return false|string false if the page should be crawled (not excluded), true / skipMessage if it should be skipped |
||
| 300 | */ |
||
| 301 | 8 | public function checkIfPageShouldBeSkipped(array $pageRow) |
|
| 302 | { |
||
| 303 | 8 | $skipPage = false; |
|
| 304 | 8 | $skipMessage = 'Skipped'; // message will be overwritten later |
|
| 305 | |||
| 306 | // if page is hidden |
||
| 307 | 8 | if (!$this->extensionSettings['crawlHiddenPages']) { |
|
| 308 | 8 | if ($pageRow['hidden']) { |
|
| 309 | 1 | $skipPage = true; |
|
| 310 | 1 | $skipMessage = 'Because page is hidden'; |
|
| 311 | } |
||
| 312 | } |
||
| 313 | |||
| 314 | 8 | if (!$skipPage) { |
|
| 315 | 7 | if (GeneralUtility::inList('3,4', $pageRow['doktype']) || $pageRow['doktype'] >= 199) { |
|
| 316 | 3 | $skipPage = true; |
|
| 317 | 3 | $skipMessage = 'Because doktype is not allowed'; |
|
| 318 | } |
||
| 319 | } |
||
| 320 | |||
| 321 | 8 | if (!$skipPage) { |
|
| 322 | 4 | foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['excludeDoktype'] ?? [] as $key => $doktypeList) { |
|
| 323 | 1 | if (GeneralUtility::inList($doktypeList, $pageRow['doktype'])) { |
|
| 324 | 1 | $skipPage = true; |
|
| 325 | 1 | $skipMessage = 'Doktype was excluded by "' . $key . '"'; |
|
| 326 | 1 | break; |
|
| 327 | } |
||
| 328 | } |
||
| 329 | } |
||
| 330 | |||
| 331 | 8 | if (!$skipPage) { |
|
| 332 | // veto hook |
||
| 333 | 3 | foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['pageVeto'] ?? [] as $key => $func) { |
|
| 334 | $params = [ |
||
| 335 | 'pageRow' => $pageRow |
||
| 336 | ]; |
||
| 337 | // expects "false" if page is ok and "true" or a skipMessage if this page should _not_ be crawled |
||
| 338 | $veto = GeneralUtility::callUserFunction($func, $params, $this); |
||
| 339 | if ($veto !== false) { |
||
| 340 | $skipPage = true; |
||
| 341 | if (is_string($veto)) { |
||
| 342 | $skipMessage = $veto; |
||
| 343 | } else { |
||
| 344 | $skipMessage = 'Veto from hook "' . htmlspecialchars($key) . '"'; |
||
| 345 | } |
||
| 346 | // no need to execute other hooks if a previous one return a veto |
||
| 347 | break; |
||
| 348 | } |
||
| 349 | } |
||
| 350 | } |
||
| 351 | |||
| 352 | 8 | return $skipPage ? $skipMessage : false; |
|
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Wrapper method for getUrlsForPageId() |
||
| 357 | * It returns an array of configurations and no urls! |
||
| 358 | * |
||
| 359 | * @param array $pageRow Page record with at least dok-type and uid columns. |
||
| 360 | * @param string $skipMessage |
||
| 361 | * @return array |
||
| 362 | * @see getUrlsForPageId() |
||
| 363 | */ |
||
| 364 | 4 | public function getUrlsForPageRow(array $pageRow, &$skipMessage = '') |
|
| 365 | { |
||
| 366 | 4 | $message = $this->checkIfPageShouldBeSkipped($pageRow); |
|
| 367 | |||
| 368 | 4 | if ($message === false) { |
|
| 369 | 3 | $res = $this->getUrlsForPageId($pageRow['uid']); |
|
| 370 | 3 | $skipMessage = ''; |
|
| 371 | } else { |
||
| 372 | 1 | $skipMessage = $message; |
|
| 373 | 1 | $res = []; |
|
| 374 | } |
||
| 375 | |||
| 376 | 4 | return $res; |
|
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * This method is used to count if there are ANY unprocessed queue entries |
||
| 381 | * of a given page_id and the configuration which matches a given hash. |
||
| 382 | * If there if none, we can skip an inner detail check |
||
| 383 | * |
||
| 384 | * @param int $uid |
||
| 385 | * @param string $configurationHash |
||
| 386 | * @return boolean |
||
| 387 | */ |
||
| 388 | 5 | protected function noUnprocessedQueueEntriesForPageWithConfigurationHashExist($uid, $configurationHash) |
|
| 389 | { |
||
| 390 | 5 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName); |
|
| 391 | 5 | $noUnprocessedQueueEntriesFound = true; |
|
| 392 | |||
| 393 | $result = $queryBuilder |
||
| 394 | 5 | ->count('*') |
|
| 395 | 5 | ->from($this->tableName) |
|
| 396 | 5 | ->where( |
|
| 397 | 5 | $queryBuilder->expr()->eq('page_id', intval($uid)), |
|
| 398 | 5 | $queryBuilder->expr()->eq('configuration_hash', $queryBuilder->createNamedParameter($configurationHash)), |
|
| 399 | 5 | $queryBuilder->expr()->eq('exec_time', 0) |
|
| 400 | ) |
||
| 401 | 5 | ->execute() |
|
| 402 | 5 | ->fetchColumn(); |
|
| 403 | |||
| 404 | 5 | if ($result) { |
|
| 405 | 3 | $noUnprocessedQueueEntriesFound = false; |
|
| 406 | } |
||
| 407 | |||
| 408 | 5 | return $noUnprocessedQueueEntriesFound; |
|
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Creates a list of URLs from input array (and submits them to queue if asked for) |
||
| 413 | * See Web > Info module script + "indexed_search"'s crawler hook-client using this! |
||
| 414 | * |
||
| 415 | * @param array Information about URLs from pageRow to crawl. |
||
| 416 | * @param array Page row |
||
| 417 | * @param integer Unix time to schedule indexing to, typically time() |
||
| 418 | * @param integer Number of requests per minute (creates the interleave between requests) |
||
| 419 | * @param boolean If set, submits the URLs to queue |
||
| 420 | * @param boolean If set (and submitcrawlUrls is false) will fill $downloadUrls with entries) |
||
| 421 | * @param array Array which is passed by reference and contains the an id per url to secure we will not crawl duplicates |
||
| 422 | * @param array Array which will be filled with URLS for download if flag is set. |
||
| 423 | * @param array Array of processing instructions |
||
| 424 | * @return string List of URLs (meant for display in backend module) |
||
| 425 | * |
||
| 426 | */ |
||
| 427 | 2 | public function urlListFromUrlArray( |
|
| 428 | array $vv, |
||
| 429 | array $pageRow, |
||
| 430 | $scheduledTime, |
||
| 431 | $reqMinute, |
||
| 432 | $submitCrawlUrls, |
||
| 433 | $downloadCrawlUrls, |
||
| 434 | array &$duplicateTrack, |
||
| 435 | array &$downloadUrls, |
||
| 436 | array $incomingProcInstructions |
||
| 437 | ) { |
||
| 438 | 2 | $urlList = ''; |
|
| 439 | |||
| 440 | 2 | if (is_array($vv['URLs'])) { |
|
| 441 | 2 | $configurationHash = $this->getConfigurationHash($vv); |
|
| 442 | 2 | $skipInnerCheck = $this->noUnprocessedQueueEntriesForPageWithConfigurationHashExist($pageRow['uid'], $configurationHash); |
|
| 443 | |||
| 444 | 2 | foreach ($vv['URLs'] as $urlQuery) { |
|
| 445 | 2 | if ($this->drawURLs_PIfilter($vv['subCfg']['procInstrFilter'], $incomingProcInstructions)) { |
|
| 446 | |||
| 447 | // Calculate cHash: |
||
| 448 | 2 | if ($vv['subCfg']['cHash']) { |
|
| 449 | /* @var $cacheHash \TYPO3\CMS\Frontend\Page\CacheHashCalculator */ |
||
| 450 | $cacheHash = GeneralUtility::makeInstance('TYPO3\CMS\Frontend\Page\CacheHashCalculator'); |
||
| 451 | $urlQuery .= '&cHash=' . $cacheHash->generateForParameters($urlQuery); |
||
| 452 | } |
||
| 453 | |||
| 454 | // Create key by which to determine unique-ness: |
||
| 455 | 2 | $uKey = $urlQuery . '|' . $vv['subCfg']['userGroups'] . '|' . $vv['subCfg']['baseUrl'] . '|' . $vv['subCfg']['procInstrFilter']; |
|
| 456 | 2 | $urlQuery = 'index.php' . $urlQuery; |
|
| 457 | |||
| 458 | // Scheduled time: |
||
| 459 | 2 | $schTime = $scheduledTime + round(count($duplicateTrack) * (60 / $reqMinute)); |
|
| 460 | 2 | $schTime = floor($schTime / 60) * 60; |
|
| 461 | |||
| 462 | 2 | if (isset($duplicateTrack[$uKey])) { |
|
| 463 | |||
| 464 | //if the url key is registered just display it and do not resubmit is |
||
| 465 | $urlList = '<em><span class="typo3-dimmed">' . htmlspecialchars($urlQuery) . '</span></em><br/>'; |
||
| 466 | } else { |
||
| 467 | 2 | $urlList = '[' . date('d.m.y H:i', $schTime) . '] ' . htmlspecialchars($urlQuery); |
|
| 468 | 2 | $this->urlList[] = '[' . date('d.m.y H:i', $schTime) . '] ' . $urlQuery; |
|
| 469 | |||
| 470 | 2 | $theUrl = ($vv['subCfg']['baseUrl'] ? $vv['subCfg']['baseUrl'] : GeneralUtility::getIndpEnv('TYPO3_SITE_URL')) . $urlQuery; |
|
| 471 | |||
| 472 | // Submit for crawling! |
||
| 473 | 2 | if ($submitCrawlUrls) { |
|
| 474 | 2 | $added = $this->addUrl( |
|
| 475 | 2 | $pageRow['uid'], |
|
| 476 | 2 | $theUrl, |
|
| 477 | 2 | $vv['subCfg'], |
|
| 478 | 2 | $scheduledTime, |
|
| 479 | 2 | $configurationHash, |
|
| 480 | 2 | $skipInnerCheck |
|
| 481 | ); |
||
| 482 | 2 | if ($added === false) { |
|
| 483 | 2 | $urlList .= ' (Url already existed)'; |
|
| 484 | } |
||
| 485 | } elseif ($downloadCrawlUrls) { |
||
| 486 | $downloadUrls[$theUrl] = $theUrl; |
||
| 487 | } |
||
| 488 | |||
| 489 | 2 | $urlList .= '<br />'; |
|
| 490 | } |
||
| 491 | 2 | $duplicateTrack[$uKey] = true; |
|
| 492 | } |
||
| 493 | } |
||
| 494 | } else { |
||
| 495 | $urlList = 'ERROR - no URL generated'; |
||
| 496 | } |
||
| 497 | |||
| 498 | 2 | return $urlList; |
|
| 499 | } |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Returns true if input processing instruction is among registered ones. |
||
| 503 | * |
||
| 504 | * @param string $piString PI to test |
||
| 505 | * @param array $incomingProcInstructions Processing instructions |
||
| 506 | * @return boolean |
||
| 507 | */ |
||
| 508 | 5 | public function drawURLs_PIfilter($piString, array $incomingProcInstructions) |
|
| 509 | { |
||
| 510 | 5 | if (empty($incomingProcInstructions)) { |
|
| 511 | 1 | return true; |
|
| 512 | } |
||
| 513 | |||
| 514 | 4 | foreach ($incomingProcInstructions as $pi) { |
|
| 515 | 4 | if (GeneralUtility::inList($piString, $pi)) { |
|
| 516 | 2 | return true; |
|
| 517 | } |
||
| 518 | } |
||
| 519 | 2 | return false; |
|
| 520 | } |
||
| 521 | |||
| 522 | 2 | public function getPageTSconfigForId($id) |
|
| 523 | { |
||
| 524 | 2 | if (!$this->MP) { |
|
| 525 | 2 | $pageTSconfig = BackendUtility::getPagesTSconfig($id); |
|
| 526 | } else { |
||
| 527 | [, $mountPointId] = explode('-', $this->MP); |
||
|
|
|||
| 528 | $pageTSconfig = BackendUtility::getPagesTSconfig($mountPointId); |
||
| 529 | } |
||
| 530 | |||
| 531 | // Call a hook to alter configuration |
||
| 532 | 2 | if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['getPageTSconfigForId'])) { |
|
| 533 | $params = [ |
||
| 534 | 'pageId' => $id, |
||
| 535 | 'pageTSConfig' => &$pageTSconfig |
||
| 536 | ]; |
||
| 537 | foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['getPageTSconfigForId'] as $userFunc) { |
||
| 538 | GeneralUtility::callUserFunction($userFunc, $params, $this); |
||
| 539 | } |
||
| 540 | } |
||
| 541 | 2 | return $pageTSconfig; |
|
| 542 | } |
||
| 543 | |||
| 544 | /** |
||
| 545 | * This methods returns an array of configurations. |
||
| 546 | * And no urls! |
||
| 547 | * |
||
| 548 | * @param integer $id Page ID |
||
| 549 | * @return array |
||
| 550 | */ |
||
| 551 | 2 | public function getUrlsForPageId($pageId) |
|
| 552 | { |
||
| 553 | // Get page TSconfig for page ID |
||
| 554 | 2 | $pageTSconfig = $this->getPageTSconfigForId($pageId); |
|
| 555 | |||
| 556 | 2 | $res = []; |
|
| 557 | |||
| 558 | // Fetch Crawler Configuration from pageTSconfig |
||
| 559 | 2 | $crawlerCfg = $pageTSconfig['tx_crawler.']['crawlerCfg.']['paramSets.'] ?? []; |
|
| 560 | 2 | foreach ($crawlerCfg as $key => $values) { |
|
| 561 | 1 | if (!is_array($values)) { |
|
| 562 | 1 | continue; |
|
| 563 | } |
||
| 564 | 1 | $key = str_replace('.', '', $key); |
|
| 565 | // Sub configuration for a single configuration string: |
||
| 566 | 1 | $subCfg = (array)$crawlerCfg[$key . '.']; |
|
| 567 | 1 | $subCfg['key'] = $key; |
|
| 568 | |||
| 569 | 1 | if (strcmp($subCfg['procInstrFilter'], '')) { |
|
| 570 | 1 | $subCfg['procInstrFilter'] = implode(',', GeneralUtility::trimExplode(',', $subCfg['procInstrFilter'])); |
|
| 571 | } |
||
| 572 | 1 | $pidOnlyList = implode(',', GeneralUtility::trimExplode(',', $subCfg['pidsOnly'], true)); |
|
| 573 | |||
| 574 | // process configuration if it is not page-specific or if the specific page is the current page: |
||
| 575 | 1 | if (!strcmp($subCfg['pidsOnly'], '') || GeneralUtility::inList($pidOnlyList, $pageId)) { |
|
| 576 | |||
| 577 | // add trailing slash if not present |
||
| 578 | 1 | if (!empty($subCfg['baseUrl']) && substr($subCfg['baseUrl'], -1) != '/') { |
|
| 579 | $subCfg['baseUrl'] .= '/'; |
||
| 580 | } |
||
| 581 | |||
| 582 | // Explode, process etc.: |
||
| 583 | 1 | $res[$key] = []; |
|
| 584 | 1 | $res[$key]['subCfg'] = $subCfg; |
|
| 585 | 1 | $res[$key]['paramParsed'] = GeneralUtility::explodeUrl2Array($crawlerCfg[$key]); |
|
| 586 | 1 | $res[$key]['paramExpanded'] = $this->expandParameters($res[$key]['paramParsed'], $pageId); |
|
| 587 | 1 | $res[$key]['origin'] = 'pagets'; |
|
| 588 | |||
| 589 | // recognize MP value |
||
| 590 | 1 | if (!$this->MP) { |
|
| 591 | 1 | $res[$key]['URLs'] = $this->compileUrls($res[$key]['paramExpanded'], ['?id=' . $pageId]); |
|
| 592 | } else { |
||
| 593 | $res[$key]['URLs'] = $this->compileUrls($res[$key]['paramExpanded'], ['?id=' . $pageId . '&MP=' . $this->MP]); |
||
| 594 | } |
||
| 595 | } |
||
| 596 | } |
||
| 597 | |||
| 598 | // Get configuration from tx_crawler_configuration records up the rootline |
||
| 599 | 2 | $crawlerConfigurations = $this->configurationRepository->getCrawlerConfigurationRecordsFromRootLine($pageId); |
|
| 600 | 2 | foreach ($crawlerConfigurations as $configurationRecord) { |
|
| 601 | |||
| 602 | // check access to the configuration record |
||
| 603 | 1 | if (empty($configurationRecord['begroups']) || $GLOBALS['BE_USER']->isAdmin() || $this->hasGroupAccess($GLOBALS['BE_USER']->user['usergroup_cached_list'], $configurationRecord['begroups'])) { |
|
| 604 | 1 | $pidOnlyList = implode(',', GeneralUtility::trimExplode(',', $configurationRecord['pidsonly'], true)); |
|
| 605 | |||
| 606 | // process configuration if it is not page-specific or if the specific page is the current page: |
||
| 607 | 1 | if (!strcmp($configurationRecord['pidsonly'], '') || GeneralUtility::inList($pidOnlyList, $pageId)) { |
|
| 608 | 1 | $key = $configurationRecord['name']; |
|
| 609 | |||
| 610 | // don't overwrite previously defined paramSets |
||
| 611 | 1 | if (!isset($res[$key])) { |
|
| 612 | |||
| 613 | /* @var $TSparserObject \TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser */ |
||
| 614 | 1 | $TSparserObject = GeneralUtility::makeInstance(TypoScriptParser::class); |
|
| 615 | 1 | $TSparserObject->parse($configurationRecord['processing_instruction_parameters_ts']); |
|
| 616 | |||
| 617 | $subCfg = [ |
||
| 618 | 1 | 'procInstrFilter' => $configurationRecord['processing_instruction_filter'], |
|
| 619 | 1 | 'procInstrParams.' => $TSparserObject->setup, |
|
| 620 | 1 | 'baseUrl' => $this->getBaseUrlForConfigurationRecord( |
|
| 621 | 1 | $configurationRecord['base_url'], |
|
| 622 | 1 | (int)$configurationRecord['sys_domain_base_url'], |
|
| 623 | 1 | (bool)($configurationRecord['force_ssl'] > 0) |
|
| 624 | ), |
||
| 625 | 1 | 'cHash' => $configurationRecord['chash'], |
|
| 626 | 1 | 'userGroups' => $configurationRecord['fegroups'], |
|
| 627 | 1 | 'exclude' => $configurationRecord['exclude'], |
|
| 628 | 1 | 'rootTemplatePid' => (int) $configurationRecord['root_template_pid'], |
|
| 629 | 1 | 'key' => $key |
|
| 630 | ]; |
||
| 631 | |||
| 632 | // add trailing slash if not present |
||
| 633 | 1 | if (!empty($subCfg['baseUrl']) && substr($subCfg['baseUrl'], -1) != '/') { |
|
| 634 | $subCfg['baseUrl'] .= '/'; |
||
| 635 | } |
||
| 636 | 1 | if (!in_array($pageId, $this->expandExcludeString($subCfg['exclude']))) { |
|
| 637 | 1 | $res[$key] = []; |
|
| 638 | 1 | $res[$key]['subCfg'] = $subCfg; |
|
| 639 | 1 | $res[$key]['paramParsed'] = GeneralUtility::explodeUrl2Array($configurationRecord['configuration']); |
|
| 640 | 1 | $res[$key]['paramExpanded'] = $this->expandParameters($res[$key]['paramParsed'], $pageId); |
|
| 641 | 1 | $res[$key]['URLs'] = $this->compileUrls($res[$key]['paramExpanded'], ['?id=' . $pageId]); |
|
| 642 | 1 | $res[$key]['origin'] = 'tx_crawler_configuration_' . $configurationRecord['uid']; |
|
| 643 | } |
||
| 644 | } |
||
| 645 | } |
||
| 646 | } |
||
| 647 | } |
||
| 648 | |||
| 649 | 2 | foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['processUrls'] ?? [] as $func) { |
|
| 650 | $params = [ |
||
| 651 | 'res' => &$res, |
||
| 652 | ]; |
||
| 653 | GeneralUtility::callUserFunction($func, $params, $this); |
||
| 654 | } |
||
| 655 | 2 | return $res; |
|
| 656 | } |
||
| 657 | |||
| 658 | /** |
||
| 659 | * Checks if a domain record exist and returns the base-url based on the record. If not the given baseUrl string is used. |
||
| 660 | * |
||
| 661 | * @param string $baseUrl |
||
| 662 | * @param integer $sysDomainUid |
||
| 663 | * @param bool $ssl |
||
| 664 | * @return string |
||
| 665 | */ |
||
| 666 | 4 | protected function getBaseUrlForConfigurationRecord(string $baseUrl, int $sysDomainUid, bool $ssl = false): string |
|
| 667 | { |
||
| 668 | 4 | if ($sysDomainUid > 0) { |
|
| 669 | 2 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_domain'); |
|
| 670 | $domainName = $queryBuilder |
||
| 671 | 2 | ->select('domainName') |
|
| 672 | 2 | ->from('sys_domain') |
|
| 673 | 2 | ->where( |
|
| 674 | 2 | $queryBuilder->expr()->eq('uid', $sysDomainUid) |
|
| 675 | ) |
||
| 676 | 2 | ->execute() |
|
| 677 | 2 | ->fetchColumn(); |
|
| 678 | |||
| 679 | 2 | if (!empty($domainName)) { |
|
| 680 | 1 | $baseUrl = ($ssl ? 'https' : 'http') . '://' . $domainName; |
|
| 681 | } |
||
| 682 | } |
||
| 683 | 4 | return $baseUrl; |
|
| 684 | } |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Find all configurations of subpages of a page |
||
| 688 | * |
||
| 689 | * @param int $rootid |
||
| 690 | * @param $depth |
||
| 691 | * @return array |
||
| 692 | * |
||
| 693 | * TODO: Write Functional Tests |
||
| 694 | */ |
||
| 695 | public function getConfigurationsForBranch(int $rootid, $depth) |
||
| 696 | { |
||
| 697 | $configurationsForBranch = []; |
||
| 698 | $pageTSconfig = $this->getPageTSconfigForId($rootid); |
||
| 699 | $sets = $pageTSconfig['tx_crawler.']['crawlerCfg.']['paramSets.'] ?? []; |
||
| 700 | foreach ($sets as $key => $value) { |
||
| 701 | if (!is_array($value)) { |
||
| 702 | continue; |
||
| 703 | } |
||
| 704 | $configurationsForBranch[] = substr($key, -1) == '.' ? substr($key, 0, -1) : $key; |
||
| 705 | } |
||
| 706 | $pids = []; |
||
| 707 | $rootLine = BackendUtility::BEgetRootLine($rootid); |
||
| 708 | foreach ($rootLine as $node) { |
||
| 709 | $pids[] = $node['uid']; |
||
| 710 | } |
||
| 711 | /* @var PageTreeView $tree */ |
||
| 712 | $tree = GeneralUtility::makeInstance(PageTreeView::class); |
||
| 713 | $perms_clause = $GLOBALS['BE_USER']->getPagePermsClause(1); |
||
| 714 | $tree->init('AND ' . $perms_clause); |
||
| 715 | $tree->getTree($rootid, $depth, ''); |
||
| 716 | foreach ($tree->tree as $node) { |
||
| 717 | $pids[] = $node['row']['uid']; |
||
| 718 | } |
||
| 719 | |||
| 720 | $queryBuilder = $this->getQueryBuilder('tx_crawler_configuration'); |
||
| 721 | |||
| 722 | $queryBuilder->getRestrictions() |
||
| 723 | ->removeAll() |
||
| 724 | ->add(GeneralUtility::makeInstance(DeletedRestriction::class)); |
||
| 725 | |||
| 726 | $statement = $queryBuilder |
||
| 727 | ->select('name') |
||
| 728 | ->from('tx_crawler_configuration') |
||
| 729 | ->where( |
||
| 730 | $queryBuilder->expr()->in('pid', $queryBuilder->createNamedParameter($pids, Connection::PARAM_INT_ARRAY)) |
||
| 731 | ) |
||
| 732 | ->execute(); |
||
| 733 | |||
| 734 | while ($row = $statement->fetch()) { |
||
| 735 | $configurationsForBranch[] = $row['name']; |
||
| 736 | } |
||
| 737 | return $configurationsForBranch; |
||
| 738 | } |
||
| 739 | |||
| 740 | /** |
||
| 741 | * Get querybuilder for given table |
||
| 742 | * |
||
| 743 | * @param string $table |
||
| 744 | * @return \TYPO3\CMS\Core\Database\Query\QueryBuilder |
||
| 745 | */ |
||
| 746 | 9 | private function getQueryBuilder(string $table) |
|
| 747 | { |
||
| 748 | 9 | return GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table); |
|
| 749 | } |
||
| 750 | |||
| 751 | /** |
||
| 752 | * Check if a user has access to an item |
||
| 753 | * (e.g. get the group list of the current logged in user from $GLOBALS['TSFE']->gr_list) |
||
| 754 | * |
||
| 755 | * @see \TYPO3\CMS\Frontend\Page\PageRepository::getMultipleGroupsWhereClause() |
||
| 756 | * @param string $groupList Comma-separated list of (fe_)group UIDs from a user |
||
| 757 | * @param string $accessList Comma-separated list of (fe_)group UIDs of the item to access |
||
| 758 | * @return bool TRUE if at least one of the users group UIDs is in the access list or the access list is empty |
||
| 759 | */ |
||
| 760 | 3 | public function hasGroupAccess($groupList, $accessList) |
|
| 761 | { |
||
| 762 | 3 | if (empty($accessList)) { |
|
| 763 | 1 | return true; |
|
| 764 | } |
||
| 765 | 2 | foreach (GeneralUtility::intExplode(',', $groupList) as $groupUid) { |
|
| 766 | 2 | if (GeneralUtility::inList($accessList, $groupUid)) { |
|
| 767 | 1 | return true; |
|
| 768 | } |
||
| 769 | } |
||
| 770 | 1 | return false; |
|
| 771 | } |
||
| 772 | |||
| 773 | /** |
||
| 774 | * Will expand the parameters configuration to individual values. This follows a certain syntax of the value of each parameter. |
||
| 775 | * Syntax of values: |
||
| 776 | * - Basically: If the value is wrapped in [...] it will be expanded according to the following syntax, otherwise the value is taken literally |
||
| 777 | * - Configuration is splitted by "|" and the parts are processed individually and finally added together |
||
| 778 | * - For each configuration part: |
||
| 779 | * - "[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" |
||
| 780 | * - "_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" |
||
| 781 | * _ENABLELANG:1 picks only original records without their language overlays |
||
| 782 | * - Default: Literal value |
||
| 783 | * |
||
| 784 | * @param array $paramArray Array with key (GET var name) and values (value of GET var which is configuration for expansion) |
||
| 785 | * @param integer $pid Current page ID |
||
| 786 | * @return array |
||
| 787 | * |
||
| 788 | * TODO: Write Functional Tests |
||
| 789 | */ |
||
| 790 | 2 | public function expandParameters($paramArray, $pid) |
|
| 791 | { |
||
| 792 | // Traverse parameter names: |
||
| 793 | 2 | foreach ($paramArray as $p => $v) { |
|
| 794 | 2 | $v = trim($v); |
|
| 795 | |||
| 796 | // If value is encapsulated in square brackets it means there are some ranges of values to find, otherwise the value is literal |
||
| 797 | 2 | if (substr($v, 0, 1) === '[' && substr($v, -1) === ']') { |
|
| 798 | // So, find the value inside brackets and reset the paramArray value as an array. |
||
| 799 | 2 | $v = substr($v, 1, -1); |
|
| 800 | 2 | $paramArray[$p] = []; |
|
| 801 | |||
| 802 | // Explode parts and traverse them: |
||
| 803 | 2 | $parts = explode('|', $v); |
|
| 804 | 2 | foreach ($parts as $pV) { |
|
| 805 | |||
| 806 | // Look for integer range: (fx. 1-34 or -40--30 // reads minus 40 to minus 30) |
||
| 807 | 2 | if (preg_match('/^(-?[0-9]+)\s*-\s*(-?[0-9]+)$/', trim($pV), $reg)) { |
|
| 808 | |||
| 809 | // Swap if first is larger than last: |
||
| 810 | if ($reg[1] > $reg[2]) { |
||
| 811 | $temp = $reg[2]; |
||
| 812 | $reg[2] = $reg[1]; |
||
| 813 | $reg[1] = $temp; |
||
| 814 | } |
||
| 815 | |||
| 816 | // Traverse range, add values: |
||
| 817 | $runAwayBrake = 1000; // Limit to size of range! |
||
| 818 | for ($a = $reg[1]; $a <= $reg[2];$a++) { |
||
| 819 | $paramArray[$p][] = $a; |
||
| 820 | $runAwayBrake--; |
||
| 821 | if ($runAwayBrake <= 0) { |
||
| 822 | break; |
||
| 823 | } |
||
| 824 | } |
||
| 825 | 2 | } elseif (substr(trim($pV), 0, 7) == '_TABLE:') { |
|
| 826 | |||
| 827 | // Parse parameters: |
||
| 828 | $subparts = GeneralUtility::trimExplode(';', $pV); |
||
| 829 | $subpartParams = []; |
||
| 830 | foreach ($subparts as $spV) { |
||
| 831 | list($pKey, $pVal) = GeneralUtility::trimExplode(':', $spV); |
||
| 832 | $subpartParams[$pKey] = $pVal; |
||
| 833 | } |
||
| 834 | |||
| 835 | // Table exists: |
||
| 836 | if (isset($GLOBALS['TCA'][$subpartParams['_TABLE']])) { |
||
| 837 | $lookUpPid = isset($subpartParams['_PID']) ? intval($subpartParams['_PID']) : $pid; |
||
| 838 | $pidField = isset($subpartParams['_PIDFIELD']) ? trim($subpartParams['_PIDFIELD']) : 'pid'; |
||
| 839 | $where = isset($subpartParams['_WHERE']) ? $subpartParams['_WHERE'] : ''; |
||
| 840 | $addTable = isset($subpartParams['_ADDTABLE']) ? $subpartParams['_ADDTABLE'] : ''; |
||
| 841 | |||
| 842 | $fieldName = $subpartParams['_FIELD'] ? $subpartParams['_FIELD'] : 'uid'; |
||
| 843 | if ($fieldName === 'uid' || $GLOBALS['TCA'][$subpartParams['_TABLE']]['columns'][$fieldName]) { |
||
| 844 | $queryBuilder = $this->getQueryBuilder($subpartParams['_TABLE']); |
||
| 845 | |||
| 846 | $queryBuilder->getRestrictions() |
||
| 847 | ->removeAll() |
||
| 848 | ->add(GeneralUtility::makeInstance(DeletedRestriction::class)); |
||
| 849 | |||
| 850 | $queryBuilder |
||
| 851 | ->select($fieldName) |
||
| 852 | ->from($subpartParams['_TABLE']) |
||
| 853 | // TODO: Check if this works as intended! |
||
| 854 | ->add('from', $addTable) |
||
| 855 | ->where( |
||
| 856 | $queryBuilder->expr()->eq($queryBuilder->quoteIdentifier($pidField), $queryBuilder->createNamedParameter($lookUpPid, \PDO::PARAM_INT)), |
||
| 857 | $where |
||
| 858 | ); |
||
| 859 | $transOrigPointerField = $GLOBALS['TCA'][$subpartParams['_TABLE']]['ctrl']['transOrigPointerField']; |
||
| 860 | |||
| 861 | if ($subpartParams['_ENABLELANG'] && $transOrigPointerField) { |
||
| 862 | $queryBuilder->andWhere( |
||
| 863 | $queryBuilder->expr()->lte( |
||
| 864 | $queryBuilder->quoteIdentifier($transOrigPointerField), |
||
| 865 | 0 |
||
| 866 | ) |
||
| 867 | ); |
||
| 868 | } |
||
| 869 | |||
| 870 | $statement = $queryBuilder->execute(); |
||
| 871 | |||
| 872 | $rows = []; |
||
| 873 | while ($row = $statement->fetch()) { |
||
| 874 | $rows[$fieldName] = $row; |
||
| 875 | } |
||
| 876 | |||
| 877 | if (is_array($rows)) { |
||
| 878 | $paramArray[$p] = array_merge($paramArray[$p], array_keys($rows)); |
||
| 879 | } |
||
| 880 | } |
||
| 881 | } |
||
| 882 | } else { // Just add value: |
||
| 883 | 2 | $paramArray[$p][] = $pV; |
|
| 884 | } |
||
| 885 | // Hook for processing own expandParameters place holder |
||
| 886 | 2 | if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['crawler/class.tx_crawler_lib.php']['expandParameters'])) { |
|
| 887 | $_params = [ |
||
| 888 | 'pObj' => &$this, |
||
| 889 | 'paramArray' => &$paramArray, |
||
| 890 | 'currentKey' => $p, |
||
| 891 | 'currentValue' => $pV, |
||
| 892 | 'pid' => $pid |
||
| 893 | ]; |
||
| 894 | foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['crawler/class.tx_crawler_lib.php']['expandParameters'] as $key => $_funcRef) { |
||
| 895 | GeneralUtility::callUserFunction($_funcRef, $_params, $this); |
||
| 896 | } |
||
| 897 | } |
||
| 898 | } |
||
| 899 | |||
| 900 | // Make unique set of values and sort array by key: |
||
| 901 | 2 | $paramArray[$p] = array_unique($paramArray[$p]); |
|
| 902 | 2 | ksort($paramArray); |
|
| 903 | } else { |
||
| 904 | // Set the literal value as only value in array: |
||
| 905 | 2 | $paramArray[$p] = [$v]; |
|
| 906 | } |
||
| 907 | } |
||
| 908 | |||
| 909 | 2 | return $paramArray; |
|
| 910 | } |
||
| 911 | |||
| 912 | /** |
||
| 913 | * Compiling URLs from parameter array (output of expandParameters()) |
||
| 914 | * The number of URLs will be the multiplication of the number of parameter values for each key |
||
| 915 | * |
||
| 916 | * @param array $paramArray Output of expandParameters(): Array with keys (GET var names) and for each an array of values |
||
| 917 | * @param array $urls URLs accumulated in this array (for recursion) |
||
| 918 | * @return array |
||
| 919 | */ |
||
| 920 | 5 | public function compileUrls($paramArray, array $urls) |
|
| 921 | { |
||
| 922 | 5 | if (empty($paramArray)) { |
|
| 923 | 5 | return $urls; |
|
| 924 | } |
||
| 925 | // shift first off stack: |
||
| 926 | 4 | reset($paramArray); |
|
| 927 | 4 | $varName = key($paramArray); |
|
| 928 | 4 | $valueSet = array_shift($paramArray); |
|
| 929 | |||
| 930 | // Traverse value set: |
||
| 931 | 4 | $newUrls = []; |
|
| 932 | 4 | foreach ($urls as $url) { |
|
| 933 | 3 | foreach ($valueSet as $val) { |
|
| 934 | 3 | $newUrls[] = $url . (strcmp($val, '') ? '&' . rawurlencode($varName) . '=' . rawurlencode($val) : ''); |
|
| 935 | |||
| 936 | 3 | if (count($newUrls) > $this->maximumUrlsToCompile) { |
|
| 937 | break; |
||
| 938 | } |
||
| 939 | } |
||
| 940 | } |
||
| 941 | 4 | return $this->compileUrls($paramArray, $newUrls); |
|
| 942 | } |
||
| 943 | |||
| 944 | /************************************ |
||
| 945 | * |
||
| 946 | * Crawler log |
||
| 947 | * |
||
| 948 | ************************************/ |
||
| 949 | |||
| 950 | /** |
||
| 951 | * Return array of records from crawler queue for input page ID |
||
| 952 | * |
||
| 953 | * @param integer $id Page ID for which to look up log entries. |
||
| 954 | * @param string$filter Filter: "all" => all entries, "pending" => all that is not yet run, "finished" => all complete ones |
||
| 955 | * @param boolean $doFlush If TRUE, then entries selected at DELETED(!) instead of selected! |
||
| 956 | * @param boolean $doFullFlush |
||
| 957 | * @param integer $itemsPerPage Limit the amount of entries per page default is 10 |
||
| 958 | * @return array |
||
| 959 | */ |
||
| 960 | 4 | public function getLogEntriesForPageId($id, $filter = '', $doFlush = false, $doFullFlush = false, $itemsPerPage = 10) |
|
| 961 | { |
||
| 962 | 4 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName); |
|
| 963 | $queryBuilder |
||
| 964 | 4 | ->select('*') |
|
| 965 | 4 | ->from($this->tableName) |
|
| 966 | 4 | ->where( |
|
| 967 | 4 | $queryBuilder->expr()->eq('page_id', $queryBuilder->createNamedParameter($id, \PDO::PARAM_INT)) |
|
| 968 | ) |
||
| 969 | 4 | ->orderBy('scheduled', 'DESC'); |
|
| 970 | |||
| 971 | 4 | $expressionBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
|
| 972 | 4 | ->getConnectionForTable($this->tableName) |
|
| 973 | 4 | ->getExpressionBuilder(); |
|
| 974 | 4 | $query = $expressionBuilder->andX(); |
|
| 975 | // PHPStorm adds the highlight that the $addWhere is immediately overwritten, |
||
| 976 | // but the $query = $expressionBuilder->andX() ensures that the $addWhere is written correctly with AND |
||
| 977 | // between the statements, it's not a mistake in the code. |
||
| 978 | 4 | $addWhere = ''; |
|
| 979 | 4 | switch ($filter) { |
|
| 980 | 4 | case 'pending': |
|
| 981 | $queryBuilder->andWhere($queryBuilder->expr()->eq('exec_time', 0)); |
||
| 982 | $addWhere = ' AND ' . $query->add($expressionBuilder->eq('exec_time', 0)); |
||
| 983 | break; |
||
| 984 | 4 | case 'finished': |
|
| 985 | $queryBuilder->andWhere($queryBuilder->expr()->gt('exec_time', 0)); |
||
| 986 | $addWhere = ' AND ' . $query->add($expressionBuilder->gt('exec_time', 0)); |
||
| 987 | break; |
||
| 988 | } |
||
| 989 | |||
| 990 | // FIXME: Write unit test that ensures that the right records are deleted. |
||
| 991 | 4 | if ($doFlush) { |
|
| 992 | 2 | $addWhere = $query->add($expressionBuilder->eq('page_id', intval($id))); |
|
| 993 | 2 | $this->flushQueue($doFullFlush ? '1=1' : $addWhere); |
|
| 994 | 2 | return []; |
|
| 995 | } else { |
||
| 996 | 2 | if ($itemsPerPage > 0) { |
|
| 997 | $queryBuilder |
||
| 998 | 2 | ->setMaxResults((int)$itemsPerPage); |
|
| 999 | } |
||
| 1000 | |||
| 1001 | 2 | return $queryBuilder->execute()->fetchAll(); |
|
| 1002 | } |
||
| 1003 | } |
||
| 1004 | |||
| 1005 | /** |
||
| 1006 | * Return array of records from crawler queue for input set ID |
||
| 1007 | * |
||
| 1008 | * @param integer $set_id Set ID for which to look up log entries. |
||
| 1009 | * @param string $filter Filter: "all" => all entries, "pending" => all that is not yet run, "finished" => all complete ones |
||
| 1010 | * @param boolean $doFlush If TRUE, then entries selected at DELETED(!) instead of selected! |
||
| 1011 | * @param integer $itemsPerPage Limit the amount of entires per page default is 10 |
||
| 1012 | * @return array |
||
| 1013 | */ |
||
| 1014 | 6 | public function getLogEntriesForSetId($set_id, $filter = '', $doFlush = false, $doFullFlush = false, $itemsPerPage = 10) |
|
| 1015 | { |
||
| 1016 | 6 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName); |
|
| 1017 | $queryBuilder |
||
| 1018 | 6 | ->select('*') |
|
| 1019 | 6 | ->from($this->tableName) |
|
| 1020 | 6 | ->where( |
|
| 1021 | 6 | $queryBuilder->expr()->eq('set_id', $queryBuilder->createNamedParameter($set_id, \PDO::PARAM_INT)) |
|
| 1022 | ) |
||
| 1023 | 6 | ->orderBy('scheduled', 'DESC'); |
|
| 1024 | |||
| 1025 | 6 | $expressionBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
|
| 1026 | 6 | ->getConnectionForTable($this->tableName) |
|
| 1027 | 6 | ->getExpressionBuilder(); |
|
| 1028 | 6 | $query = $expressionBuilder->andX(); |
|
| 1029 | // FIXME: Write Unit tests for Filters |
||
| 1030 | // PHPStorm adds the highlight that the $addWhere is immediately overwritten, |
||
| 1031 | // but the $query = $expressionBuilder->andX() ensures that the $addWhere is written correctly with AND |
||
| 1032 | // between the statements, it's not a mistake in the code. |
||
| 1033 | 6 | $addWhere = ''; |
|
| 1034 | 6 | switch ($filter) { |
|
| 1035 | 6 | case 'pending': |
|
| 1036 | 1 | $queryBuilder->andWhere($queryBuilder->expr()->eq('exec_time', 0)); |
|
| 1037 | 1 | $addWhere = $query->add($expressionBuilder->eq('exec_time', 0)); |
|
| 1038 | 1 | break; |
|
| 1039 | 5 | case 'finished': |
|
| 1040 | 1 | $queryBuilder->andWhere($queryBuilder->expr()->gt('exec_time', 0)); |
|
| 1041 | 1 | $addWhere = $query->add($expressionBuilder->gt('exec_time', 0)); |
|
| 1042 | 1 | break; |
|
| 1043 | } |
||
| 1044 | // FIXME: Write unit test that ensures that the right records are deleted. |
||
| 1045 | 6 | if ($doFlush) { |
|
| 1046 | 4 | $addWhere = $query->add($expressionBuilder->eq('set_id', intval($set_id))); |
|
| 1047 | 4 | $this->flushQueue($doFullFlush ? '' : $addWhere); |
|
| 1048 | 4 | return []; |
|
| 1049 | } else { |
||
| 1050 | 2 | if ($itemsPerPage > 0) { |
|
| 1051 | $queryBuilder |
||
| 1052 | 2 | ->setMaxResults((int)$itemsPerPage); |
|
| 1053 | } |
||
| 1054 | |||
| 1055 | 2 | return $queryBuilder->execute()->fetchAll(); |
|
| 1056 | } |
||
| 1057 | } |
||
| 1058 | |||
| 1059 | /** |
||
| 1060 | * Removes queue entries |
||
| 1061 | * |
||
| 1062 | * @param string $where SQL related filter for the entries which should be removed |
||
| 1063 | * @return void |
||
| 1064 | */ |
||
| 1065 | 9 | protected function flushQueue($where = '') |
|
| 1066 | { |
||
| 1067 | 9 | $realWhere = strlen($where) > 0 ? $where : '1=1'; |
|
| 1068 | |||
| 1069 | 9 | $queryBuilder = $this->getQueryBuilder($this->tableName); |
|
| 1070 | |||
| 1071 | 9 | if (EventDispatcher::getInstance()->hasObserver('queueEntryFlush')) { |
|
| 1072 | $groups = $queryBuilder |
||
| 1073 | ->select('DISTINCT set_id') |
||
| 1074 | ->from($this->tableName) |
||
| 1075 | ->where($realWhere) |
||
| 1076 | ->execute() |
||
| 1077 | ->fetchAll(); |
||
| 1078 | if (is_array($groups)) { |
||
| 1079 | foreach ($groups as $group) { |
||
| 1080 | $subSet = $queryBuilder |
||
| 1081 | ->select('uid', 'set_id') |
||
| 1082 | ->from($this->tableName) |
||
| 1083 | ->where( |
||
| 1084 | $realWhere, |
||
| 1085 | $queryBuilder->expr()->eq('set_id', $group['set_id']) |
||
| 1086 | ) |
||
| 1087 | ->execute() |
||
| 1088 | ->fetchAll(); |
||
| 1089 | EventDispatcher::getInstance()->post('queueEntryFlush', $group['set_id'], $subSet); |
||
| 1090 | } |
||
| 1091 | } |
||
| 1092 | } |
||
| 1093 | |||
| 1094 | $queryBuilder |
||
| 1095 | 9 | ->delete($this->tableName) |
|
| 1096 | 9 | ->where($realWhere) |
|
| 1097 | 9 | ->execute(); |
|
| 1098 | 9 | } |
|
| 1099 | |||
| 1100 | /** |
||
| 1101 | * Adding call back entries to log (called from hooks typically, see indexed search class "class.crawler.php" |
||
| 1102 | * |
||
| 1103 | * @param integer $setId Set ID |
||
| 1104 | * @param array $params Parameters to pass to call back function |
||
| 1105 | * @param string $callBack Call back object reference, eg. 'EXT:indexed_search/class.crawler.php:&tx_indexedsearch_crawler' |
||
| 1106 | * @param integer $page_id Page ID to attach it to |
||
| 1107 | * @param integer $schedule Time at which to activate |
||
| 1108 | * @return void |
||
| 1109 | */ |
||
| 1110 | public function addQueueEntry_callBack($setId, $params, $callBack, $page_id = 0, $schedule = 0) |
||
| 1111 | { |
||
| 1112 | if (!is_array($params)) { |
||
| 1113 | $params = []; |
||
| 1114 | } |
||
| 1115 | $params['_CALLBACKOBJ'] = $callBack; |
||
| 1116 | |||
| 1117 | GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('tx_crawler_queue') |
||
| 1118 | ->insert( |
||
| 1119 | 'tx_crawler_queue', |
||
| 1120 | [ |
||
| 1121 | 'page_id' => intval($page_id), |
||
| 1122 | 'parameters' => serialize($params), |
||
| 1123 | 'scheduled' => intval($schedule) ? intval($schedule) : $this->getCurrentTime(), |
||
| 1124 | 'exec_time' => 0, |
||
| 1125 | 'set_id' => intval($setId), |
||
| 1126 | 'result_data' => '', |
||
| 1127 | ] |
||
| 1128 | ); |
||
| 1129 | } |
||
| 1130 | |||
| 1131 | /************************************ |
||
| 1132 | * |
||
| 1133 | * URL setting |
||
| 1134 | * |
||
| 1135 | ************************************/ |
||
| 1136 | |||
| 1137 | /** |
||
| 1138 | * Setting a URL for crawling: |
||
| 1139 | * |
||
| 1140 | * @param integer $id Page ID |
||
| 1141 | * @param string $url Complete URL |
||
| 1142 | * @param array $subCfg Sub configuration array (from TS config) |
||
| 1143 | * @param integer $tstamp Scheduled-time |
||
| 1144 | * @param string $configurationHash (optional) configuration hash |
||
| 1145 | * @param bool $skipInnerDuplicationCheck (optional) skip inner duplication check |
||
| 1146 | * @return bool |
||
| 1147 | */ |
||
| 1148 | 2 | public function addUrl( |
|
| 1149 | $id, |
||
| 1150 | $url, |
||
| 1151 | array $subCfg, |
||
| 1152 | $tstamp, |
||
| 1153 | $configurationHash = '', |
||
| 1154 | $skipInnerDuplicationCheck = false |
||
| 1155 | ) { |
||
| 1156 | 2 | $urlAdded = false; |
|
| 1157 | 2 | $rows = []; |
|
| 1158 | |||
| 1159 | // Creating parameters: |
||
| 1160 | $parameters = [ |
||
| 1161 | 2 | 'url' => $url |
|
| 1162 | ]; |
||
| 1163 | |||
| 1164 | // fe user group simulation: |
||
| 1165 | 2 | $uGs = implode(',', array_unique(GeneralUtility::intExplode(',', $subCfg['userGroups'], true))); |
|
| 1166 | 2 | if ($uGs) { |
|
| 1167 | $parameters['feUserGroupList'] = $uGs; |
||
| 1168 | } |
||
| 1169 | |||
| 1170 | // Setting processing instructions |
||
| 1171 | 2 | $parameters['procInstructions'] = GeneralUtility::trimExplode(',', $subCfg['procInstrFilter']); |
|
| 1172 | 2 | if (is_array($subCfg['procInstrParams.'])) { |
|
| 1173 | 2 | $parameters['procInstrParams'] = $subCfg['procInstrParams.']; |
|
| 1174 | } |
||
| 1175 | |||
| 1176 | // Possible TypoScript Template Parents |
||
| 1177 | 2 | $parameters['rootTemplatePid'] = $subCfg['rootTemplatePid']; |
|
| 1178 | |||
| 1179 | // Compile value array: |
||
| 1180 | 2 | $parameters_serialized = serialize($parameters); |
|
| 1181 | $fieldArray = [ |
||
| 1182 | 2 | 'page_id' => intval($id), |
|
| 1183 | 2 | 'parameters' => $parameters_serialized, |
|
| 1184 | 2 | 'parameters_hash' => GeneralUtility::shortMD5($parameters_serialized), |
|
| 1185 | 2 | 'configuration_hash' => $configurationHash, |
|
| 1186 | 2 | 'scheduled' => $tstamp, |
|
| 1187 | 2 | 'exec_time' => 0, |
|
| 1188 | 2 | 'set_id' => intval($this->setID), |
|
| 1189 | 2 | 'result_data' => '', |
|
| 1190 | 2 | 'configuration' => $subCfg['key'], |
|
| 1191 | ]; |
||
| 1192 | |||
| 1193 | 2 | if ($this->registerQueueEntriesInternallyOnly) { |
|
| 1194 | //the entries will only be registered and not stored to the database |
||
| 1195 | $this->queueEntries[] = $fieldArray; |
||
| 1196 | } else { |
||
| 1197 | 2 | if (!$skipInnerDuplicationCheck) { |
|
| 1198 | // check if there is already an equal entry |
||
| 1199 | 2 | $rows = $this->getDuplicateRowsIfExist($tstamp, $fieldArray); |
|
| 1200 | } |
||
| 1201 | |||
| 1202 | 2 | if (empty($rows)) { |
|
| 1203 | 2 | $connectionForCrawlerQueue = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('tx_crawler_queue'); |
|
| 1204 | 2 | $connectionForCrawlerQueue->insert( |
|
| 1205 | 2 | 'tx_crawler_queue', |
|
| 1206 | 2 | $fieldArray |
|
| 1207 | ); |
||
| 1208 | 2 | $uid = $connectionForCrawlerQueue->lastInsertId('tx_crawler_queue', 'qid'); |
|
| 1209 | 2 | $rows[] = $uid; |
|
| 1210 | 2 | $urlAdded = true; |
|
| 1211 | 2 | EventDispatcher::getInstance()->post('urlAddedToQueue', $this->setID, ['uid' => $uid, 'fieldArray' => $fieldArray]); |
|
| 1212 | } else { |
||
| 1213 | EventDispatcher::getInstance()->post('duplicateUrlInQueue', $this->setID, ['rows' => $rows, 'fieldArray' => $fieldArray]); |
||
| 1214 | } |
||
| 1215 | } |
||
| 1216 | |||
| 1217 | 2 | return $urlAdded; |
|
| 1218 | } |
||
| 1219 | |||
| 1220 | /** |
||
| 1221 | * This method determines duplicates for a queue entry with the same parameters and this timestamp. |
||
| 1222 | * If the timestamp is in the past, it will check if there is any unprocessed queue entry in the past. |
||
| 1223 | * If the timestamp is in the future it will check, if the queued entry has exactly the same timestamp |
||
| 1224 | * |
||
| 1225 | * @param int $tstamp |
||
| 1226 | * @param array $fieldArray |
||
| 1227 | * |
||
| 1228 | * @return array |
||
| 1229 | * |
||
| 1230 | * TODO: Write Functional Tests |
||
| 1231 | */ |
||
| 1232 | 2 | protected function getDuplicateRowsIfExist($tstamp, $fieldArray) |
|
| 1233 | { |
||
| 1234 | 2 | $rows = []; |
|
| 1235 | |||
| 1236 | 2 | $currentTime = $this->getCurrentTime(); |
|
| 1237 | |||
| 1238 | 2 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName); |
|
| 1239 | $queryBuilder |
||
| 1240 | 2 | ->select('qid') |
|
| 1241 | 2 | ->from('tx_crawler_queue'); |
|
| 1242 | //if this entry is scheduled with "now" |
||
| 1243 | 2 | if ($tstamp <= $currentTime) { |
|
| 1244 | if ($this->extensionSettings['enableTimeslot']) { |
||
| 1245 | $timeBegin = $currentTime - 100; |
||
| 1246 | $timeEnd = $currentTime + 100; |
||
| 1247 | $queryBuilder |
||
| 1248 | ->where( |
||
| 1249 | 'scheduled BETWEEN ' . $timeBegin . ' AND ' . $timeEnd . '' |
||
| 1250 | ) |
||
| 1251 | ->orWhere( |
||
| 1252 | $queryBuilder->expr()->lte('scheduled', $currentTime) |
||
| 1253 | ); |
||
| 1254 | } else { |
||
| 1255 | $queryBuilder |
||
| 1256 | ->where( |
||
| 1257 | $queryBuilder->expr()->lte('scheduled', $currentTime) |
||
| 1258 | ); |
||
| 1259 | } |
||
| 1260 | 2 | } elseif ($tstamp > $currentTime) { |
|
| 1261 | //entry with a timestamp in the future need to have the same schedule time |
||
| 1262 | $queryBuilder |
||
| 1263 | 2 | ->where( |
|
| 1264 | 2 | $queryBuilder->expr()->eq('scheduled', $tstamp) |
|
| 1265 | ); |
||
| 1266 | } |
||
| 1267 | |||
| 1268 | $statement = $queryBuilder |
||
| 1269 | 2 | ->andWhere('exec_time != 0') |
|
| 1270 | 2 | ->andWhere('process_id != 0') |
|
| 1271 | 2 | ->andWhere($queryBuilder->expr()->eq('page_id', $queryBuilder->createNamedParameter($fieldArray['page_id'], \PDO::PARAM_INT))) |
|
| 1272 | 2 | ->andWhere($queryBuilder->expr()->eq('parameters_hash', $queryBuilder->createNamedParameter($fieldArray['parameters_hash'], \PDO::PARAM_STR))) |
|
| 1273 | 2 | ->execute(); |
|
| 1274 | |||
| 1275 | 2 | while ($row = $statement->fetch()) { |
|
| 1276 | $rows[] = $row['qid']; |
||
| 1277 | } |
||
| 1278 | |||
| 1279 | 2 | return $rows; |
|
| 1280 | } |
||
| 1281 | |||
| 1282 | /** |
||
| 1283 | * Returns the current system time |
||
| 1284 | * |
||
| 1285 | * @return int |
||
| 1286 | */ |
||
| 1287 | public function getCurrentTime() |
||
| 1288 | { |
||
| 1289 | return time(); |
||
| 1290 | } |
||
| 1291 | |||
| 1292 | /************************************ |
||
| 1293 | * |
||
| 1294 | * URL reading |
||
| 1295 | * |
||
| 1296 | ************************************/ |
||
| 1297 | |||
| 1298 | /** |
||
| 1299 | * Read URL for single queue entry |
||
| 1300 | * |
||
| 1301 | * @param integer $queueId |
||
| 1302 | * @param boolean $force If set, will process even if exec_time has been set! |
||
| 1303 | * @return integer |
||
| 1304 | */ |
||
| 1305 | public function readUrl($queueId, $force = false) |
||
| 1306 | { |
||
| 1307 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName); |
||
| 1308 | $ret = 0; |
||
| 1309 | $this->logger->debug('crawler-readurl start ' . microtime(true)); |
||
| 1310 | // Get entry: |
||
| 1311 | $queryBuilder |
||
| 1312 | ->select('*') |
||
| 1313 | ->from('tx_crawler_queue') |
||
| 1314 | ->where( |
||
| 1315 | $queryBuilder->expr()->eq('qid', $queryBuilder->createNamedParameter($queueId, \PDO::PARAM_INT)) |
||
| 1316 | ); |
||
| 1317 | if (!$force) { |
||
| 1318 | $queryBuilder |
||
| 1319 | ->andWhere('exec_time = 0') |
||
| 1320 | ->andWhere('process_scheduled > 0'); |
||
| 1321 | } |
||
| 1322 | $queueRec = $queryBuilder->execute()->fetch(); |
||
| 1323 | |||
| 1324 | if (!is_array($queueRec)) { |
||
| 1325 | return; |
||
| 1326 | } |
||
| 1327 | |||
| 1328 | $parameters = unserialize($queueRec['parameters']); |
||
| 1329 | if ($parameters['rootTemplatePid']) { |
||
| 1330 | $this->initTSFE((int)$parameters['rootTemplatePid']); |
||
| 1331 | } else { |
||
| 1332 | $this->logger->warning( |
||
| 1333 | 'Page with (' . $queueRec['page_id'] . ') could not be crawled, please check your crawler configuration. Perhaps no Root Template Pid is set' |
||
| 1334 | ); |
||
| 1335 | } |
||
| 1336 | |||
| 1337 | SignalSlotUtility::emitSignal( |
||
| 1338 | __CLASS__, |
||
| 1339 | SignalSlotUtility::SIGNNAL_QUEUEITEM_PREPROCESS, |
||
| 1340 | [$queueId, &$queueRec] |
||
| 1341 | ); |
||
| 1342 | |||
| 1343 | // Set exec_time to lock record: |
||
| 1344 | $field_array = ['exec_time' => $this->getCurrentTime()]; |
||
| 1345 | |||
| 1346 | if (isset($this->processID)) { |
||
| 1347 | //if mulitprocessing is used we need to store the id of the process which has handled this entry |
||
| 1348 | $field_array['process_id_completed'] = $this->processID; |
||
| 1349 | } |
||
| 1350 | |||
| 1351 | GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('tx_crawler_queue') |
||
| 1352 | ->update( |
||
| 1353 | 'tx_crawler_queue', |
||
| 1354 | $field_array, |
||
| 1355 | [ 'qid' => (int)$queueId ] |
||
| 1356 | ); |
||
| 1357 | |||
| 1358 | $result = $this->readUrl_exec($queueRec); |
||
| 1359 | $resultData = unserialize($result['content']); |
||
| 1360 | |||
| 1361 | //atm there's no need to point to specific pollable extensions |
||
| 1362 | if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['pollSuccess'])) { |
||
| 1363 | foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['pollSuccess'] as $pollable) { |
||
| 1364 | // only check the success value if the instruction is runnig |
||
| 1365 | // it is important to name the pollSuccess key same as the procInstructions key |
||
| 1366 | if (is_array($resultData['parameters']['procInstructions']) && in_array( |
||
| 1367 | $pollable, |
||
| 1368 | $resultData['parameters']['procInstructions'] |
||
| 1369 | ) |
||
| 1370 | ) { |
||
| 1371 | if (!empty($resultData['success'][$pollable]) && $resultData['success'][$pollable]) { |
||
| 1372 | $ret |= self::CLI_STATUS_POLLABLE_PROCESSED; |
||
| 1373 | } |
||
| 1374 | } |
||
| 1375 | } |
||
| 1376 | } |
||
| 1377 | |||
| 1378 | // Set result in log which also denotes the end of the processing of this entry. |
||
| 1379 | $field_array = ['result_data' => serialize($result)]; |
||
| 1380 | |||
| 1381 | SignalSlotUtility::emitSignal( |
||
| 1382 | __CLASS__, |
||
| 1383 | SignalSlotUtility::SIGNNAL_QUEUEITEM_POSTPROCESS, |
||
| 1384 | [$queueId, &$field_array] |
||
| 1385 | ); |
||
| 1386 | |||
| 1387 | GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('tx_crawler_queue') |
||
| 1388 | ->update( |
||
| 1389 | 'tx_crawler_queue', |
||
| 1390 | $field_array, |
||
| 1391 | [ 'qid' => (int)$queueId ] |
||
| 1392 | ); |
||
| 1393 | |||
| 1394 | $this->logger->debug('crawler-readurl stop ' . microtime(true)); |
||
| 1395 | return $ret; |
||
| 1396 | } |
||
| 1397 | |||
| 1398 | /** |
||
| 1399 | * Read URL for not-yet-inserted log-entry |
||
| 1400 | * |
||
| 1401 | * @param array $field_array Queue field array, |
||
| 1402 | * |
||
| 1403 | * @return string |
||
| 1404 | */ |
||
| 1405 | public function readUrlFromArray($field_array) |
||
| 1406 | { |
||
| 1407 | |||
| 1408 | // Set exec_time to lock record: |
||
| 1409 | $field_array['exec_time'] = $this->getCurrentTime(); |
||
| 1410 | $connectionForCrawlerQueue = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('tx_crawler_queue'); |
||
| 1411 | $connectionForCrawlerQueue->insert( |
||
| 1412 | 'tx_crawler_queue', |
||
| 1413 | $field_array |
||
| 1414 | ); |
||
| 1415 | $queueId = $field_array['qid'] = $connectionForCrawlerQueue->lastInsertId('tx_crawler_queue', 'qid'); |
||
| 1416 | |||
| 1417 | $result = $this->readUrl_exec($field_array); |
||
| 1418 | |||
| 1419 | // Set result in log which also denotes the end of the processing of this entry. |
||
| 1420 | $field_array = ['result_data' => serialize($result)]; |
||
| 1421 | |||
| 1422 | SignalSlotUtility::emitSignal( |
||
| 1423 | __CLASS__, |
||
| 1424 | SignalSlotUtility::SIGNNAL_QUEUEITEM_POSTPROCESS, |
||
| 1425 | [$queueId, &$field_array] |
||
| 1426 | ); |
||
| 1427 | |||
| 1428 | $connectionForCrawlerQueue->update( |
||
| 1429 | 'tx_crawler_queue', |
||
| 1430 | $field_array, |
||
| 1431 | ['qid' => $queueId] |
||
| 1432 | ); |
||
| 1433 | |||
| 1434 | return $result; |
||
| 1435 | } |
||
| 1436 | |||
| 1437 | /** |
||
| 1438 | * Read URL for a queue record |
||
| 1439 | * |
||
| 1440 | * @param array $queueRec Queue record |
||
| 1441 | * @return string |
||
| 1442 | */ |
||
| 1443 | public function readUrl_exec($queueRec) |
||
| 1472 | |||
| 1473 | /** |
||
| 1474 | * Gets the content of a URL. |
||
| 1475 | * |
||
| 1476 | * @param string $originalUrl URL to read |
||
| 1477 | * @param string $crawlerId Crawler ID string (qid + hash to verify) |
||
| 1478 | * @param integer $timeout Timeout time |
||
| 1479 | * @param integer $recursion Recursion limiter for 302 redirects |
||
| 1480 | * @return array|boolean |
||
| 1481 | */ |
||
| 1482 | 2 | public function requestUrl($originalUrl, $crawlerId, $timeout = 2, $recursion = 10) |
|
| 1483 | { |
||
| 1484 | 2 | if (!$recursion) { |
|
| 1485 | return false; |
||
| 1486 | } |
||
| 1487 | |||
| 1488 | // Parse URL, checking for scheme: |
||
| 1489 | 2 | $url = parse_url($originalUrl); |
|
| 1490 | |||
| 1491 | 2 | if ($url === false) { |
|
| 1492 | $this->logger->debug( |
||
| 1493 | sprintf('Could not parse_url() for string "%s"', $url), |
||
| 1494 | ['crawlerId' => $crawlerId] |
||
| 1495 | ); |
||
| 1496 | return false; |
||
| 1497 | } |
||
| 1498 | |||
| 1499 | 2 | if (!in_array($url['scheme'], ['','http','https'])) { |
|
| 1500 | $this->logger->debug( |
||
| 1501 | sprintf('Scheme does not match for url "%s"', $url), |
||
| 1502 | ['crawlerId' => $crawlerId] |
||
| 1579 | |||
| 1580 | /** |
||
| 1581 | * Gets the base path of the website frontend. |
||
| 1582 | * (e.g. if you call http://mydomain.com/cms/index.php in |
||
| 1583 | * the browser the base path is "/cms/") |
||
| 1584 | * |
||
| 1585 | * @return string Base path of the website frontend |
||
| 1586 | */ |
||
| 1587 | protected function getFrontendBasePath() |
||
| 1610 | |||
| 1611 | /** |
||
| 1612 | * Executes a shell command and returns the outputted result. |
||
| 1613 | * |
||
| 1614 | * @param string $command Shell command to be executed |
||
| 1615 | * @return string Outputted result of the command execution |
||
| 1616 | */ |
||
| 1617 | protected function executeShellCommand($command) |
||
| 1621 | |||
| 1622 | /** |
||
| 1623 | * Reads HTTP response from the given stream. |
||
| 1624 | * |
||
| 1625 | * @param resource $streamPointer Pointer to connection stream. |
||
| 1626 | * @return array Associative array with the following items: |
||
| 1627 | * headers <array> Response headers sent by server. |
||
| 1628 | * content <array> Content, with each line as an array item. |
||
| 1629 | */ |
||
| 1630 | 1 | protected function getHttpResponseFromStream($streamPointer) |
|
| 1653 | |||
| 1654 | /** |
||
| 1655 | * Builds HTTP request headers. |
||
| 1656 | * |
||
| 1657 | * @param array $url |
||
| 1658 | * @param string $crawlerId |
||
| 1659 | * |
||
| 1660 | * @return array |
||
| 1661 | */ |
||
| 1662 | 6 | protected function buildRequestHeaderArray(array $url, $crawlerId) |
|
| 1678 | |||
| 1679 | /** |
||
| 1680 | * Check if the submitted HTTP-Header contains a redirect location and built new crawler-url |
||
| 1681 | * |
||
| 1682 | * @param array $headers HTTP Header |
||
| 1683 | * @param string $user HTTP Auth. User |
||
| 1684 | * @param string $pass HTTP Auth. Password |
||
| 1685 | * @return bool|string |
||
| 1686 | */ |
||
| 1687 | 12 | protected function getRequestUrlFrom302Header($headers, $user = '', $pass = '') |
|
| 1721 | |||
| 1722 | /************************** |
||
| 1723 | * |
||
| 1724 | * tslib_fe hooks: |
||
| 1725 | * |
||
| 1726 | **************************/ |
||
| 1727 | |||
| 1728 | /** |
||
| 1729 | * Initialization hook (called after database connection) |
||
| 1730 | * Takes the "HTTP_X_T3CRAWLER" header and looks up queue record and verifies if the session comes from the system (by comparing hashes) |
||
| 1731 | * |
||
| 1732 | * @param array $params Parameters from frontend |
||
| 1733 | * @param object $ref TSFE object (reference under PHP5) |
||
| 1734 | * @return void |
||
| 1735 | * |
||
| 1736 | * FIXME: Look like this is not used, in commit 9910d3f40cce15f4e9b7bcd0488bf21f31d53ebc it's added as public, |
||
| 1737 | * FIXME: I think this can be removed. (TNM) |
||
| 1738 | */ |
||
| 1739 | public function fe_init(&$params, $ref) |
||
| 1765 | |||
| 1766 | /***************************** |
||
| 1767 | * |
||
| 1768 | * Compiling URLs to crawl - tools |
||
| 1769 | * |
||
| 1770 | *****************************/ |
||
| 1771 | |||
| 1772 | /** |
||
| 1773 | * @param integer $id Root page id to start from. |
||
| 1774 | * @param integer $depth Depth of tree, 0=only id-page, 1= on sublevel, 99 = infinite |
||
| 1775 | * @param integer $scheduledTime Unix Time when the URL is timed to be visited when put in queue |
||
| 1776 | * @param integer $reqMinute Number of requests per minute (creates the interleave between requests) |
||
| 1777 | * @param boolean $submitCrawlUrls If set, submits the URLs to queue in database (real crawling) |
||
| 1778 | * @param boolean $downloadCrawlUrls If set (and submitcrawlUrls is false) will fill $downloadUrls with entries) |
||
| 1779 | * @param array $incomingProcInstructions Array of processing instructions |
||
| 1780 | * @param array $configurationSelection Array of configuration keys |
||
| 1781 | * @return string |
||
| 1782 | */ |
||
| 1783 | public function getPageTreeAndUrls( |
||
| 1874 | |||
| 1875 | /** |
||
| 1876 | * Expands exclude string |
||
| 1877 | * |
||
| 1878 | * @param string $excludeString Exclude string |
||
| 1879 | * @return array |
||
| 1880 | */ |
||
| 1881 | 1 | public function expandExcludeString($excludeString) |
|
| 1926 | |||
| 1927 | /** |
||
| 1928 | * Create the rows for display of the page tree |
||
| 1929 | * For each page a number of rows are shown displaying GET variable configuration |
||
| 1930 | * |
||
| 1931 | * @param array Page row |
||
| 1932 | * @param string Page icon and title for row |
||
| 1933 | * @return string HTML <tr> content (one or more) |
||
| 1934 | */ |
||
| 1935 | public function drawURLs_addRowsForPage(array $pageRow, $pageTitleAndIcon) |
||
| 2044 | |||
| 2045 | /***************************** |
||
| 2046 | * |
||
| 2047 | * CLI functions |
||
| 2048 | * |
||
| 2049 | *****************************/ |
||
| 2050 | |||
| 2051 | /** |
||
| 2052 | * Running the functionality of the CLI (crawling URLs from queue) |
||
| 2053 | * |
||
| 2054 | * @param int $countInARun |
||
| 2055 | * @param int $sleepTime |
||
| 2056 | * @param int $sleepAfterFinish |
||
| 2057 | * @return string |
||
| 2058 | */ |
||
| 2059 | public function CLI_run($countInARun, $sleepTime, $sleepAfterFinish) |
||
| 2177 | |||
| 2178 | /** |
||
| 2179 | * Activate hooks |
||
| 2180 | * |
||
| 2181 | * @return void |
||
| 2182 | */ |
||
| 2183 | public function CLI_runHooks() |
||
| 2192 | |||
| 2193 | /** |
||
| 2194 | * Try to acquire a new process with the given id |
||
| 2195 | * also performs some auto-cleanup for orphan processes |
||
| 2196 | * @todo preemption might not be the most elegant way to clean up |
||
| 2197 | * |
||
| 2198 | * @param string $id identification string for the process |
||
| 2199 | * @return boolean |
||
| 2200 | */ |
||
| 2201 | public function CLI_checkAndAcquireNewProcess($id) |
||
| 2258 | |||
| 2259 | /** |
||
| 2260 | * Release a process and the required resources |
||
| 2261 | * |
||
| 2262 | * @param mixed $releaseIds string with a single process-id or array with multiple process-ids |
||
| 2263 | * @param boolean $withinLock show whether the DB-actions are included within an existing lock |
||
| 2264 | * @return boolean |
||
| 2265 | */ |
||
| 2266 | public function CLI_releaseProcesses($releaseIds, $withinLock = false) |
||
| 2353 | |||
| 2354 | /** |
||
| 2355 | * Check if there are still resources left for the process with the given id |
||
| 2356 | * Used to determine timeouts and to ensure a proper cleanup if there's a timeout |
||
| 2357 | * |
||
| 2358 | * @param string identification string for the process |
||
| 2359 | * @return boolean determines if the process is still active / has resources |
||
| 2360 | * |
||
| 2361 | * TODO: Please consider moving this to Domain Model for Process or in ProcessRepository |
||
| 2362 | */ |
||
| 2363 | 1 | public function CLI_checkIfProcessIsActive($pid) |
|
| 2383 | |||
| 2384 | /** |
||
| 2385 | * Create a unique Id for the current process |
||
| 2386 | * |
||
| 2387 | * @return string the ID |
||
| 2388 | */ |
||
| 2389 | 2 | public function CLI_buildProcessId() |
|
| 2396 | |||
| 2397 | /** |
||
| 2398 | * @param bool $get_as_float |
||
| 2399 | * |
||
| 2400 | * @return mixed |
||
| 2401 | */ |
||
| 2402 | protected function microtime($get_as_float = false) |
||
| 2406 | |||
| 2407 | /** |
||
| 2408 | * Prints a message to the stdout (only if debug-mode is enabled) |
||
| 2409 | * |
||
| 2410 | * @param string $msg the message |
||
| 2411 | */ |
||
| 2412 | public function CLI_debug($msg) |
||
| 2419 | |||
| 2420 | /** |
||
| 2421 | * Get URL content by making direct request to TYPO3. |
||
| 2422 | * |
||
| 2423 | * @param string $url Page URL |
||
| 2424 | * @param int $crawlerId Crawler-ID |
||
| 2425 | * @return array |
||
| 2426 | */ |
||
| 2427 | 2 | protected function sendDirectRequest($url, $crawlerId) |
|
| 2458 | |||
| 2459 | /** |
||
| 2460 | * Cleans up entries that stayed for too long in the queue. These are: |
||
| 2461 | * - processed entries that are over 1.5 days in age |
||
| 2462 | * - scheduled entries that are over 7 days old |
||
| 2463 | * |
||
| 2464 | * @return void |
||
| 2465 | */ |
||
| 2466 | public function cleanUpOldQueueEntries() |
||
| 2475 | |||
| 2476 | /** |
||
| 2477 | * Initializes a TypoScript Frontend necessary for using TypoScript and TypoLink functions |
||
| 2478 | * |
||
| 2479 | * @param int $pageId |
||
| 2480 | * @return void |
||
| 2481 | * @throws \TYPO3\CMS\Core\Error\Http\ServiceUnavailableException |
||
| 2482 | * @throws \TYPO3\CMS\Core\Http\ImmediateResponseException |
||
| 2483 | */ |
||
| 2484 | protected function initTSFE(int $pageId): void |
||
| 2499 | |||
| 2500 | /** |
||
| 2501 | * Returns a md5 hash generated from a serialized configuration array. |
||
| 2502 | * |
||
| 2503 | * @param array $configuration |
||
| 2504 | * |
||
| 2505 | * @return string |
||
| 2506 | */ |
||
| 2507 | 7 | protected function getConfigurationHash(array $configuration) |
|
| 2513 | } |
||
| 2514 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.