| Total Complexity | 46 |
| Total Lines | 456 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ArticleInfoApi 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.
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 ArticleInfoApi, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class ArticleInfoApi extends Model |
||
| 21 | { |
||
| 22 | /** @var ContainerInterface The application's DI container. */ |
||
| 23 | protected $container; |
||
| 24 | |||
| 25 | /** @var int Number of revisions that belong to the page. */ |
||
| 26 | protected $numRevisions; |
||
| 27 | |||
| 28 | /** @var mixed[] Prose stats, with keys 'characters', 'words', 'references', 'unique_references', 'sections'. */ |
||
| 29 | protected $proseStats; |
||
| 30 | |||
| 31 | /** @var array Number of categories, templates and files on the page. */ |
||
| 32 | protected $transclusionData; |
||
| 33 | |||
| 34 | /** @var mixed[] Various statistics about bots that edited the page. */ |
||
| 35 | protected $bots; |
||
| 36 | |||
| 37 | /** @var int Number of edits made to the page by bots. */ |
||
| 38 | protected $botRevisionCount; |
||
| 39 | |||
| 40 | /** @var int[] Number of in and outgoing links and redirects to the page. */ |
||
| 41 | protected $linksAndRedirects; |
||
| 42 | |||
| 43 | /** @var string[] Assessments of the page (see Page::getAssessments). */ |
||
| 44 | protected $assessments; |
||
| 45 | |||
| 46 | /** @var string[] List of Wikidata and Checkwiki errors. */ |
||
| 47 | protected $bugs; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * ArticleInfoApi constructor. |
||
| 51 | * @param Page $page The page to process. |
||
| 52 | * @param ContainerInterface $container The DI container. |
||
| 53 | * @param false|int $start Start date as Unix timestmap. |
||
| 54 | * @param false|int $end End date as Unix timestamp. |
||
| 55 | */ |
||
| 56 | public function __construct(Page $page, ContainerInterface $container, $start = false, $end = false) |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Get the number of revisions belonging to the page. |
||
| 66 | * @return int |
||
| 67 | */ |
||
| 68 | public function getNumRevisions(): int |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Are there more revisions than we should process, based on the config? |
||
| 78 | * @return bool |
||
| 79 | */ |
||
| 80 | public function tooManyRevisions(): bool |
||
| 81 | { |
||
| 82 | return $this->getMaxRevisions() > 0 && $this->getNumRevisions() > $this->getMaxRevisions(); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Get the maximum number of revisions that we should process. |
||
| 87 | * @return int |
||
| 88 | */ |
||
| 89 | public function getMaxRevisions(): int |
||
| 90 | { |
||
| 91 | if (!isset($this->maxRevisions)) { |
||
| 92 | $this->maxRevisions = (int) $this->container->getParameter('app.max_page_revisions'); |
||
|
|
|||
| 93 | } |
||
| 94 | return $this->maxRevisions; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Get various basic info used in the API, including the number of revisions, unique authors, initial author |
||
| 99 | * and edit count of the initial author. This is combined into one query for better performance. Caching is |
||
| 100 | * intentionally disabled, because using the gadget, this will get hit for a different page constantly, where |
||
| 101 | * the likelihood of cache benefiting us is slim. |
||
| 102 | * @return string[]|false false if the page was not found. |
||
| 103 | */ |
||
| 104 | public function getBasicEditingInfo() |
||
| 105 | { |
||
| 106 | return $this->getRepository()->getBasicEditingInfo($this->page); |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Get the top editors to the page by edit count. |
||
| 111 | * @param int $limit Default 20, maximum 1,000. |
||
| 112 | * @param bool $noBots Set to non-false to exclude bots from the result. |
||
| 113 | * @return array |
||
| 114 | */ |
||
| 115 | public function getTopEditorsByEditCount(int $limit = 20, bool $noBots = false): array |
||
| 116 | { |
||
| 117 | // Quick cache, valid only for the same request. |
||
| 118 | static $topEditors = null; |
||
| 119 | if (null !== $topEditors) { |
||
| 120 | return $topEditors; |
||
| 121 | } |
||
| 122 | |||
| 123 | $rows = $this->getRepository()->getTopEditorsByEditCount( |
||
| 124 | $this->page, |
||
| 125 | $this->start, |
||
| 126 | $this->end, |
||
| 127 | min($limit, 1000), |
||
| 128 | $noBots |
||
| 129 | ); |
||
| 130 | |||
| 131 | $topEditors = []; |
||
| 132 | $rank = 0; |
||
| 133 | foreach ($rows as $row) { |
||
| 134 | $topEditors[] = [ |
||
| 135 | 'rank' => ++$rank, |
||
| 136 | 'username' => $row['username'], |
||
| 137 | 'count' => $row['count'], |
||
| 138 | 'minor' => $row['minor'], |
||
| 139 | 'first_edit' => [ |
||
| 140 | 'id' => $row['first_revid'], |
||
| 141 | 'timestamp' => $row['first_timestamp'], |
||
| 142 | ], |
||
| 143 | 'latest_edit' => [ |
||
| 144 | 'id' => $row['latest_revid'], |
||
| 145 | 'timestamp' => $row['latest_timestamp'], |
||
| 146 | ], |
||
| 147 | ]; |
||
| 148 | } |
||
| 149 | |||
| 150 | return $topEditors; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Get prose and reference information. |
||
| 155 | * @return array With keys 'characters', 'words', 'references', 'unique_references' |
||
| 156 | */ |
||
| 157 | public function getProseStats(): array |
||
| 158 | { |
||
| 159 | if (isset($this->proseStats)) { |
||
| 160 | return $this->proseStats; |
||
| 161 | } |
||
| 162 | |||
| 163 | $datetime = is_int($this->end) ? new DateTime("@{$this->end}") : null; |
||
| 164 | $html = $this->page->getHTMLContent($datetime); |
||
| 165 | |||
| 166 | $crawler = new Crawler($html); |
||
| 167 | |||
| 168 | [$chars, $words] = $this->countCharsAndWords($crawler, '#mw-content-text p'); |
||
| 169 | |||
| 170 | $refs = $crawler->filter('#mw-content-text .reference'); |
||
| 171 | $refContent = []; |
||
| 172 | $refs->each(function ($ref) use (&$refContent): void { |
||
| 173 | $refContent[] = $ref->text(); |
||
| 174 | }); |
||
| 175 | $uniqueRefs = count(array_unique($refContent)); |
||
| 176 | |||
| 177 | $sections = count($crawler->filter('#mw-content-text .mw-headline')); |
||
| 178 | |||
| 179 | $this->proseStats = [ |
||
| 180 | 'characters' => $chars, |
||
| 181 | 'words' => $words, |
||
| 182 | 'references' => $refs->count(), |
||
| 183 | 'unique_references' => $uniqueRefs, |
||
| 184 | 'sections' => $sections, |
||
| 185 | ]; |
||
| 186 | return $this->proseStats; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Count the number of characters and words of the plain text within the DOM element matched by the given selector. |
||
| 191 | * @param Crawler $crawler |
||
| 192 | * @param string $selector HTML selector. |
||
| 193 | * @return array [num chars, num words] |
||
| 194 | */ |
||
| 195 | private function countCharsAndWords(Crawler $crawler, string $selector): array |
||
| 196 | { |
||
| 197 | $totalChars = 0; |
||
| 198 | $totalWords = 0; |
||
| 199 | $paragraphs = $crawler->filter($selector); |
||
| 200 | $paragraphs->each(function ($node) use (&$totalChars, &$totalWords): void { |
||
| 201 | /** @var Crawler $node */ |
||
| 202 | $text = preg_replace('/\[\d+]/', '', trim($node->text(null, true))); |
||
| 203 | $totalChars += strlen($text); |
||
| 204 | $totalWords += count(explode(' ', $text)); |
||
| 205 | }); |
||
| 206 | |||
| 207 | return [$totalChars, $totalWords]; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Get the page assessments of the page. |
||
| 212 | * @see https://www.mediawiki.org/wiki/Extension:PageAssessments |
||
| 213 | * @return string[]|false False if unsupported. |
||
| 214 | * @codeCoverageIgnore |
||
| 215 | */ |
||
| 216 | public function getAssessments() |
||
| 217 | { |
||
| 218 | if (!is_array($this->assessments)) { |
||
| 219 | $this->assessments = $this->page |
||
| 220 | ->getProject() |
||
| 221 | ->getPageAssessments() |
||
| 222 | ->getAssessments($this->page); |
||
| 223 | } |
||
| 224 | return $this->assessments; |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Get the list of page's wikidata and Checkwiki errors. |
||
| 229 | * @see Page::getErrors() |
||
| 230 | * @return string[] |
||
| 231 | */ |
||
| 232 | public function getBugs(): array |
||
| 233 | { |
||
| 234 | if (!is_array($this->bugs)) { |
||
| 235 | $this->bugs = $this->page->getErrors(); |
||
| 236 | } |
||
| 237 | return $this->bugs; |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Get the number of wikidata nad CheckWiki errors. |
||
| 242 | * @return int |
||
| 243 | */ |
||
| 244 | public function numBugs(): int |
||
| 245 | { |
||
| 246 | return count($this->getBugs()); |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Generate the data structure that will used in the ArticleInfo API response. |
||
| 251 | * @param Project $project |
||
| 252 | * @param Page $page |
||
| 253 | * @return array |
||
| 254 | * @codeCoverageIgnore |
||
| 255 | */ |
||
| 256 | public function getArticleInfoApiData(Project $project, Page $page): array |
||
| 316 | } |
||
| 317 | |||
| 318 | /************************ Link statistics ************************/ |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Get the number of external links on the page. |
||
| 322 | * @return int |
||
| 323 | */ |
||
| 324 | public function linksExtCount(): int |
||
| 325 | { |
||
| 326 | return $this->getLinksAndRedirects()['links_ext_count']; |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Get the number of incoming links to the page. |
||
| 331 | * @return int |
||
| 332 | */ |
||
| 333 | public function linksInCount(): int |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Get the number of outgoing links from the page. |
||
| 340 | * @return int |
||
| 341 | */ |
||
| 342 | public function linksOutCount(): int |
||
| 343 | { |
||
| 344 | return $this->getLinksAndRedirects()['links_out_count']; |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Get the number of redirects to the page. |
||
| 349 | * @return int |
||
| 350 | */ |
||
| 351 | public function redirectsCount(): int |
||
| 352 | { |
||
| 353 | return $this->getLinksAndRedirects()['redirects_count']; |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Get the number of external, incoming and outgoing links, along with the number of redirects to the page. |
||
| 358 | * @return int[] |
||
| 359 | * @codeCoverageIgnore |
||
| 360 | */ |
||
| 361 | private function getLinksAndRedirects(): array |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Fetch transclusion data (categories, templates and files) that are on the page. |
||
| 371 | * @return array With keys 'categories', 'templates' and 'files'. |
||
| 372 | */ |
||
| 373 | public function getTransclusionData(): array |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Get the number of categories that are on the page. |
||
| 384 | * @return int |
||
| 385 | */ |
||
| 386 | public function getNumCategories(): int |
||
| 387 | { |
||
| 388 | return $this->getTransclusionData()['categories']; |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Get the number of templates that are on the page. |
||
| 393 | * @return int |
||
| 394 | */ |
||
| 395 | public function getNumTemplates(): int |
||
| 396 | { |
||
| 397 | return $this->getTransclusionData()['templates']; |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Get the number of files that are on the page. |
||
| 402 | * @return int |
||
| 403 | */ |
||
| 404 | public function getNumFiles(): int |
||
| 407 | } |
||
| 408 | |||
| 409 | /************************ Bot statistics ************************/ |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Number of edits made to the page by current or former bots. |
||
| 413 | * @param string[] $bots Used only in unit tests, where we supply mock data for the bots that will get processed. |
||
| 414 | * @return int |
||
| 415 | */ |
||
| 416 | public function getBotRevisionCount(?array $bots = null): int |
||
| 417 | { |
||
| 418 | if (isset($this->botRevisionCount)) { |
||
| 419 | return $this->botRevisionCount; |
||
| 420 | } |
||
| 421 | |||
| 422 | if (null === $bots) { |
||
| 423 | $bots = $this->getBots(); |
||
| 424 | } |
||
| 425 | |||
| 426 | $count = 0; |
||
| 427 | |||
| 428 | foreach (array_values($bots) as $data) { |
||
| 429 | $count += $data['count']; |
||
| 430 | } |
||
| 431 | |||
| 432 | $this->botRevisionCount = $count; |
||
| 433 | return $count; |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Get and set $this->bots about bots that edited the page. This is done separately from the main query because |
||
| 438 | * we use this information when computing the top 10 editors in ArticleInfo, where we don't want to include bots. |
||
| 439 | * @return mixed[] |
||
| 440 | */ |
||
| 441 | public function getBots(): array |
||
| 467 | } |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Get the number of bots that edited the page. |
||
| 471 | * @return int |
||
| 472 | */ |
||
| 473 | public function getNumBots(): int |
||
| 478 |