PageRenderTime 60ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/upgrade.php

https://github.com/alx/barceloneta
PHP | 1304 lines | 940 code | 228 blank | 136 comment | 225 complexity | 56757072ebe1f300601692d911808bc7 MD5 | raw file
  1. <?php
  2. if ( file_exists(WP_CONTENT_DIR . '/install.php') )
  3. require (WP_CONTENT_DIR . '/install.php');
  4. require_once(ABSPATH . 'wp-admin/includes/admin.php');
  5. require_once(ABSPATH . 'wp-admin/includes/schema.php');
  6. if ( !function_exists('wp_install') ) :
  7. function wp_install($blog_title, $user_name, $user_email, $public, $deprecated='') {
  8. global $wp_rewrite;
  9. wp_check_mysql_version();
  10. wp_cache_flush();
  11. make_db_current_silent();
  12. populate_options();
  13. populate_roles();
  14. update_option('blogname', $blog_title);
  15. update_option('admin_email', $user_email);
  16. update_option('blog_public', $public);
  17. $guessurl = wp_guess_url();
  18. update_option('siteurl', $guessurl);
  19. // If not a public blog, don't ping.
  20. if ( ! $public )
  21. update_option('default_pingback_flag', 0);
  22. // Create default user. If the user already exists, the user tables are
  23. // being shared among blogs. Just set the role in that case.
  24. $user_id = username_exists($user_name);
  25. if ( !$user_id ) {
  26. $random_password = wp_generate_password();
  27. $user_id = wp_create_user($user_name, $random_password, $user_email);
  28. } else {
  29. $random_password = __('User already exists. Password inherited.');
  30. }
  31. $user = new WP_User($user_id);
  32. $user->set_role('administrator');
  33. wp_install_defaults($user_id);
  34. $wp_rewrite->flush_rules();
  35. wp_new_blog_notification($blog_title, $guessurl, $user_id, $random_password);
  36. wp_cache_flush();
  37. return array('url' => $guessurl, 'user_id' => $user_id, 'password' => $random_password);
  38. }
  39. endif;
  40. if ( !function_exists('wp_install_defaults') ) :
  41. function wp_install_defaults($user_id) {
  42. global $wpdb;
  43. // Default category
  44. $cat_name = $wpdb->escape(__('Uncategorized'));
  45. $cat_slug = sanitize_title(_c('Uncategorized|Default category slug'));
  46. $wpdb->query("INSERT INTO $wpdb->terms (name, slug, term_group) VALUES ('$cat_name', '$cat_slug', '0')");
  47. $wpdb->query("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ('1', 'category', '', '0', '1')");
  48. // Default link category
  49. $cat_name = $wpdb->escape(__('Blogroll'));
  50. $cat_slug = sanitize_title(_c('Blogroll|Default link category slug'));
  51. $wpdb->query("INSERT INTO $wpdb->terms (name, slug, term_group) VALUES ('$cat_name', '$cat_slug', '0')");
  52. $wpdb->query("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ('2', 'link_category', '', '0', '7')");
  53. // Now drop in some default links
  54. $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://codex.wordpress.org/', 'Documentation', 0, '', '');");
  55. $wpdb->query( "INSERT INTO $wpdb->term_relationships (`object_id`, `term_taxonomy_id`) VALUES (1, 2)" );
  56. $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://wordpress.org/development/', 'Development Blog', 0, 'http://wordpress.org/development/feed/', '');");
  57. $wpdb->query( "INSERT INTO $wpdb->term_relationships (`object_id`, `term_taxonomy_id`) VALUES (2, 2)" );
  58. $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://wordpress.org/extend/ideas/', 'Suggest Ideas', 0, '', '');");
  59. $wpdb->query( "INSERT INTO $wpdb->term_relationships (`object_id`, `term_taxonomy_id`) VALUES (3, 2)" );
  60. $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://wordpress.org/support/', 'Support Forum', 0, '', '');");
  61. $wpdb->query( "INSERT INTO $wpdb->term_relationships (`object_id`, `term_taxonomy_id`) VALUES (4, 2)" );
  62. $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://wordpress.org/extend/plugins/', 'Plugins', 0, '', '');");
  63. $wpdb->query( "INSERT INTO $wpdb->term_relationships (`object_id`, `term_taxonomy_id`) VALUES (5, 2)" );
  64. $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://wordpress.org/extend/themes/', 'Themes', 0, '', '');");
  65. $wpdb->query( "INSERT INTO $wpdb->term_relationships (`object_id`, `term_taxonomy_id`) VALUES (6, 2)" );
  66. $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://planet.wordpress.org/', 'WordPress Planet', 0, '', '');");
  67. $wpdb->query( "INSERT INTO $wpdb->term_relationships (`object_id`, `term_taxonomy_id`) VALUES (7, 2)" );
  68. // First post
  69. $now = date('Y-m-d H:i:s');
  70. $now_gmt = gmdate('Y-m-d H:i:s');
  71. $first_post_guid = get_option('home') . '/?p=1';
  72. $wpdb->query("INSERT INTO $wpdb->posts (post_author, post_date, post_date_gmt, post_content, post_excerpt, post_title, post_category, post_name, post_modified, post_modified_gmt, guid, comment_count, to_ping, pinged, post_content_filtered) VALUES ($user_id, '$now', '$now_gmt', '".$wpdb->escape(__('Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!'))."', '', '".$wpdb->escape(__('Hello world!'))."', '0', '".$wpdb->escape(_c('hello-world|Default post slug'))."', '$now', '$now_gmt', '$first_post_guid', '1', '', '', '')");
  73. $wpdb->query( "INSERT INTO $wpdb->term_relationships (`object_id`, `term_taxonomy_id`) VALUES (1, 1)" );
  74. // Default comment
  75. $wpdb->query("INSERT INTO $wpdb->comments (comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_date, comment_date_gmt, comment_content) VALUES ('1', '".$wpdb->escape(__('Mr WordPress'))."', '', 'http://wordpress.org/', '$now', '$now_gmt', '".$wpdb->escape(__('Hi, this is a comment.<br />To delete a comment, just log in and view the post&#039;s comments. There you will have the option to edit or delete them.'))."')");
  76. // First Page
  77. $first_post_guid = get_option('home') . '/?page_id=2';
  78. $wpdb->query("INSERT INTO $wpdb->posts (post_author, post_date, post_date_gmt, post_content, post_excerpt, post_title, post_category, post_name, post_modified, post_modified_gmt, guid, post_status, post_type, to_ping, pinged, post_content_filtered) VALUES ($user_id, '$now', '$now_gmt', '".$wpdb->escape(__('This is an example of a WordPress page, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many pages like this one or sub-pages as you like and manage all of your content inside of WordPress.'))."', '', '".$wpdb->escape(__('About'))."', '0', '".$wpdb->escape(_c('about|Default page slug'))."', '$now', '$now_gmt','$first_post_guid', 'publish', 'page', '', '', '')");
  79. }
  80. endif;
  81. if ( !function_exists('wp_new_blog_notification') ) :
  82. function wp_new_blog_notification($blog_title, $blog_url, $user_id, $password) {
  83. $user = new WP_User($user_id);
  84. $email = $user->user_email;
  85. $name = $user->user_login;
  86. $message_headers = 'From: "' . $blog_title . '" <wordpress@' . $_SERVER['SERVER_NAME'] . '>';
  87. $message = sprintf(__("Your new WordPress blog has been successfully set up at:
  88. %1\$s
  89. You can log in to the administrator account with the following information:
  90. Username: %2\$s
  91. Password: %3\$s
  92. We hope you enjoy your new blog. Thanks!
  93. --The WordPress Team
  94. http://wordpress.org/
  95. "), $blog_url, $name, $password);
  96. @wp_mail($email, __('New WordPress Blog'), $message, $message_headers);
  97. }
  98. endif;
  99. if ( !function_exists('wp_upgrade') ) :
  100. function wp_upgrade() {
  101. global $wp_current_db_version, $wp_db_version;
  102. $wp_current_db_version = __get_option('db_version');
  103. // We are up-to-date. Nothing to do.
  104. if ( $wp_db_version == $wp_current_db_version )
  105. return;
  106. wp_check_mysql_version();
  107. wp_cache_flush();
  108. make_db_current_silent();
  109. upgrade_all();
  110. wp_cache_flush();
  111. }
  112. endif;
  113. // Functions to be called in install and upgrade scripts
  114. function upgrade_all() {
  115. global $wp_current_db_version, $wp_db_version, $wp_rewrite;
  116. $wp_current_db_version = __get_option('db_version');
  117. // We are up-to-date. Nothing to do.
  118. if ( $wp_db_version == $wp_current_db_version )
  119. return;
  120. // If the version is not set in the DB, try to guess the version.
  121. if ( empty($wp_current_db_version) ) {
  122. $wp_current_db_version = 0;
  123. // If the template option exists, we have 1.5.
  124. $template = __get_option('template');
  125. if ( !empty($template) )
  126. $wp_current_db_version = 2541;
  127. }
  128. if ( $wp_current_db_version < 6039 )
  129. upgrade_230_options_table();
  130. populate_options();
  131. if ( $wp_current_db_version < 2541 ) {
  132. upgrade_100();
  133. upgrade_101();
  134. upgrade_110();
  135. upgrade_130();
  136. }
  137. if ( $wp_current_db_version < 3308 )
  138. upgrade_160();
  139. if ( $wp_current_db_version < 4772 )
  140. upgrade_210();
  141. if ( $wp_current_db_version < 4351 )
  142. upgrade_old_slugs();
  143. if ( $wp_current_db_version < 5539 )
  144. upgrade_230();
  145. if ( $wp_current_db_version < 6124 )
  146. upgrade_230_old_tables();
  147. if ( $wp_current_db_version < 7499 )
  148. upgrade_250();
  149. if ( $wp_current_db_version < 7796 )
  150. upgrade_251();
  151. if ( $wp_current_db_version < 7935 )
  152. upgrade_252();
  153. if ( $wp_current_db_version < 8201 )
  154. upgrade_260();
  155. maybe_disable_automattic_widgets();
  156. $wp_rewrite->flush_rules();
  157. update_option('db_version', $wp_db_version);
  158. }
  159. function upgrade_100() {
  160. global $wpdb;
  161. // Get the title and ID of every post, post_name to check if it already has a value
  162. $posts = $wpdb->get_results("SELECT ID, post_title, post_name FROM $wpdb->posts WHERE post_name = ''");
  163. if ($posts) {
  164. foreach($posts as $post) {
  165. if ('' == $post->post_name) {
  166. $newtitle = sanitize_title($post->post_title);
  167. $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_name = %s WHERE ID = %d", $newtitle, $post->ID) );
  168. }
  169. }
  170. }
  171. $categories = $wpdb->get_results("SELECT cat_ID, cat_name, category_nicename FROM $wpdb->categories");
  172. foreach ($categories as $category) {
  173. if ('' == $category->category_nicename) {
  174. $newtitle = sanitize_title($category->cat_name);
  175. $wpdb->query( $wpdb->prepare("UPDATE $wpdb->categories SET category_nicename = %s WHERE cat_ID = %d", $newtitle, $category->cat_ID) );
  176. }
  177. }
  178. $wpdb->query("UPDATE $wpdb->options SET option_value = REPLACE(option_value, 'wp-links/links-images/', 'wp-images/links/')
  179. WHERE option_name LIKE 'links_rating_image%'
  180. AND option_value LIKE 'wp-links/links-images/%'");
  181. $done_ids = $wpdb->get_results("SELECT DISTINCT post_id FROM $wpdb->post2cat");
  182. if ($done_ids) :
  183. foreach ($done_ids as $done_id) :
  184. $done_posts[] = $done_id->post_id;
  185. endforeach;
  186. $catwhere = ' AND ID NOT IN (' . implode(',', $done_posts) . ')';
  187. else:
  188. $catwhere = '';
  189. endif;
  190. $allposts = $wpdb->get_results("SELECT ID, post_category FROM $wpdb->posts WHERE post_category != '0' $catwhere");
  191. if ($allposts) :
  192. foreach ($allposts as $post) {
  193. // Check to see if it's already been imported
  194. $cat = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->post2cat WHERE post_id = %d AND category_id = %d", $post->ID, $post->post_category) );
  195. if (!$cat && 0 != $post->post_category) { // If there's no result
  196. $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->post2cat
  197. (post_id, category_id)
  198. VALUES (%s, %s)
  199. ", $post->ID, $post->post_category) );
  200. }
  201. }
  202. endif;
  203. }
  204. function upgrade_101() {
  205. global $wpdb;
  206. // Clean up indices, add a few
  207. add_clean_index($wpdb->posts, 'post_name');
  208. add_clean_index($wpdb->posts, 'post_status');
  209. add_clean_index($wpdb->categories, 'category_nicename');
  210. add_clean_index($wpdb->comments, 'comment_approved');
  211. add_clean_index($wpdb->comments, 'comment_post_ID');
  212. add_clean_index($wpdb->links , 'link_category');
  213. add_clean_index($wpdb->links , 'link_visible');
  214. }
  215. function upgrade_110() {
  216. global $wpdb;
  217. // Set user_nicename.
  218. $users = $wpdb->get_results("SELECT ID, user_nickname, user_nicename FROM $wpdb->users");
  219. foreach ($users as $user) {
  220. if ('' == $user->user_nicename) {
  221. $newname = sanitize_title($user->user_nickname);
  222. $wpdb->query( $wpdb->prepare("UPDATE $wpdb->users SET user_nicename = %s WHERE ID = %d", $newname, $user->ID) );
  223. }
  224. }
  225. $users = $wpdb->get_results("SELECT ID, user_pass from $wpdb->users");
  226. foreach ($users as $row) {
  227. if (!preg_match('/^[A-Fa-f0-9]{32}$/', $row->user_pass)) {
  228. $wpdb->query('UPDATE '.$wpdb->users.' SET user_pass = MD5(\''.$row->user_pass.'\') WHERE ID = \''.$row->ID.'\'');
  229. }
  230. }
  231. // Get the GMT offset, we'll use that later on
  232. $all_options = get_alloptions_110();
  233. $time_difference = $all_options->time_difference;
  234. $server_time = time()+date('Z');
  235. $weblogger_time = $server_time + $time_difference*3600;
  236. $gmt_time = time();
  237. $diff_gmt_server = ($gmt_time - $server_time) / 3600;
  238. $diff_weblogger_server = ($weblogger_time - $server_time) / 3600;
  239. $diff_gmt_weblogger = $diff_gmt_server - $diff_weblogger_server;
  240. $gmt_offset = -$diff_gmt_weblogger;
  241. // Add a gmt_offset option, with value $gmt_offset
  242. add_option('gmt_offset', $gmt_offset);
  243. // Check if we already set the GMT fields (if we did, then
  244. // MAX(post_date_gmt) can't be '0000-00-00 00:00:00'
  245. // <michel_v> I just slapped myself silly for not thinking about it earlier
  246. $got_gmt_fields = ($wpdb->get_var("SELECT MAX(post_date_gmt) FROM $wpdb->posts") == '0000-00-00 00:00:00') ? false : true;
  247. if (!$got_gmt_fields) {
  248. // Add or substract time to all dates, to get GMT dates
  249. $add_hours = intval($diff_gmt_weblogger);
  250. $add_minutes = intval(60 * ($diff_gmt_weblogger - $add_hours));
  251. $wpdb->query("UPDATE $wpdb->posts SET post_date_gmt = DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)");
  252. $wpdb->query("UPDATE $wpdb->posts SET post_modified = post_date");
  253. $wpdb->query("UPDATE $wpdb->posts SET post_modified_gmt = DATE_ADD(post_modified, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE) WHERE post_modified != '0000-00-00 00:00:00'");
  254. $wpdb->query("UPDATE $wpdb->comments SET comment_date_gmt = DATE_ADD(comment_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)");
  255. $wpdb->query("UPDATE $wpdb->users SET user_registered = DATE_ADD(user_registered, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)");
  256. }
  257. }
  258. function upgrade_130() {
  259. global $wpdb;
  260. // Remove extraneous backslashes.
  261. $posts = $wpdb->get_results("SELECT ID, post_title, post_content, post_excerpt, guid, post_date, post_name, post_status, post_author FROM $wpdb->posts");
  262. if ($posts) {
  263. foreach($posts as $post) {
  264. $post_content = addslashes(deslash($post->post_content));
  265. $post_title = addslashes(deslash($post->post_title));
  266. $post_excerpt = addslashes(deslash($post->post_excerpt));
  267. if ( empty($post->guid) )
  268. $guid = get_permalink($post->ID);
  269. else
  270. $guid = $post->guid;
  271. $wpdb->query("UPDATE $wpdb->posts SET post_title = '$post_title', post_content = '$post_content', post_excerpt = '$post_excerpt', guid = '$guid' WHERE ID = '$post->ID'");
  272. }
  273. }
  274. // Remove extraneous backslashes.
  275. $comments = $wpdb->get_results("SELECT comment_ID, comment_author, comment_content FROM $wpdb->comments");
  276. if ($comments) {
  277. foreach($comments as $comment) {
  278. $comment_content = addslashes(deslash($comment->comment_content));
  279. $comment_author = addslashes(deslash($comment->comment_author));
  280. $wpdb->query("UPDATE $wpdb->comments SET comment_content = '$comment_content', comment_author = '$comment_author' WHERE comment_ID = '$comment->comment_ID'");
  281. }
  282. }
  283. // Remove extraneous backslashes.
  284. $links = $wpdb->get_results("SELECT link_id, link_name, link_description FROM $wpdb->links");
  285. if ($links) {
  286. foreach($links as $link) {
  287. $link_name = addslashes(deslash($link->link_name));
  288. $link_description = addslashes(deslash($link->link_description));
  289. $wpdb->query("UPDATE $wpdb->links SET link_name = '$link_name', link_description = '$link_description' WHERE link_id = '$link->link_id'");
  290. }
  291. }
  292. // The "paged" option for what_to_show is no more.
  293. if ($wpdb->get_var("SELECT option_value FROM $wpdb->options WHERE option_name = 'what_to_show'") == 'paged') {
  294. $wpdb->query("UPDATE $wpdb->options SET option_value = 'posts' WHERE option_name = 'what_to_show'");
  295. }
  296. $active_plugins = __get_option('active_plugins');
  297. // If plugins are not stored in an array, they're stored in the old
  298. // newline separated format. Convert to new format.
  299. if ( !is_array( $active_plugins ) ) {
  300. $active_plugins = explode("\n", trim($active_plugins));
  301. update_option('active_plugins', $active_plugins);
  302. }
  303. // Obsolete tables
  304. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optionvalues');
  305. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiontypes');
  306. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiongroups');
  307. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiongroup_options');
  308. // Update comments table to use comment_type
  309. $wpdb->query("UPDATE $wpdb->comments SET comment_type='trackback', comment_content = REPLACE(comment_content, '<trackback />', '') WHERE comment_content LIKE '<trackback />%'");
  310. $wpdb->query("UPDATE $wpdb->comments SET comment_type='pingback', comment_content = REPLACE(comment_content, '<pingback />', '') WHERE comment_content LIKE '<pingback />%'");
  311. // Some versions have multiple duplicate option_name rows with the same values
  312. $options = $wpdb->get_results("SELECT option_name, COUNT(option_name) AS dupes FROM `$wpdb->options` GROUP BY option_name");
  313. foreach ( $options as $option ) {
  314. if ( 1 != $option->dupes ) { // Could this be done in the query?
  315. $limit = $option->dupes - 1;
  316. $dupe_ids = $wpdb->get_col( $wpdb->prepare("SELECT option_id FROM $wpdb->options WHERE option_name = %s LIMIT %d", $option->option_name, $limit) );
  317. $dupe_ids = join($dupe_ids, ',');
  318. $wpdb->query("DELETE FROM $wpdb->options WHERE option_id IN ($dupe_ids)");
  319. }
  320. }
  321. make_site_theme();
  322. }
  323. function upgrade_160() {
  324. global $wpdb, $wp_current_db_version;
  325. populate_roles_160();
  326. $users = $wpdb->get_results("SELECT * FROM $wpdb->users");
  327. foreach ( $users as $user ) :
  328. if ( !empty( $user->user_firstname ) )
  329. update_usermeta( $user->ID, 'first_name', $wpdb->escape($user->user_firstname) );
  330. if ( !empty( $user->user_lastname ) )
  331. update_usermeta( $user->ID, 'last_name', $wpdb->escape($user->user_lastname) );
  332. if ( !empty( $user->user_nickname ) )
  333. update_usermeta( $user->ID, 'nickname', $wpdb->escape($user->user_nickname) );
  334. if ( !empty( $user->user_level ) )
  335. update_usermeta( $user->ID, $wpdb->prefix . 'user_level', $user->user_level );
  336. if ( !empty( $user->user_icq ) )
  337. update_usermeta( $user->ID, 'icq', $wpdb->escape($user->user_icq) );
  338. if ( !empty( $user->user_aim ) )
  339. update_usermeta( $user->ID, 'aim', $wpdb->escape($user->user_aim) );
  340. if ( !empty( $user->user_msn ) )
  341. update_usermeta( $user->ID, 'msn', $wpdb->escape($user->user_msn) );
  342. if ( !empty( $user->user_yim ) )
  343. update_usermeta( $user->ID, 'yim', $wpdb->escape($user->user_icq) );
  344. if ( !empty( $user->user_description ) )
  345. update_usermeta( $user->ID, 'description', $wpdb->escape($user->user_description) );
  346. if ( isset( $user->user_idmode ) ):
  347. $idmode = $user->user_idmode;
  348. if ($idmode == 'nickname') $id = $user->user_nickname;
  349. if ($idmode == 'login') $id = $user->user_login;
  350. if ($idmode == 'firstname') $id = $user->user_firstname;
  351. if ($idmode == 'lastname') $id = $user->user_lastname;
  352. if ($idmode == 'namefl') $id = $user->user_firstname.' '.$user->user_lastname;
  353. if ($idmode == 'namelf') $id = $user->user_lastname.' '.$user->user_firstname;
  354. if (!$idmode) $id = $user->user_nickname;
  355. $wpdb->query( $wpdb->prepare("UPDATE $wpdb->users SET display_name = %s WHERE ID = %d", $id, $user->ID) );
  356. endif;
  357. // FIXME: RESET_CAPS is temporary code to reset roles and caps if flag is set.
  358. $caps = get_usermeta( $user->ID, $wpdb->prefix . 'capabilities');
  359. if ( empty($caps) || defined('RESET_CAPS') ) {
  360. $level = get_usermeta($user->ID, $wpdb->prefix . 'user_level');
  361. $role = translate_level_to_role($level);
  362. update_usermeta( $user->ID, $wpdb->prefix . 'capabilities', array($role => true) );
  363. }
  364. endforeach;
  365. $old_user_fields = array( 'user_firstname', 'user_lastname', 'user_icq', 'user_aim', 'user_msn', 'user_yim', 'user_idmode', 'user_ip', 'user_domain', 'user_browser', 'user_description', 'user_nickname', 'user_level' );
  366. $wpdb->hide_errors();
  367. foreach ( $old_user_fields as $old )
  368. $wpdb->query("ALTER TABLE $wpdb->users DROP $old");
  369. $wpdb->show_errors();
  370. // populate comment_count field of posts table
  371. $comments = $wpdb->get_results( "SELECT comment_post_ID, COUNT(*) as c FROM $wpdb->comments WHERE comment_approved = '1' GROUP BY comment_post_ID" );
  372. if( is_array( $comments ) ) {
  373. foreach ($comments as $comment) {
  374. $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET comment_count = %d WHERE ID = %d", $comment->c, $comment->comment_post_ID) );
  375. }
  376. }
  377. // Some alpha versions used a post status of object instead of attachment and put
  378. // the mime type in post_type instead of post_mime_type.
  379. if ( $wp_current_db_version > 2541 && $wp_current_db_version <= 3091 ) {
  380. $objects = $wpdb->get_results("SELECT ID, post_type FROM $wpdb->posts WHERE post_status = 'object'");
  381. foreach ($objects as $object) {
  382. $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_status = 'attachment',
  383. post_mime_type = %s,
  384. post_type = ''
  385. WHERE ID = %d", $object->post_type, $object->ID) );
  386. $meta = get_post_meta($object->ID, 'imagedata', true);
  387. if ( ! empty($meta['file']) )
  388. update_attached_file( $object->ID, $meta['file'] );
  389. }
  390. }
  391. }
  392. function upgrade_210() {
  393. global $wpdb, $wp_current_db_version;
  394. if ( $wp_current_db_version < 3506 ) {
  395. // Update status and type.
  396. $posts = $wpdb->get_results("SELECT ID, post_status FROM $wpdb->posts");
  397. if ( ! empty($posts) ) foreach ($posts as $post) {
  398. $status = $post->post_status;
  399. $type = 'post';
  400. if ( 'static' == $status ) {
  401. $status = 'publish';
  402. $type = 'page';
  403. } else if ( 'attachment' == $status ) {
  404. $status = 'inherit';
  405. $type = 'attachment';
  406. }
  407. $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_status = %s, post_type = %s WHERE ID = %d", $status, $type, $post->ID) );
  408. }
  409. }
  410. if ( $wp_current_db_version < 3845 ) {
  411. populate_roles_210();
  412. }
  413. if ( $wp_current_db_version < 3531 ) {
  414. // Give future posts a post_status of future.
  415. $now = gmdate('Y-m-d H:i:59');
  416. $wpdb->query ("UPDATE $wpdb->posts SET post_status = 'future' WHERE post_status = 'publish' AND post_date_gmt > '$now'");
  417. $posts = $wpdb->get_results("SELECT ID, post_date FROM $wpdb->posts WHERE post_status ='future'");
  418. if ( !empty($posts) )
  419. foreach ( $posts as $post )
  420. wp_schedule_single_event(mysql2date('U', $post->post_date), 'publish_future_post', array($post->ID));
  421. }
  422. }
  423. function upgrade_230() {
  424. global $wp_current_db_version, $wpdb;
  425. if ( $wp_current_db_version < 5200 ) {
  426. populate_roles_230();
  427. }
  428. // Convert categories to terms.
  429. $tt_ids = array();
  430. $have_tags = false;
  431. $categories = $wpdb->get_results("SELECT * FROM $wpdb->categories ORDER BY cat_ID");
  432. foreach ($categories as $category) {
  433. $term_id = (int) $category->cat_ID;
  434. $name = $category->cat_name;
  435. $description = $category->category_description;
  436. $slug = $category->category_nicename;
  437. $parent = $category->category_parent;
  438. $term_group = 0;
  439. // Associate terms with the same slug in a term group and make slugs unique.
  440. if ( $exists = $wpdb->get_results( $wpdb->prepare("SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $slug) ) ) {
  441. $term_group = $exists[0]->term_group;
  442. $id = $exists[0]->term_id;
  443. $num = 2;
  444. do {
  445. $alt_slug = $slug . "-$num";
  446. $num++;
  447. $slug_check = $wpdb->get_var( $wpdb->prepare("SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug) );
  448. } while ( $slug_check );
  449. $slug = $alt_slug;
  450. if ( empty( $term_group ) ) {
  451. $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms GROUP BY term_group") + 1;
  452. $wpdb->query( $wpdb->prepare("UPDATE $wpdb->terms SET term_group = %d WHERE term_id = %d", $term_group, $id) );
  453. }
  454. }
  455. $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->terms (term_id, name, slug, term_group) VALUES
  456. (%d, %s, %s, %d)", $term_id, $name, $slug, $term_group) );
  457. $count = 0;
  458. if ( !empty($category->category_count) ) {
  459. $count = (int) $category->category_count;
  460. $taxonomy = 'category';
  461. $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ( %d, %s, %s, %d, %d)", $term_id, $taxonomy, $description, $parent, $count) );
  462. $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id;
  463. }
  464. if ( !empty($category->link_count) ) {
  465. $count = (int) $category->link_count;
  466. $taxonomy = 'link_category';
  467. $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ( %d, %s, %s, %d, %d)", $term_id, $taxonomy, $description, $parent, $count) );
  468. $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id;
  469. }
  470. if ( !empty($category->tag_count) ) {
  471. $have_tags = true;
  472. $count = (int) $category->tag_count;
  473. $taxonomy = 'post_tag';
  474. $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ( %d, %s, %s, %d, %d)", $term_id, $taxonomy, $description, $parent, $count) );
  475. $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id;
  476. }
  477. if ( empty($count) ) {
  478. $count = 0;
  479. $taxonomy = 'category';
  480. $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ( %d, %s, %s, %d, %d)", $term_id, $taxonomy, $description, $parent, $count) );
  481. $tt_ids[$term_id][$taxonomy] = (int) $wpdb->insert_id;
  482. }
  483. }
  484. $select = 'post_id, category_id';
  485. if ( $have_tags )
  486. $select .= ', rel_type';
  487. $posts = $wpdb->get_results("SELECT $select FROM $wpdb->post2cat GROUP BY post_id, category_id");
  488. foreach ( $posts as $post ) {
  489. $post_id = (int) $post->post_id;
  490. $term_id = (int) $post->category_id;
  491. $taxonomy = 'category';
  492. if ( !empty($post->rel_type) && 'tag' == $post->rel_type)
  493. $taxonomy = 'tag';
  494. $tt_id = $tt_ids[$term_id][$taxonomy];
  495. if ( empty($tt_id) )
  496. continue;
  497. $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ( %d, %d)", $post_id, $tt_id) );
  498. }
  499. // < 3570 we used linkcategories. >= 3570 we used categories and link2cat.
  500. if ( $wp_current_db_version < 3570 ) {
  501. // Create link_category terms for link categories. Create a map of link cat IDs
  502. // to link_category terms.
  503. $link_cat_id_map = array();
  504. $default_link_cat = 0;
  505. $tt_ids = array();
  506. $link_cats = $wpdb->get_results("SELECT cat_id, cat_name FROM " . $wpdb->prefix . 'linkcategories');
  507. foreach ( $link_cats as $category) {
  508. $cat_id = (int) $category->cat_id;
  509. $term_id = 0;
  510. $name = $wpdb->escape($category->cat_name);
  511. $slug = sanitize_title($name);
  512. $term_group = 0;
  513. // Associate terms with the same slug in a term group and make slugs unique.
  514. if ( $exists = $wpdb->get_results( $wpdb->prepare("SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $slug) ) ) {
  515. $term_group = $exists[0]->term_group;
  516. $term_id = $exists[0]->term_id;
  517. }
  518. if ( empty($term_id) ) {
  519. $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->terms (name, slug, term_group) VALUES (%s, %s, %d)", $name, $slug, $term_group) );
  520. $term_id = (int) $wpdb->insert_id;
  521. }
  522. $link_cat_id_map[$cat_id] = $term_id;
  523. $default_link_cat = $term_id;
  524. $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES (%d, 'link_category', '', '0', '0')", $term_id) );
  525. $tt_ids[$term_id] = (int) $wpdb->insert_id;
  526. }
  527. // Associate links to cats.
  528. $links = $wpdb->get_results("SELECT link_id, link_category FROM $wpdb->links");
  529. if ( !empty($links) ) foreach ( $links as $link ) {
  530. if ( 0 == $link->link_category )
  531. continue;
  532. if ( ! isset($link_cat_id_map[$link->link_category]) )
  533. continue;
  534. $term_id = $link_cat_id_map[$link->link_category];
  535. $tt_id = $tt_ids[$term_id];
  536. if ( empty($tt_id) )
  537. continue;
  538. $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ( %d, %d)", $link->link_id, $tt_id) );
  539. }
  540. // Set default to the last category we grabbed during the upgrade loop.
  541. update_option('default_link_category', $default_link_cat);
  542. } else {
  543. $links = $wpdb->get_results("SELECT link_id, category_id FROM $wpdb->link2cat GROUP BY link_id, category_id");
  544. foreach ( $links as $link ) {
  545. $link_id = (int) $link->link_id;
  546. $term_id = (int) $link->category_id;
  547. $taxonomy = 'link_category';
  548. $tt_id = $tt_ids[$term_id][$taxonomy];
  549. if ( empty($tt_id) )
  550. continue;
  551. $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ( %d, %d)", $link_id, $tt_id) );
  552. }
  553. }
  554. if ( $wp_current_db_version < 4772 ) {
  555. // Obsolete linkcategories table
  556. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'linkcategories');
  557. }
  558. // Recalculate all counts
  559. $terms = $wpdb->get_results("SELECT term_taxonomy_id, taxonomy FROM $wpdb->term_taxonomy");
  560. foreach ( (array) $terms as $term ) {
  561. if ( ('post_tag' == $term->taxonomy) || ('category' == $term->taxonomy) )
  562. $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status = 'publish' AND post_type = 'post' AND term_taxonomy_id = %d", $term->term_taxonomy_id) );
  563. else
  564. $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term->term_taxonomy_id) );
  565. $wpdb->query( $wpdb->prepare("UPDATE $wpdb->term_taxonomy SET count = %d WHERE term_taxonomy_id = %d", $count, $term->term_taxonomy_id) );
  566. }
  567. }
  568. function upgrade_230_options_table() {
  569. global $wpdb;
  570. $old_options_fields = array( 'option_can_override', 'option_type', 'option_width', 'option_height', 'option_description', 'option_admin_level' );
  571. $wpdb->hide_errors();
  572. foreach ( $old_options_fields as $old )
  573. $wpdb->query("ALTER TABLE $wpdb->options DROP $old");
  574. $wpdb->show_errors();
  575. }
  576. function upgrade_230_old_tables() {
  577. global $wpdb;
  578. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'categories');
  579. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'link2cat');
  580. $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'post2cat');
  581. }
  582. function upgrade_old_slugs() {
  583. // upgrade people who were using the Redirect Old Slugs plugin
  584. global $wpdb;
  585. $wpdb->query("UPDATE $wpdb->postmeta SET meta_key = '_wp_old_slug' WHERE meta_key = 'old_slug'");
  586. }
  587. function upgrade_250() {
  588. global $wp_current_db_version;
  589. if ( $wp_current_db_version < 6689 ) {
  590. populate_roles_250();
  591. }
  592. }
  593. function upgrade_251() {
  594. global $wp_current_db_version;
  595. // Make the secret longer
  596. update_option('secret', wp_generate_password(64));
  597. }
  598. function upgrade_252() {
  599. global $wpdb;
  600. $wpdb->query("UPDATE $wpdb->users SET user_activation_key = ''");
  601. }
  602. function upgrade_260() {
  603. if ( $wp_current_db_version < 8000 )
  604. populate_roles_260();
  605. if ( $wp_current_db_version < 8201 ) {
  606. update_option('enable_app', 1);
  607. update_option('enable_xmlrpc', 1);
  608. }
  609. }
  610. // The functions we use to actually do stuff
  611. // General
  612. function maybe_create_table($table_name, $create_ddl) {
  613. global $wpdb;
  614. foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
  615. if ($table == $table_name) {
  616. return true;
  617. }
  618. }
  619. //didn't find it try to create it.
  620. $q = $wpdb->query($create_ddl);
  621. // we cannot directly tell that whether this succeeded!
  622. foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
  623. if ($table == $table_name) {
  624. return true;
  625. }
  626. }
  627. return false;
  628. }
  629. function drop_index($table, $index) {
  630. global $wpdb;
  631. $wpdb->hide_errors();
  632. $wpdb->query("ALTER TABLE `$table` DROP INDEX `$index`");
  633. // Now we need to take out all the extra ones we may have created
  634. for ($i = 0; $i < 25; $i++) {
  635. $wpdb->query("ALTER TABLE `$table` DROP INDEX `{$index}_$i`");
  636. }
  637. $wpdb->show_errors();
  638. return true;
  639. }
  640. function add_clean_index($table, $index) {
  641. global $wpdb;
  642. drop_index($table, $index);
  643. $wpdb->query("ALTER TABLE `$table` ADD INDEX ( `$index` )");
  644. return true;
  645. }
  646. /**
  647. ** maybe_add_column()
  648. ** Add column to db table if it doesn't exist.
  649. ** Returns: true if already exists or on successful completion
  650. ** false on error
  651. */
  652. function maybe_add_column($table_name, $column_name, $create_ddl) {
  653. global $wpdb, $debug;
  654. foreach ($wpdb->get_col("DESC $table_name", 0) as $column ) {
  655. if ($debug) echo("checking $column == $column_name<br />");
  656. if ($column == $column_name) {
  657. return true;
  658. }
  659. }
  660. //didn't find it try to create it.
  661. $q = $wpdb->query($create_ddl);
  662. // we cannot directly tell that whether this succeeded!
  663. foreach ($wpdb->get_col("DESC $table_name", 0) as $column ) {
  664. if ($column == $column_name) {
  665. return true;
  666. }
  667. }
  668. return false;
  669. }
  670. // get_alloptions as it was for 1.2.
  671. function get_alloptions_110() {
  672. global $wpdb;
  673. if ($options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options")) {
  674. foreach ($options as $option) {
  675. // "When trying to design a foolproof system,
  676. // never underestimate the ingenuity of the fools :)" -- Dougal
  677. if ('siteurl' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value);
  678. if ('home' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value);
  679. if ('category_base' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value);
  680. $all_options->{$option->option_name} = stripslashes($option->option_value);
  681. }
  682. }
  683. return $all_options;
  684. }
  685. // Version of get_option that is private to install/upgrade.
  686. function __get_option($setting) {
  687. global $wpdb;
  688. if ( $setting == 'home' && defined( 'WP_HOME' ) ) {
  689. return preg_replace( '|/+$|', '', constant( 'WP_HOME' ) );
  690. }
  691. if ( $setting == 'siteurl' && defined( 'WP_SITEURL' ) ) {
  692. return preg_replace( '|/+$|', '', constant( 'WP_SITEURL' ) );
  693. }
  694. $option = $wpdb->get_var( $wpdb->prepare("SELECT option_value FROM $wpdb->options WHERE option_name = %s", $setting) );
  695. if ( 'home' == $setting && '' == $option )
  696. return __get_option('siteurl');
  697. if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting )
  698. $option = preg_replace('|/+$|', '', $option);
  699. @ $kellogs = unserialize($option);
  700. if ($kellogs !== FALSE)
  701. return $kellogs;
  702. else
  703. return $option;
  704. }
  705. function deslash($content) {
  706. // Note: \\\ inside a regex denotes a single backslash.
  707. // Replace one or more backslashes followed by a single quote with
  708. // a single quote.
  709. $content = preg_replace("/\\\+'/", "'", $content);
  710. // Replace one or more backslashes followed by a double quote with
  711. // a double quote.
  712. $content = preg_replace('/\\\+"/', '"', $content);
  713. // Replace one or more backslashes with one backslash.
  714. $content = preg_replace("/\\\+/", "\\", $content);
  715. return $content;
  716. }
  717. function dbDelta($queries, $execute = true) {
  718. global $wpdb;
  719. // Separate individual queries into an array
  720. if( !is_array($queries) ) {
  721. $queries = explode( ';', $queries );
  722. if('' == $queries[count($queries) - 1]) array_pop($queries);
  723. }
  724. $cqueries = array(); // Creation Queries
  725. $iqueries = array(); // Insertion Queries
  726. $for_update = array();
  727. // Create a tablename index for an array ($cqueries) of queries
  728. foreach($queries as $qry) {
  729. if(preg_match("|CREATE TABLE ([^ ]*)|", $qry, $matches)) {
  730. $cqueries[strtolower($matches[1])] = $qry;
  731. $for_update[$matches[1]] = 'Created table '.$matches[1];
  732. }
  733. else if(preg_match("|CREATE DATABASE ([^ ]*)|", $qry, $matches)) {
  734. array_unshift($cqueries, $qry);
  735. }
  736. else if(preg_match("|INSERT INTO ([^ ]*)|", $qry, $matches)) {
  737. $iqueries[] = $qry;
  738. }
  739. else if(preg_match("|UPDATE ([^ ]*)|", $qry, $matches)) {
  740. $iqueries[] = $qry;
  741. }
  742. else {
  743. // Unrecognized query type
  744. }
  745. }
  746. // Check to see which tables and fields exist
  747. if($tables = $wpdb->get_col('SHOW TABLES;')) {
  748. // For every table in the database
  749. foreach($tables as $table) {
  750. // If a table query exists for the database table...
  751. if( array_key_exists(strtolower($table), $cqueries) ) {
  752. // Clear the field and index arrays
  753. unset($cfields);
  754. unset($indices);
  755. // Get all of the field names in the query from between the parens
  756. preg_match("|\((.*)\)|ms", $cqueries[strtolower($table)], $match2);
  757. $qryline = trim($match2[1]);
  758. // Separate field lines into an array
  759. $flds = explode("\n", $qryline);
  760. //echo "<hr/><pre>\n".print_r(strtolower($table), true).":\n".print_r($cqueries, true)."</pre><hr/>";
  761. // For every field line specified in the query
  762. foreach($flds as $fld) {
  763. // Extract the field name
  764. preg_match("|^([^ ]*)|", trim($fld), $fvals);
  765. $fieldname = $fvals[1];
  766. // Verify the found field name
  767. $validfield = true;
  768. switch(strtolower($fieldname))
  769. {
  770. case '':
  771. case 'primary':
  772. case 'index':
  773. case 'fulltext':
  774. case 'unique':
  775. case 'key':
  776. $validfield = false;
  777. $indices[] = trim(trim($fld), ", \n");
  778. break;
  779. }
  780. $fld = trim($fld);
  781. // If it's a valid field, add it to the field array
  782. if($validfield) {
  783. $cfields[strtolower($fieldname)] = trim($fld, ", \n");
  784. }
  785. }
  786. // Fetch the table column structure from the database
  787. $tablefields = $wpdb->get_results("DESCRIBE {$table};");
  788. // For every field in the table
  789. foreach($tablefields as $tablefield) {
  790. // If the table field exists in the field array...
  791. if(array_key_exists(strtolower($tablefield->Field), $cfields)) {
  792. // Get the field type from the query
  793. preg_match("|".$tablefield->Field." ([^ ]*( unsigned)?)|i", $cfields[strtolower($tablefield->Field)], $matches);
  794. $fieldtype = $matches[1];
  795. // Is actual field type different from the field type in query?
  796. if($tablefield->Type != $fieldtype) {
  797. // Add a query to change the column type
  798. $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)];
  799. $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}";
  800. }
  801. // Get the default value from the array
  802. //echo "{$cfields[strtolower($tablefield->Field)]}<br>";
  803. if(preg_match("| DEFAULT '(.*)'|i", $cfields[strtolower($tablefield->Field)], $matches)) {
  804. $default_value = $matches[1];
  805. if($tablefield->Default != $default_value)
  806. {
  807. // Add a query to change the column's default value
  808. $cqueries[] = "ALTER TABLE {$table} ALTER COLUMN {$tablefield->Field} SET DEFAULT '{$default_value}'";
  809. $for_update[$table.'.'.$tablefield->Field] = "Changed default value of {$table}.{$tablefield->Field} from {$tablefield->Default} to {$default_value}";
  810. }
  811. }
  812. // Remove the field from the array (so it's not added)
  813. unset($cfields[strtolower($tablefield->Field)]);
  814. }
  815. else {
  816. // This field exists in the table, but not in the creation queries?
  817. }
  818. }
  819. // For every remaining field specified for the table
  820. foreach($cfields as $fieldname => $fielddef) {
  821. // Push a query line into $cqueries that adds the field to that table
  822. $cqueries[] = "ALTER TABLE {$table} ADD COLUMN $fielddef";
  823. $for_update[$table.'.'.$fieldname] = 'Added column '.$table.'.'.$fieldname;
  824. }
  825. // Index stuff goes here
  826. // Fetch the table index structure from the database
  827. $tableindices = $wpdb->get_results("SHOW INDEX FROM {$table};");
  828. if($tableindices) {
  829. // Clear the index array
  830. unset($index_ary);
  831. // For every index in the table
  832. foreach($tableindices as $tableindex) {
  833. // Add the index to the index data array
  834. $keyname = $tableindex->Key_name;
  835. $index_ary[$keyname]['columns'][] = array('fieldname' => $tableindex->Column_name, 'subpart' => $tableindex->Sub_part);
  836. $index_ary[$keyname]['unique'] = ($tableindex->Non_unique == 0)?true:false;
  837. }
  838. // For each actual index in the index array
  839. foreach($index_ary as $index_name => $index_data) {
  840. // Build a create string to compare to the query
  841. $index_string = '';
  842. if($index_name == 'PRIMARY') {
  843. $index_string .= 'PRIMARY ';
  844. }
  845. else if($index_data['unique']) {
  846. $index_string .= 'UNIQUE ';
  847. }
  848. $index_string .= 'KEY ';
  849. if($index_name != 'PRIMARY') {
  850. $index_string .= $index_name;
  851. }
  852. $index_columns = '';
  853. // For each column in the index
  854. foreach($index_data['columns'] as $column_data) {
  855. if($index_columns != '') $index_columns .= ',';
  856. // Add the field to the column list string
  857. $index_columns .= $column_data['fieldname'];
  858. if($column_data['subpart'] != '') {
  859. $index_columns .= '('.$column_data['subpart'].')';
  860. }
  861. }
  862. // Add the column list to the index create string
  863. $index_string .= ' ('.$index_columns.')';
  864. if(!(($aindex = array_search($index_string, $indices)) === false)) {
  865. unset($indices[$aindex]);
  866. //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br />Found index:".$index_string."</pre>\n";
  867. }
  868. //else echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br /><b>Did not find index:</b>".$index_string."<br />".print_r($indices, true)."</pre>\n";
  869. }
  870. }
  871. // For every remaining index specified for the table
  872. foreach ( (array) $indices as $index ) {
  873. // Push a query line into $cqueries that adds the index to that table
  874. $cqueries[] = "ALTER TABLE {$table} ADD $index";
  875. $for_update[$table.'.'.$fieldname] = 'Added index '.$table.' '.$index;
  876. }
  877. // Remove the original table creation query from processing
  878. unset($cqueries[strtolower($table)]);
  879. unset($for_update[strtolower($table)]);
  880. } else {
  881. // This table exists in the database, but not in the creation queries?
  882. }
  883. }
  884. }
  885. $allqueries = array_merge($cqueries, $iqueries);
  886. if($execute) {
  887. foreach($allqueries as $query) {
  888. //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">".print_r($query, true)."</pre>\n";
  889. $wpdb->query($query);
  890. }
  891. }
  892. return $for_update;
  893. }
  894. function make_db_current() {
  895. global $wp_queries;
  896. $alterations = dbDelta($wp_queries);
  897. echo "<ol>\n";
  898. foreach($alterations as $alteration) echo "<li>$alteration</li>\n";
  899. echo "</ol>\n";
  900. }
  901. function make_db_current_silent() {
  902. global $wp_queries;
  903. $alterations = dbDelta($wp_queries);
  904. }
  905. function make_site_theme_from_oldschool($theme_name, $template) {
  906. $home_path = get_home_path();
  907. $site_dir = WP_CONTENT_DIR . "/themes/$template";
  908. if (! file_exists("$home_path/index.php"))
  909. return false;
  910. // Copy files from the old locations to the site theme.
  911. // TODO: This does not copy arbitarary include dependencies. Only the
  912. // standard WP files are copied.
  913. $files = array('index.php' => 'index.php', 'wp-layout.css' => 'style.css', 'wp-comments.php' => 'comments.php', 'wp-comments-popup.php' => 'comments-popup.php');
  914. foreach ($files as $oldfile => $newfile) {
  915. if ($oldfile == 'index.php')
  916. $oldpath = $home_path;
  917. else
  918. $oldpath = ABSPATH;
  919. if ($oldfile == 'index.php') { // Check to make sure it's not a new index
  920. $index = implode('', file("$oldpath/$oldfile"));
  921. if (strpos($index, 'WP_USE_THEMES') !== false) {
  922. if (! @copy(WP_CONTENT_DIR . '/themes/default/index.php', "$site_dir/$newfile"))
  923. return false;
  924. continue; // Don't copy anything
  925. }
  926. }
  927. if (! @copy("$oldpath/$oldfile", "$site_dir/$newfile"))
  928. return false;
  929. chmod("$site_dir/$newfile", 0777);
  930. // Update the blog header include in each file.
  931. $lines = explode("\n", implode('', file("$site_dir/$newfile")));
  932. if ($lines) {
  933. $f = fopen("$site_dir/$newfile", 'w');
  934. foreach ($lines as $line) {
  935. if (preg_match('/require.*wp-blog-header/', $line))
  936. $line = '//' . $line;
  937. // Update stylesheet references.
  938. $line = str_replace("<?php echo __get_option('siteurl'); ?>/wp-layout.css", "<?php bloginfo('stylesheet_url'); ?>", $line);
  939. // Update comments template inclusion.
  940. $line = str_replace("<?php include(ABSPATH . 'wp-comments.php'); ?>", "<?php comments_template(); ?>", $line);
  941. fwrite($f, "{$line}\n");
  942. }
  943. fclose($f);
  944. }
  945. }
  946. // Add a theme header.
  947. $header = "/*\nTheme Name: $theme_name\nTheme URI: " . __get_option('siteurl') . "\nDescription: A theme automatically created by the upgrade.\nVersion: 1.0\nAuthor: Moi\n*/\n";
  948. $stylelines = file_get_contents("$site_dir/style.css");
  949. if ($stylelines) {
  950. $f = fopen("$site_dir/style.css", 'w');
  951. fwrite($f, $header);
  952. fwrite($f, $stylelines);
  953. fclose($f);
  954. }
  955. return true;
  956. }
  957. function make_site_theme_from_default($theme_name, $template) {
  958. $site_dir = WP_CONTENT_DIR . "/themes/$template";
  959. $default_dir = WP_CONTENT_DIR . '/themes/default';
  960. // Copy files from the default theme to the site theme.
  961. //$files = array('index.php', 'comments.php', 'comments-popup.php', 'footer.php', 'header.php', 'sidebar.php', 'style.css');
  962. $theme_dir = @ opendir("$default_dir");
  963. if ($theme_dir) {
  964. while(($theme_file = readdir( $theme_dir )) !== false) {
  965. if (is_dir("$default_dir/$theme_file"))
  966. continue;
  967. if (! @copy("$default_dir/$theme_file", "$site_dir/$theme_file"))
  968. return;
  969. chmod("$site_dir/$theme_file", 0777);
  970. }
  971. }
  972. @closedir($theme_dir);
  973. // Rewrite the theme header.
  974. $stylelines = explode("\n", implode('', file("$site_dir/style.css")));
  975. if ($stylelines) {
  976. $f = fopen("$site_dir/style.css", 'w');
  977. foreach ($stylelines as $line) {
  978. if (strpos($line, 'Theme Name:') !== false) $line = 'Theme Name: ' . $theme_name;
  979. elseif (strpos($line, 'Theme URI:') !== false) $line = 'Theme URI: ' . __get_option('url');
  980. elseif (strpos($line, 'Description:') !== false) $line = 'Description: Your theme.';
  981. elseif (strpos($line, 'Version:') !== false) $line = 'Version: 1';
  982. elseif (strpos($line, 'Author:') !== false) $line = 'Author: You';
  983. fwrite($f, $line . "\n");
  984. }
  985. fclose($f);
  986. }
  987. // Copy the images.
  988. umask(0);
  989. if (! mkdir("$site_dir/images", 0777)) {
  990. return false;
  991. }
  992. $images_dir = @ opendir("$default_dir/images");
  993. if ($images_dir) {
  994. while(($image = readdir($images_dir)) !== false) {
  995. if (is_dir("$default_dir/images/$image"))
  996. continue;
  997. if (! @copy("$default_dir/images/$image", "$site_dir/images/$image"))
  998. return;
  999. chmod("$site_dir/images/$image", 0777);
  1000. }
  1001. }
  1002. @closedir($images_dir);
  1003. }
  1004. // Create a site theme from the default theme.
  1005. function make_site_theme() {
  1006. // Name the theme after the blog.
  1007. $theme_name = __get_option('blogname');
  1008. $template = sanitize_title($theme_name);
  1009. $site_dir = WP_CONTENT_DIR . "/themes/$template";
  1010. // If the theme already exists, nothing to do.
  1011. if ( is_dir($site_dir)) {
  1012. return false;
  1013. }
  1014. // We must be able to write to the themes dir.
  1015. if (! is_writable(WP_CONTENT_DIR . "/themes")) {
  1016. return false;
  1017. }
  1018. umask(0);
  1019. if (! mkdir($site_dir, 0777)) {
  1020. return false;
  1021. }
  1022. if (file_exists(ABSPATH . 'wp-layout.css')) {
  1023. if (! make_site_theme_from_oldschool($theme_name, $template)) {
  1024. // TODO: rm -rf the site theme directory.
  1025. return false;
  1026. }
  1027. } else {
  1028. if (! make_site_theme_from_default($theme_name, $template))
  1029. // TODO: rm -rf the site theme directory.
  1030. return false;
  1031. }
  1032. // Make the new site theme active.
  1033. $current_template = __get_option('template');
  1034. if ($current_template == 'default') {
  1035. update_option('template', $template);
  1036. update_option('stylesheet', $template);
  1037. }
  1038. return $template;
  1039. }
  1040. function translate_level_to_role($level) {
  1041. switch ($level) {
  1042. case 10:
  1043. case 9:
  1044. case 8:
  1045. return 'administrator';
  1046. case 7:
  1047. case 6:
  1048. case 5:
  1049. return 'editor';
  1050. case 4:
  1051. case 3:
  1052. case 2:
  1053. return 'author';
  1054. case 1:
  1055. return 'contributor';
  1056. case 0:
  1057. return 'subscriber';
  1058. }
  1059. }
  1060. function wp_check_mysql_version() {
  1061. global $wpdb;
  1062. $result = $wpdb->check_database_version();
  1063. if ( is_wp_error( $result ) )
  1064. die( $result->get_error_message() );
  1065. }
  1066. function maybe_disable_automattic_widgets() {
  1067. $plugins = __get_option( 'active_plugins' );
  1068. foreach ( (array) $plugins as $plugin ) {
  1069. if ( basename( $plugin ) == 'widgets.php' ) {
  1070. array_splice( $plugins, array_search( $plugin, $plugins ), 1 );
  1071. update_option( 'active_plugins', $plugins );
  1072. break;
  1073. }
  1074. }
  1075. }
  1076. ?>