ManageMaintenance.php 67 KB

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