Complex classes like CrawlerApi 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 CrawlerApi, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class CrawlerApi |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * @var CrawlerController |
||
| 44 | */ |
||
| 45 | private $crawlerController; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var QueueRepository |
||
| 49 | */ |
||
| 50 | protected $queueRepository; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var $allowedConfigurations array |
||
| 54 | */ |
||
| 55 | protected $allowedConfigurations = []; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Each crawler run has a setid, this facade method delegates |
||
| 59 | * the it to the crawler object |
||
| 60 | * |
||
| 61 | * @param int |
||
| 62 | */ |
||
| 63 | public function overwriteSetId($id) |
||
| 64 | { |
||
| 65 | $this->findCrawler()->setID = intval($id); |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * This method is used to limit the configuration selection to |
||
| 70 | * a set of configurations. |
||
| 71 | * |
||
| 72 | * @param array $allowedConfigurations |
||
| 73 | */ |
||
| 74 | public function setAllowedConfigurations(array $allowedConfigurations) |
||
| 75 | { |
||
| 76 | $this->allowedConfigurations = $allowedConfigurations; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @return array |
||
| 81 | */ |
||
| 82 | public function getAllowedConfigurations() |
||
| 83 | { |
||
| 84 | return $this->allowedConfigurations; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Returns the setID of the crawler |
||
| 89 | * |
||
| 90 | * @return int |
||
| 91 | */ |
||
| 92 | public function getSetId() |
||
| 93 | { |
||
| 94 | return $this->findCrawler()->setID; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Method to get an instance of the internal crawler singleton |
||
| 99 | * |
||
| 100 | * @return CrawlerController Instance of the crawler lib |
||
| 101 | * |
||
| 102 | * @throws \Exception |
||
| 103 | */ |
||
| 104 | protected function findCrawler() |
||
| 105 | { |
||
| 106 | if (!is_object($this->crawlerController)) { |
||
| 107 | $this->crawlerController = GeneralUtility::makeInstance(CrawlerController::class); |
||
| 108 | $this->crawlerController->setID = GeneralUtility::md5int(microtime()); |
||
| 109 | } |
||
| 110 | |||
| 111 | if (is_object($this->crawlerController)) { |
||
| 112 | return $this->crawlerController; |
||
| 113 | } else { |
||
| 114 | throw new \Exception('no crawler object', 1512659759); |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Adds a page to the crawlerqueue by uid |
||
| 120 | * |
||
| 121 | * @param int $uid uid |
||
| 122 | */ |
||
| 123 | public function addPageToQueue($uid) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * This method is used to limit the processing instructions to the processing instructions |
||
| 132 | * that are allowed. |
||
| 133 | * |
||
| 134 | * @return array |
||
| 135 | */ |
||
| 136 | protected function filterUnallowedConfigurations($configurations) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Adds a page to the crawlerqueue by uid and sets a |
||
| 152 | * timestamp when the page should be crawled. |
||
| 153 | * |
||
| 154 | * @param int $uid pageid |
||
| 155 | * @param int $time timestamp |
||
| 156 | */ |
||
| 157 | public function addPageToQueueTimed($uid, $time) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Counts all entries in the database which are scheduled for a given page id and a schedule timestamp. |
||
| 195 | * |
||
| 196 | * @param int $page_uid |
||
| 197 | * @param int $schedule_timestamp |
||
| 198 | * |
||
| 199 | * @return int |
||
| 200 | * |
||
| 201 | * @deprecated since crawler v6.2.0, will be removed in crawler v7.0.0. |
||
| 202 | */ |
||
| 203 | protected function countEntriesInQueueForPageByScheduleTime($page_uid, $schedule_timestamp) |
||
| 204 | { |
||
| 205 | $page_uid = intval($page_uid); |
||
| 206 | $schedule_timestamp = intval($schedule_timestamp); |
||
| 207 | |||
| 208 | //if the same page is scheduled for the same time and has not be executed? |
||
| 209 | if ($schedule_timestamp == 0) { |
||
| 210 | //un-timed elements need an exec_time with 0 because they can occur multiple times |
||
| 211 | $where = 'page_id=' . $page_uid . ' AND exec_time = 0 AND scheduled=' . $schedule_timestamp; |
||
| 212 | } else { |
||
| 213 | //timed elements have got a fixed schedule time, if a record with this time |
||
| 214 | //exists it is maybe queued for the future, or is has been queue for the past and therefore |
||
| 215 | //also been processed. |
||
| 216 | $where = 'page_id=' . $page_uid . ' AND scheduled=' . $schedule_timestamp; |
||
| 217 | } |
||
| 218 | |||
| 219 | $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($GLOBALS['TYPO3_DB']->exec_SELECTquery( |
||
| 220 | 'count(*) as cnt', |
||
| 221 | 'tx_crawler_queue', |
||
| 222 | $where |
||
| 223 | )); |
||
| 224 | |||
| 225 | return intval($row['cnt']); |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Determines if a page is queued |
||
| 230 | * |
||
| 231 | * @param $uid |
||
| 232 | * @param bool $unprocessed_only |
||
| 233 | * @param bool $timed_only |
||
| 234 | * @param bool $timestamp |
||
| 235 | * |
||
| 236 | * @return bool |
||
| 237 | */ |
||
| 238 | public function isPageInQueue($uid, $unprocessed_only = true, $timed_only = false, $timestamp = false) |
||
| 239 | { |
||
| 240 | if (!MathUtility::canBeInterpretedAsInteger($uid)) { |
||
| 241 | throw new \InvalidArgumentException('Invalid parameter type', 1468931945); |
||
| 242 | } |
||
| 243 | |||
| 244 | $isPageInQueue = false; |
||
| 245 | |||
| 246 | $whereClause = 'page_id = ' . (integer)$uid; |
||
| 247 | |||
| 248 | if (false !== $unprocessed_only) { |
||
| 249 | $whereClause .= ' AND exec_time = 0'; |
||
| 250 | } |
||
| 251 | |||
| 252 | if (false !== $timed_only) { |
||
| 253 | $whereClause .= ' AND scheduled != 0'; |
||
| 254 | } |
||
| 255 | |||
| 256 | if (false !== $timestamp) { |
||
| 257 | $whereClause .= ' AND scheduled = ' . (integer)$timestamp; |
||
| 258 | } |
||
| 259 | |||
| 260 | $count = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows( |
||
| 261 | '*', |
||
| 262 | 'tx_crawler_queue', |
||
| 263 | $whereClause |
||
| 264 | ); |
||
| 265 | |||
| 266 | if (false !== $count && $count > 0) { |
||
| 267 | $isPageInQueue = true; |
||
| 268 | } |
||
| 269 | |||
| 270 | return $isPageInQueue; |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Method to return the latest Crawle Timestamp for a page. |
||
| 275 | * |
||
| 276 | * @param int $uid uid id of the page |
||
| 277 | * @param bool $future_crawldates_only |
||
| 278 | * @param bool $unprocessed_only |
||
| 279 | * |
||
| 280 | * @return int |
||
| 281 | */ |
||
| 282 | public function getLatestCrawlTimestampForPage($uid, $future_crawldates_only = false, $unprocessed_only = false) |
||
| 283 | { |
||
| 284 | $uid = intval($uid); |
||
| 285 | $query = 'max(scheduled) as latest'; |
||
| 286 | $where = ' page_id = ' . $uid; |
||
| 287 | |||
| 288 | if ($future_crawldates_only) { |
||
| 289 | $where .= ' AND scheduled > ' . time(); |
||
| 290 | } |
||
| 291 | |||
| 292 | if ($unprocessed_only) { |
||
| 293 | $where .= ' AND exec_time = 0'; |
||
| 294 | } |
||
| 295 | |||
| 296 | $rs = $GLOBALS['TYPO3_DB']->exec_SELECTquery($query, 'tx_crawler_queue', $where); |
||
| 297 | if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($rs)) { |
||
| 298 | $res = $row['latest']; |
||
| 299 | } else { |
||
| 300 | $res = 0; |
||
| 301 | } |
||
| 302 | |||
| 303 | return $res; |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Returns an array with timestamps when the page has been scheduled for crawling and |
||
| 308 | * at what time the scheduled crawl has been executed. The array also contains items that are |
||
| 309 | * scheduled but have note been crawled yet. |
||
| 310 | * |
||
| 311 | * @param int $uid uid of the page |
||
| 312 | * @param bool $limit |
||
| 313 | * |
||
| 314 | * @return array array with the crawl-history of a page => 0 : scheduled time , 1 : executed_time, 2 : set_id |
||
| 315 | */ |
||
| 316 | public function getCrawlHistoryForPage($uid, $limit = 0) |
||
| 317 | { |
||
| 318 | $uid = intval($uid); |
||
| 319 | $limit = intval($limit); |
||
| 320 | |||
| 321 | $query = 'scheduled, exec_time, set_id'; |
||
| 322 | $where = ' page_id = ' . $uid; |
||
| 323 | |||
| 324 | $limit_query = ($limit) ? $limit : null; |
||
| 325 | |||
| 326 | $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows($query, 'tx_crawler_queue', $where, null, null, $limit_query); |
||
| 327 | return $rows; |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Method to determine unprocessed Items in the crawler queue. |
||
| 332 | * |
||
| 333 | * @return array |
||
| 334 | */ |
||
| 335 | public function getUnprocessedItems() |
||
| 336 | { |
||
| 337 | $query = '*'; |
||
| 338 | $where = 'exec_time = 0'; |
||
| 339 | $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows($query, 'tx_crawler_queue', $where, '', 'page_id, scheduled'); |
||
| 340 | |||
| 341 | return $rows; |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Method to get the number of unprocessed items in the crawler |
||
| 346 | * |
||
| 347 | * @param int number of unprocessed items in the queue |
||
| 348 | */ |
||
| 349 | public function countUnprocessedItems() |
||
| 350 | { |
||
| 351 | $query = 'count(page_id) as anz'; |
||
| 352 | $where = 'exec_time = 0'; |
||
| 353 | $rs = $GLOBALS['TYPO3_DB']->exec_SELECTquery($query, 'tx_crawler_queue', $where); |
||
| 354 | $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($rs); |
||
| 355 | |||
| 356 | return $row['anz']; |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Method to check if a page is in the queue which is timed for a |
||
| 361 | * date when it should be crawled |
||
| 362 | * |
||
| 363 | * @param int $uid uid of the page |
||
| 364 | * @param boolean $show_unprocessed only respect unprocessed pages |
||
| 365 | * |
||
| 366 | * @return boolean |
||
| 367 | */ |
||
| 368 | public function isPageInQueueTimed($uid, $show_unprocessed = true) |
||
| 369 | { |
||
| 370 | $uid = intval($uid); |
||
| 371 | |||
| 372 | return $this->isPageInQueue($uid, $show_unprocessed); |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Reads the registered processingInstructions of the crawler |
||
| 377 | * |
||
| 378 | * @return array |
||
| 379 | */ |
||
| 380 | private function getCrawlerProcInstructions() |
||
| 381 | { |
||
| 382 | if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['procInstructions'])) { |
||
| 383 | return $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['procInstructions']; |
||
| 384 | } |
||
| 385 | |||
| 386 | return []; |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Removes an queue entry with a given queue id |
||
| 391 | * |
||
| 392 | * @param int $qid |
||
| 393 | */ |
||
| 394 | public function removeQueueEntrie($qid) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Get queue statistics |
||
| 404 | * |
||
| 405 | * @param void |
||
| 406 | * |
||
| 407 | * @return array array('assignedButUnprocessed' => <>, 'unprocessed' => <>); |
||
| 408 | */ |
||
| 409 | public function getQueueStatistics() |
||
| 410 | { |
||
| 411 | return [ |
||
| 412 | 'assignedButUnprocessed' => $this->getQueueRepository()->countAllAssignedPendingItems(), |
||
| 413 | 'unprocessed' => $this->getQueueRepository()->countAllPendingItems() |
||
| 414 | ]; |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Get queue repository |
||
| 419 | * |
||
| 420 | * @return QueueRepository |
||
| 421 | */ |
||
| 422 | protected function getQueueRepository() |
||
| 423 | { |
||
| 424 | if (!$this->queueRepository instanceof QueueRepository) { |
||
| 425 | $this->queueRepository = new QueueRepository(); |
||
| 426 | } |
||
| 427 | |||
| 428 | return $this->queueRepository; |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Get queue statistics by configuration |
||
| 433 | * |
||
| 434 | * @return array array of array('configuration' => <>, 'assignedButUnprocessed' => <>, 'unprocessed' => <>) |
||
| 435 | */ |
||
| 436 | public function getQueueStatisticsByConfiguration() |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Get active processes count |
||
| 454 | * |
||
| 455 | * @param void |
||
| 456 | * |
||
| 457 | * @return int |
||
| 458 | */ |
||
| 459 | public function getActiveProcessesCount() |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Get last processed entries |
||
| 468 | * |
||
| 469 | * @param int $limit |
||
| 470 | * |
||
| 471 | * @return array |
||
| 472 | */ |
||
| 473 | public function getLastProcessedQueueEntries($limit) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Get current crawling speed |
||
| 480 | * |
||
| 481 | * @param float|false page speed in pages per minute |
||
| 482 | * |
||
| 483 | * @return int |
||
| 484 | */ |
||
| 485 | public function getCurrentCrawlingSpeed() |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Get some performance data |
||
| 523 | * |
||
| 524 | * @param integer $start |
||
| 525 | * @param integer $end |
||
| 526 | * @param integer $resolution |
||
| 527 | * |
||
| 528 | * @return array data |
||
| 529 | * |
||
| 530 | * @throws \Exception |
||
| 531 | */ |
||
| 532 | public function getPerformanceData($start, $end, $resolution) |
||
| 581 | } |
||
| 582 |