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:
Complex classes like ApiPageSet 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 ApiPageSet, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
41 | class ApiPageSet extends ApiBase { |
||
42 | /** |
||
43 | * Constructor flag: The new instance of ApiPageSet will ignore the 'generator=' parameter |
||
44 | * @since 1.21 |
||
45 | */ |
||
46 | const DISABLE_GENERATORS = 1; |
||
47 | |||
48 | private $mDbSource; |
||
49 | private $mParams; |
||
50 | private $mResolveRedirects; |
||
51 | private $mConvertTitles; |
||
52 | private $mAllowGenerator; |
||
53 | |||
54 | private $mAllPages = []; // [ns][dbkey] => page_id or negative when missing |
||
55 | private $mTitles = []; |
||
56 | private $mGoodAndMissingPages = []; // [ns][dbkey] => page_id or negative when missing |
||
57 | private $mGoodPages = []; // [ns][dbkey] => page_id |
||
58 | private $mGoodTitles = []; |
||
59 | private $mMissingPages = []; // [ns][dbkey] => fake page_id |
||
60 | private $mMissingTitles = []; |
||
61 | /** @var array [fake_page_id] => [ 'title' => $title, 'invalidreason' => $reason ] */ |
||
62 | private $mInvalidTitles = []; |
||
63 | private $mMissingPageIDs = []; |
||
64 | private $mRedirectTitles = []; |
||
65 | private $mSpecialTitles = []; |
||
66 | private $mNormalizedTitles = []; |
||
67 | private $mInterwikiTitles = []; |
||
68 | /** @var Title[] */ |
||
69 | private $mPendingRedirectIDs = []; |
||
70 | private $mResolvedRedirectTitles = []; |
||
71 | private $mConvertedTitles = []; |
||
72 | private $mGoodRevIDs = []; |
||
73 | private $mLiveRevIDs = []; |
||
74 | private $mDeletedRevIDs = []; |
||
75 | private $mMissingRevIDs = []; |
||
76 | private $mGeneratorData = []; // [ns][dbkey] => data array |
||
77 | private $mFakePageId = -1; |
||
78 | private $mCacheMode = 'public'; |
||
79 | private $mRequestedPageFields = []; |
||
80 | /** @var int */ |
||
81 | private $mDefaultNamespace = NS_MAIN; |
||
82 | /** @var callable|null */ |
||
83 | private $mRedirectMergePolicy; |
||
84 | |||
85 | /** |
||
86 | * Add all items from $values into the result |
||
87 | * @param array $result Output |
||
88 | * @param array $values Values to add |
||
89 | * @param string $flag The name of the boolean flag to mark this element |
||
90 | * @param string $name If given, name of the value |
||
91 | */ |
||
92 | View Code Duplication | private static function addValues( array &$result, $values, $flag = null, $name = null ) { |
|
108 | |||
109 | /** |
||
110 | * @param ApiBase $dbSource Module implementing getDB(). |
||
111 | * Allows PageSet to reuse existing db connection from the shared state like ApiQuery. |
||
112 | * @param int $flags Zero or more flags like DISABLE_GENERATORS |
||
113 | * @param int $defaultNamespace The namespace to use if none is specified by a prefix. |
||
114 | * @since 1.21 accepts $flags instead of two boolean values |
||
115 | */ |
||
116 | public function __construct( ApiBase $dbSource, $flags = 0, $defaultNamespace = NS_MAIN ) { |
||
126 | |||
127 | /** |
||
128 | * In case execute() is not called, call this method to mark all relevant parameters as used |
||
129 | * This prevents unused parameters from being reported as warnings |
||
130 | */ |
||
131 | public function executeDryRun() { |
||
134 | |||
135 | /** |
||
136 | * Populate the PageSet from the request parameters. |
||
137 | */ |
||
138 | public function execute() { |
||
141 | |||
142 | /** |
||
143 | * Populate the PageSet from the request parameters. |
||
144 | * @param bool $isDryRun If true, instantiates generator, but only to mark |
||
145 | * relevant parameters as used |
||
146 | */ |
||
147 | private function executeInternal( $isDryRun ) { |
||
232 | |||
233 | /** |
||
234 | * Check whether this PageSet is resolving redirects |
||
235 | * @return bool |
||
236 | */ |
||
237 | public function isResolvingRedirects() { |
||
240 | |||
241 | /** |
||
242 | * Return the parameter name that is the source of data for this PageSet |
||
243 | * |
||
244 | * If multiple source parameters are specified (e.g. titles and pageids), |
||
245 | * one will be named arbitrarily. |
||
246 | * |
||
247 | * @return string|null |
||
248 | */ |
||
249 | public function getDataSource() { |
||
265 | |||
266 | /** |
||
267 | * Request an additional field from the page table. |
||
268 | * Must be called before execute() |
||
269 | * @param string $fieldName Field name |
||
270 | */ |
||
271 | public function requestField( $fieldName ) { |
||
274 | |||
275 | /** |
||
276 | * Get the value of a custom field previously requested through |
||
277 | * requestField() |
||
278 | * @param string $fieldName Field name |
||
279 | * @return mixed Field value |
||
280 | */ |
||
281 | public function getCustomField( $fieldName ) { |
||
284 | |||
285 | /** |
||
286 | * Get the fields that have to be queried from the page table: |
||
287 | * the ones requested through requestField() and a few basic ones |
||
288 | * we always need |
||
289 | * @return array Array of field names |
||
290 | */ |
||
291 | public function getPageTableFields() { |
||
319 | |||
320 | /** |
||
321 | * Returns an array [ns][dbkey] => page_id for all requested titles. |
||
322 | * page_id is a unique negative number in case title was not found. |
||
323 | * Invalid titles will also have negative page IDs and will be in namespace 0 |
||
324 | * @return array |
||
325 | */ |
||
326 | public function getAllTitlesByNamespace() { |
||
329 | |||
330 | /** |
||
331 | * All Title objects provided. |
||
332 | * @return Title[] |
||
333 | */ |
||
334 | public function getTitles() { |
||
337 | |||
338 | /** |
||
339 | * Returns the number of unique pages (not revisions) in the set. |
||
340 | * @return int |
||
341 | */ |
||
342 | public function getTitleCount() { |
||
345 | |||
346 | /** |
||
347 | * Returns an array [ns][dbkey] => page_id for all good titles. |
||
348 | * @return array |
||
349 | */ |
||
350 | public function getGoodTitlesByNamespace() { |
||
353 | |||
354 | /** |
||
355 | * Title objects that were found in the database. |
||
356 | * @return Title[] Array page_id (int) => Title (obj) |
||
357 | */ |
||
358 | public function getGoodTitles() { |
||
361 | |||
362 | /** |
||
363 | * Returns the number of found unique pages (not revisions) in the set. |
||
364 | * @return int |
||
365 | */ |
||
366 | public function getGoodTitleCount() { |
||
369 | |||
370 | /** |
||
371 | * Returns an array [ns][dbkey] => fake_page_id for all missing titles. |
||
372 | * fake_page_id is a unique negative number. |
||
373 | * @return array |
||
374 | */ |
||
375 | public function getMissingTitlesByNamespace() { |
||
378 | |||
379 | /** |
||
380 | * Title objects that were NOT found in the database. |
||
381 | * The array's index will be negative for each item |
||
382 | * @return Title[] |
||
383 | */ |
||
384 | public function getMissingTitles() { |
||
387 | |||
388 | /** |
||
389 | * Returns an array [ns][dbkey] => page_id for all good and missing titles. |
||
390 | * @return array |
||
391 | */ |
||
392 | public function getGoodAndMissingTitlesByNamespace() { |
||
395 | |||
396 | /** |
||
397 | * Title objects for good and missing titles. |
||
398 | * @return array |
||
399 | */ |
||
400 | public function getGoodAndMissingTitles() { |
||
403 | |||
404 | /** |
||
405 | * Titles that were deemed invalid by Title::newFromText() |
||
406 | * The array's index will be unique and negative for each item |
||
407 | * @deprecated since 1.26, use self::getInvalidTitlesAndReasons() |
||
408 | * @return string[] Array of strings (not Title objects) |
||
409 | */ |
||
410 | public function getInvalidTitles() { |
||
416 | |||
417 | /** |
||
418 | * Titles that were deemed invalid by Title::newFromText() |
||
419 | * The array's index will be unique and negative for each item |
||
420 | * @return array[] Array of arrays with 'title' and 'invalidreason' properties |
||
421 | */ |
||
422 | public function getInvalidTitlesAndReasons() { |
||
425 | |||
426 | /** |
||
427 | * Page IDs that were not found in the database |
||
428 | * @return array Array of page IDs |
||
429 | */ |
||
430 | public function getMissingPageIDs() { |
||
433 | |||
434 | /** |
||
435 | * Get a list of redirect resolutions - maps a title to its redirect |
||
436 | * target, as an array of output-ready arrays |
||
437 | * @return Title[] |
||
438 | */ |
||
439 | public function getRedirectTitles() { |
||
442 | |||
443 | /** |
||
444 | * Get a list of redirect resolutions - maps a title to its redirect |
||
445 | * target. Includes generator data for redirect source when available. |
||
446 | * @param ApiResult $result |
||
447 | * @return array Array of prefixed_title (string) => Title object |
||
448 | * @since 1.21 |
||
449 | */ |
||
450 | public function getRedirectTitlesAsResult( $result = null ) { |
||
480 | |||
481 | /** |
||
482 | * Get a list of title normalizations - maps a title to its normalized |
||
483 | * version. |
||
484 | * @return array Array of raw_prefixed_title (string) => prefixed_title (string) |
||
485 | */ |
||
486 | public function getNormalizedTitles() { |
||
489 | |||
490 | /** |
||
491 | * Get a list of title normalizations - maps a title to its normalized |
||
492 | * version in the form of result array. |
||
493 | * @param ApiResult $result |
||
494 | * @return array Array of raw_prefixed_title (string) => prefixed_title (string) |
||
495 | * @since 1.21 |
||
496 | */ |
||
497 | public function getNormalizedTitlesAsResult( $result = null ) { |
||
515 | |||
516 | /** |
||
517 | * Get a list of title conversions - maps a title to its converted |
||
518 | * version. |
||
519 | * @return array Array of raw_prefixed_title (string) => prefixed_title (string) |
||
520 | */ |
||
521 | public function getConvertedTitles() { |
||
524 | |||
525 | /** |
||
526 | * Get a list of title conversions - maps a title to its converted |
||
527 | * version as a result array. |
||
528 | * @param ApiResult $result |
||
529 | * @return array Array of (from, to) strings |
||
530 | * @since 1.21 |
||
531 | */ |
||
532 | public function getConvertedTitlesAsResult( $result = null ) { |
||
546 | |||
547 | /** |
||
548 | * Get a list of interwiki titles - maps a title to its interwiki |
||
549 | * prefix. |
||
550 | * @return array Array of raw_prefixed_title (string) => interwiki_prefix (string) |
||
551 | */ |
||
552 | public function getInterwikiTitles() { |
||
555 | |||
556 | /** |
||
557 | * Get a list of interwiki titles - maps a title to its interwiki |
||
558 | * prefix as result. |
||
559 | * @param ApiResult $result |
||
560 | * @param bool $iwUrl |
||
561 | * @return array Array of raw_prefixed_title (string) => interwiki_prefix (string) |
||
562 | * @since 1.21 |
||
563 | */ |
||
564 | public function getInterwikiTitlesAsResult( $result = null, $iwUrl = false ) { |
||
583 | |||
584 | /** |
||
585 | * Get an array of invalid/special/missing titles. |
||
586 | * |
||
587 | * @param array $invalidChecks List of types of invalid titles to include. |
||
588 | * Recognized values are: |
||
589 | * - invalidTitles: Titles and reasons from $this->getInvalidTitlesAndReasons() |
||
590 | * - special: Titles from $this->getSpecialTitles() |
||
591 | * - missingIds: ids from $this->getMissingPageIDs() |
||
592 | * - missingRevIds: ids from $this->getMissingRevisionIDs() |
||
593 | * - missingTitles: Titles from $this->getMissingTitles() |
||
594 | * - interwikiTitles: Titles from $this->getInterwikiTitlesAsResult() |
||
595 | * @return array Array suitable for inclusion in the response |
||
596 | * @since 1.23 |
||
597 | */ |
||
598 | public function getInvalidTitlesAndRevisions( $invalidChecks = [ 'invalidTitles', |
||
623 | |||
624 | /** |
||
625 | * Get the list of valid revision IDs (requested with the revids= parameter) |
||
626 | * @return array Array of revID (int) => pageID (int) |
||
627 | */ |
||
628 | public function getRevisionIDs() { |
||
631 | |||
632 | /** |
||
633 | * Get the list of non-deleted revision IDs (requested with the revids= parameter) |
||
634 | * @return array Array of revID (int) => pageID (int) |
||
635 | */ |
||
636 | public function getLiveRevisionIDs() { |
||
639 | |||
640 | /** |
||
641 | * Get the list of revision IDs that were associated with deleted titles. |
||
642 | * @return array Array of revID (int) => pageID (int) |
||
643 | */ |
||
644 | public function getDeletedRevisionIDs() { |
||
647 | |||
648 | /** |
||
649 | * Revision IDs that were not found in the database |
||
650 | * @return array Array of revision IDs |
||
651 | */ |
||
652 | public function getMissingRevisionIDs() { |
||
655 | |||
656 | /** |
||
657 | * Revision IDs that were not found in the database as result array. |
||
658 | * @param ApiResult $result |
||
659 | * @return array Array of revision IDs |
||
660 | * @since 1.21 |
||
661 | */ |
||
662 | public function getMissingRevisionIDsAsResult( $result = null ) { |
||
675 | |||
676 | /** |
||
677 | * Get the list of titles with negative namespace |
||
678 | * @return Title[] |
||
679 | */ |
||
680 | public function getSpecialTitles() { |
||
683 | |||
684 | /** |
||
685 | * Returns the number of revisions (requested with revids= parameter). |
||
686 | * @return int Number of revisions. |
||
687 | */ |
||
688 | public function getRevisionCount() { |
||
691 | |||
692 | /** |
||
693 | * Populate this PageSet from a list of Titles |
||
694 | * @param array $titles Array of Title objects |
||
695 | */ |
||
696 | public function populateFromTitles( $titles ) { |
||
699 | |||
700 | /** |
||
701 | * Populate this PageSet from a list of page IDs |
||
702 | * @param array $pageIDs Array of page IDs |
||
703 | */ |
||
704 | public function populateFromPageIDs( $pageIDs ) { |
||
707 | |||
708 | /** |
||
709 | * Populate this PageSet from a rowset returned from the database |
||
710 | * |
||
711 | * Note that the query result must include the columns returned by |
||
712 | * $this->getPageTableFields(). |
||
713 | * |
||
714 | * @param IDatabase $db |
||
715 | * @param ResultWrapper $queryResult Query result object |
||
716 | */ |
||
717 | public function populateFromQueryResult( $db, $queryResult ) { |
||
720 | |||
721 | /** |
||
722 | * Populate this PageSet from a list of revision IDs |
||
723 | * @param array $revIDs Array of revision IDs |
||
724 | */ |
||
725 | public function populateFromRevisionIDs( $revIDs ) { |
||
728 | |||
729 | /** |
||
730 | * Extract all requested fields from the row received from the database |
||
731 | * @param stdClass $row Result row |
||
732 | */ |
||
733 | public function processDbRow( $row ) { |
||
753 | |||
754 | /** |
||
755 | * This method populates internal variables with page information |
||
756 | * based on the given array of title strings. |
||
757 | * |
||
758 | * Steps: |
||
759 | * #1 For each title, get data from `page` table |
||
760 | * #2 If page was not found in the DB, store it as missing |
||
761 | * |
||
762 | * Additionally, when resolving redirects: |
||
763 | * #3 If no more redirects left, stop. |
||
764 | * #4 For each redirect, get its target from the `redirect` table. |
||
765 | * #5 Substitute the original LinkBatch object with the new list |
||
766 | * #6 Repeat from step #1 |
||
767 | * |
||
768 | * @param array $titles Array of Title objects or strings |
||
769 | */ |
||
770 | private function initFromTitles( $titles ) { |
||
790 | |||
791 | /** |
||
792 | * Does the same as initFromTitles(), but is based on page IDs instead |
||
793 | * @param array $pageids Array of page IDs |
||
794 | */ |
||
795 | private function initFromPageIds( $pageids ) { |
||
822 | |||
823 | /** |
||
824 | * Iterate through the result of the query on 'page' table, |
||
825 | * and for each row create and store title object and save any extra fields requested. |
||
826 | * @param ResultWrapper $res DB Query result |
||
827 | * @param array $remaining Array of either pageID or ns/title elements (optional). |
||
828 | * If given, any missing items will go to $mMissingPageIDs and $mMissingTitles |
||
829 | * @param bool $processTitles Must be provided together with $remaining. |
||
830 | * If true, treat $remaining as an array of [ns][title] |
||
831 | * If false, treat it as an array of [pageIDs] |
||
832 | */ |
||
833 | private function initFromQueryResult( $res, &$remaining = null, $processTitles = null ) { |
||
896 | |||
897 | /** |
||
898 | * Does the same as initFromTitles(), but is based on revision IDs |
||
899 | * instead |
||
900 | * @param array $revids Array of revision IDs |
||
901 | */ |
||
902 | private function initFromRevIDs( $revids ) { |
||
979 | |||
980 | /** |
||
981 | * Resolve any redirects in the result if redirect resolution was |
||
982 | * requested. This function is called repeatedly until all redirects |
||
983 | * have been resolved. |
||
984 | */ |
||
985 | private function resolvePendingRedirects() { |
||
1014 | |||
1015 | /** |
||
1016 | * Get the targets of the pending redirects from the database |
||
1017 | * |
||
1018 | * Also creates entries in the redirect table for redirects that don't |
||
1019 | * have one. |
||
1020 | * @return LinkBatch |
||
1021 | */ |
||
1022 | private function getRedirectTargets() { |
||
1076 | |||
1077 | /** |
||
1078 | * Get the cache mode for the data generated by this module. |
||
1079 | * All PageSet users should take into account whether this returns a more-restrictive |
||
1080 | * cache mode than the using module itself. For possible return values and other |
||
1081 | * details about cache modes, see ApiMain::setCacheMode() |
||
1082 | * |
||
1083 | * Public caching will only be allowed if *all* the modules that supply |
||
1084 | * data for a given request return a cache mode of public. |
||
1085 | * |
||
1086 | * @param array|null $params |
||
1087 | * @return string |
||
1088 | * @since 1.21 |
||
1089 | */ |
||
1090 | public function getCacheMode( $params = null ) { |
||
1093 | |||
1094 | /** |
||
1095 | * Given an array of title strings, convert them into Title objects. |
||
1096 | * Alternatively, an array of Title objects may be given. |
||
1097 | * This method validates access rights for the title, |
||
1098 | * and appends normalization values to the output. |
||
1099 | * |
||
1100 | * @param array $titles Array of Title objects or strings |
||
1101 | * @return LinkBatch |
||
1102 | */ |
||
1103 | private function processTitlesArray( $titles ) { |
||
1180 | |||
1181 | /** |
||
1182 | * Set data for a title. |
||
1183 | * |
||
1184 | * This data may be extracted into an ApiResult using |
||
1185 | * self::populateGeneratorData. This should generally be limited to |
||
1186 | * data that is likely to be particularly useful to end users rather than |
||
1187 | * just being a dump of everything returned in non-generator mode. |
||
1188 | * |
||
1189 | * Redirects here will *not* be followed, even if 'redirects' was |
||
1190 | * specified, since in the case of multiple redirects we can't know which |
||
1191 | * source's data to use on the target. |
||
1192 | * |
||
1193 | * @param Title $title |
||
1194 | * @param array $data |
||
1195 | */ |
||
1196 | public function setGeneratorData( Title $title, array $data ) { |
||
1201 | |||
1202 | /** |
||
1203 | * Controls how generator data about a redirect source is merged into |
||
1204 | * the generator data for the redirect target. When not set no data |
||
1205 | * is merged. Note that if multiple titles redirect to the same target |
||
1206 | * the order of operations is undefined. |
||
1207 | * |
||
1208 | * Example to include generated data from redirect in target, prefering |
||
1209 | * the data generated for the destination when there is a collision: |
||
1210 | * @code |
||
1211 | * $pageSet->setRedirectMergePolicy( function( array $current, array $new ) { |
||
1212 | * return $current + $new; |
||
1213 | * } ); |
||
1214 | * @endcode |
||
1215 | * |
||
1216 | * @param callable|null $callable Recieves two array arguments, first the |
||
1217 | * generator data for the redirect target and second the generator data |
||
1218 | * for the redirect source. Returns the resulting generator data to use |
||
1219 | * for the redirect target. |
||
1220 | */ |
||
1221 | public function setRedirectMergePolicy( $callable ) { |
||
1224 | |||
1225 | /** |
||
1226 | * Populate the generator data for all titles in the result |
||
1227 | * |
||
1228 | * The page data may be inserted into an ApiResult object or into an |
||
1229 | * associative array. The $path parameter specifies the path within the |
||
1230 | * ApiResult or array to find the "pages" node. |
||
1231 | * |
||
1232 | * The "pages" node itself must be an associative array mapping the page ID |
||
1233 | * or fake page ID values returned by this pageset (see |
||
1234 | * self::getAllTitlesByNamespace() and self::getSpecialTitles()) to |
||
1235 | * associative arrays of page data. Each of those subarrays will have the |
||
1236 | * data from self::setGeneratorData() merged in. |
||
1237 | * |
||
1238 | * Data that was set by self::setGeneratorData() for pages not in the |
||
1239 | * "pages" node will be ignored. |
||
1240 | * |
||
1241 | * @param ApiResult|array &$result |
||
1242 | * @param array $path |
||
1243 | * @return bool Whether the data fit |
||
1244 | */ |
||
1245 | public function populateGeneratorData( &$result, array $path = [] ) { |
||
1330 | |||
1331 | /** |
||
1332 | * Get the database connection (read-only) |
||
1333 | * @return DatabaseBase |
||
1334 | */ |
||
1335 | protected function getDB() { |
||
1338 | |||
1339 | /** |
||
1340 | * Returns the input array of integers with all values < 0 removed |
||
1341 | * |
||
1342 | * @param array $array |
||
1343 | * @return array |
||
1344 | */ |
||
1345 | private static function getPositiveIntegers( $array ) { |
||
1357 | |||
1358 | public function getAllowedParams( $flags = 0 ) { |
||
1409 | |||
1410 | protected function handleParamNormalization( $paramName, $value, $rawValue ) { |
||
1426 | |||
1427 | private static $generators = null; |
||
1428 | |||
1429 | /** |
||
1430 | * Get an array of all available generators |
||
1431 | * @return array |
||
1432 | */ |
||
1433 | private function getGenerators() { |
||
1455 | } |
||
1456 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: