| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This class records translation warnings. |
| 5 | */ |
| 6 | class WPorg_GP_Warning_Stats { |
| 7 | |
| 8 | private $warning_stats = array(); |
| 9 | |
| 10 | public function __construct() { |
| 11 | global $wpdb, $gp_table_prefix; |
| 12 | |
| 13 | add_action( 'gp_translation_created', array( $this, 'translation_updated' ) ); |
| 14 | add_action( 'gp_translation_saved', array( $this, 'translation_updated' ) ); |
| 15 | |
| 16 | // DB Writes are delayed until shutdown to bulk-update the stats during imports. |
| 17 | add_action( 'shutdown', array( $this, 'write_stats_to_database' ) ); |
| 18 | |
| 19 | $wpdb->dotorg_translation_warnings = $gp_table_prefix . 'translation_warnings'; |
| 20 | } |
| 21 | |
| 22 | public function translation_updated( $translation ) { |
| 23 | if ( ! $translation->warnings ) { |
| 24 | return; |
| 25 | } |
| 26 | |
| 27 | // We only want to trigger for strings which are live, or are for consideration. |
| 28 | if ( ! in_array( $translation->status, array( 'current', 'waiting' ) ) ) { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | $original = GP::$original->get( $translation->original_id ); |
| 33 | $translation_set = GP::$translation_set->get( $translation->translation_set_id ); |
| 34 | |
| 35 | $key = "{$translation->user_id},{$translation_set->locale},{$translation_set->slug},{$original->project_id},{$translation->id}"; |
| 36 | |
| 37 | foreach( $translation->warnings as $index => $warnings ) { |
| 38 | foreach ( $warnings as $warning_key => $warning ) { |
| 39 | $this->warning_stats[ $key ] = $warning_key; |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | |
| 45 | public function write_stats_to_database() { |
| 46 | global $wpdb; |
| 47 | |
| 48 | $now = current_time( 'mysql', 1 ); |
| 49 | |
| 50 | $values = array(); |
| 51 | foreach ( $this->warning_stats as $key => $warning ) { |
| 52 | list( $user_id, $locale, $locale_slug, $project_id, $translation_id ) = explode( ',', $key ); |
| 53 | |
| 54 | $values[] = $wpdb->prepare( '(%d, %s, %s, %d, %d, %d, %s, %s)', |
| 55 | $user_id, |
| 56 | $locale, |
| 57 | $locale_slug, |
| 58 | $project_id, |
| 59 | $translation_id, |
| 60 | $warning, |
| 61 | $now, |
| 62 | ); |
| 63 | |
| 64 | // If we're processing a large batch, add them as we go to avoid query lengths & memory limits. |
| 65 | if ( count( $values ) > 50 ) { |
| 66 | $wpdb->query( |
| 67 | "INSERT INTO {$wpdb->dotorg_translation_warnings} (`user_id`, `locale`, `locale_slug`, `project_id`, translation_id`, `warning`, `timestamp`) |
| 68 | VALUES " . implode( ', ', $values ) |
| 69 | ); |
| 70 | $values = array(); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | if ( $values ) { |
| 75 | $wpdb->query( |
| 76 | "INSERT INTO {$wpdb->dotorg_translation_warnings} (`user_id`, `locale`, `locale_slug`, `project_id`, translation_id`, `warning`, `timestamp`) |
| 77 | VALUES " . implode( ', ', $values ) |
| 78 | ); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | Table: |
| 85 | |
| 86 | CREATE TABLE `translate_translation_warnings` ( |
| 87 | `id` bigint(10) NOT NULL AUTO_INCREMENT, |
| 88 | `user_id` bigint(20) unsigned NOT NULL DEFAULT '0', |
| 89 | `locale` varchar(255) NOT NULL DEFAULT '', |
| 90 | `locale_slug` varchar(255) NOT NULL DEFAULT '', |
| 91 | `project_id` bigint(20) unsigned NOT NULL DEFAULT '0', |
| 92 | `translation_id` bigint(20) unsigned NOT NULL DEFAULT '0', |
| 93 | `warning` varchar(20) NOT NULL DEFAULT '', |
| 94 | `timestamp` datetime NOT NULL default '0000-00-00 00:00:00', |
| 95 | PRIMARY KEY (`id`), |
| 96 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1; |
| 97 | */ |