| Conditions | 8 |
| Paths | 54 |
| Total Lines | 133 |
| Code Lines | 88 |
| Lines | 10 |
| Ratio | 7.52 % |
| 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 |
||
| 59 | public function doUpdate() { |
||
| 60 | $services = MediaWikiServices::getInstance(); |
||
| 61 | $config = $services->getMainConfig(); |
||
| 62 | $lbFactory = $services->getDBLoadBalancerFactory(); |
||
| 63 | $batchSize = $config->get( 'UpdateRowsPerQuery' ); |
||
| 64 | |||
| 65 | // Page may already be deleted, so don't just getId() |
||
| 66 | $id = $this->pageId; |
||
| 67 | |||
| 68 | if ( $this->ticket ) { |
||
| 69 | // Make sure all links update threads see the changes of each other. |
||
| 70 | // This handles the case when updates have to batched into several COMMITs. |
||
| 71 | $scopedLock = LinksUpdate::acquirePageLock( $this->getDB(), $id ); |
||
| 72 | } |
||
| 73 | |||
| 74 | $title = $this->page->getTitle(); |
||
| 75 | $dbw = $this->getDB(); // convenience |
||
| 76 | |||
| 77 | // Delete restrictions for it |
||
| 78 | $dbw->delete( 'page_restrictions', [ 'pr_page' => $id ], __METHOD__ ); |
||
| 79 | |||
| 80 | // Fix category table counts |
||
| 81 | $cats = $dbw->selectFieldValues( |
||
| 82 | 'categorylinks', |
||
| 83 | 'cl_to', |
||
| 84 | [ 'cl_from' => $id ], |
||
| 85 | __METHOD__ |
||
| 86 | ); |
||
| 87 | $catBatches = array_chunk( $cats, $batchSize ); |
||
| 88 | foreach ( $catBatches as $catBatch ) { |
||
| 89 | $this->page->updateCategoryCounts( [], $catBatch, $id ); |
||
| 90 | View Code Duplication | if ( count( $catBatches ) > 1 ) { |
|
| 91 | $lbFactory->commitAndWaitForReplication( |
||
| 92 | __METHOD__, $this->ticket, [ 'wiki' => $dbw->getWikiID() ] |
||
| 93 | ); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | // Refresh the category table entry if it seems to have no pages. Check |
||
| 98 | // master for the most up-to-date cat_pages count. |
||
| 99 | if ( $title->getNamespace() === NS_CATEGORY ) { |
||
| 100 | $row = $dbw->selectRow( |
||
| 101 | 'category', |
||
| 102 | [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ], |
||
| 103 | [ 'cat_title' => $title->getDBkey(), 'cat_pages <= 0' ], |
||
| 104 | __METHOD__ |
||
| 105 | ); |
||
| 106 | if ( $row ) { |
||
| 107 | Category::newFromRow( $row, $title )->refreshCounts(); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | $this->batchDeleteByPK( |
||
| 112 | 'pagelinks', |
||
| 113 | [ 'pl_from' => $id ], |
||
| 114 | [ 'pl_from', 'pl_namespace', 'pl_title' ], |
||
| 115 | $batchSize |
||
| 116 | ); |
||
| 117 | $this->batchDeleteByPK( |
||
| 118 | 'imagelinks', |
||
| 119 | [ 'il_from' => $id ], |
||
| 120 | [ 'il_from', 'il_to' ], |
||
| 121 | $batchSize |
||
| 122 | ); |
||
| 123 | $this->batchDeleteByPK( |
||
| 124 | 'categorylinks', |
||
| 125 | [ 'cl_from' => $id ], |
||
| 126 | [ 'cl_from', 'cl_to' ], |
||
| 127 | $batchSize |
||
| 128 | ); |
||
| 129 | $this->batchDeleteByPK( |
||
| 130 | 'templatelinks', |
||
| 131 | [ 'tl_from' => $id ], |
||
| 132 | [ 'tl_from', 'tl_namespace', 'tl_title' ], |
||
| 133 | $batchSize |
||
| 134 | ); |
||
| 135 | $this->batchDeleteByPK( |
||
| 136 | 'externallinks', |
||
| 137 | [ 'el_from' => $id ], |
||
| 138 | [ 'el_id' ], |
||
| 139 | $batchSize |
||
| 140 | ); |
||
| 141 | $this->batchDeleteByPK( |
||
| 142 | 'langlinks', |
||
| 143 | [ 'll_from' => $id ], |
||
| 144 | [ 'll_from', 'll_lang' ], |
||
| 145 | $batchSize |
||
| 146 | ); |
||
| 147 | $this->batchDeleteByPK( |
||
| 148 | 'iwlinks', |
||
| 149 | [ 'iwl_from' => $id ], |
||
| 150 | [ 'iwl_from', 'iwl_prefix', 'iwl_title' ], |
||
| 151 | $batchSize |
||
| 152 | ); |
||
| 153 | |||
| 154 | // Delete any redirect entry or page props entries |
||
| 155 | $dbw->delete( 'redirect', [ 'rd_from' => $id ], __METHOD__ ); |
||
| 156 | $dbw->delete( 'page_props', [ 'pp_page' => $id ], __METHOD__ ); |
||
| 157 | |||
| 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 | // Commit and release the lock (if set) |
||
| 190 | ScopedCallback::consume( $scopedLock ); |
||
| 191 | } |
||
| 192 | |||
| 237 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: