DbPackages-mysql.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. <?php
  2. /**
  3. * This file contains database functionality specifically designed for packages (mods) to utilize.
  4. *
  5. * Simple Machines Forum (SMF)
  6. *
  7. * @package SMF
  8. * @author Simple Machines http://www.simplemachines.org
  9. * @copyright 2011 Simple Machines
  10. * @license http://www.simplemachines.org/about/smf/license.php BSD
  11. *
  12. * @version 2.1 Alpha 1
  13. */
  14. if (!defined('SMF'))
  15. die('Hacking attempt...');
  16. /**
  17. * Add the file functions to the $smcFunc array.
  18. */
  19. function db_packages_init()
  20. {
  21. global $smcFunc, $reservedTables, $db_package_log, $db_prefix;
  22. if (!isset($smcFunc['db_create_table']) || $smcFunc['db_create_table'] != 'smf_db_create_table')
  23. {
  24. $smcFunc += array(
  25. 'db_add_column' => 'smf_db_add_column',
  26. 'db_add_index' => 'smf_db_add_index',
  27. 'db_calculate_type' => 'smf_db_calculate_type',
  28. 'db_change_column' => 'smf_db_change_column',
  29. 'db_create_table' => 'smf_db_create_table',
  30. 'db_drop_table' => 'smf_db_drop_table',
  31. 'db_table_structure' => 'smf_db_table_structure',
  32. 'db_list_columns' => 'smf_db_list_columns',
  33. 'db_list_indexes' => 'smf_db_list_indexes',
  34. 'db_remove_column' => 'smf_db_remove_column',
  35. 'db_remove_index' => 'smf_db_remove_index',
  36. );
  37. $db_package_log = array();
  38. }
  39. // We setup an array of SMF tables we can't do auto-remove on - in case a mod writer cocks it up!
  40. $reservedTables = array('admin_info_files', 'approval_queue', 'attachments', 'ban_groups', 'ban_items',
  41. 'board_permissions', 'boards', 'calendar', 'calendar_holidays', 'categories', 'collapsed_categories',
  42. 'custom_fields', 'group_moderators', 'log_actions', 'log_activity', 'log_banned', 'log_boards',
  43. 'log_digest', 'log_errors', 'log_floodcontrol', 'log_group_requests', 'log_karma', 'log_mark_read',
  44. 'log_notify', 'log_online', 'log_packages', 'log_polls', 'log_reported', 'log_reported_comments',
  45. 'log_scheduled_tasks', 'log_search_messages', 'log_search_results', 'log_search_subjects',
  46. 'log_search_topics', 'log_topics', 'mail_queue', 'membergroups', 'members', 'message_icons',
  47. 'messages', 'moderators', 'package_servers', 'permission_profiles', 'permissions', 'personal_messages',
  48. 'pm_recipients', 'poll_choices', 'polls', 'scheduled_tasks', 'sessions', 'settings', 'smileys',
  49. 'themes', 'topics');
  50. foreach ($reservedTables as $k => $table_name)
  51. $reservedTables[$k] = strtolower($db_prefix . $table_name);
  52. // We in turn may need the extra stuff.
  53. db_extend('extra');
  54. }
  55. /**
  56. * This function can be used to create a table without worrying about schema
  57. * compatabilities across supported database systems.
  58. * - If the table exists will, by default, do nothing.
  59. * - Builds table with columns as passed to it - at least one column must be sent.
  60. * The columns array should have one sub-array for each column - these sub arrays contain:
  61. * 'name' = Column name
  62. * 'type' = Type of column - values from (smallint, mediumint, int, text, varchar, char, tinytext, mediumtext, largetext)
  63. * 'size' => Size of column (If applicable) - for example 255 for a large varchar, 10 for an int etc.
  64. * If not set SMF will pick a size.
  65. * - 'default' = Default value - do not set if no default required.
  66. * - 'null' => Can it be null (true or false) - if not set default will be false.
  67. * - 'auto' => Set to true to make it an auto incrementing column. Set to a numerical value to set from what
  68. * it should begin counting.
  69. * - Adds indexes as specified within indexes parameter. Each index should be a member of $indexes. Values are:
  70. * - 'name' => Index name (If left empty SMF will generate).
  71. * - 'type' => Type of index. Choose from 'primary', 'unique' or 'index'. If not set will default to 'index'.
  72. * - 'columns' => Array containing columns that form part of key - in the order the index is to be created.
  73. * - parameters: (None yet)
  74. * - if_exists values:
  75. * - 'ignore' will do nothing if the table exists. (And will return true)
  76. * - 'overwrite' will drop any existing table of the same name.
  77. * - 'error' will return false if the table already exists.
  78. * @param string $table_name
  79. * @param array $columns, in the format specified.
  80. * @param array $indexes, default array(), in the format specified.
  81. * @param array $parameters, default array()
  82. * @param string $if_exists, default 'ignore'
  83. * @param string $error, default 'fatal'
  84. */
  85. function smf_db_create_table($table_name, $columns, $indexes = array(), $parameters = array(), $if_exists = 'ignore', $error = 'fatal')
  86. {
  87. global $reservedTables, $smcFunc, $db_package_log, $db_prefix, $db_character_set;
  88. // Strip out the table name, we might not need it in some cases
  89. $real_prefix = preg_match('~^(`?)(.+?)\\1\\.(.*?)$~', $db_prefix, $match) === 1 ? $match[3] : $db_prefix;
  90. // With or without the database name, the fullname looks like this.
  91. $full_table_name = str_replace('{db_prefix}', $real_prefix, $table_name);
  92. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  93. // First - no way do we touch SMF tables.
  94. if (in_array(strtolower($table_name), $reservedTables))
  95. return false;
  96. // Log that we'll want to remove this on uninstall.
  97. $db_package_log[] = array('remove_table', $table_name);
  98. // Slightly easier on MySQL than the others...
  99. $tables = $smcFunc['db_list_tables']();
  100. if (in_array($full_table_name, $tables))
  101. {
  102. // This is a sad day... drop the table? If not, return false (error) by default.
  103. if ($if_exists == 'overwrite')
  104. $smcFunc['db_drop_table']($table_name);
  105. else
  106. return $if_exists == 'ignore';
  107. }
  108. // Righty - let's do the damn thing!
  109. $table_query = 'CREATE TABLE ' . $table_name . "\n" . '(';
  110. foreach ($columns as $column)
  111. {
  112. // Auto increment is easy here!
  113. if (!empty($column['auto']))
  114. {
  115. $default = 'auto_increment';
  116. }
  117. elseif (isset($column['default']) && $column['default'] !== null)
  118. $default = 'default \'' . $smcFunc['db_escape_string']($column['default']) . '\'';
  119. else
  120. $default = '';
  121. // Sort out the size... and stuff...
  122. $column['size'] = isset($column['size']) && is_numeric($column['size']) ? $column['size'] : null;
  123. list ($type, $size) = $smcFunc['db_calculate_type']($column['type'], $column['size']);
  124. // Allow unsigned integers (mysql only)
  125. $unsigned = in_array($type, array('int', 'tinyint', 'smallint', 'mediumint', 'bigint')) && !empty($column['unsigned']) ? 'unsigned ' : '';
  126. if ($size !== null)
  127. $type = $type . '(' . $size . ')';
  128. // Now just put it together!
  129. $table_query .= "\n\t`" .$column['name'] . '` ' . $type . ' ' . (!empty($unsigned) ? $unsigned : '') . (!empty($column['null']) ? '' : 'NOT NULL') . ' ' . $default . ',';
  130. }
  131. // Loop through the indexes next...
  132. foreach ($indexes as $index)
  133. {
  134. $columns = implode(',', $index['columns']);
  135. // Is it the primary?
  136. if (isset($index['type']) && $index['type'] == 'primary')
  137. $table_query .= "\n\t" . 'PRIMARY KEY (' . implode(',', $index['columns']) . '),';
  138. else
  139. {
  140. if (empty($index['name']))
  141. $index['name'] = implode('_', $index['columns']);
  142. $table_query .= "\n\t" . (isset($index['type']) && $index['type'] == 'unique' ? 'UNIQUE' : 'KEY') . ' ' . $index['name'] . ' (' . $columns . '),';
  143. }
  144. }
  145. // No trailing commas!
  146. if (substr($table_query, -1) == ',')
  147. $table_query = substr($table_query, 0, -1);
  148. $table_query .= ') ENGINE=MyISAM';
  149. if (!empty($db_character_set) && $db_character_set == 'utf8')
  150. $table_query .= ' DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci';
  151. // Create the table!
  152. $smcFunc['db_query']('', $table_query,
  153. array(
  154. 'security_override' => true,
  155. )
  156. );
  157. }
  158. /**
  159. * Drop a table.
  160. * @param string $table_name
  161. * @param array $parameters, default array()
  162. * @param string $error, default 'fatal'
  163. */
  164. function smf_db_drop_table($table_name, $parameters = array(), $error = 'fatal')
  165. {
  166. global $reservedTables, $smcFunc, $db_prefix;
  167. // After stripping away the database name, this is what's left.
  168. $real_prefix = preg_match('~^(`?)(.+?)\\1\\.(.*?)$~', $db_prefix, $match) === 1 ? $match[3] : $db_prefix;
  169. // Get some aliases.
  170. $full_table_name = str_replace('{db_prefix}', $real_prefix, $table_name);
  171. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  172. // God no - dropping one of these = bad.
  173. if (in_array(strtolower($table_name), $reservedTables))
  174. return false;
  175. // Does it exist?
  176. if (in_array($full_table_name, $smcFunc['db_list_tables']()))
  177. {
  178. $query = 'DROP TABLE ' . $table_name;
  179. $smcFunc['db_query']('',
  180. $query,
  181. array(
  182. 'security_override' => true,
  183. )
  184. );
  185. return true;
  186. }
  187. // Otherwise do 'nout.
  188. return false;
  189. }
  190. /**
  191. * This function adds a column.
  192. * @param string $table_name, the name of the table
  193. * @param array $column_info, with column information
  194. * @param array $parameters, default array()
  195. * @param string $if_exists, default 'update'
  196. * @param string $error, default 'fatal'
  197. */
  198. function smf_db_add_column($table_name, $column_info, $parameters = array(), $if_exists = 'update', $error = 'fatal')
  199. {
  200. global $smcFunc, $db_package_log, $txt, $db_prefix;
  201. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  202. // Log that we will want to uninstall this!
  203. $db_package_log[] = array('remove_column', $table_name, $column_info['name']);
  204. // Does it exist - if so don't add it again!
  205. $columns = $smcFunc['db_list_columns']($table_name, false);
  206. foreach ($columns as $column)
  207. if ($column == $column_info['name'])
  208. {
  209. // If we're going to overwrite then use change column.
  210. if ($if_exists == 'update')
  211. return $smcFunc['db_change_column']($table_name, $column_info['name'], $column_info);
  212. else
  213. return false;
  214. }
  215. // Get the specifics...
  216. $column_info['size'] = isset($column_info['size']) && is_numeric($column_info['size']) ? $column_info['size'] : null;
  217. list ($type, $size) = $smcFunc['db_calculate_type']($column_info['type'], $column_info['size']);
  218. // Allow unsigned integers (mysql only)
  219. $unsigned = in_array($type, array('int', 'tinyint', 'smallint', 'mediumint', 'bigint')) && !empty($column_info['unsigned']) ? 'unsigned ' : '';
  220. if ($size !== null)
  221. $type = $type . '(' . $size . ')';
  222. // Now add the thing!
  223. $query = '
  224. ALTER TABLE ' . $table_name . '
  225. ADD `' . $column_info['name'] . '` ' . $type . ' ' . (!empty($unsigned) ? $unsigned : '') . (empty($column_info['null']) ? 'NOT NULL' : '') . ' ' .
  226. (!isset($column_info['default']) ? '' : 'default \'' . $smcFunc['db_escape_string']($column_info['default']) . '\'') . ' ' .
  227. (empty($column_info['auto']) ? '' : 'auto_increment primary key') . ' ';
  228. $smcFunc['db_query']('', $query,
  229. array(
  230. 'security_override' => true,
  231. )
  232. );
  233. return true;
  234. }
  235. /**
  236. * Removes a column.
  237. * @param string $table_name
  238. * @param string $column_name
  239. * @param array $parameters, default array()
  240. * @param string $error, default 'fatal'
  241. */
  242. function smf_db_remove_column($table_name, $column_name, $parameters = array(), $error = 'fatal')
  243. {
  244. global $smcFunc, $db_prefix;
  245. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  246. // Does it exist?
  247. $columns = $smcFunc['db_list_columns']($table_name, true);
  248. foreach ($columns as $column)
  249. if ($column['name'] == $column_name)
  250. {
  251. $smcFunc['db_query']('', '
  252. ALTER TABLE ' . $table_name . '
  253. DROP COLUMN ' . $column_name,
  254. array(
  255. 'security_override' => true,
  256. )
  257. );
  258. return true;
  259. }
  260. // If here we didn't have to work - joy!
  261. return false;
  262. }
  263. /**
  264. * Change a column.
  265. * @param string $table_name
  266. * @param $old_column
  267. * @param $column_info
  268. * @param array $parameters, default array()
  269. * @param string $error, default 'fatal'
  270. */
  271. function smf_db_change_column($table_name, $old_column, $column_info, $parameters = array(), $error = 'fatal')
  272. {
  273. global $smcFunc, $db_prefix;
  274. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  275. // Check it does exist!
  276. $columns = $smcFunc['db_list_columns']($table_name, true);
  277. $old_info = null;
  278. foreach ($columns as $column)
  279. if ($column['name'] == $old_column)
  280. $old_info = $column;
  281. // Nothing?
  282. if ($old_info === null)
  283. return false;
  284. // Get the right bits.
  285. if (!isset($column_info['name']))
  286. $column_info['name'] = $old_column;
  287. if (!isset($column_info['default']))
  288. $column_info['default'] = $old_info['default'];
  289. if (!isset($column_info['null']))
  290. $column_info['null'] = $old_info['null'];
  291. if (!isset($column_info['auto']))
  292. $column_info['auto'] = $old_info['auto'];
  293. if (!isset($column_info['type']))
  294. $column_info['type'] = $old_info['type'];
  295. if (!isset($column_info['size']) || !is_numeric($column_info['size']))
  296. $column_info['size'] = $old_info['size'];
  297. if (!isset($column_info['unsigned']) || !in_array($column_info['type'], array('int', 'tinyint', 'smallint', 'mediumint', 'bigint')))
  298. $column_info['unsigned'] = '';
  299. list ($type, $size) = $smcFunc['db_calculate_type']($column_info['type'], $column_info['size']);
  300. // Allow for unsigned integers (mysql only)
  301. $unsigned = in_array($type, array('int', 'tinyint', 'smallint', 'mediumint', 'bigint')) && !empty($column_info['unsigned']) ? 'unsigned ' : '';
  302. if ($size !== null)
  303. $type = $type . '(' . $size . ')';
  304. $smcFunc['db_query']('', '
  305. ALTER TABLE ' . $table_name . '
  306. CHANGE COLUMN `' . $old_column . '` `' . $column_info['name'] . '` ' . $type . ' ' . (!empty($unsigned) ? $unsigned : '') . (empty($column_info['null']) ? 'NOT NULL' : '') . ' ' .
  307. (!isset($column_info['default']) ? '' : 'default \'' . $smcFunc['db_escape_string']($column_info['default']) . '\'') . ' ' .
  308. (empty($column_info['auto']) ? '' : 'auto_increment') . ' ',
  309. array(
  310. 'security_override' => true,
  311. )
  312. );
  313. }
  314. /**
  315. * Add an index.
  316. * @param string $table_name
  317. * @param array $index_info
  318. * @param array $parameters, default array()
  319. * @param string $if_exists, default 'update'
  320. * @param string $error, default 'fatal'
  321. */
  322. function smf_db_add_index($table_name, $index_info, $parameters = array(), $if_exists = 'update', $error = 'fatal')
  323. {
  324. global $smcFunc, $db_package_log, $db_prefix;
  325. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  326. // No columns = no index.
  327. if (empty($index_info['columns']))
  328. return false;
  329. $columns = implode(',', $index_info['columns']);
  330. // No name - make it up!
  331. if (empty($index_info['name']))
  332. {
  333. // No need for primary.
  334. if (isset($index_info['type']) && $index_info['type'] == 'primary')
  335. $index_info['name'] = '';
  336. else
  337. $index_info['name'] = implode('_', $index_info['columns']);
  338. }
  339. else
  340. $index_info['name'] = $index_info['name'];
  341. // Log that we are going to want to remove this!
  342. $db_package_log[] = array('remove_index', $table_name, $index_info['name']);
  343. // Let's get all our indexes.
  344. $indexes = $smcFunc['db_list_indexes']($table_name, true);
  345. // Do we already have it?
  346. foreach ($indexes as $index)
  347. {
  348. if ($index['name'] == $index_info['name'] || ($index['type'] == 'primary' && isset($index_info['type']) && $index_info['type'] == 'primary'))
  349. {
  350. // If we want to overwrite simply remove the current one then continue.
  351. if ($if_exists != 'update' || $index['type'] == 'primary')
  352. return false;
  353. else
  354. $smcFunc['db_remove_index']($table_name, $index_info['name']);
  355. }
  356. }
  357. // If we're here we know we don't have the index - so just add it.
  358. if (!empty($index_info['type']) && $index_info['type'] == 'primary')
  359. {
  360. $smcFunc['db_query']('', '
  361. ALTER TABLE ' . $table_name . '
  362. ADD PRIMARY KEY (' . $columns . ')',
  363. array(
  364. 'security_override' => true,
  365. )
  366. );
  367. }
  368. else
  369. {
  370. $smcFunc['db_query']('', '
  371. ALTER TABLE ' . $table_name . '
  372. ADD ' . (isset($index_info['type']) && $index_info['type'] == 'unique' ? 'UNIQUE' : 'INDEX') . ' ' . $index_info['name'] . ' (' . $columns . ')',
  373. array(
  374. 'security_override' => true,
  375. )
  376. );
  377. }
  378. }
  379. /**
  380. * Remove an index.
  381. * @param string $table_name
  382. * @param string $index_name
  383. * @param array$parameters, default array()
  384. * @param string $error, default 'fatal'
  385. */
  386. function smf_db_remove_index($table_name, $index_name, $parameters = array(), $error = 'fatal')
  387. {
  388. global $smcFunc, $db_prefix;
  389. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  390. // Better exist!
  391. $indexes = $smcFunc['db_list_indexes']($table_name, true);
  392. foreach ($indexes as $index)
  393. {
  394. // If the name is primary we want the primary key!
  395. if ($index['type'] == 'primary' && $index_name == 'primary')
  396. {
  397. // Dropping primary key?
  398. $smcFunc['db_query']('', '
  399. ALTER TABLE ' . $table_name . '
  400. DROP PRIMARY KEY',
  401. array(
  402. 'security_override' => true,
  403. )
  404. );
  405. return true;
  406. }
  407. if ($index['name'] == $index_name)
  408. {
  409. // Drop the bugger...
  410. $smcFunc['db_query']('', '
  411. ALTER TABLE ' . $table_name . '
  412. DROP INDEX ' . $index_name,
  413. array(
  414. 'security_override' => true,
  415. )
  416. );
  417. return true;
  418. }
  419. }
  420. // Not to be found ;(
  421. return false;
  422. }
  423. /**
  424. * Get the schema formatted name for a type.
  425. * @param string $type_name
  426. * @param $type_size
  427. * @param $reverse
  428. */
  429. function smf_db_calculate_type($type_name, $type_size = null, $reverse = false)
  430. {
  431. // MySQL is actually the generic baseline.
  432. return array($type_name, $type_size);
  433. }
  434. /**
  435. * Get table structure.
  436. * @param string $table_name
  437. * @param array $parameters, default array()
  438. */
  439. function smf_db_table_structure($table_name, $parameters = array())
  440. {
  441. global $smcFunc, $db_prefix;
  442. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  443. return array(
  444. 'name' => $table_name,
  445. 'columns' => $smcFunc['db_list_columns']($table_name, true),
  446. 'indexes' => $smcFunc['db_list_indexes']($table_name, true),
  447. );
  448. }
  449. /**
  450. * Return column information for a table.
  451. * @param string $table_name
  452. * @param bool $detail
  453. * @param array $parameters, default array()
  454. * @return mixed
  455. */
  456. function smf_db_list_columns($table_name, $detail = false, $parameters = array())
  457. {
  458. global $smcFunc, $db_prefix;
  459. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  460. $result = $smcFunc['db_query']('', '
  461. SHOW FIELDS
  462. FROM {raw:table_name}',
  463. array(
  464. 'table_name' => strpos($table_name, '`') === 0 ? $table_name : '`' . $table_name . '`',
  465. )
  466. );
  467. $columns = array();
  468. while ($row = $smcFunc['db_fetch_assoc']($result))
  469. {
  470. if (!$detail)
  471. {
  472. $columns[] = $row['Field'];
  473. }
  474. else
  475. {
  476. // Is there an auto_increment?
  477. $auto = strpos($row['Extra'], 'auto_increment') !== false ? true : false;
  478. // Can we split out the size?
  479. if (preg_match('~(.+?)\s*\((\d+)\)(?:(?:\s*)?(unsigned))?~i', $row['Type'], $matches) === 1)
  480. {
  481. $type = $matches[1];
  482. $size = $matches[2];
  483. if (!empty($matches[3]) && $matches[3] == 'unsigned')
  484. $unsigned = true;
  485. }
  486. else
  487. {
  488. $type = $row['Type'];
  489. $size = null;
  490. }
  491. $columns[$row['Field']] = array(
  492. 'name' => $row['Field'],
  493. 'null' => $row['Null'] != 'YES' ? false : true,
  494. 'default' => isset($row['Default']) ? $row['Default'] : null,
  495. 'type' => $type,
  496. 'size' => $size,
  497. 'auto' => $auto,
  498. );
  499. if (isset($unsigned))
  500. {
  501. $columns[$row['Field']]['unsigned'] = $unsigned;
  502. unset($unsigned);
  503. }
  504. }
  505. }
  506. $smcFunc['db_free_result']($result);
  507. return $columns;
  508. }
  509. /**
  510. * Get index information.
  511. * @param string $table_name
  512. * @param bool $detail
  513. * @param array $parameters
  514. * @return mixed
  515. */
  516. function smf_db_list_indexes($table_name, $detail = false, $parameters = array())
  517. {
  518. global $smcFunc, $db_prefix;
  519. $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
  520. $result = $smcFunc['db_query']('', '
  521. SHOW KEYS
  522. FROM {raw:table_name}',
  523. array(
  524. 'table_name' => strpos($table_name, '`') === 0 ? $table_name : '`' . $table_name . '`',
  525. )
  526. );
  527. $indexes = array();
  528. while ($row = $smcFunc['db_fetch_assoc']($result))
  529. {
  530. if (!$detail)
  531. $indexes[] = $row['Key_name'];
  532. else
  533. {
  534. // What is the type?
  535. if ($row['Key_name'] == 'PRIMARY')
  536. $type = 'primary';
  537. elseif (empty($row['Non_unique']))
  538. $type = 'unique';
  539. elseif (isset($row['Index_type']) && $row['Index_type'] == 'FULLTEXT')
  540. $type = 'fulltext';
  541. else
  542. $type = 'index';
  543. // This is the first column we've seen?
  544. if (empty($indexes[$row['Key_name']]))
  545. {
  546. $indexes[$row['Key_name']] = array(
  547. 'name' => $row['Key_name'],
  548. 'type' => $type,
  549. 'columns' => array(),
  550. );
  551. }
  552. // Is it a partial index?
  553. if (!empty($row['Sub_part']))
  554. $indexes[$row['Key_name']]['columns'][] = $row['Column_name'] . '(' . $row['Sub_part'] . ')';
  555. else
  556. $indexes[$row['Key_name']]['columns'][] = $row['Column_name'];
  557. }
  558. }
  559. $smcFunc['db_free_result']($result);
  560. return $indexes;
  561. }
  562. ?>