Complex classes like Scribunto_LuaWikibaseLibrary 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 Scribunto_LuaWikibaseLibrary, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class Scribunto_LuaWikibaseLibrary extends Scribunto_LuaLibraryBase { |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var WikibaseLanguageIndependentLuaBindings|null |
||
| 42 | */ |
||
| 43 | private $languageIndependentLuaBindings = null; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var WikibaseLanguageDependentLuaBindings|null |
||
| 47 | */ |
||
| 48 | private $languageDependentLuaBindings = null; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var EntityAccessor|null |
||
| 52 | */ |
||
| 53 | private $entityAccessor = null; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var SnakSerializationRenderer[] |
||
| 57 | */ |
||
| 58 | private $snakSerializationRenderers = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var TermLanguageFallbackChain|null |
||
| 62 | */ |
||
| 63 | private $termFallbackChain = null; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var ParserOutputUsageAccumulator|null |
||
| 67 | */ |
||
| 68 | private $usageAccumulator = null; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var PropertyIdResolver|null |
||
| 72 | */ |
||
| 73 | private $propertyIdResolver = null; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var PropertyOrderProvider|null |
||
| 77 | */ |
||
| 78 | private $propertyOrderProvider = null; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var EntityIdParser|null |
||
| 82 | */ |
||
| 83 | private $entityIdParser = null; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var RepoLinker|null |
||
| 87 | */ |
||
| 88 | private $repoLinker = null; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var LuaFunctionCallTracker|null |
||
| 92 | */ |
||
| 93 | private $luaFunctionCallTracker = null; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var string[]|null |
||
| 97 | */ |
||
| 98 | private $luaEntityModules = null; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @return WikibaseLanguageIndependentLuaBindings |
||
| 102 | */ |
||
| 103 | private function getLanguageIndependentLuaBindings() { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @return WikibaseLanguageDependentLuaBindings |
||
| 113 | */ |
||
| 114 | private function getLanguageDependentLuaBindings() { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @return EntityAccessor |
||
| 124 | */ |
||
| 125 | private function getEntityAccessor() { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param string $type One of DataAccessSnakFormatterFactory::TYPE_* |
||
| 135 | * |
||
| 136 | * @return SnakSerializationRenderer |
||
| 137 | */ |
||
| 138 | private function getSnakSerializationRenderer( $type ) { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @return TermLanguageFallbackChain |
||
| 148 | */ |
||
| 149 | private function getLanguageFallbackChain() { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @return ParserOutputUsageAccumulator |
||
| 160 | */ |
||
| 161 | public function getUsageAccumulator() { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @return PropertyIdResolver |
||
| 175 | */ |
||
| 176 | private function getPropertyIdResolver() { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Returns the language to use. If we are on a multilingual wiki |
||
| 194 | * (allowDataAccessInUserLanguage is true) this will be the user's interface |
||
| 195 | * language, otherwise it will be the content language. |
||
| 196 | * In a perfect world, this would equal Parser::getTargetLanguage. |
||
| 197 | * |
||
| 198 | * This can probably be removed after T114640 has been implemented. |
||
| 199 | * |
||
| 200 | * Please note, that this splits the parser cache by user language, if |
||
| 201 | * allowDataAccessInUserLanguage is true. |
||
| 202 | * |
||
| 203 | * @return Language |
||
| 204 | */ |
||
| 205 | private function getLanguage() { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @return LuaFunctionCallTracker |
||
| 215 | */ |
||
| 216 | private function getLuaFunctionCallTracker() { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @return bool |
||
| 236 | */ |
||
| 237 | private function allowDataAccessInUserLanguage() { |
||
| 242 | |||
| 243 | private function newEntityAccessor() { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param string $type One of DataAccessSnakFormatterFactory::TYPE_* |
||
| 263 | * |
||
| 264 | * @return SnakSerializationRenderer |
||
| 265 | */ |
||
| 266 | private function newSnakSerializationRenderer( $type ) { |
||
| 294 | |||
| 295 | private function newLanguageDependentLuaBindings() { |
||
| 322 | |||
| 323 | private function newLanguageIndependentLuaBindings() { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @return EntityIdParser |
||
| 361 | */ |
||
| 362 | private function getEntityIdParser() { |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @throws \ScribuntoException |
||
| 372 | * @return EntityId |
||
| 373 | */ |
||
| 374 | private function parseUserGivenEntityId( $idSerialization ) { |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Register mw.wikibase.lua library |
||
| 387 | * |
||
| 388 | * @return array |
||
| 389 | */ |
||
| 390 | public function register() { |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Wrapper for getEntity in EntityAccessor |
||
| 427 | * |
||
| 428 | * @param string $prefixedEntityId |
||
| 429 | * |
||
| 430 | * @throws ScribuntoException |
||
| 431 | * @return array |
||
| 432 | */ |
||
| 433 | public function getEntity( $prefixedEntityId ) { |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Wrapper for getReferencedEntityId in WikibaseLanguageIndependentLuaBindings |
||
| 453 | * |
||
| 454 | * @param string $prefixedFromEntityId |
||
| 455 | * @param string $prefixedPropertyId |
||
| 456 | * @param string[] $prefixedToIds |
||
| 457 | * |
||
| 458 | * @throws ScribuntoException |
||
| 459 | * @return array |
||
| 460 | */ |
||
| 461 | public function getReferencedEntityId( $prefixedFromEntityId, $prefixedPropertyId, $prefixedToIds ) { |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Wrapper for entityExists in EntityAccessor |
||
| 498 | * |
||
| 499 | * @param string $prefixedEntityId |
||
| 500 | * |
||
| 501 | * @throws ScribuntoException |
||
| 502 | * @return bool[] |
||
| 503 | */ |
||
| 504 | public function entityExists( $prefixedEntityId ) { |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Wrapper for getEntityStatements in EntityAccessor |
||
| 519 | * |
||
| 520 | * @param string $prefixedEntityId |
||
| 521 | * @param string $propertyId |
||
| 522 | * @param string $rank Which statements to include. Either "best" or "all". |
||
| 523 | * |
||
| 524 | * @throws ScribuntoException |
||
| 525 | * @return array |
||
| 526 | */ |
||
| 527 | public function getEntityStatements( $prefixedEntityId, $propertyId, $rank ) { |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Wrapper for getEntityId in WikibaseLanguageIndependentLuaBindings |
||
| 549 | * |
||
| 550 | * @param string $pageTitle |
||
| 551 | * @param string|null $globalSiteId |
||
| 552 | * |
||
| 553 | * @return array |
||
| 554 | */ |
||
| 555 | public function getEntityId( $pageTitle, $globalSiteId = null ) { |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Wrapper for getSetting in WikibaseLanguageIndependentLuaBindings |
||
| 564 | * |
||
| 565 | * @param string $setting |
||
| 566 | * |
||
| 567 | * @return array |
||
| 568 | */ |
||
| 569 | public function getSetting( $setting ) { |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @param string $entityIdSerialization entity ID serialization |
||
| 576 | * |
||
| 577 | * @return string[]|null[] |
||
| 578 | */ |
||
| 579 | public function getEntityUrl( $entityIdSerialization ) { |
||
| 592 | |||
| 593 | /** |
||
| 594 | * @return RepoLinker |
||
| 595 | */ |
||
| 596 | private function getRepoLinker() { |
||
| 603 | |||
| 604 | public function setRepoLinker( RepoLinker $repoLinker ) { |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Wrapper for getLabel in WikibaseLanguageDependentLuaBindings |
||
| 610 | * |
||
| 611 | * @param string $prefixedEntityId |
||
| 612 | * |
||
| 613 | * @return string[]|null[] |
||
| 614 | */ |
||
| 615 | public function getLabel( $prefixedEntityId ) { |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Wrapper for getLabelByLanguage in WikibaseLanguageIndependentLuaBindings |
||
| 623 | * |
||
| 624 | * @param string $prefixedEntityId |
||
| 625 | * @param string $languageCode |
||
| 626 | * |
||
| 627 | * @return string[]|null[] |
||
| 628 | */ |
||
| 629 | public function getLabelByLanguage( $prefixedEntityId, $languageCode ) { |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Wrapper for getDescription in WikibaseLanguageDependentLuaBindings |
||
| 638 | * |
||
| 639 | * @param string $prefixedEntityId |
||
| 640 | * |
||
| 641 | * @return string[]|null[] |
||
| 642 | */ |
||
| 643 | public function getDescription( $prefixedEntityId ) { |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Wrapper for getSiteLinkPageName in WikibaseLanguageIndependentLuaBindings |
||
| 651 | * |
||
| 652 | * @param string $prefixedItemId |
||
| 653 | * @param string|null $globalSiteId |
||
| 654 | * |
||
| 655 | * @return string[] |
||
| 656 | */ |
||
| 657 | public function getSiteLinkPageName( $prefixedItemId, $globalSiteId ) { |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Wrapper for WikibaseLanguageIndependentLuaBindings::isValidEntityId |
||
| 666 | * |
||
| 667 | * @param string $entityIdSerialization |
||
| 668 | * |
||
| 669 | * @throws ScribuntoException |
||
| 670 | * @return bool[] One bool telling whether the entity id is valid (parseable). |
||
| 671 | */ |
||
| 672 | public function isValidEntityId( $entityIdSerialization ) { |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Wrapper for SnakSerializationRenderer::renderSnak, set to output wikitext escaped plain text. |
||
| 680 | * |
||
| 681 | * @param array $snakSerialization |
||
| 682 | * |
||
| 683 | * @throws ScribuntoException |
||
| 684 | * @return string[] Wikitext |
||
| 685 | */ |
||
| 686 | public function renderSnak( $snakSerialization ) { |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Wrapper for SnakSerializationRenderer::renderSnak, set to output rich wikitext. |
||
| 702 | * |
||
| 703 | * @param array $snakSerialization |
||
| 704 | * |
||
| 705 | * @throws ScribuntoException |
||
| 706 | * @return string[] Wikitext |
||
| 707 | */ |
||
| 708 | public function formatValue( $snakSerialization ) { |
||
| 721 | |||
| 722 | /** |
||
| 723 | * Wrapper for SnakSerializationRenderer::renderSnaks, set to output wikitext escaped plain text. |
||
| 724 | * |
||
| 725 | * @param array[] $snaksSerialization |
||
| 726 | * |
||
| 727 | * @throws ScribuntoException |
||
| 728 | * @return string[] Wikitext |
||
| 729 | */ |
||
| 730 | public function renderSnaks( $snaksSerialization ) { |
||
| 743 | |||
| 744 | /** |
||
| 745 | * Wrapper for SnakSerializationRenderer::renderSnaks, set to output rich wikitext. |
||
| 746 | * |
||
| 747 | * @param array[] $snaksSerialization |
||
| 748 | * |
||
| 749 | * @throws ScribuntoException |
||
| 750 | * @return string[] Wikitext |
||
| 751 | */ |
||
| 752 | public function formatValues( $snaksSerialization ) { |
||
| 765 | |||
| 766 | /** |
||
| 767 | * Wrapper for PropertyIdResolver |
||
| 768 | * |
||
| 769 | * @param string $propertyLabelOrId |
||
| 770 | * |
||
| 771 | * @return string[]|null[] |
||
| 772 | */ |
||
| 773 | public function resolvePropertyId( $propertyLabelOrId ) { |
||
| 786 | |||
| 787 | /** |
||
| 788 | * @param string[] $propertyIds |
||
| 789 | * |
||
| 790 | * @return array[] |
||
| 791 | */ |
||
| 792 | public function orderProperties( array $propertyIds ) { |
||
| 817 | |||
| 818 | /** |
||
| 819 | * Return the order of properties as provided by the PropertyOrderProvider |
||
| 820 | * @return array[] either int[][] or null[][] |
||
| 821 | */ |
||
| 822 | public function getPropertyOrder() { |
||
| 825 | |||
| 826 | /** |
||
| 827 | * Increment the given stats key. |
||
| 828 | * |
||
| 829 | * @param string $key |
||
| 830 | */ |
||
| 831 | public function incrementStatsKey( $key ) { |
||
| 834 | |||
| 835 | /** |
||
| 836 | * Get the entity module name to use for the entity with this ID. |
||
| 837 | * |
||
| 838 | * @param string $prefixedEntityId |
||
| 839 | * @return string[] |
||
| 840 | */ |
||
| 841 | public function getEntityModuleName( $prefixedEntityId ) { |
||
| 853 | |||
| 854 | /** |
||
| 855 | * @return PropertyOrderProvider |
||
| 856 | */ |
||
| 857 | private function getPropertyOrderProvider() { |
||
| 864 | |||
| 865 | public function setPropertyOrderProvider( PropertyOrderProvider $propertyOrderProvider ) { |
||
| 868 | |||
| 869 | private function getLuaEntityModules() { |
||
| 876 | |||
| 877 | } |
||
| 878 |