| Conditions | 91 |
| Paths | 0 |
| Total Lines | 754 |
| Code Lines | 375 |
| Lines | 122 |
| Ratio | 16.18 % |
| 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 |
||
| 1001 | function MergeExecute($topics = array()) |
||
| 1002 | { |
||
| 1003 | global $user_info, $txt, $context, $scripturl, $sourcedir; |
||
| 1004 | global $smcFunc, $language, $modSettings; |
||
| 1005 | |||
| 1006 | // Check the session. |
||
| 1007 | checkSession('request'); |
||
| 1008 | |||
| 1009 | // Handle URLs from MergeIndex. |
||
| 1010 | if (!empty($_GET['from']) && !empty($_GET['to'])) |
||
| 1011 | $topics = array((int) $_GET['from'], (int) $_GET['to']); |
||
| 1012 | |||
| 1013 | // If we came from a form, the topic IDs came by post. |
||
| 1014 | if (!empty($_POST['topics']) && is_array($_POST['topics'])) |
||
| 1015 | $topics = $_POST['topics']; |
||
| 1016 | |||
| 1017 | // There's nothing to merge with just one topic... |
||
| 1018 | if (empty($topics) || !is_array($topics) || count($topics) == 1) |
||
| 1019 | fatal_lang_error('merge_need_more_topics'); |
||
| 1020 | |||
| 1021 | // Make sure every topic is numeric, or some nasty things could be done with the DB. |
||
| 1022 | foreach ($topics as $id => $topic) |
||
| 1023 | $topics[$id] = (int) $topic; |
||
| 1024 | |||
| 1025 | // Joy of all joys, make sure they're not messing about with unapproved topics they can't see :P |
||
| 1026 | if ($modSettings['postmod_active']) |
||
| 1027 | $can_approve_boards = boardsAllowedTo('approve_posts'); |
||
| 1028 | |||
| 1029 | // Get info about the topics and polls that will be merged. |
||
| 1030 | $request = $smcFunc['db_query']('', ' |
||
| 1031 | SELECT |
||
| 1032 | t.id_topic, t.id_board, t.id_poll, t.num_views, t.is_sticky, t.approved, t.num_replies, t.unapproved_posts, t.id_redirect_topic, |
||
| 1033 | m1.subject, m1.poster_time AS time_started, COALESCE(mem1.id_member, 0) AS id_member_started, COALESCE(mem1.real_name, m1.poster_name) AS name_started, |
||
| 1034 | m2.poster_time AS time_updated, COALESCE(mem2.id_member, 0) AS id_member_updated, COALESCE(mem2.real_name, m2.poster_name) AS name_updated |
||
| 1035 | FROM {db_prefix}topics AS t |
||
| 1036 | INNER JOIN {db_prefix}messages AS m1 ON (m1.id_msg = t.id_first_msg) |
||
| 1037 | INNER JOIN {db_prefix}messages AS m2 ON (m2.id_msg = t.id_last_msg) |
||
| 1038 | LEFT JOIN {db_prefix}members AS mem1 ON (mem1.id_member = m1.id_member) |
||
| 1039 | LEFT JOIN {db_prefix}members AS mem2 ON (mem2.id_member = m2.id_member) |
||
| 1040 | WHERE t.id_topic IN ({array_int:topic_list}) |
||
| 1041 | ORDER BY t.id_first_msg |
||
| 1042 | LIMIT {int:limit}', |
||
| 1043 | array( |
||
| 1044 | 'topic_list' => $topics, |
||
| 1045 | 'limit' => count($topics), |
||
| 1046 | ) |
||
| 1047 | ); |
||
| 1048 | if ($smcFunc['db_num_rows']($request) < 2) |
||
| 1049 | fatal_lang_error('no_topic_id'); |
||
| 1050 | $num_views = 0; |
||
| 1051 | $is_sticky = 0; |
||
| 1052 | $boardTotals = array(); |
||
| 1053 | $boards = array(); |
||
| 1054 | $polls = array(); |
||
| 1055 | $firstTopic = 0; |
||
| 1056 | $context['is_approved'] = 1; |
||
| 1057 | $lowestTopicId = 0; |
||
| 1058 | $lowestTopicBoard = 0; |
||
| 1059 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
| 1060 | { |
||
| 1061 | // Sorry, redirection topics can't be merged |
||
| 1062 | if (!empty($row['id_redirect_topic'])) |
||
| 1063 | fatal_lang_error('cannot_merge_redirect', false); |
||
| 1064 | |||
| 1065 | // Make a note for the board counts... |
||
| 1066 | View Code Duplication | if (!isset($boardTotals[$row['id_board']])) |
|
| 1067 | $boardTotals[$row['id_board']] = array( |
||
| 1068 | 'posts' => 0, |
||
| 1069 | 'topics' => 0, |
||
| 1070 | 'unapproved_posts' => 0, |
||
| 1071 | 'unapproved_topics' => 0 |
||
| 1072 | ); |
||
| 1073 | |||
| 1074 | // We can't see unapproved topics here? |
||
| 1075 | if ($modSettings['postmod_active'] && !$row['approved'] && $can_approve_boards != array(0) && in_array($row['id_board'], $can_approve_boards)) |
||
| 1076 | { |
||
| 1077 | unset($topics[$row['id_topic']]); // If we can't see it, we should not merge it and not adjust counts! Instead skip it. |
||
| 1078 | continue; |
||
| 1079 | }elseif (!$row['approved']) |
||
| 1080 | $boardTotals[$row['id_board']]['unapproved_topics']++; |
||
| 1081 | else |
||
| 1082 | $boardTotals[$row['id_board']]['topics']++; |
||
| 1083 | |||
| 1084 | $boardTotals[$row['id_board']]['unapproved_posts'] += $row['unapproved_posts']; |
||
| 1085 | $boardTotals[$row['id_board']]['posts'] += $row['num_replies'] + ($row['approved'] ? 1 : 0); |
||
| 1086 | |||
| 1087 | // In the case of making a redirect, the topic count goes up by one due to the redirect topic. |
||
| 1088 | if (isset($_POST['postRedirect'])) |
||
| 1089 | $boardTotals[$row['id_board']]['topics']--; |
||
| 1090 | |||
| 1091 | $topic_data[$row['id_topic']] = array( |
||
| 1092 | 'id' => $row['id_topic'], |
||
| 1093 | 'board' => $row['id_board'], |
||
| 1094 | 'poll' => $row['id_poll'], |
||
| 1095 | 'num_views' => $row['num_views'], |
||
| 1096 | 'subject' => $row['subject'], |
||
| 1097 | 'started' => array( |
||
| 1098 | 'time' => timeformat($row['time_started']), |
||
| 1099 | 'timestamp' => forum_time(true, $row['time_started']), |
||
| 1100 | 'href' => empty($row['id_member_started']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member_started'], |
||
| 1101 | 'link' => empty($row['id_member_started']) ? $row['name_started'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member_started'] . '">' . $row['name_started'] . '</a>' |
||
| 1102 | ), |
||
| 1103 | 'updated' => array( |
||
| 1104 | 'time' => timeformat($row['time_updated']), |
||
| 1105 | 'timestamp' => forum_time(true, $row['time_updated']), |
||
| 1106 | 'href' => empty($row['id_member_updated']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member_updated'], |
||
| 1107 | 'link' => empty($row['id_member_updated']) ? $row['name_updated'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member_updated'] . '">' . $row['name_updated'] . '</a>' |
||
| 1108 | ), |
||
| 1109 | 'approved' => $row['approved'] |
||
| 1110 | ); |
||
| 1111 | $num_views += $row['num_views']; |
||
| 1112 | $boards[] = $row['id_board']; |
||
| 1113 | |||
| 1114 | // If there's no poll, id_poll == 0... |
||
| 1115 | if ($row['id_poll'] > 0) |
||
| 1116 | $polls[] = $row['id_poll']; |
||
| 1117 | // Store the id_topic with the lowest id_first_msg. |
||
| 1118 | if (empty($firstTopic)) |
||
| 1119 | $firstTopic = $row['id_topic']; |
||
| 1120 | |||
| 1121 | // Lowest topic id gets selected as surviving topic id. We need to store this board so we can adjust the topic count (This one will not have a redirect topic) |
||
| 1122 | if ($row['id_topic'] < $lowestTopicId || empty($lowestTopicId)) |
||
| 1123 | { |
||
| 1124 | $lowestTopicId = $row['id_topic']; |
||
| 1125 | $lowestTopicBoard = $row['id_board']; |
||
| 1126 | } |
||
| 1127 | |||
| 1128 | $is_sticky = max($is_sticky, $row['is_sticky']); |
||
| 1129 | } |
||
| 1130 | $smcFunc['db_free_result']($request); |
||
| 1131 | |||
| 1132 | // If we didn't get any topics then they've been messing with unapproved stuff. |
||
| 1133 | if (empty($topic_data)) |
||
| 1134 | fatal_lang_error('no_topic_id'); |
||
| 1135 | |||
| 1136 | if (isset($_POST['postRedirect']) && !empty($lowestTopicBoard)) |
||
| 1137 | $boardTotals[$lowestTopicBoard]['topics']++; |
||
| 1138 | |||
| 1139 | // Will this be approved? |
||
| 1140 | $context['is_approved'] = $topic_data[$firstTopic]['approved']; |
||
| 1141 | |||
| 1142 | $boards = array_values(array_unique($boards)); |
||
| 1143 | |||
| 1144 | // The parameters of MergeExecute were set, so this must've been an internal call. |
||
| 1145 | if (!empty($topics)) |
||
| 1146 | { |
||
| 1147 | isAllowedTo('merge_any', $boards); |
||
| 1148 | loadTemplate('MoveTopic'); |
||
| 1149 | } |
||
| 1150 | |||
| 1151 | // Get the boards a user is allowed to merge in. |
||
| 1152 | $merge_boards = boardsAllowedTo('merge_any'); |
||
| 1153 | if (empty($merge_boards)) |
||
| 1154 | fatal_lang_error('cannot_merge_any', 'user'); |
||
| 1155 | |||
| 1156 | // Make sure they can see all boards.... |
||
| 1157 | $request = $smcFunc['db_query']('', ' |
||
| 1158 | SELECT b.id_board |
||
| 1159 | FROM {db_prefix}boards AS b |
||
| 1160 | WHERE b.id_board IN ({array_int:boards}) |
||
| 1161 | AND {query_see_board}' . (!in_array(0, $merge_boards) ? ' |
||
| 1162 | AND b.id_board IN ({array_int:merge_boards})' : '') . ' |
||
| 1163 | LIMIT {int:limit}', |
||
| 1164 | array( |
||
| 1165 | 'boards' => $boards, |
||
| 1166 | 'merge_boards' => $merge_boards, |
||
| 1167 | 'limit' => count($boards), |
||
| 1168 | ) |
||
| 1169 | ); |
||
| 1170 | // If the number of boards that's in the output isn't exactly the same as we've put in there, you're in trouble. |
||
| 1171 | if ($smcFunc['db_num_rows']($request) != count($boards)) |
||
| 1172 | fatal_lang_error('no_board'); |
||
| 1173 | $smcFunc['db_free_result']($request); |
||
| 1174 | |||
| 1175 | if (empty($_REQUEST['sa']) || $_REQUEST['sa'] == 'options') |
||
| 1176 | { |
||
| 1177 | if (count($polls) > 1) |
||
| 1178 | { |
||
| 1179 | $request = $smcFunc['db_query']('', ' |
||
| 1180 | SELECT t.id_topic, t.id_poll, m.subject, p.question |
||
| 1181 | FROM {db_prefix}polls AS p |
||
| 1182 | INNER JOIN {db_prefix}topics AS t ON (t.id_poll = p.id_poll) |
||
| 1183 | INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg) |
||
| 1184 | WHERE p.id_poll IN ({array_int:polls}) |
||
| 1185 | LIMIT {int:limit}', |
||
| 1186 | array( |
||
| 1187 | 'polls' => $polls, |
||
| 1188 | 'limit' => count($polls), |
||
| 1189 | ) |
||
| 1190 | ); |
||
| 1191 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
| 1192 | $context['polls'][] = array( |
||
| 1193 | 'id' => $row['id_poll'], |
||
| 1194 | 'topic' => array( |
||
| 1195 | 'id' => $row['id_topic'], |
||
| 1196 | 'subject' => $row['subject'] |
||
| 1197 | ), |
||
| 1198 | 'question' => $row['question'], |
||
| 1199 | 'selected' => $row['id_topic'] == $firstTopic |
||
| 1200 | ); |
||
| 1201 | $smcFunc['db_free_result']($request); |
||
| 1202 | } |
||
| 1203 | if (count($boards) > 1) |
||
| 1204 | { |
||
| 1205 | $request = $smcFunc['db_query']('', ' |
||
| 1206 | SELECT id_board, name |
||
| 1207 | FROM {db_prefix}boards |
||
| 1208 | WHERE id_board IN ({array_int:boards}) |
||
| 1209 | ORDER BY name |
||
| 1210 | LIMIT {int:limit}', |
||
| 1211 | array( |
||
| 1212 | 'boards' => $boards, |
||
| 1213 | 'limit' => count($boards), |
||
| 1214 | ) |
||
| 1215 | ); |
||
| 1216 | View Code Duplication | while ($row = $smcFunc['db_fetch_assoc']($request)) |
|
| 1217 | $context['boards'][] = array( |
||
| 1218 | 'id' => $row['id_board'], |
||
| 1219 | 'name' => $row['name'], |
||
| 1220 | 'selected' => $row['id_board'] == $topic_data[$firstTopic]['board'] |
||
| 1221 | ); |
||
| 1222 | $smcFunc['db_free_result']($request); |
||
| 1223 | } |
||
| 1224 | |||
| 1225 | $context['topics'] = $topic_data; |
||
| 1226 | foreach ($topic_data as $id => $topic) |
||
| 1227 | $context['topics'][$id]['selected'] = $topic['id'] == $firstTopic; |
||
| 1228 | |||
| 1229 | $context['page_title'] = $txt['merge']; |
||
| 1230 | $context['sub_template'] = 'merge_extra_options'; |
||
| 1231 | return; |
||
| 1232 | } |
||
| 1233 | |||
| 1234 | // Determine target board. |
||
| 1235 | $target_board = count($boards) > 1 ? (int) $_REQUEST['board'] : $boards[0]; |
||
| 1236 | if (!in_array($target_board, $boards)) |
||
| 1237 | fatal_lang_error('no_board'); |
||
| 1238 | |||
| 1239 | // Determine which poll will survive and which polls won't. |
||
| 1240 | $target_poll = count($polls) > 1 ? (int) $_POST['poll'] : (count($polls) == 1 ? $polls[0] : 0); |
||
| 1241 | if ($target_poll > 0 && !in_array($target_poll, $polls)) |
||
| 1242 | fatal_lang_error('no_access', false); |
||
| 1243 | $deleted_polls = empty($target_poll) ? $polls : array_diff($polls, array($target_poll)); |
||
| 1244 | |||
| 1245 | // Determine the subject of the newly merged topic - was a custom subject specified? |
||
| 1246 | if (empty($_POST['subject']) && isset($_POST['custom_subject']) && $_POST['custom_subject'] != '') |
||
| 1247 | { |
||
| 1248 | $target_subject = strtr($smcFunc['htmltrim']($smcFunc['htmlspecialchars']($_POST['custom_subject'])), array("\r" => '', "\n" => '', "\t" => '')); |
||
| 1249 | // Keep checking the length. |
||
| 1250 | if ($smcFunc['strlen']($target_subject) > 100) |
||
| 1251 | $target_subject = $smcFunc['substr']($target_subject, 0, 100); |
||
| 1252 | |||
| 1253 | // Nothing left - odd but pick the first topics subject. |
||
| 1254 | if ($target_subject == '') |
||
| 1255 | $target_subject = $topic_data[$firstTopic]['subject']; |
||
| 1256 | } |
||
| 1257 | // A subject was selected from the list. |
||
| 1258 | elseif (!empty($topic_data[(int) $_POST['subject']]['subject'])) |
||
| 1259 | $target_subject = $topic_data[(int) $_POST['subject']]['subject']; |
||
| 1260 | // Nothing worked? Just take the subject of the first message. |
||
| 1261 | else |
||
| 1262 | $target_subject = $topic_data[$firstTopic]['subject']; |
||
| 1263 | |||
| 1264 | // Get the first and last message and the number of messages.... |
||
| 1265 | $request = $smcFunc['db_query']('', ' |
||
| 1266 | SELECT approved, MIN(id_msg) AS first_msg, MAX(id_msg) AS last_msg, COUNT(*) AS message_count |
||
| 1267 | FROM {db_prefix}messages |
||
| 1268 | WHERE id_topic IN ({array_int:topics}) |
||
| 1269 | GROUP BY approved |
||
| 1270 | ORDER BY approved DESC', |
||
| 1271 | array( |
||
| 1272 | 'topics' => $topics, |
||
| 1273 | ) |
||
| 1274 | ); |
||
| 1275 | $topic_approved = 1; |
||
| 1276 | $first_msg = 0; |
||
| 1277 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
| 1278 | { |
||
| 1279 | // If this is approved, or is fully unapproved. |
||
| 1280 | if ($row['approved'] || !empty($first_msg)) |
||
| 1281 | { |
||
| 1282 | $first_msg = $row['first_msg']; |
||
| 1283 | $last_msg = $row['last_msg']; |
||
| 1284 | if ($row['approved']) |
||
| 1285 | { |
||
| 1286 | $num_replies = $row['message_count'] - 1; |
||
| 1287 | $num_unapproved = 0; |
||
| 1288 | } |
||
| 1289 | else |
||
| 1290 | { |
||
| 1291 | $topic_approved = 0; |
||
| 1292 | $num_replies = 0; |
||
| 1293 | $num_unapproved = $row['message_count']; |
||
| 1294 | } |
||
| 1295 | } |
||
| 1296 | else |
||
| 1297 | { |
||
| 1298 | // If this has a lower first_msg then the first post is not approved and hence the number of replies was wrong! |
||
| 1299 | if ($first_msg > $row['first_msg']) |
||
| 1300 | { |
||
| 1301 | $first_msg = $row['first_msg']; |
||
| 1302 | $num_replies++; |
||
| 1303 | $topic_approved = 0; |
||
| 1304 | } |
||
| 1305 | $num_unapproved = $row['message_count']; |
||
| 1306 | } |
||
| 1307 | } |
||
| 1308 | $smcFunc['db_free_result']($request); |
||
| 1309 | |||
| 1310 | // Ensure we have a board stat for the target board. |
||
| 1311 | View Code Duplication | if (!isset($boardTotals[$target_board])) |
|
| 1312 | { |
||
| 1313 | $boardTotals[$target_board] = array( |
||
| 1314 | 'posts' => 0, |
||
| 1315 | 'topics' => 0, |
||
| 1316 | 'unapproved_posts' => 0, |
||
| 1317 | 'unapproved_topics' => 0 |
||
| 1318 | ); |
||
| 1319 | } |
||
| 1320 | |||
| 1321 | // Fix the topic count stuff depending on what the new one counts as. |
||
| 1322 | $boardTotals[$target_board][(!$topic_approved) ? 'unapproved_topics' : 'topics']--; |
||
| 1323 | |||
| 1324 | $boardTotals[$target_board]['unapproved_posts'] -= $num_unapproved; |
||
| 1325 | $boardTotals[$target_board]['posts'] -= $topic_approved ? $num_replies + 1 : $num_replies; |
||
| 1326 | |||
| 1327 | // Get the member ID of the first and last message. |
||
| 1328 | $request = $smcFunc['db_query']('', ' |
||
| 1329 | SELECT id_member |
||
| 1330 | FROM {db_prefix}messages |
||
| 1331 | WHERE id_msg IN ({int:first_msg}, {int:last_msg}) |
||
| 1332 | ORDER BY id_msg |
||
| 1333 | LIMIT 2', |
||
| 1334 | array( |
||
| 1335 | 'first_msg' => $first_msg, |
||
| 1336 | 'last_msg' => $last_msg, |
||
| 1337 | ) |
||
| 1338 | ); |
||
| 1339 | list ($member_started) = $smcFunc['db_fetch_row']($request); |
||
| 1340 | list ($member_updated) = $smcFunc['db_fetch_row']($request); |
||
| 1341 | // First and last message are the same, so only row was returned. |
||
| 1342 | if ($member_updated === NULL) |
||
| 1343 | $member_updated = $member_started; |
||
| 1344 | |||
| 1345 | $smcFunc['db_free_result']($request); |
||
| 1346 | |||
| 1347 | // Obtain all the message ids we are going to affect. |
||
| 1348 | $affected_msgs = array(); |
||
| 1349 | $request = $smcFunc['db_query']('', ' |
||
| 1350 | SELECT id_msg |
||
| 1351 | FROM {db_prefix}messages |
||
| 1352 | WHERE id_topic IN ({array_int:topic_list})', |
||
| 1353 | array( |
||
| 1354 | 'topic_list' => $topics, |
||
| 1355 | )); |
||
| 1356 | while ($row = $smcFunc['db_fetch_row']($request)) |
||
| 1357 | $affected_msgs[] = $row[0]; |
||
| 1358 | $smcFunc['db_free_result']($request); |
||
| 1359 | |||
| 1360 | // Assign the first topic ID to be the merged topic. |
||
| 1361 | $id_topic = min($topics); |
||
| 1362 | |||
| 1363 | $deleted_topics = array_diff($topics, array($id_topic)); |
||
| 1364 | $updated_topics = array(); |
||
| 1365 | |||
| 1366 | // Create stub topics out of the remaining topics. |
||
| 1367 | // We don't want the search index data though (For non-redirect merges). |
||
| 1368 | if (!isset($_POST['postRedirect'])) |
||
| 1369 | { |
||
| 1370 | $smcFunc['db_query']('', ' |
||
| 1371 | DELETE FROM {db_prefix}log_search_subjects |
||
| 1372 | WHERE id_topic IN ({array_int:deleted_topics})', |
||
| 1373 | array( |
||
| 1374 | 'deleted_topics' => $deleted_topics, |
||
| 1375 | ) |
||
| 1376 | ); |
||
| 1377 | } |
||
| 1378 | |||
| 1379 | require_once($sourcedir . '/Subs-Post.php'); |
||
| 1380 | $posterOptions = array( |
||
| 1381 | 'id' => $user_info['id'], |
||
| 1382 | 'update_post_count' => false, |
||
| 1383 | ); |
||
| 1384 | |||
| 1385 | // We only need to do this if we're posting redirection topics... |
||
| 1386 | if (isset($_POST['postRedirect'])) |
||
| 1387 | { |
||
| 1388 | $_POST['reason'] = $smcFunc['htmlspecialchars']($_POST['reason'], ENT_QUOTES); |
||
| 1389 | preparsecode($_POST['reason']); |
||
| 1390 | |||
| 1391 | // Add a URL onto the message. |
||
| 1392 | $reason = strtr($_POST['reason'], array( |
||
| 1393 | $txt['movetopic_auto_topic'] => '[iurl=' . $scripturl . '?topic=' . $id_topic . '.0]' . $target_subject . '[/iurl]' |
||
| 1394 | )); |
||
| 1395 | |||
| 1396 | // Automatically remove this MERGED redirection topic in the future? |
||
| 1397 | $redirect_expires = !empty($_POST['redirect_expires']) ? ((int) ($_POST['redirect_expires'] * 60) + time()) : 0; |
||
| 1398 | |||
| 1399 | // Redirect to the MERGED topic from topic list? |
||
| 1400 | $redirect_topic = isset($_POST['redirect_topic']) ? $id_topic : 0; |
||
| 1401 | |||
| 1402 | foreach ($deleted_topics as $this_old_topic) |
||
| 1403 | { |
||
| 1404 | $redirect_subject = sprintf($txt['merged_subject'], $topic_data[$this_old_topic]['subject']); |
||
| 1405 | |||
| 1406 | $msgOptions = array( |
||
| 1407 | 'icon' => 'moved', |
||
| 1408 | 'subject' => $redirect_subject, |
||
| 1409 | 'body' => $reason, |
||
| 1410 | 'approved' => 1, |
||
| 1411 | ); |
||
| 1412 | $topicOptions = array( |
||
| 1413 | 'id' => $this_old_topic, |
||
| 1414 | 'is_approved' => true, |
||
| 1415 | 'lock_mode' => 1, |
||
| 1416 | 'board' => $topic_data[$this_old_topic]['board'], |
||
| 1417 | 'mark_as_read' => true, |
||
| 1418 | ); |
||
| 1419 | |||
| 1420 | // So we have to make the post. We need to do *this* here so we don't foul up indexes later |
||
| 1421 | // and we have to fix them up later once everything else has happened. |
||
| 1422 | if (createPost($msgOptions, $topicOptions, $posterOptions)) |
||
| 1423 | { |
||
| 1424 | $updated_topics[$this_old_topic] = $msgOptions['id']; |
||
| 1425 | } |
||
| 1426 | |||
| 1427 | // Update subject search index |
||
| 1428 | updateStats('subject', $this_old_topic, $redirect_subject); |
||
| 1429 | } |
||
| 1430 | } |
||
| 1431 | |||
| 1432 | // Grab the response prefix (like 'Re: ') in the default forum language. |
||
| 1433 | View Code Duplication | if (!isset($context['response_prefix']) && !($context['response_prefix'] = cache_get_data('response_prefix'))) |
|
| 1434 | { |
||
| 1435 | if ($language === $user_info['language']) |
||
| 1436 | $context['response_prefix'] = $txt['response_prefix']; |
||
| 1437 | else |
||
| 1438 | { |
||
| 1439 | loadLanguage('index', $language, false); |
||
| 1440 | $context['response_prefix'] = $txt['response_prefix']; |
||
| 1441 | loadLanguage('index'); |
||
| 1442 | } |
||
| 1443 | cache_put_data('response_prefix', $context['response_prefix'], 600); |
||
| 1444 | } |
||
| 1445 | |||
| 1446 | // Change the topic IDs of all messages that will be merged. Also adjust subjects if 'enforce subject' was checked. |
||
| 1447 | $smcFunc['db_query']('', ' |
||
| 1448 | UPDATE {db_prefix}messages |
||
| 1449 | SET |
||
| 1450 | id_topic = {int:id_topic}, |
||
| 1451 | id_board = {int:target_board}' . (empty($_POST['enforce_subject']) ? '' : ', |
||
| 1452 | subject = {string:subject}') . ' |
||
| 1453 | WHERE id_topic IN ({array_int:topic_list})' . (!empty($updated_topics) ? ' |
||
| 1454 | AND id_msg NOT IN ({array_int:merge_msg})' : ''), |
||
| 1455 | array( |
||
| 1456 | 'topic_list' => $topics, |
||
| 1457 | 'id_topic' => $id_topic, |
||
| 1458 | 'merge_msg' => $updated_topics, |
||
| 1459 | 'target_board' => $target_board, |
||
| 1460 | 'subject' => $context['response_prefix'] . $target_subject, |
||
| 1461 | ) |
||
| 1462 | ); |
||
| 1463 | |||
| 1464 | // Any reported posts should reflect the new board. |
||
| 1465 | $smcFunc['db_query']('', ' |
||
| 1466 | UPDATE {db_prefix}log_reported |
||
| 1467 | SET |
||
| 1468 | id_topic = {int:id_topic}, |
||
| 1469 | id_board = {int:target_board} |
||
| 1470 | WHERE id_topic IN ({array_int:topics_list})', |
||
| 1471 | array( |
||
| 1472 | 'topics_list' => $topics, |
||
| 1473 | 'id_topic' => $id_topic, |
||
| 1474 | 'target_board' => $target_board, |
||
| 1475 | ) |
||
| 1476 | ); |
||
| 1477 | |||
| 1478 | // Change the subject of the first message... |
||
| 1479 | $smcFunc['db_query']('', ' |
||
| 1480 | UPDATE {db_prefix}messages |
||
| 1481 | SET subject = {string:target_subject} |
||
| 1482 | WHERE id_msg = {int:first_msg}', |
||
| 1483 | array( |
||
| 1484 | 'first_msg' => $first_msg, |
||
| 1485 | 'target_subject' => $target_subject, |
||
| 1486 | ) |
||
| 1487 | ); |
||
| 1488 | |||
| 1489 | // Adjust all calendar events to point to the new topic. |
||
| 1490 | $smcFunc['db_query']('', ' |
||
| 1491 | UPDATE {db_prefix}calendar |
||
| 1492 | SET |
||
| 1493 | id_topic = {int:id_topic}, |
||
| 1494 | id_board = {int:target_board} |
||
| 1495 | WHERE id_topic IN ({array_int:deleted_topics})', |
||
| 1496 | array( |
||
| 1497 | 'deleted_topics' => $deleted_topics, |
||
| 1498 | 'id_topic' => $id_topic, |
||
| 1499 | 'target_board' => $target_board, |
||
| 1500 | ) |
||
| 1501 | ); |
||
| 1502 | |||
| 1503 | // Merge log topic entries. |
||
| 1504 | // The unwatch setting comes from the oldest topic |
||
| 1505 | $request = $smcFunc['db_query']('', ' |
||
| 1506 | SELECT id_member, MIN(id_msg) AS new_id_msg, unwatched |
||
| 1507 | FROM {db_prefix}log_topics |
||
| 1508 | WHERE id_topic IN ({array_int:topics}) |
||
| 1509 | GROUP BY id_member, unwatched', |
||
| 1510 | array( |
||
| 1511 | 'topics' => $topics, |
||
| 1512 | ) |
||
| 1513 | ); |
||
| 1514 | View Code Duplication | if ($smcFunc['db_num_rows']($request) > 0) |
|
| 1515 | { |
||
| 1516 | $replaceEntries = array(); |
||
| 1517 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
| 1518 | $replaceEntries[] = array($row['id_member'], $id_topic, $row['new_id_msg'], $row['unwatched']); |
||
| 1519 | |||
| 1520 | $smcFunc['db_insert']('replace', |
||
| 1521 | '{db_prefix}log_topics', |
||
| 1522 | array('id_member' => 'int', 'id_topic' => 'int', 'id_msg' => 'int', 'unwatched' => 'int'), |
||
| 1523 | $replaceEntries, |
||
| 1524 | array('id_member', 'id_topic') |
||
| 1525 | ); |
||
| 1526 | unset($replaceEntries); |
||
| 1527 | |||
| 1528 | // Get rid of the old log entries. |
||
| 1529 | $smcFunc['db_query']('', ' |
||
| 1530 | DELETE FROM {db_prefix}log_topics |
||
| 1531 | WHERE id_topic IN ({array_int:deleted_topics})', |
||
| 1532 | array( |
||
| 1533 | 'deleted_topics' => $deleted_topics, |
||
| 1534 | ) |
||
| 1535 | ); |
||
| 1536 | } |
||
| 1537 | $smcFunc['db_free_result']($request); |
||
| 1538 | |||
| 1539 | // Merge topic notifications. |
||
| 1540 | $notifications = isset($_POST['notifications']) && is_array($_POST['notifications']) ? array_intersect($topics, $_POST['notifications']) : array(); |
||
| 1541 | if (!empty($notifications)) |
||
| 1542 | { |
||
| 1543 | $request = $smcFunc['db_query']('', ' |
||
| 1544 | SELECT id_member, MAX(sent) AS sent |
||
| 1545 | FROM {db_prefix}log_notify |
||
| 1546 | WHERE id_topic IN ({array_int:topics_list}) |
||
| 1547 | GROUP BY id_member', |
||
| 1548 | array( |
||
| 1549 | 'topics_list' => $notifications, |
||
| 1550 | ) |
||
| 1551 | ); |
||
| 1552 | View Code Duplication | if ($smcFunc['db_num_rows']($request) > 0) |
|
| 1553 | { |
||
| 1554 | $replaceEntries = array(); |
||
| 1555 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
| 1556 | $replaceEntries[] = array($row['id_member'], $id_topic, 0, $row['sent']); |
||
| 1557 | |||
| 1558 | $smcFunc['db_insert']('replace', |
||
| 1559 | '{db_prefix}log_notify', |
||
| 1560 | array('id_member' => 'int', 'id_topic' => 'int', 'id_board' => 'int', 'sent' => 'int'), |
||
| 1561 | $replaceEntries, |
||
| 1562 | array('id_member', 'id_topic', 'id_board') |
||
| 1563 | ); |
||
| 1564 | unset($replaceEntries); |
||
| 1565 | |||
| 1566 | $smcFunc['db_query']('', ' |
||
| 1567 | DELETE FROM {db_prefix}log_topics |
||
| 1568 | WHERE id_topic IN ({array_int:deleted_topics})', |
||
| 1569 | array( |
||
| 1570 | 'deleted_topics' => $deleted_topics, |
||
| 1571 | ) |
||
| 1572 | ); |
||
| 1573 | } |
||
| 1574 | $smcFunc['db_free_result']($request); |
||
| 1575 | } |
||
| 1576 | |||
| 1577 | // Get rid of the redundant polls. |
||
| 1578 | View Code Duplication | if (!empty($deleted_polls)) |
|
| 1579 | { |
||
| 1580 | $smcFunc['db_query']('', ' |
||
| 1581 | DELETE FROM {db_prefix}polls |
||
| 1582 | WHERE id_poll IN ({array_int:deleted_polls})', |
||
| 1583 | array( |
||
| 1584 | 'deleted_polls' => $deleted_polls, |
||
| 1585 | ) |
||
| 1586 | ); |
||
| 1587 | $smcFunc['db_query']('', ' |
||
| 1588 | DELETE FROM {db_prefix}poll_choices |
||
| 1589 | WHERE id_poll IN ({array_int:deleted_polls})', |
||
| 1590 | array( |
||
| 1591 | 'deleted_polls' => $deleted_polls, |
||
| 1592 | ) |
||
| 1593 | ); |
||
| 1594 | $smcFunc['db_query']('', ' |
||
| 1595 | DELETE FROM {db_prefix}log_polls |
||
| 1596 | WHERE id_poll IN ({array_int:deleted_polls})', |
||
| 1597 | array( |
||
| 1598 | 'deleted_polls' => $deleted_polls, |
||
| 1599 | ) |
||
| 1600 | ); |
||
| 1601 | } |
||
| 1602 | |||
| 1603 | // Cycle through each board... |
||
| 1604 | View Code Duplication | foreach ($boardTotals as $id_board => $stats) |
|
| 1605 | { |
||
| 1606 | $smcFunc['db_query']('', ' |
||
| 1607 | UPDATE {db_prefix}boards |
||
| 1608 | SET |
||
| 1609 | num_topics = CASE WHEN {int:topics} > num_topics THEN 0 ELSE num_topics - {int:topics} END, |
||
| 1610 | unapproved_topics = CASE WHEN {int:unapproved_topics} > unapproved_topics THEN 0 ELSE unapproved_topics - {int:unapproved_topics} END, |
||
| 1611 | num_posts = CASE WHEN {int:posts} > num_posts THEN 0 ELSE num_posts - {int:posts} END, |
||
| 1612 | unapproved_posts = CASE WHEN {int:unapproved_posts} > unapproved_posts THEN 0 ELSE unapproved_posts - {int:unapproved_posts} END |
||
| 1613 | WHERE id_board = {int:id_board}', |
||
| 1614 | array( |
||
| 1615 | 'id_board' => $id_board, |
||
| 1616 | 'topics' => $stats['topics'], |
||
| 1617 | 'unapproved_topics' => $stats['unapproved_topics'], |
||
| 1618 | 'posts' => $stats['posts'], |
||
| 1619 | 'unapproved_posts' => $stats['unapproved_posts'], |
||
| 1620 | ) |
||
| 1621 | ); |
||
| 1622 | } |
||
| 1623 | |||
| 1624 | // Determine the board the final topic resides in |
||
| 1625 | $request = $smcFunc['db_query']('', ' |
||
| 1626 | SELECT id_board |
||
| 1627 | FROM {db_prefix}topics |
||
| 1628 | WHERE id_topic = {int:id_topic} |
||
| 1629 | LIMIT 1', |
||
| 1630 | array( |
||
| 1631 | 'id_topic' => $id_topic, |
||
| 1632 | ) |
||
| 1633 | ); |
||
| 1634 | list($id_board) = $smcFunc['db_fetch_row']($request); |
||
| 1635 | $smcFunc['db_free_result']($request); |
||
| 1636 | |||
| 1637 | // Again, only do this if we're redirecting - otherwise delete |
||
| 1638 | if (isset($_POST['postRedirect'])) |
||
| 1639 | { |
||
| 1640 | // Having done all that, now make sure we fix the merge/redirect topics upp before we |
||
| 1641 | // leave here. Specifically: that there are no replies, no unapproved stuff, that the first |
||
| 1642 | // and last posts are the same and so on and so forth. |
||
| 1643 | foreach ($updated_topics as $old_topic => $id_msg) |
||
| 1644 | { |
||
| 1645 | $smcFunc['db_query']('', ' |
||
| 1646 | UPDATE {db_prefix}topics |
||
| 1647 | SET id_first_msg = id_last_msg, |
||
| 1648 | id_member_started = {int:current_user}, |
||
| 1649 | id_member_updated = {int:current_user}, |
||
| 1650 | id_poll = 0, |
||
| 1651 | approved = 1, |
||
| 1652 | num_replies = 0, |
||
| 1653 | unapproved_posts = 0, |
||
| 1654 | id_redirect_topic = {int:redirect_topic}, |
||
| 1655 | redirect_expires = {int:redirect_expires} |
||
| 1656 | WHERE id_topic = {int:old_topic}', |
||
| 1657 | array( |
||
| 1658 | 'current_user' => $user_info['id'], |
||
| 1659 | 'old_topic' => $old_topic, |
||
| 1660 | 'redirect_topic' => $redirect_topic, |
||
| 1661 | 'redirect_expires' => $redirect_expires |
||
| 1662 | ) |
||
| 1663 | ); |
||
| 1664 | } |
||
| 1665 | } |
||
| 1666 | |||
| 1667 | // Ensure we don't accidentally delete the poll we want to keep... |
||
| 1668 | $smcFunc['db_query']('', ' |
||
| 1669 | UPDATE {db_prefix}topics |
||
| 1670 | SET id_poll = 0 |
||
| 1671 | WHERE id_topic IN ({array_int:deleted_topics})', |
||
| 1672 | array( |
||
| 1673 | 'deleted_topics' => $deleted_topics |
||
| 1674 | ) |
||
| 1675 | ); |
||
| 1676 | |||
| 1677 | // Delete any remaining data regarding these topics, this is done before changing the properties of the merged topic (else we get duplicate keys)... |
||
| 1678 | if (!isset($_POST['postRedirect'])) |
||
| 1679 | { |
||
| 1680 | // Remove any remaining info about these topics... |
||
| 1681 | include_once($sourcedir . '/RemoveTopic.php'); |
||
| 1682 | // We do not need to remove the counts of the deleted topics, as we already removed these. |
||
| 1683 | removeTopics($deleted_topics, false, true, false); |
||
| 1684 | } |
||
| 1685 | |||
| 1686 | // Asssign the properties of the newly merged topic. |
||
| 1687 | $smcFunc['db_query']('', ' |
||
| 1688 | UPDATE {db_prefix}topics |
||
| 1689 | SET |
||
| 1690 | id_board = {int:id_board}, |
||
| 1691 | id_member_started = {int:id_member_started}, |
||
| 1692 | id_member_updated = {int:id_member_updated}, |
||
| 1693 | id_first_msg = {int:id_first_msg}, |
||
| 1694 | id_last_msg = {int:id_last_msg}, |
||
| 1695 | id_poll = {int:id_poll}, |
||
| 1696 | num_replies = {int:num_replies}, |
||
| 1697 | unapproved_posts = {int:unapproved_posts}, |
||
| 1698 | num_views = {int:num_views}, |
||
| 1699 | is_sticky = {int:is_sticky}, |
||
| 1700 | approved = {int:approved} |
||
| 1701 | WHERE id_topic = {int:id_topic}', |
||
| 1702 | array( |
||
| 1703 | 'id_board' => $target_board, |
||
| 1704 | 'is_sticky' => $is_sticky, |
||
| 1705 | 'approved' => $topic_approved, |
||
| 1706 | 'id_topic' => $id_topic, |
||
| 1707 | 'id_member_started' => $member_started, |
||
| 1708 | 'id_member_updated' => $member_updated, |
||
| 1709 | 'id_first_msg' => $first_msg, |
||
| 1710 | 'id_last_msg' => $last_msg, |
||
| 1711 | 'id_poll' => $target_poll, |
||
| 1712 | 'num_replies' => $num_replies, |
||
| 1713 | 'unapproved_posts' => $num_unapproved, |
||
| 1714 | 'num_views' => $num_views, |
||
| 1715 | ) |
||
| 1716 | ); |
||
| 1717 | |||
| 1718 | // Update all the statistics. |
||
| 1719 | updateStats('topic'); |
||
| 1720 | updateStats('subject', $id_topic, $target_subject); |
||
| 1721 | updateLastMessages($boards); |
||
| 1722 | |||
| 1723 | logAction('merge', array('topic' => $id_topic, 'board' => $id_board)); |
||
| 1724 | |||
| 1725 | // Notify people that these topics have been merged? |
||
| 1726 | sendNotifications($id_topic, 'merge'); |
||
| 1727 | |||
| 1728 | // If there's a search index that needs updating, update it... |
||
| 1729 | require_once($sourcedir . '/Search.php'); |
||
| 1730 | $searchAPI = findSearchAPI(); |
||
| 1731 | if (is_callable(array($searchAPI, 'topicMerge'))) |
||
| 1732 | $searchAPI->topicMerge($id_topic, $topics, $affected_msgs, empty($_POST['enforce_subject']) ? null : array($context['response_prefix'], $target_subject)); |
||
| 1733 | |||
| 1734 | // Merging is the sort of thing an external CMS might want to know about |
||
| 1735 | $merged_topic = array( |
||
| 1736 | 'id_board' => $target_board, |
||
| 1737 | 'is_sticky' => $is_sticky, |
||
| 1738 | 'approved' => $topic_approved, |
||
| 1739 | 'id_topic' => $id_topic, |
||
| 1740 | 'id_member_started' => $member_started, |
||
| 1741 | 'id_member_updated' => $member_updated, |
||
| 1742 | 'id_first_msg' => $first_msg, |
||
| 1743 | 'id_last_msg' => $last_msg, |
||
| 1744 | 'id_poll' => $target_poll, |
||
| 1745 | 'num_replies' => $num_replies, |
||
| 1746 | 'unapproved_posts' => $num_unapproved, |
||
| 1747 | 'num_views' => $num_views, |
||
| 1748 | 'subject' => $target_subject, |
||
| 1749 | ); |
||
| 1750 | call_integration_hook('integrate_merge_topic', array($merged_topic, $updated_topics, $deleted_topics, $deleted_polls)); |
||
| 1751 | |||
| 1752 | // Send them to the all done page. |
||
| 1753 | redirectexit('action=mergetopics;sa=done;to=' . $id_topic . ';targetboard=' . $target_board); |
||
| 1754 | } |
||
| 1755 | |||
| 1773 | ?> |
This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.
The variable may have been renamed without also renaming all references.