MoveTopic.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. <?php
  2. /**
  3. * This file contains the functions required to move topics from one board to
  4. * another board.
  5. *
  6. * Simple Machines Forum (SMF)
  7. *
  8. * @package SMF
  9. * @author Simple Machines http://www.simplemachines.org
  10. * @copyright 2011 Simple Machines
  11. * @license http://www.simplemachines.org/about/smf/license.php BSD
  12. *
  13. * @version 2.1 Alpha 1
  14. */
  15. if (!defined('SMF'))
  16. die('Hacking attempt...');
  17. /**
  18. * This function allows to move a topic, making sure to ask the moderator
  19. * to give reason for topic move.
  20. * It must be called with a topic specified. (that is, global $topic must
  21. * be set... @todo fix this thing.)
  22. * If the member is the topic starter requires the move_own permission,
  23. * otherwise the move_any permission.
  24. * Accessed via ?action=movetopic.
  25. *
  26. * @uses the MoveTopic template, main sub-template.
  27. */
  28. function MoveTopic()
  29. {
  30. global $txt, $board, $topic, $user_info, $context, $language, $scripturl, $settings, $smcFunc, $modSettings;
  31. if (empty($topic))
  32. fatal_lang_error('no_access', false);
  33. $request = $smcFunc['db_query']('', '
  34. SELECT t.id_member_started, ms.subject, t.approved
  35. FROM {db_prefix}topics AS t
  36. INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)
  37. WHERE t.id_topic = {int:current_topic}
  38. LIMIT 1',
  39. array(
  40. 'current_topic' => $topic,
  41. )
  42. );
  43. list ($id_member_started, $context['subject'], $context['is_approved']) = $smcFunc['db_fetch_row']($request);
  44. $smcFunc['db_free_result']($request);
  45. // Can they see it - if not approved?
  46. if ($modSettings['postmod_active'] && !$context['is_approved'])
  47. isAllowedTo('approve_posts');
  48. // Permission check!
  49. // @todo
  50. if (!allowedTo('move_any'))
  51. {
  52. if ($id_member_started == $user_info['id'])
  53. {
  54. isAllowedTo('move_own');
  55. //$boards = array_merge(boardsAllowedTo('move_own'), boardsAllowedTo('move_any'));
  56. }
  57. else
  58. isAllowedTo('move_any');
  59. }
  60. //else
  61. //$boards = boardsAllowedTo('move_any');
  62. loadTemplate('MoveTopic');
  63. // Get a list of boards this moderator can move to.
  64. $request = $smcFunc['db_query']('order_by_board_order', '
  65. SELECT b.id_board, b.name, b.child_level, c.name AS cat_name, c.id_cat
  66. FROM {db_prefix}boards AS b
  67. LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)
  68. WHERE {query_see_board}
  69. AND b.redirect = {string:blank_redirect}',
  70. array(
  71. 'blank_redirect' => '',
  72. 'current_board' => $board,
  73. )
  74. );
  75. $number_of_boards = $smcFunc['db_num_rows']($request);
  76. while ($row = $smcFunc['db_fetch_assoc']($request))
  77. {
  78. if (!isset($context['categories'][$row['id_cat']]))
  79. $context['categories'][$row['id_cat']] = array (
  80. 'name' => strip_tags($row['cat_name']),
  81. 'boards' => array(),
  82. );
  83. $context['categories'][$row['id_cat']]['boards'][] = array(
  84. 'id' => $row['id_board'],
  85. 'name' => strip_tags($row['name']),
  86. 'category' => strip_tags($row['cat_name']),
  87. 'child_level' => $row['child_level'],
  88. 'selected' => !empty($_SESSION['move_to_topic']) && $_SESSION['move_to_topic'] == $row['id_board'] && $row['id_board'] != $board,
  89. );
  90. }
  91. $smcFunc['db_free_result']($request);
  92. if (empty($context['categories']) || (!empty($number_of_boards) && $number_of_boards == 1))
  93. fatal_lang_error('moveto_noboards', false);
  94. $context['page_title'] = $txt['move_topic'];
  95. $context['linktree'][] = array(
  96. 'url' => $scripturl . '?topic=' . $topic . '.0',
  97. 'name' => $context['subject'],
  98. );
  99. $context['linktree'][] = array(
  100. 'name' => $txt['move_topic'],
  101. );
  102. $context['back_to_topic'] = isset($_REQUEST['goback']);
  103. if ($user_info['language'] != $language)
  104. {
  105. loadLanguage('index', $language);
  106. $temp = $txt['movetopic_default'];
  107. loadLanguage('index');
  108. $txt['movetopic_default'] = $temp;
  109. }
  110. // Register this form and get a sequence number in $context.
  111. checkSubmitOnce('register');
  112. }
  113. /**
  114. * Execute the move of a topic.
  115. * It is called on the submit of MoveTopic.
  116. * This function logs that topics have been moved in the moderation log.
  117. * If the member is the topic starter requires the move_own permission,
  118. * otherwise requires the move_any permission.
  119. * Upon successful completion redirects to message index.
  120. * Accessed via ?action=movetopic2.
  121. *
  122. * @uses Subs-Post.php.
  123. */
  124. function MoveTopic2()
  125. {
  126. global $txt, $board, $topic, $scripturl, $sourcedir, $modSettings, $context;
  127. global $board, $language, $user_info, $smcFunc;
  128. if (empty($topic))
  129. fatal_lang_error('no_access', false);
  130. // You can't choose to have a redirection topic and use an empty reason.
  131. if (isset($_POST['postRedirect']) && (!isset($_POST['reason']) || trim($_POST['reason']) == ''))
  132. fatal_lang_error('movetopic_no_reason', false);
  133. // Make sure this form hasn't been submitted before.
  134. checkSubmitOnce('check');
  135. $request = $smcFunc['db_query']('', '
  136. SELECT id_member_started, id_first_msg, approved
  137. FROM {db_prefix}topics
  138. WHERE id_topic = {int:current_topic}
  139. LIMIT 1',
  140. array(
  141. 'current_topic' => $topic,
  142. )
  143. );
  144. list ($id_member_started, $id_first_msg, $context['is_approved']) = $smcFunc['db_fetch_row']($request);
  145. $smcFunc['db_free_result']($request);
  146. // Can they see it?
  147. if (!$context['is_approved'])
  148. isAllowedTo('approve_posts');
  149. // Can they move topics on this board?
  150. if (!allowedTo('move_any'))
  151. {
  152. if ($id_member_started == $user_info['id'])
  153. {
  154. isAllowedTo('move_own');
  155. $boards = array_merge(boardsAllowedTo('move_own'), boardsAllowedTo('move_any'));
  156. }
  157. else
  158. isAllowedTo('move_any');
  159. }
  160. else
  161. $boards = boardsAllowedTo('move_any');
  162. // If this topic isn't approved don't let them move it if they can't approve it!
  163. if ($modSettings['postmod_active'] && !$context['is_approved'] && !allowedTo('approve_posts'))
  164. {
  165. // Only allow them to move it to other boards they can't approve it in.
  166. $can_approve = boardsAllowedTo('approve_posts');
  167. $boards = array_intersect($boards, $can_approve);
  168. }
  169. checkSession();
  170. require_once($sourcedir . '/Subs-Post.php');
  171. // The destination board must be numeric.
  172. $_POST['toboard'] = (int) $_POST['toboard'];
  173. // Make sure they can see the board they are trying to move to (and get whether posts count in the target board).
  174. $request = $smcFunc['db_query']('', '
  175. SELECT b.count_posts, b.name, m.subject
  176. FROM {db_prefix}boards AS b
  177. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = {int:current_topic})
  178. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
  179. WHERE {query_see_board}
  180. AND b.id_board = {int:to_board}
  181. AND b.redirect = {string:blank_redirect}
  182. LIMIT 1',
  183. array(
  184. 'current_topic' => $topic,
  185. 'to_board' => $_POST['toboard'],
  186. 'blank_redirect' => '',
  187. )
  188. );
  189. if ($smcFunc['db_num_rows']($request) == 0)
  190. fatal_lang_error('no_board');
  191. list ($pcounter, $board_name, $subject) = $smcFunc['db_fetch_row']($request);
  192. $smcFunc['db_free_result']($request);
  193. // Remember this for later.
  194. $_SESSION['move_to_topic'] = $_POST['toboard'];
  195. // Rename the topic...
  196. if (isset($_POST['reset_subject'], $_POST['custom_subject']) && $_POST['custom_subject'] != '')
  197. {
  198. $_POST['custom_subject'] = strtr($smcFunc['htmltrim']($smcFunc['htmlspecialchars']($_POST['custom_subject'])), array("\r" => '', "\n" => '', "\t" => ''));
  199. // Keep checking the length.
  200. if ($smcFunc['strlen']($_POST['custom_subject']) > 100)
  201. $_POST['custom_subject'] = $smcFunc['substr']($_POST['custom_subject'], 0, 100);
  202. // If it's still valid move onwards and upwards.
  203. if ($_POST['custom_subject'] != '')
  204. {
  205. if (isset($_POST['enforce_subject']))
  206. {
  207. // Get a response prefix, but in the forum's default language.
  208. if (!isset($context['response_prefix']) && !($context['response_prefix'] = cache_get_data('response_prefix')))
  209. {
  210. if ($language === $user_info['language'])
  211. $context['response_prefix'] = $txt['response_prefix'];
  212. else
  213. {
  214. loadLanguage('index', $language, false);
  215. $context['response_prefix'] = $txt['response_prefix'];
  216. loadLanguage('index');
  217. }
  218. cache_put_data('response_prefix', $context['response_prefix'], 600);
  219. }
  220. $smcFunc['db_query']('', '
  221. UPDATE {db_prefix}messages
  222. SET subject = {string:subject}
  223. WHERE id_topic = {int:current_topic}',
  224. array(
  225. 'current_topic' => $topic,
  226. 'subject' => $context['response_prefix'] . $_POST['custom_subject'],
  227. )
  228. );
  229. }
  230. $smcFunc['db_query']('', '
  231. UPDATE {db_prefix}messages
  232. SET subject = {string:custom_subject}
  233. WHERE id_msg = {int:id_first_msg}',
  234. array(
  235. 'id_first_msg' => $id_first_msg,
  236. 'custom_subject' => $_POST['custom_subject'],
  237. )
  238. );
  239. // Fix the subject cache.
  240. updateStats('subject', $topic, $_POST['custom_subject']);
  241. }
  242. }
  243. // Create a link to this in the old board.
  244. // @todo Does this make sense if the topic was unapproved before? I'd just about say so.
  245. if (isset($_POST['postRedirect']))
  246. {
  247. // Should be in the boardwide language.
  248. if ($user_info['language'] != $language)
  249. loadLanguage('index', $language);
  250. $_POST['reason'] = $smcFunc['htmlspecialchars']($_POST['reason'], ENT_QUOTES);
  251. preparsecode($_POST['reason']);
  252. // Add a URL onto the message.
  253. $_POST['reason'] = strtr($_POST['reason'], array(
  254. $txt['movetopic_auto_board'] => '[url=' . $scripturl . '?board=' . $_POST['toboard'] . '.0]' . $board_name . '[/url]',
  255. $txt['movetopic_auto_topic'] => '[iurl]' . $scripturl . '?topic=' . $topic . '.0[/iurl]'
  256. ));
  257. $msgOptions = array(
  258. 'subject' => $txt['moved'] . ': ' . $subject,
  259. 'body' => $_POST['reason'],
  260. 'icon' => 'moved',
  261. 'smileys_enabled' => 1,
  262. );
  263. $topicOptions = array(
  264. 'board' => $board,
  265. 'lock_mode' => 1,
  266. 'mark_as_read' => true,
  267. );
  268. $posterOptions = array(
  269. 'id' => $user_info['id'],
  270. 'update_post_count' => empty($pcounter),
  271. );
  272. createPost($msgOptions, $topicOptions, $posterOptions);
  273. }
  274. $request = $smcFunc['db_query']('', '
  275. SELECT count_posts
  276. FROM {db_prefix}boards
  277. WHERE id_board = {int:current_board}
  278. LIMIT 1',
  279. array(
  280. 'current_board' => $board,
  281. )
  282. );
  283. list ($pcounter_from) = $smcFunc['db_fetch_row']($request);
  284. $smcFunc['db_free_result']($request);
  285. if ($pcounter_from != $pcounter)
  286. {
  287. $request = $smcFunc['db_query']('', '
  288. SELECT id_member
  289. FROM {db_prefix}messages
  290. WHERE id_topic = {int:current_topic}
  291. AND approved = {int:is_approved}',
  292. array(
  293. 'current_topic' => $topic,
  294. 'is_approved' => 1,
  295. )
  296. );
  297. $posters = array();
  298. while ($row = $smcFunc['db_fetch_assoc']($request))
  299. {
  300. if (!isset($posters[$row['id_member']]))
  301. $posters[$row['id_member']] = 0;
  302. $posters[$row['id_member']]++;
  303. }
  304. $smcFunc['db_free_result']($request);
  305. foreach ($posters as $id_member => $posts)
  306. {
  307. // The board we're moving from counted posts, but not to.
  308. if (empty($pcounter_from))
  309. updateMemberData($id_member, array('posts' => 'posts - ' . $posts));
  310. // The reverse: from didn't, to did.
  311. else
  312. updateMemberData($id_member, array('posts' => 'posts + ' . $posts));
  313. }
  314. }
  315. // Do the move (includes statistics update needed for the redirect topic).
  316. moveTopics($topic, $_POST['toboard']);
  317. // Log that they moved this topic.
  318. if (!allowedTo('move_own') || $id_member_started != $user_info['id'])
  319. logAction('move', array('topic' => $topic, 'board_from' => $board, 'board_to' => $_POST['toboard']));
  320. // Notify people that this topic has been moved?
  321. sendNotifications($topic, 'move');
  322. // Why not go back to the original board in case they want to keep moving?
  323. if (!isset($_REQUEST['goback']))
  324. redirectexit('board=' . $board . '.0');
  325. else
  326. redirectexit('topic=' . $topic . '.0');
  327. }
  328. // Moves one or more topics to a specific board. (doesn't check permissions.)
  329. function moveTopics($topics, $toBoard)
  330. {
  331. global $sourcedir, $user_info, $modSettings, $smcFunc;
  332. // Empty array?
  333. if (empty($topics))
  334. return;
  335. // Only a single topic.
  336. elseif (is_numeric($topics))
  337. $topics = array($topics);
  338. $num_topics = count($topics);
  339. $fromBoards = array();
  340. // Destination board empty or equal to 0?
  341. if (empty($toBoard))
  342. return;
  343. // Are we moving to the recycle board?
  344. $isRecycleDest = !empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] == $toBoard;
  345. // Determine the source boards...
  346. $request = $smcFunc['db_query']('', '
  347. SELECT id_board, approved, COUNT(*) AS num_topics, SUM(unapproved_posts) AS unapproved_posts,
  348. SUM(num_replies) AS num_replies
  349. FROM {db_prefix}topics
  350. WHERE id_topic IN ({array_int:topics})
  351. GROUP BY id_board, approved',
  352. array(
  353. 'topics' => $topics,
  354. )
  355. );
  356. // Num of rows = 0 -> no topics found. Num of rows > 1 -> topics are on multiple boards.
  357. if ($smcFunc['db_num_rows']($request) == 0)
  358. return;
  359. while ($row = $smcFunc['db_fetch_assoc']($request))
  360. {
  361. if (!isset($fromBoards[$row['id_board']]['num_posts']))
  362. {
  363. $fromBoards[$row['id_board']] = array(
  364. 'num_posts' => 0,
  365. 'num_topics' => 0,
  366. 'unapproved_posts' => 0,
  367. 'unapproved_topics' => 0,
  368. 'id_board' => $row['id_board']
  369. );
  370. }
  371. // Posts = (num_replies + 1) for each approved topic.
  372. $fromBoards[$row['id_board']]['num_posts'] += $row['num_replies'] + ($row['approved'] ? $row['num_topics'] : 0);
  373. $fromBoards[$row['id_board']]['unapproved_posts'] += $row['unapproved_posts'];
  374. // Add the topics to the right type.
  375. if ($row['approved'])
  376. $fromBoards[$row['id_board']]['num_topics'] += $row['num_topics'];
  377. else
  378. $fromBoards[$row['id_board']]['unapproved_topics'] += $row['num_topics'];
  379. }
  380. $smcFunc['db_free_result']($request);
  381. // Move over the mark_read data. (because it may be read and now not by some!)
  382. $SaveAServer = max(0, $modSettings['maxMsgID'] - 50000);
  383. $request = $smcFunc['db_query']('', '
  384. SELECT lmr.id_member, lmr.id_msg, t.id_topic
  385. FROM {db_prefix}topics AS t
  386. INNER JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board
  387. AND lmr.id_msg > t.id_first_msg AND lmr.id_msg > {int:protect_lmr_msg})
  388. LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = lmr.id_member)
  389. WHERE t.id_topic IN ({array_int:topics})
  390. AND lmr.id_msg > IFNULL(lt.id_msg, 0)',
  391. array(
  392. 'protect_lmr_msg' => $SaveAServer,
  393. 'topics' => $topics,
  394. )
  395. );
  396. $log_topics = array();
  397. while ($row = $smcFunc['db_fetch_assoc']($request))
  398. {
  399. $log_topics[] = array($row['id_topic'], $row['id_member'], $row['id_msg']);
  400. // Prevent queries from getting too big. Taking some steam off.
  401. if (count($log_topics) > 500)
  402. {
  403. $smcFunc['db_insert']('replace',
  404. '{db_prefix}log_topics',
  405. array('id_topic' => 'int', 'id_member' => 'int', 'id_msg' => 'int'),
  406. $log_topics,
  407. array('id_topic', 'id_member')
  408. );
  409. $log_topics = array();
  410. }
  411. }
  412. $smcFunc['db_free_result']($request);
  413. // Now that we have all the topics that *should* be marked read, and by which members...
  414. if (!empty($log_topics))
  415. {
  416. // Insert that information into the database!
  417. $smcFunc['db_insert']('replace',
  418. '{db_prefix}log_topics',
  419. array('id_topic' => 'int', 'id_member' => 'int', 'id_msg' => 'int'),
  420. $log_topics,
  421. array('id_topic', 'id_member')
  422. );
  423. }
  424. // Update the number of posts on each board.
  425. $totalTopics = 0;
  426. $totalPosts = 0;
  427. $totalUnapprovedTopics = 0;
  428. $totalUnapprovedPosts = 0;
  429. foreach ($fromBoards as $stats)
  430. {
  431. $smcFunc['db_query']('', '
  432. UPDATE {db_prefix}boards
  433. SET
  434. num_posts = CASE WHEN {int:num_posts} > num_posts THEN 0 ELSE num_posts - {int:num_posts} END,
  435. num_topics = CASE WHEN {int:num_topics} > num_topics THEN 0 ELSE num_topics - {int:num_topics} END,
  436. unapproved_posts = CASE WHEN {int:unapproved_posts} > unapproved_posts THEN 0 ELSE unapproved_posts - {int:unapproved_posts} END,
  437. unapproved_topics = CASE WHEN {int:unapproved_topics} > unapproved_topics THEN 0 ELSE unapproved_topics - {int:unapproved_topics} END
  438. WHERE id_board = {int:id_board}',
  439. array(
  440. 'id_board' => $stats['id_board'],
  441. 'num_posts' => $stats['num_posts'],
  442. 'num_topics' => $stats['num_topics'],
  443. 'unapproved_posts' => $stats['unapproved_posts'],
  444. 'unapproved_topics' => $stats['unapproved_topics'],
  445. )
  446. );
  447. $totalTopics += $stats['num_topics'];
  448. $totalPosts += $stats['num_posts'];
  449. $totalUnapprovedTopics += $stats['unapproved_topics'];
  450. $totalUnapprovedPosts += $stats['unapproved_posts'];
  451. }
  452. $smcFunc['db_query']('', '
  453. UPDATE {db_prefix}boards
  454. SET
  455. num_topics = num_topics + {int:total_topics},
  456. num_posts = num_posts + {int:total_posts},' . ($isRecycleDest ? '
  457. unapproved_posts = {int:no_unapproved}, unapproved_topics = {int:no_unapproved}' : '
  458. unapproved_posts = unapproved_posts + {int:total_unapproved_posts},
  459. unapproved_topics = unapproved_topics + {int:total_unapproved_topics}') . '
  460. WHERE id_board = {int:id_board}',
  461. array(
  462. 'id_board' => $toBoard,
  463. 'total_topics' => $totalTopics,
  464. 'total_posts' => $totalPosts,
  465. 'total_unapproved_topics' => $totalUnapprovedTopics,
  466. 'total_unapproved_posts' => $totalUnapprovedPosts,
  467. 'no_unapproved' => 0,
  468. )
  469. );
  470. // Move the topic. Done. :P
  471. $smcFunc['db_query']('', '
  472. UPDATE {db_prefix}topics
  473. SET id_board = {int:id_board}' . ($isRecycleDest ? ',
  474. unapproved_posts = {int:no_unapproved}, approved = {int:is_approved}' : '') . '
  475. WHERE id_topic IN ({array_int:topics})',
  476. array(
  477. 'id_board' => $toBoard,
  478. 'topics' => $topics,
  479. 'is_approved' => 1,
  480. 'no_unapproved' => 0,
  481. )
  482. );
  483. // If this was going to the recycle bin, check what messages are being recycled, and remove them from the queue.
  484. if ($isRecycleDest && ($totalUnapprovedTopics || $totalUnapprovedPosts))
  485. {
  486. $request = $smcFunc['db_query']('', '
  487. SELECT id_msg
  488. FROM {db_prefix}messages
  489. WHERE id_topic IN ({array_int:topics})
  490. and approved = {int:not_approved}',
  491. array(
  492. 'topics' => $topics,
  493. 'not_approved' => 0,
  494. )
  495. );
  496. $approval_msgs = array();
  497. while ($row = $smcFunc['db_fetch_assoc']($request))
  498. $approval_msgs[] = $row['id_msg'];
  499. $smcFunc['db_free_result']($request);
  500. // Empty the approval queue for these, as we're going to approve them next.
  501. if (!empty($approval_msgs))
  502. $smcFunc['db_query']('', '
  503. DELETE FROM {db_prefix}approval_queue
  504. WHERE id_msg IN ({array_int:message_list})
  505. AND id_attach = {int:id_attach}',
  506. array(
  507. 'message_list' => $approval_msgs,
  508. 'id_attach' => 0,
  509. )
  510. );
  511. // Get all the current max and mins.
  512. $request = $smcFunc['db_query']('', '
  513. SELECT id_topic, id_first_msg, id_last_msg
  514. FROM {db_prefix}topics
  515. WHERE id_topic IN ({array_int:topics})',
  516. array(
  517. 'topics' => $topics,
  518. )
  519. );
  520. $topicMaxMin = array();
  521. while ($row = $smcFunc['db_fetch_assoc']($request))
  522. {
  523. $topicMaxMin[$row['id_topic']] = array(
  524. 'min' => $row['id_first_msg'],
  525. 'max' => $row['id_last_msg'],
  526. );
  527. }
  528. $smcFunc['db_free_result']($request);
  529. // Check the MAX and MIN are correct.
  530. $request = $smcFunc['db_query']('', '
  531. SELECT id_topic, MIN(id_msg) AS first_msg, MAX(id_msg) AS last_msg
  532. FROM {db_prefix}messages
  533. WHERE id_topic IN ({array_int:topics})
  534. GROUP BY id_topic',
  535. array(
  536. 'topics' => $topics,
  537. )
  538. );
  539. while ($row = $smcFunc['db_fetch_assoc']($request))
  540. {
  541. // If not, update.
  542. if ($row['first_msg'] != $topicMaxMin[$row['id_topic']]['min'] || $row['last_msg'] != $topicMaxMin[$row['id_topic']]['max'])
  543. $smcFunc['db_query']('', '
  544. UPDATE {db_prefix}topics
  545. SET id_first_msg = {int:first_msg}, id_last_msg = {int:last_msg}
  546. WHERE id_topic = {int:selected_topic}',
  547. array(
  548. 'first_msg' => $row['first_msg'],
  549. 'last_msg' => $row['last_msg'],
  550. 'selected_topic' => $row['id_topic'],
  551. )
  552. );
  553. }
  554. $smcFunc['db_free_result']($request);
  555. }
  556. $smcFunc['db_query']('', '
  557. UPDATE {db_prefix}messages
  558. SET id_board = {int:id_board}' . ($isRecycleDest ? ',approved = {int:is_approved}' : '') . '
  559. WHERE id_topic IN ({array_int:topics})',
  560. array(
  561. 'id_board' => $toBoard,
  562. 'topics' => $topics,
  563. 'is_approved' => 1,
  564. )
  565. );
  566. $smcFunc['db_query']('', '
  567. UPDATE {db_prefix}log_reported
  568. SET id_board = {int:id_board}
  569. WHERE id_topic IN ({array_int:topics})',
  570. array(
  571. 'id_board' => $toBoard,
  572. 'topics' => $topics,
  573. )
  574. );
  575. $smcFunc['db_query']('', '
  576. UPDATE {db_prefix}calendar
  577. SET id_board = {int:id_board}
  578. WHERE id_topic IN ({array_int:topics})',
  579. array(
  580. 'id_board' => $toBoard,
  581. 'topics' => $topics,
  582. )
  583. );
  584. // Mark target board as seen, if it was already marked as seen before.
  585. $request = $smcFunc['db_query']('', '
  586. SELECT (IFNULL(lb.id_msg, 0) >= b.id_msg_updated) AS isSeen
  587. FROM {db_prefix}boards AS b
  588. LEFT JOIN {db_prefix}log_boards AS lb ON (lb.id_board = b.id_board AND lb.id_member = {int:current_member})
  589. WHERE b.id_board = {int:id_board}',
  590. array(
  591. 'current_member' => $user_info['id'],
  592. 'id_board' => $toBoard,
  593. )
  594. );
  595. list ($isSeen) = $smcFunc['db_fetch_row']($request);
  596. $smcFunc['db_free_result']($request);
  597. if (!empty($isSeen) && !$user_info['is_guest'])
  598. {
  599. $smcFunc['db_insert']('replace',
  600. '{db_prefix}log_boards',
  601. array('id_board' => 'int', 'id_member' => 'int', 'id_msg' => 'int'),
  602. array($toBoard, $user_info['id'], $modSettings['maxMsgID']),
  603. array('id_board', 'id_member')
  604. );
  605. }
  606. // Update the cache?
  607. if (!empty($modSettings['cache_enable']) && $modSettings['cache_enable'] >= 3)
  608. foreach ($topics as $topic_id)
  609. cache_put_data('topic_board-' . $topic_id, null, 120);
  610. require_once($sourcedir . '/Subs-Post.php');
  611. $updates = array_keys($fromBoards);
  612. $updates[] = $toBoard;
  613. updateLastMessages(array_unique($updates));
  614. // Update 'em pesky stats.
  615. updateStats('topic');
  616. updateStats('message');
  617. updateSettings(array(
  618. 'calendar_updated' => time(),
  619. ));
  620. }
  621. ?>