- Timestamp:
- 09/17/2015 03:05:18 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/translate.wordpress.org/includes/gp-plugins/wporg-project-stats.php
r1827 r1881 8 8 * The datbase update is delayed until shutdown to bulk-update the database during imports. 9 9 * 10 * NOTE: The counts includes all sub-projects in the count, as that's more useful for querying (top-level projects excluded) 11 * for example, wp-plugins won't exist, but wp-plugins/akismet will include wp-plugins/akismet/stable wp-plugins/akismet/stable-readme 12 * 10 13 * @author dd32 11 14 */ … … 13 16 var $id = 'wporg-project-stats'; 14 17 15 private $projects_to_update = array(); 16 private $translation_sets_to_update = array(); 18 public $projects_to_update = array(); 17 19 18 20 function __construct() { … … 30 32 31 33 function translation_created( $translation ) { 32 $this->translation_sets_to_update[ $translation->translation_set_id ] = true; 34 $set = GP::$translation_set->get( $translation->translation_set_id ); 35 $this->projects_to_update[ $set->project_id ][ $set->locale . '/' . $set->slug ] = true; 33 36 } 34 37 35 38 function translation_saved( $translation ) { 36 $this->translation_sets_to_update[ $translation->translation_set_id ] = true; 39 $set = GP::$translation_set->get( $translation->translation_set_id ); 40 $this->projects_to_update[ $set->project_id ][ $set->locale . '/' . $set->slug ] = true; 37 41 } 38 42 … … 41 45 } 42 46 47 // Counts up all the 48 function get_project_translation_counts( $project_id, $locale, $locale_slug, &$counts = array() ) { 49 if ( ! $counts ) { 50 $counts = array( 'all' => 0, 'current' => 0, 'waiting' => 0, 'fuzzy' => 0, 'warning' => 0, 'untranslated' => 0 ); 51 } 52 53 // Not all projects have translation sets 54 $set = GP::$translation_set->by_project_id_slug_and_locale( $project_id, $locale_slug, $locale ); 55 if ( $set ) { 56 // Force a refresh of the translation set counts 57 wp_cache_delete( $set->id, 'translation_set_status_breakdown' ); 58 59 $counts['all'] += $set->all_count(); 60 $counts['current'] += $set->current_count(); 61 $counts['waiting'] += $set->waiting_count(); 62 $counts['fuzzy'] += $set->fuzzy_count(); 63 $counts['warnings'] += $set->warnings_count(); 64 $counts['untranslated'] += $set->untranslated_count(); 65 } 66 67 // Fetch the strings from the sub projects too 68 foreach ( GP::$project->get( $project_id )->sub_projects() as $project ) { 69 if ( ! $project->active ) { 70 continue; 71 } 72 $this->get_project_translation_counts( $project->id, $locale, $locale_slug, $counts ); 73 } 74 75 return $counts; 76 } 77 43 78 function shutdown() { 44 79 global $gpdb; 45 80 $values = array(); 46 81 47 // Convert projects to a list of sets 48 foreach ( $this->projects_to_update as $project_id => $dummy ) { 49 foreach ( GP::$translation_set->by_project_id( $project_id ) as $set ) { 50 $this->translation_sets_to_update[ $set->id ] = true; 82 // If a project is `true` then we need to fetch all translation sets for it. 83 foreach ( $this->projects_to_update as $project_id => $set_data ) { 84 if ( true === $set_data ) { 85 $this->projects_to_update[ $project_id ] = array(); 86 foreach ( GP::$translation_set->by_project_id( $project_id ) as $set ) { 87 $this->projects_to_update[ $project_id ][ $set->locale . '/' . $set->slug ] = true; 88 } 51 89 } 52 unset( $this->projects_to_update[ $project_id ] );90 53 91 } 54 92 55 foreach ( $this->translation_sets_to_update as $set_id => $dummy ) { 56 $set = GP::$translation_set->get( $set_id ); 93 // Update all parent projects as well. 94 // This does NOT update a root parent (ie. ! $parent_project_id) as we use those as grouping categories. 95 $projects = $this->projects_to_update; 96 foreach ( $projects as $project_id => $data ) { 97 // Do all parents 98 $project = GP::$project->get( $project_id ); 99 while ( $project ) { 100 $project = GP::$project->get( $project->parent_project_id ); 101 if ( $project->parent_project_id ) { 102 $projects[ $project->id ] = $data; 103 } else { 104 break; 105 } 106 } 107 } 108 $this->projects_to_update += $projects; 57 109 58 unset( $this->translation_sets_to_update[ $set_id ] ); 110 foreach ( $this->projects_to_update as $project_id => $locale_sets ) { 111 $locale_sets = array_keys( $locale_sets ); 112 $locale_sets = array_map( function( $set ) { return explode( '/', $set ); }, $locale_sets ); 59 113 60 if ( ! $set ) { 61 continue; 114 foreach ( $locale_sets as $locale_set ) { 115 list( $locale, $locale_slug ) = $locale_set; 116 $counts = $this->get_project_translation_counts( $project_id, $locale, $locale_slug ); 117 118 $values[] = $gpdb->prepare( '(%d, %s, %s, %d, %d, %d, %d, %d, %d)', 119 $project_id, 120 $locale, 121 $locale_slug, 122 $counts['all'], 123 $counts['current'], 124 $counts['waiting'], 125 $counts['fuzzy'], 126 $counts['warning'], 127 $counts['untranslated'] 128 ); 62 129 } 63 130 64 $values[] = $gpdb->prepare( '(%d, %d, %d, %d, %d, %d, %d, %d)', 65 $set->project_id, 66 $set->id, 67 $set->all_count(), 68 $set->current_count(), 69 $set->waiting_count(), 70 $set->fuzzy_count(), 71 $set->warnings_count(), 72 // Untranslated is ( all - current ), we really want ( all - current - waiting - fuzzy ) which is (untranslated - waiting - fuzzy ) 73 $set->untranslated_count() - $set->waiting_count() - $set->fuzzy_count() 74 ); 75 131 // If we're processing a large batch, add them as we go to avoid query lengths & memory limits 76 132 if ( count( $values ) > 50 ) { 77 // If we're processing a large batch, add them as we go to avoid query lengths & memoryl imits 78 $gpdb->query( "INSERT INTO {$gpdb->project_translation_status} (`project_id`, `translation_set_id`, `all`, `current`, `waiting`, `fuzzy`, `warnings`, `untranslated` ) VALUES " . implode( ', ', $values ) . " ON DUPLICATE KEY UPDATE `all`=VALUES(`all`), `current`=VALUES(`current`), `waiting`=VAlUES(`waiting`), `fuzzy`=VALUES(`fuzzy`), `warnings`=VALUES(`warnings`), `untranslated`=VALUES(`untranslated`)" ); 133 $gpdb->query( "INSERT INTO {$gpdb->project_translation_status} (`project_id`, `locale`, `locale_slug`, `all`, `current`, `waiting`, `fuzzy`, `warnings`, `untranslated` ) VALUES " . implode( ', ', $values ) . " ON DUPLICATE KEY UPDATE `all`=VALUES(`all`), `current`=VALUES(`current`), `waiting`=VAlUES(`waiting`), `fuzzy`=VALUES(`fuzzy`), `warnings`=VALUES(`warnings`), `untranslated`=VALUES(`untranslated`)" ); 79 134 $values = array(); 80 135 } … … 82 137 83 138 if ( $values ) { 84 $gpdb->query( "INSERT INTO {$gpdb->project_translation_status} (`project_id`, ` translation_set_id`, `all`, `current`, `waiting`, `fuzzy`, `warnings`, `untranslated` ) VALUES " . implode( ', ', $values ) . " ON DUPLICATE KEY UPDATE `all`=VALUES(`all`), `current`=VALUES(`current`), `waiting`=VALUES(`waiting`), `fuzzy`=VALUES(`fuzzy`), `warnings`=VALUES(`warnings`), `untranslated`=VALUES(`untranslated`)" );139 $gpdb->query( "INSERT INTO {$gpdb->project_translation_status} (`project_id`, `locale`, `locale_slug`, `all`, `current`, `waiting`, `fuzzy`, `warnings`, `untranslated` ) VALUES " . implode( ', ', $values ) . " ON DUPLICATE KEY UPDATE `all`=VALUES(`all`), `current`=VALUES(`current`), `waiting`=VALUES(`waiting`), `fuzzy`=VALUES(`fuzzy`), `warnings`=VALUES(`warnings`), `untranslated`=VALUES(`untranslated`)" ); 85 140 } 86 141 } … … 95 150 `id` int(10) unsigned NOT NULL AUTO_INCREMENT, 96 151 `project_id` int(10) unsigned NOT NULL, 97 `translation_set_id` int(10) unsigned NOT NULL, 152 `locale` varchar(10) NOT NULL, 153 `locale_slug` varchar(255) NOT NULL, 98 154 `all` int(10) unsigned NOT NULL DEFAULT '0', 99 155 `current` int(10) unsigned NOT NULL DEFAULT '0', … … 103 159 `untranslated` int(10) unsigned NOT NULL DEFAULT '0', 104 160 PRIMARY KEY (`id`), 105 UNIQUE KEY `project_ translation_set` (`project_id`,`translation_set_id`),161 UNIQUE KEY `project_locale` (`project_id`,`locale`,`locale_slug`), 106 162 KEY `all` (`all`), 107 163 KEY `current` (`current`), … … 113 169 114 170 */ 171
Note: See TracChangeset
for help on using the changeset viewer.