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 DifferenceEngine 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 DifferenceEngine, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class DifferenceEngine extends ContextSource { |
||
32 | /** |
||
33 | * Constant to indicate diff cache compatibility. |
||
34 | * Bump this when changing the diff formatting in a way that |
||
35 | * fixes important bugs or such to force cached diff views to |
||
36 | * clear. |
||
37 | */ |
||
38 | const DIFF_VERSION = MW_DIFF_VERSION; |
||
|
|||
39 | |||
40 | /** @var int */ |
||
41 | public $mOldid; |
||
42 | |||
43 | /** @var int */ |
||
44 | public $mNewid; |
||
45 | |||
46 | private $mOldTags; |
||
47 | private $mNewTags; |
||
48 | |||
49 | /** @var Content */ |
||
50 | public $mOldContent; |
||
51 | |||
52 | /** @var Content */ |
||
53 | public $mNewContent; |
||
54 | |||
55 | /** @var Language */ |
||
56 | protected $mDiffLang; |
||
57 | |||
58 | /** @var Title */ |
||
59 | public $mOldPage; |
||
60 | |||
61 | /** @var Title */ |
||
62 | public $mNewPage; |
||
63 | |||
64 | /** @var Revision */ |
||
65 | public $mOldRev; |
||
66 | |||
67 | /** @var Revision */ |
||
68 | public $mNewRev; |
||
69 | |||
70 | /** @var bool Have the revisions IDs been loaded */ |
||
71 | private $mRevisionsIdsLoaded = false; |
||
72 | |||
73 | /** @var bool Have the revisions been loaded */ |
||
74 | public $mRevisionsLoaded = false; |
||
75 | |||
76 | /** @var int How many text blobs have been loaded, 0, 1 or 2? */ |
||
77 | public $mTextLoaded = 0; |
||
78 | |||
79 | /** @var bool Was the diff fetched from cache? */ |
||
80 | public $mCacheHit = false; |
||
81 | |||
82 | /** |
||
83 | * Set this to true to add debug info to the HTML output. |
||
84 | * Warning: this may cause RSS readers to spuriously mark articles as "new" |
||
85 | * (bug 20601) |
||
86 | */ |
||
87 | public $enableDebugComment = false; |
||
88 | |||
89 | /** @var bool If true, line X is not displayed when X is 1, for example |
||
90 | * to increase readability and conserve space with many small diffs. |
||
91 | */ |
||
92 | protected $mReducedLineNumbers = false; |
||
93 | |||
94 | /** @var string Link to action=markpatrolled */ |
||
95 | protected $mMarkPatrolledLink = null; |
||
96 | |||
97 | /** @var bool Show rev_deleted content if allowed */ |
||
98 | protected $unhide = false; |
||
99 | |||
100 | /** @var bool Refresh the diff cache */ |
||
101 | protected $mRefreshCache = false; |
||
102 | |||
103 | /**#@-*/ |
||
104 | |||
105 | /** |
||
106 | * Constructor |
||
107 | * @param IContextSource $context Context to use, anything else will be ignored |
||
108 | * @param int $old Old ID we want to show and diff with. |
||
109 | * @param string|int $new Either revision ID or 'prev' or 'next'. Default: 0. |
||
110 | * @param int $rcid Deprecated, no longer used! |
||
111 | * @param bool $refreshCache If set, refreshes the diff cache |
||
112 | * @param bool $unhide If set, allow viewing deleted revs |
||
113 | */ |
||
114 | public function __construct( $context = null, $old = 0, $new = 0, $rcid = 0, |
||
128 | |||
129 | /** |
||
130 | * @param bool $value |
||
131 | */ |
||
132 | public function setReducedLineNumbers( $value = true ) { |
||
135 | |||
136 | /** |
||
137 | * @return Language |
||
138 | */ |
||
139 | public function getDiffLang() { |
||
147 | |||
148 | /** |
||
149 | * @return bool |
||
150 | */ |
||
151 | public function wasCacheHit() { |
||
154 | |||
155 | /** |
||
156 | * @return int |
||
157 | */ |
||
158 | public function getOldid() { |
||
163 | |||
164 | /** |
||
165 | * @return bool|int |
||
166 | */ |
||
167 | public function getNewid() { |
||
172 | |||
173 | /** |
||
174 | * Look up a special:Undelete link to the given deleted revision id, |
||
175 | * as a workaround for being unable to load deleted diffs in currently. |
||
176 | * |
||
177 | * @param int $id Revision ID |
||
178 | * |
||
179 | * @return string|bool Link HTML or false |
||
180 | */ |
||
181 | public function deletedLink( $id ) { |
||
200 | |||
201 | /** |
||
202 | * Build a wikitext link toward a deleted revision, if viewable. |
||
203 | * |
||
204 | * @param int $id Revision ID |
||
205 | * |
||
206 | * @return string Wikitext fragment |
||
207 | */ |
||
208 | public function deletedIdMarker( $id ) { |
||
216 | |||
217 | private function showMissingRevision() { |
||
239 | |||
240 | public function showDiffPage( $diffOnly = false ) { |
||
474 | |||
475 | /** |
||
476 | * Build a link to mark a change as patrolled. |
||
477 | * |
||
478 | * Returns empty string if there's either no revision to patrol or the user is not allowed to. |
||
479 | * Side effect: When the patrol link is build, this method will call |
||
480 | * OutputPage::preventClickjacking() and load mediawiki.page.patrol.ajax. |
||
481 | * |
||
482 | * @return string HTML or empty string |
||
483 | */ |
||
484 | protected function markPatrolledLink() { |
||
485 | if ( $this->mMarkPatrolledLink === null ) { |
||
486 | $linkInfo = $this->getMarkPatrolledLinkInfo(); |
||
487 | // If false, there is no patrol link needed/allowed |
||
488 | if ( !$linkInfo ) { |
||
489 | $this->mMarkPatrolledLink = ''; |
||
490 | } else { |
||
491 | $this->mMarkPatrolledLink = ' <span class="patrollink" data-mw="interface">[' . |
||
492 | Linker::linkKnown( |
||
493 | $this->mNewPage, |
||
494 | $this->msg( 'markaspatrolleddiff' )->escaped(), |
||
495 | [], |
||
496 | [ |
||
497 | 'action' => 'markpatrolled', |
||
498 | 'rcid' => $linkInfo['rcid'], |
||
499 | ] |
||
500 | ) . ']</span>'; |
||
501 | // Allow extensions to change the markpatrolled link |
||
502 | Hooks::run( 'DifferenceEngineMarkPatrolledLink', [ $this, |
||
503 | &$this->mMarkPatrolledLink, $linkInfo['rcid'] ] ); |
||
504 | } |
||
505 | } |
||
506 | return $this->mMarkPatrolledLink; |
||
507 | } |
||
508 | |||
509 | /** |
||
510 | * Returns an array of meta data needed to build a "mark as patrolled" link and |
||
511 | * adds the mediawiki.page.patrol.ajax to the output. |
||
512 | * |
||
513 | * @return array|false An array of meta data for a patrol link (rcid only) |
||
514 | * or false if no link is needed |
||
515 | */ |
||
516 | protected function getMarkPatrolledLinkInfo() { |
||
517 | global $wgUseRCPatrol, $wgEnableAPI, $wgEnableWriteAPI; |
||
518 | |||
519 | $user = $this->getUser(); |
||
520 | |||
521 | // Prepare a change patrol link, if applicable |
||
522 | if ( |
||
523 | // Is patrolling enabled and the user allowed to? |
||
524 | $wgUseRCPatrol && $this->mNewPage->quickUserCan( 'patrol', $user ) && |
||
525 | // Only do this if the revision isn't more than 6 hours older |
||
526 | // than the Max RC age (6h because the RC might not be cleaned out regularly) |
||
527 | RecentChange::isInRCLifespan( $this->mNewRev->getTimestamp(), 21600 ) |
||
528 | ) { |
||
529 | // Look for an unpatrolled change corresponding to this diff |
||
530 | $db = wfGetDB( DB_REPLICA ); |
||
531 | $change = RecentChange::newFromConds( |
||
532 | [ |
||
533 | 'rc_timestamp' => $db->timestamp( $this->mNewRev->getTimestamp() ), |
||
534 | 'rc_this_oldid' => $this->mNewid, |
||
535 | 'rc_patrolled' => 0 |
||
536 | ], |
||
537 | __METHOD__ |
||
538 | ); |
||
539 | |||
540 | if ( $change && !$change->getPerformer()->equals( $user ) ) { |
||
541 | $rcid = $change->getAttribute( 'rc_id' ); |
||
542 | } else { |
||
543 | // None found or the page has been created by the current user. |
||
544 | // If the user could patrol this it already would be patrolled |
||
545 | $rcid = 0; |
||
546 | } |
||
547 | |||
548 | // Allow extensions to possibly change the rcid here |
||
549 | // For example the rcid might be set to zero due to the user |
||
550 | // being the same as the performer of the change but an extension |
||
551 | // might still want to show it under certain conditions |
||
552 | Hooks::run( 'DifferenceEngineMarkPatrolledRCID', [ &$rcid, $this, $change, $user ] ); |
||
553 | |||
554 | // Build the link |
||
555 | if ( $rcid ) { |
||
556 | $this->getOutput()->preventClickjacking(); |
||
557 | if ( $wgEnableAPI && $wgEnableWriteAPI |
||
558 | && $user->isAllowed( 'writeapi' ) |
||
559 | ) { |
||
560 | $this->getOutput()->addModules( 'mediawiki.page.patrol.ajax' ); |
||
561 | } |
||
562 | |||
563 | return [ |
||
564 | 'rcid' => $rcid, |
||
565 | ]; |
||
566 | } |
||
567 | } |
||
568 | |||
569 | // No mark as patrolled link applicable |
||
570 | return false; |
||
571 | } |
||
572 | |||
573 | /** |
||
574 | * @param Revision $rev |
||
575 | * |
||
576 | * @return string |
||
577 | */ |
||
578 | protected function revisionDeleteLink( $rev ) { |
||
579 | $link = Linker::getRevDeleteLink( $this->getUser(), $rev, $rev->getTitle() ); |
||
580 | if ( $link !== '' ) { |
||
581 | $link = '   ' . $link . ' '; |
||
582 | } |
||
583 | |||
584 | return $link; |
||
585 | } |
||
586 | |||
587 | /** |
||
588 | * Show the new revision of the page. |
||
589 | */ |
||
590 | public function renderNewRevision() { |
||
591 | $out = $this->getOutput(); |
||
592 | $revHeader = $this->getRevisionHeader( $this->mNewRev ); |
||
593 | # Add "current version as of X" title |
||
594 | $out->addHTML( "<hr class='diff-hr' id='mw-oldid' /> |
||
595 | <h2 class='diff-currentversion-title'>{$revHeader}</h2>\n" ); |
||
596 | # Page content may be handled by a hooked call instead... |
||
597 | # @codingStandardsIgnoreStart Ignoring long lines. |
||
598 | if ( Hooks::run( 'ArticleContentOnDiff', [ $this, $out ] ) ) { |
||
599 | $this->loadNewText(); |
||
600 | $out->setRevisionId( $this->mNewid ); |
||
601 | $out->setRevisionTimestamp( $this->mNewRev->getTimestamp() ); |
||
602 | $out->setArticleFlag( true ); |
||
603 | |||
604 | // NOTE: only needed for B/C: custom rendering of JS/CSS via hook |
||
605 | if ( $this->mNewPage->isCssJsSubpage() || $this->mNewPage->isCssOrJsPage() ) { |
||
606 | // This needs to be synchronised with Article::showCssOrJsPage(), which sucks |
||
607 | // Give hooks a chance to customise the output |
||
608 | // @todo standardize this crap into one function |
||
609 | if ( ContentHandler::runLegacyHooks( 'ShowRawCssJs', [ $this->mNewContent, $this->mNewPage, $out ], '1.24' ) ) { |
||
610 | // NOTE: deprecated hook, B/C only |
||
611 | // use the content object's own rendering |
||
612 | $cnt = $this->mNewRev->getContent(); |
||
613 | $po = $cnt ? $cnt->getParserOutput( $this->mNewRev->getTitle(), $this->mNewRev->getId() ) : null; |
||
614 | if ( $po ) { |
||
615 | $out->addParserOutputContent( $po ); |
||
616 | } |
||
617 | } |
||
618 | } elseif ( !Hooks::run( 'ArticleContentViewCustom', [ $this->mNewContent, $this->mNewPage, $out ] ) ) { |
||
619 | // Handled by extension |
||
620 | } elseif ( !ContentHandler::runLegacyHooks( |
||
621 | 'ArticleViewCustom', |
||
622 | [ $this->mNewContent, $this->mNewPage, $out ], |
||
623 | '1.21' |
||
624 | ) ) { |
||
625 | // NOTE: deprecated hook, B/C only |
||
626 | // Handled by extension |
||
627 | } else { |
||
628 | // Normal page |
||
629 | if ( $this->getTitle()->equals( $this->mNewPage ) ) { |
||
630 | // If the Title stored in the context is the same as the one |
||
631 | // of the new revision, we can use its associated WikiPage |
||
632 | // object. |
||
633 | $wikiPage = $this->getWikiPage(); |
||
634 | } else { |
||
635 | // Otherwise we need to create our own WikiPage object |
||
636 | $wikiPage = WikiPage::factory( $this->mNewPage ); |
||
637 | } |
||
638 | |||
639 | $parserOutput = $this->getParserOutput( $wikiPage, $this->mNewRev ); |
||
640 | |||
641 | # WikiPage::getParserOutput() should not return false, but just in case |
||
642 | if ( $parserOutput ) { |
||
643 | // Allow extensions to change parser output here |
||
644 | if ( Hooks::run( 'DifferenceEngineRenderRevisionAddParserOutput', [ $this, $out, $parserOutput, $wikiPage ] ) ) { |
||
645 | $out->addParserOutput( $parserOutput ); |
||
646 | } |
||
647 | } |
||
648 | } |
||
649 | } |
||
650 | # @codingStandardsIgnoreEnd |
||
651 | |||
652 | // Allow extensions to optionally not show the final patrolled link |
||
653 | if ( Hooks::run( 'DifferenceEngineRenderRevisionShowFinalPatrolLink' ) ) { |
||
654 | # Add redundant patrol link on bottom... |
||
655 | $out->addHTML( $this->markPatrolledLink() ); |
||
656 | } |
||
657 | } |
||
658 | |||
659 | protected function getParserOutput( WikiPage $page, Revision $rev ) { |
||
670 | |||
671 | /** |
||
672 | * Get the diff text, send it to the OutputPage object |
||
673 | * Returns false if the diff could not be generated, otherwise returns true |
||
674 | * |
||
675 | * @param string|bool $otitle Header for old text or false |
||
676 | * @param string|bool $ntitle Header for new text or false |
||
677 | * @param string $notice HTML between diff header and body |
||
678 | * |
||
679 | * @return bool |
||
680 | */ |
||
681 | public function showDiff( $otitle, $ntitle, $notice = '' ) { |
||
697 | |||
698 | /** |
||
699 | * Add style sheets for diff display. |
||
700 | */ |
||
701 | public function showDiffStyle() { |
||
704 | |||
705 | /** |
||
706 | * Get complete diff table, including header |
||
707 | * |
||
708 | * @param string|bool $otitle Header for old text or false |
||
709 | * @param string|bool $ntitle Header for new text or false |
||
710 | * @param string $notice HTML between diff header and body |
||
711 | * |
||
712 | * @return mixed |
||
713 | */ |
||
714 | public function getDiff( $otitle, $ntitle, $notice = '' ) { |
||
730 | |||
731 | /** |
||
732 | * Get the diff table body, without header |
||
733 | * |
||
734 | * @return mixed (string/false) |
||
735 | */ |
||
736 | public function getDiffBody() { |
||
801 | |||
802 | /** |
||
803 | * Returns the cache key for diff body text or content. |
||
804 | * |
||
805 | * @since 1.23 |
||
806 | * |
||
807 | * @throws MWException |
||
808 | * @return string |
||
809 | */ |
||
810 | protected function getDiffBodyCacheKey() { |
||
818 | |||
819 | /** |
||
820 | * Generate a diff, no caching. |
||
821 | * |
||
822 | * This implementation uses generateTextDiffBody() to generate a diff based on the default |
||
823 | * serialization of the given Content objects. This will fail if $old or $new are not |
||
824 | * instances of TextContent. |
||
825 | * |
||
826 | * Subclasses may override this to provide a different rendering for the diff, |
||
827 | * perhaps taking advantage of the content's native form. This is required for all content |
||
828 | * models that are not text based. |
||
829 | * |
||
830 | * @since 1.21 |
||
831 | * |
||
832 | * @param Content $old Old content |
||
833 | * @param Content $new New content |
||
834 | * |
||
835 | * @throws MWException If old or new content is not an instance of TextContent. |
||
836 | * @return bool|string |
||
837 | */ |
||
838 | public function generateContentDiffBody( Content $old, Content $new ) { |
||
854 | |||
855 | /** |
||
856 | * Generate a diff, no caching |
||
857 | * |
||
858 | * @todo move this to TextDifferenceEngine, make DifferenceEngine abstract. At some point. |
||
859 | * |
||
860 | * @param string $otext Old text, must be already segmented |
||
861 | * @param string $ntext New text, must be already segmented |
||
862 | * |
||
863 | * @return bool|string |
||
864 | */ |
||
865 | public function generateTextDiffBody( $otext, $ntext ) { |
||
901 | |||
902 | /** |
||
903 | * Generates diff, to be wrapped internally in a logging/instrumentation |
||
904 | * |
||
905 | * @param string $otext Old text, must be already segmented |
||
906 | * @param string $ntext New text, must be already segmented |
||
907 | * @return bool|string |
||
908 | */ |
||
909 | protected function textDiff( $otext, $ntext ) { |
||
970 | |||
971 | /** |
||
972 | * Generate a debug comment indicating diff generating time, |
||
973 | * server node, and generator backend. |
||
974 | * |
||
975 | * @param string $generator : What diff engine was used |
||
976 | * |
||
977 | * @return string |
||
978 | */ |
||
979 | protected function debug( $generator = "internal" ) { |
||
994 | |||
995 | /** |
||
996 | * Replace line numbers with the text in the user's language |
||
997 | * |
||
998 | * @param string $text |
||
999 | * |
||
1000 | * @return mixed |
||
1001 | */ |
||
1002 | public function localiseLineNumbers( $text ) { |
||
1009 | |||
1010 | public function localiseLineNumbersCb( $matches ) { |
||
1017 | |||
1018 | /** |
||
1019 | * If there are revisions between the ones being compared, return a note saying so. |
||
1020 | * |
||
1021 | * @return string |
||
1022 | */ |
||
1023 | public function getMultiNotice() { |
||
1056 | |||
1057 | /** |
||
1058 | * Get a notice about how many intermediate edits and users there are |
||
1059 | * |
||
1060 | * @param int $numEdits |
||
1061 | * @param int $numUsers |
||
1062 | * @param int $limit |
||
1063 | * |
||
1064 | * @return string |
||
1065 | */ |
||
1066 | public static function intermediateEditsMsg( $numEdits, $numUsers, $limit ) { |
||
1078 | |||
1079 | /** |
||
1080 | * Get a header for a specified revision. |
||
1081 | * |
||
1082 | * @param Revision $rev |
||
1083 | * @param string $complete 'complete' to get the header wrapped depending |
||
1084 | * the visibility of the revision and a link to edit the page. |
||
1085 | * |
||
1086 | * @return string HTML fragment |
||
1087 | */ |
||
1088 | protected function getRevisionHeader( Revision $rev, $complete = '' ) { |
||
1140 | |||
1141 | /** |
||
1142 | * Add the header to a diff body |
||
1143 | * |
||
1144 | * @param string $diff Diff body |
||
1145 | * @param string $otitle Old revision header |
||
1146 | * @param string $ntitle New revision header |
||
1147 | * @param string $multi Notice telling user that there are intermediate |
||
1148 | * revisions between the ones being compared |
||
1149 | * @param string $notice Other notices, e.g. that user is viewing deleted content |
||
1150 | * |
||
1151 | * @return string |
||
1152 | */ |
||
1153 | public function addHeader( $diff, $otitle, $ntitle, $multi = '', $notice = '' ) { |
||
1201 | |||
1202 | /** |
||
1203 | * Use specified text instead of loading from the database |
||
1204 | * @param Content $oldContent |
||
1205 | * @param Content $newContent |
||
1206 | * @since 1.21 |
||
1207 | */ |
||
1208 | public function setContent( Content $oldContent, Content $newContent ) { |
||
1215 | |||
1216 | /** |
||
1217 | * Set the language in which the diff text is written |
||
1218 | * (Defaults to page content language). |
||
1219 | * @param Language|string $lang |
||
1220 | * @since 1.19 |
||
1221 | */ |
||
1222 | public function setTextLanguage( $lang ) { |
||
1225 | |||
1226 | /** |
||
1227 | * Maps a revision pair definition as accepted by DifferenceEngine constructor |
||
1228 | * to a pair of actual integers representing revision ids. |
||
1229 | * |
||
1230 | * @param int $old Revision id, e.g. from URL parameter 'oldid' |
||
1231 | * @param int|string $new Revision id or strings 'next' or 'prev', e.g. from URL parameter 'diff' |
||
1232 | * |
||
1233 | * @return int[] List of two revision ids, older first, later second. |
||
1234 | * Zero signifies invalid argument passed. |
||
1235 | * false signifies that there is no previous/next revision ($old is the oldest/newest one). |
||
1236 | */ |
||
1237 | public function mapDiffPrevNext( $old, $new ) { |
||
1253 | |||
1254 | /** |
||
1255 | * Load revision IDs |
||
1256 | */ |
||
1257 | private function loadRevisionIds() { |
||
1279 | |||
1280 | /** |
||
1281 | * Load revision metadata for the specified articles. If newid is 0, then compare |
||
1282 | * the old article in oldid to the current article; if oldid is 0, then |
||
1283 | * compare the current article to the immediately previous one (ignoring the |
||
1284 | * value of newid). |
||
1285 | * |
||
1286 | * If oldid is false, leave the corresponding revision object set |
||
1287 | * to false. This is impossible via ordinary user input, and is provided for |
||
1288 | * API convenience. |
||
1289 | * |
||
1290 | * @return bool |
||
1291 | */ |
||
1292 | public function loadRevisionData() { |
||
1366 | |||
1367 | /** |
||
1368 | * Load the text of the revisions, as well as revision data. |
||
1369 | * |
||
1370 | * @return bool |
||
1371 | */ |
||
1372 | public function loadText() { |
||
1400 | |||
1401 | /** |
||
1402 | * Load the text of the new revision, not the old one |
||
1403 | * |
||
1404 | * @return bool |
||
1405 | */ |
||
1406 | public function loadNewText() { |
||
1421 | |||
1422 | } |
||
1423 |
This class constant has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.