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