WordPress.org

GlotPress

Ticket #173: 173.0.patch

File 173.0.patch, 3.2 KB (added by petervanderdoes, 2 years ago)

Fixes the reimporting problem

  • gp-includes/things/original.php

     
    11<?php 
    22class GP_Original extends GP_Thing { 
    3          
     3 
    44        var $table_basename = 'originals'; 
    55        var $field_names = array( 'id', 'project_id', 'context', 'singular', 'plural', 'references', 'comment', 'status', 'priority', 'date_added' ); 
    66        var $non_updatable_attributes = array( 'id', 'path' ); 
    7          
     7 
    88    static $priorities = array( '-2' => 'hidden', '-1' => 'low', '0' => 'normal', '1' => 'high' ); 
    99        static $count_cache_group = 'active_originals_count_by_project_id'; 
    1010 
     
    2525                } 
    2626                return $args; 
    2727        } 
    28          
     28 
    2929        function by_project_id( $project_id ) { 
    3030                return $this->many( "SELECT * FROM $this->table WHERE project_id= %d AND status = '+active'", $project_id ); 
    3131        } 
     
    3333        function count_by_project_id( $project_id ) { 
    3434                if ( false !== ( $cached = wp_cache_get( $project_id, self::$count_cache_group ) ) ) { 
    3535                        return $cached; 
    36                 }  
     36                } 
    3737                $count = $this->value( "SELECT COUNT(*) FROM $this->table WHERE project_id= %d AND status = '+active'", $project_id ); 
    3838                wp_cache_set( $project_id, $count, self::$count_cache_group ); 
    3939                return $count; 
     
    5252                $where = implode( ' AND ', $where ); 
    5353                return $this->one( "SELECT * FROM $this->table WHERE $where", $entry->context, $entry->singular, $entry->plural, $project_id ); 
    5454        } 
    55          
     55 
    5656        function import_for_project( $project, $translations ) { 
    5757                global $gpdb; 
    5858                wp_cache_delete( $project->id, self::$count_cache_group ); 
    5959                $originals_added = $originals_existing = 0; 
    6060                $all_originals_for_project = $this->many_no_map( "SELECT * FROM $this->table WHERE project_id= %d", $project->id ); 
    61                 $this->update( array( 'status' => '+obsolete' ), array( 'project_id' => $project->id, 'status' => '+active' ) ); 
    6261                $originals_by_key = array(); 
    6362                foreach( $all_originals_for_project as $original ) { 
    6463                        $entry = new Translation_Entry( array( 'singular' => $original->singular, 'plural' => $original->plural, 'context' => $original->context ) ); 
     
    6968                        $data = array('project_id' => $project->id, 'context' => $entry->context, 'singular' => $entry->singular, 
    7069                                'plural' => $entry->plural, 'comment' => $entry->extracted_comments, 
    7170                                'references' => implode( ' ', $entry->references ), 'status' => '+active' ); 
    72                                  
     71 
    7372                        // TODO: do not obsolete similar translations 
    7473                        $original = $originals_by_key[$entry->key()]; 
    7574                        if ( isset( $original ) ) { 
     
    8281                                $originals_added++; 
    8382                        } 
    8483                } 
    85                 $this->update( array('status' => '-obsolete'), array('project_id' => $project->id, 'status' => '+obsolete')); 
     84                // Run over the originals and if the translation doesn't exists in the newly imported translatio, make the entry obsolete 
     85        foreach ( $originals_by_key as $key => $value) { 
     86            if ( !key_exists($key, $translations->entries ) ) { 
     87                        $this->update( array('status' => '-obsolete'), array( 'id' => $original->id ) ); 
     88            } 
     89        } 
    8690                return array( $originals_added, $originals_existing ); 
    8791        } 
    88          
     92 
    8993        function should_be_updated_with( $original, $data ) { 
    9094                foreach( $data as $field => $value ) { 
    9195                        if ( $original->$field != $value ) return true;