ManageMaintenance.php 73 KB

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