| Conditions | 21 |
| Paths | 1795 |
| Total Lines | 161 |
| Code Lines | 100 |
| 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 |
||
| 176 | public function backfillGroup($groupArr, $left, $articles = ''): void |
||
| 177 | { |
||
| 178 | // Start time for this group. |
||
| 179 | $startGroup = microtime(true); |
||
| 180 | |||
| 181 | $this->_binaries->logIndexerStart(); |
||
| 182 | |||
| 183 | $groupName = str_replace('alt.binaries', 'a.b', $groupArr['name']); |
||
| 184 | |||
| 185 | // If our local oldest article 0, it means we never ran update_binaries on the group. |
||
| 186 | if ($groupArr['first_record'] <= 0) { |
||
| 187 | $dMessage = |
||
| 188 | 'You need to run update_binaries on '. |
||
| 189 | $groupName. |
||
| 190 | '. Otherwise the group is dead, you must disable it.'; |
||
| 191 | |||
| 192 | if ($this->_echoCLI) { |
||
| 193 | ColorCLI::doEcho(ColorCLI::error($dMessage), true); |
||
| 194 | } |
||
| 195 | |||
| 196 | return; |
||
| 197 | } |
||
| 198 | |||
| 199 | // Select group, here, only once |
||
| 200 | $data = $this->_nntp->selectGroup($groupArr['name']); |
||
| 201 | if ($this->_nntp->isError($data)) { |
||
| 202 | $data = $this->_nntp->dataError($this->_nntp, $groupArr['name']); |
||
| 203 | if ($this->_nntp->isError($data)) { |
||
| 204 | return; |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | if ($this->_echoCLI) { |
||
| 209 | ColorCLI::doEcho(ColorCLI::primary('Processing '.$groupName), true); |
||
| 210 | } |
||
| 211 | |||
| 212 | // Check if this is days or post backfill. |
||
| 213 | $postCheck = $articles !== ''; |
||
| 214 | |||
| 215 | // Get target post based on date or user specified number. |
||
| 216 | $targetpost = (string) ( |
||
| 217 | $postCheck |
||
| 218 | ? |
||
| 219 | round($groupArr['first_record'] - $articles) |
||
| 220 | : |
||
| 221 | $this->_binaries->daytopost($groupArr['backfill_target'], $data) |
||
| 222 | ); |
||
| 223 | |||
| 224 | // Check if target post is smaller than server's oldest, set it to oldest if so. |
||
| 225 | if ($targetpost < $data['first']) { |
||
| 226 | $targetpost = $data['first']; |
||
| 227 | } |
||
| 228 | |||
| 229 | // Check if our target post is newer than our oldest post or if our local oldest article is older than the servers oldest. |
||
| 230 | if ($targetpost >= $groupArr['first_record'] || $groupArr['first_record'] <= $data['first']) { |
||
| 231 | $dMessage = |
||
| 232 | 'We have hit the maximum we can backfill for '. |
||
| 233 | $groupName. |
||
| 234 | ($this->_disableBackfillGroup ? ', disabling backfill on it.' : |
||
| 235 | ', skipping it, consider disabling backfill on it.'); |
||
| 236 | |||
| 237 | if ($this->_disableBackfillGroup) { |
||
| 238 | Group::updateGroupStatus($groupArr['id'], 'backfill', 0); |
||
| 239 | } |
||
| 240 | |||
| 241 | if ($this->_echoCLI) { |
||
| 242 | ColorCLI::doEcho(ColorCLI::notice($dMessage), true); |
||
| 243 | } |
||
| 244 | |||
| 245 | return; |
||
| 246 | } |
||
| 247 | |||
| 248 | if ($this->_echoCLI) { |
||
| 249 | ColorCLI::doEcho( |
||
| 250 | ColorCLI::primary( |
||
| 251 | 'Group '. |
||
| 252 | $groupName. |
||
| 253 | "'s oldest article is ". |
||
| 254 | number_format($data['first']). |
||
| 255 | ', newest is '. |
||
| 256 | number_format($data['last']). |
||
| 257 | ".\nOur target article is ". |
||
| 258 | number_format($targetpost). |
||
| 259 | '. Our oldest article is article '. |
||
| 260 | number_format($groupArr['first_record']). |
||
| 261 | '.' |
||
| 262 | ), true |
||
| 263 | ); |
||
| 264 | } |
||
| 265 | |||
| 266 | // Set first and last, moving the window by max messages. |
||
| 267 | $last = ($groupArr['first_record'] - 1); |
||
| 268 | // Set the initial "chunk". |
||
| 269 | $first = ($last - $this->_binaries->messageBuffer + 1); |
||
| 270 | |||
| 271 | // Just in case this is the last chunk we needed. |
||
| 272 | if ($targetpost > $first) { |
||
| 273 | $first = $targetpost; |
||
| 274 | } |
||
| 275 | |||
| 276 | $done = false; |
||
| 277 | while ($done === false) { |
||
| 278 | if ($this->_echoCLI) { |
||
| 279 | ColorCLI::doEcho( |
||
| 280 | color('Getting '. |
||
| 281 | number_format($last - $first + 1). |
||
| 282 | ' articles from '. |
||
| 283 | $groupName. |
||
| 284 | ', '. |
||
| 285 | $left. |
||
| 286 | ' group(s) left. ('. |
||
| 287 | number_format($first - $targetpost). |
||
| 288 | ' articles in queue')->fg('yellow'), true); |
||
| 289 | } |
||
| 290 | |||
| 291 | flush(); |
||
| 292 | $lastMsg = $this->_binaries->scan($groupArr, $first, $last, $this->_safePartRepair); |
||
| 293 | |||
| 294 | // Get the oldest date. |
||
| 295 | if (isset($lastMsg['firstArticleDate'])) { |
||
| 296 | // Try to get it from the oldest pulled article. |
||
| 297 | $newdate = strtotime($lastMsg['firstArticleDate']); |
||
| 298 | } else { |
||
| 299 | // If above failed, try to get it with postdate method. |
||
| 300 | $newdate = $this->_binaries->postdate($first, $data); |
||
| 301 | } |
||
| 302 | |||
| 303 | $this->pdo->queryExec( |
||
| 304 | sprintf( |
||
| 305 | ' |
||
| 306 | UPDATE groups |
||
| 307 | SET first_record_postdate = %s, first_record = %s, last_updated = NOW() |
||
| 308 | WHERE id = %d', |
||
| 309 | $this->pdo->from_unixtime($newdate), |
||
| 310 | $this->pdo->escapeString($first), |
||
| 311 | $groupArr['id'] |
||
| 312 | ) |
||
| 313 | ); |
||
| 314 | if ($first === $targetpost) { |
||
| 315 | $done = true; |
||
| 316 | } else { |
||
| 317 | // Keep going: set new last, new first, check for last chunk. |
||
| 318 | $last = ($first - 1); |
||
| 319 | $first = ($last - $this->_binaries->messageBuffer + 1); |
||
| 320 | if ($targetpost > $first) { |
||
| 321 | $first = $targetpost; |
||
| 322 | } |
||
| 323 | } |
||
| 324 | } |
||
| 325 | |||
| 326 | if ($this->_echoCLI) { |
||
| 327 | ColorCLI::doEcho( |
||
| 328 | ColorCLI::primary( |
||
| 329 | PHP_EOL. |
||
| 330 | 'Group '. |
||
| 331 | $groupName. |
||
| 332 | ' processed in '. |
||
| 333 | number_format(microtime(true) - $startGroup, 2). |
||
| 334 | ' seconds.' |
||
| 335 | ), |
||
| 336 | true |
||
| 337 | ); |
||
| 373 |