Making WordPress.org

Changeset 9709


Ignore:
Timestamp:
04/08/2020 06:31:10 AM (4 years ago)
Author:
dd32
Message:

Translate, Project Stats: Store projects that a user translates, for display in profiles.

This doesn't store the actual project, but rather the "parent" project we're most interested in. Ie. 'wp-plugins/hello-dolly' rather than 'wp-plugins/hello-dolly/stable'

See https://meta.trac.wordpress.org/ticket/1664.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-custom-stats/stats/user.php

    r4350 r9709  
    1010class WPorg_GP_User_Stats {
    1111
    12     private $user_stats = array();
     12    private $user_stats = [];
     13
     14    private $user_project_stats = [];
    1315
    1416    public function __construct() {
     
    2224
    2325        $wpdb->user_translations_count = $gp_table_prefix . 'user_translations_count';
     26        $wpdb->user_projects           = $gp_table_prefix . 'user_projects';
    2427    }
    2528
     
    4447
    4548        }
     49
     50        // Record the last time the user submitted a translation for a project/locale.
     51        if ( 'gp_translation_created' == current_filter() ) {
     52            $project = GP::$project->get( $translation_set->project_id );
     53
     54            // Find the "root" project ID.
     55            // For projects with sub-projects, we only want to store the "parent" project.
     56            // ie. wp-plugins/plugin-name, wp/dev, or apps/android
     57            $project_path_we_want = implode( '/', array_slice( explode( '/', $project->path ), 0, 2 ) );
     58            if ( $project_path_we_want != $project->path ) {
     59                $project = GP::$project->by_path( $project_path_we_want );
     60            }
     61
     62            $this->user_project_stats[ "{$translation->user_id},{$project->id},{$translation_set->locale},{$translation_set->slug}" ] = true;
     63        }
    4664    }
    4765
     
    6583        $now = current_time( 'mysql', 1 );
    6684
    67         $values = array();
     85        $values = [];
    6886        foreach ( $this->user_stats as $key => $stats ) {
    6987            list( $user_id, $locale, $locale_slug ) = explode( ',', $key );
     
    86104                    ON DUPLICATE KEY UPDATE `suggested`=`suggested` + VALUES(`suggested`), `accepted`=`accepted` + VALUES(`accepted`), `date_modified`=VALUES(`date_modified`)"
    87105                );
    88                 $values = array();
     106                $values = [];
    89107            }
    90108        }
     
    96114                ON DUPLICATE KEY UPDATE `suggested`=`suggested` + VALUES(`suggested`), `accepted`=`accepted` + VALUES(`accepted`), `date_modified`=VALUES(`date_modified`)"
    97115            );
     116            $values = [];
     117        }
     118
     119        // Process the project stats too.
     120        $values = [];
     121        foreach ( $this->user_project_stats as $key => $dummy ) {
     122            list( $user_id, $project_id, $locale, $locale_slug ) = explode( ',', $key );
     123
     124            $values[] = $wpdb->prepare( '(%d, %d, %s, %s, %s)',
     125                $user_id,
     126                $project_id,
     127                $locale,
     128                $locale_slug,
     129                $now
     130            );
     131
     132            if ( count( $values ) > 50 ) {
     133                $wpdb->query(
     134                    "INSERT INTO {$wpdb->user_projects} (`user_id`, `project_id`, `locale`, `locale_slug`, `last_modified`)
     135                    VALUES " . implode( ', ', $values ) . "
     136                    ON DUPLICATE KEY UPDATE `last_modified` = GREATEST( `last_modified`, VALUES(`last_modified`) )"
     137                );
     138                $values = [];
     139            }
     140        }
     141        if ( $values ) {
     142            $wpdb->query(
     143                "INSERT INTO {$wpdb->user_projects} (`user_id`, `project_id`, `locale`, `locale_slug`, `last_modified`)
     144                VALUES " . implode( ', ', $values ) . "
     145                ON DUPLICATE KEY UPDATE `last_modified` = GREATEST( `last_modified`, VALUES(`last_modified`) )"
     146            );
     147            $values = [];
    98148        }
    99149    }
     
    101151
    102152/*
    103 Table:
     153Tables:
    104154
    105155Note: WordPress uses BIGINT(20) for user_id; GlotPress uses int(10)
     
    119169) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    120170
     171CREATE TABLE `gp_user_projects` (
     172  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
     173  `user_id` int(10) unsigned NOT NULL,
     174  `project_id` int(11) unsigned NOT NULL,
     175  `locale` varchar(255) NOT NULL,
     176  `locale_slug` varchar(255) NOT NULL DEFAULT 'default',
     177  `last_modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
     178  PRIMARY KEY (`id`),
     179  UNIQUE KEY `user_project_locale` (`user_id`,`project_id`,`locale`,`locale_slug`)
     180) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    121181*/
Note: See TracChangeset for help on using the changeset viewer.