Changeset 142


Ignore:
Timestamp:
03/30/2009 10:07:47 PM (3 years ago)
Author:
ofer
Message:

Prevent duplicate insertion to database, second insertion of same text will not change DB

Location:
trunk/WordPress/plugin/transposh
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/WordPress/plugin/transposh/core/parser.php

    r140 r142  
    566566 
    567567                        //Don't break on ` so for our use we don't consider it an entity 
    568                         //e.g. Jack`s apple.  
    569                         //Exception: don't break when we there is a white space after the apostrophe. e.g. `uncategorized`  
    570                         if(($entity ==  "’" || $entity == "'" || $entity == "'")  
     568                        //e.g. Jack`s apple. 
     569                        //Exception: don't break when we there is a white space after the apostrophe. e.g. `uncategorized` 
     570                        if(($entity ==  "’" || $entity == "'" || $entity == "'") 
    571571                            && $page[$end_pos + 1] != " ") 
    572572                        { 
     
    577577                                $is_breaker = TRUE; 
    578578                        } 
    579                          
     579 
    580580 
    581581                        //It is an html entity. 
     
    738738{ 
    739739        logger("Enter " . __METHOD__  . " : $start", 4); 
    740         global $page, $pos; 
     740        global $page, $pos, $lang; 
    741741 
    742742        //trim white space from the start position going forward 
     
    765765        } 
    766766 
    767         list($translated_text, $source) = fetch_translation($original_text); 
     767        list($translated_text, $source) = fetch_translation($original_text, $lang); 
    768768 
    769769        insert_translation($original_text, $translated_text, $source, $start, $end); 
     
    905905        $use_params = !$enable_permalinks_rewrite; 
    906906 
    907         //Allow specific override for url rewriting .  
     907        //Allow specific override for url rewriting . 
    908908        if($enable_permalinks_rewrite &&  function_exists('is_url_excluded_from_permalink_rewrite') && 
    909909           is_url_excluded_from_permalink_rewrite($href)) 
     
    911911                $use_params = TRUE; 
    912912        } 
    913          
     913 
    914914        $href = rewrite_url_lang_param($href, $lang, $is_edit_mode, $use_params); 
    915915 
  • trunk/WordPress/plugin/transposh/transposh_db.php

    r141 r142  
    4747 *   Will return NULL if no translation is available. 
    4848 */ 
    49 function fetch_translation($original) 
    50 { 
    51         global $wpdb, $lang; 
     49function fetch_translation($original, $lang) 
     50{ 
     51        global $wpdb; 
    5252        $translated = NULL; 
    5353        logger("Enter " . __METHOD__ . ": $original", 4); 
     
    9191                if($rc === TRUE) 
    9292                { 
    93                         logger("Stored in cache: $original => $translated", 3); 
     93                        logger("Stored in cache: $original => {$translated[0]},{$translated[1]}", 3); 
    9494                } 
    9595        } 
     
    112112        $source = $_POST['source']; 
    113113 
    114         logger("Enter " . __FILE__ . " Params: $original , $translation, $lang," . $ref, 3); 
     114        logger("Enter " . __FILE__ . " Params: $original , $translation, $lang, $ref", 3); 
    115115        if(!isset($original) || !isset($translation) || !isset($lang)) 
    116116        { 
     
    118118                return; 
    119119        } 
    120          
    121         //add  our own custom header - so we will know that we got here  
    122         header("Transposh: version_". DB_VERSION); 
    123          
     120 
     121        $table_name = $wpdb->prefix . TRANSLATIONS_TABLE; 
     122 
     123        //Decode & remove already escaped character to avoid double escaping 
     124        $translation = $wpdb->escape(htmlspecialchars(stripslashes(urldecode($translation)))); 
     125 
     126        //The original content is encoded as base64 before it is sent (i.e. token), after we 
     127        //decode it should just the same after it was parsed. 
     128        $original = $wpdb->escape(html_entity_decode($original, ENT_NOQUOTES, 'UTF-8')); 
     129 
     130        //add  our own custom header - so we will know that we got here 
     131        header("Transposh: ver-<%VERSION%> db_version-". DB_VERSION); 
     132 
     133        list($translated_text, $old_source) = fetch_translation($original, $lang); 
     134        if ($translated_text) { 
     135                if ($source == 1) { 
     136                        logger("Warning " . __METHOD__ . " auto-translation for already translated: $original", 0); 
     137                        return; 
     138                } 
     139                if ($translation == $wpdb->escape(htmlspecialchars(stripslashes(urldecode($translated_text)))) && $old_source == $source) { 
     140                        logger("Warning " . __METHOD__ . " attempt to retranslate with same text: $original, $translation", 0); 
     141                        return; 
     142                } 
     143        } 
     144 
    124145        //Check permissions, first the lanugage must be on the edit list. Then either the user 
    125         //is a translator or automatic translation if it is enabled.  
    126         if(!(is_editable_lang($lang) &&  
     146        //is a translator or automatic translation if it is enabled. 
     147        if(!(is_editable_lang($lang) && 
    127148            (is_translator() || ($source == 1 && get_option(ENABLE_AUTO_TRANSLATE))))) 
    128149        { 
     
    131152                exit; 
    132153        } 
    133  
    134         $table_name = $wpdb->prefix . TRANSLATIONS_TABLE; 
    135  
    136         //Decode & remove already escaped character to avoid double escaping 
    137         $translation = $wpdb->escape(htmlspecialchars(stripslashes(urldecode($translation)))); 
    138  
    139         //The original content is encoded as base64 before it is sent (i.e. token), after we 
    140         //decode it should just the same after it was parsed. 
    141         $original = $wpdb->escape(html_entity_decode($original, ENT_NOQUOTES, 'UTF-8')); 
    142154 
    143155        $update = "REPLACE INTO  $table_name (original, translated, lang, source) 
Note: See TracChangeset for help on using the changeset viewer.