Make WordPress Core

Changeset 32814


Ignore:
Timestamp:
06/17/2015 01:52:46 AM (11 years ago)
Author:
boonebgorges
Message:

Split all shared taxonomy terms on upgrade to 4.3.

Dear Shared Terms, Welcome to Splitsville. Population: You.

See #30261.

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/upgrade.php

    r32768 r32814  
    535535        // upgrade_420() to catch bad comments prior to any auto-expansion of
    536536        // MySQL column widths.
    537         if ( $wp_current_db_version < 32364 )
     537        if ( $wp_current_db_version < 32814 )
    538538                upgrade_430();
    539539
    … …  
    15601560                }
    15611561        }
     1562
     1563        if ( $wp_current_db_version < 32814 ) {
     1564                split_all_shared_terms();
     1565        }
    15621566}
    15631567
    … …  
    18491853
    18501854        return $wpdb->query( "ALTER TABLE $table CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci" );
     1855}
     1856
     1857/**
     1858 * Split all shared taxonomy terms.
     1859 *
     1860 * @since 4.3.0
     1861 */
     1862function split_all_shared_terms() {
     1863        global $wpdb;
     1864
     1865        // Get a list of shared terms (those with more than one associated row in term_taxonomy).
     1866        $shared_terms = $wpdb->get_results(
     1867                "SELECT tt.term_id, t.*, count(*) as term_tt_count FROM {$wpdb->term_taxonomy} tt
     1868                 LEFT JOIN {$wpdb->terms} t ON t.term_id = tt.term_id
     1869                 GROUP BY t.term_id
     1870                 HAVING term_tt_count > 1"
     1871        );
     1872
     1873        // Rekey shared term array for faster lookups.
     1874        $_shared_terms = array();
     1875        foreach ( $shared_terms as $shared_term ) {
     1876                $term_id = intval( $shared_term->term_id );
     1877                $_shared_terms[ $term_id ] = $shared_term;
     1878        }
     1879        $shared_terms = $_shared_terms;
     1880
     1881        // Get term taxonomy data for all shared terms.
     1882        $shared_term_ids = implode( ',', array_keys( $shared_terms ) );
     1883        $shared_tts = $wpdb->get_results( "SELECT * FROM {$wpdb->term_taxonomy} WHERE `term_id` IN ({$shared_term_ids})" );
     1884
     1885        // Split term data recording is slow, so we do it just once, outside the loop.
     1886        $suspend = wp_suspend_cache_invalidation( true );
     1887        $split_term_data = get_option( '_split_terms', array() );
     1888        $skipped_first_term = $taxonomies = array();
     1889        foreach ( $shared_tts as $shared_tt ) {
     1890                $term_id = intval( $shared_tt->term_id );
     1891
     1892                // Don't split the first tt belonging to a given term_id.
     1893                if ( ! isset( $skipped_first_term[ $term_id ] ) ) {
     1894                        $skipped_first_term[ $term_id ] = 1;
     1895                        continue;
     1896                }
     1897
     1898                if ( ! isset( $split_term_data[ $term_id ] ) ) {
     1899                        $split_term_data[ $term_id ] = array();
     1900                }
     1901
     1902                // Keep track of taxonomies whose hierarchies need flushing.
     1903                if ( ! isset( $taxonomies[ $shared_tt->taxonomy ] ) ) {
     1904                        $taxonomies[ $shared_tt->taxonomy ] = 1;
     1905                }
     1906
     1907                // Split the term.
     1908                $split_term_data[ $term_id ][ $shared_tt->taxonomy ] = _split_shared_term( $shared_terms[ $term_id ], $shared_tt, false );
     1909        }
     1910
     1911        // Rebuild the cached hierarchy for each affected taxonomy.
     1912        foreach ( array_keys( $taxonomies ) as $tax ) {
     1913                delete_option( "{$tax}_children" );
     1914                _get_term_hierarchy( $tax );
     1915        }
     1916
     1917        wp_suspend_cache_invalidation( $suspend );
     1918        update_option( '_split_terms', $split_term_data );
    18511919}
    18521920
  • trunk/src/wp-includes/version.php

    r32454 r32814  
    1212 * @global int $wp_db_version
    1313 */
    14 $wp_db_version = 32453;
     14$wp_db_version = 32814;
    1515
    1616/**
Note: See TracChangeset for help on using the changeset viewer.