Ignore:
Timestamp:
08/12/2010 03:45:29 AM (22 months ago)
Author:
ofer
Message:

Slightly neater code of table names

File:
1 edited

Legend:

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

    r504 r505  
    3434    /** @var transposh_plugin father class */ 
    3535    private $transposh; 
     36    /** @var array holds prefetched translations */ 
    3637    private $translations; 
     38    /** @var string translation table name */ 
     39    private $translation_table; 
     40    /** @var string translation log table name */ 
     41    private $translation_log_table; 
    3742 
    3843    /** 
     
    4146    function transposh_database(&$transposh) { 
    4247        $this->transposh = &$transposh; 
     48        $this->translation_table = $GLOBALS['wpdb']->prefix . TRANSLATIONS_TABLE; 
     49        $this->translation_log_table = $GLOBALS['wpdb']->prefix . TRANSLATIONS_LOG; 
    4350    } 
    4451 
     
    140147        // If we have nothing, we will do nothing 
    141148        if (!$where) return; 
    142         $table_name = $GLOBALS['wpdb']->prefix . TRANSLATIONS_TABLE; 
    143         $query = "SELECT original, translated, source FROM $table_name WHERE ($where) and lang = '$lang' "; 
     149        $query = "SELECT original, translated, source FROM {$this->translation_table} WHERE ($where) and lang = '$lang' "; 
    144150        $rows = $GLOBALS['wpdb']->get_results($query, ARRAY_A); 
    145151        if (empty($rows)) return; 
     
    175181            logger("prefetch result for $original >>> {$this->translations[$original][0]} ({$this->translations[$original][1]})", 4); 
    176182        } else { 
    177  
    178             $table_name = $GLOBALS['wpdb']->prefix . TRANSLATIONS_TABLE; 
    179             $query = "SELECT * FROM $table_name WHERE original = '$original' and lang = '$lang' "; 
     183            $query = "SELECT * FROM {$this->translation_table} WHERE original = '$original' and lang = '$lang' "; 
    180184            $row = $GLOBALS['wpdb']->get_row($query); 
    181185 
     
    218222            logger("prefetch result for $translation >>> {$this->translations[$translation][0]} ({$this->translations[$translation][1]})", 3); 
    219223        } else { 
    220             $table_name = $GLOBALS['wpdb']->prefix . TRANSLATIONS_TABLE; 
    221             $query = "SELECT * FROM $table_name WHERE translated = '$translation' and lang = '$lang' "; 
     224            $query = "SELECT * FROM {$this->translation_table} WHERE translated = '$translation' and lang = '$lang' "; 
    222225            $row = $GLOBALS['wpdb']->get_row($query); 
    223226 
     
    333336            // If we have caching - we remove previous entry from cache 
    334337            $this->cache_delete($original, $lang); 
     338            // TODO - maybe store value here? 
    335339        } 
    336340 
     
    342346          VALUES $values"; */ 
    343347        //so we'll delete all values and insert them... 
    344         $update = "DELETE FROM " . $GLOBALS['wpdb']->prefix . TRANSLATIONS_TABLE . " WHERE $delvalues"; 
     348        $update = "DELETE FROM " . $this->translation_table . " WHERE $delvalues"; 
    345349        logger($update, 3); 
    346350        $result = $GLOBALS['wpdb']->query($update); 
    347         $update = "INSERT INTO " . $GLOBALS['wpdb']->prefix . TRANSLATIONS_TABLE . " (original, translated, lang, source) VALUES $values"; 
     351        $update = "INSERT INTO " . $this->translation_table . " (original, translated, lang, source) VALUES $values"; 
    348352        logger($update, 3); 
    349353        $result = $GLOBALS['wpdb']->query($update); 
     
    351355        // if the insertion worked, we will update the transaction log 
    352356        if ($result !== FALSE) { 
    353             $log = "INSERT INTO " . $GLOBALS['wpdb']->prefix . TRANSLATIONS_LOG . " (original, translated, lang, translated_by, source) " . 
     357            $log = "INSERT INTO " . $this->translation_log_table . " (original, translated, lang, translated_by, source) " . 
    354358                    "VALUES $logvalues"; 
    355359            $result = $GLOBALS['wpdb']->query($log); 
     
    403407        logger('Passed check for editable and translator', 4); 
    404408 
    405         $table_name = $GLOBALS['wpdb']->prefix . TRANSLATIONS_LOG; 
    406         logger("table is $table_name", 4); 
    407  
    408409        // The original content is encoded as base64 before it is sent (i.e. token), after we 
    409410        // decode it should just the same after it was parsed. 
     
    414415 
    415416        $query = "SELECT translated, translated_by, timestamp, source, user_login " . 
    416                 "FROM $table_name " . 
     417                "FROM {$this->translation_log_table} " . 
    417418                "LEFT JOIN {$GLOBALS['wpdb']->prefix}users ON translated_by = {$GLOBALS['wpdb']->prefix}users.id " . 
    418419                "WHERE original='$original' AND lang='$lang' " . 
     
    450451     */ 
    451452    function get_all_human_translation_history($date ="null", $limit = "") { 
    452  
    453         $table_name = $GLOBALS['wpdb']->prefix . TRANSLATIONS_LOG; 
    454         logger("table is $table_name", 4); 
    455  
    456453        if ($date != "null") 
    457454                $dateterm = "and UNIX_TIMESTAMP(timestamp) > $date"; 
    458455        if ($limit) $limitterm = "LIMIT $limit"; 
    459456        $query = "SELECT original, lang, translated, translated_by, UNIX_TIMESTAMP(timestamp) as timestamp " . 
    460                 "FROM $table_name " . 
     457                "FROM {$this->translation_log_table} " . 
    461458                "WHERE source= 0 $dateterm " . 
    462459                "ORDER BY timestamp ASC $limitterm"; 
     
    478475 
    479476        if ($installed_ver != DB_VERSION) { 
    480             $table_name = $GLOBALS['wpdb']->prefix . TRANSLATIONS_TABLE; 
    481  
    482             logger("Attempting to create table $table_name", 0); 
     477            logger("Attempting to create table {$this->translation_table}", 0); 
    483478            // notice - keep every field on a new line or dbdelta fails 
    484479            $GLOBALS['wpdb']->query("ALTER TABLE $table_name DROP PRIMARY KEY"); 
    485             $sql = "CREATE TABLE $table_name ( 
     480            $sql = "CREATE TABLE {$this->translation_table} ( 
    486481                    original TEXT NOT NULL,  
    487482                    lang CHAR(5) NOT NULL,  
     
    493488            dbDelta($sql); 
    494489 
    495             $table_name = $GLOBALS['wpdb']->prefix . TRANSLATIONS_LOG; 
    496  
    497             logger("Attempting to create table $table_name", 0); 
     490            logger("Attempting to create table {$this->translation_log_table}", 0); 
    498491            // notice - keep every field on a new line or dbdelta fails 
     492            // this should be removed in a far future... 
    499493            $GLOBALS['wpdb']->query("ALTER TABLE $table_name DROP PRIMARY KEY"); 
    500             $sql = "CREATE TABLE $table_name ( 
     494            $sql = "CREATE TABLE {$this->translation_log_table} ( 
    501495                    original text NOT NULL,  
    502496                    lang CHAR(5) NOT NULL,  
     
    520514    function db_stats() { 
    521515        echo "<h4>Database stats</h4>"; 
    522         $table_name = $GLOBALS['wpdb']->prefix . TRANSLATIONS_TABLE; 
    523         $log_table_name = $GLOBALS['wpdb']->prefix . TRANSLATIONS_LOG; 
    524         $query = "SELECT count(*) as count FROM `$table_name`"; 
     516        $query = "SELECT count(*) as count FROM `{$this->translation_table}`"; 
    525517        $rows = $GLOBALS['wpdb']->get_results($query); 
    526518        foreach ($rows as $row) { 
     
    529521        } 
    530522 
    531         $query = "SELECT count(*) as count,lang FROM `$table_name` WHERE source='0' GROUP BY `lang` ORDER BY `count` DESC LIMIT 3"; 
     523        $query = "SELECT count(*) as count,lang FROM `{$this->translation_table}` WHERE source='0' GROUP BY `lang` ORDER BY `count` DESC LIMIT 3"; 
    532524        $rows = $GLOBALS['wpdb']->get_results($query); 
    533525        foreach ($rows as $row) { 
     
    537529 
    538530        echo "<h4>Recent activity</h4>"; 
    539         $query = "SELECT * FROM `$log_table_name` WHERE source='0' ORDER BY `timestamp` DESC LIMIT 3"; 
     531        $query = "SELECT * FROM `{$this->translation_log_table}` WHERE source='0' ORDER BY `timestamp` DESC LIMIT 3"; 
    540532        $rows = $GLOBALS['wpdb']->get_results($query); 
    541533        foreach ($rows as $row) { 
     
    554546     */ 
    555547    function get_orignal_phrases_for_search_term($term, $language) { 
    556         $table_name = $GLOBALS['wpdb']->prefix . TRANSLATIONS_TABLE; 
    557548        $n = '%'; 
    558549        $term = addslashes_gpc($term); 
    559         $query = "SELECT original 
    560                         FROM `$table_name` 
    561                         WHERE `lang` LIKE '$language' 
    562                         AND `translated` LIKE '{$n}{$term}{$n}'"; 
     550        $query = "SELECT original" . 
     551                " FROM `{$this->translation_table}`" . 
     552                " WHERE `lang` LIKE '$language'" . 
     553                " AND `translated` LIKE '{$n}{$term}{$n}'"; 
    563554        //TODO wait for feedbacks to see if we should put a limit here. 
    564555 
     
    594585    function cleanup($days = 0) { 
    595586        $days = intval($days); // some security 
    596         $cleanup = 'DELETE ' . $GLOBALS['wpdb']->prefix . TRANSLATIONS_TABLE . ' ,' . $GLOBALS['wpdb']->prefix . TRANSLATIONS_LOG . 
    597                 ' FROM ' . $GLOBALS['wpdb']->prefix . TRANSLATIONS_TABLE . 
    598                 ' INNER JOIN ' . $GLOBALS['wpdb']->prefix . TRANSLATIONS_LOG . 
    599                 ' ON ' . $GLOBALS['wpdb']->prefix . TRANSLATIONS_TABLE . '.original = ' . $GLOBALS['wpdb']->prefix . TRANSLATIONS_LOG . '.original' . 
    600                 ' AND ' . $GLOBALS['wpdb']->prefix . TRANSLATIONS_TABLE . '.lang = ' . $GLOBALS['wpdb']->prefix . TRANSLATIONS_LOG . '.lang' . 
    601                 ' WHERE ' . $GLOBALS['wpdb']->prefix . TRANSLATIONS_TABLE . '.source > 0' . 
     587        $cleanup = 'DELETE ' . $this->translation_table . ' ,' . $this->translation_log_table . 
     588                ' FROM ' . $this->translation_table . 
     589                ' INNER JOIN ' . $this->translation_log_table . 
     590                ' ON ' . $this->translation_table . '.original = ' . $this->translation_log_table . '.original' . 
     591                ' AND ' . $this->translation_table . '.lang = ' . $this->translation_log_table . '.lang' . 
     592                ' WHERE ' . $this->translation_table . '.source > 0' . 
    602593                " AND timestamp < SUBDATE(NOW(),$days)"; 
    603594        $result = $GLOBALS['wpdb']->query($cleanup); 
     
    623614        $logvalues .= "('" . $original . "','" . $translation . "','" . $lang . "','" . $by . "',FROM_UNIXTIME(" . $timestamp . "),'" . $source . "')"; 
    624615 
    625         $update = "DELETE FROM " . $GLOBALS['wpdb']->prefix . TRANSLATIONS_TABLE . " WHERE $delvalues"; 
     616        $update = "DELETE FROM " . $this->translation_table . " WHERE $delvalues"; 
    626617        logger($update, 3); 
    627618        $result = $GLOBALS['wpdb']->query($update); 
    628         $update = "INSERT INTO " . $GLOBALS['wpdb']->prefix . TRANSLATIONS_TABLE . " (original, translated, lang, source) VALUES $values"; 
     619        $update = "INSERT INTO " . $this->translation_table . " (original, translated, lang, source) VALUES $values"; 
    629620        logger($update, 3); 
    630621        $result = $GLOBALS['wpdb']->query($update); 
     
    632623        if ($result !== FALSE) { 
    633624            // update the transaction log too 
    634             $log = "INSERT INTO " . $GLOBALS['wpdb']->prefix . TRANSLATIONS_LOG . " (original, translated, lang, translated_by, timestamp, source) " . 
     625            $log = "INSERT INTO " . $this->translation_log_table . " (original, translated, lang, translated_by, timestamp, source) " . 
    635626                    "VALUES $logvalues"; 
    636627            logger($log, 3); 
Note: See TracChangeset for help on using the changeset viewer.