Conditions | 12 |
Paths | 67 |
Total Lines | 123 |
Code Lines | 62 |
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 |
||
221 | public function getRevisions( |
||
222 | array $dbNames, |
||
223 | User $user, |
||
224 | $namespace = 'all', |
||
225 | $start = false, |
||
226 | $end = false, |
||
227 | int $limit = 31, // One extra to know whether there should be another page. |
||
228 | $offset = false |
||
229 | ): array { |
||
230 | // Check cache. |
||
231 | $cacheKey = $this->getCacheKey(func_get_args(), 'gc_revisions'); |
||
232 | if ($this->cache->hasItem($cacheKey)) { |
||
233 | return $this->cache->getItem($cacheKey)->get(); |
||
234 | } |
||
235 | |||
236 | // Just need any Connection to use the ->quote() method. |
||
237 | $quoteConn = $this->getProjectsConnection('s1'); |
||
238 | $username = $quoteConn->quote($user->getUsername(), PDO::PARAM_STR); |
||
239 | |||
240 | // IP range handling. |
||
241 | $startIp = ''; |
||
242 | $endIp = ''; |
||
243 | if ($user->isIpRange()) { |
||
244 | [$startIp, $endIp] = IPUtils::parseRange($user->getUsername()); |
||
245 | $startIp = $quoteConn->quote($startIp, PDO::PARAM_STR); |
||
246 | $endIp = $quoteConn->quote($endIp, PDO::PARAM_STR); |
||
247 | } |
||
248 | |||
249 | // Fetch actor IDs (for IP ranges, it strips trailing zeros and uses a LIKE query). |
||
250 | $actorIds = $this->getDbNamesAndActorIds($user, $dbNames); |
||
251 | |||
252 | if (!$actorIds) { |
||
253 | return []; |
||
254 | } |
||
255 | |||
256 | $namespaceCond = 'all' === $namespace |
||
257 | ? '' |
||
258 | : 'AND page_namespace = '.(int)$namespace; |
||
259 | $revDateConditions = $this->getDateConditions($start, $end, $offset, 'revs.', 'rev_timestamp'); |
||
260 | |||
261 | // Assemble queries. |
||
262 | $queriesBySlice = []; |
||
263 | $projectRepo = $this->caProject->getRepository(); |
||
264 | foreach ($dbNames as $dbName) { |
||
265 | if (isset($actorIds[$dbName])) { |
||
266 | $revisionTable = $projectRepo->getTableName($dbName, 'revision'); |
||
267 | $pageTable = $projectRepo->getTableName($dbName, 'page'); |
||
268 | $commentTable = $projectRepo->getTableName($dbName, 'comment', 'revision'); |
||
269 | $actorTable = $projectRepo->getTableName($dbName, 'actor', 'revision'); |
||
270 | $tagTable = $projectRepo->getTableName($dbName, 'change_tag'); |
||
271 | $tagDefTable = $projectRepo->getTableName($dbName, 'change_tag_def'); |
||
272 | |||
273 | if ($user->isIpRange()) { |
||
274 | $ipcTable = $projectRepo->getTableName($dbName, 'ip_changes'); |
||
275 | $ipcJoin = "JOIN $ipcTable ON revs.rev_id = ipc_rev_id"; |
||
276 | $whereClause = "ipc_hex BETWEEN $startIp AND $endIp"; |
||
277 | $username = 'actor_name'; |
||
278 | } else { |
||
279 | $ipcJoin = ''; |
||
280 | $whereClause = 'revs.rev_actor = '.$actorIds[$dbName]; |
||
281 | } |
||
282 | |||
283 | $slice = $this->getDbList()[$dbName]; |
||
284 | $queriesBySlice[$slice][] = " |
||
285 | SELECT |
||
286 | '$dbName' AS dbName, |
||
287 | revs.rev_id AS id, |
||
288 | revs.rev_timestamp AS `timestamp`, |
||
289 | UNIX_TIMESTAMP(revs.rev_timestamp) AS `unix_timestamp`, |
||
290 | revs.rev_minor_edit AS `minor`, |
||
291 | revs.rev_deleted AS `deleted`, |
||
292 | revs.rev_len AS `length`, |
||
293 | (CAST(revs.rev_len AS SIGNED) - IFNULL(parentrevs.rev_len, 0)) AS `length_change`, |
||
294 | revs.rev_parent_id AS `parent_id`, |
||
295 | $username AS `username`, |
||
296 | page.page_title, |
||
297 | page.page_namespace AS `namespace`, |
||
298 | comment_text AS `comment`, |
||
299 | ( |
||
300 | SELECT 1 |
||
301 | FROM $tagTable |
||
302 | WHERE ct_rev_id = revs.rev_id |
||
303 | AND ct_tag_id = ( |
||
304 | SELECT ctd_id |
||
305 | FROM $tagDefTable |
||
306 | WHERE ctd_name = 'mw-reverted' |
||
307 | ) |
||
308 | LIMIT 1 |
||
309 | ) AS reverted |
||
310 | FROM $revisionTable AS revs |
||
311 | $ipcJoin |
||
312 | JOIN $pageTable AS page ON (rev_page = page_id) |
||
313 | JOIN $actorTable ON (actor_id = revs.rev_actor) |
||
314 | LEFT JOIN $revisionTable AS parentrevs ON (revs.rev_parent_id = parentrevs.rev_id) |
||
315 | LEFT OUTER JOIN $commentTable ON revs.rev_comment_id = comment_id |
||
316 | WHERE $whereClause |
||
317 | $namespaceCond |
||
318 | $revDateConditions"; |
||
319 | } |
||
320 | } |
||
321 | |||
322 | // Re-assemble into UNIONed queries, executing as many per slice as possible. |
||
323 | $revisions = []; |
||
324 | foreach ($queriesBySlice as $slice => $queries) { |
||
325 | $sql = "SELECT * FROM ((\n" . join("\n) UNION (\n", $queries) . ")) a ORDER BY timestamp DESC LIMIT $limit"; |
||
326 | $revisions = array_merge($revisions, $this->executeProjectsQuery($slice, $sql)->fetchAllAssociative()); |
||
327 | } |
||
328 | |||
329 | // If there are more than $limit results, re-sort by timestamp. |
||
330 | if (count($revisions) > $limit) { |
||
331 | usort($revisions, function ($a, $b) { |
||
332 | if ($a['unix_timestamp'] === $b['unix_timestamp']) { |
||
333 | return 0; |
||
334 | } |
||
335 | return $a['unix_timestamp'] > $b['unix_timestamp'] ? -1 : 1; |
||
336 | }); |
||
337 | |||
338 | // Truncate size to $limit. |
||
339 | $revisions = array_slice($revisions, 0, $limit); |
||
340 | } |
||
341 | |||
342 | // Cache and return. |
||
343 | return $this->setCache($cacheKey, $revisions); |
||
344 | } |
||
346 |