Conditions | 9 |
Paths | 36 |
Total Lines | 136 |
Code Lines | 89 |
Lines | 10 |
Ratio | 7.35 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
57 | public function doUpdate() { |
||
58 | $services = MediaWikiServices::getInstance(); |
||
59 | $config = $services->getMainConfig(); |
||
60 | $lbFactory = $services->getDBLoadBalancerFactory(); |
||
61 | $batchSize = $config->get( 'UpdateRowsPerQuery' ); |
||
62 | |||
63 | // Page may already be deleted, so don't just getId() |
||
64 | $id = $this->pageId; |
||
65 | // Make sure all links update threads see the changes of each other. |
||
66 | // This handles the case when updates have to batched into several COMMITs. |
||
67 | $scopedLock = LinksUpdate::acquirePageLock( $this->getDB(), $id ); |
||
68 | |||
69 | $title = $this->page->getTitle(); |
||
70 | $dbw = $this->getDB(); // convenience |
||
71 | |||
72 | // Delete restrictions for it |
||
73 | $dbw->delete( 'page_restrictions', [ 'pr_page' => $id ], __METHOD__ ); |
||
74 | |||
75 | // Fix category table counts |
||
76 | $cats = $dbw->selectFieldValues( |
||
77 | 'categorylinks', |
||
78 | 'cl_to', |
||
79 | [ 'cl_from' => $id ], |
||
80 | __METHOD__ |
||
81 | ); |
||
82 | $catBatches = array_chunk( $cats, $batchSize ); |
||
83 | foreach ( $catBatches as $catBatch ) { |
||
84 | $this->page->updateCategoryCounts( [], $catBatch, $id ); |
||
85 | View Code Duplication | if ( count( $catBatches ) > 1 ) { |
|
86 | $lbFactory->commitAndWaitForReplication( |
||
87 | __METHOD__, $this->ticket, [ 'wiki' => $dbw->getWikiID() ] |
||
88 | ); |
||
89 | } |
||
90 | } |
||
91 | |||
92 | // Refresh the category table entry if it seems to have no pages. Check |
||
93 | // master for the most up-to-date cat_pages count. |
||
94 | if ( $title->getNamespace() === NS_CATEGORY ) { |
||
95 | $row = $dbw->selectRow( |
||
96 | 'category', |
||
97 | [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ], |
||
98 | [ 'cat_title' => $title->getDBkey(), 'cat_pages <= 0' ], |
||
99 | __METHOD__ |
||
100 | ); |
||
101 | if ( $row ) { |
||
102 | Category::newFromRow( $row, $title )->refreshCounts(); |
||
103 | } |
||
104 | } |
||
105 | |||
106 | // If using cascading deletes, we can skip some explicit deletes |
||
107 | if ( !$dbw->cascadingDeletes() ) { |
||
108 | // Delete outgoing links |
||
109 | $this->batchDeleteByPK( |
||
110 | 'pagelinks', |
||
111 | [ 'pl_from' => $id ], |
||
112 | [ 'pl_from', 'pl_namespace', 'pl_title' ], |
||
113 | $batchSize |
||
114 | ); |
||
115 | $this->batchDeleteByPK( |
||
116 | 'imagelinks', |
||
117 | [ 'il_from' => $id ], |
||
118 | [ 'il_from', 'il_to' ], |
||
119 | $batchSize |
||
120 | ); |
||
121 | $this->batchDeleteByPK( |
||
122 | 'categorylinks', |
||
123 | [ 'cl_from' => $id ], |
||
124 | [ 'cl_from', 'cl_to' ], |
||
125 | $batchSize |
||
126 | ); |
||
127 | $this->batchDeleteByPK( |
||
128 | 'templatelinks', |
||
129 | [ 'tl_from' => $id ], |
||
130 | [ 'tl_from', 'tl_namespace', 'tl_title' ], |
||
131 | $batchSize |
||
132 | ); |
||
133 | $this->batchDeleteByPK( |
||
134 | 'externallinks', |
||
135 | [ 'el_from' => $id ], |
||
136 | [ 'el_id' ], |
||
137 | $batchSize |
||
138 | ); |
||
139 | $this->batchDeleteByPK( |
||
140 | 'langlinks', |
||
141 | [ 'll_from' => $id ], |
||
142 | [ 'll_from', 'll_lang' ], |
||
143 | $batchSize |
||
144 | ); |
||
145 | $this->batchDeleteByPK( |
||
146 | 'iwlinks', |
||
147 | [ 'iwl_from' => $id ], |
||
148 | [ 'iwl_from', 'iwl_prefix', 'iwl_title' ], |
||
149 | $batchSize |
||
150 | ); |
||
151 | // Delete any redirect entry or page props entries |
||
152 | $dbw->delete( 'redirect', [ 'rd_from' => $id ], __METHOD__ ); |
||
153 | $dbw->delete( 'page_props', [ 'pp_page' => $id ], __METHOD__ ); |
||
154 | } |
||
155 | |||
156 | // If using cleanup triggers, we can skip some manual deletes |
||
157 | if ( !$dbw->cleanupTriggers() ) { |
||
158 | // Find recentchanges entries to clean up... |
||
159 | $rcIdsForTitle = $dbw->selectFieldValues( |
||
160 | 'recentchanges', |
||
161 | 'rc_id', |
||
162 | [ |
||
163 | 'rc_type != ' . RC_LOG, |
||
164 | 'rc_namespace' => $title->getNamespace(), |
||
165 | 'rc_title' => $title->getDBkey(), |
||
166 | 'rc_timestamp < ' . |
||
167 | $dbw->addQuotes( $dbw->timestamp( $this->timestamp ) ) |
||
168 | ], |
||
169 | __METHOD__ |
||
170 | ); |
||
171 | $rcIdsForPage = $dbw->selectFieldValues( |
||
172 | 'recentchanges', |
||
173 | 'rc_id', |
||
174 | [ 'rc_type != ' . RC_LOG, 'rc_cur_id' => $id ], |
||
175 | __METHOD__ |
||
176 | ); |
||
177 | |||
178 | // T98706: delete by PK to avoid lock contention with RC delete log insertions |
||
179 | $rcIdBatches = array_chunk( array_merge( $rcIdsForTitle, $rcIdsForPage ), $batchSize ); |
||
180 | foreach ( $rcIdBatches as $rcIdBatch ) { |
||
181 | $dbw->delete( 'recentchanges', [ 'rc_id' => $rcIdBatch ], __METHOD__ ); |
||
182 | View Code Duplication | if ( count( $rcIdBatches ) > 1 ) { |
|
183 | $lbFactory->commitAndWaitForReplication( |
||
184 | __METHOD__, $this->ticket, [ 'wiki' => $dbw->getWikiID() ] |
||
185 | ); |
||
186 | } |
||
187 | } |
||
188 | } |
||
189 | |||
190 | // Commit and release the lock |
||
191 | ScopedCallback::consume( $scopedLock ); |
||
192 | } |
||
193 | |||
238 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: