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 |
||
58 | class CrawlerController |
||
59 | { |
||
60 | const CLI_STATUS_NOTHING_PROCCESSED = 0; |
||
61 | const CLI_STATUS_REMAIN = 1; //queue not empty |
||
62 | const CLI_STATUS_PROCESSED = 2; //(some) queue items where processed |
||
63 | const CLI_STATUS_ABORTED = 4; //instance didn't finish |
||
64 | const CLI_STATUS_POLLABLE_PROCESSED = 8; |
||
65 | |||
66 | /** |
||
67 | * @var integer |
||
68 | */ |
||
69 | public $setID = 0; |
||
70 | |||
71 | /** |
||
72 | * @var string |
||
73 | */ |
||
74 | public $processID = ''; |
||
75 | |||
76 | /** |
||
77 | * One hour is max stalled time for the CLI |
||
78 | * If the process had the status "start" for 3600 seconds, it will be regarded stalled and a new process is started |
||
79 | * |
||
80 | * @var integer |
||
81 | */ |
||
82 | public $max_CLI_exec_time = 3600; |
||
83 | |||
84 | /** |
||
85 | * @var array |
||
86 | */ |
||
87 | public $duplicateTrack = []; |
||
88 | |||
89 | /** |
||
90 | * @var array |
||
91 | */ |
||
92 | public $downloadUrls = []; |
||
93 | |||
94 | /** |
||
95 | * @var array |
||
96 | */ |
||
97 | public $incomingProcInstructions = []; |
||
98 | |||
99 | /** |
||
100 | * @var array |
||
101 | */ |
||
102 | public $incomingConfigurationSelection = []; |
||
103 | |||
104 | /** |
||
105 | * @var bool |
||
106 | */ |
||
107 | public $registerQueueEntriesInternallyOnly = false; |
||
108 | |||
109 | /** |
||
110 | * @var array |
||
111 | */ |
||
112 | public $queueEntries = []; |
||
113 | |||
114 | /** |
||
115 | * @var array |
||
116 | */ |
||
117 | public $urlList = []; |
||
118 | |||
119 | /** |
||
120 | * @var boolean |
||
121 | */ |
||
122 | public $debugMode = false; |
||
123 | |||
124 | /** |
||
125 | * @var array |
||
126 | */ |
||
127 | public $extensionSettings = []; |
||
128 | |||
129 | /** |
||
130 | * Mount Point |
||
131 | * |
||
132 | * @var boolean |
||
133 | */ |
||
134 | public $MP = false; |
||
135 | |||
136 | /** |
||
137 | * @var string |
||
138 | */ |
||
139 | protected $processFilename; |
||
140 | |||
141 | /** |
||
142 | * Holds the internal access mode can be 'gui','cli' or 'cli_im' |
||
143 | * |
||
144 | * @var string |
||
145 | */ |
||
146 | protected $accessMode; |
||
147 | |||
148 | /** |
||
149 | * @var DatabaseConnection |
||
150 | */ |
||
151 | private $db; |
||
152 | |||
153 | /** |
||
154 | * @var BackendUserAuthentication |
||
155 | */ |
||
156 | private $backendUser; |
||
157 | |||
158 | /** |
||
159 | * @var integer |
||
160 | */ |
||
161 | private $scheduledTime = 0; |
||
162 | |||
163 | /** |
||
164 | * @var integer |
||
165 | */ |
||
166 | private $reqMinute = 0; |
||
167 | |||
168 | /** |
||
169 | * @var bool |
||
170 | */ |
||
171 | private $submitCrawlUrls = false; |
||
172 | |||
173 | /** |
||
174 | * @var bool |
||
175 | */ |
||
176 | private $downloadCrawlUrls = false; |
||
177 | |||
178 | /** |
||
179 | * @var QueueRepository |
||
180 | */ |
||
181 | protected $queueRepository; |
||
182 | |||
183 | /** |
||
184 | * Method to set the accessMode can be gui, cli or cli_im |
||
185 | * |
||
186 | * @return string |
||
187 | */ |
||
188 | 1 | public function getAccessMode() |
|
192 | |||
193 | /** |
||
194 | * @param string $accessMode |
||
195 | */ |
||
196 | 1 | public function setAccessMode($accessMode) |
|
200 | |||
201 | /** |
||
202 | * Set disabled status to prevent processes from being processed |
||
203 | * |
||
204 | * @param bool $disabled (optional, defaults to true) |
||
205 | * @return void |
||
206 | */ |
||
207 | 3 | public function setDisabled($disabled = true) |
|
217 | |||
218 | /** |
||
219 | * Get disable status |
||
220 | * |
||
221 | * @return bool true if disabled |
||
222 | */ |
||
223 | 3 | public function getDisabled() |
|
231 | |||
232 | /** |
||
233 | * @param string $filenameWithPath |
||
234 | * |
||
235 | * @return void |
||
236 | */ |
||
237 | 4 | public function setProcessFilename($filenameWithPath) |
|
241 | |||
242 | /** |
||
243 | * @return string |
||
244 | */ |
||
245 | 1 | public function getProcessFilename() |
|
249 | |||
250 | /************************************ |
||
251 | * |
||
252 | * Getting URLs based on Page TSconfig |
||
253 | * |
||
254 | ************************************/ |
||
255 | |||
256 | 24 | public function __construct() |
|
278 | |||
279 | /** |
||
280 | * Sets the extensions settings (unserialized pendant of $TYPO3_CONF_VARS['EXT']['extConf']['crawler']). |
||
281 | * |
||
282 | * @param array $extensionSettings |
||
283 | * @return void |
||
284 | */ |
||
285 | 33 | public function setExtensionSettings(array $extensionSettings) |
|
289 | |||
290 | /** |
||
291 | * Check if the given page should be crawled |
||
292 | * |
||
293 | * @param array $pageRow |
||
294 | * @return false|string false if the page should be crawled (not excluded), true / skipMessage if it should be skipped |
||
295 | */ |
||
296 | 6 | public function checkIfPageShouldBeSkipped(array $pageRow) |
|
353 | |||
354 | /** |
||
355 | * Wrapper method for getUrlsForPageId() |
||
356 | * It returns an array of configurations and no urls! |
||
357 | * |
||
358 | * @param array $pageRow Page record with at least dok-type and uid columns. |
||
359 | * @param string $skipMessage |
||
360 | * @return array |
||
361 | * @see getUrlsForPageId() |
||
362 | */ |
||
363 | 2 | public function getUrlsForPageRow(array $pageRow, &$skipMessage = '') |
|
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 | 3 | protected function noUnprocessedQueueEntriesForPageWithConfigurationHashExist($uid, $configurationHash) |
|
396 | |||
397 | /** |
||
398 | * Creates a list of URLs from input array (and submits them to queue if asked for) |
||
399 | * See Web > Info module script + "indexed_search"'s crawler hook-client using this! |
||
400 | * |
||
401 | * @param array Information about URLs from pageRow to crawl. |
||
402 | * @param array Page row |
||
403 | * @param integer Unix time to schedule indexing to, typically time() |
||
404 | * @param integer Number of requests per minute (creates the interleave between requests) |
||
405 | * @param boolean If set, submits the URLs to queue |
||
406 | * @param boolean If set (and submitcrawlUrls is false) will fill $downloadUrls with entries) |
||
407 | * @param array Array which is passed by reference and contains the an id per url to secure we will not crawl duplicates |
||
408 | * @param array Array which will be filled with URLS for download if flag is set. |
||
409 | * @param array Array of processing instructions |
||
410 | * @return string List of URLs (meant for display in backend module) |
||
411 | * |
||
412 | */ |
||
413 | public function urlListFromUrlArray( |
||
414 | array $vv, |
||
415 | array $pageRow, |
||
416 | $scheduledTime, |
||
417 | $reqMinute, |
||
418 | $submitCrawlUrls, |
||
419 | $downloadCrawlUrls, |
||
420 | array &$duplicateTrack, |
||
421 | array &$downloadUrls, |
||
422 | array $incomingProcInstructions |
||
423 | ) { |
||
424 | $urlList = ''; |
||
425 | // realurl support (thanks to Ingo Renner) |
||
426 | if (ExtensionManagementUtility::isLoaded('realurl') && $vv['subCfg']['realurl']) { |
||
427 | |||
428 | /** @var tx_realurl $urlObj */ |
||
429 | $urlObj = GeneralUtility::makeInstance('tx_realurl'); |
||
430 | |||
431 | if (!empty($vv['subCfg']['baseUrl'])) { |
||
432 | $urlParts = parse_url($vv['subCfg']['baseUrl']); |
||
433 | $host = strtolower($urlParts['host']); |
||
434 | $urlObj->host = $host; |
||
435 | |||
436 | // First pass, finding configuration OR pointer string: |
||
437 | $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']; |
||
438 | |||
439 | // If it turned out to be a string pointer, then look up the real config: |
||
440 | if (is_string($urlObj->extConf)) { |
||
441 | $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']; |
||
442 | } |
||
443 | } |
||
444 | |||
445 | if (!$GLOBALS['TSFE']->sys_page) { |
||
446 | $GLOBALS['TSFE']->sys_page = GeneralUtility::makeInstance('TYPO3\CMS\Frontend\Page\PageRepository'); |
||
447 | } |
||
448 | if (!$GLOBALS['TSFE']->csConvObj) { |
||
449 | $GLOBALS['TSFE']->csConvObj = GeneralUtility::makeInstance('TYPO3\CMS\Core\Charset\CharsetConverter'); |
||
450 | } |
||
451 | if (!$GLOBALS['TSFE']->tmpl->rootLine[0]['uid']) { |
||
452 | $GLOBALS['TSFE']->tmpl->rootLine[0]['uid'] = $urlObj->extConf['pagePath']['rootpage_id']; |
||
453 | } |
||
454 | } |
||
455 | |||
456 | if (is_array($vv['URLs'])) { |
||
457 | $configurationHash = $this->getConfigurationHash($vv); |
||
458 | $skipInnerCheck = $this->noUnprocessedQueueEntriesForPageWithConfigurationHashExist($pageRow['uid'], $configurationHash); |
||
459 | |||
460 | foreach ($vv['URLs'] as $urlQuery) { |
||
461 | if ($this->drawURLs_PIfilter($vv['subCfg']['procInstrFilter'], $incomingProcInstructions)) { |
||
462 | |||
463 | // Calculate cHash: |
||
464 | if ($vv['subCfg']['cHash']) { |
||
465 | /* @var $cacheHash \TYPO3\CMS\Frontend\Page\CacheHashCalculator */ |
||
466 | $cacheHash = GeneralUtility::makeInstance('TYPO3\CMS\Frontend\Page\CacheHashCalculator'); |
||
467 | $urlQuery .= '&cHash=' . $cacheHash->generateForParameters($urlQuery); |
||
468 | } |
||
469 | |||
470 | // Create key by which to determine unique-ness: |
||
471 | $uKey = $urlQuery . '|' . $vv['subCfg']['userGroups'] . '|' . $vv['subCfg']['baseUrl'] . '|' . $vv['subCfg']['procInstrFilter']; |
||
472 | |||
473 | // realurl support (thanks to Ingo Renner) |
||
474 | $urlQuery = 'index.php' . $urlQuery; |
||
475 | if (ExtensionManagementUtility::isLoaded('realurl') && $vv['subCfg']['realurl']) { |
||
476 | $params = [ |
||
477 | 'LD' => [ |
||
478 | 'totalURL' => $urlQuery |
||
479 | ], |
||
480 | 'TCEmainHook' => true |
||
481 | ]; |
||
482 | $urlObj->encodeSpURL($params); |
||
|
|||
483 | $urlQuery = $params['LD']['totalURL']; |
||
484 | } |
||
485 | |||
486 | // Scheduled time: |
||
487 | $schTime = $scheduledTime + round(count($duplicateTrack) * (60 / $reqMinute)); |
||
488 | $schTime = floor($schTime / 60) * 60; |
||
489 | |||
490 | if (isset($duplicateTrack[$uKey])) { |
||
491 | |||
492 | //if the url key is registered just display it and do not resubmit is |
||
493 | $urlList = '<em><span class="typo3-dimmed">' . htmlspecialchars($urlQuery) . '</span></em><br/>'; |
||
494 | } else { |
||
495 | $urlList = '[' . date('d.m.y H:i', $schTime) . '] ' . htmlspecialchars($urlQuery); |
||
496 | $this->urlList[] = '[' . date('d.m.y H:i', $schTime) . '] ' . $urlQuery; |
||
497 | |||
498 | $theUrl = ($vv['subCfg']['baseUrl'] ? $vv['subCfg']['baseUrl'] : GeneralUtility::getIndpEnv('TYPO3_SITE_URL')) . $urlQuery; |
||
499 | |||
500 | // Submit for crawling! |
||
501 | if ($submitCrawlUrls) { |
||
502 | $added = $this->addUrl( |
||
503 | $pageRow['uid'], |
||
504 | $theUrl, |
||
505 | $vv['subCfg'], |
||
506 | $scheduledTime, |
||
507 | $configurationHash, |
||
508 | $skipInnerCheck |
||
509 | ); |
||
510 | if ($added === false) { |
||
511 | $urlList .= ' (Url already existed)'; |
||
512 | } |
||
513 | } elseif ($downloadCrawlUrls) { |
||
514 | $downloadUrls[$theUrl] = $theUrl; |
||
515 | } |
||
516 | |||
517 | $urlList .= '<br />'; |
||
518 | } |
||
519 | $duplicateTrack[$uKey] = true; |
||
520 | } |
||
521 | } |
||
522 | } else { |
||
523 | $urlList = 'ERROR - no URL generated'; |
||
524 | } |
||
525 | |||
526 | return $urlList; |
||
527 | } |
||
528 | |||
529 | /** |
||
530 | * Returns true if input processing instruction is among registered ones. |
||
531 | * |
||
532 | * @param string $piString PI to test |
||
533 | * @param array $incomingProcInstructions Processing instructions |
||
534 | * @return boolean |
||
535 | */ |
||
536 | 5 | public function drawURLs_PIfilter($piString, array $incomingProcInstructions) |
|
548 | |||
549 | public function getPageTSconfigForId($id) |
||
571 | |||
572 | /** |
||
573 | * This methods returns an array of configurations. |
||
574 | * And no urls! |
||
575 | * |
||
576 | * @param integer $id Page ID |
||
577 | * @param bool $forceSsl Use https |
||
578 | * @return array |
||
579 | * |
||
580 | * TODO: Should be switched back to protected - TNM 2018-11-16 |
||
581 | */ |
||
582 | public function getUrlsForPageId($id, $forceSsl = false) |
||
583 | { |
||
584 | |||
585 | /** |
||
586 | * Get configuration from tsConfig |
||
587 | */ |
||
588 | |||
589 | // Get page TSconfig for page ID: |
||
590 | $pageTSconfig = $this->getPageTSconfigForId($id); |
||
591 | |||
592 | $res = []; |
||
593 | |||
594 | if (is_array($pageTSconfig) && is_array($pageTSconfig['tx_crawler.']['crawlerCfg.'])) { |
||
595 | $crawlerCfg = $pageTSconfig['tx_crawler.']['crawlerCfg.']; |
||
596 | |||
597 | if (is_array($crawlerCfg['paramSets.'])) { |
||
598 | foreach ($crawlerCfg['paramSets.'] as $key => $values) { |
||
599 | if (is_array($values)) { |
||
600 | $key = str_replace('.', '', $key); |
||
601 | // Sub configuration for a single configuration string: |
||
602 | $subCfg = (array)$crawlerCfg['paramSets.'][$key . '.']; |
||
603 | $subCfg['key'] = $key; |
||
604 | |||
605 | if (strcmp($subCfg['procInstrFilter'], '')) { |
||
606 | $subCfg['procInstrFilter'] = implode(',', GeneralUtility::trimExplode(',', $subCfg['procInstrFilter'])); |
||
607 | } |
||
608 | $pidOnlyList = implode(',', GeneralUtility::trimExplode(',', $subCfg['pidsOnly'], true)); |
||
609 | |||
610 | // process configuration if it is not page-specific or if the specific page is the current page: |
||
611 | if (!strcmp($subCfg['pidsOnly'], '') || GeneralUtility::inList($pidOnlyList, $id)) { |
||
612 | |||
613 | // add trailing slash if not present |
||
614 | if (!empty($subCfg['baseUrl']) && substr($subCfg['baseUrl'], -1) != '/') { |
||
615 | $subCfg['baseUrl'] .= '/'; |
||
616 | } |
||
617 | |||
618 | // Explode, process etc.: |
||
619 | $res[$key] = []; |
||
620 | $res[$key]['subCfg'] = $subCfg; |
||
621 | $res[$key]['paramParsed'] = $this->parseParams($crawlerCfg['paramSets.'][$key]); |
||
622 | $res[$key]['paramExpanded'] = $this->expandParameters($res[$key]['paramParsed'], $id); |
||
623 | $res[$key]['origin'] = 'pagets'; |
||
624 | |||
625 | // recognize MP value |
||
626 | if (!$this->MP) { |
||
627 | $res[$key]['URLs'] = $this->compileUrls($res[$key]['paramExpanded'], ['?id=' . $id]); |
||
628 | } else { |
||
629 | $res[$key]['URLs'] = $this->compileUrls($res[$key]['paramExpanded'], ['?id=' . $id . '&MP=' . $this->MP]); |
||
630 | } |
||
631 | } |
||
632 | } |
||
633 | } |
||
634 | } |
||
635 | } |
||
636 | |||
637 | /** |
||
638 | * Get configuration from tx_crawler_configuration records |
||
639 | */ |
||
640 | |||
641 | // get records along the rootline |
||
642 | $rootLine = BackendUtility::BEgetRootLine($id); |
||
643 | |||
644 | foreach ($rootLine as $page) { |
||
645 | $configurationRecordsForCurrentPage = BackendUtility::getRecordsByField( |
||
646 | 'tx_crawler_configuration', |
||
647 | 'pid', |
||
648 | intval($page['uid']), |
||
649 | BackendUtility::BEenableFields('tx_crawler_configuration') . BackendUtility::deleteClause('tx_crawler_configuration') |
||
650 | ); |
||
651 | |||
652 | if (is_array($configurationRecordsForCurrentPage)) { |
||
653 | foreach ($configurationRecordsForCurrentPage as $configurationRecord) { |
||
654 | |||
655 | // check access to the configuration record |
||
656 | if (empty($configurationRecord['begroups']) || $GLOBALS['BE_USER']->isAdmin() || $this->hasGroupAccess($GLOBALS['BE_USER']->user['usergroup_cached_list'], $configurationRecord['begroups'])) { |
||
657 | $pidOnlyList = implode(',', GeneralUtility::trimExplode(',', $configurationRecord['pidsonly'], true)); |
||
658 | |||
659 | // process configuration if it is not page-specific or if the specific page is the current page: |
||
660 | if (!strcmp($configurationRecord['pidsonly'], '') || GeneralUtility::inList($pidOnlyList, $id)) { |
||
661 | $key = $configurationRecord['name']; |
||
662 | |||
663 | // don't overwrite previously defined paramSets |
||
664 | if (!isset($res[$key])) { |
||
665 | |||
666 | /* @var $TSparserObject \TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser */ |
||
667 | $TSparserObject = GeneralUtility::makeInstance('TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser'); |
||
668 | $TSparserObject->parse($configurationRecord['processing_instruction_parameters_ts']); |
||
669 | |||
670 | $isCrawlingProtocolHttps = $this->isCrawlingProtocolHttps($configurationRecord['force_ssl'], $forceSsl); |
||
671 | |||
672 | $subCfg = [ |
||
673 | 'procInstrFilter' => $configurationRecord['processing_instruction_filter'], |
||
674 | 'procInstrParams.' => $TSparserObject->setup, |
||
675 | 'baseUrl' => $this->getBaseUrlForConfigurationRecord( |
||
676 | $configurationRecord['base_url'], |
||
677 | $configurationRecord['sys_domain_base_url'], |
||
678 | $isCrawlingProtocolHttps |
||
679 | ), |
||
680 | 'realurl' => $configurationRecord['realurl'], |
||
681 | 'cHash' => $configurationRecord['chash'], |
||
682 | 'userGroups' => $configurationRecord['fegroups'], |
||
683 | 'exclude' => $configurationRecord['exclude'], |
||
684 | 'rootTemplatePid' => (int) $configurationRecord['root_template_pid'], |
||
685 | 'key' => $key |
||
686 | ]; |
||
687 | |||
688 | // add trailing slash if not present |
||
689 | if (!empty($subCfg['baseUrl']) && substr($subCfg['baseUrl'], -1) != '/') { |
||
690 | $subCfg['baseUrl'] .= '/'; |
||
691 | } |
||
692 | if (!in_array($id, $this->expandExcludeString($subCfg['exclude']))) { |
||
693 | $res[$key] = []; |
||
694 | $res[$key]['subCfg'] = $subCfg; |
||
695 | $res[$key]['paramParsed'] = $this->parseParams($configurationRecord['configuration']); |
||
696 | $res[$key]['paramExpanded'] = $this->expandParameters($res[$key]['paramParsed'], $id); |
||
697 | $res[$key]['URLs'] = $this->compileUrls($res[$key]['paramExpanded'], ['?id=' . $id]); |
||
698 | $res[$key]['origin'] = 'tx_crawler_configuration_' . $configurationRecord['uid']; |
||
699 | } |
||
700 | } |
||
701 | } |
||
702 | } |
||
703 | } |
||
704 | } |
||
705 | } |
||
706 | |||
707 | if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['processUrls'])) { |
||
708 | foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['processUrls'] as $func) { |
||
709 | $params = [ |
||
710 | 'res' => &$res, |
||
711 | ]; |
||
712 | GeneralUtility::callUserFunction($func, $params, $this); |
||
713 | } |
||
714 | } |
||
715 | |||
716 | return $res; |
||
717 | } |
||
718 | |||
719 | /** |
||
720 | * Checks if a domain record exist and returns the base-url based on the record. If not the given baseUrl string is used. |
||
721 | * |
||
722 | * @param string $baseUrl |
||
723 | * @param integer $sysDomainUid |
||
724 | * @param bool $ssl |
||
725 | * @return string |
||
726 | */ |
||
727 | 3 | protected function getBaseUrlForConfigurationRecord($baseUrl, $sysDomainUid, $ssl = false) |
|
747 | |||
748 | public function getConfigurationsForBranch($rootid, $depth) |
||
793 | |||
794 | /** |
||
795 | * Check if a user has access to an item |
||
796 | * (e.g. get the group list of the current logged in user from $GLOBALS['TSFE']->gr_list) |
||
797 | * |
||
798 | * @see \TYPO3\CMS\Frontend\Page\PageRepository::getMultipleGroupsWhereClause() |
||
799 | * @param string $groupList Comma-separated list of (fe_)group UIDs from a user |
||
800 | * @param string $accessList Comma-separated list of (fe_)group UIDs of the item to access |
||
801 | * @return bool TRUE if at least one of the users group UIDs is in the access list or the access list is empty |
||
802 | */ |
||
803 | 3 | public function hasGroupAccess($groupList, $accessList) |
|
815 | |||
816 | /** |
||
817 | * Parse GET vars of input Query into array with key=>value pairs |
||
818 | * |
||
819 | * @param string $inputQuery Input query string |
||
820 | * @return array |
||
821 | */ |
||
822 | 3 | public function parseParams($inputQuery) |
|
837 | |||
838 | /** |
||
839 | * Will expand the parameters configuration to individual values. This follows a certain syntax of the value of each parameter. |
||
840 | * Syntax of values: |
||
841 | * - Basically: If the value is wrapped in [...] it will be expanded according to the following syntax, otherwise the value is taken literally |
||
842 | * - Configuration is splitted by "|" and the parts are processed individually and finally added together |
||
843 | * - For each configuration part: |
||
844 | * - "[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" |
||
845 | * - "_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" |
||
846 | * _ENABLELANG:1 picks only original records without their language overlays |
||
847 | * - Default: Literal value |
||
848 | * |
||
849 | * @param array $paramArray Array with key (GET var name) and values (value of GET var which is configuration for expansion) |
||
850 | * @param integer $pid Current page ID |
||
851 | * @return array |
||
852 | */ |
||
853 | public function expandParameters($paramArray, $pid) |
||
854 | { |
||
855 | global $TCA; |
||
856 | |||
857 | // Traverse parameter names: |
||
858 | foreach ($paramArray as $p => $v) { |
||
859 | $v = trim($v); |
||
860 | |||
861 | // If value is encapsulated in square brackets it means there are some ranges of values to find, otherwise the value is literal |
||
862 | if (substr($v, 0, 1) === '[' && substr($v, -1) === ']') { |
||
863 | // So, find the value inside brackets and reset the paramArray value as an array. |
||
864 | $v = substr($v, 1, -1); |
||
865 | $paramArray[$p] = []; |
||
866 | |||
867 | // Explode parts and traverse them: |
||
868 | $parts = explode('|', $v); |
||
869 | foreach ($parts as $pV) { |
||
870 | |||
871 | // Look for integer range: (fx. 1-34 or -40--30 // reads minus 40 to minus 30) |
||
872 | if (preg_match('/^(-?[0-9]+)\s*-\s*(-?[0-9]+)$/', trim($pV), $reg)) { |
||
873 | |||
874 | // Swap if first is larger than last: |
||
875 | if ($reg[1] > $reg[2]) { |
||
876 | $temp = $reg[2]; |
||
877 | $reg[2] = $reg[1]; |
||
878 | $reg[1] = $temp; |
||
879 | } |
||
880 | |||
881 | // Traverse range, add values: |
||
882 | $runAwayBrake = 1000; // Limit to size of range! |
||
883 | for ($a = $reg[1]; $a <= $reg[2];$a++) { |
||
884 | $paramArray[$p][] = $a; |
||
885 | $runAwayBrake--; |
||
886 | if ($runAwayBrake <= 0) { |
||
887 | break; |
||
888 | } |
||
889 | } |
||
890 | } elseif (substr(trim($pV), 0, 7) == '_TABLE:') { |
||
891 | |||
892 | // Parse parameters: |
||
893 | $subparts = GeneralUtility::trimExplode(';', $pV); |
||
894 | $subpartParams = []; |
||
895 | foreach ($subparts as $spV) { |
||
896 | list($pKey, $pVal) = GeneralUtility::trimExplode(':', $spV); |
||
897 | $subpartParams[$pKey] = $pVal; |
||
898 | } |
||
899 | |||
900 | // Table exists: |
||
901 | if (isset($TCA[$subpartParams['_TABLE']])) { |
||
902 | $lookUpPid = isset($subpartParams['_PID']) ? intval($subpartParams['_PID']) : $pid; |
||
903 | $pidField = isset($subpartParams['_PIDFIELD']) ? trim($subpartParams['_PIDFIELD']) : 'pid'; |
||
904 | $where = isset($subpartParams['_WHERE']) ? $subpartParams['_WHERE'] : ''; |
||
905 | $addTable = isset($subpartParams['_ADDTABLE']) ? $subpartParams['_ADDTABLE'] : ''; |
||
906 | |||
907 | $fieldName = $subpartParams['_FIELD'] ? $subpartParams['_FIELD'] : 'uid'; |
||
908 | if ($fieldName === 'uid' || $TCA[$subpartParams['_TABLE']]['columns'][$fieldName]) { |
||
909 | $andWhereLanguage = ''; |
||
910 | $transOrigPointerField = $TCA[$subpartParams['_TABLE']]['ctrl']['transOrigPointerField']; |
||
911 | |||
912 | if ($subpartParams['_ENABLELANG'] && $transOrigPointerField) { |
||
913 | $andWhereLanguage = ' AND ' . $this->db->quoteStr($transOrigPointerField, $subpartParams['_TABLE']) . ' <= 0 '; |
||
914 | } |
||
915 | |||
916 | $where = $this->db->quoteStr($pidField, $subpartParams['_TABLE']) . '=' . intval($lookUpPid) . ' ' . |
||
917 | $andWhereLanguage . $where; |
||
918 | |||
919 | $rows = $this->db->exec_SELECTgetRows( |
||
920 | $fieldName, |
||
921 | $subpartParams['_TABLE'] . $addTable, |
||
922 | $where . BackendUtility::deleteClause($subpartParams['_TABLE']), |
||
923 | '', |
||
924 | '', |
||
925 | '', |
||
926 | $fieldName |
||
927 | ); |
||
928 | |||
929 | if (is_array($rows)) { |
||
930 | $paramArray[$p] = array_merge($paramArray[$p], array_keys($rows)); |
||
931 | } |
||
932 | } |
||
933 | } |
||
934 | } else { // Just add value: |
||
935 | $paramArray[$p][] = $pV; |
||
936 | } |
||
937 | // Hook for processing own expandParameters place holder |
||
938 | if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['crawler/class.tx_crawler_lib.php']['expandParameters'])) { |
||
939 | $_params = [ |
||
940 | 'pObj' => &$this, |
||
941 | 'paramArray' => &$paramArray, |
||
942 | 'currentKey' => $p, |
||
943 | 'currentValue' => $pV, |
||
944 | 'pid' => $pid |
||
945 | ]; |
||
946 | foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['crawler/class.tx_crawler_lib.php']['expandParameters'] as $key => $_funcRef) { |
||
947 | GeneralUtility::callUserFunction($_funcRef, $_params, $this); |
||
948 | } |
||
949 | } |
||
950 | } |
||
951 | |||
952 | // Make unique set of values and sort array by key: |
||
953 | $paramArray[$p] = array_unique($paramArray[$p]); |
||
954 | ksort($paramArray); |
||
955 | } else { |
||
956 | // Set the literal value as only value in array: |
||
957 | $paramArray[$p] = [$v]; |
||
958 | } |
||
959 | } |
||
960 | |||
961 | return $paramArray; |
||
962 | } |
||
963 | |||
964 | /** |
||
965 | * Compiling URLs from parameter array (output of expandParameters()) |
||
966 | * The number of URLs will be the multiplication of the number of parameter values for each key |
||
967 | * |
||
968 | * @param array $paramArray Output of expandParameters(): Array with keys (GET var names) and for each an array of values |
||
969 | * @param array $urls URLs accumulated in this array (for recursion) |
||
970 | * @return array |
||
971 | */ |
||
972 | 3 | public function compileUrls($paramArray, $urls = []) |
|
997 | |||
998 | /************************************ |
||
999 | * |
||
1000 | * Crawler log |
||
1001 | * |
||
1002 | ************************************/ |
||
1003 | |||
1004 | /** |
||
1005 | * Return array of records from crawler queue for input page ID |
||
1006 | * |
||
1007 | * @param integer $id Page ID for which to look up log entries. |
||
1008 | * @param string$filter Filter: "all" => all entries, "pending" => all that is not yet run, "finished" => all complete ones |
||
1009 | * @param boolean $doFlush If TRUE, then entries selected at DELETED(!) instead of selected! |
||
1010 | * @param boolean $doFullFlush |
||
1011 | * @param integer $itemsPerPage Limit the amount of entries per page default is 10 |
||
1012 | * @return array |
||
1013 | */ |
||
1014 | 4 | public function getLogEntriesForPageId($id, $filter = '', $doFlush = false, $doFullFlush = false, $itemsPerPage = 10) |
|
1043 | |||
1044 | /** |
||
1045 | * Return array of records from crawler queue for input set ID |
||
1046 | * |
||
1047 | * @param integer $set_id Set ID for which to look up log entries. |
||
1048 | * @param string $filter Filter: "all" => all entries, "pending" => all that is not yet run, "finished" => all complete ones |
||
1049 | * @param boolean $doFlush If TRUE, then entries selected at DELETED(!) instead of selected! |
||
1050 | * @param integer $itemsPerPage Limit the amount of entires per page default is 10 |
||
1051 | * @return array |
||
1052 | */ |
||
1053 | 6 | public function getLogEntriesForSetId($set_id, $filter = '', $doFlush = false, $doFullFlush = false, $itemsPerPage = 10) |
|
1082 | |||
1083 | /** |
||
1084 | * Removes queue entries |
||
1085 | * |
||
1086 | * @param string $where SQL related filter for the entries which should be removed |
||
1087 | * @return void |
||
1088 | */ |
||
1089 | 10 | protected function flushQueue($where = '') |
|
1122 | |||
1123 | /** |
||
1124 | * Adding call back entries to log (called from hooks typically, see indexed search class "class.crawler.php" |
||
1125 | * |
||
1126 | * @param integer $setId Set ID |
||
1127 | * @param array $params Parameters to pass to call back function |
||
1128 | * @param string $callBack Call back object reference, eg. 'EXT:indexed_search/class.crawler.php:&tx_indexedsearch_crawler' |
||
1129 | * @param integer $page_id Page ID to attach it to |
||
1130 | * @param integer $schedule Time at which to activate |
||
1131 | * @return void |
||
1132 | */ |
||
1133 | public function addQueueEntry_callBack($setId, $params, $callBack, $page_id = 0, $schedule = 0) |
||
1152 | |||
1153 | /************************************ |
||
1154 | * |
||
1155 | * URL setting |
||
1156 | * |
||
1157 | ************************************/ |
||
1158 | |||
1159 | /** |
||
1160 | * Setting a URL for crawling: |
||
1161 | * |
||
1162 | * @param integer $id Page ID |
||
1163 | * @param string $url Complete URL |
||
1164 | * @param array $subCfg Sub configuration array (from TS config) |
||
1165 | * @param integer $tstamp Scheduled-time |
||
1166 | * @param string $configurationHash (optional) configuration hash |
||
1167 | * @param bool $skipInnerDuplicationCheck (optional) skip inner duplication check |
||
1168 | * @return bool |
||
1169 | */ |
||
1170 | public function addUrl( |
||
1255 | |||
1256 | /** |
||
1257 | * This method determines duplicates for a queue entry with the same parameters and this timestamp. |
||
1258 | * If the timestamp is in the past, it will check if there is any unprocessed queue entry in the past. |
||
1259 | * If the timestamp is in the future it will check, if the queued entry has exactly the same timestamp |
||
1260 | * |
||
1261 | * @param int $tstamp |
||
1262 | * @param array $fieldArray |
||
1263 | * |
||
1264 | * @return array |
||
1265 | */ |
||
1266 | protected function getDuplicateRowsIfExist($tstamp, $fieldArray) |
||
1306 | |||
1307 | /** |
||
1308 | * Returns the current system time |
||
1309 | * |
||
1310 | * @return int |
||
1311 | */ |
||
1312 | public function getCurrentTime() |
||
1316 | |||
1317 | /************************************ |
||
1318 | * |
||
1319 | * URL reading |
||
1320 | * |
||
1321 | ************************************/ |
||
1322 | |||
1323 | /** |
||
1324 | * Read URL for single queue entry |
||
1325 | * |
||
1326 | * @param integer $queueId |
||
1327 | * @param boolean $force If set, will process even if exec_time has been set! |
||
1328 | * @return integer |
||
1329 | */ |
||
1330 | public function readUrl($queueId, $force = false) |
||
1410 | |||
1411 | /** |
||
1412 | * Read URL for not-yet-inserted log-entry |
||
1413 | * |
||
1414 | * @param array $field_array Queue field array, |
||
1415 | * |
||
1416 | * @return string |
||
1417 | */ |
||
1418 | public function readUrlFromArray($field_array) |
||
1441 | |||
1442 | /** |
||
1443 | * Read URL for a queue record |
||
1444 | * |
||
1445 | * @param array $queueRec Queue record |
||
1446 | * @return string |
||
1447 | */ |
||
1448 | public function readUrl_exec($queueRec) |
||
1485 | |||
1486 | /** |
||
1487 | * Gets the content of a URL. |
||
1488 | * |
||
1489 | * @param string $originalUrl URL to read |
||
1490 | * @param string $crawlerId Crawler ID string (qid + hash to verify) |
||
1491 | * @param integer $timeout Timeout time |
||
1492 | * @param integer $recursion Recursion limiter for 302 redirects |
||
1493 | * @return array |
||
1494 | */ |
||
1495 | 2 | public function requestUrl($originalUrl, $crawlerId, $timeout = 2, $recursion = 10) |
|
1588 | |||
1589 | /** |
||
1590 | * Gets the base path of the website frontend. |
||
1591 | * (e.g. if you call http://mydomain.com/cms/index.php in |
||
1592 | * the browser the base path is "/cms/") |
||
1593 | * |
||
1594 | * @return string Base path of the website frontend |
||
1595 | */ |
||
1596 | protected function getFrontendBasePath() |
||
1619 | |||
1620 | /** |
||
1621 | * Executes a shell command and returns the outputted result. |
||
1622 | * |
||
1623 | * @param string $command Shell command to be executed |
||
1624 | * @return string Outputted result of the command execution |
||
1625 | */ |
||
1626 | protected function executeShellCommand($command) |
||
1631 | |||
1632 | /** |
||
1633 | * Reads HTTP response from the given stream. |
||
1634 | * |
||
1635 | * @param resource $streamPointer Pointer to connection stream. |
||
1636 | * @return array Associative array with the following items: |
||
1637 | * headers <array> Response headers sent by server. |
||
1638 | * content <array> Content, with each line as an array item. |
||
1639 | */ |
||
1640 | 1 | protected function getHttpResponseFromStream($streamPointer) |
|
1663 | |||
1664 | /** |
||
1665 | * @param message |
||
1666 | */ |
||
1667 | 2 | protected function log($message) |
|
1676 | |||
1677 | /** |
||
1678 | * Builds HTTP request headers. |
||
1679 | * |
||
1680 | * @param array $url |
||
1681 | * @param string $crawlerId |
||
1682 | * |
||
1683 | * @return array |
||
1684 | */ |
||
1685 | 6 | protected function buildRequestHeaderArray(array $url, $crawlerId) |
|
1701 | |||
1702 | /** |
||
1703 | * Check if the submitted HTTP-Header contains a redirect location and built new crawler-url |
||
1704 | * |
||
1705 | * @param array $headers HTTP Header |
||
1706 | * @param string $user HTTP Auth. User |
||
1707 | * @param string $pass HTTP Auth. Password |
||
1708 | * @return bool|string |
||
1709 | */ |
||
1710 | 12 | protected function getRequestUrlFrom302Header($headers, $user = '', $pass = '') |
|
1744 | |||
1745 | /************************** |
||
1746 | * |
||
1747 | * tslib_fe hooks: |
||
1748 | * |
||
1749 | **************************/ |
||
1750 | |||
1751 | /** |
||
1752 | * Initialization hook (called after database connection) |
||
1753 | * Takes the "HTTP_X_T3CRAWLER" header and looks up queue record and verifies if the session comes from the system (by comparing hashes) |
||
1754 | * |
||
1755 | * @param array $params Parameters from frontend |
||
1756 | * @param object $ref TSFE object (reference under PHP5) |
||
1757 | * @return void |
||
1758 | * |
||
1759 | * FIXME: Look like this is not used, in commit 9910d3f40cce15f4e9b7bcd0488bf21f31d53ebc it's added as public, |
||
1760 | * FIXME: I think this can be removed. (TNM) |
||
1761 | */ |
||
1762 | public function fe_init(&$params, $ref) |
||
1779 | |||
1780 | /***************************** |
||
1781 | * |
||
1782 | * Compiling URLs to crawl - tools |
||
1783 | * |
||
1784 | *****************************/ |
||
1785 | |||
1786 | /** |
||
1787 | * @param integer $id Root page id to start from. |
||
1788 | * @param integer $depth Depth of tree, 0=only id-page, 1= on sublevel, 99 = infinite |
||
1789 | * @param integer $scheduledTime Unix Time when the URL is timed to be visited when put in queue |
||
1790 | * @param integer $reqMinute Number of requests per minute (creates the interleave between requests) |
||
1791 | * @param boolean $submitCrawlUrls If set, submits the URLs to queue in database (real crawling) |
||
1792 | * @param boolean $downloadCrawlUrls If set (and submitcrawlUrls is false) will fill $downloadUrls with entries) |
||
1793 | * @param array $incomingProcInstructions Array of processing instructions |
||
1794 | * @param array $configurationSelection Array of configuration keys |
||
1795 | * @return string |
||
1796 | */ |
||
1797 | public function getPageTreeAndUrls( |
||
1884 | |||
1885 | /** |
||
1886 | * Expands exclude string |
||
1887 | * |
||
1888 | * @param string $excludeString Exclude string |
||
1889 | * @return array |
||
1890 | */ |
||
1891 | public function expandExcludeString($excludeString) |
||
1936 | |||
1937 | /** |
||
1938 | * Create the rows for display of the page tree |
||
1939 | * For each page a number of rows are shown displaying GET variable configuration |
||
1940 | * |
||
1941 | * @param array Page row |
||
1942 | * @param string Page icon and title for row |
||
1943 | * @return string HTML <tr> content (one or more) |
||
1944 | */ |
||
1945 | public function drawURLs_addRowsForPage(array $pageRow, $pageTitleAndIcon) |
||
2054 | |||
2055 | /***************************** |
||
2056 | * |
||
2057 | * CLI functions |
||
2058 | * |
||
2059 | *****************************/ |
||
2060 | |||
2061 | /** |
||
2062 | * Main function for running from Command Line PHP script (cron job) |
||
2063 | * See ext/crawler/cli/crawler_cli.phpsh for details |
||
2064 | * |
||
2065 | * @return int number of remaining items or false if error |
||
2066 | */ |
||
2067 | public function CLI_main() |
||
2108 | |||
2109 | /** |
||
2110 | * Function executed by crawler_im.php cli script. |
||
2111 | * |
||
2112 | * @return void |
||
2113 | */ |
||
2114 | public function CLI_main_im() |
||
2222 | |||
2223 | /** |
||
2224 | * Function executed by crawler_im.php cli script. |
||
2225 | * |
||
2226 | * @return bool |
||
2227 | */ |
||
2228 | public function CLI_main_flush() |
||
2266 | |||
2267 | /** |
||
2268 | * Obtains configuration keys from the CLI arguments |
||
2269 | * |
||
2270 | * @param QueueCommandLineController $cliObj |
||
2271 | * @return array |
||
2272 | * |
||
2273 | * @deprecated since crawler v6.3.0, will be removed in crawler v7.0.0. |
||
2274 | */ |
||
2275 | protected function getConfigurationKeys(QueueCommandLineController $cliObj) |
||
2280 | |||
2281 | /** |
||
2282 | * Running the functionality of the CLI (crawling URLs from queue) |
||
2283 | * |
||
2284 | * @param int $countInARun |
||
2285 | * @param int $sleepTime |
||
2286 | * @param int $sleepAfterFinish |
||
2287 | * @return string |
||
2288 | */ |
||
2289 | public function CLI_run($countInARun, $sleepTime, $sleepAfterFinish) |
||
2396 | |||
2397 | /** |
||
2398 | * Activate hooks |
||
2399 | * |
||
2400 | * @return void |
||
2401 | */ |
||
2402 | public function CLI_runHooks() |
||
2414 | |||
2415 | /** |
||
2416 | * Try to acquire a new process with the given id |
||
2417 | * also performs some auto-cleanup for orphan processes |
||
2418 | * @todo preemption might not be the most elegant way to clean up |
||
2419 | * |
||
2420 | * @param string $id identification string for the process |
||
2421 | * @return boolean |
||
2422 | */ |
||
2423 | public function CLI_checkAndAcquireNewProcess($id) |
||
2479 | |||
2480 | /** |
||
2481 | * Release a process and the required resources |
||
2482 | * |
||
2483 | * @param mixed $releaseIds string with a single process-id or array with multiple process-ids |
||
2484 | * @param boolean $withinLock show whether the DB-actions are included within an existing lock |
||
2485 | * @return boolean |
||
2486 | */ |
||
2487 | public function CLI_releaseProcesses($releaseIds, $withinLock = false) |
||
2549 | |||
2550 | /** |
||
2551 | * Delete processes marked as deleted |
||
2552 | * |
||
2553 | * @return void |
||
2554 | */ |
||
2555 | 1 | public function CLI_deleteProcessesMarkedDeleted() |
|
2559 | |||
2560 | /** |
||
2561 | * Check if there are still resources left for the process with the given id |
||
2562 | * Used to determine timeouts and to ensure a proper cleanup if there's a timeout |
||
2563 | * |
||
2564 | * @param string identification string for the process |
||
2565 | * @return boolean determines if the process is still active / has resources |
||
2566 | * |
||
2567 | * FIXME: Please remove Transaction, not needed as only a select query. |
||
2568 | */ |
||
2569 | public function CLI_checkIfProcessIsActive($pid) |
||
2588 | |||
2589 | /** |
||
2590 | * Create a unique Id for the current process |
||
2591 | * |
||
2592 | * @return string the ID |
||
2593 | */ |
||
2594 | 2 | public function CLI_buildProcessId() |
|
2601 | |||
2602 | /** |
||
2603 | * @param bool $get_as_float |
||
2604 | * |
||
2605 | * @return mixed |
||
2606 | */ |
||
2607 | protected function microtime($get_as_float = false) |
||
2611 | |||
2612 | /** |
||
2613 | * Prints a message to the stdout (only if debug-mode is enabled) |
||
2614 | * |
||
2615 | * @param string $msg the message |
||
2616 | */ |
||
2617 | public function CLI_debug($msg) |
||
2624 | |||
2625 | /** |
||
2626 | * Get URL content by making direct request to TYPO3. |
||
2627 | * |
||
2628 | * @param string $url Page URL |
||
2629 | * @param int $crawlerId Crawler-ID |
||
2630 | * @return array |
||
2631 | */ |
||
2632 | 2 | protected function sendDirectRequest($url, $crawlerId) |
|
2663 | |||
2664 | /** |
||
2665 | * Cleans up entries that stayed for too long in the queue. These are: |
||
2666 | * - processed entries that are over 1.5 days in age |
||
2667 | * - scheduled entries that are over 7 days old |
||
2668 | * |
||
2669 | * @return void |
||
2670 | * |
||
2671 | * TODO: Should be switched back to protected - TNM 2018-11-16 |
||
2672 | */ |
||
2673 | public function cleanUpOldQueueEntries() |
||
2682 | |||
2683 | /** |
||
2684 | * Initializes a TypoScript Frontend necessary for using TypoScript and TypoLink functions |
||
2685 | * |
||
2686 | * @param int $id |
||
2687 | * @param int $typeNum |
||
2688 | * |
||
2689 | * @return void |
||
2690 | */ |
||
2691 | protected function initTSFE($id = 1, $typeNum = 0) |
||
2710 | |||
2711 | /** |
||
2712 | * Returns a md5 hash generated from a serialized configuration array. |
||
2713 | * |
||
2714 | * @param array $configuration |
||
2715 | * |
||
2716 | * @return string |
||
2717 | */ |
||
2718 | 5 | protected function getConfigurationHash(array $configuration) { |
|
2723 | |||
2724 | /** |
||
2725 | * Check whether the Crawling Protocol should be http or https |
||
2726 | * |
||
2727 | * @param $crawlerConfiguration |
||
2728 | * @param $pageConfiguration |
||
2729 | * |
||
2730 | * @return bool |
||
2731 | */ |
||
2732 | 5 | protected function isCrawlingProtocolHttps($crawlerConfiguration, $pageConfiguration) { |
|
2744 | } |
||
2745 |
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: