PageRenderTime 55ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/php/Sources/ManageMaintenance.php

https://github.com/dekoza/openshift-smf-2.0.7
PHP | 1727 lines | 1253 code | 220 blank | 254 comment | 157 complexity | af545e5cfd6d0930acb13c5f8936648d MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Simple Machines Forum (SMF)
  4. *
  5. * @package SMF
  6. * @author Simple Machines http://www.simplemachines.org
  7. * @copyright 2011 Simple Machines
  8. * @license http://www.simplemachines.org/about/smf/license.php BSD
  9. *
  10. * @version 2.0.7
  11. */
  12. if (!defined('SMF'))
  13. die('Hacking attempt...');
  14. /* /!!!
  15. void ManageMaintenance()
  16. // !!!
  17. void MaintainDatabase()
  18. // !!!
  19. void MaintainMembers()
  20. // !!!
  21. void MaintainTopics()
  22. // !!!
  23. void MaintainCleanCache()
  24. // !!!
  25. void MaintainFindFixErrors()
  26. // !!!
  27. void MaintainEmptyUnimportantLogs()
  28. // !!!
  29. void ConvertUtf8()
  30. - converts the data and database tables to UTF-8 character set.
  31. - requires the admin_forum permission.
  32. - uses the convert_utf8 sub template of the Admin template.
  33. - only works if UTF-8 is not the global character set.
  34. - supports all character sets used by SMF's language files.
  35. - redirects to ?action=admin;area=maintain after finishing.
  36. - is linked from the maintenance screen (if applicable).
  37. - accessed by ?action=admin;area=maintain;sa=database;activity=convertutf8.
  38. void ConvertEntities()
  39. - converts HTML-entities to UTF-8 characters.
  40. - requires the admin_forum permission.
  41. - uses the convert_entities sub template of the Admin template.
  42. - only works if UTF-8 has been set as database and global character set.
  43. - is divided in steps of 10 seconds.
  44. - is linked from the maintenance screen (if applicable).
  45. - accessed by ?action=admin;area=maintain;sa=database;activity=convertentities.
  46. void OptimizeTables()
  47. - optimizes all tables in the database and lists how much was saved.
  48. - requires the admin_forum permission.
  49. - uses the rawdata sub template (built in.)
  50. - shows as the maintain_forum admin area.
  51. - updates the optimize scheduled task such that the tables are not
  52. automatically optimized again too soon.
  53. - accessed from ?action=admin;area=maintain;sa=database;activity=optimize.
  54. void AdminBoardRecount()
  55. - recounts many forum totals that can be recounted automatically
  56. without harm.
  57. - requires the admin_forum permission.
  58. - shows the maintain_forum admin area.
  59. - fixes topics with wrong num_replies.
  60. - updates the num_posts and num_topics of all boards.
  61. - recounts instant_messages but not unread_messages.
  62. - repairs messages pointing to boards with topics pointing to
  63. other boards.
  64. - updates the last message posted in boards and children.
  65. - updates member count, latest member, topic count, and message count.
  66. - redirects back to ?action=admin;area=maintain when complete.
  67. - accessed via ?action=admin;area=maintain;sa=database;activity=recount.
  68. void VersionDetail()
  69. - parses the comment headers in all files for their version information
  70. and outputs that for some javascript to check with simplemacines.org.
  71. - does not connect directly with simplemachines.org, but rather
  72. expects the client to.
  73. - requires the admin_forum permission.
  74. - uses the view_versions admin area.
  75. - loads the view_versions sub template (in the Admin template.)
  76. - accessed through ?action=admin;area=maintain;sa=routine;activity=version.
  77. void MaintainReattributePosts()
  78. // !!!
  79. void MaintainDownloadBackup()
  80. // !!!
  81. void MaintainPurgeInactiveMembers()
  82. // !!!
  83. void MaintainRemoveOldPosts(bool do_action = true)
  84. // !!!
  85. mixed MaintainMassMoveTopics()
  86. - Moves topics from one board to another.
  87. - User the not_done template to pause the process.
  88. */
  89. // The maintenance access point.
  90. function ManageMaintenance()
  91. {
  92. global $txt, $modSettings, $scripturl, $context, $options;
  93. // You absolutely must be an admin by here!
  94. isAllowedTo('admin_forum');
  95. // Need something to talk about?
  96. loadLanguage('ManageMaintenance');
  97. loadTemplate('ManageMaintenance');
  98. // This uses admin tabs - as it should!
  99. $context[$context['admin_menu_name']]['tab_data'] = array(
  100. 'title' => $txt['maintain_title'],
  101. 'description' => $txt['maintain_info'],
  102. 'tabs' => array(
  103. 'routine' => array(),
  104. 'database' => array(),
  105. 'members' => array(),
  106. 'topics' => array(),
  107. ),
  108. );
  109. // So many things you can do - but frankly I won't let you - just these!
  110. $subActions = array(
  111. 'routine' => array(
  112. 'function' => 'MaintainRoutine',
  113. 'template' => 'maintain_routine',
  114. 'activities' => array(
  115. 'version' => 'VersionDetail',
  116. 'repair' => 'MaintainFindFixErrors',
  117. 'recount' => 'AdminBoardRecount',
  118. 'logs' => 'MaintainEmptyUnimportantLogs',
  119. 'cleancache' => 'MaintainCleanCache',
  120. ),
  121. ),
  122. 'database' => array(
  123. 'function' => 'MaintainDatabase',
  124. 'template' => 'maintain_database',
  125. 'activities' => array(
  126. 'optimize' => 'OptimizeTables',
  127. 'backup' => 'MaintainDownloadBackup',
  128. 'convertentities' => 'ConvertEntities',
  129. 'convertutf8' => 'ConvertUtf8',
  130. ),
  131. ),
  132. 'members' => array(
  133. 'function' => 'MaintainMembers',
  134. 'template' => 'maintain_members',
  135. 'activities' => array(
  136. 'reattribute' => 'MaintainReattributePosts',
  137. 'purgeinactive' => 'MaintainPurgeInactiveMembers',
  138. ),
  139. ),
  140. 'topics' => array(
  141. 'function' => 'MaintainTopics',
  142. 'template' => 'maintain_topics',
  143. 'activities' => array(
  144. 'massmove' => 'MaintainMassMoveTopics',
  145. 'pruneold' => 'MaintainRemoveOldPosts',
  146. ),
  147. ),
  148. 'destroy' => array(
  149. 'function' => 'Destroy',
  150. 'activities' => array(),
  151. ),
  152. );
  153. // Yep, sub-action time!
  154. if (isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]))
  155. $subAction = $_REQUEST['sa'];
  156. else
  157. $subAction = 'routine';
  158. // Doing something special?
  159. if (isset($_REQUEST['activity']) && isset($subActions[$subAction]['activities'][$_REQUEST['activity']]))
  160. $activity = $_REQUEST['activity'];
  161. // Set a few things.
  162. $context['page_title'] = $txt['maintain_title'];
  163. $context['sub_action'] = $subAction;
  164. $context['sub_template'] = !empty($subActions[$subAction]['template']) ? $subActions[$subAction]['template'] : '';
  165. // Finally fall through to what we are doing.
  166. $subActions[$subAction]['function']();
  167. // Any special activity?
  168. if (isset($activity))
  169. $subActions[$subAction]['activities'][$activity]();
  170. //converted to UTF-8? show a small maintenance info
  171. if (isset($_GET['done']) && $_GET['done'] == 'convertutf8')
  172. $context['maintenance_finished'] = $txt['utf8_title'];
  173. }
  174. // Supporting function for the database maintenance area.
  175. function MaintainDatabase()
  176. {
  177. global $context, $db_type, $db_character_set, $modSettings, $smcFunc, $txt;
  178. // Show some conversion options?
  179. $context['convert_utf8'] = $db_type == 'mysql' && (!isset($db_character_set) || $db_character_set !== 'utf8' || empty($modSettings['global_character_set']) || $modSettings['global_character_set'] !== 'UTF-8') && version_compare('4.1.2', preg_replace('~\-.+?$~', '', $smcFunc['db_server_info']())) <= 0;
  180. $context['convert_entities'] = $db_type == 'mysql' && isset($db_character_set, $modSettings['global_character_set']) && $db_character_set === 'utf8' && $modSettings['global_character_set'] === 'UTF-8';
  181. if (isset($_GET['done']) && $_GET['done'] == 'convertutf8')
  182. $context['maintenance_finished'] = $txt['utf8_title'];
  183. if (isset($_GET['done']) && $_GET['done'] == 'convertentities')
  184. $context['maintenance_finished'] = $txt['entity_convert_title'];
  185. }
  186. // Supporting function for the routine maintenance area.
  187. function MaintainRoutine()
  188. {
  189. global $context, $txt;
  190. if (isset($_GET['done']) && $_GET['done'] == 'recount')
  191. $context['maintenance_finished'] = $txt['maintain_recount'];
  192. }
  193. // Supporting function for the members maintenance area.
  194. function MaintainMembers()
  195. {
  196. global $context, $smcFunc, $txt;
  197. // Get membergroups - for deleting members and the like.
  198. $result = $smcFunc['db_query']('', '
  199. SELECT id_group, group_name
  200. FROM {db_prefix}membergroups',
  201. array(
  202. )
  203. );
  204. $context['membergroups'] = array(
  205. array(
  206. 'id' => 0,
  207. 'name' => $txt['maintain_members_ungrouped']
  208. ),
  209. );
  210. while ($row = $smcFunc['db_fetch_assoc']($result))
  211. {
  212. $context['membergroups'][] = array(
  213. 'id' => $row['id_group'],
  214. 'name' => $row['group_name']
  215. );
  216. }
  217. $smcFunc['db_free_result']($result);
  218. }
  219. // Supporting function for the topics maintenance area.
  220. function MaintainTopics()
  221. {
  222. global $context, $smcFunc, $txt;
  223. // Let's load up the boards in case they are useful.
  224. $result = $smcFunc['db_query']('order_by_board_order', '
  225. SELECT b.id_board, b.name, b.child_level, c.name AS cat_name, c.id_cat
  226. FROM {db_prefix}boards AS b
  227. LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)
  228. WHERE {query_see_board}
  229. AND redirect = {string:blank_redirect}',
  230. array(
  231. 'blank_redirect' => '',
  232. )
  233. );
  234. $context['categories'] = array();
  235. while ($row = $smcFunc['db_fetch_assoc']($result))
  236. {
  237. if (!isset($context['categories'][$row['id_cat']]))
  238. $context['categories'][$row['id_cat']] = array(
  239. 'name' => $row['cat_name'],
  240. 'boards' => array()
  241. );
  242. $context['categories'][$row['id_cat']]['boards'][] = array(
  243. 'id' => $row['id_board'],
  244. 'name' => $row['name'],
  245. 'child_level' => $row['child_level']
  246. );
  247. }
  248. $smcFunc['db_free_result']($result);
  249. if (isset($_GET['done']) && $_GET['done'] == 'purgeold')
  250. $context['maintenance_finished'] = $txt['maintain_old'];
  251. elseif (isset($_GET['done']) && $_GET['done'] == 'massmove')
  252. $context['maintenance_finished'] = $txt['move_topics_maintenance'];
  253. }
  254. // Find and fix all errors.
  255. function MaintainFindFixErrors()
  256. {
  257. global $sourcedir;
  258. require_once($sourcedir . '/RepairBoards.php');
  259. RepairBoards();
  260. }
  261. // Wipes the whole cache directory.
  262. function MaintainCleanCache()
  263. {
  264. global $context, $txt;
  265. // Just wipe the whole cache directory!
  266. clean_cache();
  267. $context['maintenance_finished'] = $txt['maintain_cache'];
  268. }
  269. // Empties all uninmportant logs
  270. function MaintainEmptyUnimportantLogs()
  271. {
  272. global $context, $smcFunc, $txt;
  273. checkSession();
  274. // No one's online now.... MUHAHAHAHA :P.
  275. $smcFunc['db_query']('', '
  276. DELETE FROM {db_prefix}log_online');
  277. // Dump the banning logs.
  278. $smcFunc['db_query']('', '
  279. DELETE FROM {db_prefix}log_banned');
  280. // Start id_error back at 0 and dump the error log.
  281. $smcFunc['db_query']('truncate_table', '
  282. TRUNCATE {db_prefix}log_errors');
  283. // Clear out the spam log.
  284. $smcFunc['db_query']('', '
  285. DELETE FROM {db_prefix}log_floodcontrol');
  286. // Clear out the karma actions.
  287. $smcFunc['db_query']('', '
  288. DELETE FROM {db_prefix}log_karma');
  289. // Last but not least, the search logs!
  290. $smcFunc['db_query']('truncate_table', '
  291. TRUNCATE {db_prefix}log_search_topics');
  292. $smcFunc['db_query']('truncate_table', '
  293. TRUNCATE {db_prefix}log_search_messages');
  294. $smcFunc['db_query']('truncate_table', '
  295. TRUNCATE {db_prefix}log_search_results');
  296. updateSettings(array('search_pointer' => 0));
  297. $context['maintenance_finished'] = $txt['maintain_logs'];
  298. }
  299. // Oh noes!
  300. function Destroy()
  301. {
  302. global $context;
  303. echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  304. <html xmlns="http://www.w3.org/1999/xhtml"', $context['right_to_left'] ? ' dir="rtl"' : '', '><head><title>', $context['forum_name_html_safe'], ' deleted!</title></head>
  305. <body style="background-color: orange; font-family: arial, sans-serif; text-align: center;">
  306. <div style="margin-top: 8%; font-size: 400%; color: black;">Oh my, you killed ', $context['forum_name_html_safe'], '!</div>
  307. <div style="margin-top: 7%; font-size: 500%; color: red;"><strong>You lazy bum!</strong></div>
  308. </body></html>';
  309. obExit(false);
  310. }
  311. // Convert both data and database tables to UTF-8 character set.
  312. function ConvertUtf8()
  313. {
  314. global $scripturl, $context, $txt, $language, $db_character_set;
  315. global $modSettings, $user_info, $sourcedir, $smcFunc, $db_prefix;
  316. // Show me your badge!
  317. isAllowedTo('admin_forum');
  318. // The character sets used in SMF's language files with their db equivalent.
  319. $charsets = array(
  320. // Chinese-traditional.
  321. 'big5' => 'big5',
  322. // Chinese-simplified.
  323. 'gbk' => 'gbk',
  324. // West European.
  325. 'ISO-8859-1' => 'latin1',
  326. // Romanian.
  327. 'ISO-8859-2' => 'latin2',
  328. // Turkish.
  329. 'ISO-8859-9' => 'latin5',
  330. // West European with Euro sign.
  331. 'ISO-8859-15' => 'latin9',
  332. // Thai.
  333. 'tis-620' => 'tis620',
  334. // Persian, Chinese, etc.
  335. 'UTF-8' => 'utf8',
  336. // Russian.
  337. 'windows-1251' => 'cp1251',
  338. // Greek.
  339. 'windows-1253' => 'utf8',
  340. // Hebrew.
  341. 'windows-1255' => 'utf8',
  342. // Arabic.
  343. 'windows-1256' => 'cp1256',
  344. );
  345. // Get a list of character sets supported by your MySQL server.
  346. $request = $smcFunc['db_query']('', '
  347. SHOW CHARACTER SET',
  348. array(
  349. )
  350. );
  351. $db_charsets = array();
  352. while ($row = $smcFunc['db_fetch_assoc']($request))
  353. $db_charsets[] = $row['Charset'];
  354. $smcFunc['db_free_result']($request);
  355. // Character sets supported by both MySQL and SMF's language files.
  356. $charsets = array_intersect($charsets, $db_charsets);
  357. // This is for the first screen telling backups is good.
  358. if (!isset($_POST['proceed']))
  359. {
  360. // Character set conversions are only supported as of MySQL 4.1.2.
  361. if (version_compare('4.1.2', preg_replace('~\-.+?$~', '', $smcFunc['db_server_info']())) > 0)
  362. fatal_lang_error('utf8_db_version_too_low');
  363. // Use the messages.body column as indicator for the database charset.
  364. $request = $smcFunc['db_query']('', '
  365. SHOW FULL COLUMNS
  366. FROM {db_prefix}messages
  367. LIKE {string:body_like}',
  368. array(
  369. 'body_like' => 'body',
  370. )
  371. );
  372. $column_info = $smcFunc['db_fetch_assoc']($request);
  373. $smcFunc['db_free_result']($request);
  374. // A collation looks like latin1_swedish. We only need the character set.
  375. list($context['database_charset']) = explode('_', $column_info['Collation']);
  376. $context['database_charset'] = in_array($context['database_charset'], $charsets) ? array_search($context['database_charset'], $charsets) : $context['database_charset'];
  377. // No need to convert to UTF-8 if it already is.
  378. if ($db_character_set === 'utf8' && !empty($modSettings['global_character_set']) && $modSettings['global_character_set'] === 'UTF-8')
  379. fatal_lang_error('utf8_already_utf8');
  380. // Cannot do conversion if using a fulltext index
  381. if (!empty($modSettings['search_index']) && $modSettings['search_index'] == 'fulltext')
  382. fatal_lang_error('utf8_cannot_convert_fulltext');
  383. // Grab the character set from the default language file.
  384. loadLanguage('index', $language, true);
  385. $context['charset_detected'] = $txt['lang_character_set'];
  386. $context['charset_about_detected'] = sprintf($txt['utf8_detected_charset'], $language, $context['charset_detected']);
  387. // Go back to your own language.
  388. loadLanguage('index', $user_info['language'], true);
  389. // Show a warning if the character set seems not to be supported.
  390. if (!isset($charsets[strtr(strtolower($context['charset_detected']), array('utf' => 'UTF', 'iso' => 'ISO'))]))
  391. {
  392. $context['charset_warning'] = sprintf($txt['utf8_charset_not_supported'], $txt['lang_character_set']);
  393. // Default to ISO-8859-1.
  394. $context['charset_detected'] = 'ISO-8859-1';
  395. }
  396. $context['charset_list'] = array_keys($charsets);
  397. $context['page_title'] = $txt['utf8_title'];
  398. $context['sub_template'] = 'convert_utf8';
  399. return;
  400. }
  401. // After this point we're starting the conversion. But first: session check.
  402. checkSession();
  403. // Translation table for the character sets not native for MySQL.
  404. $translation_tables = array(
  405. 'windows-1255' => array(
  406. '0x81' => '\'\'', '0x8A' => '\'\'', '0x8C' => '\'\'',
  407. '0x8D' => '\'\'', '0x8E' => '\'\'', '0x8F' => '\'\'',
  408. '0x90' => '\'\'', '0x9A' => '\'\'', '0x9C' => '\'\'',
  409. '0x9D' => '\'\'', '0x9E' => '\'\'', '0x9F' => '\'\'',
  410. '0xCA' => '\'\'', '0xD9' => '\'\'', '0xDA' => '\'\'',
  411. '0xDB' => '\'\'', '0xDC' => '\'\'', '0xDD' => '\'\'',
  412. '0xDE' => '\'\'', '0xDF' => '\'\'', '0xFB' => '\'\'',
  413. '0xFC' => '\'\'', '0xFF' => '\'\'', '0xC2' => '0xFF',
  414. '0x80' => '0xFC', '0xE2' => '0xFB', '0xA0' => '0xC2A0',
  415. '0xA1' => '0xC2A1', '0xA2' => '0xC2A2', '0xA3' => '0xC2A3',
  416. '0xA5' => '0xC2A5', '0xA6' => '0xC2A6', '0xA7' => '0xC2A7',
  417. '0xA8' => '0xC2A8', '0xA9' => '0xC2A9', '0xAB' => '0xC2AB',
  418. '0xAC' => '0xC2AC', '0xAD' => '0xC2AD', '0xAE' => '0xC2AE',
  419. '0xAF' => '0xC2AF', '0xB0' => '0xC2B0', '0xB1' => '0xC2B1',
  420. '0xB2' => '0xC2B2', '0xB3' => '0xC2B3', '0xB4' => '0xC2B4',
  421. '0xB5' => '0xC2B5', '0xB6' => '0xC2B6', '0xB7' => '0xC2B7',
  422. '0xB8' => '0xC2B8', '0xB9' => '0xC2B9', '0xBB' => '0xC2BB',
  423. '0xBC' => '0xC2BC', '0xBD' => '0xC2BD', '0xBE' => '0xC2BE',
  424. '0xBF' => '0xC2BF', '0xD7' => '0xD7B3', '0xD1' => '0xD781',
  425. '0xD4' => '0xD7B0', '0xD5' => '0xD7B1', '0xD6' => '0xD7B2',
  426. '0xE0' => '0xD790', '0xEA' => '0xD79A', '0xEC' => '0xD79C',
  427. '0xED' => '0xD79D', '0xEE' => '0xD79E', '0xEF' => '0xD79F',
  428. '0xF0' => '0xD7A0', '0xF1' => '0xD7A1', '0xF2' => '0xD7A2',
  429. '0xF3' => '0xD7A3', '0xF5' => '0xD7A5', '0xF6' => '0xD7A6',
  430. '0xF7' => '0xD7A7', '0xF8' => '0xD7A8', '0xF9' => '0xD7A9',
  431. '0x82' => '0xE2809A', '0x84' => '0xE2809E', '0x85' => '0xE280A6',
  432. '0x86' => '0xE280A0', '0x87' => '0xE280A1', '0x89' => '0xE280B0',
  433. '0x8B' => '0xE280B9', '0x93' => '0xE2809C', '0x94' => '0xE2809D',
  434. '0x95' => '0xE280A2', '0x97' => '0xE28094', '0x99' => '0xE284A2',
  435. '0xC0' => '0xD6B0', '0xC1' => '0xD6B1', '0xC3' => '0xD6B3',
  436. '0xC4' => '0xD6B4', '0xC5' => '0xD6B5', '0xC6' => '0xD6B6',
  437. '0xC7' => '0xD6B7', '0xC8' => '0xD6B8', '0xC9' => '0xD6B9',
  438. '0xCB' => '0xD6BB', '0xCC' => '0xD6BC', '0xCD' => '0xD6BD',
  439. '0xCE' => '0xD6BE', '0xCF' => '0xD6BF', '0xD0' => '0xD780',
  440. '0xD2' => '0xD782', '0xE3' => '0xD793', '0xE4' => '0xD794',
  441. '0xE5' => '0xD795', '0xE7' => '0xD797', '0xE9' => '0xD799',
  442. '0xFD' => '0xE2808E', '0xFE' => '0xE2808F', '0x92' => '0xE28099',
  443. '0x83' => '0xC692', '0xD3' => '0xD783', '0x88' => '0xCB86',
  444. '0x98' => '0xCB9C', '0x91' => '0xE28098', '0x96' => '0xE28093',
  445. '0xBA' => '0xC3B7', '0x9B' => '0xE280BA', '0xAA' => '0xC397',
  446. '0xA4' => '0xE282AA', '0xE1' => '0xD791', '0xE6' => '0xD796',
  447. '0xE8' => '0xD798', '0xEB' => '0xD79B', '0xF4' => '0xD7A4',
  448. '0xFA' => '0xD7AA', '0xFF' => '0xD6B2', '0xFC' => '0xE282AC',
  449. '0xFB' => '0xD792',
  450. ),
  451. 'windows-1253' => array(
  452. '0x81' => '\'\'', '0x88' => '\'\'', '0x8A' => '\'\'',
  453. '0x8C' => '\'\'', '0x8D' => '\'\'', '0x8E' => '\'\'',
  454. '0x8F' => '\'\'', '0x90' => '\'\'', '0x98' => '\'\'',
  455. '0x9A' => '\'\'', '0x9C' => '\'\'', '0x9D' => '\'\'',
  456. '0x9E' => '\'\'', '0x9F' => '\'\'', '0xAA' => '\'\'',
  457. '0xD2' => '\'\'', '0xFF' => '\'\'', '0xCE' => '0xCE9E',
  458. '0xB8' => '0xCE88', '0xBA' => '0xCE8A', '0xBC' => '0xCE8C',
  459. '0xBE' => '0xCE8E', '0xBF' => '0xCE8F', '0xC0' => '0xCE90',
  460. '0xC8' => '0xCE98', '0xCA' => '0xCE9A', '0xCC' => '0xCE9C',
  461. '0xCD' => '0xCE9D', '0xCF' => '0xCE9F', '0xDA' => '0xCEAA',
  462. '0xE8' => '0xCEB8', '0xEA' => '0xCEBA', '0xEC' => '0xCEBC',
  463. '0xEE' => '0xCEBE', '0xEF' => '0xCEBF', '0xC2' => '0xFF',
  464. '0xBD' => '0xC2BD', '0xED' => '0xCEBD', '0xB2' => '0xC2B2',
  465. '0xA0' => '0xC2A0', '0xA3' => '0xC2A3', '0xA4' => '0xC2A4',
  466. '0xA5' => '0xC2A5', '0xA6' => '0xC2A6', '0xA7' => '0xC2A7',
  467. '0xA8' => '0xC2A8', '0xA9' => '0xC2A9', '0xAB' => '0xC2AB',
  468. '0xAC' => '0xC2AC', '0xAD' => '0xC2AD', '0xAE' => '0xC2AE',
  469. '0xB0' => '0xC2B0', '0xB1' => '0xC2B1', '0xB3' => '0xC2B3',
  470. '0xB5' => '0xC2B5', '0xB6' => '0xC2B6', '0xB7' => '0xC2B7',
  471. '0xBB' => '0xC2BB', '0xE2' => '0xCEB2', '0x80' => '0xD2',
  472. '0x82' => '0xE2809A', '0x84' => '0xE2809E', '0x85' => '0xE280A6',
  473. '0x86' => '0xE280A0', '0xA1' => '0xCE85', '0xA2' => '0xCE86',
  474. '0x87' => '0xE280A1', '0x89' => '0xE280B0', '0xB9' => '0xCE89',
  475. '0x8B' => '0xE280B9', '0x91' => '0xE28098', '0x99' => '0xE284A2',
  476. '0x92' => '0xE28099', '0x93' => '0xE2809C', '0x94' => '0xE2809D',
  477. '0x95' => '0xE280A2', '0x96' => '0xE28093', '0x97' => '0xE28094',
  478. '0x9B' => '0xE280BA', '0xAF' => '0xE28095', '0xB4' => '0xCE84',
  479. '0xC1' => '0xCE91', '0xC3' => '0xCE93', '0xC4' => '0xCE94',
  480. '0xC5' => '0xCE95', '0xC6' => '0xCE96', '0x83' => '0xC692',
  481. '0xC7' => '0xCE97', '0xC9' => '0xCE99', '0xCB' => '0xCE9B',
  482. '0xD0' => '0xCEA0', '0xD1' => '0xCEA1', '0xD3' => '0xCEA3',
  483. '0xD4' => '0xCEA4', '0xD5' => '0xCEA5', '0xD6' => '0xCEA6',
  484. '0xD7' => '0xCEA7', '0xD8' => '0xCEA8', '0xD9' => '0xCEA9',
  485. '0xDB' => '0xCEAB', '0xDC' => '0xCEAC', '0xDD' => '0xCEAD',
  486. '0xDE' => '0xCEAE', '0xDF' => '0xCEAF', '0xE0' => '0xCEB0',
  487. '0xE1' => '0xCEB1', '0xE3' => '0xCEB3', '0xE4' => '0xCEB4',
  488. '0xE5' => '0xCEB5', '0xE6' => '0xCEB6', '0xE7' => '0xCEB7',
  489. '0xE9' => '0xCEB9', '0xEB' => '0xCEBB', '0xF0' => '0xCF80',
  490. '0xF1' => '0xCF81', '0xF2' => '0xCF82', '0xF3' => '0xCF83',
  491. '0xF4' => '0xCF84', '0xF5' => '0xCF85', '0xF6' => '0xCF86',
  492. '0xF7' => '0xCF87', '0xF8' => '0xCF88', '0xF9' => '0xCF89',
  493. '0xFA' => '0xCF8A', '0xFB' => '0xCF8B', '0xFC' => '0xCF8C',
  494. '0xFD' => '0xCF8D', '0xFE' => '0xCF8E', '0xFF' => '0xCE92',
  495. '0xD2' => '0xE282AC',
  496. ),
  497. );
  498. // Make some preparations.
  499. if (isset($translation_tables[$_POST['src_charset']]))
  500. {
  501. $replace = '%field%';
  502. foreach ($translation_tables[$_POST['src_charset']] as $from => $to)
  503. $replace = 'REPLACE(' . $replace . ', ' . $from . ', ' . $to . ')';
  504. }
  505. // Grab a list of tables.
  506. if (preg_match('~^`(.+?)`\.(.+?)$~', $db_prefix, $match) === 1)
  507. $queryTables = $smcFunc['db_query']('', '
  508. SHOW TABLE STATUS
  509. FROM `' . strtr($match[1], array('`' => '')) . '`
  510. LIKE {string:table_name}',
  511. array(
  512. 'table_name' => str_replace('_', '\_', $match[2]) . '%',
  513. )
  514. );
  515. else
  516. $queryTables = $smcFunc['db_query']('', '
  517. SHOW TABLE STATUS
  518. LIKE {string:table_name}',
  519. array(
  520. 'table_name' => str_replace('_', '\_', $db_prefix) . '%',
  521. )
  522. );
  523. while ($table_info = $smcFunc['db_fetch_assoc']($queryTables))
  524. {
  525. // Just to make sure it doesn't time out.
  526. if (function_exists('apache_reset_timeout'))
  527. @apache_reset_timeout();
  528. $table_charsets = array();
  529. // Loop through each column.
  530. $queryColumns = $smcFunc['db_query']('', '
  531. SHOW FULL COLUMNS
  532. FROM ' . $table_info['Name'],
  533. array(
  534. )
  535. );
  536. while ($column_info = $smcFunc['db_fetch_assoc']($queryColumns))
  537. {
  538. // Only text'ish columns have a character set and need converting.
  539. if (strpos($column_info['Type'], 'text') !== false || strpos($column_info['Type'], 'char') !== false)
  540. {
  541. $collation = empty($column_info['Collation']) || $column_info['Collation'] === 'NULL' ? $table_info['Collation'] : $column_info['Collation'];
  542. if (!empty($collation) && $collation !== 'NULL')
  543. {
  544. list($charset) = explode('_', $collation);
  545. if (!isset($table_charsets[$charset]))
  546. $table_charsets[$charset] = array();
  547. $table_charsets[$charset][] = $column_info;
  548. }
  549. }
  550. }
  551. $smcFunc['db_free_result']($queryColumns);
  552. // Only change the column if the data doesn't match the current charset.
  553. if ((count($table_charsets) === 1 && key($table_charsets) !== $charsets[$_POST['src_charset']]) || count($table_charsets) > 1)
  554. {
  555. $updates_blob = '';
  556. $updates_text = '';
  557. foreach ($table_charsets as $charset => $columns)
  558. {
  559. if ($charset !== $charsets[$_POST['src_charset']])
  560. {
  561. foreach ($columns as $column)
  562. {
  563. $updates_blob .= '
  564. CHANGE COLUMN ' . $column['Field'] . ' ' . $column['Field'] . ' ' . strtr($column['Type'], array('text' => 'blob', 'char' => 'binary')) . ($column['Null'] === 'YES' ? ' NULL' : ' NOT NULL') . (strpos($column['Type'], 'char') === false ? '' : ' default \'' . $column['Default'] . '\'') . ',';
  565. $updates_text .= '
  566. CHANGE COLUMN ' . $column['Field'] . ' ' . $column['Field'] . ' ' . $column['Type'] . ' CHARACTER SET ' . $charsets[$_POST['src_charset']] . ($column['Null'] === 'YES' ? '' : ' NOT NULL') . (strpos($column['Type'], 'char') === false ? '' : ' default \'' . $column['Default'] . '\'') . ',';
  567. }
  568. }
  569. }
  570. // Change the columns to binary form.
  571. $smcFunc['db_query']('', '
  572. ALTER TABLE {raw:table_name}{raw:updates_blob}',
  573. array(
  574. 'table_name' => $table_info['Name'],
  575. 'updates_blob' => substr($updates_blob, 0, -1),
  576. )
  577. );
  578. // Convert the character set if MySQL has no native support for it.
  579. if (isset($translation_tables[$_POST['src_charset']]))
  580. {
  581. $update = '';
  582. foreach ($table_charsets as $charset => $columns)
  583. foreach ($columns as $column)
  584. $update .= '
  585. ' . $column['Field'] . ' = ' . strtr($replace, array('%field%' => $column['Field'])) . ',';
  586. $smcFunc['db_query']('', '
  587. UPDATE {raw:table_name}
  588. SET {raw:updates}',
  589. array(
  590. 'table_name' => $table_info['Name'],
  591. 'updates' => substr($update, 0, -1),
  592. )
  593. );
  594. }
  595. // Change the columns back, but with the proper character set.
  596. $smcFunc['db_query']('', '
  597. ALTER TABLE {raw:table_name}{raw:updates_text}',
  598. array(
  599. 'table_name' => $table_info['Name'],
  600. 'updates_text' => substr($updates_text, 0, -1),
  601. )
  602. );
  603. }
  604. // Now do the actual conversion (if still needed).
  605. if ($charsets[$_POST['src_charset']] !== 'utf8')
  606. $smcFunc['db_query']('', '
  607. ALTER TABLE {raw:table_name}
  608. CONVERT TO CHARACTER SET utf8',
  609. array(
  610. 'table_name' => $table_info['Name'],
  611. )
  612. );
  613. }
  614. $smcFunc['db_free_result']($queryTables);
  615. // Let the settings know we have a new character set.
  616. updateSettings(array('global_character_set' => 'UTF-8', 'previousCharacterSet' => (empty($translation_tables[$_POST['src_charset']])) ? $charsets[$_POST['src_charset']] : $translation_tables[$_POST['src_charset']]));
  617. // Store it in Settings.php too because it's needed before db connection.
  618. require_once($sourcedir . '/Subs-Admin.php');
  619. updateSettingsFile(array('db_character_set' => '\'utf8\''));
  620. // The conversion might have messed up some serialized strings. Fix them!
  621. require_once($sourcedir . '/Subs-Charset.php');
  622. fix_serialized_columns();
  623. redirectexit('action=admin;area=maintain;done=convertutf8');
  624. }
  625. // Convert HTML-entities to their UTF-8 character equivalents.
  626. function ConvertEntities()
  627. {
  628. global $db_character_set, $modSettings, $context, $sourcedir, $smcFunc;
  629. isAllowedTo('admin_forum');
  630. // Check to see if UTF-8 is currently the default character set.
  631. if ($modSettings['global_character_set'] !== 'UTF-8' || !isset($db_character_set) || $db_character_set !== 'utf8')
  632. fatal_lang_error('entity_convert_only_utf8');
  633. // Some starting values.
  634. $context['table'] = empty($_REQUEST['table']) ? 0 : (int) $_REQUEST['table'];
  635. $context['start'] = empty($_REQUEST['start']) ? 0 : (int) $_REQUEST['start'];
  636. $context['start_time'] = time();
  637. $context['first_step'] = !isset($_REQUEST[$context['session_var']]);
  638. $context['last_step'] = false;
  639. // The first step is just a text screen with some explanation.
  640. if ($context['first_step'])
  641. {
  642. $context['sub_template'] = 'convert_entities';
  643. return;
  644. }
  645. // Otherwise use the generic "not done" template.
  646. $context['sub_template'] = 'not_done';
  647. $context['continue_post_data'] = '';
  648. $context['continue_countdown'] = 3;
  649. // Now we're actually going to convert...
  650. checkSession('request');
  651. // A list of tables ready for conversion.
  652. $tables = array(
  653. 'ban_groups',
  654. 'ban_items',
  655. 'boards',
  656. 'calendar',
  657. 'calendar_holidays',
  658. 'categories',
  659. 'log_errors',
  660. 'log_search_subjects',
  661. 'membergroups',
  662. 'members',
  663. 'message_icons',
  664. 'messages',
  665. 'package_servers',
  666. 'personal_messages',
  667. 'pm_recipients',
  668. 'polls',
  669. 'poll_choices',
  670. 'smileys',
  671. 'themes',
  672. );
  673. $context['num_tables'] = count($tables);
  674. // This function will do the conversion later on.
  675. $entity_replace = create_function('$string', '
  676. $num = substr($string, 0, 1) === \'x\' ? hexdec(substr($string, 1)) : (int) $string;
  677. return $num < 0x20 || $num > 0x10FFFF || ($num >= 0xD800 && $num <= 0xDFFF) ? \'\' : ($num < 0x80 ? \'&#\' . $num . \';\' : ($num < 0x800 ? chr(192 | $num >> 6) . chr(128 | $num & 63) : ($num < 0x10000 ? chr(224 | $num >> 12) . chr(128 | $num >> 6 & 63) . chr(128 | $num & 63) : chr(240 | $num >> 18) . chr(128 | $num >> 12 & 63) . chr(128 | $num >> 6 & 63) . chr(128 | $num & 63))));');
  678. // Loop through all tables that need converting.
  679. for (; $context['table'] < $context['num_tables']; $context['table']++)
  680. {
  681. $cur_table = $tables[$context['table']];
  682. $primary_key = '';
  683. // Make sure we keep stuff unique!
  684. $primary_keys = array();
  685. if (function_exists('apache_reset_timeout'))
  686. @apache_reset_timeout();
  687. // Get a list of text columns.
  688. $columns = array();
  689. $request = $smcFunc['db_query']('', '
  690. SHOW FULL COLUMNS
  691. FROM {db_prefix}' . $cur_table,
  692. array(
  693. )
  694. );
  695. while ($column_info = $smcFunc['db_fetch_assoc']($request))
  696. if (strpos($column_info['Type'], 'text') !== false || strpos($column_info['Type'], 'char') !== false)
  697. $columns[] = strtolower($column_info['Field']);
  698. // Get the column with the (first) primary key.
  699. $request = $smcFunc['db_query']('', '
  700. SHOW KEYS
  701. FROM {db_prefix}' . $cur_table,
  702. array(
  703. )
  704. );
  705. while ($row = $smcFunc['db_fetch_assoc']($request))
  706. {
  707. if ($row['Key_name'] === 'PRIMARY')
  708. {
  709. if (empty($primary_key) || ($row['Seq_in_index'] == 1 && !in_array(strtolower($row['Column_name']), $columns)))
  710. $primary_key = $row['Column_name'];
  711. $primary_keys[] = $row['Column_name'];
  712. }
  713. }
  714. $smcFunc['db_free_result']($request);
  715. // No primary key, no glory.
  716. // Same for columns. Just to be sure we've work to do!
  717. if (empty($primary_key) || empty($columns))
  718. continue;
  719. // Get the maximum value for the primary key.
  720. $request = $smcFunc['db_query']('', '
  721. SELECT MAX(' . $primary_key . ')
  722. FROM {db_prefix}' . $cur_table,
  723. array(
  724. )
  725. );
  726. list($max_value) = $smcFunc['db_fetch_row']($request);
  727. $smcFunc['db_free_result']($request);
  728. if (empty($max_value))
  729. continue;
  730. while ($context['start'] <= $max_value)
  731. {
  732. // Retrieve a list of rows that has at least one entity to convert.
  733. $request = $smcFunc['db_query']('', '
  734. SELECT {raw:primary_keys}, {raw:columns}
  735. FROM {db_prefix}{raw:cur_table}
  736. WHERE {raw:primary_key} BETWEEN {int:start} AND {int:start} + 499
  737. AND {raw:like_compare}
  738. LIMIT 500',
  739. array(
  740. 'primary_keys' => implode(', ', $primary_keys),
  741. 'columns' => implode(', ', $columns),
  742. 'cur_table' => $cur_table,
  743. 'primary_key' => $primary_key,
  744. 'start' => $context['start'],
  745. 'like_compare' => '(' . implode(' LIKE \'%&#%\' OR ', $columns) . ' LIKE \'%&#%\')',
  746. )
  747. );
  748. while ($row = $smcFunc['db_fetch_assoc']($request))
  749. {
  750. $insertion_variables = array();
  751. $changes = array();
  752. foreach ($row as $column_name => $column_value)
  753. if ($column_name !== $primary_key && strpos($column_value, '&#') !== false)
  754. {
  755. $changes[] = $column_name . ' = {string:changes_' . $column_name . '}';
  756. $insertion_variables['changes_' . $column_name] = preg_replace_callback('~&#(\d{1,7}|x[0-9a-fA-F]{1,6});~', 'fixchar__callback', $column_value);
  757. }
  758. $where = array();
  759. foreach ($primary_keys as $key)
  760. {
  761. $where[] = $key . ' = {string:where_' . $key . '}';
  762. $insertion_variables['where_' . $key] = $row[$key];
  763. }
  764. // Update the row.
  765. if (!empty($changes))
  766. $smcFunc['db_query']('', '
  767. UPDATE {db_prefix}' . $cur_table . '
  768. SET
  769. ' . implode(',
  770. ', $changes) . '
  771. WHERE ' . implode(' AND ', $where),
  772. $insertion_variables
  773. );
  774. }
  775. $smcFunc['db_free_result']($request);
  776. $context['start'] += 500;
  777. // After ten seconds interrupt.
  778. if (time() - $context['start_time'] > 10)
  779. {
  780. // Calculate an approximation of the percentage done.
  781. $context['continue_percent'] = round(100 * ($context['table'] + ($context['start'] / $max_value)) / $context['num_tables'], 1);
  782. $context['continue_get_data'] = '?action=admin;area=maintain;sa=database;activity=convertentities;table=' . $context['table'] . ';start=' . $context['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
  783. return;
  784. }
  785. }
  786. $context['start'] = 0;
  787. }
  788. // Make sure all serialized strings are all right.
  789. require_once($sourcedir . '/Subs-Charset.php');
  790. fix_serialized_columns();
  791. // If we're here, we must be done.
  792. $context['continue_percent'] = 100;
  793. $context['continue_get_data'] = '?action=admin;area=maintain;sa=database;done=convertentities';
  794. $context['last_step'] = true;
  795. $context['continue_countdown'] = -1;
  796. }
  797. // Optimize the database's tables.
  798. function OptimizeTables()
  799. {
  800. global $db_type, $db_name, $db_prefix, $txt, $context, $scripturl, $sourcedir, $smcFunc;
  801. isAllowedTo('admin_forum');
  802. checkSession('post');
  803. ignore_user_abort(true);
  804. db_extend();
  805. // Start with no tables optimized.
  806. $opttab = 0;
  807. $context['page_title'] = $txt['database_optimize'];
  808. $context['sub_template'] = 'optimize';
  809. // Only optimize the tables related to this smf install, not all the tables in the db
  810. $real_prefix = preg_match('~^(`?)(.+?)\\1\\.(.*?)$~', $db_prefix, $match) === 1 ? $match[3] : $db_prefix;
  811. // Get a list of tables, as well as how many there are.
  812. $temp_tables = $smcFunc['db_list_tables'](false, $real_prefix . '%');
  813. $tables = array();
  814. foreach ($temp_tables as $table)
  815. $tables[] = array('table_name' => $table);
  816. // If there aren't any tables then I believe that would mean the world has exploded...
  817. $context['num_tables'] = count($tables);
  818. if ($context['num_tables'] == 0)
  819. fatal_error('You appear to be running SMF in a flat file mode... fantastic!', false);
  820. // For each table....
  821. $context['optimized_tables'] = array();
  822. foreach ($tables as $table)
  823. {
  824. // Optimize the table! We use backticks here because it might be a custom table.
  825. $data_freed = $smcFunc['db_optimize_table']($table['table_name']);
  826. // Optimizing one sqlite table optimizes them all.
  827. if ($db_type == 'sqlite')
  828. break;
  829. if ($data_freed > 0)
  830. $context['optimized_tables'][] = array(
  831. 'name' => $table['table_name'],
  832. 'data_freed' => $data_freed,
  833. );
  834. }
  835. // Number of tables, etc....
  836. $txt['database_numb_tables'] = sprintf($txt['database_numb_tables'], $context['num_tables']);
  837. $context['num_tables_optimized'] = count($context['optimized_tables']);
  838. // Check that we don't auto optimise again too soon!
  839. require_once($sourcedir . '/ScheduledTasks.php');
  840. CalculateNextTrigger('auto_optimize', true);
  841. }
  842. // Recount all the important board totals.
  843. function AdminBoardRecount()
  844. {
  845. global $txt, $context, $scripturl, $modSettings, $sourcedir;
  846. global $time_start, $smcFunc;
  847. isAllowedTo('admin_forum');
  848. checkSession('request');
  849. $context['page_title'] = $txt['not_done_title'];
  850. $context['continue_post_data'] = '';
  851. $context['continue_countdown'] = '3';
  852. $context['sub_template'] = 'not_done';
  853. // Try for as much time as possible.
  854. @set_time_limit(600);
  855. // Step the number of topics at a time so things don't time out...
  856. $request = $smcFunc['db_query']('', '
  857. SELECT MAX(id_topic)
  858. FROM {db_prefix}topics',
  859. array(
  860. )
  861. );
  862. list ($max_topics) = $smcFunc['db_fetch_row']($request);
  863. $smcFunc['db_free_result']($request);
  864. $increment = min(max(50, ceil($max_topics / 4)), 2000);
  865. if (empty($_REQUEST['start']))
  866. $_REQUEST['start'] = 0;
  867. $total_steps = 8;
  868. // Get each topic with a wrong reply count and fix it - let's just do some at a time, though.
  869. if (empty($_REQUEST['step']))
  870. {
  871. $_REQUEST['step'] = 0;
  872. while ($_REQUEST['start'] < $max_topics)
  873. {
  874. // Recount approved messages
  875. $request = $smcFunc['db_query']('', '
  876. SELECT /*!40001 SQL_NO_CACHE */ t.id_topic, MAX(t.num_replies) AS num_replies,
  877. CASE WHEN COUNT(ma.id_msg) >= 1 THEN COUNT(ma.id_msg) - 1 ELSE 0 END AS real_num_replies
  878. FROM {db_prefix}topics AS t
  879. LEFT JOIN {db_prefix}messages AS ma ON (ma.id_topic = t.id_topic AND ma.approved = {int:is_approved})
  880. WHERE t.id_topic > {int:start}
  881. AND t.id_topic <= {int:max_id}
  882. GROUP BY t.id_topic
  883. HAVING CASE WHEN COUNT(ma.id_msg) >= 1 THEN COUNT(ma.id_msg) - 1 ELSE 0 END != MAX(t.num_replies)',
  884. array(
  885. 'is_approved' => 1,
  886. 'start' => $_REQUEST['start'],
  887. 'max_id' => $_REQUEST['start'] + $increment,
  888. )
  889. );
  890. while ($row = $smcFunc['db_fetch_assoc']($request))
  891. $smcFunc['db_query']('', '
  892. UPDATE {db_prefix}topics
  893. SET num_replies = {int:num_replies}
  894. WHERE id_topic = {int:id_topic}',
  895. array(
  896. 'num_replies' => $row['real_num_replies'],
  897. 'id_topic' => $row['id_topic'],
  898. )
  899. );
  900. $smcFunc['db_free_result']($request);
  901. // Recount unapproved messages
  902. $request = $smcFunc['db_query']('', '
  903. SELECT /*!40001 SQL_NO_CACHE */ t.id_topic, MAX(t.unapproved_posts) AS unapproved_posts,
  904. COUNT(mu.id_msg) AS real_unapproved_posts
  905. FROM {db_prefix}topics AS t
  906. LEFT JOIN {db_prefix}messages AS mu ON (mu.id_topic = t.id_topic AND mu.approved = {int:not_approved})
  907. WHERE t.id_topic > {int:start}
  908. AND t.id_topic <= {int:max_id}
  909. GROUP BY t.id_topic
  910. HAVING COUNT(mu.id_msg) != MAX(t.unapproved_posts)',
  911. array(
  912. 'not_approved' => 0,
  913. 'start' => $_REQUEST['start'],
  914. 'max_id' => $_REQUEST['start'] + $increment,
  915. )
  916. );
  917. while ($row = $smcFunc['db_fetch_assoc']($request))
  918. $smcFunc['db_query']('', '
  919. UPDATE {db_prefix}topics
  920. SET unapproved_posts = {int:unapproved_posts}
  921. WHERE id_topic = {int:id_topic}',
  922. array(
  923. 'unapproved_posts' => $row['real_unapproved_posts'],
  924. 'id_topic' => $row['id_topic'],
  925. )
  926. );
  927. $smcFunc['db_free_result']($request);
  928. $_REQUEST['start'] += $increment;
  929. if (array_sum(explode(' ', microtime())) - array_sum(explode(' ', $time_start)) > 3)
  930. {
  931. $context['continue_get_data'] = '?action=admin;area=maintain;sa=routine;activity=recount;step=0;start=' . $_REQUEST['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
  932. $context['continue_percent'] = round((100 * $_REQUEST['start'] / $max_topics) / $total_steps);
  933. return;
  934. }
  935. }
  936. $_REQUEST['start'] = 0;
  937. }
  938. // Update the post count of each board.
  939. if ($_REQUEST['step'] <= 1)
  940. {
  941. if (empty($_REQUEST['start']))
  942. $smcFunc['db_query']('', '
  943. UPDATE {db_prefix}boards
  944. SET num_posts = {int:num_posts}
  945. WHERE redirect = {string:redirect}',
  946. array(
  947. 'num_posts' => 0,
  948. 'redirect' => '',
  949. )
  950. );
  951. while ($_REQUEST['start'] < $max_topics)
  952. {
  953. $request = $smcFunc['db_query']('', '
  954. SELECT /*!40001 SQL_NO_CACHE */ m.id_board, COUNT(*) AS real_num_posts
  955. FROM {db_prefix}messages AS m
  956. WHERE m.id_topic > {int:id_topic_min}
  957. AND m.id_topic <= {int:id_topic_max}
  958. AND m.approved = {int:is_approved}
  959. GROUP BY m.id_board',
  960. array(
  961. 'id_topic_min' => $_REQUEST['start'],
  962. 'id_topic_max' => $_REQUEST['start'] + $increment,
  963. 'is_approved' => 1,
  964. )
  965. );
  966. while ($row = $smcFunc['db_fetch_assoc']($request))
  967. $smcFunc['db_query']('', '
  968. UPDATE {db_prefix}boards
  969. SET num_posts = num_posts + {int:real_num_posts}
  970. WHERE id_board = {int:id_board}',
  971. array(
  972. 'id_board' => $row['id_board'],
  973. 'real_num_posts' => $row['real_num_posts'],
  974. )
  975. );
  976. $smcFunc['db_free_result']($request);
  977. $_REQUEST['start'] += $increment;
  978. if (array_sum(explode(' ', microtime())) - array_sum(explode(' ', $time_start)) > 3)
  979. {
  980. $context['continue_get_data'] = '?action=admin;area=maintain;sa=routine;activity=recount;step=1;start=' . $_REQUEST['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
  981. $context['continue_percent'] = round((200 + 100 * $_REQUEST['start'] / $max_topics) / $total_steps);
  982. return;
  983. }
  984. }
  985. $_REQUEST['start'] = 0;
  986. }
  987. // Update the topic count of each board.
  988. if ($_REQUEST['step'] <= 2)
  989. {
  990. if (empty($_REQUEST['start']))
  991. $smcFunc['db_query']('', '
  992. UPDATE {db_prefix}boards
  993. SET num_topics = {int:num_topics}',
  994. array(
  995. 'num_topics' => 0,
  996. )
  997. );
  998. while ($_REQUEST['start'] < $max_topics)
  999. {
  1000. $request = $smcFunc['db_query']('', '
  1001. SELECT /*!40001 SQL_NO_CACHE */ t.id_board, COUNT(*) AS real_num_topics
  1002. FROM {db_prefix}topics AS t
  1003. WHERE t.approved = {int:is_approved}
  1004. AND t.id_topic > {int:id_topic_min}
  1005. AND t.id_topic <= {int:id_topic_max}
  1006. GROUP BY t.id_board',
  1007. array(
  1008. 'is_approved' => 1,
  1009. 'id_topic_min' => $_REQUEST['start'],
  1010. 'id_topic_max' => $_REQUEST['start'] + $increment,
  1011. )
  1012. );
  1013. while ($row = $smcFunc['db_fetch_assoc']($request))
  1014. $smcFunc['db_query']('', '
  1015. UPDATE {db_prefix}boards
  1016. SET num_topics = num_topics + {int:real_num_topics}
  1017. WHERE id_board = {int:id_board}',
  1018. array(
  1019. 'id_board' => $row['id_board'],
  1020. 'real_num_topics' => $row['real_num_topics'],
  1021. )
  1022. );
  1023. $smcFunc['db_free_result']($request);
  1024. $_REQUEST['start'] += $increment;
  1025. if (array_sum(explode(' ', microtime())) - array_sum(explode(' ', $time_start)) > 3)
  1026. {
  1027. $context['continue_get_data'] = '?action=admin;area=maintain;sa=routine;activity=recount;step=2;start=' . $_REQUEST['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
  1028. $context['continue_percent'] = round((300 + 100 * $_REQUEST['start'] / $max_topics) / $total_steps);
  1029. return;
  1030. }
  1031. }
  1032. $_REQUEST['start'] = 0;
  1033. }
  1034. // Update the unapproved post count of each board.
  1035. if ($_REQUEST['step'] <= 3)
  1036. {
  1037. if (empty($_REQUEST['start']))
  1038. $smcFunc['db_query']('', '
  1039. UPDATE {db_prefix}boards
  1040. SET unapproved_posts = {int:unapproved_posts}',
  1041. array(
  1042. 'unapproved_posts' => 0,
  1043. )
  1044. );
  1045. while ($_REQUEST['start'] < $max_topics)
  1046. {
  1047. $request = $smcFunc['db_query']('', '
  1048. SELECT /*!40001 SQL_NO_CACHE */ m.id_board, COUNT(*) AS real_unapproved_posts
  1049. FROM {db_prefix}messages AS m
  1050. WHERE m.id_topic > {int:id_topic_min}
  1051. AND m.id_topic <= {int:id_topic_max}
  1052. AND m.approved = {int:is_approved}
  1053. GROUP BY m.id_board',
  1054. array(
  1055. 'id_topic_min' => $_REQUEST['start'],
  1056. 'id_topic_max' => $_REQUEST['start'] + $increment,
  1057. 'is_approved' => 0,
  1058. )
  1059. );
  1060. while ($row = $smcFunc['db_fetch_assoc']($request))
  1061. $smcFunc['db_query']('', '
  1062. UPDATE {db_prefix}boards
  1063. SET unapproved_posts = unapproved_posts + {int:unapproved_posts}
  1064. WHERE id_board = {int:id_board}',
  1065. array(
  1066. 'id_board' => $row['id_board'],
  1067. 'unapproved_posts' => $row['real_unapproved_posts'],
  1068. )
  1069. );
  1070. $smcFunc['db_free_result']($request);
  1071. $_REQUEST['start'] += $increment;
  1072. if (array_sum(explode(' ', microtime())) - array_sum(explode(' ', $time_start)) > 3)
  1073. {
  1074. $context['continue_get_data'] = '?action=admin;area=maintain;sa=routine;activity=recount;step=3;start=' . $_REQUEST['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
  1075. $context['continue_percent'] = round((400 + 100 * $_REQUEST['start'] / $max_topics) / $total_steps);
  1076. return;
  1077. }
  1078. }
  1079. $_REQUEST['start'] = 0;
  1080. }
  1081. // Update the unapproved topic count of each board.
  1082. if ($_REQUEST['step'] <= 4)
  1083. {
  1084. if (empty($_REQUEST['start']))
  1085. $smcFunc['db_query']('', '
  1086. UPDATE {db_prefix}boards
  1087. SET unapproved_topics = {int:unapproved_topics}',
  1088. array(
  1089. 'unapproved_topics' => 0,
  1090. )
  1091. );
  1092. while ($_REQUEST['start'] < $max_topics)
  1093. {
  1094. $request = $smcFunc['db_query']('', '
  1095. SELECT /*!40001 SQL_NO_CACHE */ t.id_board, COUNT(*) AS real_unapproved_topics
  1096. FROM {db_prefix}topics AS t
  1097. WHERE t.approved = {int:is_approved}
  1098. AND t.id_topic > {int:id_topic_min}
  1099. AND t.id_topic <= {int:id_topic_max}
  1100. GROUP BY t.id_board',
  1101. array(
  1102. 'is_approved' => 0,
  1103. 'id_topic_min' => $_REQUEST['start'],
  1104. 'id_topic_max' => $_REQUEST['start'] + $increment,
  1105. )
  1106. );
  1107. while ($row = $smcFunc['db_fetch_assoc']($request))
  1108. $smcFunc['db_query']('', '
  1109. UPDATE {db_prefix}boards
  1110. SET unapproved_topics = unapproved_topics + {int:real_unapproved_topics}
  1111. WHERE id_board = {int:id_board}',
  1112. array(
  1113. 'id_board' => $row['id_board'],
  1114. 'real_unapproved_topics' => $row['real_unapproved_topics'],
  1115. )
  1116. );
  1117. $smcFunc['db_free_result']($request);
  1118. $_REQUEST['start'] += $increment;
  1119. if (array_sum(explode(' ', microtime())) - array_sum(explode(' ', $time_start)) > 3)
  1120. {
  1121. $context['continue_get_data'] = '?action=admin;area=maintain;sa=routine;activity=recount;step=4;start=' . $_REQUEST['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
  1122. $context['continue_percent'] = round((500 + 100 * $_REQUEST['start'] / $max_topics) / $total_steps);
  1123. return;
  1124. }
  1125. }
  1126. $_REQUEST['start'] = 0;
  1127. }
  1128. // Get all members with wrong number of personal messages.
  1129. if ($_REQUEST['step'] <= 5)
  1130. {
  1131. $request = $smcFunc['db_query']('', '
  1132. SELECT /*!40001 SQL_NO_CACHE */ mem.id_member, COUNT(pmr.id_pm) AS real_num,
  1133. MAX(mem.instant_messages) AS instant_messages
  1134. FROM {db_prefix}members AS mem
  1135. LEFT JOIN {db_prefix}pm_recipients AS pmr ON (mem.id_member = pmr.id_member AND pmr.deleted = {int:is_not_deleted})
  1136. GROUP BY mem.id_member
  1137. HAVING COUNT(pmr.id_pm) != MAX(mem.instant_messages)',
  1138. array(
  1139. 'is_not_deleted' => 0,
  1140. )
  1141. );
  1142. while ($row = $smcFunc['db_fetch_assoc']($request))
  1143. updateMemberData($row['id_member'], array('instant_messages' => $row['real_num']));
  1144. $smcFunc['db_free_result']($request);
  1145. $request = $smcFunc['db_query']('', '
  1146. SELECT /*!40001 SQL_NO_CACHE */ mem.id_member, COUNT(pmr.id_pm) AS real_num,
  1147. MAX(mem.unread_messages) AS unread_messages
  1148. FROM {db_prefix}members AS mem
  1149. LEFT JOIN {db_prefix}pm_recipients AS pmr ON (mem.id_member = pmr.id_member AND pmr.deleted = {int:is_not_deleted} AND pmr.is_read = {int:is_not_read})
  1150. GROUP BY mem.id_member
  1151. HAVING COUNT(pmr.id_pm) != MAX(mem.unread_messages)',
  1152. array(
  1153. 'is_not_deleted' => 0,
  1154. 'is_not_read' => 0,
  1155. )
  1156. );
  1157. while ($row = $smcFunc['db_fetch_assoc']($request))
  1158. updateMemberData($row['id_member'], array('unread_messages' => $row['real_num']));
  1159. $smcFunc['db_free_result']($request);
  1160. if (array_sum(explode(' ', microtime())) - array_sum(explode(' ', $time_start)) > 3)
  1161. {
  1162. $context['continue_get_data'] = '?action=admin;area=maintain;sa=routine;activity=recount;step=6;start=0;' . $context['session_var'] . '=' . $context['session_id'];
  1163. $context['continue_percent'] = round(700 / $total_steps);
  1164. return;
  1165. }
  1166. }
  1167. // Any messages pointing to the wrong board?
  1168. if ($_REQUEST['step'] <= 6)
  1169. {
  1170. while ($_REQUEST['start'] < $modSettings['maxMsgID'])
  1171. {
  1172. $request = $smcFunc['db_query']('', '
  1173. SELECT /*!40001 SQL_NO_CACHE */ t.id_board, m.id_msg
  1174. FROM {db_prefix}messages AS m
  1175. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic AND t.id_board != m.id_board)
  1176. WHERE m.id_msg > {int:id_msg_min}
  1177. AND m.id_msg <= {int:id_msg_max}',
  1178. array(
  1179. 'id_msg_min' => $_REQUEST['start'],
  1180. 'id_msg_max' => $_REQUEST['start'] + $increment,
  1181. )
  1182. );
  1183. $boards = array();
  1184. while ($row = $smcFunc['db_fetch_assoc']($request))
  1185. $boards[$row['id_board']][] = $row['id_msg'];
  1186. $smcFunc['db_free_result']($request);
  1187. foreach ($boards as $board_id => $messages)
  1188. $smcFunc['db_query']('', '
  1189. UPDATE {db_prefix}messages
  1190. SET id_board = {int:id_board}
  1191. WHERE id_msg IN ({array_int:id_msg_array})',
  1192. array(
  1193. 'id_msg_array' => $messages,
  1194. 'id_board' => $board_id,
  1195. )
  1196. );
  1197. $_REQUEST['start'] += $increment;
  1198. if (array_sum(explode(' ', microtime())) - array_sum(explode(' ', $time_start)) > 3)
  1199. {
  1200. $context['continue_get_data'] = '?action=admin;area=maintain;sa=routine;activity=recount;step=6;start=' . $_REQUEST['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
  1201. $context['continue_percent'] = round((700 + 100 * $_REQUEST['start'] / $modSettings['maxMsgID']) / $total_steps);
  1202. return;
  1203. }
  1204. }
  1205. $_REQUEST['start'] = 0;
  1206. }
  1207. // Update the latest message of each board.
  1208. $request = $smcFunc['db_query']('', '
  1209. SELECT m.id_board, MAX(m.id_msg) AS local_last_msg
  1210. FROM {db_prefix}messages AS m
  1211. WHERE m.approved = {int:is_approved}
  1212. GROUP BY m.id_board',
  1213. array(
  1214. 'is_approved' => 1,
  1215. )
  1216. );
  1217. $realBoardCounts = array();
  1218. while ($row = $smcFunc['db_fetch_assoc']($request))
  1219. $realBoardCounts[$row['id_board']] = $row['local_last_msg'];
  1220. $smcFunc['db_free_result']($request);
  1221. $request = $smcFunc['db_query']('', '
  1222. SELECT /*!40001 SQL_NO_CACHE */ id_board, id_parent, id_last_msg, child_level, id_msg_updated
  1223. FROM {db_prefix}boards',
  1224. array(
  1225. )
  1226. );
  1227. $resort_me = array();
  1228. while ($row = $smcFunc['db_fetch_assoc']($request))
  1229. {
  1230. $row['local_last_msg'] = isset($realBoardCounts[$row['id_board']]) ? $realBoardCounts[$row['id_board']] : 0;
  1231. $resort_me[$row['child_level']][] = $row;
  1232. }
  1233. $smcFunc['db_free_result']($request);
  1234. krsort($resort_me);
  1235. $lastModifiedMsg = array();
  1236. foreach ($resort_me as $rows)
  1237. foreach ($rows as $row)
  1238. {
  1239. // The latest message is the latest of the current board and its children.
  1240. if (isset($lastModifiedMsg[$row['id_board']]))
  1241. $curLastModifiedMsg = max($row['local_last_msg'], $lastModifiedMsg[$row['id_board']]);
  1242. else
  1243. $curLastModifiedMsg = $row['local_last_msg'];
  1244. // If what is and what should be the latest message differ, an update is necessary.
  1245. if ($row['local_last_msg'] != $row['id_last_msg'] || $curLastModifiedMsg != $row['id_msg_updated'])
  1246. $smcFunc['db_query']('', '
  1247. UPDATE {db_prefix}boards
  1248. SET id_last_msg = {int:id_last_msg}, id_msg_updated = {int:id_msg_updated}
  1249. WHERE id_board = {int:id_board}',
  1250. array(
  1251. 'id_last_msg' => $row['local_last_msg'],
  1252. 'id_msg_updated' => $curLastModifiedMsg,
  1253. 'id_board' => $row['id_board'],
  1254. )
  1255. );
  1256. // Parent boards inherit the latest modified message of their children.
  1257. if (isset($lastModifiedMsg[$row['id_parent']]))
  1258. $lastModifiedMsg[$row['id_parent']] = max($row['local_last_msg'], $lastModifiedMsg[$row['id_parent']]);
  1259. else
  1260. $lastModifiedMsg[$row['id_parent']] = $row['local_last_msg'];
  1261. }
  1262. // Update all the basic statistics.
  1263. updateStats('member');
  1264. updateStats('message');
  1265. updateStats('topic');
  1266. // Finally, update the latest event times.
  1267. require_once($sourcedir . '/ScheduledTasks.php');
  1268. CalculateNextTrigger();
  1269. redirectexit('action=admin;area=maintain;sa=routine;done=recount');
  1270. }
  1271. // Perform a detailed version check. A very good thing ;).
  1272. function VersionDetail()
  1273. {
  1274. global $forum_version, $txt, $sourcedir, $context;
  1275. isAllowedTo('admin_forum');
  1276. // Call the function that'll get all the version info we need.
  1277. require_once($sourcedir . '/Subs-Admin.php');
  1278. $versionOptions = array(
  1279. 'include_ssi' => true,
  1280. 'include_subscriptions' => true,
  1281. 'sort_results' => true,
  1282. );
  1283. $version_info = getFileVersions($versionOptions);
  1284. // Add the new info to the template context.
  1285. $context += array(
  1286. 'file_versions' => $version_info['file_versions'],
  1287. 'default_template_versions' => $version_info['default_template_versions'],
  1288. 'template_versions' => $version_info['template_versions'],
  1289. 'default_language_versions' => $version_info['default_language_versions'],
  1290. 'default_known_languages' => array_keys($version_info['default_language_versions']),
  1291. );
  1292. // Make it easier to manage for the template.
  1293. $context['forum_version'] = $forum_version;
  1294. $context['sub_template'] = 'view_versions';
  1295. $context['page_title'] = $txt['admin_version_check'];
  1296. }
  1297. // Removing old posts doesn't take much as we really pass through.
  1298. function MaintainReattributePosts()
  1299. {
  1300. global $sourcedir, $context, $txt;
  1301. checkSession();
  1302. // Find the member.
  1303. require_once($sourcedir . '/Subs-Auth.php');
  1304. $members = findMembers($_POST['to']);
  1305. if (empty($members))
  1306. fatal_lang_error('reattribute_cannot_find_member');
  1307. $memID = array_shift($members);
  1308. $memID = $memID['id'];
  1309. $email = $_POST['type'] == 'email' ? $_POST['from_email'] : '';
  1310. $membername = $_POST['type'] == 'name' ? $_POST['from_name'] : '';
  1311. // Now call the reattribute function.
  1312. require_once($sourcedir . '/Subs-Members.php');
  1313. reattributePosts($memID, $email, $membername, !empty($_POST['posts']));
  1314. $context['maintenance_finished'] = $txt['maintain_reattribute_posts'];
  1315. }
  1316. // Handling function for the backup stuff.
  1317. function MaintainDownloadBackup()
  1318. {
  1319. global $sourcedir;
  1320. require_once($sourcedir . '/DumpDatabase.php');
  1321. DumpDatabase2();
  1322. }
  1323. // Removing old members?
  1324. function MaintainPurgeInactiveMembers()
  1325. {
  1326. global $sourcedir, $context, $smcFunc, $txt;
  1327. $_POST['maxdays'] = empty($_POST['maxdays']) ? 0 : (int) $_POST['maxdays'];
  1328. if (!empty($_POST['groups']) && $_POST['maxdays'] > 0)
  1329. {
  1330. checkSession();
  1331. $groups = array();
  1332. foreach ($_POST['groups'] as $id => $dummy)
  1333. $groups[] = (int) $id;
  1334. $time_limit = (time() - ($_POST['maxdays'] * 24 * 3600));
  1335. $where_vars = array(
  1336. 'time_limit' => $time_limit,
  1337. );
  1338. if ($_POST['del_type'] == 'activated')
  1339. {
  1340. $where = 'mem.date_registered < {int:time_limit} AND mem.is_activated = {int:is_activated}';
  1341. $where_vars['is_activated'] = 0;
  1342. }
  1343. else
  1344. $where = 'mem.last_login < {int:time_limit}';
  1345. // Need to get *all* groups then work out which (if any) we avoid.
  1346. $request = $smcFunc['db_query']('', '
  1347. SELECT id_group, group_name, min_posts
  1348. FROM {db_prefix}membergroups',
  1349. array(
  1350. )
  1351. );
  1352. while ($row = $smcFunc['db_fetch_assoc']($request))
  1353. {
  1354. // Avoid this one?
  1355. if (!in_array($row['id_group'], $groups))
  1356. {
  1357. // Post group?
  1358. if ($row['min_posts'] != -1)
  1359. {
  1360. $where .= ' AND mem.id_post_group != {int:id_post_group_' . $row['id_group'] . '}';
  1361. $where_vars['id_post_group_' . $row['id_group']] = $row['id_group'];
  1362. }
  1363. else
  1364. {
  1365. $where .= ' AND mem.id_group != {int:id_group_' . $row['id_group'] . '} AND FIND_IN_SET({int:id_group_' . $row['id_group'] . '}, mem.additional_groups) = 0';
  1366. $where_vars['id_group_' . $row['id_group']] = $row['id_group'];
  1367. }
  1368. }
  1369. }
  1370. $smcFunc['db_free_result']($request);
  1371. // If we have ungrouped unselected we need to avoid those guys.
  1372. if (!in_array(0, $groups))
  1373. {
  1374. $where .= ' AND (mem.id_group != 0 OR mem.additional_groups != {string:blank_add_groups})';
  1375. $where_vars['blank_add_groups'] = '';
  1376. }
  1377. // Select all the members we're about to murder/remove...
  1378. $request = $smcFunc['db_query']('', '
  1379. SELECT mem.id_member, IFNULL(m.id_member, 0) AS is_mod
  1380. FROM {db_prefix}members AS mem
  1381. LEFT JOIN {db_prefix}moderators AS m ON (m.id_member = mem.id_member)
  1382. WHERE ' . $where,
  1383. $where_vars
  1384. );
  1385. $members = array();
  1386. while ($row = $smcFunc['db_fetch_assoc']($request))
  1387. {
  1388. if (!$row['is_mod'] || !in_array(3, $groups))
  1389. $members[] = $row['id_member'];
  1390. }
  1391. $smcFunc['db_free_result']($request);
  1392. require_once($sourcedir . '/Subs-Members.php');
  1393. deleteMembers($members);
  1394. }
  1395. $context['maintenance_finished'] = $txt['maintain_members'];
  1396. }
  1397. // Removing old posts doesn't take much as we really pass through.
  1398. function MaintainRemoveOldPosts()
  1399. {
  1400. global $sourcedir, $context, $txt;
  1401. // Actually do what we're told!
  1402. require_once($sourcedir . '/RemoveTopic.php');
  1403. RemoveOldTopics2();
  1404. }
  1405. function MaintainMassMoveTopics()
  1406. {
  1407. global $smcFunc, $sourcedir, $context, $txt;
  1408. // Only admins.
  1409. isAllowedTo('admin_forum');
  1410. checkSession('request');
  1411. // Set up to the context.
  1412. $context['page_title'] = $txt['not_done_title'];
  1413. $context['continue_countdown'] = '3';
  1414. $context['continue_post_data'] = '';
  1415. $context['continue_get_data'] = '';
  1416. $context['sub_template'] = 'not_done';
  1417. $context['start'] = empty($_REQUEST['start']) ? 0 : (int) $_REQUEST['start'];
  1418. $context['start_time'] = time();
  1419. // First time we do this?
  1420. $id_board_from = isset($_POST['id_board_from']) ? (int) $_POST['id_board_from'] : (int) $_REQUEST['id_board_from'];
  1421. $id_board_to = isset($_POST['id_board_to']) ? (int) $_POST['id_board_to'] : (int) $_REQUEST['id_board_to'];
  1422. // No boards then this is your stop.
  1423. if (empty($id_board_from) || empty($id_board_to))
  1424. return;
  1425. // How many topics are we converting?
  1426. if (!isset($_REQUEST['totaltopics']))
  1427. {
  1428. $request = $smcFunc['db_query']('', '
  1429. SELECT COUNT(*)
  1430. FROM {db_prefix}topics
  1431. WHERE id_board = {int:id_board_from}',
  1432. array(
  1433. 'id_board_from' => $id_board_from,
  1434. )
  1435. );
  1436. list ($total_topics) = $smcFunc['db_fetch_row']($request);
  1437. $smcFunc['db_free_result']($request);
  1438. }
  1439. else
  1440. $total_topics = (int) $_REQUEST['totaltopics'];
  1441. // Seems like we need this here.
  1442. $context['continue_get_data'] = '?action=admin;area=maintain;sa=topics;activity=massmove;id_board_from=' . $id_board_from . ';id_board_to=' . $id_board_to . ';totaltopics=' . $total_topics . ';start=' . $context['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
  1443. // We have topics to move so start the process.
  1444. if (!empty($total_topics))
  1445. {
  1446. while ($context['start'] <= $total_topics)
  1447. {
  1448. // Lets get the topics.
  1449. $request = $smcFunc['db_query']('', '
  1450. SELECT id_topic
  1451. FROM {db_prefix}topics
  1452. WHERE id_board = {int:id_board_from}
  1453. LIMIT 10',
  1454. array(
  1455. 'id_board_from' => $id_board_from,
  1456. )
  1457. );
  1458. // Get the ids.
  1459. $topics = array();
  1460. while ($row = $smcFunc['db_fetch_assoc']($request))
  1461. $topics[] = $row['id_topic'];
  1462. // Just return if we don't have any topics left to move.
  1463. if (empty($topics))
  1464. {
  1465. cache_put_data('board-' . $id_board_from, null, 120);
  1466. cache_put_data('board-' . $id_board_to, null, 120);
  1467. redirectexit('action=admin;area=maintain;sa=topics;done=massmove');
  1468. }
  1469. // Lets move them.
  1470. require_once($sourcedir . '/MoveTopic.php');
  1471. moveTopics($topics, $id_board_to);
  1472. // We've done at least ten more topics.
  1473. $context['start'] += 10;
  1474. // Lets wait a while.
  1475. if (time() - $context['start_time'] > 3)
  1476. {
  1477. // What's the percent?
  1478. $context['continue_percent'] = round(100 * ($context['start'] / $total_topics), 1);
  1479. $context['continue_get_data'] = '?action=admin;area=maintain;sa=topics;activity=massmove;id_board_from=' . $id_board_from . ';id_board_to=' . $id_board_to . ';totaltopics=' . $total_topics . ';start=' . $context['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
  1480. // Let the template system do it's thang.
  1481. return;
  1482. }
  1483. }
  1484. }
  1485. // Don't confuse admins by having an out of date cache.
  1486. cache_put_data('board-' . $id_board_from, null, 120);
  1487. cache_put_data('board-' . $id_board_to, null, 120);
  1488. redirectexit('action=admin;area=maintain;sa=topics;done=massmove');
  1489. }
  1490. ?>