upgrade_2-1_postgresql.sql 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  1. /* ATTENTION: You don't need to run or use this file! The upgrade.php script does everything for you! */
  2. /******************************************************************************/
  3. --- Adding new settings...
  4. /******************************************************************************/
  5. ---# Creating login history sequence.
  6. CREATE SEQUENCE {$db_prefix}member_logins_seq;
  7. ---#
  8. ---# Creating login history table.
  9. CREATE TABLE {$db_prefix}member_logins (
  10. id_login int NOT NULL default nextval('{$db_prefix}member_logins_seq'),
  11. id_member int NOT NULL,
  12. time int NOT NULL,
  13. ip varchar(255) NOT NULL default '',
  14. ip2 varchar(255) NOT NULL default '',
  15. PRIMARY KEY (id_login)
  16. );
  17. ---#
  18. ---# Copying the current package backup setting...
  19. ---{
  20. if (!isset($modSettings['package_make_full_backups']) && isset($modSettings['package_make_backups']))
  21. upgrade_query("
  22. INSERT INTO {$db_prefix}settings
  23. (variable, value)
  24. VALUES
  25. ('package_make_full_backups', '" . $modSettings['package_make_backups'] . "')");
  26. ---}
  27. ---#
  28. ---# Copying the current "allow users to disable word censor" setting...
  29. ---{
  30. if (!isset($modSettings['allow_no_censored']))
  31. {
  32. $request = upgrade_query("
  33. SELECT value
  34. FROM {$db_prefix}themes
  35. WHERE variable='allow_no_censored'
  36. AND id_theme = 1 OR id_theme = '$modSettings[theme_default]'
  37. ");
  38. // Is it set for either "default" or the one they've set as default?
  39. while ($row = $smcFunc['db_fetch_assoc']($request))
  40. {
  41. if ($row['value'] == 1)
  42. {
  43. upgrade_query("
  44. INSERT INTO {$db_prefix}settings
  45. VALUES ('allow_no_censored', 1)
  46. ");
  47. // Don't do this twice...
  48. break;
  49. }
  50. }
  51. }
  52. ---}
  53. ---#
  54. ---# Adding new "topic_move_any" setting
  55. INSERT INTO {$db_prefix}settings (variable, value) VALUES ('topic_move_any', '1');
  56. ---#
  57. /******************************************************************************/
  58. --- Updating legacy attachments...
  59. /******************************************************************************/
  60. ---# Converting legacy attachments.
  61. ---{
  62. $request = upgrade_query("
  63. SELECT MAX(id_attach)
  64. FROM {$db_prefix}attachments");
  65. list ($step_progress['total']) = $smcFunc['db_fetch_row']($request);
  66. $smcFunc['db_free_result']($request);
  67. $_GET['a'] = isset($_GET['a']) ? (int) $_GET['a'] : 0;
  68. $step_progress['name'] = 'Converting legacy attachments';
  69. $step_progress['current'] = $_GET['a'];
  70. // We may be using multiple attachment directories.
  71. if (!empty($modSettings['currentAttachmentUploadDir']) && !is_array($modSettings['attachmentUploadDir']))
  72. $modSettings['attachmentUploadDir'] = unserialize($modSettings['attachmentUploadDir']);
  73. $is_done = false;
  74. while (!$is_done)
  75. {
  76. nextSubStep($substep);
  77. $fileHash = '';
  78. $request = upgrade_query("
  79. SELECT id_attach, id_folder, filename, file_hash
  80. FROM {$db_prefix}attachments
  81. WHERE attachment_type != 1
  82. LIMIT $_GET[a], 100");
  83. // Finished?
  84. if ($smcFunc['db_num_rows']($request) == 0)
  85. $is_done = true;
  86. while ($row = $smcFunc['db_fetch_assoc']($request))
  87. {
  88. // The current folder.
  89. $currentFolder = !empty($modSettings['currentAttachmentUploadDir']) ? $modSettings['attachmentUploadDir'][$row['id_folder']] : $modSettings['attachmentUploadDir'];
  90. // Old School?
  91. if (empty($row['file_hash']))
  92. {
  93. // Remove international characters (windows-1252)
  94. // These lines should never be needed again. Still, behave.
  95. if (empty($db_character_set) || $db_character_set != 'utf8')
  96. {
  97. $row['filename'] = strtr($row['filename'],
  98. "\x8a\x8e\x9a\x9e\x9f\xc0\xc1\xc2\xc3\xc4\xc5\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xd9\xda\xdb\xdc\xdd\xe0\xe1\xe2\xe3\xe4\xe5\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf1\xf2\xf3\xf4\xf5\xf6\xf8\xf9\xfa\xfb\xfc\xfd\xff",
  99. 'SZszYAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy');
  100. $row['filename'] = strtr($row['filename'], array("\xde" => 'TH', "\xfe" =>
  101. 'th', "\xd0" => 'DH', "\xf0" => 'dh', "\xdf" => 'ss', "\x8c" => 'OE',
  102. "\x9c" => 'oe', "\xc6" => 'AE', "\xe6" => 'ae', "\xb5" => 'u'));
  103. }
  104. // Sorry, no spaces, dots, or anything else but letters allowed.
  105. $row['filename'] = preg_replace(array('/\s/', '/[^\w_\.\-]/'), array('_', ''), $row['filename']);
  106. // Create a nice hash.
  107. $fileHash = sha1(md5($row['filename'] . time()) . mt_rand());
  108. // The old file, we need to know if the filename was encrypted or not.
  109. if (file_exists($currentFolder . '/' . $row['id_attach']. '_' . strtr($row['filename'], '.', '_') . md5($row['filename'])))
  110. $oldFile = $currentFolder . '/' . $row['id_attach']. '_' . strtr($row['filename'], '.', '_') . md5($row['filename']);
  111. else if (file_exists($currentFolder . '/' . $row['filename']));
  112. $oldFile = $currentFolder . '/' . $row['filename'];
  113. // Build the new file.
  114. $newFile = $currentFolder . '/' . $row['id_attach'] . '_' . $fileHash .'.dat';
  115. }
  116. // Just rename the file.
  117. else
  118. {
  119. $oldFile = $currentFolder . '/' . $row['id_attach'] . '_' . $row['file_hash'];
  120. $newFile = $currentFolder . '/' . $row['id_attach'] . '_' . $row['file_hash'] .'.dat';
  121. }
  122. // And we try to move it.
  123. rename($oldFile, $newFile);
  124. // Only update this if it was successful and the file was using the old system.
  125. if (empty($row['file_hash']) && !empty($fileHash) && file_exists($newFile) && !file_exists($oldFile))
  126. upgrade_query("
  127. UPDATE {$db_prefix}attachments
  128. SET file_hash = '$fileHash'
  129. WHERE id_attach = $row[id_attach]");
  130. }
  131. $smcFunc['db_free_result']($request);
  132. $_GET['a'] += 100;
  133. $step_progress['current'] = $_GET['a'];
  134. }
  135. unset($_GET['a']);
  136. ---}
  137. ---#
  138. /******************************************************************************/
  139. --- Adding support for IPv6...
  140. /******************************************************************************/
  141. ---# Adding new columns to ban items...
  142. ALTER TABLE {$db_prefix}ban_items
  143. ADD COLUMN ip_low5 smallint NOT NULL DEFAULT '0',
  144. ADD COLUMN ip_high5 smallint NOT NULL DEFAULT '0',
  145. ADD COLUMN ip_low6 smallint NOT NULL DEFAULT '0',
  146. ADD COLUMN ip_high6 smallint NOT NULL DEFAULT '0',
  147. ADD COLUMN ip_low7 smallint NOT NULL DEFAULT '0',
  148. ADD COLUMN ip_high7 smallint NOT NULL DEFAULT '0',
  149. ADD COLUMN ip_low8 smallint NOT NULL DEFAULT '0',
  150. ADD COLUMN ip_high8 smallint NOT NULL DEFAULT '0';
  151. ---#
  152. ---# Changing existing columns to ban items...
  153. ---{
  154. upgrade_query("
  155. ALTER TABLE {$db_prefix}ban_items
  156. ALTER COLUMN ip_low1 type smallint,
  157. ALTER COLUMN ip_high1 type smallint,
  158. ALTER COLUMN ip_low2 type smallint,
  159. ALTER COLUMN ip_high2 type smallint,
  160. ALTER COLUMN ip_low3 type smallint,
  161. ALTER COLUMN ip_high3 type smallint,
  162. ALTER COLUMN ip_low4 type smallint,
  163. ALTER COLUMN ip_high4 type smallint;"
  164. );
  165. upgrade_query("
  166. ALTER TABLE {$db_prefix}ban_items
  167. ALTER COLUMN ip_low1 SET DEFAULT '0',
  168. ALTER COLUMN ip_high1 SET DEFAULT '0',
  169. ALTER COLUMN ip_low2 SET DEFAULT '0',
  170. ALTER COLUMN ip_high2 SET DEFAULT '0',
  171. ALTER COLUMN ip_low3 SET DEFAULT '0',
  172. ALTER COLUMN ip_high3 SET DEFAULT '0',
  173. ALTER COLUMN ip_low4 SET DEFAULT '0',
  174. ALTER COLUMN ip_high4 SET DEFAULT '0';"
  175. );
  176. upgrade_query("
  177. ALTER TABLE {$db_prefix}ban_items
  178. ALTER COLUMN ip_low1 SET NOT NULL,
  179. ALTER COLUMN ip_high1 SET NOT NULL,
  180. ALTER COLUMN ip_low2 SET NOT NULL,
  181. ALTER COLUMN ip_high2 SET NOT NULL,
  182. ALTER COLUMN ip_low3 SET NOT NULL,
  183. ALTER COLUMN ip_high3 SET NOT NULL,
  184. ALTER COLUMN ip_low4 SET NOT NULL,
  185. ALTER COLUMN ip_high4 SET NOT NULL;"
  186. );
  187. ---}
  188. ---#
  189. /******************************************************************************/
  190. --- Adding support for logging who fulfils a group request.
  191. /******************************************************************************/
  192. ---# Adding new columns to log_group_requests
  193. ALTER TABLE {$db_prefix}log_group_requests
  194. ADD COLUMN status smallint NOT NULL default '0',
  195. ADD COLUMN id_member_acted int NOT NULL default '0',
  196. ADD COLUMN member_name_acted varchar(255) NOT NULL default '',
  197. ADD COLUMN time_acted int NOT NULL default '0',
  198. ADD COLUMN act_reason text NOT NULL;
  199. ---#
  200. ---# Adjusting the indexes for log_group_requests
  201. DROP INDEX {$db_prefix}log_group_requests_id_member;
  202. CREATE INDEX {$db_prefix}log_group_requests_id_member ON {$db_prefix}log_group_requests (id_member, id_group);
  203. ---#
  204. /******************************************************************************/
  205. --- Adding support for <credits> tag in package manager
  206. /******************************************************************************/
  207. ---# Adding new columns to log_packages ..
  208. ALTER TABLE {$db_prefix}log_packages
  209. ADD COLUMN credits varchar(255) NOT NULL DEFAULT '';
  210. ---#
  211. /******************************************************************************/
  212. --- Adding more space for session ids
  213. /******************************************************************************/
  214. ---# Altering the session_id columns...
  215. ---{
  216. upgrade_query("
  217. ALTER TABLE {$db_prefix}log_online
  218. ALTER COLUMN session type varchar(64);
  219. ALTER TABLE {$db_prefix}log_errors
  220. ALTER COLUMN session type char(64);
  221. ALTER TABLE {$db_prefix}sessions
  222. ALTER COLUMN session_id type char(64);");
  223. upgrade_query("
  224. ALTER TABLE {$db_prefix}log_online
  225. ALTER COLUMN session SET DEFAULT '';
  226. ALTER TABLE {$db_prefix}log_errors
  227. ALTER COLUMN session SET default ' ';");
  228. upgrade_query("
  229. ALTER TABLE {$db_prefix}log_online
  230. ALTER COLUMN session SET NOT NULL;
  231. ALTER TABLE {$db_prefix}log_errors
  232. ALTER COLUMN session SET NOT NULL;
  233. ALTER TABLE {$db_prefix}sessions
  234. ALTER COLUMN session_id SET NOT NULL;");
  235. ---}
  236. ---#
  237. /******************************************************************************/
  238. --- Adding support for MOVED topics enhancements
  239. /******************************************************************************/
  240. ---# Adding new columns to topics table
  241. ---{
  242. upgrade_query("
  243. ALTER TABLE {$db_prefix}topics
  244. ADD COLUMN redirect_expires int NOT NULL DEFAULT '0'");
  245. upgrade_query("
  246. ALTER TABLE {$db_prefix}topics
  247. ADD COLUMN id_redirect_topic int NOT NULL DEFAULT '0'");
  248. ---}
  249. ---#
  250. /******************************************************************************/
  251. --- Adding new scheduled tasks
  252. /******************************************************************************/
  253. ---# Adding new scheduled tasks
  254. INSERT INTO {$db_prefix}scheduled_tasks
  255. (next_time, time_offset, time_regularity, time_unit, disabled, task)
  256. VALUES
  257. (0, 120, 1, 'd', 0, 'remove_temp_attachments');
  258. INSERT INTO {$db_prefix}scheduled_tasks
  259. (next_time, time_offset, time_regularity, time_unit, disabled, task)
  260. VALUES
  261. (0, 180, 1, 'd', 0, 'remove_topic_redirect');
  262. INSERT INTO {$db_prefix}scheduled_tasks
  263. (next_time, time_offset, time_regularity, time_unit, disabled, task)
  264. VALUES
  265. (0, 240, 1, 'd', 0, 'remove_old_drafts');
  266. ---#
  267. /******************************************************************************/
  268. ---- Adding background tasks support
  269. /******************************************************************************/
  270. ---# Adding the sequence
  271. CREATE SEQUENCE {$db_prefix}background_tasks_seq;
  272. ---#
  273. ---# Adding the table
  274. CREATE TABLE {$db_prefix}background_tasks (
  275. id_task int default nextval('{$db_prefix}background_tasks_seq'),
  276. task_file varchar(255) NOT NULL default '',
  277. task_class varchar(255) NOT NULL default '',
  278. task_data text NOT NULL,
  279. claimed_time int NOT NULL default '0',
  280. PRIMARY KEY (id_task)
  281. );
  282. ---#
  283. /******************************************************************************/
  284. ---- Replacing MSN with Skype
  285. /******************************************************************************/
  286. ---# Modifying the "msn" column...
  287. ALTER TABLE {$db_prefix}members
  288. RENAME msn TO skype;
  289. ---#
  290. /******************************************************************************/
  291. --- Adding support for deny boards access
  292. /******************************************************************************/
  293. ---# Adding new columns to boards...
  294. ---{
  295. upgrade_query("
  296. ALTER TABLE {$db_prefix}boards
  297. ADD COLUMN deny_member_groups varchar(255) NOT NULL DEFAULT ''");
  298. ---}
  299. ---#
  300. /******************************************************************************/
  301. --- Updating board access rules
  302. /******************************************************************************/
  303. ---# Updating board access rules
  304. ---{
  305. $member_groups = array(
  306. 'allowed' => array(),
  307. 'denied' => array(),
  308. );
  309. $request = $smcFunc['db_query']('', '
  310. SELECT id_group, add_deny
  311. FROM {db_prefix}permissions
  312. WHERE permission = {string:permission}',
  313. array(
  314. 'permission' => 'manage_boards',
  315. )
  316. );
  317. while ($row = $smcFunc['db_fetch_assoc']($request))
  318. $member_groups[$row['add_deny'] === '1' ? 'allowed' : 'denied'][] = $row['id_group'];
  319. $smcFunc['db_free_result']($request);
  320. $member_groups = array_diff($member_groups['allowed'], $member_groups['denied']);
  321. if (!empty($member_groups))
  322. {
  323. $count = count($member_groups);
  324. $changes = array();
  325. $request = $smcFunc['db_query']('', '
  326. SELECT id_board, member_groups
  327. FROM {db_prefix}boards');
  328. while ($row = $smcFunc['db_fetch_assoc']($request))
  329. {
  330. $current_groups = explode(',', $row['member_groups']);
  331. if (count(array_intersect($current_groups, $member_groups)) != $count)
  332. {
  333. $new_groups = array_unique(array_merge($current_groups, $member_groups));
  334. $changes[$row['id_board']] = implode(',', $new_groups);
  335. }
  336. }
  337. $smcFunc['db_free_result']($request);
  338. if (!empty($changes))
  339. {
  340. foreach ($changes as $id_board => $member_groups)
  341. $smcFunc['db_query']('', '
  342. UPDATE {db_prefix}boards
  343. SET member_groups = {string:member_groups}
  344. WHERE id_board = {int:id_board}',
  345. array(
  346. 'member_groups' => $member_groups,
  347. 'id_board' => $id_board,
  348. )
  349. );
  350. }
  351. }
  352. ---}
  353. ---#
  354. /******************************************************************************/
  355. --- Adding support for category descriptions
  356. /******************************************************************************/
  357. ---# Adding new columns to categories...
  358. ---{
  359. // Sadly, PostgreSQL whines if we add a NOT NULL column without a default value to an existing table...
  360. upgrade_query("
  361. ALTER TABLE {$db_prefix}categories
  362. ADD COLUMN description text");
  363. upgrade_query("
  364. UPDATE {$db_prefix}categories
  365. SET description = ''");
  366. upgrade_query("
  367. ALTER TABLE {$db_prefix}categories
  368. ALTER COLUMN description SET NOT NULL");
  369. ---}
  370. ---#
  371. /******************************************************************************/
  372. --- Adding support for alerts
  373. /******************************************************************************/
  374. ---# Adding the count to the members table...
  375. ALTER TABLE {$db_prefix}members
  376. ADD COLUMN alerts int NOT NULL default '0';
  377. ---#
  378. ---# Adding the new table for alerts.
  379. CREATE SEQUENCE {$db_prefix}user_alerts_seq;
  380. CREATE TABLE {$db_prefix}user_alerts (
  381. id_alert int default nextval('{$db_prefix}user_alerts_seq'),
  382. alert_time int NOT NULL default '0',
  383. id_member int NOT NULL default '0',
  384. id_member_started int NOT NULL default '0',
  385. member_name varchar(255) NOT NULL default '',
  386. content_type varchar(255) NOT NULL default '',
  387. content_id int NOT NULL default '0',
  388. content_action varchar(255) NOT NULL default '',
  389. is_read smallint NOT NULL default '0',
  390. extra text NOT NULL,
  391. PRIMARY KEY (id_alert)
  392. );
  393. CREATE INDEX {$db_prefix}user_alerts_id_member ON {$db_prefix}user_alerts (id_member);
  394. CREATE INDEX {$db_prefix}user_alerts_alert_time ON {$db_prefix}user_alerts (alert_time);
  395. ---#
  396. ---# Adding alert preferences.
  397. CREATE TABLE {$db_prefix}user_alerts_prefs (
  398. id_member int NOT NULL default '0',
  399. alert_pref varchar(32) NOT NULL default '',
  400. alert_value smallint NOT NULL default '0',
  401. PRIMARY KEY (id_member, alert_pref)
  402. );
  403. INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'member_group_request', 1);
  404. INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'member_register', 1);
  405. INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'msg_like', 1);
  406. INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'msg_report', 1);
  407. INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'msg_report_reply', 1);
  408. ---#
  409. /******************************************************************************/
  410. --- Adding support for topic unwatch
  411. /******************************************************************************/
  412. ---# Adding new columns to log_topics...
  413. ---{
  414. upgrade_query("
  415. ALTER TABLE {$db_prefix}log_topics
  416. ADD COLUMN unwatched int NOT NULL DEFAULT '0'");
  417. UPDATE {$db_prefix}log_topics
  418. SET unwatched = 0;
  419. INSERT INTO {$db_prefix}settings
  420. (variable, value)
  421. VALUES
  422. ('enable_unwatch', 0);
  423. ---}
  424. ---#
  425. ---# Fixing column name change...
  426. ---{
  427. upgrade_query("
  428. ALTER TABLE {$db_prefix}log_topics
  429. RENAME disregarded TO unwatched");
  430. ---}
  431. ---#
  432. /******************************************************************************/
  433. --- Name changes
  434. /******************************************************************************/
  435. ---# Altering the membergroup stars to icons
  436. ---{
  437. upgrade_query("
  438. ALTER TABLE {$db_prefix}membergroups
  439. RENAME stars TO icons");
  440. ---}
  441. ---#
  442. ---# Renaming default theme...
  443. UPDATE {$db_prefix}themes
  444. SET value = 'SMF Default Theme - Curve2'
  445. WHERE value LIKE 'SMF Default Theme%';
  446. ---#
  447. ---# Adding the enableThemes setting.
  448. INSERT INTO {$db_prefix}settings
  449. (variable, value)
  450. VALUES
  451. ('enableThemes', '1');
  452. ---#
  453. ---# Setting "default" as the default...
  454. UPDATE {$db_prefix}settings
  455. SET value = '1'
  456. WHERE variable = 'theme_guests';
  457. UPDATE {$db_prefix}boards
  458. SET id_theme = 0;
  459. UPDATE {$db_prefix}members
  460. SET id_theme = 0;
  461. ---#
  462. /******************************************************************************/
  463. --- Cleaning up after old themes...
  464. /******************************************************************************/
  465. ---# Checking for "core" and removing it if necessary...
  466. ---{
  467. // Do they have "core" installed?
  468. if (file_exists($GLOBALS['boarddir'] . '/Themes/core'))
  469. {
  470. $core_dir = $GLOBALS['boarddir'] . '/Themes/core';
  471. $theme_request = upgrade_query("
  472. SELECT id_theme
  473. FROM {$db_prefix}themes
  474. WHERE variable = 'theme_dir'
  475. AND value ='$core_dir'");
  476. // Don't do anything if this theme is already uninstalled
  477. if ($smcFunc['db_num_rows']($theme_request) == 1)
  478. {
  479. list($id_theme) = $smcFunc['db_fetch_row']($theme_request, 0);
  480. $smcFunc['db_free_result']($theme_request);
  481. $known_themes = explode(', ', $modSettings['knownThemes']);
  482. // Remove this value...
  483. $known_themes = array_diff($known_themes, array($id_theme));
  484. // Change back to a string...
  485. $known_themes = implode(', ', $known_themes);
  486. // Update the database
  487. upgrade_query("
  488. UPDATE {$db_prefix}settings
  489. SET value = '$known_themes'
  490. WHERE variable = 'knownThemes'");
  491. // Delete any info about this theme
  492. upgrade_query("
  493. DELETE FROM {$db_prefix}themes
  494. WHERE id_theme = $id_theme");
  495. }
  496. }
  497. ---}
  498. ---#
  499. /******************************************************************************/
  500. --- Adding support for drafts
  501. /******************************************************************************/
  502. ---# Creating drafts table.
  503. CREATE SEQUENCE {$db_prefix}user_drafts_seq;
  504. CREATE TABLE {$db_prefix}user_drafts (
  505. id_draft int NOT NULL default nextval('{$db_prefix}user_drafts_seq'),
  506. id_topic int NOT NULL default '0',
  507. id_board smallint NOT NULL default '0',
  508. id_reply int NOT NULL default '0',
  509. type smallint NOT NULL default '0',
  510. poster_time int NOT NULL default '0',
  511. id_member int NOT NULL default '0',
  512. subject varchar(255) NOT NULL default '',
  513. smileys_enabled smallint NOT NULL default '1',
  514. body text NOT NULL,
  515. icon varchar(16) NOT NULL default 'xx',
  516. locked smallint NOT NULL default '0',
  517. is_sticky smallint NOT NULL default '0',
  518. to_list varchar(255) NOT NULL default '',
  519. PRIMARY KEY (id_draft)
  520. );
  521. CREATE UNIQUE INDEX {$db_prefix}user_drafts_id_member ON {$db_prefix}user_drafts (id_member, id_draft, type);
  522. ---#
  523. ---# Adding draft permissions...
  524. ---{
  525. // We cannot do this twice
  526. if (@$modSettings['smfVersion'] < '2.1')
  527. {
  528. // Anyone who can currently post unapproved topics we assume can create drafts as well ...
  529. $request = upgrade_query("
  530. SELECT id_group, id_board, add_deny, permission
  531. FROM {$db_prefix}board_permissions
  532. WHERE permission = 'post_unapproved_topics'");
  533. $inserts = array();
  534. while ($row = $smcFunc['db_fetch_assoc']($request))
  535. {
  536. $inserts[] = "($row[id_group], $row[id_board], 'post_draft', $row[add_deny])";
  537. $inserts[] = "($row[id_group], $row[id_board], 'post_autosave_draft', $row[add_deny])";
  538. }
  539. $smcFunc['db_free_result']($request);
  540. if (!empty($inserts))
  541. {
  542. foreach ($inserts AS $insert)
  543. {
  544. upgrade_query("
  545. INSERT INTO {$db_prefix}board_permissions
  546. (id_group, id_board, permission, add_deny)
  547. VALUES
  548. " . $insert);
  549. }
  550. }
  551. // Next we find people who can send PMs, and assume they can save pm_drafts as well
  552. $request = upgrade_query("
  553. SELECT id_group, add_deny, permission
  554. FROM {$db_prefix}permissions
  555. WHERE permission = 'pm_send'");
  556. $inserts = array();
  557. while ($row = $smcFunc['db_fetch_assoc']($request))
  558. {
  559. $inserts[] = "($row[id_group], 'pm_draft', $row[add_deny])";
  560. $inserts[] = "($row[id_group], 'pm_autosave_draft', $row[add_deny])";
  561. }
  562. $smcFunc['db_free_result']($request);
  563. if (!empty($inserts))
  564. {
  565. foreach ($inserts AS $insert)
  566. {
  567. upgrade_query("
  568. INSERT INTO {$db_prefix}permissions
  569. (id_group, permission, add_deny)
  570. VALUES
  571. " . $insert);
  572. }
  573. }
  574. }
  575. ---}
  576. INSERT INTO {$db_prefix}settings (variable, value) VALUES ('drafts_autosave_enabled', '1');
  577. INSERT INTO {$db_prefix}settings (variable, value) VALUES ('drafts_show_saved_enabled', '1');
  578. INSERT INTO {$db_prefix}settings (variable, value) VALUES ('drafts_keep_days', '7');
  579. INSERT INTO {$db_prefix}themes (id_theme, variable, value) VALUES ('1', 'drafts_autosave_enabled', '1');
  580. INSERT INTO {$db_prefix}themes (id_theme, variable, value) VALUES ('1', 'drafts_show_saved_enabled', '1');
  581. ---#
  582. /******************************************************************************/
  583. --- Adding support for likes
  584. /******************************************************************************/
  585. ---# Creating likes table.
  586. CREATE TABLE {$db_prefix}user_likes (
  587. id_member int NOT NULL default '0',
  588. content_type char(6) default '',
  589. content_id int NOT NULL default '0',
  590. like_time int NOT NULL default '0',
  591. PRIMARY KEY (content_id, content_type, id_member)
  592. );
  593. CREATE INDEX {$db_prefix}user_likes_content ON {$db_prefix}user_likes (content_id, content_type);
  594. CREATE INDEX {$db_prefix}user_likes_liker ON {$db_prefix}user_likes (id_member);
  595. ---#
  596. ---# Adding count to the messages table.
  597. ALTER TABLE {$db_prefix}messages
  598. ADD COLUMN likes smallint NOT NULL default '0';
  599. ---#
  600. /******************************************************************************/
  601. --- Adding support for group-based board moderation
  602. /******************************************************************************/
  603. ---# Creating moderator_groups table
  604. CREATE TABLE {$db_prefix}moderator_groups (
  605. id_board smallint NOT NULL default '0',
  606. id_group smallint NOT NULL default '0',
  607. PRIMARY KEY (id_board, id_group)
  608. );
  609. ---#
  610. /******************************************************************************/
  611. --- Cleaning up integration hooks
  612. /******************************************************************************/
  613. ---#
  614. DELETE FROM {$db_prefix}settings
  615. WHERE variable LIKE 'integrate_%';
  616. ---#
  617. /******************************************************************************/
  618. --- Cleaning up old settings
  619. /******************************************************************************/
  620. ---# Updating the default time format
  621. ---{
  622. if (!empty($modSettings['time_format']))
  623. {
  624. // First, use the shortened form of the month in the date.
  625. $time_format = str_replace('%B', '%b', $modSettings['time_format']);
  626. // Second, shorten the time to stop including seconds.
  627. $time_format = str_replace(':%S', '', $time_format);
  628. // Then, update the database.
  629. $smcFunc['db_query']('', '
  630. UPDATE {db_prefix}settings
  631. SET value = {string:new_format}
  632. WHERE variable = {literal:time_format}',
  633. array(
  634. 'new_format' => $time_format,
  635. )
  636. );
  637. }
  638. ---}
  639. ---#
  640. ---# Fixing a deprecated option.
  641. UPDATE {$db_prefix}settings
  642. SET value = 'option_css_resize'
  643. WHERE variable = 'avatar_action_too_large'
  644. AND (value = 'option_html_resize' OR value = 'option_js_resize');
  645. ---#
  646. ---# Cleaning up the old Core Features page.
  647. ---{
  648. // First get the original value
  649. $request = $smcFunc['db_query']('', '
  650. SELECT value
  651. FROM {db_prefix}settings
  652. WHERE variable = {literal:admin_features}');
  653. if ($smcFunc['db_num_rows']($request) > 0 && $row = $smcFunc['db_fetch_assoc']($request))
  654. {
  655. // Some of these *should* already be set but you never know.
  656. $new_settings = array();
  657. $admin_features = explode(',', $row['value']);
  658. // Now, let's just recap something.
  659. // cd = calendar, should also have set cal_enabled already
  660. // cp = custom profile fields, which already has several fields that cover tracking
  661. // k = karma, should also have set karmaMode already
  662. // ps = paid subs, should also have set paid_enabled already
  663. // rg = reports generation, which is now permanently on
  664. // sp = spider tracking, should also have set spider_mode already
  665. // w = warning system, which will be covered with warning_settings
  666. // The rest we have to deal with manually.
  667. // Moderation log - modlog_enabled itself should be set but we have others now
  668. if (in_array('ml', $admin_features))
  669. {
  670. $new_settings[] = array('adminlog_enabled', '1');
  671. $new_settings[] = array('userlog_enabled', '1');
  672. }
  673. // Post moderation
  674. if (in_array('pm', $admin_features))
  675. {
  676. $new_settings[] = array('postmod_active', '1');
  677. }
  678. // And now actually apply it.
  679. if (!empty($new_settings))
  680. {
  681. $smcFunc['db_insert']('replace',
  682. '{db_prefix}settings',
  683. array('variable' => 'string', 'value' => 'string'),
  684. $new_settings,
  685. array('variable')
  686. );
  687. }
  688. }
  689. $smcFunc['db_free_result']($request);
  690. ---}
  691. ---#
  692. ---# Cleaning up old settings.
  693. DELETE FROM {$db_prefix}settings
  694. WHERE variable IN ('enableStickyTopics', 'guest_hideContacts', 'notify_new_registration', 'attachmentEncryptFilenames', 'hotTopicPosts', 'hotTopicVeryPosts', 'fixLongWords', 'admin_features');
  695. ---#
  696. ---# Cleaning up old theme settings.
  697. DELETE FROM {$db_prefix}themes
  698. WHERE variable IN ('show_board_desc', 'no_new_reply_warning', 'display_quick_reply', 'show_mark_read');
  699. ---#
  700. /******************************************************************************/
  701. --- Updating files that fetched from simplemachines.org
  702. /******************************************************************************/
  703. ---# We no longer call on several files.
  704. DELETE FROM {$db_prefix}admin_info_files
  705. WHERE filename IN ('latest-packages.js', 'latest-support.js', 'latest-themes.js')
  706. AND path = '/smf/';
  707. ---#
  708. ---# But we do need new files.
  709. ---{
  710. $smcFunc['db_insert']('',
  711. '{db_prefix}admin_info_files',
  712. array('filename' => 'string', 'path' => 'string', 'parameters' => 'string', 'data' => 'string', 'filetype' => 'string'),
  713. array('latest-versions.txt', '/smf/', 'version=%3$s', '', 'text/plain'),
  714. array('id_file')
  715. );
  716. ---}
  717. ---#
  718. /******************************************************************************/
  719. --- Upgrading "verification questions" feature
  720. /******************************************************************************/
  721. ---# Creating qanda table
  722. CREATE SEQUENCE {$db_prefix}qanda_seq;
  723. CREATE TABLE {$db_prefix}qanda (
  724. id_question smallint NOT NULL default nextval('{$db_prefix}qanda_seq'),
  725. lngfile varchar(255) NOT NULL default '',
  726. question varchar(255) NOT NULL default '',
  727. answers text NOT NULL,
  728. PRIMARY KEY (id_question),
  729. KEY lngfile (lngfile)
  730. );
  731. ---#
  732. ---# Moving questions and answers to the new table
  733. ---{
  734. $questions = array();
  735. $get_questions = upgrade_query("
  736. SELECT body AS question, recipient_name AS answer
  737. FROM {$db_prefix}log_comments
  738. WHERE comment_type = 'ver_test'");
  739. while ($row = $smcFunc['db_fetch_assoc']($get_questions))
  740. $questions[] = array($language, $row['question'], serialize(array($row['answer'])));
  741. $smcFunc['db_free_result']($get_questions);
  742. if (!empty($questions))
  743. {
  744. $smcFunc['db_insert']('',
  745. '{db_prefix}qanda',
  746. array('lngfile' => 'string', 'question' => 'string', 'answers' => 'string'),
  747. $questions,
  748. array('id_question')
  749. );
  750. // Delete the questions from log_comments now
  751. upgrade_query("
  752. DELETE FROM {$db_prefix}log_comments
  753. WHERE comment_type = 'ver_test'
  754. ");
  755. }
  756. ---}
  757. ---#
  758. /******************************************************************************/
  759. --- Fixing log_online table
  760. /******************************************************************************/
  761. ---# Changing ip to bigint
  762. ALTER TABLE {$db_prefix}log_online ALTER ip TYPE bigint;
  763. ---#
  764. /******************************************************************************/
  765. --- Marking packages as uninstalled...
  766. /******************************************************************************/
  767. ---# Updating log_packages
  768. UPDATE {$db_prefix}log_packages
  769. SET install_state = 0;
  770. ---#
  771. /******************************************************************************/
  772. --- Updating profile permissions...
  773. /******************************************************************************/
  774. ---# Removing the old "view your own profile" permission
  775. DELETE FROM {$db_prefix}permissions
  776. WHERE permission = 'profile_view_own';
  777. ---#
  778. ---# Updating the old "view any profile" permission
  779. UPDATE {$db_prefix}permissions
  780. SET permission = 'profile_view'
  781. WHERE permission = 'profile_view_any';
  782. ---#
  783. ---# Removing the old notification permissions
  784. DELETE FROM {$db_prefix}board_permissions
  785. WHERE permission = 'mark_notify' OR permission = 'mark_any_notify';
  786. ---#
  787. ---# Adding "profile_password_own"
  788. ---{
  789. $inserts = array();
  790. $request = upgrade_query("
  791. SELECT id_group, add_deny
  792. FROM {$db_prefix}permissions
  793. WHERE permission = 'profile_identity_own'");
  794. while ($row = $smcFunc['db_fetch_assoc']($request))
  795. {
  796. $inserts[] = "($row[id_group], 'profile_password_own', $row[add_deny])";
  797. }
  798. $smcFunc['db_free_result']($request);
  799. if (!empty($inserts))
  800. {
  801. foreach ($inserts as $insert)
  802. {
  803. upgrade_query("
  804. INSERT INTO {$db_prefix}permissions
  805. (id_group, permission, add_deny)
  806. VALUES
  807. " . $insert);
  808. }
  809. }
  810. ---}
  811. ---#
  812. ---# Adding other profile permissions
  813. ---{
  814. $inserts = array();
  815. $request = upgrade_query("
  816. SELECT id_group, add_deny
  817. FROM {$db_prefix}permissions
  818. WHERE permission = 'profile_extra_own'");
  819. while ($row = $smcFunc['db_fetch_assoc']($request))
  820. {
  821. $inserts[] = "($row[id_group], 'profile_blurb_own', $row[add_deny])";
  822. $inserts[] = "($row[id_group], 'profile_displayed_name_own', $row[add_deny])";
  823. $inserts[] = "($row[id_group], 'profile_forum_own', $row[add_deny])";
  824. $inserts[] = "($row[id_group], 'profile_other_own', $row[add_deny])";
  825. $inserts[] = "($row[id_group], 'profile_signature_own', $row[add_deny])";
  826. }
  827. $smcFunc['db_free_result']($request);
  828. if (!empty($inserts))
  829. {
  830. foreach ($inserts as $insert)
  831. {
  832. upgrade_query("
  833. INSERT INTO {$db_prefix}permissions
  834. (id_group, permission, add_deny)
  835. VALUES
  836. " . $insert
  837. );
  838. }
  839. }
  840. ---}
  841. ---#
  842. /******************************************************************************/
  843. --- Upgrading PM labels...
  844. /******************************************************************************/
  845. ---# Creating pm_labels sequence...
  846. CREATE SEQUENCE {$db_prefix}pm_labels_sequence;
  847. ---#
  848. ---# Adding pm_labels table...
  849. CREATE TABLE {$db_prefix}pm_labels (
  850. id_label int NOT NULL default nextval('{$db_prefix}pm_labels_seq'),
  851. id_member int NOT NULL default '0',
  852. name varchar(30) NOT NULL default '',
  853. PRIMARY KEY (id_label)
  854. );
  855. ---#
  856. ---# Adding pm_labeled_messages table...
  857. CREATE TABLE {$db_prefix}pm_labeled_messages (
  858. id_label int NOT NULL default '0',
  859. id_pm int NOT NULL default '0',
  860. PRIMARY KEY (id_label, id_pm)
  861. );
  862. ---#
  863. ---# Adding "in_inbox" column to pm_recipients
  864. ALTER TABLE {$db_prefix}pm_recipients
  865. ADD COLUMN in_inbox smallint NOT NULL default '1';
  866. ---#
  867. ---# Moving label info to new tables and updating rules...
  868. ---{
  869. // First see if we still have a message_labels column
  870. $results = $smcFunc['db_list_columns']('{db_prefix}members', false);
  871. if (in_array('message_labels', $results))
  872. {
  873. // They've still got it, so pull the label info
  874. $get_labels = $smcFunc['db_query']('', '
  875. SELECT id_member, message_labels
  876. FROM {db_prefix}members
  877. WHERE message_labels != {string:blank}',
  878. array(
  879. 'blank' => '',
  880. )
  881. );
  882. $inserts = array();
  883. $label_info = array();
  884. while ($row = $smcFunc['db_fetch_assoc']($get_labels))
  885. {
  886. // Stick this in an array
  887. $labels = explode(',', $row['message_labels']);
  888. // Build some inserts
  889. foreach ($labels AS $index => $label)
  890. {
  891. // Keep track of the index of this label - we'll need that in a bit...
  892. $label_info[$row['id_member']][$label] = $index;
  893. $inserts[] = array($row['id_member'], $label);
  894. }
  895. }
  896. $smcFunc['db_free_result']($get_labels);
  897. if (!empty($inserts))
  898. {
  899. $smcFunc['db_insert']('', '{db_prefix}pm_labels', array('id_member' => 'int', 'name' => 'string-30'), $inserts, array());
  900. // Clear this out for our next query below
  901. $inserts = array();
  902. }
  903. // This is the easy part - update the inbox stuff
  904. $smcFunc['db_query']('', '
  905. UPDATE {db_prefix}pm_recipients
  906. SET in_inbox = {int:in_inbox}
  907. WHERE FIND_IN_SET({int:minus_one}, labels)',
  908. array(
  909. 'in_inbox' => 1,
  910. 'minus_one' => -1,
  911. )
  912. );
  913. // Now we go pull the new IDs for each label
  914. $get_new_label_ids = $smcFunc['db_query']('', '
  915. SELECT *
  916. FROM {db_prefix}pm_labels',
  917. array(
  918. )
  919. );
  920. $label_info_2 = array();
  921. while ($label_row = $smcFunc['db_fetch_assoc']($get_new_label_ids))
  922. {
  923. // Map the old index values to the new ID values...
  924. $old_index = $label_info[$row['id_member']][$row['label_name']];
  925. $label_info_2[$row['id_member']][$old_index] = $row['id_label'];
  926. }
  927. $smcFunc['db_free_result']($get_new_label_ids);
  928. // Pull label info from pm_recipients
  929. // Ignore any that are only in the inbox
  930. $get_pm_labels = $smcFunc['db_query']('', '
  931. SELECT id_pm, id_member, labels
  932. FROM {db_prefix}pm_recipients
  933. WHERE deleted = {int:not_deleted}
  934. AND labels != {string:minus_one}',
  935. array(
  936. 'not_deleted' => 0,
  937. 'minus_one' => -1,
  938. )
  939. );
  940. while ($row = $smcFunc['db_fetch_assoc']($get_pm_labels))
  941. {
  942. $labels = explode(',', $row['labels']);
  943. foreach ($labels as $a_label)
  944. {
  945. if ($a_label == '-1')
  946. continue;
  947. $new_label_info = $label_info_2[$row['id_member']][$a_label];
  948. $inserts[] = array($row['id_pm'], $new_label_info);
  949. }
  950. }
  951. $smcFunc['db_free_result']($get_pm_labels);
  952. // Insert the new data
  953. if (!empty($inserts))
  954. {
  955. $smcFunc['db_insert']('', '{db_prefix}pm_labeled_messages', array('id_pm' => 'int', 'id_label' => 'int'), $inserts, array());
  956. }
  957. // Final step of this ridiculously massive process
  958. $get_pm_rules = $smcFunc['db_query']('', '
  959. SELECT id_member, id_rule, actions
  960. FROM {db_prefix}pm_rules',
  961. array(
  962. ),
  963. );
  964. // Go through the rules, unserialize the actions, then figure out if there's anything we can use
  965. while ($row = $smcFunc['db_fetch_assoc']($get_pm_rules))
  966. {
  967. // Turn this into an array...
  968. $actions = unserialize($row['actions']);
  969. // Loop through the actions and see if we're applying a label anywhere
  970. foreach ($actions as $index => $action)
  971. {
  972. if ($action['t'] == 'lab')
  973. {
  974. // Update the value of this label...
  975. $actions[$index]['v'] = $label_info_2[$row['id_member']][$action['v']];
  976. }
  977. }
  978. // Put this back into a string
  979. $actions = serialize($actions);
  980. $smcFunc['db_query']('', '
  981. UPDATE {db_prefix}pm_rules
  982. SET actions = {string:actions}
  983. WHERE id_rule = {int:id_rule}',
  984. array(
  985. 'actions' => $actions,
  986. 'id_rule' => $row['id_rule'],
  987. )
  988. );
  989. }
  990. $smcFunc['db_free_result']($get_pm_rules);
  991. // Lastly, we drop the old columns
  992. $smcFunc['db_remove_column']('{db_prefix}members', 'message_labels');
  993. $smcFunc['db_remove_column']('{db_prefix}pm_recipients', 'labels');
  994. }
  995. }
  996. /******************************************************************************/
  997. --- Adding support for edit reasons
  998. /******************************************************************************/
  999. ---# Adding "modified_reason" column to messages
  1000. ALTER TABLE {$db_prefix}messages
  1001. ADD COLUMN modified_reason varchar(255) NOT NULL default '';
  1002. ---#
  1003. /******************************************************************************/
  1004. --- Cleaning up guest permissions
  1005. /******************************************************************************/
  1006. ---# Removing permissions guests can no longer have...
  1007. ---{
  1008. $illegal_board_permissions = array(
  1009. 'announce_topic',
  1010. 'delete_any',
  1011. 'lock_any',
  1012. 'make_sticky',
  1013. 'merge_any',
  1014. 'modify_any',
  1015. 'modify_replies',
  1016. 'move_any',
  1017. 'poll_add_any',
  1018. 'poll_edit_any',
  1019. 'poll_lock_any',
  1020. 'poll_remove_any',
  1021. 'remove_any',
  1022. 'report_any',
  1023. 'split_any'
  1024. );
  1025. $illegal_permissions = array('calendar_edit_any', 'moderate_board', 'moderate_forum', 'send_email_to_members');
  1026. $smcFunc['db_query']('', '
  1027. DELETE FROM {db_prefix}board_permissions
  1028. WHERE id_group = {int:guests}
  1029. AND permission IN ({array_string:illegal_board_perms})',
  1030. array(
  1031. 'guests' => -1,
  1032. 'illegal_board_perms' => $illegal_board_permissions,
  1033. )
  1034. );
  1035. $smcFunc['db_query']('', '
  1036. DELETE FROM {db_prefix}permissions
  1037. WHERE id_group = {int:guests}
  1038. AND permission IN ({array_string:illegal_perms})',
  1039. array(
  1040. 'guests' => -1,
  1041. 'illegal_perms' => $illegal_permissions,
  1042. )
  1043. );
  1044. ---}
  1045. ---#
  1046. /******************************************************************************/
  1047. --- Adding mail queue settings
  1048. /******************************************************************************/
  1049. ---#
  1050. ---{
  1051. if (empty($modSettings['mail_limit']))
  1052. {
  1053. $smcFunc['db_insert']('replace',
  1054. '{db_prefix}settings',
  1055. array('variable' => 'string-255', 'value' => 'string'),
  1056. array(
  1057. array('mail_limit', '5'),
  1058. array('mail_quantity', '5'),
  1059. ),
  1060. array('variable')
  1061. );
  1062. }
  1063. ---}
  1064. ---#