Making WordPress.org

Changeset 12766


Ignore:
Timestamp:
08/01/2023 02:58:10 PM (3 years ago)
Author:
amieiro
Message:

Translate: Add the stats for the different translation sources

File:
1 edited

Legend:

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

    r12434 r12766  
    121121        private string $header                           = '';
    122122        private string $originals_by_year                = '';
     123        private string $originals_by_translation_source  = '';
    123124        private string $translations_translators_by_year = '';
    124125        private string $forum_post_and_replies_by_year   = '';
    … …  
    146147
    147148        /**
     149         * The id of the blog where the stats are stored.
     150         *
     151         * @var int
     152         */
     153        private const MAKE_POLYGLOTS_BLOG_ID = 19;
     154
     155        /**
     156         * The id of the page where the stats are stored.
     157         *
     158         * @var int
     159         */
     160        private const POLYGLOTS_PAGE_ID = 42132;
     161
     162        /**
    148163         * Prints the Polyglots stats or stores them on a page.
    149164         *
    … …  
    161176                }
    162177
    163                 define( 'MAKE_POLYGLOTS_BLOG_ID', 19 );
    164                 define( 'POLYGLOTS_PAGE_ID', 42132 );
    165 
    166178                $this->echo_the_values = $echo_the_values;
    167179                $this->set_number_of_years_with_data();
    168180                $this->print_header();
     181                $this->print_stats_comparison( gmdate( 'Y-m-d' ) );
    169182                $this->print_wordpress_translation_percentage();
     183                $this->print_stats_for_translation_sources();
    170184                $this->print_packages_generated();
    171185                $this->print_unique_themes_plugins_by_year();
    … …  
    178192                $this->print_most_active_translators();
    179193                $this->store_stats();
    180                 $this->print_stats_comparison( gmdate( 'Y-m-d' ) );
    181194
    182195                $this->update_page();
    … …  
    240253        private function get_locale_requests() {
    241254                $locale_requests = array();
    242                 switch_to_blog( MAKE_POLYGLOTS_BLOG_ID );
     255                switch_to_blog( self::MAKE_POLYGLOTS_BLOG_ID );
    243256                $args                     = array(
    244257                        'post_type'   => 'post',
    … …  
    282295        private function get_editor_requests() {
    283296                $editor_requests = array();
    284                 switch_to_blog( MAKE_POLYGLOTS_BLOG_ID );
     297                switch_to_blog( self::MAKE_POLYGLOTS_BLOG_ID );
    285298                register_taxonomy(
    286299                        'p2_resolved',
    … …  
    943956                        'posts_per_page' => - 1,
    944957                        'post_status'    => 'publish',
    945                         'post_type'      => $this::FEEDBACK_POST_TYPE,
     958                        'post_type'      => self::FEEDBACK_POST_TYPE,
    946959                        'date_query'     => array(
    947960                                array( 'after' => '2022-07-28' ),
    948961                        ),
    949962                );
    950                 $feedback_posts_count = $wpdb->get_var( $wpdb->prepare(
    951                         "SELECT COUNT(*) FROM $wpdb->posts WHERE post_status='publish' AND post_type=%s AND post_date > %s", $this::FEEDBACK_POST_TYPE, '2022-07-28'
    952                 ));
     963                $feedback_posts_count = $wpdb->get_var(
     964                        $wpdb->prepare(
     965                                "SELECT COUNT(*) FROM $wpdb->posts WHERE post_status='publish' AND post_type=%s AND post_date > %s",
     966                                self::FEEDBACK_POST_TYPE,
     967                                '2022-07-28'
     968                        )
     969                );
    953970                $original_strings_with_comments = number_format_i18n( $feedback_posts_count );
    954971
    … …  
    958975                                array(
    959976                                        'number'     => - 1,
    960                                         'post_type'  => $this::FEEDBACK_POST_TYPE,
     977                                        'post_type'  => self::FEEDBACK_POST_TYPE,
    961978                                        'count'      => true,
    962979                                        'date_query' => array(
    … …  
    970987                // First, get the comments related with a translation, because we can get comments related
    971988                // only with the original.
    972                 $comment_meta_translation_ids = $wpdb->get_col( $wpdb->prepare(
    973                         "SELECT cm.meta_value FROM $wpdb->commentmeta cm, $wpdb->comments c, $wpdb->posts p WHERE p.post_status='publish' AND p.post_type=%s AND p.post_date > %s AND cm.meta_key = 'translation_id' AND c.comment_post_id = p.id AND cm.comment_id = c.comment_id", $this::FEEDBACK_POST_TYPE, '2022-07-28'
    974                 ));
     989                $comment_meta_translation_ids = $wpdb->get_col(
     990                        $wpdb->prepare(
     991                                "SELECT cm.meta_value FROM $wpdb->commentmeta cm, $wpdb->comments c, $wpdb->posts p WHERE p.post_status='publish' AND p.post_type=%s AND p.post_date > %s AND cm.meta_key = 'translation_id' AND c.comment_post_id = p.id AND cm.comment_id = c.comment_id",
     992                                self::FEEDBACK_POST_TYPE,
     993                                '2022-07-28'
     994                        )
     995                );
    975996
    976997                // Check all comments with a related translation.
    … …  
    10161037
    10171038                // Get most active commenter's.
    1018                 $comment_user_ids = $wpdb->get_col( $wpdb->prepare(
    1019                         "SELECT c.user_id FROM $wpdb->comments c, $wpdb->posts p WHERE p.post_status='publish' AND p.post_type=%s AND p.post_date > %s AND c.comment_post_id = p.id", $this::FEEDBACK_POST_TYPE, '2022-07-28'
    1020                 ));
     1039                $comment_user_ids = $wpdb->get_col(
     1040                        $wpdb->prepare(
     1041                                "SELECT c.user_id FROM $wpdb->comments c, $wpdb->posts p WHERE p.post_status='publish' AND p.post_type=%s AND p.post_date > %s AND c.comment_post_id = p.id",
     1042                                self::FEEDBACK_POST_TYPE,
     1043                                '2022-07-28'
     1044                        )
     1045                );
    10211046
    10221047                $commenters_with_comment_count = array_count_values( $comment_user_ids );
    … …  
    11661191                if ( ! $this->echo_the_values ) {
    11671192                        $this->contributors_per_locale .= $this->create_gutenberg_code( $code );
     1193                } else {
     1194                        WP_CLI::log( $code );
     1195                }
     1196        }
     1197
     1198        /**
     1199         * Print the stats for translation sources.
     1200         *
     1201         * @return void
     1202         */
     1203        private function print_stats_for_translation_sources(): void {
     1204                global $wpdb;
     1205                // Source used: frontend, file import, playground
     1206                $originals = $wpdb->get_results(
     1207                        "SELECT
     1208                        meta_key,
     1209                        meta_value,
     1210                        count(*) as number_of_strings
     1211                        FROM `translate_meta`
     1212                        WHERE
     1213                            object_type = 'translation'
     1214                                AND meta_key = 'source'
     1215                            AND meta_value <> ''
     1216                        GROUP BY
     1217                            meta_key,
     1218                            meta_value
     1219                        ORDER BY
     1220                            `translate_meta`.
     1221                            `meta_key` ASC,
     1222                            count(*) desc
     1223                        "
     1224                );
     1225                if ( ! $this->echo_the_values ) {
     1226                        $this->originals_by_translation_source = $this->create_gutenberg_heading( 'Number of translations by translation source (starting on 2023-06-30)' );
     1227                } else {
     1228                        $this->print_wpcli_heading( 'Number of translations by translation source (starting on 2023-06-30)' );
     1229                }
     1230                $code  = "Source \t\t\t\t Number of strings" . PHP_EOL;
     1231                $code .= '................................................................' . PHP_EOL;
     1232
     1233                foreach ( $originals as $original ) {
     1234                        $code .= str_pad( ucfirst( $original->meta_value ), 15 ) . " \t\t " . str_pad( number_format_i18n( $original->number_of_strings ), 15, ' ', STR_PAD_LEFT ) . PHP_EOL;
     1235                }
     1236                $code .= PHP_EOL;
     1237
     1238                if ( ! $this->echo_the_values ) {
     1239                        $this->originals_by_translation_source .= $this->create_gutenberg_code( $code );
     1240                } else {
     1241                        WP_CLI::log( $code );
     1242                }
     1243
     1244                // Suggestion used: TM, OpenAI, DeepL, undefined.
     1245                $suggestions = $wpdb->get_results(
     1246                        "SELECT
     1247                        meta_key,
     1248                        meta_value,
     1249                        count(*) as number_of_strings
     1250                        FROM `translate_meta`
     1251                        WHERE
     1252                            object_type = 'translation'
     1253                                AND meta_key = 'suggestion_used'
     1254                            AND
     1255                        (meta_value LIKE 'tm%'
     1256                     OR meta_value LIKE 'openai%'
     1257                     OR meta_value LIKE 'deepl%') 
     1258                        GROUP BY
     1259                            meta_key,
     1260                            meta_value
     1261                        ORDER BY
     1262                            `translate_meta`.
     1263                            `meta_key` ASC,
     1264                            count(*) desc
     1265                        "
     1266                );
     1267                if ( ! $this->echo_the_values ) {
     1268                        $this->originals_by_translation_source .= $this->create_gutenberg_heading( 'Number of translations by suggestion source (starting on 2023-06-30)' );
     1269                } else {
     1270                        $this->print_wpcli_heading( 'Number of translations by suggestion source (starting on 2023-06-30)' );
     1271                }
     1272                $code  = "Source \t\t\t\t Number of strings" . PHP_EOL;
     1273                $code .= '................................................................' . PHP_EOL;
     1274
     1275                foreach ( $suggestions as $suggestion ) {
     1276                        $code .= str_pad( $suggestion->meta_value, 15 ) . " \t\t " . str_pad( number_format_i18n( $suggestion->number_of_strings ), 15, ' ', STR_PAD_LEFT ) . PHP_EOL;
     1277                }
     1278                $code .= PHP_EOL;
     1279
     1280                if ( ! $this->echo_the_values ) {
     1281                        $this->originals_by_translation_source .= $this->create_gutenberg_code( $code );
     1282                } else {
     1283                        WP_CLI::log( $code );
     1284                }
     1285
     1286                // Suggestion from other languages.
     1287                $suggestions_ol = $wpdb->get_results(
     1288                        "SELECT
     1289                        meta_key,
     1290                        meta_value,
     1291                        count(*) as number_of_strings
     1292                        FROM `translate_meta`
     1293                        WHERE
     1294                            object_type = 'translation'
     1295                                AND meta_key = 'suggestion_used'
     1296                            AND
     1297                        NOT (meta_value LIKE 'tm%'
     1298                     OR meta_value LIKE 'undefined%'
     1299                     OR meta_value LIKE 'openai%'
     1300                     OR meta_value LIKE 'deepl%') 
     1301                        GROUP BY
     1302                            meta_key,
     1303                            meta_value
     1304                        ORDER BY
     1305                            `translate_meta`.
     1306                            `meta_key` ASC,
     1307                            count(*) desc
     1308                        "
     1309                );
     1310                if ( ! $this->echo_the_values ) {
     1311                        $this->originals_by_translation_source .= $this->create_gutenberg_heading( 'Number of translations suggested from another language (starting on 2023-06-30)' );
     1312                } else {
     1313                        $this->print_wpcli_heading( 'Number of translations suggested from another language (starting on 2023-06-30)' );
     1314                }
     1315                $code  = "Language \t\t\t\t Number of strings" . PHP_EOL;
     1316                $code .= '................................................................' . PHP_EOL;
     1317
     1318                foreach ( $suggestions_ol as $suggestion ) {
     1319                        $code .= str_pad( $suggestion->meta_value, 15 ) . " \t\t " . str_pad( number_format_i18n( $suggestion->number_of_strings ), 15, ' ', STR_PAD_LEFT ) . PHP_EOL;
     1320                }
     1321                $code .= PHP_EOL;
     1322
     1323                if ( ! $this->echo_the_values ) {
     1324                        $this->originals_by_translation_source .= $this->create_gutenberg_code( $code );
    11681325                } else {
    11691326                        WP_CLI::log( $code );
    … …  
    14961653                        'wp_revisions_to_keep',
    14971654                        function ( $num, $post ) {
    1498                                 if ( POLYGLOTS_PAGE_ID === $post->ID ) {
     1655                                if ( self::POLYGLOTS_PAGE_ID === $post->ID ) {
    14991656                                        $num = 0; // pretend we don't want to keep revisions so that it will not lookup all old revisions.
    15001657                                }
    … …  
    15051662                        2
    15061663                );
    1507                 switch_to_blog( MAKE_POLYGLOTS_BLOG_ID );
     1664                switch_to_blog( self::MAKE_POLYGLOTS_BLOG_ID );
    15081665
    15091666                wp_update_post(
    15101667                        array(
    1511                                 'ID'           => POLYGLOTS_PAGE_ID,
     1668                                'ID'           => self::POLYGLOTS_PAGE_ID,
    15121669                                'post_type'    => 'page',
    15131670                                'post_author'  => 'Amieiro',
    … …  
    15281685                        $this->stats_comparison .
    15291686                        $this->wordpress_translation_percentage .
     1687                        $this->originals_by_translation_source .
    15301688                        $this->originals_by_year .
    15311689                        $this->packages_generated_by_year .
Note: See TracChangeset for help on using the changeset viewer.