Changeset 492


Ignore:
Timestamp:
08/08/2010 03:15:28 PM (18 months ago)
Author:
ofer
Message:

Committed preliminary restore facility for backup

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

Legend:

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

    r490 r492  
    719719 
    720720    /** 
     721     * Runs a restore 
     722     */ 
     723    function run_restore() { 
     724        logger('restoring..'); 
     725        $my_transposh_backup = new transposh_backup($this); 
     726        $my_transposh_backup->do_restore(); 
     727    } 
     728 
     729    /** 
    721730     * Adding the comment meta language, for later use in display 
    722731     * TODO: can use the language detection feature of some translation engines 
  • trunk/WordPress/plugin/transposh/wp/transposh_ajax.php

    r491 r492  
    101101    $my_transposh_plugin->run_backup(); 
    102102} 
     103// Start restore on demand 
     104elseif (isset($_GET['restore'])) { 
     105    $my_transposh_plugin->run_restore(); 
     106} 
    103107// Start cleanup on demand 
    104108elseif (isset($_GET['cleanup'])) { 
  • trunk/WordPress/plugin/transposh/wp/transposh_backup.php

    r437 r492  
    2525 
    2626define('TRANSPOSH_BACKUP_SERVICE_URL', 'http://svc.transposh.org/backup'); 
     27define('TRANSPOSH_RESTORE_SERVICE_URL', 'http://svc.transposh.org/restore'); 
    2728 
    2829//define ("TRANSPOSH_BACKUP_SERVICE_URL" , "http://ofergen:8888/backup"); 
     
    3536//constructor of class, PHP4 compatible construction for backward compatibility 
    3637    function transposh_backup(&$transposh) { 
    37         $this->transposh = &$transposh; 
     38        $this->transposh = &$transposh; 
    3839    } 
    3940 
    4041    function do_backup() { 
    41         $body = array(); 
    42         $body["home_url"] = $this->transposh->home_url; 
    43         $body["key"] = $this->transposh->options->get_transposh_key(); 
    44         //Check if there are thing to backup, before even accessing the service 
    45         $rowstosend = $this->transposh->database->get_all_human_translation_history('null', 1); 
    46         if (empty($rowstosend)) { 
    47             echo "500 - No human translations to backup."; 
    48             return; 
    49         } 
     42        $body = array(); 
     43        $body['home_url'] = $this->transposh->home_url; 
     44        $body['key'] = $this->transposh->options->get_transposh_key(); 
     45        //Check if there are thing to backup, before even accessing the service 
     46        $rowstosend = $this->transposh->database->get_all_human_translation_history('null', 1); 
     47        if (empty($rowstosend)) { 
     48            echo "500 - No human translations to backup."; 
     49            return; 
     50        } 
    5051 
    51         $result = wp_remote_post(TRANSPOSH_BACKUP_SERVICE_URL, array('body' => $body)); 
    52         if (is_wp_error($result)) { 
    53             echo '500 - ' . $result->get_error_message(); 
    54             return; 
    55         } 
    56         if ($result['headers']['fail']) { 
    57             echo '500 - ' . $result['headers']['fail']; 
    58             return; 
    59         } 
    60         if ($this->transposh->options->get_transposh_key() == "") { 
    61             $this->transposh->options->set_transposh_key($result['headers']['transposh-key']); 
    62             // TODO: deliever new gottenkey to client side? 
    63             //echo $this->transposh->options->get_transposh_key(); 
    64             $this->transposh->options->update_options(); 
    65         } 
    66         if ($result['headers']['lastitem']) { 
    67             $rowstosend = $this->transposh->database->get_all_human_translation_history($result['headers']['lastitem'], 500); 
    68             while ($rowstosend) { 
    69                 $item = 0; 
    70                 foreach ($rowstosend as $row) { 
    71                     if ($body['or' . ($item - 1)] != $row->original) 
    72                             $body['or' . $item] = $row->original; 
    73                     if ($body['ln' . ($item - 1)] != $row->lang) 
    74                             $body['ln' . $item] = $row->lang; 
    75                     if ($body['tr' . ($item - 1)] != $row->translated) 
    76                             $body['tr' . $item] = $row->translated; 
    77                     if ($body['tb' . ($item - 1)] != $row->translated_by) 
    78                             $body['tb' . $item] = $row->translated_by; 
    79                     if ($body['ts' . ($item - 1)] != $row->timestamp) 
    80                             $body['ts' . $item] = $row->timestamp; 
    81                     $item++; 
    82                 } 
    83                 $body['items'] = $item; 
    84                 // no need to post 0 items 
    85                 if ($item == 0) return; 
    86                 $result = wp_remote_post(TRANSPOSH_BACKUP_SERVICE_URL, array('body' => $body)); 
    87                 if (is_wp_error($result)) { 
    88                     echo "500 - " . $result->get_error_message(); 
    89                     return; 
    90                 } 
    91                 if ($result['headers']['fail']) { 
    92                     echo "500 - " . $result['headers']['fail']; 
    93                     return; 
    94                 } 
    95                 $rowstosend = $this->transposh->database->get_all_human_translation_history($row->timestamp, 500); 
    96             } 
    97         } 
    98         Echo '200 - backup in sync'; 
     52        $result = wp_remote_post(TRANSPOSH_BACKUP_SERVICE_URL, array('body' => $body)); 
     53        if (is_wp_error($result)) { 
     54            echo '500 - ' . $result->get_error_message(); 
     55            return; 
     56        } 
     57        if ($result['headers']['fail']) { 
     58            echo '500 - ' . $result['headers']['fail']; 
     59            return; 
     60        } 
     61        if ($this->transposh->options->get_transposh_key() == "") { 
     62            $this->transposh->options->set_transposh_key($result['headers']['transposh-key']); 
     63            // TODO: deliever new gottenkey to client side? 
     64            //echo $this->transposh->options->get_transposh_key(); 
     65            $this->transposh->options->update_options(); 
     66        } 
     67        if ($result['headers']['lastitem']) { 
     68            $rowstosend = $this->transposh->database->get_all_human_translation_history($result['headers']['lastitem'], 500); 
     69            while ($rowstosend) { 
     70                $item = 0; 
     71                foreach ($rowstosend as $row) { 
     72                    if ($body['or' . ($item - 1)] != $row->original) 
     73                            $body['or' . $item] = $row->original; 
     74                    if ($body['ln' . ($item - 1)] != $row->lang) 
     75                            $body['ln' . $item] = $row->lang; 
     76                    if ($body['tr' . ($item - 1)] != $row->translated) 
     77                            $body['tr' . $item] = $row->translated; 
     78                    if ($body['tb' . ($item - 1)] != $row->translated_by) 
     79                            $body['tb' . $item] = $row->translated_by; 
     80                    if ($body['ts' . ($item - 1)] != $row->timestamp) 
     81                            $body['ts' . $item] = $row->timestamp; 
     82                    $item++; 
     83                } 
     84                $body['items'] = $item; 
     85                // no need to post 0 items 
     86                if ($item == 0) return; 
     87                $result = wp_remote_post(TRANSPOSH_BACKUP_SERVICE_URL, array('body' => $body)); 
     88                if (is_wp_error($result)) { 
     89                    echo "500 - " . $result->get_error_message(); 
     90                    return; 
     91                } 
     92                if ($result['headers']['fail']) { 
     93                    echo "500 - " . $result['headers']['fail']; 
     94                    return; 
     95                } 
     96                $rowstosend = $this->transposh->database->get_all_human_translation_history($row->timestamp, 500); 
     97            } 
     98        } 
     99        Echo '200 - backup in sync'; 
     100    } 
     101 
     102    function do_restore() { 
     103        $body['to'] = time(); //TODO: fix this to get from DB 
     104        $body['home_url'] = $this->transposh->home_url; 
     105        $body['key'] = $this->transposh->options->get_transposh_key(); 
     106        $result = wp_remote_get(TRANSPOSH_RESTORE_SERVICE_URL . "?to={$body['to']}&key={$body['key']}&home_url={$body['home_url']}"); // gotta be a better way... 
     107        $lines = split("[\n|\r]", $result['body']); 
     108        foreach ($lines as $line) { 
     109            $trans = split(',', $line); 
     110            if ($trans[0]) 
     111            $this->transposh->database->restore_translation($trans[0], $trans[1], $trans[2], $trans[3], $trans[4]); 
     112        } 
     113        apc_clear_cache('user'); // clean up cache so that results will actually show 
     114        exit; 
    99115    } 
    100116 
  • trunk/WordPress/plugin/transposh/wp/transposh_db.php

    r491 r492  
    580580    } 
    581581 
     582    function restore_translation($original,$lang,$translation,$by,$timestamp) { 
     583        // TODO in future 
     584        // if there is a newer human translation, just ignore this 
     585        // if there is a newer auto translation, remove it 
     586        // update it 
     587        $source = 0; 
     588        // for now - just update it... 
     589        $values .= "('" . $original . "','" . $translation . "','" . $lang . "','" . $source . "')"; 
     590        $delvalues .= "(original ='$original' AND lang='$lang')"; 
     591       // Setting the transaction log records 
     592        $logvalues .= "('" . $original . "','" . $translation . "','" . $lang . "','" . $by . "',FROM_UNIXTIME(" . $timestamp . "),'" . $source . "')" ; 
     593 
     594        $update = "DELETE FROM " . $GLOBALS['wpdb']->prefix . TRANSLATIONS_TABLE . " WHERE $delvalues"; 
     595        logger($update, 3); 
     596        $result = $GLOBALS['wpdb']->query($update); 
     597        $update = "INSERT INTO " . $GLOBALS['wpdb']->prefix . TRANSLATIONS_TABLE . " (original, translated, lang, source) VALUES $values"; 
     598        logger($update, 3); 
     599        $result = $GLOBALS['wpdb']->query($update); 
     600 
     601        if ($result !== FALSE) { 
     602            // update the transaction log too 
     603            $log = "INSERT INTO " . $GLOBALS['wpdb']->prefix . TRANSLATIONS_LOG . " (original, translated, lang, translated_by, timestamp, source) " . 
     604                    "VALUES $logvalues"; 
     605            logger($log, 3); 
     606            $result = $GLOBALS['wpdb']->query($log); 
     607        } else { 
     608            logger(mysql_error(), 0); 
     609            logger("Error !!! failed to insert to db $original , $translation, $lang,", 0); 
     610            header("HTTP/1.0 404 Failed to update language database " . mysql_error()); 
     611        } 
     612    } 
     613 
    582614} 
    583615?> 
Note: See TracChangeset for help on using the changeset viewer.