| Conditions | 33 |
| Paths | 17291 |
| Total Lines | 322 |
| Code Lines | 162 |
| Lines | 25 |
| Ratio | 7.76 % |
| 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 |
||
| 374 | function moveTopics($topics, $toBoard) |
||
| 375 | { |
||
| 376 | global $sourcedir, $user_info, $modSettings, $smcFunc; |
||
| 377 | |||
| 378 | // Empty array? |
||
| 379 | if (empty($topics)) |
||
| 380 | return; |
||
| 381 | |||
| 382 | // Only a single topic. |
||
| 383 | if (is_numeric($topics)) |
||
| 384 | $topics = array($topics); |
||
| 385 | |||
| 386 | $fromBoards = array(); |
||
| 387 | |||
| 388 | // Destination board empty or equal to 0? |
||
| 389 | if (empty($toBoard)) |
||
| 390 | return; |
||
| 391 | |||
| 392 | // Are we moving to the recycle board? |
||
| 393 | $isRecycleDest = !empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] == $toBoard; |
||
| 394 | |||
| 395 | // Callback for search APIs to do their thing |
||
| 396 | require_once($sourcedir . '/Search.php'); |
||
| 397 | $searchAPI = findSearchAPI(); |
||
| 398 | if ($searchAPI->supportsMethod('topicsMoved')) |
||
| 399 | $searchAPI->topicsMoved($topics, $toBoard); |
||
| 400 | |||
| 401 | // Determine the source boards... |
||
| 402 | $request = $smcFunc['db_query']('', ' |
||
| 403 | SELECT id_board, approved, COUNT(*) AS num_topics, SUM(unapproved_posts) AS unapproved_posts, |
||
| 404 | SUM(num_replies) AS num_replies |
||
| 405 | FROM {db_prefix}topics |
||
| 406 | WHERE id_topic IN ({array_int:topics}) |
||
| 407 | GROUP BY id_board, approved', |
||
| 408 | array( |
||
| 409 | 'topics' => $topics, |
||
| 410 | ) |
||
| 411 | ); |
||
| 412 | // Num of rows = 0 -> no topics found. Num of rows > 1 -> topics are on multiple boards. |
||
| 413 | if ($smcFunc['db_num_rows']($request) == 0) |
||
| 414 | return; |
||
| 415 | View Code Duplication | while ($row = $smcFunc['db_fetch_assoc']($request)) |
|
| 416 | { |
||
| 417 | if (!isset($fromBoards[$row['id_board']]['num_posts'])) |
||
| 418 | { |
||
| 419 | $fromBoards[$row['id_board']] = array( |
||
| 420 | 'num_posts' => 0, |
||
| 421 | 'num_topics' => 0, |
||
| 422 | 'unapproved_posts' => 0, |
||
| 423 | 'unapproved_topics' => 0, |
||
| 424 | 'id_board' => $row['id_board'] |
||
| 425 | ); |
||
| 426 | } |
||
| 427 | // Posts = (num_replies + 1) for each approved topic. |
||
| 428 | $fromBoards[$row['id_board']]['num_posts'] += $row['num_replies'] + ($row['approved'] ? $row['num_topics'] : 0); |
||
| 429 | $fromBoards[$row['id_board']]['unapproved_posts'] += $row['unapproved_posts']; |
||
| 430 | |||
| 431 | // Add the topics to the right type. |
||
| 432 | if ($row['approved']) |
||
| 433 | $fromBoards[$row['id_board']]['num_topics'] += $row['num_topics']; |
||
| 434 | else |
||
| 435 | $fromBoards[$row['id_board']]['unapproved_topics'] += $row['num_topics']; |
||
| 436 | } |
||
| 437 | $smcFunc['db_free_result']($request); |
||
| 438 | |||
| 439 | // Move over the mark_read data. (because it may be read and now not by some!) |
||
| 440 | $SaveAServer = max(0, $modSettings['maxMsgID'] - 50000); |
||
| 441 | $request = $smcFunc['db_query']('', ' |
||
| 442 | SELECT lmr.id_member, lmr.id_msg, t.id_topic, COALESCE(lt.unwatched, 0) AS unwatched |
||
| 443 | FROM {db_prefix}topics AS t |
||
| 444 | INNER JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board |
||
| 445 | AND lmr.id_msg > t.id_first_msg AND lmr.id_msg > {int:protect_lmr_msg}) |
||
| 446 | LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = lmr.id_member) |
||
| 447 | WHERE t.id_topic IN ({array_int:topics}) |
||
| 448 | AND lmr.id_msg > COALESCE(lt.id_msg, 0)', |
||
| 449 | array( |
||
| 450 | 'protect_lmr_msg' => $SaveAServer, |
||
| 451 | 'topics' => $topics, |
||
| 452 | ) |
||
| 453 | ); |
||
| 454 | $log_topics = array(); |
||
| 455 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
| 456 | { |
||
| 457 | $log_topics[] = array($row['id_topic'], $row['id_member'], $row['id_msg'], (is_null($row['unwatched']) ? 0 : $row['unwatched'])); |
||
| 458 | |||
| 459 | // Prevent queries from getting too big. Taking some steam off. |
||
| 460 | if (count($log_topics) > 500) |
||
| 461 | { |
||
| 462 | $smcFunc['db_insert']('replace', |
||
| 463 | '{db_prefix}log_topics', |
||
| 464 | array('id_topic' => 'int', 'id_member' => 'int', 'id_msg' => 'int', 'unwatched' => 'int'), |
||
| 465 | $log_topics, |
||
| 466 | array('id_topic', 'id_member') |
||
| 467 | ); |
||
| 468 | |||
| 469 | $log_topics = array(); |
||
| 470 | } |
||
| 471 | } |
||
| 472 | $smcFunc['db_free_result']($request); |
||
| 473 | |||
| 474 | // Now that we have all the topics that *should* be marked read, and by which members... |
||
| 475 | if (!empty($log_topics)) |
||
| 476 | { |
||
| 477 | // Insert that information into the database! |
||
| 478 | $smcFunc['db_insert']('replace', |
||
| 479 | '{db_prefix}log_topics', |
||
| 480 | array('id_topic' => 'int', 'id_member' => 'int', 'id_msg' => 'int', 'unwatched' => 'int'), |
||
| 481 | $log_topics, |
||
| 482 | array('id_topic', 'id_member') |
||
| 483 | ); |
||
| 484 | } |
||
| 485 | |||
| 486 | // Update the number of posts on each board. |
||
| 487 | $totalTopics = 0; |
||
| 488 | $totalPosts = 0; |
||
| 489 | $totalUnapprovedTopics = 0; |
||
| 490 | $totalUnapprovedPosts = 0; |
||
| 491 | foreach ($fromBoards as $stats) |
||
| 492 | { |
||
| 493 | $smcFunc['db_query']('', ' |
||
| 494 | UPDATE {db_prefix}boards |
||
| 495 | SET |
||
| 496 | num_posts = CASE WHEN {int:num_posts} > num_posts THEN 0 ELSE num_posts - {int:num_posts} END, |
||
| 497 | num_topics = CASE WHEN {int:num_topics} > num_topics THEN 0 ELSE num_topics - {int:num_topics} END, |
||
| 498 | unapproved_posts = CASE WHEN {int:unapproved_posts} > unapproved_posts THEN 0 ELSE unapproved_posts - {int:unapproved_posts} END, |
||
| 499 | unapproved_topics = CASE WHEN {int:unapproved_topics} > unapproved_topics THEN 0 ELSE unapproved_topics - {int:unapproved_topics} END |
||
| 500 | WHERE id_board = {int:id_board}', |
||
| 501 | array( |
||
| 502 | 'id_board' => $stats['id_board'], |
||
| 503 | 'num_posts' => $stats['num_posts'], |
||
| 504 | 'num_topics' => $stats['num_topics'], |
||
| 505 | 'unapproved_posts' => $stats['unapproved_posts'], |
||
| 506 | 'unapproved_topics' => $stats['unapproved_topics'], |
||
| 507 | ) |
||
| 508 | ); |
||
| 509 | $totalTopics += $stats['num_topics']; |
||
| 510 | $totalPosts += $stats['num_posts']; |
||
| 511 | $totalUnapprovedTopics += $stats['unapproved_topics']; |
||
| 512 | $totalUnapprovedPosts += $stats['unapproved_posts']; |
||
| 513 | } |
||
| 514 | $smcFunc['db_query']('', ' |
||
| 515 | UPDATE {db_prefix}boards |
||
| 516 | SET |
||
| 517 | num_topics = num_topics + {int:total_topics}, |
||
| 518 | num_posts = num_posts + {int:total_posts},' . ($isRecycleDest ? ' |
||
| 519 | unapproved_posts = {int:no_unapproved}, unapproved_topics = {int:no_unapproved}' : ' |
||
| 520 | unapproved_posts = unapproved_posts + {int:total_unapproved_posts}, |
||
| 521 | unapproved_topics = unapproved_topics + {int:total_unapproved_topics}') . ' |
||
| 522 | WHERE id_board = {int:id_board}', |
||
| 523 | array( |
||
| 524 | 'id_board' => $toBoard, |
||
| 525 | 'total_topics' => $totalTopics, |
||
| 526 | 'total_posts' => $totalPosts, |
||
| 527 | 'total_unapproved_topics' => $totalUnapprovedTopics, |
||
| 528 | 'total_unapproved_posts' => $totalUnapprovedPosts, |
||
| 529 | 'no_unapproved' => 0, |
||
| 530 | ) |
||
| 531 | ); |
||
| 532 | |||
| 533 | // Move the topic. Done. :P |
||
| 534 | $smcFunc['db_query']('', ' |
||
| 535 | UPDATE {db_prefix}topics |
||
| 536 | SET id_board = {int:id_board}' . ($isRecycleDest ? ', |
||
| 537 | unapproved_posts = {int:no_unapproved}, approved = {int:is_approved}' : '') . ' |
||
| 538 | WHERE id_topic IN ({array_int:topics})', |
||
| 539 | array( |
||
| 540 | 'id_board' => $toBoard, |
||
| 541 | 'topics' => $topics, |
||
| 542 | 'is_approved' => 1, |
||
| 543 | 'no_unapproved' => 0, |
||
| 544 | ) |
||
| 545 | ); |
||
| 546 | |||
| 547 | // If this was going to the recycle bin, check what messages are being recycled, and remove them from the queue. |
||
| 548 | if ($isRecycleDest && ($totalUnapprovedTopics || $totalUnapprovedPosts)) |
||
| 549 | { |
||
| 550 | $request = $smcFunc['db_query']('', ' |
||
| 551 | SELECT id_msg |
||
| 552 | FROM {db_prefix}messages |
||
| 553 | WHERE id_topic IN ({array_int:topics}) |
||
| 554 | and approved = {int:not_approved}', |
||
| 555 | array( |
||
| 556 | 'topics' => $topics, |
||
| 557 | 'not_approved' => 0, |
||
| 558 | ) |
||
| 559 | ); |
||
| 560 | $approval_msgs = array(); |
||
| 561 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
| 562 | $approval_msgs[] = $row['id_msg']; |
||
| 563 | $smcFunc['db_free_result']($request); |
||
| 564 | |||
| 565 | // Empty the approval queue for these, as we're going to approve them next. |
||
| 566 | if (!empty($approval_msgs)) |
||
| 567 | $smcFunc['db_query']('', ' |
||
| 568 | DELETE FROM {db_prefix}approval_queue |
||
| 569 | WHERE id_msg IN ({array_int:message_list}) |
||
| 570 | AND id_attach = {int:id_attach}', |
||
| 571 | array( |
||
| 572 | 'message_list' => $approval_msgs, |
||
| 573 | 'id_attach' => 0, |
||
| 574 | ) |
||
| 575 | ); |
||
| 576 | |||
| 577 | // Get all the current max and mins. |
||
| 578 | $request = $smcFunc['db_query']('', ' |
||
| 579 | SELECT id_topic, id_first_msg, id_last_msg |
||
| 580 | FROM {db_prefix}topics |
||
| 581 | WHERE id_topic IN ({array_int:topics})', |
||
| 582 | array( |
||
| 583 | 'topics' => $topics, |
||
| 584 | ) |
||
| 585 | ); |
||
| 586 | $topicMaxMin = array(); |
||
| 587 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
| 588 | { |
||
| 589 | $topicMaxMin[$row['id_topic']] = array( |
||
| 590 | 'min' => $row['id_first_msg'], |
||
| 591 | 'max' => $row['id_last_msg'], |
||
| 592 | ); |
||
| 593 | } |
||
| 594 | $smcFunc['db_free_result']($request); |
||
| 595 | |||
| 596 | // Check the MAX and MIN are correct. |
||
| 597 | $request = $smcFunc['db_query']('', ' |
||
| 598 | SELECT id_topic, MIN(id_msg) AS first_msg, MAX(id_msg) AS last_msg |
||
| 599 | FROM {db_prefix}messages |
||
| 600 | WHERE id_topic IN ({array_int:topics}) |
||
| 601 | GROUP BY id_topic', |
||
| 602 | array( |
||
| 603 | 'topics' => $topics, |
||
| 604 | ) |
||
| 605 | ); |
||
| 606 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
| 607 | { |
||
| 608 | // If not, update. |
||
| 609 | if ($row['first_msg'] != $topicMaxMin[$row['id_topic']]['min'] || $row['last_msg'] != $topicMaxMin[$row['id_topic']]['max']) |
||
| 610 | $smcFunc['db_query']('', ' |
||
| 611 | UPDATE {db_prefix}topics |
||
| 612 | SET id_first_msg = {int:first_msg}, id_last_msg = {int:last_msg} |
||
| 613 | WHERE id_topic = {int:selected_topic}', |
||
| 614 | array( |
||
| 615 | 'first_msg' => $row['first_msg'], |
||
| 616 | 'last_msg' => $row['last_msg'], |
||
| 617 | 'selected_topic' => $row['id_topic'], |
||
| 618 | ) |
||
| 619 | ); |
||
| 620 | } |
||
| 621 | $smcFunc['db_free_result']($request); |
||
| 622 | } |
||
| 623 | |||
| 624 | $smcFunc['db_query']('', ' |
||
| 625 | UPDATE {db_prefix}messages |
||
| 626 | SET id_board = {int:id_board}' . ($isRecycleDest ? ',approved = {int:is_approved}' : '') . ' |
||
| 627 | WHERE id_topic IN ({array_int:topics})', |
||
| 628 | array( |
||
| 629 | 'id_board' => $toBoard, |
||
| 630 | 'topics' => $topics, |
||
| 631 | 'is_approved' => 1, |
||
| 632 | ) |
||
| 633 | ); |
||
| 634 | $smcFunc['db_query']('', ' |
||
| 635 | UPDATE {db_prefix}log_reported |
||
| 636 | SET id_board = {int:id_board} |
||
| 637 | WHERE id_topic IN ({array_int:topics})', |
||
| 638 | array( |
||
| 639 | 'id_board' => $toBoard, |
||
| 640 | 'topics' => $topics, |
||
| 641 | ) |
||
| 642 | ); |
||
| 643 | $smcFunc['db_query']('', ' |
||
| 644 | UPDATE {db_prefix}calendar |
||
| 645 | SET id_board = {int:id_board} |
||
| 646 | WHERE id_topic IN ({array_int:topics})', |
||
| 647 | array( |
||
| 648 | 'id_board' => $toBoard, |
||
| 649 | 'topics' => $topics, |
||
| 650 | ) |
||
| 651 | ); |
||
| 652 | |||
| 653 | // Mark target board as seen, if it was already marked as seen before. |
||
| 654 | $request = $smcFunc['db_query']('', ' |
||
| 655 | SELECT (COALESCE(lb.id_msg, 0) >= b.id_msg_updated) AS isSeen |
||
| 656 | FROM {db_prefix}boards AS b |
||
| 657 | LEFT JOIN {db_prefix}log_boards AS lb ON (lb.id_board = b.id_board AND lb.id_member = {int:current_member}) |
||
| 658 | WHERE b.id_board = {int:id_board}', |
||
| 659 | array( |
||
| 660 | 'current_member' => $user_info['id'], |
||
| 661 | 'id_board' => $toBoard, |
||
| 662 | ) |
||
| 663 | ); |
||
| 664 | list ($isSeen) = $smcFunc['db_fetch_row']($request); |
||
| 665 | $smcFunc['db_free_result']($request); |
||
| 666 | |||
| 667 | if (!empty($isSeen) && !$user_info['is_guest']) |
||
| 668 | { |
||
| 669 | $smcFunc['db_insert']('replace', |
||
| 670 | '{db_prefix}log_boards', |
||
| 671 | array('id_board' => 'int', 'id_member' => 'int', 'id_msg' => 'int'), |
||
| 672 | array($toBoard, $user_info['id'], $modSettings['maxMsgID']), |
||
| 673 | array('id_board', 'id_member') |
||
| 674 | ); |
||
| 675 | } |
||
| 676 | |||
| 677 | // Update the cache? |
||
| 678 | View Code Duplication | if (!empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= 3) |
|
| 679 | foreach ($topics as $topic_id) |
||
| 680 | cache_put_data('topic_board-' . $topic_id, null, 120); |
||
| 681 | |||
| 682 | require_once($sourcedir . '/Subs-Post.php'); |
||
| 683 | |||
| 684 | $updates = array_keys($fromBoards); |
||
| 685 | $updates[] = $toBoard; |
||
| 686 | |||
| 687 | updateLastMessages(array_unique($updates)); |
||
| 688 | |||
| 689 | // Update 'em pesky stats. |
||
| 690 | updateStats('topic'); |
||
| 691 | updateStats('message'); |
||
| 692 | updateSettings(array( |
||
| 693 | 'calendar_updated' => time(), |
||
| 694 | )); |
||
| 695 | } |
||
| 696 | |||
| 733 | ?> |
||
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.