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