ManageMaintenance.php 70 KB

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