WordPress.org

Making WordPress.org


Ignore:
Timestamp:
03/30/2019 10:12:10 AM (6 months ago)
Author:
ocean90
Message:

Translate: Query variants of a locale in a more performant way.

Instead of querying for a specific list of projects, use the main WordPress core project (wp/dev) which is the canonical source for all available and active locales.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-routes/inc/routes/class-locale.php

    r8548 r8558  
    134134        }
    135135
    136         $variants = $this->get_locale_variants( $locale_slug, $project_ids );
     136        $variants = $this->get_locale_variants( $locale_slug );
    137137        // If there were no results for the current variant in the current project branch, it should still show it.
    138138        if ( ! in_array( $set_slug, $variants, true ) ) {
     
    188188        }
    189189
    190         $sub_projects = $this->get_active_sub_projects( $sub_project, true );
    191         $sub_project_slugs = array();
     190        $sub_project_statuses = array();
     191        $sub_projects         = $this->get_active_sub_projects( $sub_project, true );
    192192        if ( $sub_projects ) {
    193             $sub_project_ids      = array();
    194             $sub_project_statuses = array();
    195193            foreach ( $sub_projects as $key => $_sub_project ) {
    196                 $sub_project_slugs[] = $_sub_project->slug;
    197                 $status = $this->get_project_status( $_sub_project, $locale_slug, $set_slug, null, false );
    198 
    199                 $sub_project_ids[] = $_sub_project->id;
    200 
    201                 $sub_project_statuses[ $_sub_project->slug ] = $status;
    202             }
    203 
    204             $variants = $this->get_locale_variants( $locale_slug, $sub_project_ids );
    205 
    206             unset( $sub_project_ids );
    207         } else {
    208             $variants = $this->get_locale_variants( $locale_slug, array( $sub_project->id ) );
    209         }
     194                $sub_project_statuses[ $_sub_project->slug ] = $this->get_project_status(
     195                    $_sub_project,
     196                    $locale_slug,
     197                    $set_slug,
     198                    null,
     199                    false
     200                );
     201            }
     202        }
     203
     204        $sub_project_slugs = array_keys( $sub_project_statuses );
     205
     206        $variants = $this->get_locale_variants( $locale_slug );
    210207
    211208        $locale_contributors = $this->get_locale_contributors( $sub_project, $locale_slug, $set_slug );
     
    221218     * Whether a translation set slug exists for a locale.
    222219     *
     220     * For performance reasons, this checks the wp/dev project which is the canonical
     221     * source for all available and active locales.
     222     *
    223223     * @param \GP_Locale $locale The locale.
    224224     * @param string     $slug   The slug of a translation set.
     
    226226     */
    227227    private function translation_set_slug_exists( $locale, $slug ) {
    228         /*$cache_key = "translation_set_slugs:{$locale->slug}";
    229         $slugs = wp_cache_get( $cache_key, $this->cache_group );
    230 
    231         if ( false === $slugs ) {
    232             global $wpdb;
    233             $slugs = $wpdb->get_col( $wpdb->prepare(
    234                 "SELECT DISTINCT(slug) FROM {$wpdb->gp_translation_sets} WHERE locale = %s",
    235                 $locale->slug
    236             ) );
    237 
    238             wp_cache_set( $cache_key, $slugs, $this->cache_group, DAY_IN_SECONDS );
    239         }*/
    240 
    241         // Hardcoded list because the query above doesn't perform well due to a missing index.
    242         $slugs = [
    243             'default' => [
    244                 'default',
    245             ],
    246             'ca' => [
    247                 'default',
    248                 'valencia',
    249             ],
    250             'de' => [
    251                 'default',
    252                 'formal',
    253             ],
    254             'de-ch' => [
    255                 'default',
    256                 'informal',
    257             ],
    258             'nl' => [
    259                 'default',
    260                 'formal',
    261             ],
    262             'pt' => [
    263                 'default',
    264                 'informal',
    265                 'ao90',
    266             ],
    267         ];
    268 
    269         if ( isset( $slugs[ $locale->slug ] ) ) {
    270             return in_array( $slug, $slugs[ $locale->slug ], true );
    271         }
    272 
    273         return in_array( $slug, $slugs['default'], true );
     228        global $wpdb;
     229
     230        $id = $wpdb->get_var( $wpdb->prepare(
     231            "SELECT id FROM {$wpdb->gp_translation_sets} WHERE locale = %s AND slug = %s AND project_id = %d",
     232            $locale->slug,
     233            $slug,
     234            2 // wp/dev
     235        ) );
     236
     237        return null !== $id;
    274238    }
    275239
     
    346310
    347311    /**
    348      * Retrieves non-default slugs of translation sets for a list of
    349      * project IDs.
    350      *
    351      * @param string $locale     Slug of a GlotPress locale.
    352      * @param array $project_ids List of project IDs.
    353      * @return array List of non-default slugs.
    354      */
    355     private function get_locale_variants( $locale, $project_ids ) {
     312     * Retrieves all slugs of translation sets for a locale.
     313     *
     314     * For performance reasons, this checks the wp/dev project which is the canonical
     315     * source for all available and active locales.
     316     *
     317     * @param string $locale Slug of a GlotPress locale.
     318     * @return array List of translation set slugs.
     319     */
     320    private function get_locale_variants( $locale ) {
    356321        global $wpdb;
    357322
    358         $project_ids = implode( ',', $project_ids );
    359         $slugs = $wpdb->get_col( $wpdb->prepare( "
    360             SELECT DISTINCT slug
    361             FROM {$wpdb->gp_translation_sets}
    362             WHERE
    363                 project_id IN( $project_ids )
    364                 AND locale = %s
    365         ", $locale ) );
    366 
    367         return $slugs;
     323        return $wpdb->get_col( $wpdb->prepare(
     324            "SELECT slug FROM {$wpdb->gp_translation_sets} WHERE locale = %s AND project_id = %d",
     325            $locale,
     326            2 // wp/dev
     327        ) );
    368328    }
    369329
Note: See TracChangeset for help on using the changeset viewer.