| Total Complexity | 47 |
| Total Lines | 473 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 1 |
Complex classes like PageRepository 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 PageRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class PageRepository extends Repository |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Get metadata about a single page from the API. |
||
| 26 | * @param Project $project The project to which the page belongs. |
||
| 27 | * @param string $pageTitle Page title. |
||
| 28 | * @return string[]|null Array with some of the following keys: pageid, title, missing, displaytitle, url. |
||
| 29 | * Returns null if page does not exist. |
||
| 30 | */ |
||
| 31 | public function getPageInfo(Project $project, string $pageTitle): ?array |
||
| 32 | { |
||
| 33 | $info = $this->getPagesInfo($project, [$pageTitle]); |
||
| 34 | return null !== $info ? array_shift($info) : null; |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Get metadata about a set of pages from the API. |
||
| 39 | * @param Project $project The project to which the pages belong. |
||
| 40 | * @param string[] $pageTitles Array of page titles. |
||
| 41 | * @return string[]|null Array keyed by the page names, each element with some of the following keys: pageid, |
||
| 42 | * title, missing, displaytitle, url. Returns null if page does not exist. |
||
| 43 | */ |
||
| 44 | public function getPagesInfo(Project $project, array $pageTitles): ?array |
||
| 45 | { |
||
| 46 | $params = [ |
||
| 47 | 'prop' => 'info|pageprops', |
||
| 48 | 'inprop' => 'protection|talkid|watched|watchers|notificationtimestamp|subjectid|url|displaytitle', |
||
| 49 | 'converttitles' => '', |
||
| 50 | 'titles' => join('|', $pageTitles), |
||
| 51 | 'formatversion' => 2, |
||
| 52 | ]; |
||
| 53 | |||
| 54 | $res = $this->executeApiRequest($project, $params); |
||
| 55 | $result = []; |
||
| 56 | if (isset($res['query']['pages'])) { |
||
| 57 | foreach ($res['query']['pages'] as $pageInfo) { |
||
| 58 | $result[$pageInfo['title']] = $pageInfo; |
||
| 59 | } |
||
| 60 | } else { |
||
| 61 | return null; |
||
| 62 | } |
||
| 63 | return $result; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Get the full page text of a set of pages. |
||
| 68 | * @param Project $project The project to which the pages belong. |
||
| 69 | * @param string[] $pageTitles Array of page titles. |
||
| 70 | * @return string[] Array keyed by the page names, with the page text as the values. |
||
| 71 | */ |
||
| 72 | public function getPagesWikitext(Project $project, array $pageTitles): array |
||
| 73 | { |
||
| 74 | $params = [ |
||
| 75 | 'prop' => 'revisions', |
||
| 76 | 'rvprop' => 'content', |
||
| 77 | 'titles' => join('|', $pageTitles), |
||
| 78 | 'formatversion' => 2, |
||
| 79 | ]; |
||
| 80 | $res = $this->executeApiRequest($project, $params); |
||
| 81 | $result = []; |
||
| 82 | |||
| 83 | if (!isset($res['query']['pages'])) { |
||
| 84 | return []; |
||
| 85 | } |
||
| 86 | |||
| 87 | foreach ($res['query']['pages'] as $page) { |
||
| 88 | if (isset($page['revisions'][0]['content'])) { |
||
| 89 | $result[$page['title']] = $page['revisions'][0]['content']; |
||
| 90 | } else { |
||
| 91 | $result[$page['title']] = ''; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | return $result; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Get revisions of a single page. |
||
| 100 | * @param Page $page The page. |
||
| 101 | * @param User|null $user Specify to get only revisions by the given user. |
||
| 102 | * @param false|int $start |
||
| 103 | * @param false|int $end |
||
| 104 | * @return string[] Each member with keys: id, timestamp, length. |
||
| 105 | */ |
||
| 106 | public function getRevisions(Page $page, ?User $user = null, $start = false, $end = false): array |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Get the statement for a single revision, so that you can iterate row by row. |
||
| 122 | * @param Page $page The page. |
||
| 123 | * @param User|null $user Specify to get only revisions by the given user. |
||
| 124 | * @param int $limit Max number of revisions to process. |
||
| 125 | * @param int $numRevisions Number of revisions, if known. This is used solely to determine the |
||
| 126 | * OFFSET if we are given a $limit (see below). If $limit is set and $numRevisions is not set, |
||
| 127 | * a separate query is ran to get the number of revisions. |
||
| 128 | * @param false|int $start |
||
| 129 | * @param false|int $end |
||
| 130 | * @return Statement |
||
| 131 | */ |
||
| 132 | public function getRevisionsStmt( |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Get a count of the number of revisions of a single page |
||
| 192 | * @param Page $page The page. |
||
| 193 | * @param User|null $user Specify to only count revisions by the given user. |
||
| 194 | * @param false|int $start |
||
| 195 | * @param false|int $end |
||
| 196 | * @return int |
||
| 197 | */ |
||
| 198 | public function getNumRevisions(Page $page, ?User $user = null, $start = false, $end = false): int |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Get any CheckWiki errors of a single page |
||
| 230 | * @param Page $page |
||
| 231 | * @return array Results from query |
||
| 232 | */ |
||
| 233 | public function getCheckWikiErrors(Page $page): array |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Get basic wikidata on the page: label and description. |
||
| 265 | * @param Page $page |
||
| 266 | * @return string[] In the format: |
||
| 267 | * [[ |
||
| 268 | * 'term' => string such as 'label', |
||
| 269 | * 'term_text' => string (value for 'label'), |
||
| 270 | * ], ... ] |
||
| 271 | */ |
||
| 272 | public function getWikidataInfo(Page $page): array |
||
| 273 | { |
||
| 274 | if (empty($page->getWikidataId())) { |
||
| 275 | return []; |
||
| 276 | } |
||
| 277 | |||
| 278 | $wikidataId = ltrim($page->getWikidataId(), 'Q'); |
||
| 279 | $lang = $page->getProject()->getLang(); |
||
| 280 | $wdp = 'wikidatawiki_p'; |
||
| 281 | |||
| 282 | $sql = "SELECT wby_name AS term, wbx_text AS term_text |
||
| 283 | FROM $wdp.wbt_item_terms |
||
| 284 | JOIN $wdp.wbt_term_in_lang ON wbit_term_in_lang_id = wbtl_id |
||
| 285 | JOIN $wdp.wbt_type ON wbtl_type_id = wby_id |
||
| 286 | JOIN $wdp.wbt_text_in_lang ON wbtl_text_in_lang_id = wbxl_id |
||
| 287 | JOIN $wdp.wbt_text ON wbxl_text_id = wbx_id |
||
| 288 | WHERE wbit_item_id = :wikidataId |
||
| 289 | AND wby_name IN ('label', 'description') |
||
| 290 | AND wbxl_language = :lang"; |
||
| 291 | |||
| 292 | return $this->executeProjectsQuery($sql, [ |
||
| 293 | 'lang' => $lang, |
||
| 294 | 'wikidataId' => $wikidataId, |
||
| 295 | ])->fetchAll(); |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Get or count all wikidata items for the given page, |
||
| 300 | * not just languages of sister projects |
||
| 301 | * @param Page $page |
||
| 302 | * @param bool $count Set to true to get only a COUNT |
||
| 303 | * @return string[]|int Records as returend by the DB, |
||
| 304 | * or raw COUNT of the records. |
||
| 305 | */ |
||
| 306 | public function getWikidataItems(Page $page, bool $count = false) |
||
| 307 | { |
||
| 308 | if (!$page->getWikidataId()) { |
||
| 309 | return $count ? 0 : []; |
||
| 310 | } |
||
| 311 | |||
| 312 | $wikidataId = ltrim($page->getWikidataId(), 'Q'); |
||
| 313 | |||
| 314 | $sql = "SELECT " . ($count ? 'COUNT(*) AS count' : '*') . " |
||
| 315 | FROM wikidatawiki_p.wb_items_per_site |
||
| 316 | WHERE ips_item_id = :wikidataId"; |
||
| 317 | |||
| 318 | $result = $this->executeProjectsQuery($sql, [ |
||
| 319 | 'wikidataId' => $wikidataId, |
||
| 320 | ])->fetchAll(); |
||
| 321 | |||
| 322 | return $count ? (int) $result[0]['count'] : $result; |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Get number of in and outgoing links and redirects to the given page. |
||
| 327 | * @param Page $page |
||
| 328 | * @return string[] Counts with the keys 'links_ext_count', 'links_out_count', |
||
| 329 | * 'links_in_count' and 'redirects_count' |
||
| 330 | */ |
||
| 331 | public function countLinksAndRedirects(Page $page): array |
||
| 332 | { |
||
| 333 | $externalLinksTable = $this->getTableName($page->getProject()->getDatabaseName(), 'externallinks'); |
||
| 334 | $pageLinksTable = $this->getTableName($page->getProject()->getDatabaseName(), 'pagelinks'); |
||
| 335 | $redirectTable = $this->getTableName($page->getProject()->getDatabaseName(), 'redirect'); |
||
| 336 | |||
| 337 | $sql = "SELECT COUNT(*) AS value, 'links_ext' AS type |
||
| 338 | FROM $externalLinksTable WHERE el_from = :id |
||
| 339 | UNION |
||
| 340 | SELECT COUNT(*) AS value, 'links_out' AS type |
||
| 341 | FROM $pageLinksTable WHERE pl_from = :id |
||
| 342 | UNION |
||
| 343 | SELECT COUNT(*) AS value, 'links_in' AS type |
||
| 344 | FROM $pageLinksTable WHERE pl_namespace = :namespace AND pl_title = :title |
||
| 345 | UNION |
||
| 346 | SELECT COUNT(*) AS value, 'redirects' AS type |
||
| 347 | FROM $redirectTable WHERE rd_namespace = :namespace AND rd_title = :title"; |
||
| 348 | |||
| 349 | $params = [ |
||
| 350 | 'id' => $page->getId(), |
||
| 351 | 'title' => str_replace(' ', '_', $page->getTitleWithoutNamespace()), |
||
| 352 | 'namespace' => $page->getNamespace(), |
||
| 353 | ]; |
||
| 354 | |||
| 355 | $res = $this->executeProjectsQuery($sql, $params); |
||
| 356 | $data = []; |
||
| 357 | |||
| 358 | // Transform to associative array by 'type' |
||
| 359 | foreach ($res as $row) { |
||
| 360 | $data[$row['type'] . '_count'] = (int)$row['value']; |
||
| 361 | } |
||
| 362 | |||
| 363 | return $data; |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Count wikidata items for the given page, not just languages of sister projects |
||
| 368 | * @param Page $page |
||
| 369 | * @return int Number of records. |
||
| 370 | */ |
||
| 371 | public function countWikidataItems(Page $page): int |
||
| 372 | { |
||
| 373 | return $this->getWikidataItems($page, true); |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Get page views for the given page and timeframe. |
||
| 378 | * @fixme use Symfony Guzzle package. |
||
| 379 | * @param Page $page |
||
| 380 | * @param string|DateTime $start In the format YYYYMMDD |
||
| 381 | * @param string|DateTime $end In the format YYYYMMDD |
||
| 382 | * @return string[] |
||
| 383 | */ |
||
| 384 | public function getPageviews(Page $page, $start, $end): array |
||
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Get the full HTML content of the the page. |
||
| 413 | * @param Page $page |
||
| 414 | * @param int $revId What revision to query for. |
||
| 415 | * @return string |
||
| 416 | */ |
||
| 417 | public function getHTMLContent(Page $page, ?int $revId = null): string |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Get the ID of the revision of a page at the time of the given DateTime. |
||
| 432 | * @param Page $page |
||
| 433 | * @param DateTime $date |
||
| 434 | * @return int |
||
| 435 | */ |
||
| 436 | public function getRevisionIdAtDate(Page $page, DateTime $date): int |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Get HTML display titles of a set of pages (or the normal title if there's no display title). |
||
| 451 | * This will send t/50 API requests where t is the number of titles supplied. |
||
| 452 | * @param Project $project The project. |
||
| 453 | * @param string[] $pageTitles The titles to fetch. |
||
| 454 | * @return string[] Keys are the original supplied title, and values are the display titles. |
||
| 455 | * @static |
||
| 456 | */ |
||
| 457 | public function displayTitles(Project $project, array $pageTitles): array |
||
| 495 | } |
||
| 496 | } |
||
| 497 |