Conditions | 16 |
Paths | 352 |
Total Lines | 106 |
Code Lines | 70 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
39 | public function execute() { |
||
40 | $main = $this->getMain(); |
||
41 | if ( !$main->isInternalMode() && !$main->getRequest()->wasPosted() ) { |
||
42 | $this->logFeatureUsage( 'purge-via-GET' ); |
||
43 | $this->setWarning( 'Use of action=purge via GET is deprecated. Use POST instead.' ); |
||
44 | } |
||
45 | |||
46 | $params = $this->extractRequestParams(); |
||
47 | |||
48 | $continuationManager = new ApiContinuationManager( $this, [], [] ); |
||
49 | $this->setContinuationManager( $continuationManager ); |
||
50 | |||
51 | $forceLinkUpdate = $params['forcelinkupdate']; |
||
52 | $forceRecursiveLinkUpdate = $params['forcerecursivelinkupdate']; |
||
53 | $pageSet = $this->getPageSet(); |
||
54 | $pageSet->execute(); |
||
55 | |||
56 | $result = $pageSet->getInvalidTitlesAndRevisions(); |
||
57 | $user = $this->getUser(); |
||
58 | |||
59 | foreach ( $pageSet->getGoodTitles() as $title ) { |
||
60 | $r = []; |
||
61 | ApiQueryBase::addTitleInfo( $r, $title ); |
||
62 | $page = WikiPage::factory( $title ); |
||
63 | if ( !$user->pingLimiter( 'purge' ) ) { |
||
64 | $flags = WikiPage::PURGE_ALL; |
||
65 | if ( !$this->getRequest()->wasPosted() ) { |
||
66 | $flags ^= WikiPage::PURGE_GLOBAL_PCACHE; // skip DB_MASTER write |
||
67 | } |
||
68 | // Directly purge and skip the UI part of purge() |
||
69 | $page->doPurge( $flags ); |
||
70 | $r['purged'] = true; |
||
71 | } else { |
||
72 | $error = $this->parseMsg( [ 'actionthrottledtext' ] ); |
||
73 | $this->setWarning( $error['info'] ); |
||
74 | } |
||
75 | |||
76 | if ( $forceLinkUpdate || $forceRecursiveLinkUpdate ) { |
||
77 | if ( !$user->pingLimiter( 'linkpurge' ) ) { |
||
78 | $popts = $page->makeParserOptions( 'canonical' ); |
||
79 | |||
80 | # Parse content; note that HTML generation is only needed if we want to cache the result. |
||
81 | $content = $page->getContent( Revision::RAW ); |
||
82 | if ( $content ) { |
||
83 | $enableParserCache = $this->getConfig()->get( 'EnableParserCache' ); |
||
84 | $p_result = $content->getParserOutput( |
||
85 | $title, |
||
86 | $page->getLatest(), |
||
87 | $popts, |
||
88 | $enableParserCache |
||
89 | ); |
||
90 | |||
91 | # Logging to better see expensive usage patterns |
||
92 | if ( $forceRecursiveLinkUpdate ) { |
||
93 | LoggerFactory::getInstance( 'RecursiveLinkPurge' )->info( |
||
94 | "Recursive link purge enqueued for {title}", |
||
95 | [ |
||
96 | 'user' => $this->getUser()->getName(), |
||
97 | 'title' => $title->getPrefixedText() |
||
98 | ] |
||
99 | ); |
||
100 | } |
||
101 | |||
102 | # Update the links tables |
||
103 | $updates = $content->getSecondaryDataUpdates( |
||
104 | $title, null, $forceRecursiveLinkUpdate, $p_result ); |
||
105 | foreach ( $updates as $update ) { |
||
106 | DeferredUpdates::addUpdate( $update, DeferredUpdates::PRESEND ); |
||
107 | } |
||
108 | |||
109 | $r['linkupdate'] = true; |
||
110 | |||
111 | if ( $enableParserCache ) { |
||
112 | $pcache = ParserCache::singleton(); |
||
113 | $pcache->save( $p_result, $page, $popts ); |
||
|
|||
114 | } |
||
115 | } |
||
116 | } else { |
||
117 | $error = $this->parseMsg( [ 'actionthrottledtext' ] ); |
||
118 | $this->setWarning( $error['info'] ); |
||
119 | $forceLinkUpdate = false; |
||
120 | } |
||
121 | } |
||
122 | |||
123 | $result[] = $r; |
||
124 | } |
||
125 | $apiResult = $this->getResult(); |
||
126 | ApiResult::setIndexedTagName( $result, 'page' ); |
||
127 | $apiResult->addValue( null, $this->getModuleName(), $result ); |
||
128 | |||
129 | $values = $pageSet->getNormalizedTitlesAsResult( $apiResult ); |
||
130 | if ( $values ) { |
||
131 | $apiResult->addValue( null, 'normalized', $values ); |
||
132 | } |
||
133 | $values = $pageSet->getConvertedTitlesAsResult( $apiResult ); |
||
134 | if ( $values ) { |
||
135 | $apiResult->addValue( null, 'converted', $values ); |
||
136 | } |
||
137 | $values = $pageSet->getRedirectTitlesAsResult( $apiResult ); |
||
138 | if ( $values ) { |
||
139 | $apiResult->addValue( null, 'redirects', $values ); |
||
140 | } |
||
141 | |||
142 | $this->setContinuationManager( null ); |
||
143 | $continuationManager->setContinuationIntoResult( $apiResult ); |
||
144 | } |
||
145 | |||
207 |
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: