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 |
||
41 | class CrawlerApi |
||
42 | { |
||
43 | /** |
||
44 | * @var CrawlerController |
||
45 | */ |
||
46 | private $crawlerController; |
||
47 | |||
48 | /** |
||
49 | * @var QueueRepository |
||
50 | */ |
||
51 | protected $queueRepository; |
||
52 | |||
53 | /** |
||
54 | * @var ProcessRepository |
||
55 | */ |
||
56 | protected $processRepository; |
||
57 | |||
58 | /** |
||
59 | * @var $allowedConfigurations array |
||
60 | */ |
||
61 | protected $allowedConfigurations = []; |
||
62 | |||
63 | /** |
||
64 | * Each crawler run has a setid, this facade method delegates |
||
65 | * the it to the crawler object |
||
66 | * |
||
67 | * @param int |
||
68 | */ |
||
69 | 1 | public function overwriteSetId($id) |
|
73 | |||
74 | /** |
||
75 | * This method is used to limit the configuration selection to |
||
76 | * a set of configurations. |
||
77 | * |
||
78 | * @param array $allowedConfigurations |
||
79 | */ |
||
80 | 1 | public function setAllowedConfigurations(array $allowedConfigurations) |
|
84 | |||
85 | /** |
||
86 | * @return array |
||
87 | */ |
||
88 | 1 | public function getAllowedConfigurations() |
|
92 | |||
93 | /** |
||
94 | * Returns the setID of the crawler |
||
95 | * |
||
96 | * @return int |
||
97 | */ |
||
98 | 1 | public function getSetId() |
|
102 | |||
103 | 5 | public function __construct() |
|
109 | |||
110 | /** |
||
111 | * Method to get an instance of the internal crawler singleton |
||
112 | * |
||
113 | * @return CrawlerController Instance of the crawler lib |
||
114 | * |
||
115 | * @throws \Exception |
||
116 | */ |
||
117 | 1 | protected function findCrawler() |
|
130 | |||
131 | /** |
||
132 | * Adds a page to the crawlerqueue by uid |
||
133 | * |
||
134 | * @param int $uid uid |
||
135 | */ |
||
136 | public function addPageToQueue($uid) |
||
142 | |||
143 | /** |
||
144 | * This method is used to limit the processing instructions to the processing instructions |
||
145 | * that are allowed. |
||
146 | * |
||
147 | * @return array |
||
148 | */ |
||
149 | protected function filterUnallowedConfigurations($configurations) |
||
150 | { |
||
151 | if (count($this->allowedConfigurations) > 0) { |
||
152 | // remove configuration that does not match the current selection |
||
153 | foreach ($configurations as $confKey => $confArray) { |
||
154 | if (!in_array($confKey, $this->allowedConfigurations)) { |
||
155 | unset($configurations[$confKey]); |
||
156 | } |
||
157 | } |
||
158 | } |
||
159 | |||
160 | return $configurations; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Adds a page to the crawlerqueue by uid and sets a |
||
165 | * timestamp when the page should be crawled. |
||
166 | * |
||
167 | * @param int $uid pageid |
||
168 | * @param int $time timestamp |
||
169 | * |
||
170 | * @throws \Exception |
||
171 | */ |
||
172 | public function addPageToQueueTimed($uid, $time) |
||
173 | { |
||
174 | $uid = intval($uid); |
||
175 | $time = intval($time); |
||
176 | |||
177 | $crawler = $this->findCrawler(); |
||
178 | $pageData = GeneralUtility::makeInstance(PageRepository::class)->getPage($uid); |
||
179 | $configurations = $crawler->getUrlsForPageRow($pageData); |
||
180 | $configurations = $this->filterUnallowedConfigurations($configurations); |
||
181 | $downloadUrls = []; |
||
182 | $duplicateTrack = []; |
||
183 | |||
184 | if (is_array($configurations)) { |
||
185 | foreach ($configurations as $cv) { |
||
186 | //enable inserting of entries |
||
187 | $crawler->registerQueueEntriesInternallyOnly = false; |
||
188 | $crawler->urlListFromUrlArray( |
||
189 | $cv, |
||
190 | $pageData, |
||
191 | $time, |
||
192 | 300, |
||
193 | true, |
||
194 | false, |
||
195 | $duplicateTrack, |
||
196 | $downloadUrls, |
||
197 | array_keys($this->getCrawlerProcInstructions()) |
||
198 | ); |
||
199 | |||
200 | //reset the queue because the entries have been written to the db |
||
201 | unset($crawler->queueEntries); |
||
202 | } |
||
203 | } else { |
||
204 | //no configuration found |
||
205 | } |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Counts all entries in the database which are scheduled for a given page id and a schedule timestamp. |
||
210 | * |
||
211 | * @param int $page_uid |
||
212 | * @param int $schedule_timestamp |
||
213 | * |
||
214 | * @return int |
||
215 | * |
||
216 | * @deprecated since crawler v6.2.0, will be removed in crawler v7.0.0. |
||
217 | */ |
||
218 | protected function countEntriesInQueueForPageByScheduleTime($page_uid, $schedule_timestamp) |
||
219 | { |
||
220 | $page_uid = intval($page_uid); |
||
221 | $schedule_timestamp = intval($schedule_timestamp); |
||
222 | |||
223 | //if the same page is scheduled for the same time and has not be executed? |
||
224 | if ($schedule_timestamp == 0) { |
||
225 | //un-timed elements need an exec_time with 0 because they can occur multiple times |
||
226 | $where = 'page_id=' . $page_uid . ' AND exec_time = 0 AND scheduled=' . $schedule_timestamp; |
||
227 | } else { |
||
228 | //timed elements have got a fixed schedule time, if a record with this time |
||
229 | //exists it is maybe queued for the future, or is has been queue for the past and therefore |
||
230 | //also been processed. |
||
231 | $where = 'page_id=' . $page_uid . ' AND scheduled=' . $schedule_timestamp; |
||
232 | } |
||
233 | |||
234 | $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($GLOBALS['TYPO3_DB']->exec_SELECTquery( |
||
235 | 'count(*) as cnt', |
||
236 | 'tx_crawler_queue', |
||
237 | $where |
||
238 | )); |
||
239 | |||
240 | return intval($row['cnt']); |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Determines if a page is queued |
||
245 | * |
||
246 | * @param $uid |
||
247 | * @param bool $unprocessed_only |
||
248 | * @param bool $timed_only |
||
249 | * @param bool $timestamp |
||
250 | * |
||
251 | * @deprecated since crawler v6.5.1, will be removed in crawler v9.0.0. |
||
252 | * |
||
253 | * @return bool |
||
254 | */ |
||
255 | 1 | public function isPageInQueue($uid, $unprocessed_only = true, $timed_only = false, $timestamp = false) |
|
256 | { |
||
257 | 1 | if (!MathUtility::canBeInterpretedAsInteger($uid)) { |
|
258 | 1 | throw new \InvalidArgumentException('Invalid parameter type', 1468931945); |
|
259 | } |
||
260 | |||
261 | $isPageInQueue = false; |
||
262 | |||
263 | $whereClause = 'page_id = ' . (integer)$uid; |
||
264 | |||
265 | if (false !== $unprocessed_only) { |
||
266 | $whereClause .= ' AND exec_time = 0'; |
||
267 | } |
||
268 | |||
269 | if (false !== $timed_only) { |
||
270 | $whereClause .= ' AND scheduled != 0'; |
||
271 | } |
||
272 | |||
273 | if (false !== $timestamp) { |
||
274 | $whereClause .= ' AND scheduled = ' . (integer)$timestamp; |
||
275 | } |
||
276 | |||
277 | $count = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows( |
||
278 | '*', |
||
279 | 'tx_crawler_queue', |
||
280 | $whereClause |
||
281 | ); |
||
282 | |||
283 | if (false !== $count && $count > 0) { |
||
284 | $isPageInQueue = true; |
||
285 | } |
||
286 | |||
287 | return $isPageInQueue; |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Method to return the latest Crawler Timestamp for a page. |
||
292 | * |
||
293 | * @param int $uid uid id of the page |
||
294 | * @param bool $future_crawldates_only |
||
295 | * @param bool $unprocessed_only |
||
296 | * |
||
297 | * @return int |
||
298 | */ |
||
299 | public function getLatestCrawlTimestampForPage($uid, $future_crawldates_only = false, $unprocessed_only = false) |
||
300 | { |
||
301 | $uid = intval($uid); |
||
302 | $query = 'max(scheduled) as latest'; |
||
303 | $where = ' page_id = ' . $uid; |
||
304 | |||
305 | if ($future_crawldates_only) { |
||
306 | $where .= ' AND scheduled > ' . time(); |
||
307 | } |
||
308 | |||
309 | if ($unprocessed_only) { |
||
310 | $where .= ' AND exec_time = 0'; |
||
311 | } |
||
312 | |||
313 | $rs = $GLOBALS['TYPO3_DB']->exec_SELECTquery($query, 'tx_crawler_queue', $where); |
||
314 | if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($rs)) { |
||
315 | $res = $row['latest']; |
||
316 | } else { |
||
317 | $res = 0; |
||
318 | } |
||
319 | |||
320 | return intval($res); |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * Returns an array with timestamps when the page has been scheduled for crawling and |
||
325 | * at what time the scheduled crawl has been executed. The array also contains items that are |
||
326 | * scheduled but have note been crawled yet. |
||
327 | * |
||
328 | * @param int $uid uid of the page |
||
329 | * @param bool $limit |
||
330 | * |
||
331 | * @return array array with the crawl-history of a page => 0 : scheduled time , 1 : executed_time, 2 : set_id |
||
332 | */ |
||
333 | public function getCrawlHistoryForPage($uid, $limit = 0) |
||
334 | { |
||
335 | $uid = intval($uid); |
||
336 | $limit = intval($limit); |
||
337 | |||
338 | $query = 'scheduled, exec_time, set_id'; |
||
339 | $where = ' page_id = ' . $uid; |
||
340 | |||
341 | $limit_query = ($limit) ? $limit : null; |
||
342 | |||
343 | $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows($query, 'tx_crawler_queue', $where, null, null, $limit_query); |
||
344 | return $rows; |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * Method to determine unprocessed Items in the crawler queue. |
||
349 | * |
||
350 | * @deprecated since crawler v6.5.1, will be removed in crawler v9.0.0. |
||
351 | * |
||
352 | * @return array |
||
353 | */ |
||
354 | public function getUnprocessedItems() |
||
355 | { |
||
356 | $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows( |
||
357 | '*', |
||
358 | 'tx_crawler_queue', |
||
359 | 'exec_time = 0', |
||
360 | '', |
||
361 | 'page_id, scheduled' |
||
362 | ); |
||
363 | |||
364 | return $rows; |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * Method to get the number of unprocessed items in the crawler |
||
369 | * |
||
370 | * @param int number of unprocessed items in the queue |
||
371 | * @deprecated since crawler v6.5.1, will be removed in crawler v9.0.0. |
||
372 | * |
||
373 | */ |
||
374 | public function countUnprocessedItems() |
||
375 | { |
||
376 | $query = 'count(page_id) as anz'; |
||
377 | $where = 'exec_time = 0'; |
||
378 | $rs = $GLOBALS['TYPO3_DB']->exec_SELECTquery($query, 'tx_crawler_queue', $where); |
||
379 | $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($rs); |
||
380 | |||
381 | return $row['anz']; |
||
382 | } |
||
383 | |||
384 | /** |
||
385 | * Method to check if a page is in the queue which is timed for a |
||
386 | * date when it should be crawled |
||
387 | * |
||
388 | * @param int $uid uid of the page |
||
389 | * @param boolean $show_unprocessed only respect unprocessed pages |
||
390 | * |
||
391 | * @deprecated since crawler v6.5.1, will be removed in crawler v9.0.0. |
||
392 | * |
||
393 | * @return boolean |
||
394 | */ |
||
395 | public function isPageInQueueTimed($uid, $show_unprocessed = true) |
||
396 | { |
||
397 | $uid = intval($uid); |
||
398 | return $this->queueRepository->isPageInQueue($uid, $show_unprocessed); |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * Reads the registered processingInstructions of the crawler |
||
403 | * |
||
404 | * @return array |
||
405 | */ |
||
406 | private function getCrawlerProcInstructions() |
||
407 | { |
||
408 | if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['procInstructions'])) { |
||
409 | return $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['procInstructions']; |
||
410 | } |
||
411 | |||
412 | return []; |
||
413 | } |
||
414 | |||
415 | /** |
||
416 | * Removes an queue entry with a given queue id |
||
417 | * |
||
418 | * @param int $qid |
||
419 | * @deprecated since crawler v6.5.1, will be removed in crawler v9.0.0. Please use the QueueRepository instead |
||
420 | */ |
||
421 | public function removeQueueEntrie($qid) |
||
428 | |||
429 | /** |
||
430 | * Get queue statistics |
||
431 | * |
||
432 | * @param void |
||
433 | * |
||
434 | * @return array array('assignedButUnprocessed' => <>, 'unprocessed' => <>); |
||
435 | */ |
||
436 | public function getQueueStatistics() |
||
437 | { |
||
438 | return [ |
||
439 | 'assignedButUnprocessed' => $this->getQueueRepository()->countAllAssignedPendingItems(), |
||
440 | 'unprocessed' => $this->getQueueRepository()->countAllPendingItems() |
||
441 | ]; |
||
442 | } |
||
443 | |||
444 | /** |
||
445 | * Get queue repository |
||
446 | * |
||
447 | * @return QueueRepository |
||
448 | */ |
||
449 | 1 | protected function getQueueRepository() |
|
458 | |||
459 | /** |
||
460 | * Get queue statistics by configuration |
||
461 | * |
||
462 | * @return array array of array('configuration' => <>, 'assignedButUnprocessed' => <>, 'unprocessed' => <>) |
||
463 | */ |
||
464 | public function getQueueStatisticsByConfiguration() |
||
479 | |||
480 | /** |
||
481 | * Get active processes count |
||
482 | * |
||
483 | * @param void |
||
484 | * |
||
485 | * @return int |
||
486 | */ |
||
487 | public function getActiveProcessesCount() |
||
491 | |||
492 | /** |
||
493 | * Get last processed entries |
||
494 | * |
||
495 | * @param int $limit |
||
496 | * |
||
497 | * @return array |
||
498 | */ |
||
499 | public function getLastProcessedQueueEntries($limit) |
||
503 | |||
504 | /** |
||
505 | * Get current crawling speed |
||
506 | * |
||
507 | * @param float|false page speed in pages per minute |
||
508 | * |
||
509 | * @return int |
||
510 | */ |
||
511 | public function getCurrentCrawlingSpeed() |
||
546 | |||
547 | /** |
||
548 | * Get some performance data |
||
549 | * |
||
550 | * @param integer $start |
||
551 | * @param integer $end |
||
552 | * @param integer $resolution |
||
553 | * |
||
554 | * @return array data |
||
555 | * |
||
556 | * @throws \Exception |
||
557 | */ |
||
558 | public function getPerformanceData($start, $end, $resolution) |
||
607 | } |
||
608 |