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