PageRenderTime 45ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/forum/Sources/ManageMaintenance.php

https://github.com/leftnode/nooges.com
PHP | 1858 lines | 1333 code | 239 blank | 286 comment | 168 complexity | 74a2f2e8265b5e0587d59296f59d2903 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file