Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 16 | class PageListGetter extends Service { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Get the set of pages in a given category. Extra parameters can include: |
||
| 20 | * cmtype: default 'page|subcat|file' |
||
| 21 | * cmlimit: default 10, maximum 500 (5000 for bots) |
||
| 22 | * |
||
| 23 | * @link https://www.mediawiki.org/wiki/API:Categorymembers |
||
| 24 | * @since 0.3 |
||
| 25 | * |
||
| 26 | * |
||
| 27 | */ |
||
| 28 | public function getPageListFromCategoryName( string $name, array $extraParams = [] ): Pages { |
||
| 35 | |||
| 36 | /** |
||
| 37 | * List pages that transclude a certain page. |
||
| 38 | * |
||
| 39 | * @link https://www.mediawiki.org/wiki/API:Embeddedin |
||
| 40 | * @since 0.5 |
||
| 41 | * |
||
| 42 | * |
||
| 43 | */ |
||
| 44 | public function getPageListFromPageTransclusions( string $pageName, array $extraParams = [] ): Pages { |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Get all pages that link to the given page. |
||
| 54 | * |
||
| 55 | * @link https://www.mediawiki.org/wiki/API:Linkshere |
||
| 56 | * @since 0.5 |
||
| 57 | * |
||
| 58 | * @param string $pageName The page name |
||
| 59 | * @param string[] $extraParams Any extra parameters to use |
||
| 60 | * glhprop, glhnamespace, glhshow, glhlimit |
||
| 61 | */ |
||
| 62 | public function getFromWhatLinksHere( string $pageName, array $extraParams = [] ): Pages { |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Get all pages that are linked to from the given page. |
||
| 73 | * |
||
| 74 | * @link https://www.mediawiki.org/wiki/API:Links |
||
| 75 | * |
||
| 76 | * @param string $pageName The page name |
||
| 77 | * @param string[] $extraParams Any extra parameters to use |
||
| 78 | * gpltitles, gplnamespace, gpldir, gpllimit |
||
| 79 | */ |
||
| 80 | public function getLinksFromHere( string $pageName, array $extraParams = [] ): Pages { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Get all pages that have the given prefix. |
||
| 91 | * |
||
| 92 | * @link https://www.mediawiki.org/wiki/API:Allpages |
||
| 93 | * |
||
| 94 | * @param string $prefix The page title prefix. |
||
| 95 | */ |
||
| 96 | public function getFromPrefix( string $prefix ): Pages { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Get up to 10 random pages. |
||
| 106 | * |
||
| 107 | * @link https://www.mediawiki.org/wiki/API:Random |
||
| 108 | * |
||
| 109 | * @param array $extraParams |
||
| 110 | */ |
||
| 111 | public function getRandom( array $extraParams = [] ): Pages { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Run a query to completion. |
||
| 118 | * |
||
| 119 | * @param string[] $params Query parameters |
||
| 120 | * @param string|null $contName Result subelement name for continue details |
||
| 121 | * @param string $resName Result element name for main results array |
||
| 122 | * @param string $pageIdName Result element name for page ID |
||
| 123 | * @param bool $cont Whether to continue the query, using multiple requests |
||
| 124 | */ |
||
| 125 | protected function runQuery( array $params, ?string $contName, string $resName, string $pageIdName = 'pageid', bool $cont = true ): Pages { |
||
| 159 | } |
||
| 160 |