function upgrade_database($from = NULL) { global $wpdb; if (is_null($from) or $from <= 0.96) { $from = 0.96; } switch ($from) { case 0.96: // account for changes to syndication custom values and guid echo "<p>Upgrading database from {$from} to " . FEEDWORDPRESS_VERSION . "...</p>\n"; $cat_id = FeedWordPress::link_category_id(); // Avoid duplicates $wpdb->query("DELETE FROM `{$wpdb->postmeta}` WHERE meta_key = 'syndication_feed_id'"); // Look up all the link IDs $wpdb->query("\n\t\t\tCREATE TEMPORARY TABLE tmp_custom_values\n\t\t\tSELECT\n\t\t\t\tNULL AS meta_id,\n\t\t\t\tpost_id,\n\t\t\t\t'syndication_feed_id' AS meta_key,\n\t\t\t\tlink_id AS meta_value\n\t\t\tFROM `{$wpdb->postmeta}`, `{$wpdb->links}`\n\t\t\tWHERE\n\t\t\t\tmeta_key='syndication_feed'\n\t\t\t\tAND meta_value=link_rss\n\t\t\t\tAND link_category = {$cat_id}\n\t\t\t"); // Now attach them to their posts $wpdb->query("INSERT INTO `{$wpdb->postmeta}` SELECT * FROM tmp_custom_values"); // And clean up after ourselves. $wpdb->query("DROP TABLE tmp_custom_values"); // Now fix the guids to avoid duplicate posts echo "<ul>"; foreach ($this->feeds as $feed) { echo "<li>Fixing post meta-data for <cite>" . $feed['link/name'] . "</cite> … "; flush(); $rss = @fetch_rss($feed['link/uri']); if (is_array($rss->items)) { foreach ($rss->items as $item) { $guid = $wpdb->escape(FeedWordPress::guid($item, $feed)); // new GUID algorithm $link = $wpdb->escape($item['link']); $wpdb->query("\n\t\t\t\t\t\tUPDATE `{$wpdb->posts}` SET guid='{$guid}' WHERE guid='{$link}'\n\t\t\t\t\t\t"); } } echo "<strong>complete.</strong></li>\n"; } echo "</ul>\n"; // Mark the upgrade as successful. update_option('feedwordpress_version', FEEDWORDPRESS_VERSION); } echo "<p>Upgrade complete. FeedWordPress is now ready to use again.</p>"; }