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