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:
1 | <?php |
||
36 | class HTMLCacheUpdateJob extends Job { |
||
37 | function __construct( Title $title, array $params ) { |
||
42 | |||
43 | /** |
||
44 | * @param Title $title Title to purge backlink pages from |
||
45 | * @param string $table Backlink table name |
||
46 | * @return HTMLCacheUpdateJob |
||
47 | */ |
||
48 | public static function newForBacklinks( Title $title, $table ) { |
||
59 | |||
60 | function run() { |
||
92 | |||
93 | /** |
||
94 | * @param array $pages Map of (page ID => (namespace, DB key)) entries |
||
95 | */ |
||
96 | protected function invalidateTitles( array $pages ) { |
||
97 | global $wgUpdateRowsPerQuery, $wgUseFileCache; |
||
98 | |||
99 | // Get all page IDs in this query into an array |
||
100 | $pageIds = array_keys( $pages ); |
||
101 | if ( !$pageIds ) { |
||
102 | return; |
||
103 | } |
||
104 | |||
105 | // Bump page_touched to the current timestamp. This used to use the root job timestamp |
||
106 | // (e.g. template/file edit time), which was a bit more efficient when template edits are |
||
107 | // rare and don't effect the same pages much. However, this way allows for better |
||
108 | // de-duplication, which is much more useful for wikis with high edit rates. Note that |
||
109 | // RefreshLinksJob, which is enqueued alongside HTMLCacheUpdateJob, saves the parser output |
||
110 | // since it has to parse anyway. We assume that vast majority of the cache jobs finish |
||
111 | // before the link jobs, so using the current timestamp instead of the root timestamp is |
||
112 | // not expected to invalidate these cache entries too often. |
||
113 | $touchTimestamp = wfTimestampNow(); |
||
114 | |||
115 | $dbw = wfGetDB( DB_MASTER ); |
||
116 | $factory = wfGetLBFactory(); |
||
|
|||
117 | $ticket = $factory->getEmptyTransactionTicket( __METHOD__ ); |
||
118 | // Update page_touched (skipping pages already touched since the root job). |
||
119 | // Check $wgUpdateRowsPerQuery for sanity; batch jobs are sized by that already. |
||
120 | View Code Duplication | foreach ( array_chunk( $pageIds, $wgUpdateRowsPerQuery ) as $batch ) { |
|
121 | $factory->commitAndWaitForReplication( __METHOD__, $ticket ); |
||
122 | |||
123 | $dbw->update( 'page', |
||
124 | [ 'page_touched' => $dbw->timestamp( $touchTimestamp ) ], |
||
125 | [ 'page_id' => $batch, |
||
126 | // don't invalidated pages that were already invalidated |
||
127 | "page_touched < " . $dbw->addQuotes( $dbw->timestamp( $touchTimestamp ) ) |
||
128 | ], |
||
129 | __METHOD__ |
||
130 | ); |
||
131 | } |
||
132 | // Get the list of affected pages (races only mean something else did the purge) |
||
133 | $titleArray = TitleArray::newFromResult( $dbw->select( |
||
134 | 'page', |
||
135 | [ 'page_namespace', 'page_title' ], |
||
136 | [ 'page_id' => $pageIds, 'page_touched' => $dbw->timestamp( $touchTimestamp ) ], |
||
137 | __METHOD__ |
||
138 | ) ); |
||
139 | |||
140 | // Update CDN |
||
141 | $u = CdnCacheUpdate::newFromTitles( $titleArray ); |
||
142 | $u->doUpdate(); |
||
143 | |||
144 | // Update file cache |
||
145 | if ( $wgUseFileCache ) { |
||
146 | foreach ( $titleArray as $title ) { |
||
147 | HTMLFileCache::clearFileCache( $title ); |
||
148 | } |
||
149 | } |
||
150 | } |
||
151 | |||
152 | public function workItemCount() { |
||
155 | } |
||
156 |
This function 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 function will be removed from the class and what other function to use instead.