public static function fix_duplicate_contributions()
 {
     global $wpdb;
     $contributions = self::find_duplicate_episode_contributions();
     if (!is_array($contributions) || empty($contributions)) {
         Repair::add_to_repair_log(__('Contributions did not need repair', 'podlove'));
         return;
     }
     foreach ($contributions as $contribution) {
         $sql = "\n\t\t\t\tDELETE FROM\n\t\t\t\t\t" . \Podlove\Modules\Contributors\Model\EpisodeContribution::table_name() . "\n\t\t\t\tWHERE\n\t\t\t\t\tid != " . $contribution['id'] . "\n\t\t\t\t\tAND `contributor_id` = \"" . $contribution['contributor_id'] . "\"\n\t\t\t\t\tAND `episode_id` = \"" . $contribution['episode_id'] . "\"\n\t\t\t\t\tAND `role_id` = \"" . $contribution['role_id'] . "\"\n\t\t\t\t\tAND `group_id` = \"" . $contribution['group_id'] . "\"\n\t\t\t\t";
         $wpdb->query($sql);
         $ec = \Podlove\Modules\Contributors\Model\EpisodeContribution::find_by_id($contribution['id']);
         $ec->save();
         // recalculates contribution count
     }
     Repair::add_to_repair_log(sprintf(_n('Deleted 1 duplicate contribution', 'Deleted %s duplicate contributions', count($contributions), 'podlove'), count($contributions)));
 }
 public static function fix_duplicate_services()
 {
     global $wpdb;
     $services = self::find_duplicate_services();
     if (!is_array($services) || empty($services)) {
         Repair::add_to_repair_log(__('Services did not need repair', 'podlove'));
         return;
     }
     foreach ($services as $service) {
         # update contributor services
         $sql = "UPDATE " . ContributorService::table_name() . " SET service_id = " . $service['id'] . " WHERE service_id IN (\n\t\t\t\tSELECT id FROM " . Service::table_name() . " WHERE `type` = \"" . $service['type'] . "\"\n\t\t\t)";
         $wpdb->query($sql);
         # update show services
         $sql = "UPDATE " . Model\ShowService::table_name() . " SET service_id = " . $service['id'] . " WHERE service_id IN (\n\t\t\t\tSELECT id FROM " . Service::table_name() . " WHERE `type` = \"" . $service['type'] . "\"\n\t\t\t)";
         $wpdb->query($sql);
         # delete obsolete services
         $sql = "DELETE FROM " . Service::table_name() . " WHERE id != " . $service['id'] . " AND `type` = \"" . $service['type'] . "\"";
         $wpdb->query($sql);
     }
     Repair::add_to_repair_log(sprintf(__('Consolidated duplicate services (%s)', 'podlove'), implode(', ', array_map(function ($s) {
         return $s['type'];
     }, $services))));
 }